Keras 2 Released

The / in Python 2 is also floor integer division. Sure there are examples when you might want to round up instead, but that is not covered by Python 2 either.

Thank you all for your comments. I have just added to my Github repo the missing statefarm-sample.ipynb and state_farm.ipynb notebooks, so now it should be complete.

2 Likes

Thanks so much!

You are welcome!

Here is a good explanation on how you should change it in vgg16.py: https://stackoverflow.com/questions/43457862/whats-the-difference-between-samples-per-epoch-and-steps-per-epoch-in-fit-g

Great thanks to @eljas and others in the focum, I’ve put together this note, mainly for my own benefit but hopefully will be handy for others here:

Notes on Python 2.x / Keras 1.x to Python 3.x / Keras 2.x transition

Some notes on moving from Python 2.x & Keras 1.x -> to Python 3.x & Keras 2.x. (Note Keras currently supports Python 2.7 to 3.5 only. i.e. Python 3.6 will not work on Keras - yet).

Change accordingly in vgg16.py and utils.py.

Keras 1.x -> Keras 2.x

Keras 1.x:

#1 from keras.layers.convolutional import Convolution2D
#2 from keras.regularizers import l2, activity_l2, l1, activity_l1
#3 from keras.utils.layer_utils import layer_from_config
#4 from keras import backend
#5 Convolution2D
#6 batches.nb_sample
#7 batches.nb_class
#8 model.add(Convolution2D(filters, 3, 3, activation="relu"))
#9 fit_generator(batches, samples_per_epoch=batches.nb_sample, nb_epoch=nb_epoch, validation_data=val_batches, nb_val_samples=val_batches.nb_sample)
#10 nb_epoch
#11 self.model.predict_generator(test_batches, test_batches.nb_sample)

Keras 2.x:

#1 from keras.layers.convolutional import Conv2D
#2 from keras.regularizers import l2, l1
#3 from keras.layers import deserialize as layer_from_config
#4 from keras import backend; backend.set_image_dim_ordering('th')
#5 Conv2D
#6 batches.samples
#7 batches.num_class
#8 model.add(Conv2D(filters, (3, 3), activation="relu"))
#9 fit_generator(batches, steps_per_epoch=batches.samples//batches.batch_size, epochs=nb_epoch, validation_steps=val_batches.samples//val_batches.batch_size)
#10 epochs
#11 self.model.predict_generator(test_batches, test_batches.samples//test_batches.batch_size)

Note:

  • #8 goes from: “3, 3” … to “(3, 3)”… i.e. with the brackets.
  • #9 in Keras 1, the progress bar shows total number of training samples processed. In Keras 2, the progress bar shows total number of batches processed (steps_per_epoch). Recall that total batches = total samples // batch size. (I floor it to ensure integer value).
  • #11 in Keras 1, regarding model.predict_generator(), 2nd argument corresponds to number of total samples. In Keras 2, that 2nd argument becomes total number of batches. Recall that total batches = total samples // batch size. (I floor it to ensure integer value).

Note: it looks like Keras 1 talks in “number of samples”. Keras 2 prefers “number of batches”. Beware.

Python 2.x -> Python 3.x

Python 2.x:

#1 import cPickle as pickle
#2 reload()

Python 3.x:

#1 import _pickle as pickle
#2 from importlib import reload; reload()

References:

8 Likes

In case any one wants keras 1.1 docs they can be found here https://faroit.github.io/keras-docs/1.1.1/

So I am trying to create a file for submission for Cats & Dogs redux. My test directory has 12499 images. I modified the test function slightly to comply with keras 2.0 as follows

