【问题标题】:('Input has undefined rank:', TensorShape(None)) <- Error in Building ResNet('输入具有未定义的等级:',TensorShape(None)) <- 构建 ResNet 时出错
【发布时间】:2021-09-07 09:05:56
【问题描述】:

我在构建 ResNet 时遇到未定义的排名错误;我在下面提到了一个可重现代码:

这是我的身份块:

def IdentityBlock(X, f, filters):
    
    F1, F2, F3 = filters
    X_shortcut = X
    
    X = Conv2D(filters = F1, kernel_size = (3, 3), padding = 'valid')(X)
    X = BatchNormalization()(X)
    X = Activation('relu')(X)
    
    X = Conv2D(filters = F2, kernel_size = (f, f), padding = 'same')(X)
    X = BatchNormalization()(X)
    X = Activation('relu')(X)
    
    X = Conv2D(filters = F3, kernel_size = (3, 3), padding = 'same')(X)
    X = BatchNormalization()(X)
    
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    
    return X

这是我的卷积块

def ConvBlock(X, f, filters):
    
    F1, F2, F3 = filters
    X_shortcut = X
    
    X = Conv2D(filters = F1, kernel_size = (3, 3), padding = 'valid')(X)
    X = BatchNormalization()(X)
    X = Activation('relu')(X)
    
    X = Conv2D(filters = F2, kernel_size = (f, f), padding = 'same')(X)
    X = BatchNormalization()(X)
    X = Activation('relu')(X)
    
    X = Conv2D(filters = F3, kernel_size = (3, 3), padding = 'same')(X)
    X = BatchNormalization()(X)
    
    X_shortcut = Conv2D(filters = F3, kernel_size = (3, 3), padding = 'same')
    X_shortcut = BatchNormalization()(X_shortcut)
    
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    
    return X

还有我的 resnet 模型:

def ResNet(input_shape = (224, 224, 3)):
    X_input = Input(input_shape)
    
    X = Conv2D(64, (7, 7))(X_input)
    X = BatchNormalization()(X)
    X = Activation('relu')(X)
    X = MaxPooling2D((3, 3))(X)
    
    X = ConvBlock(X, f = 3, filters = [64, 64, 128])
    X = IdentityBlock(X, 3, filters = [64, 64, 128])
    X = IdentityBlock(X, 3, filters = [64, 64, 128])
    
    X = ConvBlock(X, f = 3, filters = [128, 128, 512])
    X = IdentityBlock(X, 3, filters = [128, 128, 512])
    X = IdentityBlock(X, 3, filters = [128, 128, 512])
    X = IdentityBlock(X, 3, filters = [128, 128, 512])
    
    X = MaxPooling2D((2, 2))(X)
    model = Model(input = X_input, output = X)
    
    return model

当我这样调用 RenNet 时:

base_model = ResNet50(input_shape=(224, 224, 3))

我收到以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-22-f81766b0bb4e> in <module>
----> 1 base_model = ResNet50(input_shape=(224, 224, 3))

<ipython-input-21-309ae6f634f4> in ResNet50(input_shape)
      7     X = MaxPooling2D((3, 3))(X)
      8 
----> 9     X = ConvBlock(X, f = 3, filters = [64, 64, 128])
     10     X = IdentityBlock(X, 3, filters = [64, 64, 128])
     11     X = IdentityBlock(X, 3, filters = [64, 64, 128])

<ipython-input-20-aeab857c5df6> in ConvBlock(X, f, filters)
     16 
     17     X_shortcut = Conv2D(filters = F3, kernel_size = (3, 3), padding = 'same')
---> 18     X_shortcut = BatchNormalization()(X_shortcut)
     19 
     20     X = Add()([X, X_shortcut])
.
.
.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/normalization.py in build(self, input_shape)
    296     input_shape = tensor_shape.TensorShape(input_shape)
    297     if not input_shape.ndims:
--> 298       raise ValueError('Input has undefined rank:', input_shape)
    299     ndims = len(input_shape)
    300 

