forked from wuzaipei/python_TensorFlow_yolov3-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_weight.py
145 lines (119 loc) · 5.3 KB
/
convert_weight.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#! /usr/bin/env python
# coding=utf-8
#================================================================
# Copyright (C) 2018 * Ltd. All rights reserved.
#
# Editor : VIM
# File name : convert_weight.py
# Author : YunYang1994
# Created date: 2018-11-27 12:37:22
# Description :
#
#================================================================
import os
import sys
import wget
import time
import argparse
import tensorflow as tf
from core import yolov3, utils
class parser(argparse.ArgumentParser):
def __init__(self,description):
super(parser, self).__init__(description)
self.add_argument(
"--ckpt_file", "-cf", default='./checkpoint/yolov3.ckpt', type=str,
help="[default: %(default)s] The checkpoint file ...",
metavar="<CF>",
)
self.add_argument(
"--num_classes", "-nc", default=80, type=int,
help="[default: %(default)s] The number of classes ...",
metavar="<NC>",
)
self.add_argument(
"--anchors_path", "-ap", default="./data/raccoon_anchors_wzp.txt", type=str,
help="[default: %(default)s] The path of anchors ...",
metavar="<AP>",
)
self.add_argument(
"--weights_path", "-wp", default='./checkpoint/yolov3.weights', type=str,
help="[default: %(default)s] Download binary file with desired weights",
metavar="<WP>",
)
self.add_argument(
"--convert", "-cv", action='store_true',
help="[default: %(default)s] Downloading yolov3 weights and convert them",
)
self.add_argument(
"--freeze", "-fz", action='store_true',
help="[default: %(default)s] freeze the yolov3 graph to pb ...",
)
self.add_argument(
"--image_h", "-ih", default=416, type=int,
help="[default: %(default)s] The height of image, 416 or 608",
metavar="<IH>",
)
self.add_argument(
"--image_w", "-iw", default=416, type=int,
help="[default: %(default)s] The width of image, 416 or 608",
metavar="<IW>",
)
self.add_argument(
"--iou_threshold", "-it", default=0.5, type=float,
help="[default: %(default)s] The iou_threshold for gpu nms",
metavar="<IT>",
)
self.add_argument(
"--score_threshold", "-st", default=0.5, type=float,
help="[default: %(default)s] The score_threshold for gpu nms",
metavar="<ST>",
)
def main(argv):
flags = parser(description="freeze yolov3 graph from checkpoint file").parse_args()
# print("=> the input image size is [%d, %d]" %(flags.image_h, flags.image_w))
anchors_path ="./data/raccoon_anchors_wzp.txt"
image_h = 416;image_w = 416
num_classes = 1
score_threshold = 0.5
iou_threshold=0.5
ckpt_file = './model/yolov3.ckpt-1000'
anchors = utils.get_anchors(anchors_path,image_h, image_w)
model = yolov3.yolov3(num_classes, anchors)
with tf.Graph().as_default() as graph:
sess = tf.Session(graph=graph)
inputs = tf.placeholder(tf.float32, [1, image_h, image_w, 3]) # placeholder for detector inputs
print("=>", inputs)
with tf.variable_scope('yolov3'):
feature_map = model.forward(inputs, is_training=False)
boxes, confs, probs = model.predict(feature_map)
scores = confs * probs
print("=>", boxes.name[:-2], scores.name[:-2])
cpu_out_node_names = [boxes.name[:-2], scores.name[:-2]]
boxes, scores, labels = utils.gpu_nms(boxes, scores, num_classes,
score_thresh=score_threshold,
iou_thresh=iou_threshold)
print("=>", boxes.name[:-2], scores.name[:-2], labels.name[:-2])
gpu_out_node_names = [boxes.name[:-2], scores.name[:-2], labels.name[:-2]]
feature_map_1, feature_map_2, feature_map_3 = feature_map
saver = tf.train.Saver(var_list=tf.global_variables(scope='yolov3'))
if flags.convert:
if not os.path.exists(flags.weights_path):
url = 'https://github.com/YunYang1994/tensorflow-yolov3/releases/download/v1.0/yolov3.weights'
for i in range(3):
time.sleep(1)
print("=> %s does not exists ! " %flags.weights_path)
print("=> It will take a while to download it from %s" %url)
print('=> Downloading yolov3 weights ... ')
wget.download(url, flags.weights_path)
load_ops = utils.load_weights(tf.global_variables(scope='yolov3'), flags.weights_path)
sess.run(load_ops)
save_path = saver.save(sess, save_path=flags.ckpt_file)
print('=> model saved in path: {}'.format(save_path))
if True:
saver.restore(sess, ckpt_file)
print('=> checkpoint file restored from ', ckpt_file)
utils.freeze_graph(sess, './model/yolov3_cpu_nms.pb', cpu_out_node_names)
utils.freeze_graph(sess, './model/yolov3_gpu_nms.pb', gpu_out_node_names)
if __name__ == "__main__":
main(sys.argv)
# python convert_weight.py -cf ./checkpoint/yolov3.ckpt -nc 1 -ap ./data/raccoon_anchors_wzp.txt --freeze