Tensorflowでプログラムを書いているのですが,
number of input channels does not match corresponding dimension of filter, 2 != 11
というエラーが出ています.
プログラムを以下に添付します.
def make_test(self):
inputs = Input(shape=(28, 28, 11), name='label')
x = Conv2D(64, (5, 5), padding='same')(inputs)
x = LeakyReLU()(x)
x = Conv2D(128, (5, 5), kernel_initializer='he_normal', strides=[2, 2])(x)
x = LeakyReLU()(x)
x = Conv2D(128, (5, 5), kernel_initializer='he_normal', padding='same', strides=[2, 2])(x)
x = LeakyReLU()(x)
x = Flatten()(x)
x = Dense(1024, kernel_initializer='he_normal')(x)
x = LeakyReLU()(x)
x = Dense(1, kernel_initializer='he_normal')(x)
model = Model(inputs=[inputs], outputs=[x])
return model
def make_hoge(self):
noise_shape = (110,)
inputs = Input(shape=noise_shape)
x = Dense(1024)(inputs)
x = LeakyReLU()(x)
x = Dense(128 * 7 * 7)(x)
x = BatchNormalization()(x)
x = LeakyReLU()(x)
if K.image_data_format() == 'channels_first':
x = Reshape((128, 7, 7), input_shape=(128 * 7 * 7,))(x)
bn_axis = 1
else:
x = Reshape((7, 7, 128), input_shape=(128 * 7 * 7,))(x)
bn_axis = -1
x = Conv2DTranspose(128, (5, 5), strides=2, padding='same')(x)
x = BatchNormalization(axis=bn_axis)(x)
x = LeakyReLU()(x)
x = Conv2D(64, (5, 5), padding='same')(x)
x = BatchNormalization(axis=bn_axis)(x)
x = LeakyReLU()(x)
x = Conv2DTranspose(64, (5, 5), strides=2, padding='same')(x)
x = BatchNormalization(axis=bn_axis)(x)
x = LeakyReLU()(x)
x = Conv2D(1, (5, 5), padding='same', activation='tanh')(x)
model = Model(inputs=[inputs], outputs=[x])
return model
def loss(self, y_true, y_pred):
return K.mean(y_true * y_pred)
def __init__():
self.test = self.make_test()
self.hoge = self.make_hoge()
for layer in self.test.layers:
layer.trainable = False
self.test.trainable = False
input_noise = Input(shape=(11,))
generated_images = self.hoge(input_noise)
input_label = Input(shape=(28, 28, 10,))
inputs = concatenate([generated_images, input_label], axis=3)
outputs = self.test(inputs)
self.train_model = Model(inputs=[input_noise, input_label], outputs=[outputs])
self.train_model.compile(optimizer=Adam(0.0001, beta_1=0.5, beta_2=0.9), loss=self.loss)
アルゴリズムの内容はGANです.変数名を短く書き直して上げたので間違っていたらすみません.
環境はpython2.7, tensorflow1.12です.
ちなみにinputs, generated_images, input_labelはshapeを出力するとそれぞれ(?,28,28,11), (?,?,?,1), (?,28,28,10)になっています.
generated_imagesをconcatenateせずにself.testに入れると動作することを確認しています.input_labelは動きませんでした.
conv層を消してみると動きます.
あと,エラー文が出ているのがtensorflowのnn_ops.pyの836行目付近なのですが,input_shape, filter_shape, num_spatial_dimsを出力してみると(?, 28, 28, 2), (5, 5, 11, 64), 2でした.filter_shape[num_spatial_dims]!=input_shape[num_spatial_dims+1]なのが問題のようです.
よろしくお願いいたします.