Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'Convolution2D' object has no attribute 'get_output' #3166

Closed
RaviTej310 opened this issue Jul 7, 2016 · 10 comments
Closed

Comments

@RaviTej310
Copy link

RaviTej310 commented Jul 7, 2016


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

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(im) 
    print np.argmax(out)

Error:

File "./VGG_Keras.py", line 86, in <module> get_activations = theano.function([model.layers[0].input], model.layers[1].get_output(train=False), allow_input_downcast=True) AttributeError: 'Convolution2D' object has no attribute 'get_output'

I have also tried .output and .get_out_at both of which aren't working. I have the updated versions of keras and theano. Please help.

@JonathanKChang
Copy link

JonathanKChang commented Jul 7, 2016

The new backend API requires the output to also be a list, not sure if this applies to using the pure theano.function. You should also pass in the learning phase (e.g. train vs test). Try using this function from here

from keras import backend as K
def get_activations(model, layer_idx, X_batch):
    get_activations = K.function([model.layers[0].input, K.learning_phase()], [model.layers[layer_idx].output,])
    activations = get_activations([X_batch,0])
    return activations

@RaviTej310
Copy link
Author

What could I pass as an argument to this get_activations function instead of ([model.layers[0].input], model.layers[1].get_output(train=False), allow_input_downcast=True)

@JonathanKChang
Copy link

activations = get_activations(model, 1, im)

@RaviTej310
Copy link
Author

Thanks a ton! But what about other usages like:
get_feature = theano.function([model.layers[0].input], model.layers[15].get_output(train=False), allow_input_downcast=False) feat = get_feature(im) plt.imshow(feat[0][13])

@JonathanKChang
Copy link

JonathanKChang commented Jul 7, 2016

Just pass in that layer number as the 2nd parameter

activations = get_activations(model, 15, im)
plt.imshow(activations[0][13])

You could also modify it to use the layer name, to accept a list of layers (and get a list of activations), or also to return the backend function that you can call with data yourself

@vishalcool1401
Copy link

vishalcool1401 commented Feb 13, 2017

I am getting this error for w

model.layers[0].w.get_value() #weights of the parameters


AttributeError Traceback (most recent call last)
in ()
----> 1 model.layers[0].w.get_value() #weights of the parameters

AttributeError: 'Convolution2D' object has no attribute 'w'

@gsmshsns
Copy link

gsmshsns commented Apr 1, 2017

same issue as above..
someone told me to change it to layer.kernel, layer.bias instead..
not sure how to do that..

@fragiacalone
Copy link

fragiacalone commented Jul 9, 2017

Hello. I arranged my code as above

def get_activations(model,layer_idx, X_batch):
    get_activations = K.function([model.layers[0].input, K.learning_phase()], [model.layers[layer_idx].output,])
    activations = get_activations([X_batch,0])
    return activations


feat = get_activations(model, 3, im_converted)
plt.imshow(feat[0][2])
plt.show()

but then I get this error

plt.imshow(feat[0][2])
IndexError: index 2 is out of bounds for axis 0 with size 1

What's wrong with imshow? Please help

@AchmnShukla
Copy link

@fragiacalone Hi, did you find whats wrong with imshow I am stuck on the same.

@Abdoulzen
Copy link

hello everyboby I have some error after executing this part of code:

visualizing intermediate layers

output_layer = model.layers[0].get_output()
output_fn = theano.function([model.layers[0].get_input()], output_layer)

It give me this error:
output_layer = model.layers[0].get_output()
output_fn = theano.function([model.layers[0].get_input()], output_layer)

Traceback (most recent call last):

File "", line 1, in
output_layer = model.layers[0].get_output()

AttributeError: 'Conv2D' object has no attribute 'get_output'

Need someone help

Ps: the full code==>https://learnandshare645.blogspot.com/2016/06/feeding-your-own-data-set-into-cnn.html?showComment=1519292737662#c3337302287175052994

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants