我是一名本科生,致力于使用TensorFlow重新创建论文“使用生成的对抗网络实现逼真的单图像超分辨率”。训练完成后尝试测试网络时,出现关于不兼容形状的错误(即使它适用于训练)。看来此错误与Keras Sequential有关。
错误结尾给出:
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [24,24,64] vs. [16,256,256,64] [Op:Mul] name: generator/sequential/p_re_lu/mul/
其中训练输入形状为(16,24,24,3)
,测试输入形状为(1,256,256,3)
。网络中的第一层是tf.keras.layers.Conv2D(64, 9, strides=1, padding='same'),
层。
我从顺序中分离出了一些层,并打印了输出形状作为验证。我留在顺序块中的图层会抛出相同的错误,而分开的图层可以正常工作
输出:
x_in: (16, 24, 24, 3)
x1: (16, 24, 24, 64)
x2a: (16, 24, 24, 64)
x2: (16, 24, 24, 64)
x3: (16, 24, 24, 64)
x4: (16, 48, 48, 256)
x5: (16, 96, 96, 256)
x_out: (16, 96, 96, 3)
x_in: (1, 256, 256, 3)
x1: (1, 256, 256, 64)
x2a: (1, 256, 256, 64)
Traceback (most recent call last):
File "srgan.py", line 249, in <module> ...
顺序块:
self.residual = tf.keras.Sequential([
Residual(cfg, num_filters),
Residual(cfg, num_filters),
Residual(cfg, num_filters),
Residual(cfg, num_filters),
])
通话功能
def call(self, x_in):
print(f'x_in: {x_in.shape}')
x1a = tf.keras.layers.Conv2D(64, 9, strides=1, padding='same')(x_in)
x1 = tf.keras.layers.PReLU()(x1a)
#x1 = self.start(x_in)
print(f'x1: {x1.shape}')
x2a = Residual(self.cfg, 64)(x1)
print(f'x2a: {x2a.shape}')
x2 = self.residual(x1)
我希望序列与分离的层相同。我是否缺少某些东西或这是一个错误?
edit1: 当我说我分离层时,我的意思是将层移到顺序之外。
我将它们全部删除,然后再次尝试模型。该错误现在针对tf.keras.layers.PReLU()
,这是有道理的,因为该错误提到了name: generator/sequential/p_re_lu/mul/
很奇怪,这样会给比较形状带来错误
这是模型摘要
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) multiple 15616
_________________________________________________________________
p_re_lu (PReLU) multiple 36864
_________________________________________________________________
residual (Residual) multiple 111232
_________________________________________________________________
residual_1 (Residual) multiple 111232
_________________________________________________________________
residual_2 (Residual) multiple 111232
_________________________________________________________________
residual_3 (Residual) multiple 111232
_________________________________________________________________
conv2d_9 (Conv2D) multiple 331840
_________________________________________________________________
batch_normalization_v2_8 (Ba multiple 256
_________________________________________________________________
conv2d_transpose (Conv2DTran multiple 147712
_________________________________________________________________
p_re_lu_5 (PReLU) multiple 589824
_________________________________________________________________
conv2d_transpose_1 (Conv2DTr multiple 590080
_________________________________________________________________
p_re_lu_6 (PReLU) multiple 2359296
_________________________________________________________________
conv2d_10 (Conv2D) multiple 6915
=================================================================
Total params: 4,523,331
Trainable params: 4,522,179
Non-trainable params: 1,152
答案 0 :(得分:0)
事实证明,您需要重新初始化以下模型。 Is loading in eager TensorFlow broken right now?
有多种方法可以做到,如上面链接中问题的答案所述。