def load(self): # Remove nodes from graph or reset entire default graph tf.reset_default_graph() # Generate colors for drawing bounding boxes. hsv_tuples = [(x / len(self.class_names), 1., 1.) for x in range(len(self.class_names))] self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) self.colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), self.colors)) random.seed(10101) # Fixed seed for consistent colors across runs. random.shuffle(self.colors) # Shuffle colors to decorrelate adjacent classes. random.seed(None) # Reset seed to default. # Generate output tensor targets for filtered bounding boxes. self.x = tf.placeholder(tf.float32, shape=[None, Input_height, Input_width, channels]) self.image_shape = tf.placeholder(tf.float32, shape=[2,]) # self.is_training = tf.placeholder(tf.bool) # image_shape = np.array([image.size[0], image.size[1]]) # tf.placeholder(tf.float32, shape=[2,]) # Generate output tensor targets for filtered bounding boxes. # scale1, scale2, scale3 = YOLOv3(self.x, len(self.class_names), trainable=self.trainable, is_training=self.is_training).feature_extractor() scale1, scale2, scale3 = YOLOv3(self.x, len(self.class_names), trainable=self.trainable).feature_extractor() scale_total = [scale1, scale2, scale3] # detect # for 3D boxes, scores, classes, alphas, hwls = predict(scale_total, self.anchors, len(self.class_names), self.image_shape, score_threshold=self.threshold, iou_threshold=self.ignore_thresh) # Add ops to save and restore all the variables saver = tf.train.Saver(var_list=None if self.COCO==True else tf.trainable_variables()) # Allowing GPU memory growth config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config = config) sess.run(tf.global_variables_initializer()) epoch = input('Entrer a check point at epoch:') # For the case of COCO epoch = epoch if self.COCO == False else 2000 checkpoint = path + "/save_model/kitti/kitti_l2loss" + ".ckpt-" + str(epoch) try: aaa = checkpoint + '.meta' my_abs_path = Path(aaa).resolve() except FileNotFoundError: print("Not yet training!") else: saver.restore(sess, checkpoint) print("checkpoint: ", checkpoint) print("already training!") return boxes, scores, classes, alphas, hwls, sess
# tf Graph input #graph = tf.Graph() #global_step # input X = tf.placeholder(tf.float32, [None, 128, 128, 3]) # expected outputs Y1 = tf.placeholder(tf.float32, shape=[None, Input_shape/32, Input_shape/32, 3, (5+num_classes)]) Y2 = tf.placeholder(tf.float32, shape=[None, Input_shape/16, Input_shape/16, 3, (5+num_classes)]) Y3 = tf.placeholder(tf.float32, shape=[None, Input_shape/8, Input_shape/8, 3, (5+num_classes)]) # Generate output tensor targets for filtered bounding boxes. scale1, scale2, scale3 = YOLOv3(X, num_classes).feature_extractor() scale_total = [scale1, scale2, scale3] # detect boxes, scores, classes = predict(scale_total, anchors, num_classes, input_shape) # Label Y_ = [Y1, Y2, Y3] # Calculate loss loss = compute_loss(scale_total, Y_, anchors, num_classes, print_loss=True) # Optimizer optimizer = tf.train.AdamOptimizer(lr).minimize(loss) #, global_step=global_step) ################################################################################################################## # Init for saving models. They will be saved into a directory named 'checkpoints'. # Only the last checkpoint is kept. if not os.path.exists("checkpoints"): os.mkdir("checkpoints") saver = tf.train.Saver()
x_reshape = tf.reshape(X, [-1, Input_shape, Input_shape, 1]) tf.summary.image("input", x_reshape) # STEP 2: Building the graph ####################################################################################### # Building the graph # Generate output tensor targets for filtered bounding boxes. scale1, scale2, scale3 = YOLOv3(X, len(classes_data)).feature_extractor() scale_total = [] scale_total.append(scale1) scale_total.append(scale2) scale_total.append(scale3) with tf.name_scope("Loss"): # predict boxes, score, classes boxes, scores, classes = predict(scale_total, anchors, len(classes_data), S, score_threshold=threshold, iou_threshold=ignore_thresh) # Label y_true = preprocess_true_boxes(Y, 416, anchors, len(classes_data)) # Calculate loss loss = compute_loss(scale_total, y_true, anchors, len(classes_data)) with tf.name_scope("Optimizer"): # optimizer learning_rate = 0.001 decay = 0.0005 optimizer = tf.train.RMSPropOptimizer(learning_rate, decay).minimize(loss) # STEP 3: Build the evaluation step ################################################################################ # with tf.name_scope("Accuracy"): # # Model evaluation
def detect_image(self, image): start = time.time() # Generate colors for drawing bounding boxes. hsv_tuples = [(x / len(self.class_names), 1., 1.) for x in range(len(self.class_names))] self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) self.colors = list( map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), self.colors)) random.seed(10101) # Fixed seed for consistent colors across runs. random.shuffle( self.colors) # Shuffle colors to decorrelate adjacent classes. random.seed(None) # Reset seed to default. if self.is_fixed_size: assert self.INPUT_SIZE[0] % 32 == 0, 'Multiples of 32 required' assert self.INPUT_SIZE[1] % 32 == 0, 'Multiples of 32 required' boxed_image, image_shape = letterbox_image( image, tuple(reversed(self.INPUT_SIZE))) # boxed_image, image_shape = resize_image(image, tuple(reversed(self.INPUT_SIZE))) else: new_image_size = (image.width - (image.width % 32), image.height - (image.height % 32)) boxed_image, image_shape = letterbox_image(image, new_image_size) # boxed_image, image_shape = resize_image(image, new_image_size) image_data = np.array(boxed_image, dtype='float32') print("heights, widths:", image_shape) image_data /= 255. inputs = np.expand_dims(image_data, 0) # Add batch dimension. # # Generate output tensor targets for filtered bounding boxes. x = tf.placeholder(tf.float32, shape=[None, Input_shape, Input_shape, channels]) # image_shape = np.array([image.size[0], image.size[1]]) # tf.placeholder(tf.float32, shape=[2,]) # Generate output tensor targets for filtered bounding boxes. scale1, scale2, scale3 = YOLOv3(x, len( self.class_names)).feature_extractor() scale_total = [scale1, scale2, scale3] # detect boxes, scores, classes = predict(scale_total, self.anchors, len(self.class_names), image_shape, score_threshold=self.threshold, iou_threshold=self.ignore_thresh) # Add ops to save and restore all the variables. saver = tf.train.Saver() writer = tf.summary.FileWriter('./graphs', tf.get_default_graph()) # tensorboard --logdir="./graphs" --port 6006 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) # Restore variables from disk. epoch = input('Entrer a check point at epoch(%10=0):') checkpoint = "/home/minh/stage/saver_model/model" + str( epoch) + ".ckpt" try: my_abs_path = Path(checkpoint).resolve() saver.restore(sess, checkpoint) except FileNotFoundError: print("Not yet training!") else: print("already training!") out_boxes, out_scores, out_classes = sess.run( [boxes, scores, classes], feed_dict={x: inputs}) writer.close() print('Found {} boxes for {}'.format(len(out_boxes), 'img')) # Visualisation################################################################################################# font = ImageFont.truetype(font='font/FiraMono-Medium.otf', size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32')) thickness = (image.size[0] + image.size[1]) // 400 # do day cua BB for i, c in reversed(list(enumerate(out_classes))): predicted_class = self.class_names[c] box = out_boxes[i] score = out_scores[i] label = '{} {:.2f}'.format(predicted_class, score) draw = ImageDraw.Draw(image) label_size = draw.textsize(label, font) top, left, bottom, right = box # y_min, x_min, y_max, x_max top = max(0, np.floor(top + 0.5).astype('int32')) left = max(0, np.floor(left + 0.5).astype('int32')) bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32')) right = min(image.size[0], np.floor(right + 0.5).astype('int32')) print(label, (left, top), (right, bottom)) # (x_min, y_min), (x_max, y_max) if top - label_size[1] >= 0: text_origin = np.array([left, top - label_size[1]]) else: text_origin = np.array([left, top + 1]) # My kingdom for a good redistributable image drawing library. for j in range(thickness): draw.rectangle([left + j, top + j, right - j, bottom - j], outline=self.colors[c]) draw.rectangle( [tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[c]) draw.text(text_origin, label, fill=(0, 0, 0), font=font) del draw end = time.time() print(end - start) return image