Skip to content

Commit

Permalink
refactor efficientnet backbone on yolo3, and use processbar for eval …
Browse files Browse the repository at this point in the history
…display
  • Loading branch information
david8862 committed Dec 9, 2019
1 parent 45d407f commit 137c60a
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 111 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A common YOLOv3/v2 object detection pipeline inherited from [keras-yolo3-Mobilen
- [x] Darknet19
- [x] MobilenetV1
- [x] MobilenetV2
- [x] EfficientNetB0
- [x] EfficientNet
- [x] VGG16
- [x] Xception

Expand Down
4 changes: 2 additions & 2 deletions common/backbones/efficientnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,8 @@ def preprocess_input(x):

if __name__ == '__main__':
input_tensor = Input(shape=(None, None, 3), name='image_input')
#model = EfficientNetB0(include_top=False, input_shape=(416, 416, 3), weights='imagenet')
model = EfficientNetB0(include_top=True, input_tensor=input_tensor, weights='imagenet')
model = EfficientNetB1(include_top=False, input_shape=(416, 416, 3), weights='imagenet')
#model = EfficientNetB0(include_top=True, input_tensor=input_tensor, weights='imagenet')
model.summary()

import numpy as np
Expand Down
3 changes: 2 additions & 1 deletion common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ def draw_boxes(image, boxes, classes, scores, class_names, colors, show_score=Tr
label = '{} {:.2f}'.format(class_name, score)
else:
label = '{}'.format(class_name)
print(label, (xmin, ymin), (xmax, ymax))
#print(label, (xmin, ymin), (xmax, ymax))

# if no color info, use black(0,0,0)
if colors == None:
color = (0,0,0)
Expand Down
6 changes: 5 additions & 1 deletion eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from PIL import Image
import operator
import matplotlib.pyplot as plt
from tqdm import tqdm

from tensorflow.keras.models import load_model
import tensorflow.keras.backend as K
Expand Down Expand Up @@ -278,6 +279,7 @@ def get_prediction_class_records(model, model_format, annotation_records, anchor
session = model.createSession()

pred_classes_records = {}
pbar = tqdm(total=len(annotation_records), desc='Eval model')
for (image_name, gt_records) in annotation_records.items():
image = Image.open(image_name)
image_array = np.array(image, dtype='uint8')
Expand All @@ -297,7 +299,8 @@ def get_prediction_class_records(model, model_format, annotation_records, anchor
else:
raise ValueError('invalid model format')

print('Found {} boxes for {}'.format(len(pred_boxes), image_name))
#print('Found {} boxes for {}'.format(len(pred_boxes), image_name))
pbar.update(1)

if save_result:

Expand Down Expand Up @@ -334,6 +337,7 @@ def get_prediction_class_records(model, model_format, annotation_records, anchor
for pred_class_list in pred_classes_records.values():
pred_class_list.sort(key=lambda ele: ele[2], reverse=True)

pbar.close()
return pred_classes_records


Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ opencv-python
tensorflow-gpu
tensorflow-model-optimization
matplotlib
tqdm
pillow
mnn
3 changes: 2 additions & 1 deletion tools/validate_yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ def handle_prediction(prediction, image_file, image, image_shape, anchors, class

print('Found {} boxes for {}'.format(len(boxes), image_file))
for box, cls, score in zip(boxes, classes, scores):
print("Class: {}, Score: {}".format(class_names[cls], score))
xmin, ymin, xmax, ymax = box
print("Class: {}, Score: {}, Box: {},{}".format(class_names[cls], score, (xmin, ymin), (xmax, ymax)))

colors = get_colors(class_names)
image = draw_boxes(image, boxes, classes, scores, class_names, colors)
Expand Down
16 changes: 10 additions & 6 deletions yolo3/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from yolo3.models.yolo3_vgg16 import yolo3_vgg16_body, tiny_yolo3_vgg16_body
from yolo3.models.yolo3_xception import yolo3_xception_body, yolo3lite_xception_body, tiny_yolo3_xception_body, tiny_yolo3lite_xception_body, yolo3_spp_xception_body
from yolo3.models.yolo3_nano import yolo3_nano_body
from yolo3.models.yolo3_efficientnet import yolo3_efficientnetb0_body, tiny_yolo3_efficientnetb0_body, yolo3lite_efficientnetb0_body, yolo3lite_spp_efficientnetb0_body, tiny_yolo3lite_efficientnetb0_body
from yolo3.models.yolo3_efficientnet import yolo3_efficientnet_body, tiny_yolo3_efficientnet_body, yolo3lite_efficientnet_body, yolo3lite_spp_efficientnet_body, tiny_yolo3lite_efficientnet_body
from yolo3.loss import yolo3_loss
from yolo3.postprocess import batched_yolo3_postprocess, batched_yolo3_prenms, Yolo3PostProcessLayer

Expand All @@ -40,9 +40,11 @@
'yolo3_shufflenetv2_lite': [yolo3lite_shufflenetv2_body, 205, None],
'yolo3_shufflenetv2_lite_spp': [yolo3lite_spp_shufflenetv2_body, 205, None],

'yolo3_efficientnetb0': [yolo3_efficientnetb0_body, 235, None],
'yolo3_efficientnetb0_lite': [yolo3lite_efficientnetb0_body, 235, None],
'yolo3_efficientnetb0_lite_spp': [yolo3lite_spp_efficientnetb0_body, 235, None],
# NOTE: backbone_length is for EfficientNetB0
# if change to other efficientnet level, you need to modify it
'yolo3_efficientnet': [yolo3_efficientnet_body, 235, None],
'yolo3_efficientnet_lite': [yolo3lite_efficientnet_body, 235, None],
'yolo3_efficientnet_lite_spp': [yolo3lite_spp_efficientnet_body, 235, None],

'yolo3_darknet': [yolo3_body, 185, 'weights/darknet53.h5'],
'yolo3_darknet_spp': [custom_yolo3_spp_body, 185, 'weights/yolov3-spp.h5'],
Expand Down Expand Up @@ -73,8 +75,10 @@
'tiny_yolo3_shufflenetv2': [tiny_yolo3_shufflenetv2_body, 205, None],
'tiny_yolo3_shufflenetv2_lite': [tiny_yolo3lite_shufflenetv2_body, 205, None],

'tiny_yolo3_efficientnetb0': [tiny_yolo3_efficientnetb0_body, 235, None],
'tiny_yolo3_efficientnetb0_lite': [tiny_yolo3lite_efficientnetb0_body, 235, None],
# NOTE: backbone_length is for EfficientNetB0
# if change to other efficientnet level, you need to modify it
'tiny_yolo3_efficientnet': [tiny_yolo3_efficientnet_body, 235, None],
'tiny_yolo3_efficientnet_lite': [tiny_yolo3lite_efficientnet_body, 235, None],

'tiny_yolo3_darknet': [custom_tiny_yolo3_body, 20, 'weights/yolov3-tiny.h5'],
#Doesn't have pretrained weights, so no need to return backbone length
Expand Down
Loading

0 comments on commit 137c60a

Please sign in to comment.