factory.py 4.11 KB
Newer Older
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1
# Copyright 2022 The TensorFlow Authors. All Rights Reserved.
Vishnu Banna's avatar
Vishnu Banna committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#
# 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.

"""Contains common factory functions yolo neural networks."""

from absl import logging

Abdullah Rashwan's avatar
Abdullah Rashwan committed
19
20
21
22
from official.projects.yolo.configs import yolo
from official.projects.yolo.modeling import yolo_model
from official.projects.yolo.modeling.heads import yolo_head
from official.projects.yolo.modeling.layers import detection_generator
Abdullah Rashwan's avatar
Abdullah Rashwan committed
23
24
from official.vision.modeling.backbones import factory as backbone_factory
from official.vision.modeling.decoders import factory as decoder_factory
Vishnu Banna's avatar
Vishnu Banna committed
25

26

Vishnu Banna's avatar
Vishnu Banna committed
27
def build_yolo_detection_generator(model_config: yolo.Yolo, anchor_boxes):
28
  """Builds yolo detection generator."""
Vishnu Banna's avatar
Vishnu Banna committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  model = detection_generator.YoloLayer(
      classes=model_config.num_classes,
      anchors=anchor_boxes,
      iou_thresh=model_config.detection_generator.iou_thresh,
      nms_thresh=model_config.detection_generator.nms_thresh,
      max_boxes=model_config.detection_generator.max_boxes,
      pre_nms_points=model_config.detection_generator.pre_nms_points,
      nms_type=model_config.detection_generator.nms_type,
      box_type=model_config.detection_generator.box_type.get(),
      path_scale=model_config.detection_generator.path_scales.get(),
      scale_xy=model_config.detection_generator.scale_xy.get(),
      label_smoothing=model_config.loss.label_smoothing,
      use_scaled_loss=model_config.loss.use_scaled_loss,
      update_on_repeat=model_config.loss.update_on_repeat,
      truth_thresh=model_config.loss.truth_thresh.get(),
      loss_type=model_config.loss.box_loss_type.get(),
      max_delta=model_config.loss.max_delta.get(),
      iou_normalizer=model_config.loss.iou_normalizer.get(),
      cls_normalizer=model_config.loss.cls_normalizer.get(),
Vishnu Banna's avatar
Vishnu Banna committed
48
      object_normalizer=model_config.loss.object_normalizer.get(),
Vishnu Banna's avatar
Vishnu Banna committed
49
50
51
52
53
54
      ignore_thresh=model_config.loss.ignore_thresh.get(),
      objectness_smooth=model_config.loss.objectness_smooth.get())
  return model


def build_yolo_head(input_specs, model_config: yolo.Yolo, l2_regularization):
55
  """Builds yolo head."""
Vishnu Banna's avatar
Vishnu Banna committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  min_level = min(map(int, input_specs.keys()))
  max_level = max(map(int, input_specs.keys()))
  head = yolo_head.YoloHead(
      min_level=min_level,
      max_level=max_level,
      classes=model_config.num_classes,
      boxes_per_level=model_config.anchor_boxes.anchors_per_scale,
      norm_momentum=model_config.norm_activation.norm_momentum,
      norm_epsilon=model_config.norm_activation.norm_epsilon,
      kernel_regularizer=l2_regularization,
      smart_bias=model_config.head.smart_bias)
  return head


def build_yolo(input_specs, model_config, l2_regularization):
71
  """Builds yolo model."""
Vishnu Banna's avatar
Vishnu Banna committed
72
  backbone = model_config.backbone.get()
73
74
75
76
77
78
  anchor_dict, _ = model_config.anchor_boxes.get(
      backbone.min_level, backbone.max_level)
  backbone = backbone_factory.build_backbone(input_specs, model_config.backbone,
                                             model_config.norm_activation,
                                             l2_regularization)
  decoder = decoder_factory.build_decoder(backbone.output_specs, model_config,
Vishnu Banna's avatar
Vishnu Banna committed
79
80
81
                                          l2_regularization)

  head = build_yolo_head(decoder.output_specs, model_config, l2_regularization)
82
83
  detection_generator_obj = build_yolo_detection_generator(model_config,
                                                           anchor_dict)
Vishnu Banna's avatar
Vishnu Banna committed
84
85
86
87
88

  model = yolo_model.Yolo(
      backbone=backbone,
      decoder=decoder,
      head=head,
89
      detection_generator=detection_generator_obj)
Vishnu Banna's avatar
Vishnu Banna committed
90
91
92
93
  model.build(input_specs.shape)

  model.summary(print_fn=logging.info)

94
  losses = detection_generator_obj.get_losses()
Vishnu Banna's avatar
Vishnu Banna committed
95
  return model, losses