forked from zzh8829/yolov3-tf2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
90 lines (71 loc) · 2.74 KB
/
convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from absl import app, flags, logging
from absl.flags import FLAGS
import numpy as np
from yolov3_tf2.models import YoloV3, YoloV3Tiny, YoloV2Lite
from yolov3_tf2.utils import load_darknet_weights
import tensorflow as tf
import cv2
import os
from convert_to_pb import convert_to_pb
tf_version = tf.__version__.split('.')[0]
flags.DEFINE_string('weights', './data/yolov3.weights', 'path to weights file')
flags.DEFINE_boolean('from_h5', False, 'source of weights')
flags.DEFINE_string('output', './checkpoints/yolov3.tf', 'path to output')
flags.DEFINE_boolean('tiny', False, 'yolov3 or yolov3-tiny')
flags.DEFINE_boolean('lite', False, 'yolov3 or yolov2-lite')
flags.DEFINE_integer('num_classes', 80, 'number of classes in the model')
flags.DEFINE_integer('size', 0, 'Default is None')
flags.DEFINE_boolean('training', False, 'training flag')
def predict(model):
size = model.input_shape[1]
if size is None:
size = 288
im = cv2.imread('data\meme.jpg')
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
im = cv2.resize(im, (size, size))
im = im / 255.
im = np.expand_dims(im, axis=0)
p = model.predict(im)
p = p[0]
filename = os.path.join('unversion', os.path.basename(model.name) + '_pred.txt')
print('writing predictions to ' + filename)
with open(filename, 'w') as f:
f.write(str(p))
def main(_argv):
if tf_version == '2':
physical_devices = tf.config.experimental.list_physical_devices('GPU')
if len(physical_devices) > 0:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
size = None if FLAGS.size == 0 else FLAGS.size
if FLAGS.tiny:
yolo = YoloV3Tiny(size=size, classes=FLAGS.num_classes, training=FLAGS.training)
elif FLAGS.lite:
yolo = YoloV2Lite(size=size, classes=FLAGS.num_classes, training=FLAGS.training)
else:
yolo = YoloV3(size=size, classes=FLAGS.num_classes, training=FLAGS.training)
yolo.summary()
logging.info('model created')
if FLAGS.from_h5:
yolo.load_weights(FLAGS.weights)
else:
load_darknet_weights(yolo, FLAGS.weights, FLAGS.tiny, FLAGS.lite)
logging.info('weights loaded')
imsize = 320 if size is None else size
img = np.random.random((1, imsize, imsize, 3)).astype(np.float32)
output = yolo(img)
logging.info('sanity check passed')
predict(yolo)
if FLAGS.output.endswith(('.h5', '.hdf5')):
yolo.save(FLAGS.output)
logging.info('keras model saved')
else:
yolo.save_weights(FLAGS.output)
logging.info('weights saved')
if tf_version == '1':
convert_to_pb(yolo)
logging.info('pb saved')
if __name__ == '__main__':
try:
app.run(main)
except SystemExit:
pass