From 40d0193ef5e41c9b0e9b0026880195b603297fcb Mon Sep 17 00:00:00 2001 From: Ross Harrison Date: Tue, 23 Oct 2018 18:55:39 -0400 Subject: [PATCH] Apply fix to issue #39 --- image_analogy/vgg16.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/image_analogy/vgg16.py b/image_analogy/vgg16.py index ae754e3..8a6577d 100644 --- a/image_analogy/vgg16.py +++ b/image_analogy/vgg16.py @@ -7,6 +7,8 @@ AveragePooling2D, Convolution2D, MaxPooling2D, ZeroPadding2D) from keras.models import Sequential +from keras import __version__ as keras_version +from distutils.version import StrictVersion def img_from_vgg(x): '''Decondition an image from the VGG16 model.''' @@ -83,10 +85,26 @@ def get_model(img_width, img_height, weights_path='vgg16_weights.h5', pool_mode= break g = f['layer_{}'.format(k)] weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])] + + # Check if your version of keras is version '2.0.0' or above. + if StrictVersion(keras_version) >= StrictVersion('2.0.0'): + # If your version of keras is version '2.0.0' or above, + # then + # 1. convert each element x of the list 'weights' + # into a numpy array, + # 2. transpose each of those arrays, and + # 3. save the list of transposed arrays back to + # the 'weights' list. + weights_T = [np.array(x).T for x in weights] + weights = weights_T + + # else, leave the 'weights' list unchanged + # leave the elements of 'weights' untransposed. + layer = model.layers[k] if isinstance(layer, Convolution2D): weights[0] = np.array(weights[0])[:, :, ::-1, ::-1] layer.set_weights(weights) f.close() - return model + return model \ No newline at end of file