def __init__(self, test_weight): log_dir = os.path.join(cfg.LOG_DIR, 'test') moving_ave_decay = cfg.MOVING_AVE_DECAY test_weight_path = os.path.join(cfg.WEIGHTS_DIR, test_weight) with tf.name_scope('input'): input_data = tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3], name='input_data') training = tf.placeholder(dtype=tf.bool, name='training') if cfg.Mode == "YOLOv3": _, _, _, pred_sbbox, pred_mbbox, pred_lbbox = YOLOV3( training).build_nework(input_data) elif cfg.Mode == "MobileNet": _, _, _, pred_sbbox, pred_mbbox, pred_lbbox = MobileNet_YOLOV3( training).build_nework(input_data) with tf.name_scope('summary'): tf.summary.FileWriter(log_dir).add_graph(tf.get_default_graph()) with tf.name_scope('ema'): ema_obj = tf.train.ExponentialMovingAverage(moving_ave_decay) self.__sess = tf.Session() saver = tf.train.Saver(ema_obj.variables_to_restore()) saver.restore(self.__sess, test_weight_path) super(Yolo_test, self).__init__(self.__sess, input_data, training, pred_sbbox, pred_mbbox, pred_lbbox)
def __init__(self): self.__train_input_sizes = cfg.TRAIN_INPUT_SIZES self.__multi_test = cfg.MULTI_TEST self.__flip_test = cfg.FLIP_TEST self.__test_input_size = cfg.TEST_INPUT_SIZE self.__anchor_per_scale = cfg.ANCHOR_PER_SCALE self.__classes = cfg.CLASSES self.__num_classes = len(self.__classes) self.__class_to_ind = dict( zip(self.__classes, range(self.__num_classes))) self.__anchors = np.array(cfg.ANCHORS) self.__score_threshold = cfg.SCORE_THRESHOLD self.__iou_threshold = cfg.IOU_THRESHOLD self.__log_dir = os.path.join(cfg.LOG_DIR, 'test') self.__annot_dir_path = cfg.ANNOT_DIR_PATH self.__moving_ave_decay = cfg.MOVING_AVE_DECAY self.__dataset_path = cfg.DATASET_PATH with tf.name_scope('input'): self.__input_data = tf.placeholder(dtype=tf.float32, name='input_data') self.__training = tf.placeholder(dtype=tf.bool, name='training') _, _, _, self.__pred_sbbox, self.__pred_mbbox, self.__pred_lbbox = YOLOV3( self.__training).build_nework(self.__input_data) with tf.name_scope('summary'): tf.summary.FileWriter(self.__log_dir).add_graph( tf.get_default_graph()) with tf.name_scope('ema'): ema_obj = tf.train.ExponentialMovingAverage( self.__moving_ave_decay) self.__sess = tf.Session() self.__saver = tf.train.Saver(ema_obj.variables_to_restore()) self.__ckpt = tf.train.get_checkpoint_state(cfg.WEIGHTS_DIR) print('-------------->>>>> load model {}'.format(self.__ckpt)) self.__saver.restore(self.__sess, self.__ckpt.model_checkpoint_path)
def __init__(self, test_weight): log_dir = os.path.join(cfg.LOG_DIR, 'test') test_weight_path = os.path.join(cfg.WEIGHTS_DIR, test_weight) with tf.name_scope('input'): input_data = tf.placeholder(dtype=tf.float32, name='input_data') training = tf.placeholder(dtype=tf.bool, name='training') _, _, _, pred_sbbox, pred_mbbox, pred_lbbox = YOLOV3( training).build_nework(input_data) with tf.name_scope('summary'): tf.summary.FileWriter(log_dir).add_graph(tf.get_default_graph()) self.__sess = tf.Session() net_vars = tf.get_collection('YoloV3') saver = tf.train.Saver(net_vars) saver.restore(self.__sess, test_weight_path) super(Yolo_test, self).__init__(self.__sess, input_data, training, pred_sbbox, pred_mbbox, pred_lbbox)
def freeze_graph(checkpoint_path, output_node_names, savename): with tf.name_scope('input'): input_data = tf.placeholder(dtype=tf.float32, shape=(1, INPUTSIZE, INPUTSIZE, 3), name='input_data') training = tf.placeholder(dtype=tf.bool, name='training') output = YOLOV3(training).build_nework_MNN(input_data,inputsize=INPUTSIZE) with tf.Session() as sess: net_vars = tf.get_collection('YoloV3') saver = tf.train.Saver(net_vars) saver.restore(sess, checkpoint_path) output_graph_def = tf.graph_util.convert_variables_to_constants( sess=sess, input_graph_def=sess.graph_def, output_node_names=output_node_names.split(",")) with tf.gfile.GFile('{}/{}'.format('port', savename), "wb") as f: f.write(output_graph_def.SerializeToString()) for node in output_graph_def.node: if 'strided_slice' in node.name: print node.name, node.input print("%d ops in the final graph." % len(output_graph_def.node))
def __init__(self): self.__learn_rate_init = cfg.LEARN_RATE_INIT self.__learn_rate_end = cfg.LEARN_RATE_END self.__max_periods = cfg.MAX_PERIODS self.__warmup_periods = cfg.WARMUP_PERIODS self.__weights_dir = cfg.WEIGHTS_DIR self.__weights_init = cfg.WEIGHTS_INIT self.__time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) self.__log_dir = os.path.join(cfg.LOG_DIR, 'train', self.__time) self.__moving_ave_decay = cfg.MOVING_AVE_DECAY self.__train_data = Data('train') self.__test_data = Data('test') self.__steps_per_period = len(self.__train_data) with tf.name_scope('input'): self.__input_data = tf.placeholder(dtype=tf.float32, name='input_data') self.__label_sbbox = tf.placeholder(dtype=tf.float32, name='label_sbbox') self.__label_mbbox = tf.placeholder(dtype=tf.float32, name='label_mbbox') self.__label_lbbox = tf.placeholder(dtype=tf.float32, name='label_lbbox') self.__sbboxes = tf.placeholder(dtype=tf.float32, name='sbboxes') self.__mbboxes = tf.placeholder(dtype=tf.float32, name='mbboxes') self.__lbboxes = tf.placeholder(dtype=tf.float32, name='lbboxes') self.__training = tf.placeholder(dtype=tf.bool, name='training') with tf.name_scope('learning_rate'): self.__global_step = tf.Variable(1.0, dtype=tf.float64, trainable=False, name='global_step') warmup_steps = tf.constant(self.__warmup_periods * self.__steps_per_period, dtype=tf.float64, name='warmup_steps') train_steps = tf.constant(self.__max_periods * self.__steps_per_period, dtype=tf.float64, name='train_steps') self.__learn_rate = tf.cond(pred=self.__global_step < warmup_steps, true_fn=lambda: self.__global_step / warmup_steps * self.__learn_rate_init, false_fn=lambda: self.__learn_rate_end + 0.5 * (self.__learn_rate_init - self.__learn_rate_end) * (1 + tf.cos((self.__global_step - warmup_steps) / (train_steps - warmup_steps) * np.pi))) global_step_update = tf.assign_add(self.__global_step, 1.0) # 相加操作 yolo = YOLOV3(self.__training) conv_sbbox, conv_mbbox, conv_lbbox, pred_sbbox, pred_mbbox, pred_lbbox = yolo.build_nework(self.__input_data) load_var = tf.global_variables('yolov3') restore_dict = self.__get_restore_dict(load_var) self.__loss = yolo.loss(conv_sbbox, conv_mbbox, conv_lbbox, pred_sbbox, pred_mbbox, pred_lbbox, self.__label_sbbox, self.__label_mbbox, self.__label_lbbox, self.__sbboxes, self.__mbboxes, self.__lbboxes) with tf.name_scope('optimizer'): moving_ave = tf.train.ExponentialMovingAverage(self.__moving_ave_decay).apply(tf.trainable_variables()) optimizer = tf.train.AdamOptimizer(self.__learn_rate).minimize(self.__loss, var_list=tf.trainable_variables()) with tf.control_dependencies([optimizer, global_step_update]): with tf.control_dependencies([moving_ave]): self.__train_op = tf.no_op() with tf.name_scope('load_save'): self.__load = tf.train.Saver(restore_dict) self.__save = tf.train.Saver(tf.global_variables(), max_to_keep=5) with tf.name_scope('summary'): self.__loss_ave = tf.Variable(0, dtype=tf.float32, trainable=False) tf.summary.scalar('loss_ave', self.__loss_ave) tf.summary.scalar('learn_rate', self.__learn_rate) self.__summary_op = tf.summary.merge_all() self.__summary_writer = tf.summary.FileWriter(self.__log_dir) self.__summary_writer.add_graph(tf.get_default_graph()) self.__sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) self.__sess.run(tf.global_variables_initializer()) logging.info('Restoring weights from:\t %s' % self.__weights_init) self.__load.restore(self.__sess, self.__weights_init)
# conding :utf-8 import sys import os sys.path.append(os.path.abspath('../')) import tensorflow as tf from model.head.yolov3 import YOLOV3 os.environ['CUDA_VISIBLE_DEVICES'] = '0' # params ckpt_path = '../weights/' saver_path = './predicte_model/yolo.ckpt' ckpt = tf.train.get_checkpoint_state(ckpt_path) if not os.path.exists(saver_path): os.makedirs(saver_path) training = tf.placeholder(dtype=tf.bool, name='training') input_data = tf.placeholder(dtype=tf.float32, name='input_data') yolo = YOLOV3(training) _, _, _, pred_sbbox, pred_mbbox, pred_lbbox = yolo.build_nework(input_data) saver_to_restore = tf.train.Saver() saver_to_save = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver_to_restore.restore(sess, ckpt.model_checkpoint_path) saver_to_save.save(sess, saver_path)
import tensorflow as tf from model.head.yolov3 import YOLOV3 pb_file = "weights/yolov3_lite_freeze_graph.pb" ckpt_file = "weights/yolo.ckpt-60-0.7911" output_node_names = [ "input/input_data", "YoloV3/pred_sbbox/concat_2", "YoloV3/pred_mbbox/concat_2", "YoloV3/pred_lbbox/concat_2" ] with tf.name_scope('input'): input_data = tf.placeholder(dtype=tf.float32, shape=(1, 416, 416, 3), name='input_data') training = tf.constant(False, dtype=tf.bool, name='training') _, _, _, pred_sbbox, pred_mbbox, pred_lbbox = YOLOV3(training).build_nework( input_data) sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) saver = tf.train.Saver() tf.global_variables_initializer() tf.local_variables_initializer() saver.restore(sess, ckpt_file) graphdef = sess.graph.as_graph_def() for node in graphdef.node: if node.op == 'RefSwitch': node.op = 'Switch' for index in range(len(node.input)): if 'moving_' in node.input[index]: node.input[index] = node.input[index] + '/read' elif node.op == 'AssignSub':
def __init__(self): self.__learn_rate_init = cfg.LEARN_RATE_INIT self.__learn_rate_end = cfg.LEARN_RATE_END self.__max_periods = cfg.MAX_PERIODS self.__warmup_periods = cfg.WARMUP_PERIODS self.__weights_dir = cfg.WEIGHTS_DIR self.__weights_init = cfg.WEIGHTS_INIT self.__log_dir = os.path.join( cfg.LOG_DIR, 'train', time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))) os.makedirs(self.__log_dir) logging.basicConfig(filename=self.__log_dir + '.log', format='%(filename)s %(asctime)s\t%(message)s', level=logging.DEBUG, datefmt='%Y-%m-%d %I:%M:%S', filemode='w') self.__train_data = Data() self.__steps_per_period = len(self.__train_data) with tf.name_scope('input'): self.__input_data = tf.placeholder(dtype=tf.float32, name='input_data') self.__label_sbbox = tf.placeholder(dtype=tf.float32, name='label_sbbox') self.__label_mbbox = tf.placeholder(dtype=tf.float32, name='label_mbbox') self.__label_lbbox = tf.placeholder(dtype=tf.float32, name='label_lbbox') self.__sbboxes = tf.placeholder(dtype=tf.float32, name='sbboxes') self.__mbboxes = tf.placeholder(dtype=tf.float32, name='mbboxes') self.__lbboxes = tf.placeholder(dtype=tf.float32, name='lbboxes') self.__training = tf.placeholder(dtype=tf.bool, name='training') with tf.name_scope('learning_rate'): self.__global_step = tf.Variable(1.0, dtype=tf.float64, trainable=False, name='global_step') warmup_steps = tf.constant(self.__warmup_periods * self.__steps_per_period, dtype=tf.float64, name='warmup_steps') train_steps = tf.constant(self.__max_periods * self.__steps_per_period, dtype=tf.float64, name='train_steps') self.__learn_rate = tf.cond( pred=self.__global_step < warmup_steps, true_fn=lambda: self.__global_step / warmup_steps * self. __learn_rate_init, false_fn=lambda: self.__learn_rate_end + 0.5 * (self.__learn_rate_init - self.__learn_rate_end) * (1 + tf.cos( (self.__global_step - warmup_steps) / (train_steps - warmup_steps) * np.pi))) global_step_update = tf.assign_add(self.__global_step, 1.0) yolo = YOLOV3(self.__training) conv_sbbox, conv_mbbox, conv_lbbox, \ pred_sbbox, pred_mbbox, pred_lbbox = yolo.build_nework(self.__input_data) self.__loss = yolo.loss(conv_sbbox, conv_mbbox, conv_lbbox, pred_sbbox, pred_mbbox, pred_lbbox, self.__label_sbbox, self.__label_mbbox, self.__label_lbbox, self.__sbboxes, self.__mbboxes, self.__lbboxes) with tf.name_scope('optimizer'): optimizer = tf.train.AdamOptimizer(self.__learn_rate).\ minimize(self.__loss, var_list=tf.trainable_variables()) with tf.control_dependencies([optimizer, global_step_update]): self.__train_op = tf.no_op() with tf.name_scope('load_save'): net_vars = tf.get_collection('YoloV3') restore_dict = {} for var in net_vars: var_name = str(var.op.name) splited_name = var_name.split('/') if splited_name[1] == 'MobilenetV2': org_name = '/'.join(splited_name[1:]) restore_dict[org_name] = var logging.info('\t' + str(var_name)) logging.info('\t' + str(org_name)) self.__load = tf.train.Saver(restore_dict) self.__save = tf.train.Saver(net_vars, max_to_keep=self.__max_periods) with tf.name_scope('summary'): self.__loss_ave = tf.Variable(0, dtype=tf.float32, trainable=False) tf.summary.scalar('loss_ave', self.__loss_ave) tf.summary.scalar('learn_rate', self.__learn_rate) self.__summary_op = tf.summary.merge_all() self.__summary_writer = tf.summary.FileWriter(self.__log_dir) self.__summary_writer.add_graph(tf.get_default_graph()) self.__sess = tf.Session(config=tf.ConfigProto( allow_soft_placement=True)) self.__sess.run(tf.global_variables_initializer()) logging.info('Restoring weights from:\t %s' % self.__weights_init) self.__load.restore(self.__sess, self.__weights_init) super(Yolo_train, self).__init__(self.__sess, self.__input_data, self.__training, pred_sbbox, pred_mbbox, pred_lbbox)