from modelv3 import yolov3 from utils.data_aug import letterbox_resize from utils.misc_utils import parse_anchors, read_class_names from utils.nms_utils import gpu_nms from utils.plot_utils import get_color_table, plot_one_box config = tf.compat.v1.ConfigProto(gpu_options=tf.compat.v1.GPUOptions(allow_growth=True)) anchor_path = "./data/yolo_anchors.txt" class_name_path = "./data/coco.names" anchors = parse_anchors(anchor_path) classes = read_class_names(class_name_path) class_num = len(classes) color_table = get_color_table(class_num) img_size = [416, 416] yolo_model = yolov3(class_num, anchors) yolo_model.set_img_size(np.asarray(img_size)) sess = tf.Session(config=config) with gfile.FastGFile("./pb_model/frozen_model_v3.pb",'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) sess.graph.as_default() tf.import_graph_def(graph_def, name='') sess.run(tf.global_variables_initializer()) input = sess.graph.get_tensor_by_name('image:0') feature_map_1 = sess.graph.get_tensor_by_name('yolov3/yolov3_head/feature_map_1:0') feature_map_2 = sess.graph.get_tensor_by_name('yolov3/yolov3_head/feature_map_2:0') feature_map_3 = sess.graph.get_tensor_by_name('yolov3/yolov3_head/feature_map_3:0') pred_boxes, pred_confs, pred_probs = yolo_model.predict([feature_map_1, feature_map_2, feature_map_3]) pred_scores = pred_confs * pred_probs
import tensorflow as tf from modelv3 import yolov3 from utils.misc_utils import parse_anchors, load_weights num_class = 80 img_size = 416 weight_path = './data/darknet_weights_v3/yolov3.weights' save_path = './data/darknet_weights_v3/yolov3.ckpt' anchors = parse_anchors('./data/yolo_anchors.txt') model = yolov3(80, anchors) with tf.Session() as sess: inputs = tf.placeholder(tf.float32, [1, img_size, img_size, 3]) with tf.variable_scope('yolov3'): feature_map = model.forward(inputs) saver = tf.train.Saver(var_list=tf.global_variables(scope='yolov3')) load_ops = load_weights(tf.global_variables(scope='yolov3'), weight_path) sess.run(load_ops) saver.save(sess, save_path=save_path) print('TensorFlow model checkpoint has been saved to {}'.format(save_path))
import os import tensorflow as tf from modelv3 import yolov3 # params restore_path = './data/darknet_weights_v3/' class_num = 80 save_dir = './ckpt_without_optimizer/' if not os.path.exists(save_dir): os.makedirs(save_dir) image = tf.placeholder(tf.float32, [1, 416, 416, 3]) yolo_model = yolov3(class_num, None) with tf.variable_scope('yolov3'): pred_feature_maps = yolo_model.forward(image) saver_to_restore = tf.train.Saver() saver_to_save = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) ckpt = tf.compat.v1.train.get_checkpoint_state(restore_path) if ckpt and ckpt.model_checkpoint_path: saver_to_restore.restore(sess, ckpt.model_checkpoint_path) saver_to_save.save( sess, save_dir + ckpt.model_checkpoint_path.split('/')[-1]) print("done") else: print("message:can not fint ckpt model")
image_ids, image, y_true_13, y_true_26, y_true_52 = iterator.get_next() y_true = [y_true_13, y_true_26, y_true_52] # tf.data pipeline will lose the data `static` shape, so we need to set it manually image_ids.set_shape([None]) image.set_shape([None, None, None, 3]) for y in y_true: y.set_shape([None, None, None, None, None]) ################## # Model definition ################## yolo_model = yolov3(args.class_num, args.anchors, args.use_label_smooth, args.use_focal_loss, args.batch_norm_decay, args.weight_decay, use_static_shape=False) with tf.variable_scope('yolov3'): pred_feature_maps = yolo_model.forward(image, is_training=is_training) loss = yolo_model.compute_loss(pred_feature_maps, y_true) y_pred = yolo_model.predict(pred_feature_maps) l2_loss = tf.losses.get_regularization_loss() # setting restore parts and vars to update saver_to_restore = tf.train.Saver( var_list=tf.contrib.framework.get_variables_to_restore( include=args.restore_include, exclude=args.restore_exclude)) update_vars = tf.contrib.framework.get_variables_to_restore(