我正在尝试进行不成对的图像转换,我有两个数据集,第一个数据集的形状为(1067、256、256、3),而另一个数据集的形状为(1337、256、256、3)
该模型的代码如下:
class ReflectionPad2D(tf.keras.layers.Layer):
def __init__(self, padding=(1,1)):
self.padding = tuple(padding)
super(ReflectionPad2D, self).__init__()
def call(self, input_tensor, mask=None):
padding_width, padding_height = self.padding
padding_tensor = [[0,0],[padding_height,padding_height],[padding_width,padding_width],[0,0]]
return tf.pad(input_tensor,padding_tensor,mode='REFLECT')
class ResNetBlock(tf.keras.layers.Layer):
def __init__(self, num_filters, kernel_init=None, use1by1conv=False, stides=1):
super(ResNetBlock, self).__init__()
if kernel_init == None:
self.kernel_init = tf.keras.initializers.RandomNormal(0.0, 0.02)
else:
self.kernel_init = kernel_init
self.conv_1 = tf.keras.layers.Conv2D(256, (3, 3), 1, padding='valid',
kernel_initializer=self.kernel_init, use_bias=False)
self.conv_2 = tf.keras.layers.Conv2D(256, (3, 3), 1, padding='valid',
kernel_initializer=self.kernel_init, use_bias=False)
self.conv_3 = None
if use1by1conv:
self.conv_3 = tf.keras.layers.Conv2D(256, (1, 1), 1)
self.instance_norm1 = InstanceNormalization(axis=-1)
self.instance_norm2 = InstanceNormalization(axis=-1)
self.reflect_pad1 = ReflectionPad2D()
self.reflect_pad2 = ReflectionPad2D()
def call(self, x):
y = self.reflect_pad1(x)
y = tf.keras.activations.relu(self.instance_norm1(self.conv_1(y)))
y = self.reflect_pad2(y)
y = self.instance_norm2(self.conv_2(y))
y = tf.add(y, x)
return y
def build_gen(input_shape, K_init):
inp = tf.keras.layers.Input(shape=input_shape)
x = tf.keras.layers.Conv2D(64, (7, 7), kernel_initializer=K_init, strides=1, padding='same')(inp)
x = InstanceNormalization(axis=-1)(x)
x = tf.keras.layers.Activation('relu')(x)
x = tf.keras.layers.Conv2D(128, (3, 3), kernel_initializer=K_init, strides=2, padding='same')(inp)
x = InstanceNormalization(axis=-1)(x)
x = tf.keras.layers.Activation('relu')(x)
x = tf.keras.layers.Conv2D(256, (3, 3), kernel_initializer=K_init, strides=2, padding='same')(inp)
x = InstanceNormalization(axis=-1)(x)
x = tf.keras.layers.Activation('relu')(x)
x = ResNetBlock(256, kernel_init=K_init)(x)
x = ResNetBlock(256, kernel_init=K_init)(x)
x = ResNetBlock(256, kernel_init=K_init)(x)
x = ResNetBlock(256, kernel_init=K_init)(x)
x = ResNetBlock(256, kernel_init=K_init)(x)
x = ResNetBlock(256, kernel_init=K_init)(x)
x = ResNetBlock(256, kernel_init=K_init)(x)
x = ResNetBlock(256, kernel_init=K_init)(x)
x = ResNetBlock(256, kernel_init=K_init)(x)
x = tf.keras.layers.Conv2DTranspose(128, kernel_size=(3, 3), kernel_initializer=K_init, strides=2, padding='same')(x)
x = InstanceNormalization(axis=-1)(x)
x = tf.keras.layers.Activation('relu')(x)
x = tf.keras.layers.Conv2DTranspose(64, kernel_size=(3, 3), kernel_initializer=K_init, strides=2, padding='same')(x)
x = InstanceNormalization(axis=-1)(x)
x = tf.keras.layers.Activation('relu')(x)
last_x = tf.keras.layers.Conv2DTranspose(3, kernel_size=(7, 7), kernel_initializer=K_init, strides=1, padding='same')(x)
last_x = InstanceNormalization(axis=-1)(last_x)
last_x = tf.keras.layers.Activation('tanh')(last_x)
return tf.keras.Model(inputs=inp, outputs=last_x)
def build_discriminator(input_shape, k_init):
inp = tf.keras.layers.Input(shape=input_shape)
x = tf.keras.layers.Conv2D(64, (4, 4), kernel_initializer=k_init, strides=2, padding='same')(inp)
x = tf.keras.layers.LeakyReLU(alpha=0.2)(x)
x = tf.keras.layers.Conv2D(128, (4, 4), kernel_initializer=k_init, strides=2, padding='same')(x)
x = InstanceNormalization(axis=-1)(x)
x = tf.keras.layers.LeakyReLU(alpha=0.2)(x)
x = tf.keras.layers.Conv2D(256, (4, 4), kernel_initializer=k_init, strides=2, padding='same')(x)
x = InstanceNormalization(axis=-1)(x)
x = tf.keras.layers.LeakyReLU(alpha=0.2)(x)
x = tf.keras.layers.Conv2D(256, (4, 4), kernel_initializer=k_init, strides=2, padding='same')(x)
x = InstanceNormalization(axis=-1)(x)
x = tf.keras.layers.LeakyReLU(alpha=0.2)(x)
x = tf.keras.layers.Conv2D(512, (4, 4), kernel_initializer=k_init, padding='same')(x)
x = InstanceNormalization(axis=-1)(x)
x = tf.keras.layers.LeakyReLU(alpha=0.2)(x)
output_x = tf.keras.layers.Conv2D(1, kernel_size=(4, 4), padding='same', kernel_initializer=k_init)(x)
output_x = InstanceNormalization(axis=-1)(output_x)
output_x = tf.keras.layers.LeakyReLU(alpha=0.2)(output_x)
return tf.keras.Model(inputs=inp, outputs=output_x)
完整的回溯是:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:806 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:789 run_step **
outputs = model.train_step(data)
<ipython-input-32-15044c300934>:50 train_step
identity_loss_G = self.identity_loss_fn(real_y, identity_y) * self.lambda_val_cycle * self.lambda_val_identity
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:149 __call__
losses = ag_call(y_true, y_pred)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:253 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:1228 mean_absolute_error
return K.mean(math_ops.abs(y_pred - y_true), axis=-1)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:1141 binary_op_wrapper
raise e
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:1125 binary_op_wrapper
return func(x, y, name=name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:527 subtract
return gen_math_ops.sub(x, y, name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_math_ops.py:10466 sub
"Sub", x=x, y=y, name=name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:744 _apply_op_helper
attrs=attr_protos, op_def=op_def)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py:593 _create_op_internal
compute_device)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:3485 _create_op_internal
op_def=op_def)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1975 __init__
control_input_ops, op_def)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1815 _create_c_op
raise ValueError(str(e))
ValueError: Dimensions must be equal, but are 512 and 256 for '{{node mean_absolute_error/sub}} = Sub[T=DT_FLOAT](functional_33/activation_53/Tanh_1, IteratorGetNext:1)' with input shapes: [?,512,512,3], [?,256,256,3].
我已经仔细检查过,我的图像形状都是(256、256、3),并且所有模型输出都应该正确,但是令我感到困惑的是,当我将512作为输入尺寸时,错误就出现了。输入为1024。随时问任何问题或要求提供更多代码。谢谢!