Getting error when try to instanciate vgg16()

I am getting the error below when trying to create the object from vgg16(). I seems that Flatten() is not well defined but I saw that in all examples in source code, Flatten class has no declared argument. How to fix this issue?
----> 1 vgg = Vgg16()

Lesson1\ in init(self, size, include_top)
31 def init(self, size=(224,224), include_top=True):
32 self.FILE_PATH = ‘
—> 33 self.create(size, include_top)
34 self.get_classes()

C:\Users\carlao\Deep-Learning\Lesson1\ in create(self, size, include_top)
83 return
—> 85 model.add(Flatten())
86 self.FCBlock()
87 self.FCBlock()

c:\users\carlao\anaconda2\lib\site-packages\keras\keras\models.pyc in add(self, layer)
329 output_shapes=[self.outputs[0]._keras_shape])
330 else:
–> 331 output_tensor = layer(self.outputs[0])
332 if isinstance(output_tensor, list):
333 raise TypeError('All layers in a Sequential model ’

c:\users\carlao\anaconda2\lib\site-packages\keras\keras\engine\topology.pyc in call(self, x, mask)
567 if inbound_layers:
568 # This will call if necessary.
–> 569 self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
570 # Outputs were already computed when calling self.add_inbound_node.
571 outputs = self.inbound_nodes[-1].output_tensors

c:\users\carlao\anaconda2\lib\site-packages\keras\keras\engine\topology.pyc in add_inbound_node(self, inbound_layers, node_indices, tensor_indices)
630 # creating the node automatically updates self.inbound_nodes
631 # as well as outbound_nodes on inbound layers.
–> 632 Node.create_node(self, inbound_layers, node_indices, tensor_indices)
634 def get_output_shape_for(self, input_shape):

c:\users\carlao\anaconda2\lib\site-packages\keras\keras\engine\topology.pyc in create_node(cls, outbound_layer, inbound_layers, node_indices, tensor_indices)
166 # TODO: try to auto-infer shape
167 # if exception is raised by get_output_shape_for.
–> 168 output_shapes = to_list(outbound_layer.get_output_shape_for(input_shapes[0]))
169 else:
170 output_tensors = to_list(, mask=input_masks))

