我按照this加载并运行了预训练的VGG模型。但是,我试图从隐藏层中提取要素图,并尝试复制“提取任意要素图”部分here中的结果。我的代码如下:

#!/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]

关于python - Keras训练的VGG错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38353491/

10-12 17:14