dave
dave

Reputation: 113

Conv1D with data_format channels_first yields error on Keras

I am trying to define a basic ConvNet using one Conv1D operation as follows:

n_class = 10

# channels last (default)
input_shape = (100, 1)
inp = Input(shape=input_shape)
x = Conv1D(4, kernel_size=9, activation=activations.relu, padding='same')(inp)

x = MaxPool1D(pool_size=5)(x)
x = Flatten()(x)
out = Dense(n_class, activation='softmax')(x)

model = Model(inputs=inp, outputs=out)

and that works fine (which uses data_format='channels_last' as default). However, if, instead, I want to use data_format='channels_first':

# "channels_first" inputs with shape (batch, channels, length).
input_shape = (1, 100)
inp = Input(shape=input_shape)
x = Conv1D(4, kernel_size=9, activation=activations.relu, data_format='channels_first', padding='same')(inp)

x = MaxPool1D(pool_size=5, data_format='channels_first')(x)
x = Flatten()(x)
out = Dense(n_class, activation='softmax')(x)

model = Model(inputs=inp, outputs=out)

I get the following error when defining the Conv1D layer:

Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 974, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/edward/Desktop/baselines/cnn_mel.py", line 720, in <module>
    model = get_1d_dummy_model(params_learn=params_learn, params_extract=params_extract)
  File "/Users/edward/Desktop/baselines/architectures.py", line 71, in get_1d_dummy_model
    x = Conv1D(4, kernel_size=9, activation=activations.relu, data_format='channels_first', padding='same')(inp)
  File "/Users/edward/miniconda3/envs/baseline/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/Users/edward/miniconda3/envs/baseline/lib/python3.6/site-packages/keras/layers/convolutional.py", line 337, in __init__
    **kwargs)
TypeError: __init__() got multiple values for keyword argument 'data_format'

Any insight as to what is being done wrong? thanks!

Upvotes: 2

Views: 1604

Answers (1)

Y. Luo
Y. Luo

Reputation: 5732

It seems that you are not using the most recent code (not just most recent release). For Conv1D layer, data_format='channels_first' is not supported even in the most recent release 2.1.6. You will need to clone and use codes from the master branch. The support is added by this commit on 5/7/2018. The documentation is always synced to the master which can be confusing. The idea (from the Keras creator François Chollet) is that

versions only exist to force PyPI users to upgrade. They are not meaningful. You should be constantly synced to master.

You can find some old Keras documentation here.

Upvotes: 3

Related Questions