Skip to content

Commit b8c80d9

Browse files
authored
Merge pull request #1122 from Laicheng0830/app_format
do format
2 parents 1f30ceb + 8d9ead0 commit b8c80d9

File tree

5 files changed

+135
-113
lines changed

5 files changed

+135
-113
lines changed

examples/app_tutorials/tutorial_human_3dpose_estimation_LCN.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
# -*- coding: utf-8 -*-
33

44
from tensorlayer.app.human_pose_estimation.common import DataReader, visualize_3D_pose, flip_data
5-
from tensorlayer.app.human_pose_estimation.LCN import CGCNN
5+
from tensorlayer.app import computer_vision
66
import numpy as np
77

88
datareader = DataReader()
99
train_data, test_data = datareader.read_2d(which='scale', mode='gt', read_confidence=False)
1010
train_labels, test_labels = datareader.read_3d(which='scale', mode='gt')
11-
network = CGCNN(pretrained=True)
11+
network = computer_vision.human_pose_estimation('3D-pose')
1212
test_data = flip_data(test_data)
13-
result = network(test_data, is_train=False)
13+
result = network(test_data)
1414
result = datareader.denormalize3D(np.asarray(result), which='scale')
1515
test_data = datareader.denormalize2D(test_data, which='scale')
1616
test_labels = datareader.denormalize3D(test_labels, which='scale')

tensorlayer/app/computer_vision.py

+53-82
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#! /usr/bin/python
22
# -*- coding: utf-8 -*-
33

4-
from tensorlayer.app import YOLOv4, get_anchors, decode, filter_boxes
4+
from tensorlayer.app import YOLOv4
55
from tensorlayer.app import CGCNN
6-
import numpy as np
7-
import tensorflow as tf
86
from tensorlayer import logging
9-
import cv2
7+
from tensorlayer.app import yolo4_input_processing, yolo4_output_processing, result_to_json
108

119

1210
class object_detection(object):
@@ -42,8 +40,6 @@ def __init__(self, model_name='yolo4-mscoco'):
4240
self.model_name = model_name
4341
if self.model_name == 'yolo4-mscoco':
4442
self.model = YOLOv4(NUM_CLASS=80, pretrained=True)
45-
elif self.model_name == 'lcn':
46-
self.model = CGCNN(pretrained=True)
4743
else:
4844
raise ("The model does not support.")
4945

@@ -53,8 +49,6 @@ def __call__(self, input_data):
5349
feature_maps = self.model(batch_data, is_train=False)
5450
pred_bbox = yolo4_output_processing(feature_maps)
5551
output = result_to_json(input_data, pred_bbox)
56-
elif self.model_name == 'lcn':
57-
output = self.model(input_data)
5852
else:
5953
raise NotImplementedError
6054

@@ -70,78 +64,55 @@ def list(self):
7064
logging.info("The model name list: 'yolov4-mscoco', 'lcn'")
7165

7266

73-
def yolo4_input_processing(original_image):
74-
image_data = cv2.resize(original_image, (416, 416))
75-
image_data = image_data / 255.
76-
images_data = []
77-
for i in range(1):
78-
images_data.append(image_data)
79-
images_data = np.asarray(images_data).astype(np.float32)
80-
batch_data = tf.constant(images_data)
81-
return batch_data
82-
83-
84-
def yolo4_output_processing(feature_maps):
85-
STRIDES = [8, 16, 32]
86-
ANCHORS = get_anchors([12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401])
87-
NUM_CLASS = 80
88-
XYSCALE = [1.2, 1.1, 1.05]
89-
iou_threshold = 0.45
90-
score_threshold = 0.25
91-
92-
bbox_tensors = []
93-
prob_tensors = []
94-
score_thres = 0.2
95-
for i, fm in enumerate(feature_maps):
96-
if i == 0:
97-
output_tensors = decode(fm, 416 // 8, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE)
98-
elif i == 1:
99-
output_tensors = decode(fm, 416 // 16, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE)
67+
class human_pose_estimation(object):
68+
"""Model encapsulation.
69+
70+
Parameters
71+
----------
72+
model_name : str
73+
Choose the model to inference.
74+
75+
Methods
76+
---------
77+
__init__()
78+
Initializing the model.
79+
__call__()
80+
(1)Formatted input and output. (2)Inference model.
81+
list()
82+
Abstract method. Return available a list of model_name.
83+
84+
Examples
85+
---------
86+
LCN to estimate 3D human poses from 2D poses, see `tutorial_human_3dpose_estimation_LCN.py
87+
<https://github.com/tensorlayer/tensorlayer/blob/master/example/app_tutorials/tutorial_human_3dpose_estimation_LCN.py>`__
88+
With TensorLayer
89+
90+
>>> # get the whole model
91+
>>> net = tl.app.computer_vision.human_pose_estimation('3D-pose')
92+
>>> # use for inferencing
93+
>>> output = net(img)
94+
"""
95+
96+
def __init__(self, model_name='3D-pose'):
97+
self.model_name = model_name
98+
if self.model_name == '3D-pose':
99+
self.model = CGCNN(pretrained=True)
100100
else:
101-
output_tensors = decode(fm, 416 // 32, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE)
102-
bbox_tensors.append(output_tensors[0])
103-
prob_tensors.append(output_tensors[1])
104-
pred_bbox = tf.concat(bbox_tensors, axis=1)
105-
pred_prob = tf.concat(prob_tensors, axis=1)
106-
boxes, pred_conf = filter_boxes(
107-
pred_bbox, pred_prob, score_threshold=score_thres, input_shape=tf.constant([416, 416])
108-
)
109-
pred = {'concat': tf.concat([boxes, pred_conf], axis=-1)}
110-
111-
for key, value in pred.items():
112-
boxes = value[:, :, 0:4]
113-
pred_conf = value[:, :, 4:]
114-
115-
boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
116-
boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
117-
scores=tf.reshape(pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
118-
max_output_size_per_class=50, max_total_size=50, iou_threshold=iou_threshold, score_threshold=score_threshold
119-
)
120-
output = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()]
121-
return output
122-
123-
124-
def result_to_json(image, pred_bbox):
125-
image_h, image_w, _ = image.shape
126-
out_boxes, out_scores, out_classes, num_boxes = pred_bbox
127-
class_names = {}
128-
json_result = []
129-
with open('model/coco.names', 'r') as data:
130-
for ID, name in enumerate(data):
131-
class_names[ID] = name.strip('\n')
132-
nums_class = len(class_names)
133-
134-
for i in range(num_boxes[0]):
135-
if int(out_classes[0][i]) < 0 or int(out_classes[0][i]) > nums_class: continue
136-
coor = out_boxes[0][i]
137-
coor[0] = int(coor[0] * image_h)
138-
coor[2] = int(coor[2] * image_h)
139-
coor[1] = int(coor[1] * image_w)
140-
coor[3] = int(coor[3] * image_w)
141-
142-
score = float(out_scores[0][i])
143-
class_ind = int(out_classes[0][i])
144-
bbox = np.array([coor[1], coor[0], coor[3], coor[2]]).tolist() # [x1,y1,x2,y2]
145-
json_result.append({'image': None, 'category_id': class_ind, 'bbox': bbox, 'score': score})
146-
147-
return json_result
101+
raise ("The model does not support.")
102+
103+
def __call__(self, input_data):
104+
if self.model_name == '3D-pose':
105+
output = self.model(input_data, is_train=False)
106+
else:
107+
raise NotImplementedError
108+
109+
return output
110+
111+
def __repr__(self):
112+
s = ('(model_name={model_name}, model_structure={model}')
113+
s += ')'
114+
return s.format(classname=self.__class__.__name__, **self.__dict__)
115+
116+
@property
117+
def list(self):
118+
logging.info("The model name list: '3D-pose'")

tensorlayer/app/computer_vision_object_detection/common.py

+75-24
Original file line numberDiff line numberDiff line change
@@ -147,27 +147,78 @@ def decode_train(conv_output, output_size, NUM_CLASS, STRIDES, ANCHORS, i=0, XYS
147147
return tf.concat([pred_xywh, pred_conf, pred_prob], axis=-1)
148148

149149

150-
# def weights_sorted():
151-
# # download weights
152-
# maybe_download_and_extract(
153-
# 'yolov4.npz',
154-
# model_path,
155-
# 'https://github.com/',
156-
# ) # ls -al
157-
# weights = []
158-
# track_weights = []
159-
# weights_dict = {}
160-
#
161-
# npz = np.load(model_path, allow_pickle=True)
162-
# # get weight list
163-
# for val in sorted(npz.items()):
164-
# logging.info(" Loading weights %s in %s" % (str(val[1].shape), val[0]))
165-
# try:
166-
# weights.append(int(val[0].split('/')[0].split('-')[-1]))
167-
# track_weights.append(val[0])
168-
# except:
169-
# pass
170-
# zip_weights = zip(weights, track_weights)
171-
# zip_weights = sorted(zip_weights)
172-
# for value, key in zip_weights:
173-
# print(key)
150+
def yolo4_input_processing(original_image):
151+
image_data = cv2.resize(original_image, (416, 416))
152+
image_data = image_data / 255.
153+
images_data = []
154+
for i in range(1):
155+
images_data.append(image_data)
156+
images_data = np.asarray(images_data).astype(np.float32)
157+
batch_data = tf.constant(images_data)
158+
return batch_data
159+
160+
161+
def yolo4_output_processing(feature_maps):
162+
STRIDES = [8, 16, 32]
163+
ANCHORS = get_anchors([12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401])
164+
NUM_CLASS = 80
165+
XYSCALE = [1.2, 1.1, 1.05]
166+
iou_threshold = 0.45
167+
score_threshold = 0.25
168+
169+
bbox_tensors = []
170+
prob_tensors = []
171+
score_thres = 0.2
172+
for i, fm in enumerate(feature_maps):
173+
if i == 0:
174+
output_tensors = decode(fm, 416 // 8, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE)
175+
elif i == 1:
176+
output_tensors = decode(fm, 416 // 16, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE)
177+
else:
178+
output_tensors = decode(fm, 416 // 32, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE)
179+
bbox_tensors.append(output_tensors[0])
180+
prob_tensors.append(output_tensors[1])
181+
pred_bbox = tf.concat(bbox_tensors, axis=1)
182+
pred_prob = tf.concat(prob_tensors, axis=1)
183+
boxes, pred_conf = filter_boxes(
184+
pred_bbox, pred_prob, score_threshold=score_thres, input_shape=tf.constant([416, 416])
185+
)
186+
pred = {'concat': tf.concat([boxes, pred_conf], axis=-1)}
187+
188+
for key, value in pred.items():
189+
boxes = value[:, :, 0:4]
190+
pred_conf = value[:, :, 4:]
191+
192+
boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
193+
boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
194+
scores=tf.reshape(pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
195+
max_output_size_per_class=50, max_total_size=50, iou_threshold=iou_threshold, score_threshold=score_threshold
196+
)
197+
output = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()]
198+
return output
199+
200+
201+
def result_to_json(image, pred_bbox):
202+
image_h, image_w, _ = image.shape
203+
out_boxes, out_scores, out_classes, num_boxes = pred_bbox
204+
class_names = {}
205+
json_result = []
206+
with open('model/coco.names', 'r') as data:
207+
for ID, name in enumerate(data):
208+
class_names[ID] = name.strip('\n')
209+
nums_class = len(class_names)
210+
211+
for i in range(num_boxes[0]):
212+
if int(out_classes[0][i]) < 0 or int(out_classes[0][i]) > nums_class: continue
213+
coor = out_boxes[0][i]
214+
coor[0] = int(coor[0] * image_h)
215+
coor[2] = int(coor[2] * image_h)
216+
coor[1] = int(coor[1] * image_w)
217+
coor[3] = int(coor[3] * image_w)
218+
219+
score = float(out_scores[0][i])
220+
class_ind = int(out_classes[0][i])
221+
bbox = np.array([coor[1], coor[0], coor[3], coor[2]]).tolist() # [x1,y1,x2,y2]
222+
json_result.append({'image': None, 'category_id': class_ind, 'bbox': bbox, 'score': score})
223+
224+
return json_result

tensorlayer/app/computer_vision_object_detection/yolov4.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def YOLOv4(NUM_CLASS, pretrained=False):
222222
network = Model(input_layer, [conv_sbbox, conv_mbbox, conv_lbbox])
223223

224224
if pretrained:
225-
restore_params(network, model_path='model/model.npz')
225+
restore_params(network, model_path='model/yolov4_model.npz')
226226

227227
return network
228228

@@ -236,7 +236,7 @@ def restore_params(network, model_path='models.npz'):
236236
print("Download the model file, placed in the /model ")
237237
print("Weights download: ", weights_url['link'], "password:", weights_url['password'])
238238

239-
txt_path = 'model/yolov4_config.txt'
239+
txt_path = 'model/yolov4_weights_config.txt'
240240
f = open(txt_path, "r")
241241
line = f.readlines()
242242
for i in range(len(line)):

tensorlayer/app/human_pose_estimation/LCN.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def restore_params(network, model_path='model.npz'):
288288
print("Download the model file, placed in the /model ")
289289
print("Weights download: ", weights_url['link'], "password:", weights_url['password'])
290290

291-
txt_path = 'model/pose_config.txt'
291+
txt_path = 'model/pose_weights_config.txt'
292292
f = open(txt_path, "r")
293293
line = f.readlines()
294294
for i in range(len(line)):
@@ -325,7 +325,7 @@ def CGCNN(pretrained=True):
325325
"""
326326
if pretrained:
327327
network = cgcnn_inference()
328-
restore_params(network, model_path='model/model.npz')
328+
restore_params(network, model_path='model/lcn_model.npz')
329329
else:
330330
network = cgcnn_train()
331331
return network

0 commit comments

Comments
 (0)