0

i designed below architecture by Keras(actually my architecture is like 3D_ResNet18 model):

def relu_bn(inputs: Tensor) -> Tensor:
    bn = BatchNormalization()(inputs)
    relu = ReLU()(bn)
    return relu
def residual_block(x: Tensor, downsample: bool, filters: int ) -> Tensor:   #def residual_block(x: Tensor, downsample: bool, filters: int,  kernel_size: int = 3) -> Tensor:
    conv = Conv3D(filters, kernel_size=(3,3,3),
                  strides=(1,1,1), 
                  #padding="same",                   #"same"
                  kernel_initializer="he_normal"     #,kernel_regularizer=kernel_regularizer
                  )(x)
    
    conv = relu_bn(conv)

    conv = Conv3D(filters, kernel_size=(3,3,3),
                  strides=(1,1,1),
                  #padding="same",                   #"same"
                  kernel_initializer="he_normal"     #,kernel_regularizer=kernel_regularizer
                  )(conv)
    
    conv = BatchNormalization()(conv)

    if downsample:
        x = Conv3D(filters, kernel_size=(1,1,1),
                  strides=(2,2,2),
                  #padding="same",                   #"same"
                  kernel_initializer="he_normal"     #,kernel_regularizer=kernel_regularizer
                  )(x)
        x = BatchNormalization()(x)

    out = Add()([x, conv])
    out = ReLU()(out)
    return out


def ResNet_3D_model(input_shape):

    inputs = Input(shape=input_shape)
    num_filters = 64
    
    t = BatchNormalization()(inputs)

    stem_out = Conv3D(filters=64,
                      kernel_size=(3,7,7),
                      strides=(1,2,2), 
                      padding=(1,3,3),
                      kernel_initializer="he_normal"            #,kernel_regularizer=kernel_regularizer
                      )(t)
    t = relu_bn(stem_out)

    num_blocks_list = [2, 2, 2, 2]
    for i in range(len(num_blocks_list)):
        num_blocks = num_blocks_list[i]
        for j in range(num_blocks):
            t = residual_block(t, downsample=(j==0 and i!=0), filters=num_filters)          # t = residual_block(t, downsample=(j==0 and i!=0), filters=num_filters)
        num_filters *= 2
    
    #Out_block
    t = AveragePooling3D(pool_size=(1, 1, 1))(t)
    t = Dropout(0.5)(t)
    t = Flatten()(t)
    outputs = Dense(3, activation='softmax')(t)
    
    model = Model(inputs, outputs)

    return model

so, i pass shape of inputs to architecture:

m0 = ResNet_3D_model((155, 240, 240, 1))
m0.summary()

but, when i pass shape of inputs to architecture, I get the following error:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-40-07fc82fac381> in <module>
      1 #  (batch_size, height, width, depth)
----> 2 m0 = ResNet_3D_model((155, 240, 240, 1))
      3 m0.summary()

2 frames
<ipython-input-39-79d868dde880> in ResNet_3D_model(input_shape)
      6     t = BatchNormalization()(inputs)
      7 
----> 8     stem_out = Conv3D(filters=64,
      9                       kernel_size=(3,7,7),
     10                       strides=(1,2,2),

/usr/local/lib/python3.9/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     68             # To get the full stack trace, call:
     69             # `tf.debugging.disable_traceback_filtering()`
---> 70             raise e.with_traceback(filtered_tb) from None
     71         finally:
     72             del filtered_tb

/usr/local/lib/python3.9/dist-packages/keras/utils/conv_utils.py in conv_output_length(input_length, filter_size, padding, stride, dilation)
    131     if input_length is None:
    132         return None
--> 133     assert padding in {"same", "valid", "full", "causal"}
    134     dilated_filter_size = filter_size + (filter_size - 1) * (dilation - 1)
    135     if padding in ["same", "causal"]:

AssertionError: 

Do you know where the problem is?

I applied inputs with different dimensions to the model, but the problem did not go away.

1 Answers1

0

I am not too experienced with ResNet, but the traceback pretty much tells you where to find your error

-> assert padding in {"same", "valid", "full", "causal"}

In your function ResNet_3D_model() you appear to supply a tuple of integers to the padding argument of the Conv3D Layer constructor:
padding=(1,3,3),

However, according to the docs it should accept either "valid" or "same"

def ResNet_3D_model(input_shape):

    inputs = Input(shape=input_shape)
    num_filters = 64
    
    t = BatchNormalization()(inputs)

    stem_out = Conv3D(filters=64,
                      kernel_size=(3,7,7),
                      strides=(1,2,2), 
                      def ResNet_3D_model(input_shape):

    inputs = Input(shape=input_shape)
    num_filters = 64
    
    t = BatchNormalization()(inputs)

    stem_out = Conv3D(filters=64,
                      kernel_size=(3,7,7),
                      strides=(1,2,2), 
                      padding=(1,3,3),
# ...
Björn
  • 1,610
  • 2
  • 17
  • 37