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
Example #2
0
def image_to_embedding(inputs, is_training, args):
    with slim.arg_scope([slim.batch_norm, slim.dropout],
                        is_training=is_training):
        with slim.arg_scope(
                inception_v3_arg_scope(weight_decay=args.weight_decay)):
            logits, _ = inception_v2.inception_v2(
                inputs,
                num_classes=args.embedding_size,
                is_training=is_training,
                dropout_keep_prob=args.dropout_keep_prob,
                last_pool_size=int(args.image_size * 7 / 224))
            embeddings = tf.nn.l2_normalize(logits,
                                            1,
                                            1e-10,
                                            name='embeddings')
    return embeddings
Example #3
0
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))

# run
saver = tf.train.Saver()
with tf.Session() as sess:
with tf.name_scope('input_data'):
    with tf.device('/gpu:0'):                   
        x1 = tf.placeholder(tf.float32, [None,  Height, Width, 3], name = 'x1') 
        x2 = tf.placeholder(tf.float32, [None,  Height, Width, 3], name = 'x2') 
        x3 = tf.placeholder(tf.float32, [None,  Height, Width, 3], name = 'x3') 
    y = tf.placeholder(tf.int32, [None], name = 'y')
    y_onehot = tf.one_hot(y, n_classes, name = 'label')
    learning_rate = tf.placeholder(tf.float32, name = 'learning_rate')

slim = tf.contrib.slim

# TSN network
with tf.name_scope('model'):
    with tf.device('/gpu:0'):
        with slim.arg_scope(net.inception_v2_arg_scope()) as sc:
            _, end_points_1 = net.inception_v2(x1, num_classes = 101,
                                               is_training=True, reuse=None)
            _, end_points_2 = net.inception_v2(x2, num_classes = 101, 
                                               is_training=True, reuse=True)
            _, end_points_3 = net.inception_v2(x3, num_classes = 101,
                                               is_training=True, reuse=True)
    variables_to_restore = slim.get_variables_to_restore(
                                include=["InceptionV2"], 
                                exclude=[v.name for v in tf.trainable_variables() if '/Logits/' in v.name])

    ## print([v for v in variables_to_restore if '/Logits/' in v.name])
    init_cnn = slim.assign_from_checkpoint_fn('./checkpoints/slim_models/inception_v2.ckpt',
               variables_to_restore)

    logits = (end_points_1['Logits'] + end_points_2['Logits'] + end_points_3['Logits'])/3.

with tf.name_scope('loss'):
        label = label_set[label_index][0]
        #print(label)
        label_list[index] = label


n_classes = len(label_set)
n_data = len(training_set)
Size = 224
Height = Width = Size

# define variable                           
xs = tf.placeholder(tf.float32, [None,  Height, Width, 3]) 

slim = tf.contrib.slim
with slim.arg_scope(net.inception_v2_arg_scope()) as sc:
    logits, end_points = net.inception_v2(xs, num_classes = 101, is_training=False)
init_cnn = slim.assign_from_checkpoint_fn('path/to/save/model.ckpt', slim.get_model_variables())

pool = tf.get_default_graph().get_tensor_by_name("InceptionV2/Logits/AvgPool_1a_7x7/AvgPool:0")
feat = tf.squeeze(pool, [1, 2], name='SpatialSqueeze')


def open_video(data, is_flipping):
    file_dir = "dataset/UCF-101/" + data
    with imageio.get_reader(file_dir,  'ffmpeg') as vid:
        nframes = vid.get_meta_data()['nframes']
        xs = [np.zeros((nframes, Height, Width, 3)) for p in range(5)]
        try:
            for i, frame in enumerate(vid):
                for p in range(5)
                    xs[p][i] = data_augmentation(frame, flipping = is_flipping, p = p)                
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
Example #7
0
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()