c:\users\carlao\anaconda2\lib\site-packages\keras\keras\layers\core.pyc in get_output_shape_for(self, input_shape)
434 raise ValueError('The shape of the input to “Flatten” '
435 'is not fully defined '
–> 436 '(got ’ + str(input_shape[1:]) + '. '
437 'Make sure to pass a complete “input_shape” '
438 'or “batch_input_shape” argument to the first ’

ValueError: The shape of the input to “Flatten” is not fully defined (got (0, 7, 512). Make sure to pass a complete “input_shape” or “batch_input_shape” argument to the first layer in your model.

Ok, I did find a comment about this issue in the thread Setup issue. It seems that now it is working because I can see that there is a vgg16 file being downloaded.
I think that because this is a configuration issue (I am guessing if everybody working locally will face this issue), it should be listed in the installation setup or then be part of Lesson 1 comments.

@carlosdeep what change did you make that fixed this?

You need go to your home directory and edit ~/.keras/keras.json file and set to “tf” instead of "th"
I think that we have tf because in the installation guide they instruct to change tensorflow to theano but they forget to say to change tf to th. In other words, I think that we have tf for tensorflow and th for theano. So we need do 2 updates and not just one.
Now it is working fine.

Did you mean that you need to change ‘tf’ to ‘th’? Or are you using the tensorflow backend? (If so, you won’t be able to use the weights files from this class, since they’re specific to theano)

I made three changes (not sure the third one had any affect):

  • in keras.json, change the tf -> th
  • in keras.json change the backend to “theano”
  • in the file for keras/backend, I noticed that “tensorflow” was hard coded as the backend, so I changed it to “theano”. This is the one I am not sure I needed to do.

Yes @Jeremy. In this course we are using theano and so, you need change tf to th, where th is relate to theano.

I’m using a local setup on Windows 7 and I got this error too when creating the vgg16 object.

I fixed it by changing the version of Keras from 2.0.0 to 1.2.2. It seems v2.0.0 version of keras introduced compatibiliy breaks…

For those not familiar with git, you can get Keras 1.2.2 by entering
git checkout tags/1.2.2

and rerun setup
python develop

My setting is just like below
“epsilon”: 1e-07,
“floatx”: “float32”,
“image_data_format”: “channels_last”,
“backend”: “theano”

Can you tell me where to change “tf” to “th”??

Look for a .keras file in your home directory

Make sure you have image_dim_ordering in there.

“epsilon”: 1e-07,
“floatx”: “float32”,
“image_data_format”: “channels_last”,
“backend”: “theano”,
“image_dim_ordering”: “th”

After adding the line how do I update .keras? Is there some analogue of source?

That’s a config file for keras 2. Best uninstall that, remove the config file, and install keras 1.

Oh, I see.
I installed Keras 1.22.
So is this config from @jtymes right?

"epsilon": 1e-07, 
"floatx": "float32", 
"image_data_format": "channels_last", 
"backend": "theano",
"image_dim_ordering": "th"


I had the same problem - from what I understood, either keras2 or keras 1.2 will cause this issue. Keras 1.2 didn’t accept it when I manually introduced the “image_dim_ordering” line.

To solve this, I downgraded to keras 1.1.2 with these two lines:
pip uninstall keras
pip install keras==1.1.2

Hi, I wanted to document a similar problem with vgg16() I’ve had. First my setup (MacOS):

  • python2.7 environment setup via conda (Miniconda3).
  • Theano .9.0 dev install following their guide:
  • Keras 1.2.2
  • .keras/keras.json is set to “tensorflow” and “th”
  • I think I’m using MKL as my BLAS.
  • Theano and Keras were both pip installed to my environment.

I’m working alongside the cats_vs_dogs_redux notebook trying to build my own. When I try to instantiate vgg = Vgg16() I got an error:

/Users/WayNoxchi/Miniconda3/envs/FAI/lib/python2.7/site-packages/keras/backend/ in in_train_phase(x, alt)
   1173     elif _LEARNING_PHASE is 0:
   1174         return alt
-> 1175     x = theano.ifelse.ifelse(_LEARNING_PHASE, x, alt)
   1176     # x = theano.ifelse(_LEARNING_PHASE, x, alt)
   1177     x._uses_learning_phase = True

AttributeError: 'module' object has no attribute 'ifelse'

I can post the full traceback if anyone likes. It essentially goes from
self.create() in line 33 vgg16.pyc
–> self.FCBlock() line 77 vgg16.pyc
–> model.add(Dropout(0.5)) line 63 vgg16.pyc
–> output_tensor = layer(self.outputs[0]) line 332 /keras/models.pyc
–> self.add_inbound_node(inbound_layers, node_indices, tensor_indices) line 572 /keras/engine/topology.pyc
–> Node.create_node(self, inbound_layers, node_indices, tensor_indices) line 635 /keras/engine/topology.pyc
–> output_tensors = to_list([0], mas k=input_masks[0])) line 166 /keras/engine/topology.pyc
–> x = K.in_train_phase(dropped_inputs, lambda: x) line 111 /keras/layers/core.pyc
And finally:
–> x = theano.ifelse.ifelse(_LEARNING_PHASE, x, alt) line 1174 /keras/backend/

Steps I took to fix this:

  • Commented out all references of and replaced them with in

  • Uninstalled Keras & Theano. Reinstalled Keras 1.2.2 (Keras 2 renames activity_l2 to l2, so some compatibility breaks) with Theano .9.0.

  • Uninstalled Keras & Theano. Reinstalled Keras 1.2.2 with Theano 0.8.0.

  • Tried to ensure I had Theano linked to MKL due to this comment referencing this comment.

  • Reinstalled the original Keras & Theano setup. Dove into /lib/python2.7/site-packages/keras/backend/ (inside my environment folder).

Then I tried to figure out exactly why theano.ifelse.ifelse(...) was causing problems. The best I could do with Google was the comment talking about linking MKL. I used a python shell in terminal to find when theano.ifelse.ifelse would work and when not.

>>> import theano
>>> theano.ifelse.ifelse
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'ifelse'

< quit & restart shell >

>>> import theano
>>> import theano.ifelse
>>> theano.ifelse.ifelse
<function ifelse at 0x114da9d70>

so then I did probably the most hackish thing I’ve done so far and added the line

from theano.ifelse import ifelse

as the 2nd line in

Then when I ran vgg = Vgg16() in the notebook (after restarting everything of course) I finally go the message:
"Downloading data from"

But it doesn’t end there. Mysteriously I started getting a new error:
ImportError: No module named h5py from line 2701 of /keras/engine/topology.pyc.

Not sure what I touched or if I touched anything. Maybe I should delete the .pyc file and let it recompile it?

… that did not fix it.

Google…google…google…(I’m debugging this as I’m writing this at this point) I find this, of course I conda install the wrong thing, hdf5: 1.8.17-1 instead of h5py, but when I do install h5py: 2.7.0-np112py27_0:

Downloading data from

So… yeah. Watch it be as simple as me downloading an older version of or accidentally deleting some dependency. But I figured this was worth documenting. Will be setting up GPU support for my Win8.1 machine this weekend, will see how that goes (in a relevant thread).

By the way, big thanks to Jeremy Howard & everyone who worked on this course. This is definitely being a crash course in a literal sense for me and it’s awesome. We’ll see how things are a few months from now.

1 Like

Thank you for this hack!