Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras Concatenate TypeError: __init__() got multiple values for argument 'axis'

I am currently trying to recreate the Unet. At the "upconvolution" part where the outputs of two layers needs to be merged I got the mentioned error. (TypeError: init() got multiple values for argument 'axis')

  • Keras Version: 2.0.6
  • Tensorflow-gpu: 1.2.1

Code snippet:

import gzip
import os

from six.moves import urllib
import tensorflow as tf
import numpy as np

from keras.models import Sequential, Model
from keras.layers import Input, Dropout, Flatten, Concatenate
from keras.layers import Conv2D, MaxPool2D, Conv2DTranspose
from keras.utils import np_utils
import keras.callbacks

# Define model architecture
input1 = Input((X_train.shape[1], X_train.shape[2], 1))

conv1 = Conv2D(64,(3,3), activation='relu', padding='same')(input1)
conv1 = Dropout(0.2)(conv1)
conv1 = Conv2D(64,(3,3), activation='relu', padding='same')(conv1)
pool1 = MaxPool2D(pool_size=(2,2))(conv1)

conv2 = Conv2D(128,(3,3), activation='relu', padding='same')(pool1)
conv2 = Dropout(0.2)(conv2)
conv2 = Conv2D(128,(3,3), activation='relu')(conv2)
pool2 = MaxPool2D(pool_size=(2,2))(conv2)

conv3 = Conv2D(256,(3,3), activation='relu', padding='same')(pool2)
conv3 = Dropout(0.2)(conv3)
conv3 = Conv2D(256,(3,3), activation='relu', padding='same')(conv3)
pool3 = MaxPool2D(pool_size=(2,2))(conv3)

conv4 = Conv2D(512,(3,3), activation='relu', padding='same')(pool3)
conv4 = Conv2D(512,(3,3), activation='relu', padding='same')(conv4)

up5 = Concatenate([Conv2DTranspose(256, (2,2), strides=(2,2),padding='same')(conv4), conv3], axis=3)
conv5 = Conv2D(256,(3,3), activation='relu', padding='same')(up5)
conv5 = Conv2D(256,(3,3), activation='relu', padding='same')(conv5)

Detailed error message:

Traceback (most recent call last):

File "<ipython-input-48-d61955511ff9>", line 1, in <module>
runfile('C:/Users/.../MNIST_Unet_new.py', wdir='C:/Users/.../Documents/KerasTutorials')

File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\spyder\utils\site\sitecustomize.py", line 688, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/.../MNIST_Unet_new.py", line 107, in <module>
up5 = Concatenate([Conv2DTranspose(256, (2,2), strides=(2,2),padding='same')(conv4), conv3], axis=3)

TypeError: __init__() got multiple values for argument 'axis'
like image 603
Midas.Inc Avatar asked Aug 09 '17 07:08

Midas.Inc


3 Answers

I have found a solution that seems to work!

I have done two changes to the code.

  1. Instead of using keras.layers.Concatenate I use keras.layers.concatenate
  2. I "excluded" the Conv2dTranspose step from the concatenation

The relevant code snippet now looks like this

trans5 = Conv2DTranspose(256, (2,2), strides=(2,2),padding='same')(conv4)
up5 = keras.layers.concatenate([trans5, conv3], axis=3)

Might this be some kind of bug in keras? Should I report that issue?

Thanks so much for your help anyway. Appreciate it!

like image 161
Midas.Inc Avatar answered Nov 10 '22 17:11

Midas.Inc


I was also getting error like this.

up5_0 = Concatenate([UpSampling2D(size=(2, 2))(conv4_0), conv4], axis=3)
TypeError: __init__() got multiple values for argument 'axis'

I solved it using just concatenate instead of Concatenate

up5_0 = concatenate([UpSampling2D(size=(2, 2))(conv4_0), conv4], axis=3)
like image 21
Shehab Ahmed Sayem Avatar answered Nov 10 '22 16:11

Shehab Ahmed Sayem


Concatenate is a layer of keras,the use of it is

keras.layers.Concatenate(axis=-1)

Here if you want to use Concatenate instead of concatenate.you should use like this:

up5 = Concatenate(axis=3)([Conv2DTranspose(256, (2,2), strides=(2,2),padding='same')(conv4), conv3])

Hope useful for you!

like image 35
Han Avatar answered Nov 10 '22 18:11

Han