def inception_v2_ssd(img): with slim.arg_scope(inception_v2.inception_v2_arg_scope()): logits, end_point = inception_v2.inception_v2_base(img) c1 = end_point['Mixed_3c'] c2 = end_point['Mixed_4e'] c3 = end_point['Mixed_5c'] return c1, c2, c3
def test_inception_v2(img_dir): """ Test Inception-V2 with a single image. :param img_dir: Path of the image to be classified :return: classification result and probability of a single image """ img = cv2.imread(img_dir) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (224, 224)) / 255 img = img.reshape((1, 224, 224, 3)) tf.reset_default_graph() inputs = tf.placeholder(name='input_images', shape=[None, 224, 224, 3], dtype=tf.float32) with slim.arg_scope(inception_v2_arg_scope()): _, _ = inception_v2(inputs, 1001, is_training=False) with tf.Session() as sess: tf.train.Saver().restore(sess, './models/inception_v2.ckpt') inputs = sess.graph.get_tensor_by_name('input_images:0') outputs = sess.graph.get_tensor_by_name( 'InceptionV2/Logits/SpatialSqueeze:0') pred = tf.argmax(tf.nn.softmax(outputs), axis=1)[0] prob = tf.reduce_max(tf.nn.softmax(outputs), axis=1)[0] pred, prob = sess.run([pred, prob], feed_dict={inputs: img}) name = label_dict[pred] print('Result of Inception-V2:', name, prob) return name, prob
def inception_v2_ssd(img): with slim.arg_scope(inception_v2.inception_v2_arg_scope()): logits, end_point = inception_v2.inception_v2_base(img) vbs = slim.get_variables_to_restore() c1 = end_point['Mixed_3c'] c2 = end_point['Mixed_4e'] c3 = end_point['Mixed_5c'] return c1, c2, c3, vbs
def inception_v2_ssd(img,cfg): with slim.arg_scope(inception_v2.inception_v2_arg_scope()): logits, end_point = inception_v2.inception_v2_base(img) Mixed_3c = end_point['Mixed_3c'] Mixed_4e = end_point['Mixed_4e'] cell_11 = end_point['Mixed_5c'] vbs = slim.get_trainable_variables() cell_11 = tf.image.resize_bilinear(cell_11,size=[32,32]) cell_11 = tf.concat([cell_11,Mixed_4e],axis=3) cell_7 = tf.image.resize_bilinear(Mixed_4e,size=[64,64]) cell_7 = tf.concat([cell_7,Mixed_3c],axis=3) cell_11 = slim.conv2d(cell_11,1024,kernel_size=1,activation_fn=slim.nn.relu) cell_7 = slim.conv2d(cell_7, 512, kernel_size=3, activation_fn=slim.nn.relu) cell_7 = slim.conv2d(cell_7, 256, kernel_size=1, activation_fn=slim.nn.relu) cv6 = slim.conv2d(cell_11, 1024, kernel_size=3, rate=6, activation_fn=slim.nn.relu, scope='conv6') cv7 = slim.conv2d(cv6, 1024, kernel_size=1, activation_fn=slim.nn.relu, scope='conv7') s = utils.normalize_to_target(cell_7, target_norm_value=12.0, dim=1) cv8 = slim.conv2d(cv7, 256, kernel_size=1, stride=1, scope='conv8_0') cv8 = slim.conv2d(cv8, 512, kernel_size=3, stride=2, scope='conv8_1') cv9 = slim.conv2d(cv8, 128, kernel_size=1, stride=1, scope='conv9_0') cv9 = slim.conv2d(cv9, 256, kernel_size=3, stride=2, scope='conv9_1') cv10 = slim.conv2d(cv9, 128, kernel_size=1, stride=1, scope='conv10_0') cv10 = slim.conv2d(cv10, 256, kernel_size=3, stride=2, scope='conv10_1') cv11 = slim.conv2d(cv10, 128, kernel_size=1, stride=1, scope='conv11_0') cv11 = slim.conv2d(cv11, 256, kernel_size=3, stride=2, scope='conv11_1') source = [s, cv7, cv8, cv9, cv10, cv11] conf = [] loc = [] for cv, num in zip(source, cfg.Config['aspect_num']): print(num) loc.append(slim.conv2d(cv, num * 4, kernel_size=3, stride=1, activation_fn=None)) conf.append( slim.conv2d(cv, num * cfg.Config['num_classes'], kernel_size=3, stride=1, activation_fn=None)) print(loc) loc = tf.concat([tf.reshape(o, shape=(cfg.batch_size, -1, 4)) for o in loc], axis=1) conf = tf.concat([tf.reshape(o, shape=(cfg.batch_size, -1, cfg.Config['num_classes'])) for o in conf], axis=1) return loc, conf, vbs
def inception_v2_ssd(img,cfg): with slim.arg_scope(inception_v2.inception_v2_arg_scope()): logits, end_point = inception_v2.inception_v2_base(img) Mixed_3c = end_point['Mixed_3c'] Mixed_4e = end_point['Mixed_4e'] Mixed_5c = end_point['Mixed_5c'] vbs = slim.get_trainable_variables() #vbs = None cell_11 = tf.image.resize_bilinear(Mixed_5c,size=[int(32*(cfg.image_size/512)),int(32*(cfg.image_size/512))]) cell_11 = tf.concat([cell_11,Mixed_4e],axis=3) cell_7 = tf.image.resize_bilinear(Mixed_4e,size=[int(64*(cfg.image_size/512)),int(64*(cfg.image_size/512))]) cell_7 = tf.concat([cell_7,Mixed_3c],axis=3) mask_fp = get_mask_fp(Mixed_3c ,Mixed_4e,Mixed_5c) cell_11 = slim.conv2d(cell_11,1024,kernel_size=1,activation_fn=slim.nn.relu) cell_7 = slim.conv2d(cell_7, 512, kernel_size=3, activation_fn=slim.nn.relu) cell_7 = slim.conv2d(cell_7, 256, kernel_size=1, activation_fn=slim.nn.relu) cv6 = slim.conv2d(cell_11, 1024, kernel_size=3, rate=6, activation_fn=slim.nn.relu, scope='conv6') cv7 = slim.conv2d(cv6, 1024, kernel_size=1, activation_fn=slim.nn.relu, scope='conv7') s = utils.normalize_to_target(cell_7, target_norm_value=cfg.norm_value, dim=1) cv8 = inception(cv7, out_put=512, name='cv8', stride=2) cv9 = inception(cv8, out_put=256, name='cv9', stride=2) cv10 = inception(cv9, out_put=256, name='cv10', stride=2) cv11 = inception(cv10, out_put=256,name= 'cv11', stride=2) source = [s, cv7, cv8, cv9, cv10, cv11] conf = [] loc = [] for cv, num in zip(source, cfg.Config['aspect_num']): loc.append(slim.conv2d(cv, num * 4, kernel_size=3, stride=1, activation_fn=None)) conf.append( slim.conv2d(cv, num * cfg.Config['num_classes'], kernel_size=3, stride=1, activation_fn=None)) loc = tf.concat([tf.reshape(o, shape=(cfg.batch_size, -1, 4)) for o in loc], axis=1) conf = tf.concat([tf.reshape(o, shape=(cfg.batch_size, -1, cfg.Config['num_classes'])) for o in conf], axis=1) return loc, conf,mask_fp, vbs
def model(img): with slim.arg_scope(inception_v2.inception_v2_arg_scope()): with slim.arg_scope([slim.batch_norm], is_training=True): with slim.arg_scope([slim.conv2d], trainable=True): logits, end_point = inception_v2.inception_v2_base(img) c1 = end_point['Mixed_3c'] c2 = end_point['Mixed_4e'] c3 = end_point['Mixed_5c'] vbs = slim.get_variables_to_restore() c3 = slim.conv2d(c3, 256, 1, 1, activation_fn=None) c2 = slim.conv2d(c2, 256, 1, 1, activation_fn=None) + tf.image.resize_bilinear( c3, size=tf.shape(c2)[1:3]) c1 = slim.conv2d(c1, 256, 1, 1, activation_fn=None) + tf.image.resize_bilinear( c2, size=tf.shape(c1)[1:3]) return c1, c2, c3, vbs
'shape': [1, 224, 224, 3], 'scope': resnet_v2.resnet_arg_scope(), 'net': resnet_v2.resnet_v2_152, 'num_classes': 1001, 'end_point': 'global_pool' }, 'inception_v1': { 'shape': [1, 224, 224, 3], 'scope': inception_v1.inception_v1_arg_scope(), 'net': inception_v1.inception_v1, 'num_classes': 1000, 'end_point': 'AvgPool_0a_7x7' }, 'inception_v2': { 'shape': [1, 224, 224, 3], 'scope': inception_v2.inception_v2_arg_scope(), 'net': inception_v2.inception_v2, 'num_classes': 1000 }, 'inception_v3': { 'shape': [1, 299, 299, 3], 'scope': inception_v3.inception_v3_arg_scope(), 'net': inception_v3.inception_v3, 'num_classes': 1000 }, 'vgg_16': { 'shape': [1, 224, 224, 3], 'scope': vgg.vgg_arg_scope(), 'net': vgg.vgg_16, 'num_classes': 1000 },
def compute_feature_of_batch_ts_with_cnn(file_path_of_ts, file_path_of_feature, cnn_model_name, file_path_of_pretrained_model): r''' compute feature of somme time series with pretrained CNN :param file_path_of_ts: file path of time series :param file_path_of_feature: file path of saving feature :param cnn_model_name: name of CNN model :param file_path_of_pretrained_model: file path of pretrained CNN :return: '' ''' #tf.reset_default_graph() #read data data = pd.read_csv(file_path_of_ts) #data=data.sample(20) #change dataframe to list id_list = data.iloc[:, 0].tolist() data_list = change_dataframe_to_dict_(data) model = cnn_model_name checkpoint_file = file_path_of_pretrained_model # I only have these because I thought some take in size of (299,299), but maybe not if 'inception' in model: height, width, channels = 224, 224, 3 if 'resnet' in model: height, width, channels = 224, 224, 3 if 'vgg' in model: height, width, channels = 224, 224, 3 if model == 'inception_resnet_v2': height, width, channels = 299, 299, 3 x = tf.placeholder(tf.float32, shape=(1, height, width, channels)) # load up model specific stuff if model == 'inception_v1': #from inception_v1 import * from nets import inception_v1 arg_scope = inception_v1.inception_v1_arg_scope() with slim.arg_scope(arg_scope): logits, end_points = inception_v1.inception_v1(x, is_training=False, num_classes=None) features = end_points['AvgPool_0a_7x7'] # print('logits') # print(logits.shape) # print('features') # print(features.shape) elif model == 'inception_v2': #from inception_v2 import * from nets import inception_v2 arg_scope = inception_v2.inception_v2_arg_scope() with slim.arg_scope(arg_scope): logits, end_points = inception_v2(x, is_training=False, num_classes=None) features = end_points['AvgPool_1a'] elif model == 'inception_v3': #from inception_v3 import * from nets import inception_v3 arg_scope = inception_v3.inception_v3_arg_scope() with slim.arg_scope(arg_scope): logits, end_points = inception_v3(x, is_training=False, num_classes=None) features = end_points['AvgPool_1a'] elif model == 'inception_resnet_v2': #from inception_resnet_v2 import * from nets import inception_resnet_v2 arg_scope = inception_resnet_v2.inception_resnet_v2_arg_scope() with slim.arg_scope(arg_scope): logits, end_points = inception_resnet_v2(x, is_training=False, num_classes=1001) features = end_points['PreLogitsFlatten'] elif model == 'resnet_v1_50': #from resnet_v1 import * from nets import resnet_v1 arg_scope = resnet_v1.resnet_arg_scope() with slim.arg_scope(arg_scope): logits, end_points = resnet_v1.resnet_v1_50(x, is_training=False, num_classes=1000) features = end_points['global_pool'] elif model == 'resnet_v1_101': #from resnet_v1 import * from nets import resnet_v1 arg_scope = resnet_v1.resnet_arg_scope() with slim.arg_scope(arg_scope): logits, end_points = resnet_v1.resnet_v1_101(x, is_training=False, num_classes=1000) features = end_points['global_pool'] elif model == 'vgg_16': #from vgg import * from nets import vgg arg_scope = vgg.vgg_arg_scope() with slim.arg_scope(arg_scope): logits, end_points = vgg.vgg_16(x, is_training=False) features = end_points['vgg_16/fc8'] elif model == 'vgg_19': #from vgg import * from nets import vgg arg_scope = vgg.vgg_arg_scope() with slim.arg_scope(arg_scope): logits, end_points = vgg.vgg_19(x, is_training=False) features = end_points['vgg_19/fc8'] #cpu_config = tf.ConfigProto(intra_op_parallelism_threads = 8, inter_op_parallelism_threads = 8, device_count = {'CPU': 3}) #sess = tf.Session(config = cpu_config) sess = tf.Session() saver = tf.train.Saver() saver.restore(sess, checkpoint_file) feature_list = [] count_temp = 0 for i in range(len(data_list)): count_temp = count_temp + 1 #imaging ts ts_dict = data_list[i] ts = ts_dict['ts'] id = ts_dict['id'] new_ts = min_max_transform(ts) normalized = np.array(new_ts) fig, ax = plt.subplots() #plt.imshow(recurrence_plot.rec_plot(normalized), cmap=plt.cm.gray) plt.imshow(recurrence_plot.rec_plot(normalized)) ax.set_xticks([]) ax.set_yticks([]) #print(id) path = "inception-v1/" + id + ".jpg" plt.savefig(path) plt.close(fig) #compute feature # #begin to compute features image = misc.imread(path) #from matplotlib.pyplot import imread #image=imread(path) # print('image') # print(image.size) image = misc.imresize(image, (height, width)) image = np.expand_dims(image, 0) feature = np.squeeze(sess.run(features, feed_dict={x: image})) feature_list.append(feature) # print('feature-test') # print(feature) os.remove(path) if count_temp % 100 == 0: print(count_temp) #begin to process parellel result and write_to_csv feature_array = np.array(feature_list) feature_df = pd.DataFrame(feature_array) # print(feature_df.shape) # print(len(id_list)) #add id feature_df.insert(loc=0, column='id', value=id_list) # print(feature_final_df.shape) # print(feature_final_df.head()) feature_df.to_csv(file_path_of_feature, index=False) gc.collect()
n_classes = 101 n_test_data = len(testing_set) Size = 224 Height = Width = Size batchsize = 1 conf = cf.configuration(batchsize, data_list, label_list) # define variable with tf.name_scope('input_data'): x = tf.placeholder(tf.float32, [None, Height, Width, 3], name='x1') y = tf.placeholder(tf.int32, [None], name='y') y_onehot = tf.one_hot(y, n_classes, name='label') slim = tf.contrib.slim with slim.arg_scope(net.inception_v2_arg_scope()) as sc: _, end_points = net.inception_v2(x, num_classes=101, is_training=False, reuse=None) init_cnn = slim.assign_from_checkpoint_fn('pass/to/save/model.ckpt', slim.get_model_variables()) logits = end_points['Logits'] with tf.name_scope('accuracy'): pred = tf.nn.softmax(logits, 1) pred_restore = tf.placeholder(pred.dtype, pred.shape) correct_pred = tf.equal(tf.argmax(pred_restore, 1), tf.argmax(y_onehot, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
def main(): #-------------解析参数-------------# args = parse_args() if args.cfg_file is not None: cfg_from_file(args.cfg_file) #读取args.cfg_file文件内容并融合到cfg中 pprint.pprint(cfg) #-------------任务相关配置-------------# tf.logging.set_verbosity(tf.logging.INFO) #设置日志级别 os.environ['CUDA_VISBLE_DEVICES'] = cfg.GPUS tf.logging.set_verbosity(tf.logging.INFO) #设置日志级别 #-------------搭建计算图-------------# # 读取数据,tsn_batch形式:(batch_size*num_seg*new_length) * h * w * num_channels with tf.device('/cpu:0'): # 简单计算放在CPU上进行 ite = TSNDataReader( cfg.INPUT.DATA_DIR, cfg.INPUT.MODALITY, # flow模态读取方式与rgb稍有不同 cfg.INPUT.NUM_SEGMENTS, cfg.INPUT.NEW_LENGTH, cfg.INPUT.SPLIT_PATH, cfg.TRAIN.BATCH_SIZE, isTraining=False).get_dataset_iter() # 读取数据,tsn_batch形式:(batch_size*num_seg*new_length) * h * w * num_channels tsn_batch, labels = ite.get_next() if cfg.INPUT.MODALITY == 'rgb': tsn_batch = tf.reshape(tsn_batch, [ cfg.TRAIN.BATCH_SIZE * cfg.INPUT.NUM_SEGMENTS * cfg.INPUT.NEW_LENGTH, 224, 224, 3 ]) elif cfg.INPUT.MODALITY == 'flow': tsn_batch = tf.reshape(tsn_batch, [ cfg.TRAIN.BATCH_SIZE * cfg.INPUT.NUM_SEGMENTS * cfg.INPUT.NEW_LENGTH, 224, 224, 2 ]) else: raise ValueError("modality must be one of rgb or flow") # 获取网络, 并完成前传 with slim.arg_scope(inception_v2_arg_scope()): logits, _ = inception_v2(tsn_batch, num_classes=cfg.NUM_CLASSES, is_training=False, dropout_keep_prob=cfg.TRAIN.DROPOUT_KEEP_PROB, min_depth=16, depth_multiplier=1.0, prediction_fn=slim.softmax, spatial_squeeze=True, reuse=None, scope='InceptionV2', global_pool=False) logits = tf.reshape(logits, [ cfg.TRAIN.BATCH_SIZE, cfg.INPUT.NUM_SEGMENTS * cfg.INPUT.NEW_LENGTH, -1 ]) logits = tf.reduce_mean(logits, 1) # 取采样图片输出的平均值 # 做一个batch准确度的预测 prediction = tf.nn.softmax(logits) acc_batch = tf.reduce_mean( tf.cast(tf.equal(tf.argmax(prediction, 1), tf.argmax(labels, 1)), tf.float32)) # saver model_variables_map = {} for variable in tf.global_variables(): if variable.name.split('/')[0] == 'InceptionV2' and variable.name.find( 'Conv2d_1c_1x1') == -1: model_variables_map[variable.name.replace(':0', '')] = variable print '####################################################' for i in model_variables_map.keys(): print i print '#####################################################' saver = tf.train.Saver(var_list=model_variables_map) with tf.Session() as sess: #初始化变量 tf.global_variables_initializer().run() saver.restore(sess, 'models/tsn_rgb_bk_v1.ckpt-4401') acc = [] sess.graph.finalize() for i in range(200): try: print 'testing the %dth vid' % i prediction = sess.run(acc_batch) print prediction acc.append(prediction) except: continue acc = np.mean(acc) print "the accu is %f" % acc
def main(): #-------------解析参数-------------# args = _parse_args() if args.cfg_file is not None: cfg_from_file(args.cfg_file) #读取args.cfg_file文件内容并融合到cfg中 pprint.pprint(cfg) #-------------任务相关配置-------------# os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" os.environ['CUDA_VISBLE_DEVICES'] = cfg.GPUS tf.logging.set_verbosity(tf.logging.INFO) #设置日志级别 #-------------搭建计算图-------------# with tf.device('/cpu:0'): # 操作密集型放在CPU上进行 global_step = tf.get_variable('global_step', [], dtype=None, initializer=tf.constant_initializer(0), trainable=False) lr = tf.train.exponential_decay(cfg.TRAIN.LEARNING_RATE_BASE, global_step, cfg.TRAIN.DECAY_STEP, cfg.TRAIN.DECAY_RATE, staircase=True) # 学习率 tf.summary.scalar('learnrate', lr) opt = tf.train.MomentumOptimizer(lr, cfg.TRAIN.MOMENTUM) # 优化函数 #opt = tf.train.GradientDescentOptimizer(lr) # 优化函数 num_gpus = len(cfg.GPUS.split(',')) # 建立dataset,获取iterator reader.set_param( cfg.INPUT.DATA_DIR, cfg.INPUT.MODALITY, # flow模态读取方式与rgb稍有不同 cfg.VALID.SPLIT_PATH, cfg.VALID.BATCH_SIZE, num_segments=cfg.INPUT.NUM_SEGMENTS, new_length=cfg.INPUT.NEW_LENGTH, train_split_path=cfg.INPUT.SPLIT_PATH, train_batch_size=cfg.TRAIN.BATCH_SIZE, isTraining=True) ite_train, ite_valid = reader.get_dataset_iter() tsn_batch, label_batch = ite_train.get_next() tsn_batch_splits = tf.split(tsn_batch, num_or_size_splits=num_gpus, axis=0) label_batch_splits = tf.split(label_batch, num_or_size_splits=num_gpus, axis=0) tsn_valid_batch, label_valid_batch = ite_valid.get_next() # 在GPU上运行训练(并行) tower_grads = [] with tf.variable_scope(tf.get_variable_scope( )) as vscope: # 见https://github.com/tensorflow/tensorflow/issues/6220 for i in range(num_gpus): with tf.device('/gpu:%d' % i), tf.name_scope('GPU_%d' % i) as scope: # 获取数据,tsn_batch形式:(batch_size/num_gpus*num_seg*new_length) * h * w * num_channels tsn_batch_split, label_batch_split = tsn_batch_splits[ i], label_batch_splits[i] if cfg.INPUT.MODALITY == 'rgb': tsn_batch_split = tf.reshape(tsn_batch_split, [ cfg.TRAIN.BATCH_SIZE / num_gpus * cfg.INPUT.NUM_SEGMENTS * cfg.INPUT.NEW_LENGTH, 224, 224, 3 ]) elif cfg.INPUT.MODALITY == 'flow': tsn_batch_split = tf.reshape(tsn_batch_split, [ cfg.TRAIN.BATCH_SIZE / num_gpus * cfg.INPUT.NUM_SEGMENTS * cfg.INPUT.NEW_LENGTH, 224, 224, 2 ]) else: raise ValueError("modality must be one of rgb or flow") # 获取网络,并完成前传 with slim.arg_scope(inception_v2_arg_scope()): logits, _ = inception_v2( tsn_batch_split, num_classes=cfg.NUM_CLASSES, is_training=True, dropout_keep_prob=cfg.TRAIN.DROPOUT_KEEP_PROB, min_depth=16, depth_multiplier=1.0, prediction_fn=slim.softmax, spatial_squeeze=True, reuse=None, scope='InceptionV2', global_pool=False) tf.get_variable_scope().reuse_variables() logits = tf.reshape(logits, [ cfg.TRAIN.BATCH_SIZE / num_gpus, cfg.INPUT.NUM_SEGMENTS * cfg.INPUT.NEW_LENGTH, -1 ]) #tsn的特殊性决定 logits = tf.reduce_mean(logits, 1) # 取采样图片输出的平均值 # 做一个batch准确度的预测 prediction = tf.nn.softmax(logits) acc_batch = tf.reduce_mean( tf.cast( tf.equal(tf.argmax(prediction, 1), tf.argmax(label_batch_split, 1)), tf.float32)) tf.summary.scalar('acc_on_batch', acc_batch) # 求loss for variable in tf.global_variables(): if variable.name.find( 'weights' ) > 0: # 把参数w加入集合tf.GraphKeys.WEIGHTS,方便做正则化(此句必须放在正则化之前) tf.add_to_collection(tf.GraphKeys.WEIGHTS, variable) loss = tsn_loss(logits, label_batch_split, regularization=True) tf.summary.scalar('loss', loss) # 计算梯度,并由tower_grads收集 grads_and_vars = opt.compute_gradients( loss, var_list=tf.trainable_variables( )) # (gradient, variable)组成的列表 tower_grads.append(grads_and_vars) grads_and_vars = average_gradients(tower_grads) # 求取各GPU平均梯度 train_step = opt.apply_gradients(grads_and_vars, global_step=global_step) # 在GPU上运行验证(串行) with tf.variable_scope(tf.get_variable_scope( )) as vscope: # 见https://github.com/tensorflow/tensorflow/issues/6220 with tf.device('/gpu:0'), tf.name_scope('VALID') as scope: tf.get_variable_scope().reuse_variables() if cfg.INPUT.MODALITY == 'rgb': tsn_valid_batch = tf.reshape( tsn_valid_batch, [cfg.VALID.BATCH_SIZE * 25, 224, 224, 3]) elif cfg.INPUT.MODALITY == 'flow': tsn_valid_batch = tf.reshape( tsn_valid_batch, [cfg.VALID.BATCH_SIZE * 25, 224, 224, 2]) else: raise ValueError("modality must be one of rgb or flow") with slim.arg_scope(inception_v2_arg_scope()): logits_valid, _ = inception_v2( tsn_valid_batch, num_classes=cfg.NUM_CLASSES, is_training=False, dropout_keep_prob=cfg.TRAIN.DROPOUT_KEEP_PROB, min_depth=16, depth_multiplier=1.0, prediction_fn=slim.softmax, spatial_squeeze=True, reuse=None, scope='InceptionV2', global_pool=False) logits_valid = tf.reshape( logits_valid, [cfg.VALID.BATCH_SIZE, 25, -1]) #tsn的特殊性决定 logits_valid = tf.reduce_mean(logits_valid, 1) # 取采样图片输出的平均值 # 做一个batch准确度的预测 prediction_valid = tf.nn.softmax(logits_valid) acc_valid_batch = tf.reduce_mean( tf.cast( tf.equal(tf.argmax(prediction_valid, 1), tf.argmax(label_valid_batch, 1)), tf.float32)) merged = tf.summary.merge_all() # saver model_variables_map = {} for variable in tf.global_variables(): if variable.name.split('/')[0] == 'InceptionV2' and variable.name.find( 'Conv2d_1c_1x1') == -1 and variable.name.find( 'Momentum') == -1: model_variables_map[variable.name.replace(':0', '')] = variable print '####################################################' for i in model_variables_map.keys(): print i print '#####################################################' saver_model = tf.train.Saver( var_list=model_variables_map, max_to_keep=20) #不加载'InceptionV2/Logits/Conv2d_1c_1x1/'下的参数 #-------------启动Session-------------# # (预测验证集,求取精度) gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8) config = tf.ConfigProto(gpu_options=gpu_options, allow_soft_placement=True) with tf.Session(config=config) as sess: run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() joint_writer = tf.summary.FileWriter(cfg.SUMMARY_DIR, sess.graph) summary_writer = tf.summary.FileWriter(cfg.SUMMARY_DIR, sess.graph) #初始化变量(或加载pretrained models) tf.global_variables_initializer().run() saver_model.restore(sess, cfg.TRAIN.PRETRAINED_MODEL_NAME) sess.graph.finalize() start_time = time.time() for i in range(cfg.TRAIN.MAX_ITE): _, learnrate, loss_value, step, summary = sess.run( [train_step, lr, loss, global_step, merged], options=run_options, run_metadata=run_metadata) if i == 0: start_time = time.time() if i % 10 == 0: if i >= 1: end_time = time.time() avg_time = (end_time - start_time) / float(i + 1) print("Average time consumed per step is %0.2f secs." % avg_time) print( "After %d training step(s), learning rate is %g, loss on training batch is %g." % (step, learnrate, loss_value)) # 每个epoch验证一次,保存模型 if i % 100 == 0: print '#############################################' print 'valid and save model' accs = [] num = 0 for j in range(849): num += 1 acc = sess.run(acc_valid_batch) accs.append(acc) print num acc_valid = np.mean(np.array(accs)) print 'accuracy on validation set is %0.4f' % acc_valid print 'saving model...' saver_model.save(sess, cfg.TRAIN.SAVED_MODEL_PATTERN, global_step=global_step) print 'successfully saved !' print '#############################################' joint_writer.add_run_metadata(run_metadata, 'step%03d' % i) summary_writer.add_summary(summary, i) end_time = time.time() #print '%dth time step,consuming %f secs'%(i, start_time-end_time) summary_writer.close()