with tf.name_scope("Loss_and_Detect"): # Label y_predict = [Y1, Y2, Y3] # Calculate loss loss = compute_loss(scale_total, y_predict, anchors, len(classes_data), print_loss=False) image_shappe = tf.placeholder(tf.float32, shape=[ 2, ]) boxes, scores, classes = predict(scale_total, anchors, len(classes_data), image_shappe, score_threshold=threshold, iou_threshold=ignore_thresh) # loss_print = compute_loss(scale_total, y_predict, anchors, len(classes_data), print_loss=False) tf.summary.scalar("Loss", loss) with tf.name_scope("Optimizer"): # optimizer # for VOC: lr:0.0001, decay:0.0003 with RMSProOp after 60 epochs # learning_rate = tf.placeholder(tf.float32, shape=[1], name='lr') # decay = 0.0003 # optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss) applied_learning_rate = tf.placeholder(tf.float32) optimizer = tf.train.AdamOptimizer( learning_rate=applied_learning_rate).minimize( loss, global_step=global_step)
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 * 1.0 / 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_shape, Input_shape, channels]) self.image_shape = 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).feature_extractor() scale_total = [scale1, scale2, scale3] # detect boxes, scores, classes = predict(scale_total, self.anchors, len(self.class_names), self.image_shape, max_boxes=max_boxes, 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 # Initialize tf session sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) # # For the case of COCO if (self.trainable): epoch = num_epochs if self.COCO == False else 2000 checkpoint = model_checkpoint_path + '/model.ckpt-' + str(epoch - 1) 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, sess