Commit 3214ae9f authored by Vishnu Banna's avatar Vishnu Banna
Browse files

model params

parent 634079da
# 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.
"""Yolo Loss function."""
import tensorflow as tf import tensorflow as tf
from collections import defaultdict from collections import defaultdict
import abc import abc
...@@ -217,18 +231,13 @@ class YoloLossBase(object, metaclass=abc.ABCMeta): ...@@ -217,18 +231,13 @@ class YoloLossBase(object, metaclass=abc.ABCMeta):
@tf.custom_gradient @tf.custom_gradient
def grad_sigmoid(values): def grad_sigmoid(values):
""" """This function scales the gradient as if a signmoid was applied to the
This function scales the gradient as if a signmoid was applied to the
model output. This is used in the Darknet Loss when the choosen box type model output. This is used in the Darknet Loss when the choosen box type
is the scaled coordinate type. This function is used to match the propagated is the scaled coordinate type. This function is used to match the propagated
gradient to match that of the Darkent Yolov4 model. gradient to match that of the Darkent Yolov4 model. This is an Identity
operation that allows us to add some estra steps to the back propagation.
""" """
# This is an identity operation that will
# allow us to add some steps to the back propagation.
def delta(dy): def delta(dy):
# Darknet only propagtes sigmoids for the boxes
# under some conditions, so we need this to selectively
# add the sigmoid to the chain rule
t = tf.math.sigmoid(values) t = tf.math.sigmoid(values)
return dy * t * (1 - t) return dy * t * (1 - t)
...@@ -239,8 +248,10 @@ class DarknetLoss(YoloLossBase): ...@@ -239,8 +248,10 @@ class DarknetLoss(YoloLossBase):
"""This class implements the full logic for the standard Yolo models.""" """This class implements the full logic for the standard Yolo models."""
def _build_per_path_attributes(self): def _build_per_path_attributes(self):
"""Paramterization of pair wise search and grid generators for box """Paramterization of pair wise search and grid generators.
decoding and dynamic ground truth association."""
Objects created here are used for box decoding and dynamic ground truth
association."""
self._anchor_generator = loss_utils.GridGenerator( self._anchor_generator = loss_utils.GridGenerator(
masks=self._masks, masks=self._masks,
anchors=self._anchors, anchors=self._anchors,
...@@ -252,8 +263,7 @@ class DarknetLoss(YoloLossBase): ...@@ -252,8 +263,7 @@ class DarknetLoss(YoloLossBase):
return return
def _compute_loss(self, true_counts, inds, y_true, boxes, classes, y_pred): def _compute_loss(self, true_counts, inds, y_true, boxes, classes, y_pred):
"""Per FPN path loss computation logic used for Yolov3, Yolov4, and """Per FPN path loss logic used for Yolov3, Yolov4, and Yolo-Tiny."""
Yolo-Tiny."""
if self._box_type == "scaled": if self._box_type == "scaled":
# Darknet Model Propagates a sigmoid once in back prop so we replicate # Darknet Model Propagates a sigmoid once in back prop so we replicate
# that behaviour # that behaviour
...@@ -389,8 +399,10 @@ class ScaledLoss(YoloLossBase): ...@@ -389,8 +399,10 @@ class ScaledLoss(YoloLossBase):
"""This class implements the full logic for the scaled Yolo models. """ """This class implements the full logic for the scaled Yolo models. """
def _build_per_path_attributes(self): def _build_per_path_attributes(self):
"""Paramterization of pair wise search and grid generators for box """Paramterization of pair wise search and grid generators.
decoding and dynamic ground truth association."""
Objects created here are used for box decoding and dynamic ground truth
association."""
self._anchor_generator = loss_utils.GridGenerator( self._anchor_generator = loss_utils.GridGenerator(
masks=self._masks, masks=self._masks,
anchors=self._anchors, anchors=self._anchors,
...@@ -402,8 +414,7 @@ class ScaledLoss(YoloLossBase): ...@@ -402,8 +414,7 @@ class ScaledLoss(YoloLossBase):
return return
def _compute_loss(self, true_counts, inds, y_true, boxes, classes, y_pred): def _compute_loss(self, true_counts, inds, y_true, boxes, classes, y_pred):
"""Per FPN path loss computation logic Yolov4-csp, Yolov4-Large, and """Per FPN path loss logic for Yolov4-csp, Yolov4-Large, and Yolov5."""
Yolov5."""
# Generate shape constants. # Generate shape constants.
shape = tf.shape(true_counts) shape = tf.shape(true_counts)
batch_size, width, height, num = shape[0], shape[1], shape[2], shape[3] batch_size, width, height, num = shape[0], shape[1], shape[2], shape[3]
...@@ -518,14 +529,12 @@ class ScaledLoss(YoloLossBase): ...@@ -518,14 +529,12 @@ class ScaledLoss(YoloLossBase):
return loss * scale return loss * scale
def cross_replica_aggregation(self, loss, num_replicas_in_sync): def cross_replica_aggregation(self, loss, num_replicas_in_sync):
"""In the scaled loss, the loss is aggregated across replicas via the """In the scaled loss, take the sum of the loss across replicas."""
sum."""
return loss return loss
class YoloLoss(object): class YoloLoss:
"""This class implements the aggregated loss across paths for the YOLO """This class implements the aggregated loss across YOLO model FPN levels."""
model."""
def __init__(self, def __init__(self,
keys, keys,
......
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