InvalidArgumentError: Incompatible shapes: [128,14,14,16] vs. [8] [[{{node max_unpooling2d_4/max_unpooling2d_4/mul_4}}]] [[{{node Mean_1}}]]
fatemeh190 opened this issue · 1 comments
fatemeh190 commented
hello
my code is worked as autoencoder but when i want to train it has error in dimension!
can you help me to adjust it?
thanks alot.....
from keras import layers
from keras.models import Model
from keras import optimizers, losses
from layers1 import MaxPoolingWithArgmax2D, MaxUnpooling2D
#import tensorflow as tf
#===============================================================================
# Prepare data
from keras.datasets import mnist
import numpy as np
(x_train, _), (x_test, _) = mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) # adapt this if using `channels_first` image data format
pool_size=(2,2)
#===============================================================================
# Create Layers + Model
input_img = layers.Input(shape=(28, 28, 1)) # adapt this if using `channels_first` image data format
x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x2 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1)
pool_1, mask_1 = MaxPoolingWithArgmax2D(pool_size=(2, 2))(x2)
x3 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(pool_1)
x4 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x3)
pool_2, mask_2 = MaxPoolingWithArgmax2D(pool_size=(2, 2))(x4)
unpool_1 = MaxUnpooling2D(pool_size)([pool_2, mask_2])
x5 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(unpool_1)
x6 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x5)
unpool_2 = MaxUnpooling2D(pool_size)([x6, mask_1])
x7 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(unpool_2)
x8 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x7)
decoded = layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x8)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer=optimizers.Adam(), loss=losses.binary_crossentropy)
print(autoencoder.summary())
#===============================================================================
# Train the Model
autoencoder.fit(x_train, x_train,
epochs=10,
batch_size=128,
shuffle=(True),
validation_data=(x_test, x_test))
InvalidArgumentError: Incompatible shapes: [128,14,14,16] vs. [8]
[[{{node max_unpooling2d_4/max_unpooling2d_4/mul_4}}]]
[[{{node Mean_1}}]]
fatemeh190 commented
layers1 that import in code is a part of codes for SegNet:
from keras import backend as K
from keras.layers import Layer
import tensorflow as tf
class MaxPoolingWithArgmax2D(Layer):
def __init__(self, pool_size=(2, 2), strides=(2, 2), padding="same", **kwargs):
super(MaxPoolingWithArgmax2D, self).__init__(**kwargs)
self.padding = padding
self.pool_size = pool_size
self.strides = strides
def call(self, inputs, **kwargs):
padding = self.padding
pool_size = self.pool_size
strides = self.strides
if K.backend() == "tensorflow":
ksize = [1, pool_size[0], pool_size[1], 1]
padding = padding.upper()
strides = [1, strides[0], strides[1], 1]
output, argmax = tf.nn.max_pool_with_argmax(
inputs, ksize=ksize, strides=strides, padding=padding
)
else:
errmsg = "{} backend is not supported for layer {}".format(
K.backend(), type(self).__name__
)
raise NotImplementedError(errmsg)
argmax = K.cast(argmax, K.floatx())
return [output, argmax]
def compute_output_shape(self, input_shape):
ratio = (1, 2, 2, 1)
output_shape = [
dim // ratio[idx] if dim is not None else None
for idx, dim in enumerate(input_shape)
]
output_shape = tuple(output_shape)
return [output_shape, output_shape]
def compute_mask(self, inputs, mask=None):
return 2 * [None]
class MaxUnpooling2D(Layer):
def __init__(self, size=(2, 2), **kwargs):
super(MaxUnpooling2D, self).__init__(**kwargs)
self.size = size
def call(self, inputs, output_shape=None):
updates, mask = inputs[0], inputs[1]
with tf.variable_scope(self.name):
mask = tf.cast(mask, "int32")
input_shape = tf.shape(updates, out_type="int32")
# calculation new shape
if output_shape is None:
output_shape = (
input_shape[0],
input_shape[1] * self.size[0],
input_shape[2] * self.size[1],
input_shape[3],
)
self.output_shape1 = output_shape
# calculation indices for batch, height, width and feature maps
one_like_mask = tf.ones_like(mask, dtype="int32")
batch_shape = tf.concat([[input_shape[0]], [1], [1], [1]], axis=0)
batch_range = tf.reshape(
tf.range(output_shape[0], dtype="int32"), shape=batch_shape
)
b = one_like_mask * batch_range
y = mask // (output_shape[2] * output_shape[3])
x = (mask // output_shape[3]) % output_shape[2]
feature_range = tf.range(output_shape[3], dtype="int32")
f = one_like_mask * feature_range
# transpose indices & reshape update values to one dimension
updates_size = tf.size(updates)
indices = tf.transpose(tf.reshape(tf.stack([b, y, x, f]), [4, updates_size]))
values = tf.reshape(updates, [updates_size])
ret = tf.scatter_nd(indices, values, output_shape)
return ret
def compute_output_shape(self, input_shape):
mask_shape = input_shape[1]
return (
mask_shape[0],
mask_shape[1] * self.size[0],
mask_shape[2] * self.size[1],
mask_shape[3],
)