I downloaded CNN's pre-trained VGG face and successfully executed it. I want to extract a hypercolumn mean from layers 3 and 8. I followed the section on extracting hypercolumns from here . However, since the get_output function did not work, I had to make a few changes:
Import
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
The main function:
#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()
Get function functions: (I followed 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
Hypercolumn removal:
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)
However, when I run the code, I get the following error:
get_features = K.function([model.layers[0].input, K.learning_phase()], [model.layers[layer].output,]) TypeError: list indices must be integers, not list
How can i fix this?
Note:
In the hypercolumn extraction function, when I use feature_maps = get_features(model,1,instance) or any integer instead of 1, it works fine. But I want to extract the average from layers 3 through 8.