factory.py 6.41 KB
Newer Older
Yeqing Li's avatar
Yeqing Li committed
1
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2
3
4
5
6
7
8
9
10
11
12
13
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Yeqing Li's avatar
Yeqing Li committed
14

15
16
17
18
19
20
"""Model architecture factory."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

Yeqing Li's avatar
Yeqing Li committed
21
from official.vision.detection.dataloader import maskrcnn_parser
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
22
from official.vision.detection.dataloader import olnmask_parser
23
from official.vision.detection.dataloader import retinanet_parser
Yeqing Li's avatar
Yeqing Li committed
24
from official.vision.detection.dataloader import shapemask_parser
25

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
26

27
28
29
30
31
32
33
def parser_generator(params, mode):
  """Generator function for various dataset parser."""
  if params.architecture.parser == 'retinanet_parser':
    anchor_params = params.anchor
    parser_params = params.retinanet_parser
    parser_fn = retinanet_parser.Parser(
        output_size=parser_params.output_size,
Pengchong Jin's avatar
Pengchong Jin committed
34
35
        min_level=params.architecture.min_level,
        max_level=params.architecture.max_level,
36
37
38
39
40
41
42
43
44
45
46
47
        num_scales=anchor_params.num_scales,
        aspect_ratios=anchor_params.aspect_ratios,
        anchor_size=anchor_params.anchor_size,
        match_threshold=parser_params.match_threshold,
        unmatched_threshold=parser_params.unmatched_threshold,
        aug_rand_hflip=parser_params.aug_rand_hflip,
        aug_scale_min=parser_params.aug_scale_min,
        aug_scale_max=parser_params.aug_scale_max,
        use_autoaugment=parser_params.use_autoaugment,
        autoaugment_policy_name=parser_params.autoaugment_policy_name,
        skip_crowd_during_training=parser_params.skip_crowd_during_training,
        max_num_instances=parser_params.max_num_instances,
Pengchong Jin's avatar
Pengchong Jin committed
48
        use_bfloat16=params.architecture.use_bfloat16,
49
        mode=mode)
Yeqing Li's avatar
Yeqing Li committed
50
51
52
53
54
  elif params.architecture.parser == 'maskrcnn_parser':
    anchor_params = params.anchor
    parser_params = params.maskrcnn_parser
    parser_fn = maskrcnn_parser.Parser(
        output_size=parser_params.output_size,
Pengchong Jin's avatar
Pengchong Jin committed
55
56
        min_level=params.architecture.min_level,
        max_level=params.architecture.max_level,
Yeqing Li's avatar
Yeqing Li committed
57
58
59
60
61
62
63
64
65
66
67
68
        num_scales=anchor_params.num_scales,
        aspect_ratios=anchor_params.aspect_ratios,
        anchor_size=anchor_params.anchor_size,
        rpn_match_threshold=parser_params.rpn_match_threshold,
        rpn_unmatched_threshold=parser_params.rpn_unmatched_threshold,
        rpn_batch_size_per_im=parser_params.rpn_batch_size_per_im,
        rpn_fg_fraction=parser_params.rpn_fg_fraction,
        aug_rand_hflip=parser_params.aug_rand_hflip,
        aug_scale_min=parser_params.aug_scale_min,
        aug_scale_max=parser_params.aug_scale_max,
        skip_crowd_during_training=parser_params.skip_crowd_during_training,
        max_num_instances=parser_params.max_num_instances,
Pengchong Jin's avatar
Pengchong Jin committed
69
        include_mask=params.architecture.include_mask,
Yeqing Li's avatar
Yeqing Li committed
70
        mask_crop_size=parser_params.mask_crop_size,
Pengchong Jin's avatar
Pengchong Jin committed
71
        use_bfloat16=params.architecture.use_bfloat16,
Yeqing Li's avatar
Yeqing Li committed
72
        mode=mode)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  elif params.architecture.parser == 'olnmask_parser':
    anchor_params = params.anchor
    parser_params = params.olnmask_parser
    parser_fn = olnmask_parser.Parser(
        output_size=parser_params.output_size,
        min_level=params.architecture.min_level,
        max_level=params.architecture.max_level,
        num_scales=anchor_params.num_scales,
        aspect_ratios=anchor_params.aspect_ratios,
        anchor_size=anchor_params.anchor_size,
        rpn_match_threshold=parser_params.rpn_match_threshold,
        rpn_unmatched_threshold=parser_params.rpn_unmatched_threshold,
        rpn_batch_size_per_im=parser_params.rpn_batch_size_per_im,
        rpn_fg_fraction=parser_params.rpn_fg_fraction,
        aug_rand_hflip=parser_params.aug_rand_hflip,
        aug_scale_min=parser_params.aug_scale_min,
        aug_scale_max=parser_params.aug_scale_max,
        skip_crowd_during_training=parser_params.skip_crowd_during_training,
        max_num_instances=parser_params.max_num_instances,
        include_mask=params.architecture.include_mask,
        mask_crop_size=parser_params.mask_crop_size,
        use_bfloat16=params.architecture.use_bfloat16,
        mode=mode,
        has_centerness=parser_params.has_centerness,
        rpn_center_match_iou_threshold=(
            parser_params.rpn_center_match_iou_threshold),
        rpn_center_unmatched_iou_threshold=(
            parser_params.rpn_center_unmatched_iou_threshold),
        rpn_num_center_samples_per_im=(
            parser_params.rpn_num_center_samples_per_im),
        class_agnostic=parser_params.class_agnostic,
        train_class=parser_params.train_class,)
Yeqing Li's avatar
Yeqing Li committed
105
106
107
108
109
  elif params.architecture.parser == 'shapemask_parser':
    anchor_params = params.anchor
    parser_params = params.shapemask_parser
    parser_fn = shapemask_parser.Parser(
        output_size=parser_params.output_size,
Pengchong Jin's avatar
Pengchong Jin committed
110
111
        min_level=params.architecture.min_level,
        max_level=params.architecture.max_level,
Yeqing Li's avatar
Yeqing Li committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
        num_scales=anchor_params.num_scales,
        aspect_ratios=anchor_params.aspect_ratios,
        anchor_size=anchor_params.anchor_size,
        use_category=parser_params.use_category,
        outer_box_scale=parser_params.outer_box_scale,
        box_jitter_scale=parser_params.box_jitter_scale,
        num_sampled_masks=parser_params.num_sampled_masks,
        mask_crop_size=parser_params.mask_crop_size,
        mask_min_level=parser_params.mask_min_level,
        mask_max_level=parser_params.mask_max_level,
        upsample_factor=parser_params.upsample_factor,
        match_threshold=parser_params.match_threshold,
        unmatched_threshold=parser_params.unmatched_threshold,
        aug_rand_hflip=parser_params.aug_rand_hflip,
        aug_scale_min=parser_params.aug_scale_min,
        aug_scale_max=parser_params.aug_scale_max,
        skip_crowd_during_training=parser_params.skip_crowd_during_training,
        max_num_instances=parser_params.max_num_instances,
Pengchong Jin's avatar
Pengchong Jin committed
130
        use_bfloat16=params.architecture.use_bfloat16,
Yeqing Li's avatar
Yeqing Li committed
131
132
        mask_train_class=parser_params.mask_train_class,
        mode=mode)
133
134
135
136
  else:
    raise ValueError('Parser %s is not supported.' % params.architecture.parser)

  return parser_fn