iamaaditya / VQA_Demo

Visual Question Answering Demo on pretrained model
http://iamaaditya.github.io/2016/04/visual_question_answering_demo_notebook
MIT License
241 stars 133 forks source link

KeyError: 'class_name' #20

Closed singhbhupender1 closed 6 years ago

singhbhupender1 commented 6 years ago

when i run this part from notebook from keras.utils.visualize_util import plot model_vqa = get_VQA_model(VQA_model_file_name, VQA_weights_file_name) plot(model_vqa, to_file='model_vqa.png')

i get error KeyError Traceback (most recent call last)

in () 1 from keras.utils.visualize_util import plot ----> 2 model_vqa = get_VQA_model(VQA_model_file_name, VQA_weights_file_name) 3 plot(model_vqa, to_file='model_vqa.png') in get_VQA_model(VQA_model_file_name, VQA_weights_file_name) 5 # very easy to understand and work. Alternative would be to load model 6 # from binary like cPickle but then model would be obfuscated to users ----> 7 vqa_model = model_from_json(open(VQA_model_file_name).read()) 8 vqa_model.load_weights(VQA_weights_file_name) 9 vqa_model.compile(loss='categorical_crossentropy', optimizer='rmsprop') /home/ap/github/VQA_Demo/keras/models.pyc in model_from_json(json_string, custom_objects) 28 from keras.utils.layer_utils import layer_from_config 29 config = json.loads(json_string) ---> 30 return layer_from_config(config, custom_objects=custom_objects) 31 32 /home/ap/github/VQA_Demo/keras/utils/layer_utils.py in layer_from_config(config, custom_objects) 22 globals()[cls_key] = custom_objects[cls_key] 23 ---> 24 if 'class_name' in config: 25 class_name = config['class_name'] 26 layer_class = Sequential KeyError: 'class_name'
iamaaditya commented 6 years ago

Remove all the "pyc" files and run them again, from the comment line it looks like it is loading path I ran in my computer "/home/ap/github/...".

singhbhupender1 commented 6 years ago

Thanks, I created a new notebook coz I was facing the same issue even after deleting the .pyc files. But I found another issue. In this code block: # get the image features image_features = get_image_features(image_file_name, CNN_weights_file_name) i get error: TypeError Traceback (most recent call last)

in () 1 # get the image features ----> 2 image_features = get_image_features(image_file_name, CNN_weights_file_name) TypeError: get_image_features() takes 1 positional argument but 2 were given as i can see that in the definition, there is space for only one argument
iamaaditya commented 6 years ago

Replace the function get_image_features with

def get_image_features(image_file_name, CNN_weights_file_name):
    ''' Runs the given image_file to VGG 16 model and returns the 
    weights (filters) as a 1, 4096 dimension vector '''
    image_features = np.zeros((1, 4096))

    from skimage import io
    # if you would rather not install skimage, then use cv2.VideoCapture which surprisingly can read from url
    # see this SO answer http://answers.opencv.org/question/16385/cv2imread-a-url/?answer=16389#post-id-16389
    im = cv2.resize(io.imread(image_file_name), (224, 224))
    im = im.transpose((2,0,1)) # convert the image to RGBA

    # this axis dimension is required because VGG was trained on a dimension
    # of 1, 3, 224, 224 (first axis is for the batch size
    # even though we are using only one image, we have to keep the dimensions consistent
    im = np.expand_dims(im, axis=0) 

    image_features[0,:] = get_image_model(CNN_weights_file_name).predict(im)[0]
    return image_features
singhbhupender1 commented 6 years ago

I updated accordingly but again on # get the image features image_features = get_image_features(image_file_name, CNN_weights_file_name) another error creeps in:

TypeError Traceback (most recent call last)

in () 1 # get the image features ----> 2 image_features = get_image_features(image_file_name, CNN_weights_file_name) in get_image_features(image_file_name, CNN_weights_file_name) 16 im = np.expand_dims(im, axis=0) 17 ---> 18 image_features[0,:] = get_image_model(CNN_weights_file_name).predict(im)[0] 19 return image_features TypeError: get_image_model() takes 0 positional arguments but 1 was given FYI, I I am running keras 2.1.5
iamaaditya commented 6 years ago

Okay, give me a couple of hours, and I will fix the issues. I have not updated the notebook in long time and probably newer versions of Keras does not support old api calls. What is your version of Keras?

singhbhupender1 commented 6 years ago

keras : 2.1.5 spacy : 2.0.12 Tensorflow-gpu: 1.7.0

singhbhupender1 commented 6 years ago

scikit-learn : 0.19.2

iamaaditya commented 6 years ago

To a fresh pull

git pull origin master

I have fixed it to run on Keras 2.1, Spacy 2.0, and TF 1.6

singhbhupender1 commented 6 years ago

runs without a problem now. As a suggestion xrange() could be a replaced by range() to support python 3. Also, is there a research paper referring to this?

iamaaditya commented 6 years ago

This is the baseline model (figure 8) from this paper: https://arxiv.org/pdf/1505.00468.pdf