From 6a25d43f9569f6fb137f30ab169e8538e780e6a4 Mon Sep 17 00:00:00 2001 From: IdeaKing Date: Mon, 21 Mar 2022 16:23:29 -0400 Subject: [PATCH 1/2] inference --- inference.py | 92 +++++++++++++++++++++++++++++++++ src/tests/test_trained_model.py | 6 +-- src/utils/postprocess.py | 2 +- 3 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 inference.py diff --git a/inference.py b/inference.py new file mode 100644 index 0000000..ddba438 --- /dev/null +++ b/inference.py @@ -0,0 +1,92 @@ +import os +import argparse +import tensorflow as tf +import matplotlib.pyplot as plt + +from src.utils.postprocess import FilterDetections +from src.utils.visualize import draw_boxes +from src.utils.file_reader import parse_label_file + + +def preprocess_image(image_path, image_dims): + image = tf.io.read_file(image_path) + image = tf.io.decode_image(image, channels=3) + image = tf.cast(image, dtype=tf.float32)/255. # Normalize + image = tf.image.resize(image, size=image_dims) + image = tf.expand_dims(image, axis=0) + return image + + +def test(image_path, image_dir, save_dir, model, + image_dims, label_dict, score_threshold, iou_threshold): + """Preprocesses, Tests, and Postprocesses""" + image = preprocess_image( + os.path.join(image_dir, image_path), image_dims) + + pred_cls, pred_box = model(image, training=False) + labels, bboxes, scores = FilterDetections( + score_threshold=score_threshold, + iou_threshold=iou_threshold, + image_dims=image_dims)( + labels=pred_cls, + bboxes=pred_box) + + labels = [list(label_dict.keys())[int(l)] + for l in labels[0]] + bboxes=bboxes[0] + scores=scores[0] + + image = draw_boxes( + image=tf.squeeze(image, axis=0), + bboxes=bboxes, + labels=labels, + scores=scores) + + image.save(os.path.join(save_dir, image_path)) + +if __name__ == "__main__": + parser=argparse.ArgumentParser( + description="Run i-Sight Tests", + prog="i-Sight") + parser.add_argument("--testing-image-dir", + type=str, + default="data/dataset/VOC2012/TestImages", + help="Path to testing images directory.") + parser.add_argument("--save-image-dir", + type=str, + default="data/dataset/Tests", + help="Path to testing images directory.") + parser.add_argument("--model-dir", + type=str, + default="training_dir/voc/model-exported", + help="Path to testing model directory.") + parser.add_argument("--image-dims", + type=tuple, + default=(512, 512), + help="Size of the input image.") + parser.add_argument("--labels-file", + type=str, + default="data/dataset/VOC2012/labels.txt", + help="Path to labels file.") + parser.add_argument("--score-threshold", + type=float, + default=0.1, + help="Score threshold for NMS.") + parser.add_argument("--iou-threshold", + type=float, + default=0.5, + help="IOU threshold for NMS.") + args=parser.parse_args() + + label_dict = parse_label_file( + path_to_label_file=args.labels_files) + + model = tf.keras.models.load_model(args.model_dir) + + for image_path in os.listdir(args.testing_image_dir): + # Test the model on the image + test(image_path=image_path, + model=model, + image_dims=args.image_dims, + label_dict=label_dict, + score_threshold=args.score_threshold) diff --git a/src/tests/test_trained_model.py b/src/tests/test_trained_model.py index c762544..fbd84a9 100644 --- a/src/tests/test_trained_model.py +++ b/src/tests/test_trained_model.py @@ -47,11 +47,11 @@ def test(image_path, model, image_dims, label_dict, score_threshold): image_dims = (512, 512) label_dict = parse_label_file( path_to_label_file="datasets/data/VOC2012/labels.txt") - score_threshold = 0.10 + score_threshold = 0.1 # Path to image and model - image_path = "datasets/data/VOC2012/images/2007_002545.jpg" - model_path = "model/model" + image_path = "datasets/data/VOC2012/images/2008_000676.jpg" + model_path = "training_dir/voc/model-exported" model = tf.keras.models.load_model(model_path) # Test the model on the image diff --git a/src/utils/postprocess.py b/src/utils/postprocess.py index b02ddf8..88441b3 100644 --- a/src/utils/postprocess.py +++ b/src/utils/postprocess.py @@ -12,7 +12,7 @@ def __init__(self, image_dims: Tuple[int, int] = (512, 512), max_boxes: int = 150, max_size: int = 100, - iou_threshold: int = 0.7): + iou_threshold: int = 0.8): self.score_threshold = score_threshold self.image_dims = image_dims From 2afa0f13464b904321b054808c1bc294eba016ae Mon Sep 17 00:00:00 2001 From: IdeaKing Date: Mon, 21 Mar 2022 16:33:29 -0400 Subject: [PATCH 2/2] inference debugs --- inference.py | 17 +++++++++++------ requirements.txt | 1 + src/dataset.py | 1 - 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/inference.py b/inference.py index ddba438..70affdc 100644 --- a/inference.py +++ b/inference.py @@ -1,7 +1,6 @@ import os import argparse import tensorflow as tf -import matplotlib.pyplot as plt from src.utils.postprocess import FilterDetections from src.utils.visualize import draw_boxes @@ -50,11 +49,11 @@ def test(image_path, image_dir, save_dir, model, prog="i-Sight") parser.add_argument("--testing-image-dir", type=str, - default="data/dataset/VOC2012/TestImages", + default="datasets/data/VOC2012/TestImages", help="Path to testing images directory.") parser.add_argument("--save-image-dir", type=str, - default="data/dataset/Tests", + default="datasets/data/Tests", help="Path to testing images directory.") parser.add_argument("--model-dir", type=str, @@ -66,7 +65,7 @@ def test(image_path, image_dir, save_dir, model, help="Size of the input image.") parser.add_argument("--labels-file", type=str, - default="data/dataset/VOC2012/labels.txt", + default="datasets/data/VOC2012/labels.txt", help="Path to labels file.") parser.add_argument("--score-threshold", type=float, @@ -79,14 +78,20 @@ def test(image_path, image_dir, save_dir, model, args=parser.parse_args() label_dict = parse_label_file( - path_to_label_file=args.labels_files) + path_to_label_file=args.labels_file) model = tf.keras.models.load_model(args.model_dir) + if os.path.exists(args.save_image_dir) == False: + os.mkdir(args.save_image_dir) + for image_path in os.listdir(args.testing_image_dir): # Test the model on the image test(image_path=image_path, + image_dir=args.testing_image_dir, + save_dir=args.save_image_dir, model=model, image_dims=args.image_dims, label_dict=label_dict, - score_threshold=args.score_threshold) + score_threshold=args.score_threshold, + iou_threshold=args.iou_threshold) diff --git a/requirements.txt b/requirements.txt index e2008fa..93db403 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,5 +3,6 @@ tensorflow==2.7.0 opencv-python albumentations matplotlib +Pillow # Web development backends \ No newline at end of file diff --git a/src/dataset.py b/src/dataset.py index 8d0e756..383b273 100644 --- a/src/dataset.py +++ b/src/dataset.py @@ -126,7 +126,6 @@ def parse_process_voc(self, file_name): # Reads a voc annotation and returns # a list of tuples containing the ground # truth boxes and its respective label - root = ET.parse(file_name).getroot() image_size = (int(root.findtext("size/width")), int(root.findtext("size/height")))