AttributeError:“顺序”对象没有属性“ shape”

时间:2020-05-28 01:29:56

标签: deep-learning conv-neural-network tensorflow2.0 resnet batch-normalization

enter image description here 我正在Google Colab的Tensorflow 2.0中从零开始尝试ResNet 50。请参考下面的代码。我收到一个错误:AttributeError:“顺序”对象没有属性“ shape”。过去我曾经使用conv2d实现VGG,但从未给我带来任何问题。所以我想Keras版本或TensorFlow版本都正确。

代码如下:

def conv_diff_size(X, filters):
    f1, f2, f3 = filters  
    X_shortcircuit = X
    X_shortcircuit = tf.keras.models.Sequential([
        tf.keras.layers.Conv2D(f1, (1, 1), padding='same')(X_shortcircuit),
        tf.keras.layers.BatchNormalization(axis=-1, momentum=0.9)
    ])

    X = tf.keras.models.Sequential([
        tf.keras.layers.Conv2D(f1, (1, 1), padding='same'),
        tf.keras.layers.BatchNormalization(axis=-1, momentum=0.9),
        tf.keras.layers.Activation('relu'),

        tf.keras.layers.Conv2D(f2, (3, 3), padding='valid'),
        tf.keras.layers.BatchNormalization(axis=-1, momentum=0.9),
        tf.keras.layers.Activation('relu'),

        tf.keras.layers.Conv2D(f3, (1, 1), padding='valid'),
        tf.keras.layers.BatchNormalization(axis=-1, momentum=0.9)   
    ])

    X = tf.math.add(X, X_shortcircuit)
    X = tf.keras.layers.Activation('relu')

    return X


    def ResNet50():
            X = tf.keras.layers.Conv2D(64, kernel_size = (7,7), strides=2, padding='valid', data_format='channels_last', input_shape = (50000, 32, 32, 3))
            X = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.9)(X)
            X = tf.keras.layers.MaxPool2D(pool_size=(3, 3), strides=2)(X)
            X = conv_diff_size(X, [64, 64, 256])(X)
            X = conv_same_size(X, [64, 64, 256])(X)
            X = conv_same_size(X, [64, 64, 256])(X)

            X = conv_diff_size(X, [128, 128, 512])(X)
            X = conv_same_size(X, [128, 128, 512])(X)
            X = conv_same_size(X, [128, 128, 512])(X)
            X = conv_same_size(X, [128, 128, 512])(X)

            X = conv_diff_size(X, [256, 256, 1024])(X)
            X = conv_same_size(X, [256, 256, 1024])(X)
            X = conv_same_size(X, [256, 256, 1024])(X)
            X = conv_same_size(X, [256, 256, 1024])(X)
            X = conv_same_size(X, [256, 256, 1024])(X)

            X = conv_diff_size(X, [512, 512, 2048])(X)
            X = conv_same_size(X, [512, 512, 2048])(X)
            X = conv_same_size(X, [512, 512, 2048])(X)
            X = conv_same_size(X, [512, 512, 2048])(X)
            X = conv_same_size(X, [512, 512, 2048])(X)
            X = conv_same_size(X, [512, 512, 2048](X))

            X = tf.keras.layers.Dense(1000, activation='relu')(X)
            X = tf.keras.layers.Dense(10, activation=relu)(X)
            return X

0 个答案:

没有答案