我已经按照这个来加载和运行一个预训练的VGG模型。但是,我试图从隐藏层中提取特征图,并尝试从此处的"提取任意特征图"部分复制结果。我的代码如下:
#!/usr/bin/python
import matplotlib.pyplot as plt
import theano
from scipy import misc
from PIL import Image
import PIL.ImageOps
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
import numpy as np
from keras import backend as K
def get_features(model, layer, X_batch):
get_features = K.function([model.layers[0].input, K.learning_phase()], [model.layers[layer].output,])
features = get_features([X_batch,0])
return features
def VGG_16(weights_path=None):
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax'))
if weights_path:
model.load_weights("/home/srilatha/Desktop/Research_intern/vgg16_weights.h5")
return model
if __name__ == "__main__":
#f="/home/srilatha/Desktop/Research_intern/Data_sets/Data_set_2/FGNET/male/007A23.JPG"
f="/home/srilatha/Desktop/Research_intern/Data_sets/Cropped_data_set/1/7.JPG"
image = Image.open(f)
new_width = 224
new_height = 224
im = image.resize((new_width, new_height), Image.ANTIALIAS)
im=np.array(im)
im=np.tile(im[:,:,None],(1,1,3))
#imRGB = np.repeat(im[:, :, np.newaxis], 3, axis=2)
print(im)
#print(type(im))
im = im.transpose((2,0,1))
im = np.expand_dims(im, axis=0)
# Test pretrained model
model = VGG_16('/home/srilatha/Desktop/Research_intern/vgg16_weights.h5')
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy')
out = model.predict(im)
#get_feature = theano.function([model.layers[0].input], model.layers[3].get_output(train=False), allow_input_downcast=False)
#feat = get_feature(im)
#get_activations = theano.function([model.layers[0].input], model.layers[1].get_output(train=False), allow_input_downcast=True)
#activations = get_activations(model, 1, im)
#plt.imshow(activations)
#plt.imshow(im)
features=get_features(model,15,im)
plt.imshow(features[0][13])
#out = model.predict(im)
#plt.plot(out.ravel())
#plt.show()
print np.argmax(out)
但是,我收到此错误:
File "VGG_Keras.py", line 98, in <module>
plt.imshow(features[0][13])
IndexError: index 13 is out of bounds for axis 0 with size 1
我该如何解决这个问题?
首先,下次请更新更干净的代码版本,以便其他人可以更轻松地帮助您。
其次,修改函数以调试:
def get_features(model, layer, X_batch):
print model.layers[layer]
print model.layers[layer].output_shape
get_features = K.function([model.layers[0].input, K.learning_phase()], [model.layers[layer].output,])
features = get_features([X_batch,0])
print features.shape
return features
你会发现features
其实是一个list
:
K.function
的输出是列表,即get_features
是[model.layers[layer].output,]
的结果。- 因此,
get_features[0]
的形状model.layers[layer].output
(1, 256, 56, 56)==>(batch_size, channel, W, H)
-
get_features[0][0]
是批量第一张图片的特征。 - 我相信您正在寻找的是
get_features[0][0][13]
.