[英]Tensorflow - incompatible shape with conv2d after depth to space
I'm having a problem implementing a super-resolution model我在实现超分辨率模型时遇到了问题
class SRNet(Model):
def __init__(self, scale=4):
super(SRNet, self).__init__()
self.scale = scale
self.conv1 = Sequential([
layers.Conv2D(filters=64, kernel_size=3,
strides=(1, 1), padding="same", data_format="channels_first"),
layers.ReLU(),
])
self.residualBlocks = Sequential(
[ResidualBlock() for _ in range(16)])
self.convUp = Sequential([
layers.Conv2DTranspose(filters=64, kernel_size=3, strides=(
2, 2), padding="same", data_format="channels_first"),
layers.ReLU(),
layers.Conv2DTranspose(filters=64, kernel_size=3, strides=(
2, 2), padding="same", data_format="channels_first"),
layers.ReLU(),
])
self.reluAfterPixleShuffle = layers.ReLU()
self.convOut = layers.Conv2D(
filters=3, kernel_size=3, strides=(1, 1), padding="same", data_format="channels_first", input_shape=(4, 1440, 2560)) # (kernel, kernel, channel, output)
def call(self, lrCur_hrPrevTran):
lrCur, hrPrevTran = lrCur_hrPrevTran
x = tf.concat([lrCur, hrPrevTran], axis=1)
x = self.conv1(x)
x = self.residualBlocks(x)
x = self.convUp(x)
# pixel shuffle
Subpixel_layer = Lambda(lambda x: tf.nn.depth_to_space(
x, self.scale, data_format="NCHW"))
x = Subpixel_layer(inputs=x)
x = self.reluAfterPixleShuffle(x)
x = self.convOut(x)
return x
Error错误
/usr/src/app/generator.py:164 call *
x = self.convOut(x)
ValueError: Tensor's shape (3, 3, 64, 3) is not compatible with supplied shape (3, 3, 4, 3)
after reading the error I know that (3, 3, 4, 3) is (kernel size, kernel size, channel, output) mean that only channel of input is not correct阅读错误后我知道 (3, 3, 4, 3) is (kernel size, kernel size, channel, output) 意味着只有输入通道不正确
so I printed out the shape of the input所以我打印出输入的形状
# after pixel shuffle before convOut
print(x.shape)
>>> (1, 4, 1440, 2560) (batch size, channel, height, width)
but the shape of x after pixel shuffle (depth_to_space)
is (1, 4, 1440, 2560) the channel value is 4 which is the same as convOut
need但是
pixel shuffle (depth_to_space)
后x的形状pixel shuffle (depth_to_space)
是(1, 4, 1440, 2560)通道值是4,这和convOut
需要的一样
question is why the input's channel is changing from 4 to 64 as the error?问题是为什么输入的通道会从 4 变为 64 作为错误?
I have found a solution我找到了解决办法
First of all, I'm using checkpoints to save model weight when training首先,我在训练时使用检查点来保存模型权重
during the implementation and testing of the model, I have changed some of the layers so the input size is changed too, but my weight still remember the input size from the previous checkpoint在模型的实现和测试过程中,我改变了一些层,所以输入大小也改变了,但我的体重仍然记得上一个检查点的输入大小
so I delete the checkpoints folder and then everything works again所以我删除了检查点文件夹,然后一切又恢复了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.