def freeze_graph_test(pb_path, outnode): with tf.Graph().as_default(): output_graph_def = tf.GraphDef() with open(pb_path, "rb") as f: output_graph_def.ParseFromString(f.read()) for node in output_graph_def.node: if node.op == 'Conv2D' and 'explicit_paddings' in node.attr: del node.attr['explicit_paddings'] if node.op == 'ResizeNearestNeighbor' and 'half_pixel_centers' in node.attr: del node.attr['half_pixel_centers'] tf.import_graph_def(output_graph_def, name="") with tf.Session() as sess: summary_writer = tf.summary.FileWriter('mylog/new2') summary_writer.add_graph(tf.get_default_graph()) sess.run(tf.global_variables_initializer()) img = cv2.imread('004650.jpg') originimg = img orishape = img.shape img = tools.img_preprocess2(img, None, (INPUTSIZE, INPUTSIZE), False)[np.newaxis, ...] img = img.astype(np.float32) outbox = sess.graph.get_tensor_by_name('YoloV3/output/boxconcat:0') inputdata = sess.graph.get_tensor_by_name("input/input_data:0") outbox = sess.run(outbox, feed_dict={inputdata: img}) outbox = np.array(postprocess(outbox, INPUTSIZE, orishape[:2])) originimg = tools.draw_bbox(originimg, outbox, CLASSES) cv2.imwrite('004650_detected.jpg', originimg)
def __parse_annotation(self, annotation): """ 读取annotation中image_path对应的图片,并将该图片进行resize(不改变图片的高宽比) 获取annotation中所有的bbox,并将这些bbox的坐标(xmin, ymin, xmax, ymax)进行纠正, 使得纠正后bbox在resize后的图片中的相对位置与纠正前bbox在resize前的图片中的相对位置相同 :param annotation: 图片地址和bbox的坐标、类别, 如:'image_path xmin,ymin,xmax,ymax,class_ind xmin,ymin,xmax,ymax,class_ind ...' :return: image和bboxes bboxes的shape为(N, 5),其中N表示一站图中有N个bbox,5表示(xmin, ymin, xmax, ymax, class_ind) """ line = annotation.split() image_path = line[0] image = np.array(cv2.imread(image_path)) bboxes = np.array([map(int, box.split(',')) for box in line[1:]]) image, bboxes = dataAug.random_horizontal_flip(np.copy(image), np.copy(bboxes)) image, bboxes = dataAug.random_vertical_flip(np.copy(image), np.copy(bboxes)) image, bboxes = dataAug.random_rotation90_flip(np.copy(image), np.copy(bboxes)) image, bboxes = dataAug.random_crop(np.copy(image), np.copy(bboxes)) image, bboxes = tools.img_preprocess2( np.copy(image), np.copy(bboxes), (self.__train_input_size, self.__train_input_size), True) return image, bboxes
def __predict(self, image, test_input_size, valid_scale): org_image = np.copy(image) org_h, org_w, _ = org_image.shape cur_milli_time = lambda: int(round(time.time() * 1000)) start_time = cur_milli_time() yolo_input = tools.img_preprocess2(image, None, (test_input_size, test_input_size), False) yolo_input = yolo_input[np.newaxis, ...] self.__time_pre += (cur_milli_time() - start_time) start_time = cur_milli_time() pred_sbbox, pred_mbbox, pred_lbbox = self.__sess.run( [self.__pred_sbbox, self.__pred_mbbox, self.__pred_lbbox], feed_dict={ self.__input_data: yolo_input, self.__training: False }) self.__time_inf += (cur_milli_time() - start_time) start_time = cur_milli_time() pred_bbox = np.concatenate([ np.reshape(pred_sbbox, (-1, 5 + self._num_classes)), np.reshape(pred_mbbox, (-1, 5 + self._num_classes)), np.reshape(pred_lbbox, (-1, 5 + self._num_classes)) ], axis=0) bboxes = self.__convert_pred(pred_bbox, test_input_size, (org_h, org_w), valid_scale) self.__time_pos += (cur_milli_time() - start_time) return bboxes
def __parse_annotation(self, annotation): line = annotation.split() image_path = line[0] image = np.array(cv2.imread(image_path)) bboxes = np.array([box.split(',') for box in line[1:]], dtype=int) image, bboxes = dataAug.random_horizontal_flip(np.copy(image), np.copy(bboxes)) image, bboxes = dataAug.random_crop(np.copy(image), np.copy(bboxes)) image, bboxes = dataAug.random_translate(np.copy(image), np.copy(bboxes)) image, bboxes = tools.img_preprocess2(np.copy(image), np.copy(bboxes), (self.__train_input_size, self.__train_input_size), True) return image, bboxes
def __predict(self, image, test_input_size, valid_scale): org_image = np.copy(image) org_h, org_w, _ = org_image.shape yolo_input = tools.img_preprocess2(image, None, (test_input_size, test_input_size), False) yolo_input = yolo_input[np.newaxis, ...] pred_mbbox, pred_lbbox = self.__sess.run( [self.__pred_mbbox, self.__pred_lbbox], feed_dict={ self.__input_data: yolo_input, self.__training: False }) pred_bbox = np.concatenate([ np.reshape(pred_mbbox, (-1, 5 + self._num_classes)), np.reshape(pred_lbbox, (-1, 5 + self._num_classes)) ], axis=0) bboxes = self.__convert_pred(pred_bbox, test_input_size, (org_h, org_w), valid_scale) return bboxes