VGG16_avg.py modification for keras version 2+

I use keras version =2.03 and the first time I tried to run VGG16_avg.py, I got the error saying:

"_obtain_input_shape() got an unexpected keyword argument 'dim_ordering"

This, apparently , is due to an API change in the newer version of keras. Easily solvable by replacing the parameter with data_format in vgg16_avg.py, as below:

# Determine proper input shape
input_shape = _obtain_input_shape(input_shape,
                                  default_size=224,
                                  min_size=48,
                                  data_format=K.image_data_format(),
                                  include_top=include_top)
6 Likes

if K.image_dim_ordering() == ā€˜thā€™:
data_format = "channels_first"
else:
data_format = ā€œchannels_lastā€

On a related noteā€¦ keras v2 also updated/renamed initialization

In utils2.py update:
``- from keras import initialization

  • from keras import initializers``
4 Likes

For kerasā€™ latest version (2.0.8) the ā€œinclude_topā€ should be ā€œrequire_flattenā€:
# Determine proper input shape
input_shape = _obtain_input_shape(input_shape,
default_size=224,
min_size=48,
data_format=K.image_data_format(),
require_flatten=include_top)

1 Like

i changed to what you have saidā€¦but still showing the same error.

same problem occurred to me, i downgraded keras and it worked

it seems that keras 2 has changed many syntaxes that used in the script

I updated vgg_16.py to Keras 2.1.5, seems to be working for me. See my changes at github:

[KERAS] Changed Convolution2D to Conv2D for keras2