def test(self, path, batch_size=8):

        test_batches = self.get_batches(path, shuffle=False, batch_size=batch_size, class_mode=None)
        test_batches.nb_sample = test_batches.samples             #RC added to comply with Keras 2.x API
        return test_batches, self.model.predict_generator(test_batches, test_batches.samples//test_batches.batch_size)

As shown in the screenshot below, the length of the np array is 12480 (multiple of the number of batches and batch size), however I need 12499 predictions. Wondering if I make the batch size = 1 and what would be the performance hit in that case.

Friends,

I am facing the following error while running the code mentioned above. Could you please assist me on this?

ValueError: When using a generator for validation data, you must specify a value for validation_steps.

1 Like

Hi eljas,you mean change vgg16.py, where can I insert this code lines to it?
Thank you!

Hi guys , I am trying to do the similar exercise of converting the program into Keras 2 . Thanks @Robi for the great repo .

I thought i can start with Keras 2 application model of Vgg16 and I compared it with the implementation of Vgg16 in the repo .

I found the later has additionally Zero padding to every convolutional layers and Drop out layers to the Linear layers . Any particular reason the implementation has a change .

Also I am still in Lesson 1 so If it will explained later then I will wait for the same. Thanks

I just updated my repo which now includes also the “Python 3.5 - Keras 2” adaptation for Part 2 of the course. I hope someone will find it useful!
Thanks in advance to everybody who will have a chance to provide any comments, suggestions or corrections.
For any questions or issues related to the repo I suggest to directly visit its issues section.
https://github.com/roebius/deeplearning_keras2

Hi Robi,

Thanks for sharing the python3/keras2 version of the notebooks. I tried executing lesson1 and am getting the following error when Vgg class is instantiated:

~\AppData\Local\Continuum\Anaconda3\envs\dlwin36\lib\site-packages\tensorflow\python\framework\common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, debug_python_shape_fn, require_shape_fn)
674 missing_shape_fn = True
675 else:
–> 676 raise ValueError(err.message)
677
678 if missing_shape_fn:

ValueError: Negative dimension size caused by subtracting 2 from 1 for ‘max_pooling2d_2/MaxPool’ (op: ‘MaxPool’) with input shapes: [?,1,112,128].

Any idea why?

It looks like your Keras is set to use TensorFlow instead of Theano (part 1 notebooks use Theano).
One thing to check is the keras.json file in your .keras directory. There is a template of this file for Theano in my repo.
Let me know if this helps!

P.S. I updated the template yesterday

oh never mind. I was using tensorflow as backend. After fixing the keras config file this worked fine.

Yup that’s what I did . Thansk a lot!

Thanks, Elijas!

Hi Robi,
I tried using your updated course file for part 1 lesson 1 and can never get the vgg = Vgg16() to succeed. It keeps giving me this error:


ValueError Traceback (most recent call last)
in ()
----> 1 vgg = Vgg16()
2 # Grab a few images at a time for training and validation.
3 # NB: They must be in subdirectories named based on their category
4
5 #batches = vgg.get_batches(path+‘train’, batch_size=batch_size)

~/deep-learning/fastai/deeplearning_keras2/nbs/vgg16.py in init(self)
30 def init(self):
31 self.FILE_PATH = ‘http://files.fast.ai/models/
—> 32 self.create()
33 self.get_classes()
34

~/deep-learning/fastai/deeplearning_keras2/nbs/vgg16.py in create(self)
73 self.ConvBlock(3, 512)
74
—> 75 model.add(Flatten())
76 self.FCBlock()
77 self.FCBlock()

~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/keras/models.py in add(self, layer)
474 output_shapes=[self.outputs[0]._keras_shape])
475 else:
–> 476 output_tensor = layer(self.outputs[0])
477 if isinstance(output_tensor, list):
478 raise TypeError('All layers in a Sequential model ’

~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/keras/engine/topology.py in call(self, inputs, **kwargs)
613 # Infering the output shape is only relevant for Theano.
614 if all([s is not None for s in _to_list(input_shape)]):
–> 615 output_shape = self.compute_output_shape(input_shape)
616 else:
617 if isinstance(input_shape, list):

~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/keras/layers/core.py in compute_output_shape(self, input_shape)
479 raise ValueError('The shape of the input to “Flatten” '
480 'is not fully defined '
–> 481 '(got ’ + str(input_shape[1:]) + '. '
482 'Make sure to pass a complete “input_shape” '
483 '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.

@asharafshahi
It might be that you are using the TensorFlow backend instead of the Theano backend. Check the content of your keras.json file like I suggested above at Keras 2 Released

Hope this will help!

Thanks for the help, it did turn out to be an issue in the ~/.keras/keras.json file. Here is the correct format that made it work:
{
“epsilon”: 1e-07,
“floatx”: “float32”,
“image_data_format”: “channels_first”,
“backend”: “theano”,
“image_dim_ordering”: “th”
}

1 Like