[45a3e1]: / darkflow / net / yolo / misc.py

Download this file

136 lines (114 with data), 4.6 kB

  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
import pickle
import numpy as np
import cv2
import os
labels20 = ["aeroplane", "bicycle", "bird", "boat", "bottle",
"bus", "car", "cat", "chair", "cow", "diningtable", "dog",
"horse", "motorbike", "person", "pottedplant", "sheep", "sofa",
"train", "tvmonitor"]
# 8, 14, 15, 19
voc_models = ['yolo-full', 'yolo-tiny', 'yolo-small', # <- v1
'yolov1', 'tiny-yolov1', # <- v1.1
'tiny-yolo-voc', 'yolo-voc'] # <- v2
coco_models = ['tiny-coco', 'yolo-coco', # <- v1.1
'yolo', 'tiny-yolo'] # <- v2
coco_names = 'coco.names'
nine_names = '9k.names'
def labels(meta, FLAGS):
model = os.path.basename(meta['name'])
if model in voc_models:
print("Model has a VOC model name, loading VOC labels.")
meta['labels'] = labels20
else:
file = FLAGS.labels
if model in coco_models:
print("Model has a coco model name, loading coco labels.")
file = os.path.join(FLAGS.config, coco_names)
elif model == 'yolo9000':
print("Model has name yolo9000, loading yolo9000 labels.")
file = os.path.join(FLAGS.config, nine_names)
with open(file, 'r') as f:
meta['labels'] = list()
labs = [l.strip() for l in f.readlines()]
for lab in labs:
if lab == '----': break
meta['labels'] += [lab]
if len(meta['labels']) == 0:
meta['labels'] = labels20
def is_inp(self, name):
return name.lower().endswith(('.jpg', '.jpeg', '.png'))
def show(im, allobj, S, w, h, cellx, celly):
for obj in allobj:
a = obj[5] % S
b = obj[5] // S
cx = a + obj[1]
cy = b + obj[2]
centerx = cx * cellx
centery = cy * celly
ww = obj[3] ** 2 * w
hh = obj[4] ** 2 * h
cv2.rectangle(im,
(int(centerx - ww / 2), int(centery - hh / 2)),
(int(centerx + ww / 2), int(centery + hh / 2)),
(0, 0, 255), 2)
cv2.imshow('result', im)
cv2.waitKey()
cv2.destroyAllWindows()
def show2(im, allobj):
for obj in allobj:
cv2.rectangle(im,
(obj[1], obj[2]),
(obj[3], obj[4]),
(0, 0, 255), 2)
cv2.imshow('result', im)
cv2.waitKey()
cv2.destroyAllWindows()
_MVA = .05
def profile(self, net):
pass
# data = self.parse(exclusive = True)
# size = len(data); batch = self.FLAGS.batch
# all_inp_ = [x[0] for x in data]
# net.say('Will cycle through {} examples {} times'.format(
# len(all_inp_), net.FLAGS.epoch))
# fetch = list(); mvave = list(); names = list();
# this = net.top
# conv_lay = ['convolutional', 'connected', 'local', 'conv-select']
# while this.inp is not None:
# if this.lay.type in conv_lay:
# fetch = [this.out] + fetch
# names = [this.lay.signature] + names
# mvave = [None] + mvave
# this = this.inp
# print(names)
# total = int(); allofthem = len(all_inp_) * net.FLAGS.epoch
# batch = min(net.FLAGS.batch, len(all_inp_))
# for count in range(net.FLAGS.epoch):
# net.say('EPOCH {}'.format(count))
# for j in range(len(all_inp_)/batch):
# inp_feed = list(); new_all = list()
# all_inp = all_inp_[j*batch: (j*batch+batch)]
# for inp in all_inp:
# new_all += [inp]
# this_inp = os.path.join(net.FLAGS.dataset, inp)
# this_inp = net.framework.preprocess(this_inp)
# expanded = np.expand_dims(this_inp, 0)
# inp_feed.append(expanded)
# all_inp = new_all
# feed_dict = {net.inp : np.concatenate(inp_feed, 0)}
# out = net.sess.run(fetch, feed_dict)
# for i, o in enumerate(out):
# oi = out[i];
# dim = len(oi.shape) - 1
# ai = mvave[i];
# mi = np.mean(oi, tuple(range(dim)))
# vi = np.var(oi, tuple(range(dim)))
# if ai is None: mvave[i] = [mi, vi]
# elif 'banana ninja yada yada':
# ai[0] = (1 - _MVA) * ai[0] + _MVA * mi
# ai[1] = (1 - _MVA) * ai[1] + _MVA * vi
# total += len(inp_feed)
# net.say('{} / {} = {}%'.format(
# total, allofthem, 100. * total / allofthem))
# with open('profile', 'wb') as f:
# pickle.dump([mvave], f, protocol = -1)