用Vgg16来提取特征
引自:https://www.cnblogs.com/pypypy/p/11830464.html
文件test_vgg16.py可以用于提取特征。其中vgg16.npy是需要单独下载的文件。
2、使用预训练的模型提取特征
打开test_vgg16.py,做如下修改:
1 import numpy as np 2 import tensorflow as tf 3 4 import vgg16 5 import utils 6 7 img1 = utils.load_image("./test_data/tiger.jpeg") 8 img2 = utils.load_image("./test_data/puzzle.jpeg") 9 10 batch1 = img1.reshape((1, 224, 224, 3)) 11 batch2 = img2.reshape((1, 224, 224, 3)) 12 13 batch = np.concatenate((batch1, batch2), 0) 14 15 # with tf.Session(config=tf.ConfigProto(gpu_options=(tf.GPUOptions(per_process_gpu_memory_fraction=0.7)))) as sess: 16 with tf.device('/cpu:0'): 17 with tf.Session() as sess: 18 images = tf.placeholder("float", [2, 224, 224, 3]) 19 feed_dict = {images: batch} 20 21 vgg = vgg16.Vgg16() 22 with tf.name_scope("content_vgg"): 23 vgg.build(images) 24 25 prob = sess.run(vgg.fc7, feed_dict=feed_dict) #需要提取哪一层特征,就在这里做修改,比如fc6,只需要把vgg.fc7修改为vgg.fc6 26 print(prob) 27 # utils.print_prob(prob[0], './synset.txt') 28 # utils.print_prob(prob[1], './synset.txt')