ValueError: ('Input has undefined rank:', TensorShape(None))

【问题讨论】:

  • 那个ResNet块名其实是ResNet50,我不小心改了。

标签: tensorflow machine-learning deep-learning


【解决方案1】:

您将Conv2D 操作传递给BatchNormalization 层而不是张量。尝试改变

X_shortcut = Conv2D(filters = F3, kernel_size = (3, 3), padding = 'same')

X_shortcut = Conv2D(filters = F3, kernel_size = (3, 3), padding = 'same')(X_shortcut)

【讨论】:

  • 我尝试了您的解决方案,但没有奏效。现在它向我显示了这个错误:TypeError: Inputs to a layer should be tensors. Got: &lt;tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7f93dad5b550&gt;
  • 该错误与之前的错误在同一行吗?
【解决方案2】:

这些是我为达到最终解决方案所遵循的步骤:

1 - add 函数的工作方式类似于 add([x,x_shortcut]) ,我不太确定其他方式。

2 - 对变量进行了更改,而不是在 IdentityBlock 函数中使用 X 我将其更改为 input_

3 - 添加层中输入的形状不是我们想要的,所以我 玩弄内核大小和步幅值(不太擅长这些) 使该层工作。为 ConvBlock 和 IdentityBlock 执行此操作。

4 - 最后一个小错误是在调用 keras 模型时,它的模型(输入 = ,输出 = ),输入和输出不起作用。

def IdentityBlock(input_, f, filters):
    
        F1, F2, F3 = filters

        X = Conv2D(filters = F1, kernel_size = (1, 1), padding = 'valid') (input_)
        X = BatchNormalization()(X)
        X = Activation('relu')(X)

        X = Conv2D(filters = F2, kernel_size = (f, f), padding = 'same')(X)
        X = BatchNormalization()(X)
        X = Activation('relu')(X)

        X = Conv2D(filters = F3, kernel_size = (1, 1), padding = 'same')(X)
        X = BatchNormalization()(X)

        X = add([X, input_])
        X = Activation('relu')(X)

        return X
def ConvBlock(X, f, filters):
    
        F1, F2, F3 = filters
        X_shortcut = X
    
        X = Conv2D(filters = F1, kernel_size = (1, 1),strides = (1,1), padding 
        = 'valid')(X)
        X = BatchNormalization()(X)
        X = Activation('relu')(X)
    
        X = Conv2D(filters = F2, kernel_size = (f, f), padding = 'same')(X)
        X = BatchNormalization()(X)
        X = Activation('relu')(X)
    
        X = Conv2D(filters = F3, kernel_size = (1, 1), padding = 'same')(X)
        X = BatchNormalization()(X)
    
        X_shortcut = Conv2D(filters = F3,padding = 'same' , kernel_size = (1,1) 
        , strides = (1,1))(X_shortcut)
        X_shortcut = BatchNormalization()(X_shortcut)
    
        X = add([X, X_shortcut])
        X = Activation('relu')(X)
    
        return X
def ResNet(input_shape = (224, 224, 3)):

  X_input = Input(input_shape)
  X = ZeroPadding2D((3,3))(X_input)

  X = Conv2D(64, (7, 7) , strides = (2,2))(X_input)
  X = BatchNormalization()(X)
  X = Activation('relu')(X)

  X = ZeroPadding2D((1,1))(X)
  X = MaxPooling2D((3, 3) , strides = (2,2))(X)

  X = ConvBlock(X, f = 3, filters = [64, 64, 128])
  X = IdentityBlock(X, 3, filters = [64, 64, 128])
  X = IdentityBlock(X, 3, filters = [64, 64, 128])

  X = ConvBlock(X, f = 3, filters = [128, 128, 512])
  X = IdentityBlock(X, 3, filters = [128, 128, 512])
  X = IdentityBlock(X, 3, filters = [128, 128, 512])
  X = IdentityBlock(X, 3, filters = [128, 128, 512])

  model = Model(inputs = X_input, outputs = X)

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 2013-01-01
    相关资源
    最近更新 更多