tensorflow提取VGG特征

来源:互联网 发布:windows c语言多线程 编辑:程序博客网 时间:2024/05/29 03:05

我们知道,再网络训练好之后,只需要forward过程就能做预测,当然,我们也可以直接把这个网络当成一个feature extractor来用,可以直接用任何一层的输出作为特征,根据R-CNN论文对Alexnet的实验结果,如果不做fine-tuning,pool5和fc6和fc7的特征效果并没有很强的提升,所以,如果直接用作feature extractor,直接用pool的最后一层输出就OK.



这里是一个简单的演示,提取VGG的pool5层特征,存储为.mat文件

    import scipy.io as sio   from scipy.misc import imread, imresize   sess = tf.Session()    imgs = tf.placeholder(tf.float32, [None, 224, 224, 3])    vgg = vgg16(imgs, '/aa/data/vgg16_weights.npz', sess)    img1 = imread('/aa/data/laska.png', mode='RGB')    img1 = imresize(img1, (224, 224))    path = '/aa/data/AllSample/'    for i in range(1,211):        img = imread(path+str(i)+'.jpg',mode='RGB')        print(path+str(i)+'.jpg')        img = imresize(img, (224, 224))            feature = sess.run(vgg.pool5, feed_dict={vgg.imgs: [img]})        feature = np.reshape(feature,[7,7,512])        dic = {'features':feature}        sio.savemat('/aa/data/features/'+str(i)+'.mat',dic)    #     features = feature.eval(session=sess)#     features = np.reshape(features,[7,7,512])    


0 0
原创粉丝点击