Closed aligoglos closed 6 years ago
Usually you don't need to explicitly set data_format='channels_last'
for each layer, because it's already the default parameter.
Also K.image_data_format() == 'channels_last'
seems to be the case with your setup, because otherwise this assertion in convert_model.py
would have triggered before.
1) Are you using Theano or CNTK as backend instead of TensorFlow? Frugally-deep only supports the latter.
2) What versions of Keras and TensorFlow do you have installed?
3) Are you using the latest version of frugally-deep?
I use TensorFlow as backend and keras version is 2.2 and tensorflow is 1.8 and i download latest frugally-deep.
Can you please post the actual source of your model definition?
The code you posted is broken (invalid Python):
model_vgg = Sequential()
model_vgg.add(Conv2D(128, 9, strides=(1, 1), input_shape=inputShape,
data_format='channels_last', activation='relu', name='conv1_1'))
model_vgg.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1),data_format='channels_last'))
model_vgg.add(Conv2D(256, 7, strides=(1, 1), activation='relu', name='conv2_1',data_format='channels_last'))
name='conv2_2'),data_format='channels_last')
model_vgg.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1),data_format='channels_last'))
model_vgg.add(Conv2D(256, 5, strides=(1, 1), activation='relu', name='conv3_1',data_format='channels_last'))
model_vgg.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1),data_format='channels_last'))
model_vgg.add(Conv2D(256, 3, strides=(1, 1), activation='relu', name='conv4_1',data_format='channels_last'))
model_vgg.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2),data_format='channels_last'))
model_vgg.add(Flatten())
model_vgg.add(Dense(256, activation='relu'))
model_vgg.add(Dropout(0.5))
model_vgg.add(Dense(2, activation='softmax'))
definition method :
def createNetwork(inputShape):
print(inputShape);
model_vgg = Sequential()
model_vgg.add(Conv2D(128, 9, strides=(1, 1), input_shape=inputShape,
data_format='channels_last', activation='relu', name='conv1_1'))
# model_vgg.add(ZeroPadding2D((1, 1)))
# model_vgg.add(Convolution2D(64, 3, 1, activation='relu', name='conv1_2'))
model_vgg.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1),data_format='channels_last'))
model_vgg.add(Conv2D(256, 7, strides=(1, 1), activation='relu', name='conv2_1',data_format='channels_last'))
# model_vgg.add(ZeroPadding2D((1, 1)))
# model_vgg.add(Convolution2D(128, 3, 1, activation='relu', name='conv2_2'),data_format='channels_last')
model_vgg.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1),data_format='channels_last'))
# model_vgg.add(keras.layers.ActivityRegularization(l1=0.0, l2=1.0))
model_vgg.add(Conv2D(256, 5, strides=(1, 1), activation='relu', name='conv3_1',data_format='channels_last'))
# model_vgg.add(ZeroPadding2D((1, 1)))
# model_vgg.add(Convolution2D(256, 3,1, activation='relu', name='conv3_2'))
# model_vgg.add(ZeroPadding2D((1, 1)))
# model_vgg.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3'))
model_vgg.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1),data_format='channels_last'))
model_vgg.add(Conv2D(256, 3, strides=(1, 1), activation='relu', name='conv4_1',data_format='channels_last'))
# model_vgg.add(ZeroPadding2D((1, 1)))
# model_vgg.add(Convolution2D(512, 3, 1, activation='relu', name='conv4_2'))
# model_vgg.add(ZeroPadding2D((1, 1)))
# model_vgg.add(Convolution2D(512, 3, 1, activation='relu', name='conv4_3'))
model_vgg.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2),data_format='channels_last'))
model_vgg.add(Flatten())
model_vgg.add(Dense(256, activation='relu'))
model_vgg.add(Dropout(0.5))
model_vgg.add(Dense(2, activation='softmax'))
sgd = optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
adam = keras.optimizers.Adam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False);
model_vgg.compile(loss='mse',
optimizer=sgd,
metrics=['accuracy'])
model_vgg.summary(line_length=None, positions=None, print_fn=print)
return model_vgg;
What's the value for inputShape
you call this function with?
inputShape : (32 ,32, 1)
OK, so the actual minimal code (reference 1, reference 2 :wink:) to reproduce your model then seems to be the following:
# fdeep_issue63.py
import keras.optimizers as optimizers
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
inputShape = (32, 32, 1)
model_vgg = Sequential()
model_vgg.add(Conv2D(128, 9, strides=(1, 1), input_shape=inputShape, data_format='channels_last', activation='relu', name='conv1_1'))
model_vgg.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1), data_format='channels_last'))
model_vgg.add(Conv2D(256, 7, strides=(1, 1), activation='relu', name='conv2_1', data_format='channels_last'))
model_vgg.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1), data_format='channels_last'))
model_vgg.add(Conv2D(256, 5, strides=(1, 1), activation='relu', name='conv3_1', data_format='channels_last'))
model_vgg.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1), data_format='channels_last'))
model_vgg.add(Conv2D(256, 3, strides=(1, 1), activation='relu', name='conv4_1', data_format='channels_last'))
model_vgg.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), data_format='channels_last'))
model_vgg.add(Flatten())
model_vgg.add(Dense(256, activation='relu'))
model_vgg.add(Dropout(0.5))
model_vgg.add(Dense(2, activation='softmax'))
sgd = optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
model_vgg.compile(loss='mse', optimizer=sgd, metrics=['accuracy'])
model_vgg.save('model_vgg.h5', include_optimizer=False)
I run it like this:
python3 fdeep_issue63.py
and convert the model as follows:
python3 convert_model.py model_vgg.h5 model_vgg.json
It converted without any error and produced a model_vgg.json
file.
Can you please check if these exact same steps (with the exact same code) really reproduce the problem on your machine? If so, we should check if you are working with any non-default system settings.
Is this the file produced by executing the code I posted on your machine?
I'm asking, because the layers of the model you uploaded have the following data formats:
input_1
conv1_1: channels_last
max_pooling2d_1: channels_last
conv2_1: channels_last
max_pooling2d_2: channels_last
conv3_1: channels_last
max_pooling2d_3: channels_last
conv4_1: channels_last
max_pooling2d_4: channels_last
flatten_1: channels_first
Again, this is no complete code to produce the model. One can not simply paste it into a .py
file and run it. That is why I created an actual minimal example code and asked you run it.
Anyways, to keep things short, please use data_format='channels_last'
for your Flatten
layer too, i.e.:
model_vgg.add(Flatten(data_format='channels_last'))
This should help.
Some settings on your system seem to default to channels_first
by default. So you either can always set channels_last
explicitly every time or find this setting and change it, or try to re-install the libraries freshly without adopting the old config files.
this issue solved with :`
from keras import backend as K
K.set_image_data_format('channels_last')
i trained model with this definition :
but when i want convert model with script
convert_model.py
this error occur :