首页 文章

Keras VGG提取功能

提问于
浏览
7

我已经加载了预先训练好的VGG面部CNN并成功运行了它 . 我想从第3层和第8层中提取超列平均值 . 我正在关注从here中提取超列的部分 . 但是,由于get_output函数不起作用,我不得不做一些更改:

进口:

import matplotlib.pyplot as plt
import theano
from scipy import misc
import scipy as sp
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

主功能:

#after necessary processing of input to get im
layers_extract = [3, 8]
hc = extract_hypercolumn(model, layers_extract, im)
ave = np.average(hc.transpose(1, 2, 0), axis=2)
print(ave.shape)
plt.imshow(ave)
plt.show()

获取功能功能:(我跟着this

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 extract_hypercolumn(model, layer_indexes, instance):
    layers = [K.function([model.layers[0].input],[model.layers[li].output])([instance])[0] for li in layer_indexes]
    feature_maps = get_features(model,layers,instance)
    hypercolumns = []
    for convmap in feature_maps:
        for fmap in convmap[0]:
            upscaled = sp.misc.imresize(fmap, size=(224, 224),mode="F", interp='bilinear')
            hypercolumns.append(upscaled)
    return np.asarray(hypercolumns)

但是,当我运行代码时,我收到以下错误:

get_features = K.function([model.layers[0].input, K.learning_phase()], [model.layers[layer].output,])
TypeError: list indices must be integers, not list

我怎样才能解决这个问题?

注意:

在超列提取功能中,当我使用 feature_maps = get_features(model,1,instance) 或任何整数代替1时,它工作正常 . 但我想从第3层到第8层提取平均值 .

2 回答

  • 0

    它让我很困惑:

    • layers = [K.function([model.layers[0].input],[model.layers[li].output])([instance])[0] for li in layer_indexes] 之后,图层是提取的要素列表 .

    • 然后将该列表发送到 feature_maps = get_features(model,layers,instance) .

    • def get_features(model, layer, X_batch): 中,它们的第二个参数,即 layer ,用于在 model.layers[layer].output 中进行索引 .

    你想要的是:

    • feature_maps = get_features(model, layer_indexes ,instance) :传递图层索引而不是提取的要素 .

    • get_features = K.function([model.layers[0].input, K.learning_phase()], [ model.layers[l].output for l in layer ]) :列表不能用于索引列表 .

    尽管如此,您的功能抽象功能仍然可怕 . 我建议你重写一切,而不是混合代码 .

  • 1

    我重写了单通道输入图像(W x H x 1)的功能 . 也许它会有所帮助 .

    def extract_hypercolumn(model, layer_indexes, instance):
        test_image = instance
        outputs    = [layer.output for layer in model.layers]          # all layer outputs
        comp_graph = [K.function([model.input]+ [K.learning_phase()], [output]) for output in outputs]  # evaluation functions
    
        feature_maps = []
        for layerIdx in layer_indexes:
            feature_maps.append(layer_outputs_list[layerIdx][0][0])
    
    
        hypercolumns = []
        for idx, convmap in enumerate(feature_maps):
            #        vv = np.asarray(convmap)
            #        print(vv.shape)
            vv = np.asarray(convmap)
            print('shape of feature map at layer ', layer_indexes[idx], ' is: ', vv.shape)
    
            for i in range(vv.shape[-1]):
                fmap = vv[:,:,i]
                upscaled = sp.misc.imresize(fmap, size=(img_width, img_height),
                                        mode="F", interp='bilinear')
                hypercolumns.append(upscaled)  
    
        # hypc = np.asarray(hypercolumns)
        # print('shape of hypercolumns ', hypc.shape)
    
        return np.asarray(hypercolumns)
    

相关问题