Skip to content

Commit

Permalink
Docstrings added, comments removed
Browse files Browse the repository at this point in the history
  • Loading branch information
Manojkumarmuru committed Oct 20, 2022
1 parent 7b91a7f commit bebca37
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 36 deletions.
21 changes: 21 additions & 0 deletions examples/efficientdet/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ def call(self, image):


class DetectSingleShot(DetectSingleShot):
"""Single-shot object detection prediction.
# Arguments
model: Keras model.
class_names: List of strings indicating the class names.
score_thresh: Float between [0, 1]
nms_thresh: Float between [0, 1].
mean: List of three elements indicating the per channel mean.
variances: List containing the variances of the encoded boxes.
draw: Boolean. If ``True`` prediction are drawn in the returned image.
"""
def __init__(
self, model, class_names, score_thresh, nms_thresh,
mean=pr.BGR_IMAGENET_MEAN, variances=[0.1, 0.1, 0.2, 0.2],
Expand All @@ -119,6 +130,16 @@ def __init__(


class DrawBoxes2D(pr.DrawBoxes2D):
"""Draws bounding boxes from Boxes2D messages.
# Arguments
class_names: List of strings.
colors: List of lists containing the color values
weighted: Boolean. If ``True`` the colors are weighted with the
score of the bounding box.
scale: Float. Scale of drawn text.
with_score: Boolean. If ``True`` displays the confidence score.
"""
def __init__(
self, class_names=None, colors=None,
weighted=False, scale=0.7, with_score=True):
Expand Down
35 changes: 34 additions & 1 deletion examples/efficientdet/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@ def put_text(image, text, point, scale, color, thickness):


def get_text_size(text, scale, FONT_THICKNESS, FONT=FONT):
"""Calculates the size of a given text.
# Arguments
text: String. Text whose width and height is to be calculated.
scale: Float. Scale of text.
FONT_THICKNESS: Integer. Thickness of the lines used for drawing text.
FONT: Integer. Style of the text font.
# Returns
Numpy array with shape ``[H, W, 3]``. Image with text.
"""
return cv2.getTextSize(text, FONT, scale, FONT_THICKNESS)


def add_box_border(image, corner_A, corner_B, color, thickness):
""" Draws a filled rectangle from ``corner_A`` to ``corner_B``.
""" Draws an open rectangle from ``corner_A`` to ``corner_B``.
# Arguments
image: Numpy array of shape ``[H, W, 3]``.
Expand All @@ -45,10 +55,33 @@ def add_box_border(image, corner_A, corner_B, color, thickness):


def draw_opaque_box(image, corner_A, corner_B, color, thickness=-1):
""" Draws a filled rectangle from ``corner_A`` to ``corner_B``.
# Arguments
image: Numpy array of shape ``[H, W, 3]``.
corner_A: List of length two indicating ``(y, x)`` openCV coordinates.
corner_B: List of length two indicating ``(y, x)`` openCV coordinates.
color: List of length three indicating RGB color of point.
thickness: Integer/openCV Flag. Thickness of rectangle line.
or for filled use cv2.FILLED flag.
# Returns
Numpy array with shape ``[H, W, 3]``. Image with rectangle.
"""
return cv2.rectangle(
image, tuple(corner_A), tuple(corner_B), tuple(color),
thickness)


def make_box_transparent(raw_image, image, alpha=0.30):
""" Blends the raw image with bounding box image to add transparency.
# Arguments
raw_image: Numpy array of shape ``[H, W, 3]``.
image: Numpy array of shape ``[H, W, 3]``.
alpha: Float, weightage parameter of weighted sum.
# Returns
Numpy array with shape ``[H, W, 3]``. Image with rectangle.
"""
return cv2.addWeighted(raw_image, 1-alpha, image, alpha, 0.0)
12 changes: 2 additions & 10 deletions examples/efficientdet/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,20 @@
from paz.abstract import ProcessingSequence
from paz.datasets import VOC
from paz.optimization import MultiBoxLoss
from paz.optimization.callbacks import EvaluateMAP, LearningRateScheduler
from paz.pipelines import AugmentDetection, DetectSingleShot
from paz.optimization.callbacks import LearningRateScheduler
from paz.pipelines import AugmentDetection
from paz.processors import TRAIN, VAL
from tensorflow.keras.callbacks import CSVLogger, ModelCheckpoint
from tensorflow.keras.optimizers import SGD

from efficientdet import EFFICIENTDETD0

gpus = tf.config.experimental.list_physical_devices('GPU')
# tf.config.experimental.set_memory_growth(gpus[0], True)

# from tensorflow.python.framework.ops import disable_eager_execution
# disable_eager_execution()
# import tensorflow as tf
# tf.compat.v1.experimental.output_all_intermediates(True)

description = 'Training script for single-shot object detection models'
parser = argparse.ArgumentParser(description=description)
parser.add_argument('-bs', '--batch_size', default=128, type=int,
help='Batch size for training')
parser.add_argument('-et', '--evaluation_period', default=5, type=int,
help='evaluation frequency')
parser.add_argument('-lr', '--learning_rate', default=0.08, type=float,
help='Initial learning rate for SGD')
parser.add_argument('-m', '--momentum', default=0.9, type=float,
Expand Down
25 changes: 0 additions & 25 deletions examples/efficientdet/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,6 @@ def get_class_name_efficientdet(dataset_name):
'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']


# def get_drop_connect(features, is_training, survival_rate):
# """Drop the entire conv with given survival probability.
# Deep Networks with Stochastic Depth, https://arxiv.org/pdf/1603.09382.pdf

# # Arguments
# features: Tensor, input feature map to undergo
# drop connection.
# is_training: Bool specifying the training phase.
# survival_rate: Float, survival probability to drop
# input convolution features.

# # Returns
# output: Tensor, output feature map after drop connect.
# """
# if not is_training:
# return features
# batch_size = tf.shape(features)[0]
# random_tensor = survival_rate
# random_tensor = random_tensor + tf.random.uniform(
# [batch_size, 1, 1, 1], dtype=features.dtype)
# binary_tensor = tf.floor(random_tensor)
# output = (features / survival_rate) * binary_tensor
# return output


class CustomDropout(keras.layers.Layer):
"""Implements dropout for layers of the model.
Expand Down

0 comments on commit bebca37

Please sign in to comment.