Keras训练了VGG错误

时间:2016-07-13 13:44:05

标签: python keras theano vgg-net

我已按照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

我该如何解决这个问题?

1 个答案:

答案 0 :(得分: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

  1. K.function的输出是列表,即get_features[model.layers[layer].output,]的结果。
  2. 因此,
  3. get_features[0] model.layers[layer].output形状为(1, 256, 56, 56)==>(batch_size, channel, W, H)
  4. get_features[0][0]是第一张图片的特色。
  5. 我相信你要找的是get_features[0][0][13]