Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
ResNet50_tensorflow
Commits
67446adb
"git@developer.sourcefind.cn:OpenDAS/torch-sparce.git" did not exist on "bd91e28503351b766e236fab81d01d06ed09c67f"
Commit
67446adb
authored
Jul 04, 2022
by
Gunho Park
Browse files
Make both input types work
parent
38a5d626
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
103 additions
and
44 deletions
+103
-44
official/projects/detr/configs/detr.py
official/projects/detr/configs/detr.py
+72
-5
official/projects/detr/dataloaders/coco.py
official/projects/detr/dataloaders/coco.py
+0
-1
official/projects/detr/dataloaders/detr_input.py
official/projects/detr/dataloaders/detr_input.py
+3
-9
official/projects/detr/tasks/detection.py
official/projects/detr/tasks/detection.py
+28
-29
No files found.
official/projects/detr/configs/detr.py
View file @
67446adb
...
@@ -58,6 +58,7 @@ class DataConfig(cfg.DataConfig):
...
@@ -58,6 +58,7 @@ class DataConfig(cfg.DataConfig):
@
dataclasses
.
dataclass
@
dataclasses
.
dataclass
class
Losses
(
hyperparams
.
Config
):
class
Losses
(
hyperparams
.
Config
):
class_offset
:
int
=
0
lambda_cls
:
float
=
1.0
lambda_cls
:
float
=
1.0
lambda_box
:
float
=
5.0
lambda_box
:
float
=
5.0
lambda_giou
:
float
=
2.0
lambda_giou
:
float
=
2.0
...
@@ -101,7 +102,7 @@ def detr_coco() -> cfg.ExperimentConfig:
...
@@ -101,7 +102,7 @@ def detr_coco() -> cfg.ExperimentConfig:
decay_at
=
train_steps
-
100
*
num_steps_per_epoch
# 400 epochs
decay_at
=
train_steps
-
100
*
num_steps_per_epoch
# 400 epochs
config
=
cfg
.
ExperimentConfig
(
config
=
cfg
.
ExperimentConfig
(
task
=
DetrTask
(
task
=
DetrTask
(
init_checkpoint
=
'gs://
ghpark-imagenet-tfrecord/ckpt
/resnet50_imagenet'
,
init_checkpoint
=
'gs://
tf_model_garden/vision
/resnet50_imagenet
/ckpt-62400
'
,
init_checkpoint_modules
=
'backbone'
,
init_checkpoint_modules
=
'backbone'
,
model
=
Detr
(
model
=
Detr
(
num_classes
=
81
,
num_classes
=
81
,
...
@@ -109,7 +110,6 @@ def detr_coco() -> cfg.ExperimentConfig:
...
@@ -109,7 +110,6 @@ def detr_coco() -> cfg.ExperimentConfig:
norm_activation
=
common
.
NormActivation
(
use_sync_bn
=
False
)),
norm_activation
=
common
.
NormActivation
(
use_sync_bn
=
False
)),
losses
=
Losses
(),
losses
=
Losses
(),
train_data
=
coco
.
COCODataConfig
(
train_data
=
coco
.
COCODataConfig
(
file_type
=
'tfrecord'
,
tfds_name
=
'coco/2017'
,
tfds_name
=
'coco/2017'
,
tfds_split
=
'train'
,
tfds_split
=
'train'
,
is_training
=
True
,
is_training
=
True
,
...
@@ -117,7 +117,6 @@ def detr_coco() -> cfg.ExperimentConfig:
...
@@ -117,7 +117,6 @@ def detr_coco() -> cfg.ExperimentConfig:
shuffle_buffer_size
=
1000
,
shuffle_buffer_size
=
1000
,
),
),
validation_data
=
coco
.
COCODataConfig
(
validation_data
=
coco
.
COCODataConfig
(
file_type
=
'tfrecord'
,
tfds_name
=
'coco/2017'
,
tfds_name
=
'coco/2017'
,
tfds_split
=
'validation'
,
tfds_split
=
'validation'
,
is_training
=
False
,
is_training
=
False
,
...
@@ -159,7 +158,7 @@ def detr_coco() -> cfg.ExperimentConfig:
...
@@ -159,7 +158,7 @@ def detr_coco() -> cfg.ExperimentConfig:
])
])
return
config
return
config
COCO_INPUT_PATH_BASE
=
'
gs://ghpark-tfrecords/coco
'
COCO_INPUT_PATH_BASE
=
''
COCO_TRAIN_EXAMPLES
=
118287
COCO_TRAIN_EXAMPLES
=
118287
COCO_VAL_EXAMPLES
=
5000
COCO_VAL_EXAMPLES
=
5000
...
@@ -173,7 +172,7 @@ def detr_coco() -> cfg.ExperimentConfig:
...
@@ -173,7 +172,7 @@ def detr_coco() -> cfg.ExperimentConfig:
decay_at
=
train_steps
-
100
*
steps_per_epoch
# 200 epochs
decay_at
=
train_steps
-
100
*
steps_per_epoch
# 200 epochs
config
=
cfg
.
ExperimentConfig
(
config
=
cfg
.
ExperimentConfig
(
task
=
DetrTask
(
task
=
DetrTask
(
init_checkpoint
=
'gs://
ghpark-imagenet-tfrecord/ckpt
/resnet50_imagenet'
,
init_checkpoint
=
'gs://
tf_model_garden/vision
/resnet50_imagenet
/ckpt-62400
'
,
init_checkpoint_modules
=
'backbone'
,
init_checkpoint_modules
=
'backbone'
,
annotation_file
=
os
.
path
.
join
(
COCO_INPUT_PATH_BASE
,
annotation_file
=
os
.
path
.
join
(
COCO_INPUT_PATH_BASE
,
'instances_val2017.json'
),
'instances_val2017.json'
),
...
@@ -227,3 +226,71 @@ def detr_coco() -> cfg.ExperimentConfig:
...
@@ -227,3 +226,71 @@ def detr_coco() -> cfg.ExperimentConfig:
'task.train_data.is_training != None'
,
'task.train_data.is_training != None'
,
])
])
return
config
return
config
@
exp_factory
.
register_config_factory
(
'detr_coco_tfds'
)
def
detr_coco
()
->
cfg
.
ExperimentConfig
:
"""Config to get results that matches the paper."""
train_batch_size
=
64
eval_batch_size
=
64
steps_per_epoch
=
COCO_TRAIN_EXAMPLES
//
train_batch_size
train_steps
=
300
*
steps_per_epoch
# 300 epochs
decay_at
=
train_steps
-
100
*
steps_per_epoch
# 200 epochs
config
=
cfg
.
ExperimentConfig
(
task
=
DetrTask
(
init_checkpoint
=
'gs://tf_model_garden/vision/resnet50_imagenet/ckpt-62400'
,
init_checkpoint_modules
=
'backbone'
,
model
=
Detr
(
num_classes
=
81
,
input_size
=
[
1333
,
1333
,
3
],
norm_activation
=
common
.
NormActivation
(
use_sync_bn
=
False
)),
losses
=
Losses
(
class_offset
=
1
),
train_data
=
DataConfig
(
tfds_name
=
'coco/2017'
,
tfds_split
=
'train'
,
is_training
=
True
,
global_batch_size
=
train_batch_size
,
shuffle_buffer_size
=
1000
,
),
validation_data
=
DataConfig
(
tfds_name
=
'coco/2017'
,
tfds_split
=
'validation'
,
is_training
=
False
,
global_batch_size
=
eval_batch_size
,
drop_remainder
=
False
)
),
trainer
=
cfg
.
TrainerConfig
(
train_steps
=
train_steps
,
validation_steps
=
COCO_VAL_EXAMPLES
//
eval_batch_size
,
steps_per_loop
=
steps_per_epoch
,
summary_interval
=
steps_per_epoch
,
checkpoint_interval
=
steps_per_epoch
,
validation_interval
=
5
*
steps_per_epoch
,
max_to_keep
=
1
,
best_checkpoint_export_subdir
=
'best_ckpt'
,
best_checkpoint_eval_metric
=
'AP'
,
optimizer_config
=
optimization
.
OptimizationConfig
({
'optimizer'
:
{
'type'
:
'detr_adamw'
,
'detr_adamw'
:
{
'weight_decay_rate'
:
1e-4
,
'global_clipnorm'
:
0.1
,
# Avoid AdamW legacy behavior.
'gradient_clip_norm'
:
0.0
}
},
'learning_rate'
:
{
'type'
:
'stepwise'
,
'stepwise'
:
{
'boundaries'
:
[
decay_at
],
'values'
:
[
0.0001
,
1.0e-05
]
}
},
})
),
restrictions
=
[
'task.train_data.is_training != None'
,
])
return
config
\ No newline at end of file
official/projects/detr/dataloaders/coco.py
View file @
67446adb
...
@@ -27,7 +27,6 @@ from official.vision.ops import preprocess_ops
...
@@ -27,7 +27,6 @@ from official.vision.ops import preprocess_ops
@
dataclasses
.
dataclass
@
dataclasses
.
dataclass
class
COCODataConfig
(
cfg
.
DataConfig
):
class
COCODataConfig
(
cfg
.
DataConfig
):
"""Data config for COCO."""
"""Data config for COCO."""
file_type
:
str
=
'tfrecord'
output_size
:
Tuple
[
int
,
int
]
=
(
1333
,
1333
)
output_size
:
Tuple
[
int
,
int
]
=
(
1333
,
1333
)
max_num_boxes
:
int
=
100
max_num_boxes
:
int
=
100
resize_scales
:
Tuple
[
int
,
...]
=
(
resize_scales
:
Tuple
[
int
,
...]
=
(
...
...
official/projects/detr/dataloaders/detr_input.py
View file @
67446adb
...
@@ -31,34 +31,28 @@ class Parser(parser.Parser):
...
@@ -31,34 +31,28 @@ class Parser(parser.Parser):
"""Parse an image and its annotations into a dictionary of tensors."""
"""Parse an image and its annotations into a dictionary of tensors."""
def
__init__
(
self
,
def
__init__
(
self
,
class_offset
:
int
=
0
,
output_size
:
Tuple
[
int
,
int
]
=
(
1333
,
1333
),
output_size
:
Tuple
[
int
,
int
]
=
(
1333
,
1333
),
max_num_boxes
:
int
=
100
,
max_num_boxes
:
int
=
100
,
resize_scales
:
Tuple
[
int
,
...]
=
RESIZE_SCALES
,
resize_scales
:
Tuple
[
int
,
...]
=
RESIZE_SCALES
,
aug_rand_hflip
=
True
):
aug_rand_hflip
=
True
):
self
.
_class_offset
=
class_offset
self
.
_output_size
=
output_size
self
.
_output_size
=
output_size
self
.
_max_num_boxes
=
max_num_boxes
self
.
_max_num_boxes
=
max_num_boxes
self
.
_resize_scales
=
resize_scales
self
.
_resize_scales
=
resize_scales
self
.
_aug_rand_hflip
=
aug_rand_hflip
self
.
_aug_rand_hflip
=
aug_rand_hflip
def
_parse_train_data
(
self
,
data
):
def
_parse_train_data
(
self
,
data
):
"""Parses data for training and evaluation."""
"""Parses data for training and evaluation."""
#classes = data['groundtruth_classes'] + 1
classes
=
data
[
'groundtruth_classes'
]
+
self
.
_class_offset
classes
=
data
[
'groundtruth_classes'
]
boxes
=
data
[
'groundtruth_boxes'
]
boxes
=
data
[
'groundtruth_boxes'
]
is_crowd
=
data
[
'groundtruth_is_crowd'
]
is_crowd
=
data
[
'groundtruth_is_crowd'
]
# Gets original image.
# Gets original image.
image
=
data
[
'image'
]
image
=
data
[
'image'
]
# Apply autoaug or randaug.
#if self._augmenter is not None:
# image, boxes = self._augmenter.distort_with_boxes(image, boxes)
# Normalizes image with mean and std pixel values.
# Normalizes image with mean and std pixel values.
image
=
preprocess_ops
.
normalize_image
(
image
)
image
=
preprocess_ops
.
normalize_image
(
image
)
image
,
boxes
,
_
=
preprocess_ops
.
random_horizontal_flip
(
image
,
boxes
)
image
,
boxes
,
_
=
preprocess_ops
.
random_horizontal_flip
(
image
,
boxes
)
do_crop
=
tf
.
greater
(
tf
.
random
.
uniform
([]),
0.5
)
do_crop
=
tf
.
greater
(
tf
.
random
.
uniform
([]),
0.5
)
...
...
official/projects/detr/tasks/detection.py
View file @
67446adb
...
@@ -31,6 +31,7 @@ from official.vision.dataloaders import tf_example_decoder
...
@@ -31,6 +31,7 @@ from official.vision.dataloaders import tf_example_decoder
from
official.vision.dataloaders
import
tfds_factory
from
official.vision.dataloaders
import
tfds_factory
from
official.vision.dataloaders
import
tf_example_label_map_decoder
from
official.vision.dataloaders
import
tf_example_label_map_decoder
from
official.projects.detr.dataloaders
import
detr_input
from
official.projects.detr.dataloaders
import
detr_input
from
official.projects.detr.dataloaders
import
coco
from
official.vision.modeling
import
backbones
from
official.vision.modeling
import
backbones
@
task_factory
.
register_task_cls
(
detr_cfg
.
DetrTask
)
@
task_factory
.
register_task_cls
(
detr_cfg
.
DetrTask
)
...
@@ -84,41 +85,39 @@ class DectectionTask(base_task.Task):
...
@@ -84,41 +85,39 @@ class DectectionTask(base_task.Task):
logging
.
info
(
'Finished loading pretrained checkpoint from %s'
,
logging
.
info
(
'Finished loading pretrained checkpoint from %s'
,
ckpt_dir_or_file
)
ckpt_dir_or_file
)
"""def build_inputs(self,
params: detr_cfg.DataConfig,
input_context: Optional[tf.distribute.InputContext] = None):
return coco.COCODataLoader(params).load(input_context)"""
def
build_inputs
(
self
,
def
build_inputs
(
self
,
params
,
params
,
input_context
:
Optional
[
tf
.
distribute
.
InputContext
]
=
None
):
input_context
:
Optional
[
tf
.
distribute
.
InputContext
]
=
None
):
"""Build input dataset."""
"""Build input dataset."""
if
type
(
params
)
is
coco
.
COCODataConfig
:
if
params
.
tfds_name
:
dataset
=
coco
.
COCODataLoader
(
params
).
load
(
input_context
)
decoder
=
tfds_factory
.
get_detection_decoder
(
params
.
tfds_name
)
else
:
else
:
decoder_cfg
=
params
.
decoder
.
get
()
if
params
.
tfds_name
:
if
params
.
decoder
.
type
==
'simple_decoder'
:
decoder
=
tfds_factory
.
get_detection_decoder
(
params
.
tfds_name
)
decoder
=
tf_example_decoder
.
TfExampleDecoder
(
regenerate_source_id
=
decoder_cfg
.
regenerate_source_id
)
elif
params
.
decoder
.
type
==
'label_map_decoder'
:
decoder
=
tf_example_label_map_decoder
.
TfExampleDecoderLabelMap
(
label_map
=
decoder_cfg
.
label_map
,
regenerate_source_id
=
decoder_cfg
.
regenerate_source_id
)
else
:
else
:
raise
ValueError
(
'Unknown decoder type: {}!'
.
format
(
decoder_cfg
=
params
.
decoder
.
get
()
params
.
decoder
.
type
))
if
params
.
decoder
.
type
==
'simple_decoder'
:
decoder
=
tf_example_decoder
.
TfExampleDecoder
(
parser
=
detr_input
.
Parser
(
regenerate_source_id
=
decoder_cfg
.
regenerate_source_id
)
output_size
=
self
.
_task_config
.
model
.
input_size
[:
2
],
elif
params
.
decoder
.
type
==
'label_map_decoder'
:
)
decoder
=
tf_example_label_map_decoder
.
TfExampleDecoderLabelMap
(
label_map
=
decoder_cfg
.
label_map
,
reader
=
input_reader_factory
.
input_reader_generator
(
regenerate_source_id
=
decoder_cfg
.
regenerate_source_id
)
params
,
else
:
dataset_fn
=
dataset_fn
.
pick_dataset_fn
(
params
.
file_type
),
raise
ValueError
(
'Unknown decoder type: {}!'
.
format
(
decoder_fn
=
decoder
.
decode
,
params
.
decoder
.
type
))
parser_fn
=
parser
.
parse_fn
(
params
.
is_training
))
dataset
=
reader
.
read
(
input_context
=
input_context
)
parser
=
detr_input
.
Parser
(
class_offset
=
self
.
_task_config
.
losses
.
class_offset
,
output_size
=
self
.
_task_config
.
model
.
input_size
[:
2
],
)
reader
=
input_reader_factory
.
input_reader_generator
(
params
,
dataset_fn
=
dataset_fn
.
pick_dataset_fn
(
params
.
file_type
),
decoder_fn
=
decoder
.
decode
,
parser_fn
=
parser
.
parse_fn
(
params
.
is_training
))
dataset
=
reader
.
read
(
input_context
=
input_context
)
return
dataset
return
dataset
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment