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

与世无争的帅哥 提交于 2021-02-09 05:47:45

问题


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'

回答1:


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!




回答2:


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)



回答3:


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!



来源:https://stackoverflow.com/questions/45584258/keras-concatenate-typeerror-init-got-multiple-values-for-argument-axis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!