def main(unused_argv): with tf.device('/gpu:2'): x_input = tf.placeholder(tf.float32, shape=(None, 3, 1, 1, 1024)) y_ = tf.placeholder(tf.float32, shape=(None, _NUM_CLASSES)) end_point = 'Logits' with tf.variable_scope(end_point): if TRAINING: x_input = tf.nn.dropout(x_input, 0.7) logits = i3d.Unit3D(output_channels=_NUM_CLASSES, kernel_shape=[1, 1, 1], activation_fn=None, use_batch_norm=False, use_bias=True, name='Conv3d_0c_1x1')(x_input, is_training=True) logits = tf.squeeze(logits, [2, 3], name='SpatialSqueeze') averaged_logits = tf.reduce_mean(logits, axis=1) cross_entropy = tf.nn.softmax_cross_entropy_with_logits( logits=averaged_logits, labels=y_) loss = tf.reduce_mean(cross_entropy) optimizer_op = tf.train.AdamOptimizer( learning_rate=learning_rate).minimize(loss=loss) predictions = tf.nn.softmax(averaged_logits) model_saver = tf.train.Saver() init = tf.global_variables_initializer() config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init) i = 0 T_batch, t_batch = test_data(data_dir, 'Cam04') feed_dict_test = {x_input: T_batch, y_: t_batch} for epoch in range(epoch_num): for X_batch, y_batch in generator_data(data_dir, '2'): i = i + 1 feed_dict_train = {x_input: X_batch, y_: y_batch} op_, loss_, pre_train = sess.run( [optimizer_op, loss, predictions], feed_dict=feed_dict_train) train_acc = accuracy(pre_train, y_batch) print( "After %d epoch %d training step(s), train accuracy using average model is %g " % (epoch, i, train_acc)) print("Train_loss is %f " % (loss_ * 1.0)) pre_test = sess.run(predictions, feed_dict=feed_dict_test) test_acc = train_acc = accuracy(pre_test, t_batch) print( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ) print( "After %d epoch, validate accuracy using average model is %g " % (epoch, test_acc)) print( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ) model_saver.save(sess, save_model_path) print("OK!!!")
def _build_stream(stream_name, is_training): dims = 3 if stream_name is 'RGB' else 2 input_shape = (_BATCH_SIZE, NUM_FRAMES, IMAGE_SIZE, IMAGE_SIZE, dims) inp = tf.placeholder(tf.float32, shape=input_shape) with tf.variable_scope(stream_name): model = i3d.InceptionI3d(spatial_squeeze=True, final_endpoint='Mixed_5c') # No training here. Don't backpropagate the main model, and no dropout. mixed_5c, _ = model(inp, is_training=False, dropout_keep_prob=1.0) var_map = {} for var in tf.global_variables(): if var.name.split('/')[0] == stream_name: var_map[var.name.replace(':0', '')] = var saver = tf.train.Saver(var_list=var_map, reshape=True) with tf.variable_scope(stream_name): net = tf.nn.avg_pool3d(mixed_5c, ksize=[1, 2, 7, 7, 1], strides=[1, 1, 1, 1, 1], padding=snt.VALID) logit_fn = i3d.Unit3D(output_channels=NUM_CLASSES, kernel_shape=[1, 1, 1], activation_fn=None, use_batch_norm=False, use_bias=True, regularizers={'w': tf.nn.l2_loss}, name='Conv3d_0c_1x1') logits = logit_fn(net, is_training=is_training) logits = tf.squeeze(logits, [2, 3], name='SpatialSqueeze') logits = tf.reduce_mean(logits, axis=1) custom_vars = {} for var in tf.global_variables(): name = var.name.replace(':0', '') if var.name.split( '/')[0] == stream_name and name not in var_map.keys(): custom_vars[name] = var with tf.name_scope(name): _variable_summaries(var) return (inp, logits, saver, custom_vars)
name='groundtruth_input') #global_step global_step = tf.Variable(97000, name='global_step', trainable=False) #搭建全连接层(此处到时候可以在这个命名空间下,然后专门保存这几个变量) end_point = 'UCF_Logits' with tf.variable_scope(end_point): net = tf.nn.avg_pool3d(bottleneck_input, ksize=[1, 2, 7, 7, 1], strides=[1, 1, 1, 1, 1], padding=snt.VALID) net = tf.nn.dropout(net, 1.0) logits = i3d.Unit3D(output_channels=n_classes, kernel_shape=[1, 1, 1], activation_fn=None, use_batch_norm=True, use_bias=True, name='Conv3d_0c_1x1')(net, is_training=True) logits = tf.squeeze(logits, [2, 3], name='SpatialSqueeze') #这个返回值维度是(6,7,10) print(logits) averaged_logits = tf.reduce_mean(logits, axis=1) #那么理论上此时返回的维度是(6,10)i #获取finetune层的参数 finetune_variable_map = {} finetune_variable_list = [] for variable in tf.global_variables(): #收集参数,方便正则化 if variable.name.split('/')[0] == 'UCF_Logits': finetune_variable_list.append(variable) tf.add_to_collection(tf.GraphKeys.WEIGHTS, variable)
def main(unused_argv): with tf.device('/gpu:1'): x_rgb = tf.placeholder(tf.float32, shape=(None, _SAMPLE_VIDEO_FRAMES, _IMAGE_SIZE, _IMAGE_SIZE, 3)) y_rgb = tf.placeholder(tf.float32, shape=(None, _NUM_CLASSES)) with tf.variable_scope('RGB'): rgb_model = i3d.InceptionI3d(_NUM_CLASSES, spatial_squeeze=True, final_endpoint='Mixed_5c') rgb_net, A = rgb_model(x_rgb, is_training=False, dropout_keep_prob=1.0) end_point = 'Logits' with tf.variable_scope('inception_i3d'): with tf.variable_scope(end_point): rgb_net = tf.nn.avg_pool3d(rgb_net, ksize=[1, 2, 7, 7, 1], strides=[1, 1, 1, 1, 1], padding=snt.VALID) if TRAINING: rgb_net = tf.nn.dropout(rgb_net, 0.7) logits = i3d.Unit3D(output_channels=_NUM_CLASSES, kernel_shape=[1, 1, 1], activation_fn=None, use_batch_norm=False, use_bias=True, name='Conv3d_0c_1x1')(rgb_net, is_training=True) logits = tf.squeeze(logits, [2, 3], name='SpatialSqueeze') averaged_logits = tf.reduce_mean(logits, axis=1) rgb_variable_map = {} last_layer = [] for variable in tf.global_variables(): if variable.name.split('/')[0] == 'RGB': if (variable.name.find("Logits") == -1): rgb_variable_map[variable.name.replace(':0', '')] = variable if (variable.name.find("Mixed_5c") == -1): last_layer.append(variable.name.replace(':0', '')) #[variable.name.replace(':0', '')] = variable else: #last_layer[variable.name.replace(':0', '')] = variable last_layer.append(variable.name.replace(':0', '')) rgb_saver = tf.train.Saver(var_list=rgb_variable_map, reshape=True) last_layer_var = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='RGB/inception_i3d/Logits') for variabel_name in tf.get_collection( tf.GraphKeys.TRAINABLE_VARIABLES, scope='RGB/inception_i3d/Mixed_5c'): last_layer_var.append(variabel_name) print(last_layer_var) global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay(0.002, global_step, 100, 0.98, staircase=False) cross_entropy = tf.nn.softmax_cross_entropy_with_logits( logits=averaged_logits, labels=y_rgb) loss = tf.reduce_mean(cross_entropy) optimizer_op = tf.train.AdamOptimizer( learning_rate=learning_rate).minimize(loss=loss, var_list=last_layer_var) predictions = tf.nn.softmax(averaged_logits) model_saver = tf.train.Saver() init = tf.global_variables_initializer() config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init) rgb_saver.restore(sess, _CHECKPOINT_PATHS['rgb_imagenet']) tf.logging.info('RGB checkpoint restored %s', _CHECKPOINT_PATHS['rgb_imagenet']) i = 0 vali_list_prepare = prepare_vali_data(data_dir, 'Cam04') T_batch, t_batch = generator_vali_data(vali_list_prepare, data_dir) for epoch in range(epoch_num): for X_batch, y_batch in generator_data(data_dir, '3'): i = i + 1 feed_dict_train = {x_rgb: X_batch, y_rgb: y_batch} op_, loss_, pre_train = sess.run( [optimizer_op, loss, predictions], feed_dict=feed_dict_train) train_acc = accuracy(pre_train, y_batch) print( "After %d epoch %d training step(s), train accuracy using average model is %g " % (epoch, i, train_acc)) print("Train_loss is %f " % (loss_ * 1.0)) if (i % 100 == 0): T_batch, t_batch = generator_vali_data( vali_list_prepare, data_dir) feed_dict_test = {x_rgb: T_batch, y_rgb: t_batch} pre_test = sess.run(predictions, feed_dict=feed_dict_test) test_acc = train_acc = accuracy(pre_test, t_batch) print( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ) print( "After %d iter, validate accuracy using average model is %g " % (epoch, test_acc)) print( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ) model_saver.save(sess, save_model_path) model_saver.save(sess, save_model_path2) print("OK!!!")