Commit cd598da1 authored by anivegesana's avatar anivegesana
Browse files

Fix merge

parent b92025a9
......@@ -15,7 +15,7 @@
# Lint as: python3
"""Contains definitions of Darknet Backbone Networks.
These models are inspired by ResNet and CSPNet.
The models are inspired by ResNet and CSPNet.
Residual networks (ResNets) were proposed in:
[1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
......@@ -43,8 +43,6 @@ from official.modeling import hyperparams
from official.vision.beta.modeling.backbones import factory
from official.vision.beta.projects.yolo.modeling.layers import nn_blocks
# builder required classes
class BlockConfig:
"""Class to store layer config to make code more readable."""
......@@ -667,8 +665,7 @@ def build_darknet(
l2_regularizer: tf.keras.regularizers.Regularizer = None) -> tf.keras.Model:
"""Builds darknet."""
backbone_cfg = model_config.backbone.get()
norm_activation_config = model_config.norm_activation
backbone_cfg = backbone_config.backbone.get()
model = Darknet(
model_id=backbone_cfg.model_id,
......
......@@ -70,7 +70,7 @@ class DarknetTest(parameterized.TestCase, tf.test.TestCase):
@combinations.generate(
combinations.combine(
strategy=[
strategy_combinations.tpu_strategy,
strategy_combinations.cloud_tpu_strategy,
strategy_combinations.one_device_strategy_gpu,
],
use_sync_bn=[False, True],
......
from official.vision.beta.modeling.backbones import factory
from yolo.modeling.decoders.yolo_decoder import YoloDecoder
from yolo.modeling.heads.yolo_head import YoloHead
from yolo.modeling.layers.detection_generator import YoloLayer
from yolo.modeling.backbones.darknet import build_darknet
from yolo.modeling import yolo_model
from yolo.configs import yolo
def build_yolo_decoder(input_specs, model_config: yolo.Yolo, l2_regularization):
activation = (
model_config.decoder.activation
if model_config.decoder.activation != "same" else
model_config.norm_activation.activation)
subdivisions = 1
if model_config.decoder.version is None: # custom yolo
model = YoloDecoder(
input_specs,
embed_spp=model_config.decoder.embed_spp,
use_fpn=model_config.decoder.use_fpn,
fpn_depth=model_config.decoder.fpn_depth,
path_process_len=model_config.decoder.path_process_len,
max_level_process_len=model_config.decoder.max_level_process_len,
xy_exponential=model_config.decoder.xy_exponential,
activation=activation,
subdivisions=subdivisions,
use_spatial_attention=model_config.use_sam,
use_sync_bn=model_config.norm_activation.use_sync_bn,
norm_momentum=model_config.norm_activation.norm_momentum,
norm_epsilon=model_config.norm_activation.norm_epsilon,
kernel_regularizer=l2_regularization)
return model
if model_config.decoder.type == None:
model_config.decoder.type = "regular"
if model_config.decoder.version not in yolo_model.YOLO_MODELS.keys():
raise Exception(
"unsupported model version please select from {v3, v4}, \n\n \
or specify a custom decoder config using YoloDecoder in you yaml")
if model_config.decoder.type not in yolo_model.YOLO_MODELS[
model_config.decoder.version].keys():
raise Exception("unsupported model type please select from \
{yolo_model.YOLO_MODELS[model_config.decoder.version].keys()},\
\n\n or specify a custom decoder config using YoloDecoder in you yaml")
base_model = yolo_model.YOLO_MODELS[model_config.decoder.version][
model_config.decoder.type]
cfg_dict = model_config.decoder.as_dict()
for key in base_model:
if cfg_dict[key] is not None:
base_model[key] = cfg_dict[key]
base_dict = dict(
activation=activation,
subdivisions=subdivisions,
use_spatial_attention=model_config.decoder.use_spatial_attention,
use_sync_bn=model_config.norm_activation.use_sync_bn,
norm_momentum=model_config.norm_activation.norm_momentum,
norm_epsilon=model_config.norm_activation.norm_epsilon,
kernel_regularizer=l2_regularization)
base_model.update(base_dict)
print(base_model)
model = YoloDecoder(input_specs, **base_model)
return model
def build_yolo_filter(model_config: yolo.Yolo, decoder: YoloDecoder, masks,
xy_scales, path_scales):
def _build(values):
print(values)
if "all" in values and values["all"] is not None:
for key in values:
if key != 'all':
values[key] = values["all"]
print(values)
return values
model = YoloLayer(
masks=masks,
classes=model_config.num_classes,
anchors=model_config._boxes,
iou_thresh=model_config.filter.iou_thresh,
nms_thresh=model_config.filter.nms_thresh,
max_boxes=model_config.filter.max_boxes,
nms_type=model_config.filter.nms_type,
path_scale=path_scales,
scale_xy=xy_scales,
darknet=model_config.filter.darknet,
label_smoothing=model_config.filter.label_smoothing,
pre_nms_points=model_config.filter.pre_nms_points,
use_scaled_loss=model_config.filter.use_scaled_loss,
truth_thresh=_build(model_config.filter.truth_thresh.as_dict()),
loss_type=_build(model_config.filter.loss_type.as_dict()),
max_delta=_build(model_config.filter.max_delta.as_dict()),
new_cords=_build(model_config.filter.new_cords.as_dict()),
iou_normalizer=_build(model_config.filter.iou_normalizer.as_dict()),
cls_normalizer=_build(model_config.filter.cls_normalizer.as_dict()),
obj_normalizer=_build(model_config.filter.obj_normalizer.as_dict()),
ignore_thresh=_build(model_config.filter.ignore_thresh.as_dict()),
objectness_smooth=_build(model_config.filter.objectness_smooth.as_dict()))
return model
def build_yolo_head(input_specs, model_config: yolo.Yolo, l2_regularization):
head = YoloHead(
min_level=model_config.min_level,
max_level=model_config.max_level,
classes=model_config.num_classes,
boxes_per_level=model_config.boxes_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.smart_bias)
return head
def build_yolo(input_specs, model_config, l2_regularization, masks, xy_scales,
path_scales):
print(model_config.as_dict())
print(input_specs)
print(l2_regularization)
# backbone = factory.build_backbone(input_specs, model_config,
# l2_regularization)
backbone = build_darknet(input_specs, model_config,
l2_regularization)
decoder = build_yolo_decoder(backbone.output_specs, model_config,
l2_regularization)
head = build_yolo_head(decoder.output_specs, model_config, l2_regularization)
filter = build_yolo_filter(model_config, head, masks, xy_scales, path_scales)
model = yolo_model.Yolo(
backbone=backbone, decoder=decoder, head=head, filter=filter)
model.build(input_specs.shape)
model.decoder.summary()
model.summary()
losses = filter.losses
return model, losses
......@@ -23,9 +23,6 @@ from official.vision.beta.ops import spatial_transform_ops
@tf.keras.utils.register_keras_serializable(package='yolo')
class Identity(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def call(self, inputs):
return inputs
......@@ -642,11 +639,6 @@ class CSPRoute(tf.keras.layers.Layer):
x = self._conv3(inputs)
return (x, y)
self._conv2 = ConvBN(
filters=self._filters // self._filter_scale,
kernel_size=(1, 1),
strides=(1, 1),
**dark_conv_args)
@tf.keras.utils.register_keras_serializable(package='yolo')
class CSPConnect(tf.keras.layers.Layer):
......@@ -683,8 +675,8 @@ class CSPConnect(tf.keras.layers.Layer):
"""Initializer for CSPConnect block.
Args:
filters: integer for output depth, or the number of features to learn
filter_scale: integer dicating (filters//2) or the number of filters in
filters: integer for output depth, or the number of features to learn.
filter_scale: integer dictating (filters//2) or the number of filters in
the partial feature stack.
drop_final: `bool`, whether to drop final conv layer.
drop_first: `bool`, whether to drop first conv layer.
......@@ -807,7 +799,7 @@ class CSPStack(tf.keras.layers.Layer):
model_to_wrap: callable Model or a list of callable objects that will
process the output of CSPRoute, and be input into CSPConnect.
list will be called sequentially.
filter_scale: integer dicating (filters//2) or the number of filters in
filter_scale: integer dictating (filters//2) or the number of filters in
the partial feature stack.
activation: string for activation function to use in layer.
kernel_initializer: string to indicate which function to use to initialize
......
......@@ -16,8 +16,6 @@
from absl.testing import parameterized
import numpy as np
import tensorflow as tf
import numpy as np
from absl.testing import parameterized
from official.vision.beta.projects.yolo.modeling.layers import nn_blocks
......@@ -62,6 +60,7 @@ class CSPConnectTest(tf.test.TestCase, parameterized.TestCase):
grad_loss = loss(x_hat, y)
grad = tape.gradient(grad_loss, test_layer.trainable_variables)
optimizer.apply_gradients(zip(grad, test_layer.trainable_variables))
self.assertNotIn(None, grad)
......@@ -72,7 +71,7 @@ class CSPRouteTest(tf.test.TestCase, parameterized.TestCase):
def test_pass_through(self, width, height, filters, mod):
x = tf.keras.Input(shape=(width, height, filters))
test_layer = nn_blocks.CSPRoute(filters=filters, filter_scale=mod)
outx, px = test_layer(x)
outx, _ = test_layer(x)
print(outx)
print(outx.shape.as_list())
self.assertAllEqual(
......
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# Lint as: python3
"""Tests for Yolo models."""
from official.vision.beta.projects.yolo.modeling.backbones.darknet import \
Darknet
from official.vision.beta.projects.yolo.modeling.decoders.yolo_decoder import \
YoloDecoder
from official.vision.beta.projects.yolo.modeling.heads.yolo_head import YoloHead
from official.vision.beta.projects.yolo.modeling import yolo_model
class YoloTest(parameterized.TestCase, tf.test.TestCase):
def test_network_creation(self):
backbone = ['darknettiny', 'darknet53', 'cspdarknet53', 'altered_cspdarknet53', 'cspdarknettiny', 'csp-large']
base_model = yolo_model.YOLO_MODELS[version][decoder]
backbone = Darknet(model_id=backbone)
decoder = YoloDecoder(backbone.output_specs, **base_model)
head = YoloHead(
min_level=model_config.min_level,
max_level=model_config.max_level,
classes=model_config.num_classes,
boxes_per_level=model_config.boxes_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.smart_bias)
pass
if __name__ == '__main__':
tf.test.main()
......@@ -51,4 +51,4 @@ class InputUtilsTest(parameterized.TestCase, tf.test.TestCase):
self.assertArrayNear(diou, expected_iou, 0.001)
if __name__ == '__main__':
tf.test.main()
\ No newline at end of file
tf.test.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment