Skip to content

Commit

Permalink
fix merge error
Browse files Browse the repository at this point in the history
  • Loading branch information
IdeaKing committed Mar 21, 2022
2 parents 7ab4aa0 + 2afa0f1 commit 7c15408
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 6 deletions.
97 changes: 97 additions & 0 deletions inference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import os
import argparse
import tensorflow as tf

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="datasets/data/VOC2012/TestImages",
help="Path to testing images directory.")
parser.add_argument("--save-image-dir",
type=str,
default="datasets/data/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="datasets/data/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_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,
iou_threshold=args.iou_threshold)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
tensorflow==2.7.0
opencv-python # Only needed for fundus images
albumentations
matplotlib # Only needed for notebooks and display
matplotlib
Pillow

# Web development backends
1 change: 0 additions & 1 deletion src/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,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")))
Expand Down
6 changes: 3 additions & 3 deletions src/tests/test_trained_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/utils/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7c15408

Please sign in to comment.