简体   繁体   English

Tensorflow - 深度到空间后与 conv2d 不兼容的形状

[英]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.

相关问题 TensorFlow conv2d数据集形状不被接受 - TensorFlow conv2d dataset shape not accepted Tensorflow Conv2D 层 input_shape 配置错误:ValueError: Input 0 of layer "sequential" is in compatible with the layer: - Tensorflow Conv2D layers input_shape configuration error: ValueError: Input 0 of layer "sequential" is incompatible with the layer: Tensorflow/Keras Conv2D 层中的输入形状错误 - Input shape error in Tensorflow/Keras Conv2D layer 在张量流中使用conv2d - Using conv2d in tensorflow “conv2d”层的输入 0 与输入形状的预期轴 -1 层不兼容,其值为 3 - Input 0 of layer "conv2d" is incompatible with the layer expected axis -1 of input shape to have value 3 与输入形状维度相关的 MRI 分割错误(层 conv2d 的输入 0 与该层不兼容) - MRI Segmentation Error related to input shape dimension (Input 0 of layer conv2d is incompatible with the layer) Keras Conv2D 输入形状 - Keras Conv2D input Shape 多类文本分类中的 Conv2d 形状 - shape of Conv2d in multiclass text classification TensorFlow CNN tf.nn.conv2d ValueError:输出深度不是组数的倍数(操作:'Conv2D') - TensorFlow CNN tf.nn.conv2d ValueError: Depth of output is not a multiple of the number of groups for (op: 'Conv2D') TensorFlow conv2d未收敛到预期结果 - TensorFlow conv2d not converging to expected result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM