Resnet 152张量流Keras转移学习进行特征提取

时间:2020-10-29 04:34:30

标签: tensorflow keras feature-extraction resnet transfer-learning

我正在尝试使用TensorFlow进行特征提取。简而言之,我试图在ATTEND-GAN Model中重现链接到Show Attend and Tell Repo

中prepro.py中给定提供的代码库的步骤3
vggnet = Vgg19(vgg_model_path)
vggnet.build()
with tf.Session() as sess:
    tf.initialize_all_variables().run()
    for split in ['train', 'val', 'test']:
        anno_path = './data/%s/%s.annotations.pkl' % (split, split)
        save_path = './data/%s/%s.features.hkl' % (split, split)
        annotations = load_pickle(anno_path)
        image_path = list(annotations['file_name'].unique())
        n_examples = len(image_path)

        all_feats = np.ndarray([n_examples, 196, 512], dtype=np.float32)

        for start, end in zip(range(0, n_examples, batch_size),
                              range(batch_size, n_examples + batch_size, batch_size)):
            image_batch_file = image_path[start:end]
            image_batch = np.array(map(lambda x: ndimage.imread(x, mode='RGB'), image_batch_file)).astype(
                np.float32)
            feats = sess.run(vggnet.features, feed_dict={vggnet.images: image_batch})
            all_feats[start:end, :] = feats
            print ("Processed %d %s features.." % (end, split))

        # use hickle to save huge feature vectors
        hickle.dump(all_feats, save_path)
        print ("Saved %s.." % (save_path))

在尝试进行第3步中的预处理时,我试图通过更改使用resnet152更改vgg19模型,并通过更改在res5c层中提取特征

vggnet = Vgg19(vgg_model_path)

使用

import tensorflow as tf


rn152 = tf.keras.applications.ResNet152V2(
    include_top=False, weights='imagenet', input_tensor=None, input_shape=(224, 224, 3)
)
new_input = rn152.input
hidden_layer = rn152.layers[-1].output

# image_model.summary()

image_features_extract_model = tf.keras.Model(new_input, hidden_layer) 

注意:请告诉我是否遇到这种错误或上面的代码不正确或需要进行一些调整。

在上面的代码中,我认为我已经提取了功能。但是,我坚持如何将其集成到tf.Session块中。特别是在这行上:

专长= sess.run(vggnet.features,feed_dict = {vggnet.images:image_batch})

请帮助我尝试此任务。谢谢您的帮助。

0 个答案:

没有答案
相关问题