Dobiasd / frugally-deep

A lightweight header-only library for using Keras (TensorFlow) models in C++.
MIT License
1.06k stars 235 forks source link

assert layer.data_format == 'channels_last' Error #63

Closed aligoglos closed 6 years ago

aligoglos commented 6 years ago

i trained model with this definition :

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'))

but when i want convert model with script convert_model.py this error occur :

C:\Users\127051\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\sequential.py:109: UserWarning: `Sequential.model` is deprecated. `Sequential` is a subclass of `Model`, you can just use your `Sequential` instance directly.
  warnings.warn('`Sequential.model` is deprecated. '
Forward pass took 0.988684 s.
Forward pass took 0.006995 s.
Forward pass took 0.007967 s.
Starting performance measurements.
Forward pass took 0.007939 s.
Forward pass took 0.007978 s.
Forward pass took 0.007952 s.
Forward pass took 0.007986 s.
Forward pass took 0.007979 s.
Forward pass took 0.0079668 s on average.
Traceback (most recent call last):
  File "convert_model.py", line 540, in <module>
    main()
  File "convert_model.py", line 536, in main
    convert(in_path, out_path)
  File "convert_model.py", line 517, in convert
    json_output['trainable_params'] = get_all_weights(model)
  File "convert_model.py", line 358, in get_all_weights
    assert layer.data_format == 'channels_last'
AssertionError
Dobiasd commented 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?

aligoglos commented 6 years ago

I use TensorFlow as backend and keras version is 2.2 and tensorflow is 1.8 and i download latest frugally-deep.

Dobiasd commented 6 years ago

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'))
aligoglos commented 6 years ago

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;
Dobiasd commented 6 years ago

What's the value for inputShape you call this function with?

aligoglos commented 6 years ago

inputShape : (32 ,32, 1)

Dobiasd commented 6 years ago

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.

aligoglos commented 6 years ago

i upload my model : my model

Dobiasd commented 6 years ago

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
aligoglos commented 6 years ago

this is model of This Code i trained in my machine

Dobiasd commented 6 years ago

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.

aligoglos commented 6 years ago

this issue solved with :`

from keras import backend as K
K.set_image_data_format('channels_last')