Hello, I am running lesson1.ipynb, when I create the VGG16 model, then I can’t load vgg16.h5,
This is my code:
import keras
from keras import Sequential, Model
from keras.layers.convolutional import ZeroPadding2D, MaxPooling2D, Conv2D,Convolution2D
from numpy.random import random, permutation
from scipy import misc, ndimage
from scipy.ndimage.interpolation import zoom
from keras.utils.data_utils import get_file
from keras import backend as K
from keras.layers.core import Flatten, Dense, Dropout, Lambda
from keras.layers import Input
from keras.optimizers import SGD, RMSprop
from keras.preprocessing import image
import numpy as np
def ConvBlock(layers, model, filters):
for i in range(layers):
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(filters, kernel_size=(3, 3), activation='relu', padding="same"))
model.add(MaxPooling2D((2,2), strides=(2,2)))
def FCBlock(model):
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
vgg_mean = np.array([123.68, 116.779, 103.939]).reshape((3,1,1))
def vgg_preprocess(x):
x = x - vgg_mean # subtract mean
return x[:, ::-1] # reverse axis bgr->rgb
def VGG_16():
model = Sequential()
model.add(Lambda(vgg_preprocess, input_shape=(3,224,224)))
ConvBlock(2, model, 64)
ConvBlock(2, model, 128)
ConvBlock(3, model, 256)
ConvBlock(3, model, 512)
ConvBlock(3, model, 512)
model.add(Flatten())
FCBlock(model)
FCBlock(model)
model.add(Dense(1000, activation='softmax'))
return model
model = VGG_16()
FILES_PATH = 'http://files.fast.ai/models/'
CLASS_FILE='imagenet_class_index.json'
fpath = get_file('vgg16.h5', FILES_PATH+'vgg16.h5', cache_subdir='models')
model.load_weights(fpath)
But I got the following error:
InvalidArgumentError Traceback (most recent call last)
~/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
1588 try:
-> 1589 c_op = c_api.TF_FinishOperation(op_desc)
1590 except errors.InvalidArgumentError as e:
InvalidArgumentError: Dimension 0 in both shapes must be equal, but are 3 and 64. Shapes are [3,3,224,64] and [64,3,3,3]. for 'Assign_2' (op: 'Assign') with input shapes: [3,3,224,64], [64,3,3,3].
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-18-3fc2194f3419> in <module>()
1 # fpath = get_file('vgg16.h5', FILES_PATH+'vgg16.h5', cache_subdir='models')
2 fpath = "/atlas/home/hzhou/.keras/models/vgg16_weights_tf_dim_ordering_tf_kernels.h5"
----> 3 model.load_weights(fpath)
/usr/local/anaconda3/lib/python3.6/site-packages/keras/models.py in load_weights(self, filepath, by_name, skip_mismatch, reshape)
736 reshape=reshape)
737 else:
--> 738 topology.load_weights_from_hdf5_group(f, layers, reshape=reshape)
739
740 def save_weights(self, filepath, overwrite=True):
/usr/local/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py in load_weights_from_hdf5_group(f, layers, reshape)
3380 ' elements.')
3381 weight_value_tuples += zip(symbolic_weights, weight_values)
-> 3382 K.batch_set_value(weight_value_tuples)
3383
3384
/usr/local/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in batch_set_value(tuples)
2366 assign_placeholder = tf.placeholder(tf_dtype,
2367 shape=value.shape)
-> 2368 assign_op = x.assign(assign_placeholder)
2369 x._assign_placeholder = assign_placeholder
2370 x._assign_op = assign_op
~/.local/lib/python3.6/site-packages/tensorflow/python/ops/variables.py in assign(self, value, use_locking)
643 the assignment has completed.
644 """
--> 645 return state_ops.assign(self._variable, value, use_locking=use_locking)
646
647 def assign_add(self, delta, use_locking=False):
~/.local/lib/python3.6/site-packages/tensorflow/python/ops/state_ops.py in assign(ref, value, validate_shape, use_locking, name)
217 return gen_state_ops.assign(
218 ref, value, use_locking=use_locking, name=name,
--> 219 validate_shape=validate_shape)
220 return ref.assign(value, name=name)
221
~/.local/lib/python3.6/site-packages/tensorflow/python/ops/gen_state_ops.py in assign(ref, value, validate_shape, use_locking, name)
58 _, _, _op = _op_def_lib._apply_op_helper(
59 "Assign", ref=ref, value=value, validate_shape=validate_shape,
---> 60 use_locking=use_locking, name=name)
61 _result = _op.outputs[:]
62 _inputs_flat = _op.inputs
~/.local/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
785 op = g.create_op(op_type_name, inputs, output_types, name=scope,
786 input_types=input_types, attrs=attr_protos,
--> 787 op_def=op_def)
788 return output_structure, op_def.is_stateful, op
789
~/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
3412 input_types=input_types,
3413 original_op=self._default_original_op,
-> 3414 op_def=op_def)
3415
3416 # Note: shapes are lazily computed with the C API enabled.
~/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
1754 op_def, inputs, node_def.attr)
1755 self._c_op = _create_c_op(self._graph, node_def, grouped_inputs,
-> 1756 control_input_ops)
1757 else:
1758 self._c_op = None
~/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
1590 except errors.InvalidArgumentError as e:
1591 # Convert to ValueError for backwards compatibility.
-> 1592 raise ValueError(str(e))
1593
1594 return c_op
ValueError: Dimension 0 in both shapes must be equal, but are 3 and 64. Shapes are [3,3,224,64] and [64,3,3,3]. for 'Assign_2' (op: 'Assign') with input shapes: [3,3,224,64], [64,3,3,3].
I don’t understand why this is, is it because I use keras==2.2.4? This sounds ridiculous, isn’t it?