From bebca37b1b1fdcccdf08547ab81b0c48f1d58a97 Mon Sep 17 00:00:00 2001 From: ManojKumarMuru Date: Thu, 20 Oct 2022 15:53:55 +0200 Subject: [PATCH] Docstrings added, comments removed --- examples/efficientdet/detection.py | 21 ++++++++++++++++++ examples/efficientdet/draw.py | 35 +++++++++++++++++++++++++++++- examples/efficientdet/train.py | 12 ++-------- examples/efficientdet/utils.py | 25 --------------------- 4 files changed, 57 insertions(+), 36 deletions(-) diff --git a/examples/efficientdet/detection.py b/examples/efficientdet/detection.py index d47b3f92f..f3fba9e72 100644 --- a/examples/efficientdet/detection.py +++ b/examples/efficientdet/detection.py @@ -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], @@ -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): diff --git a/examples/efficientdet/draw.py b/examples/efficientdet/draw.py index dd52c83b1..3c489ea91 100644 --- a/examples/efficientdet/draw.py +++ b/examples/efficientdet/draw.py @@ -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]``. @@ -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) diff --git a/examples/efficientdet/train.py b/examples/efficientdet/train.py index 957321885..f14d954d1 100644 --- a/examples/efficientdet/train.py +++ b/examples/efficientdet/train.py @@ -5,8 +5,8 @@ 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 @@ -14,19 +14,11 @@ 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, diff --git a/examples/efficientdet/utils.py b/examples/efficientdet/utils.py index b105e5967..7e8557b46 100644 --- a/examples/efficientdet/utils.py +++ b/examples/efficientdet/utils.py @@ -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.