saucypanda
saucypanda

Reputation: 37

Attribute error while creating CNN model in Keras

The function used to build the model:

def CancerModel(input_shape):   

    X_input = Input(input_shape)

    X = ZeroPadding2D((2, 2))(X_input)

    X = Conv2D(8, (5, 5), strides = (1, 1), name = 'conv')(X)
    X = BatchNormalization(axis = 3, name = 'bn1')(X)
    X = Activation('relu')(X)

    X = MaxPooling2D((2, 2), name='max_pool')(X)

    X = Conv2D(16, (5, 5), strides = (1, 1), name = 'conv2')(X)
    X = BatchNormalization(axis = 3, name = 'bn2')(X)
    X = Activation('relu')(X)

    X = MaxPooling2D((2, 2), name='max_pool2')(X)

    X = Flatten()(X)
    X = Dense(120, activation='relu', name='fc1')(X)
    X = Dense(84, activation='relu', name='fc2')(X)
    X = Dense(7, activation='softmax', name='output')(X)

    model = Model(inputs = X_input, outputs = output, name='CancerModel')

    return model

The model was attempted to be created using:

cancerModel = CancerModel(X_train.shape[1:])

However, I am getting an error saying that the attribute can't be set. I have also attached a screenshot of the error I'm getting. Any help would be appreciated.

Screenshot of bug

Upvotes: 0

Views: 96

Answers (1)

ashraful16
ashraful16

Reputation: 2782

I found no error in your code. It may be problem in your training data shape or Keras version issue (My Keras version 2.4.3).

def CancerModel(input_shape):   

    X_input = Input(input_shape)

    X = ZeroPadding2D((2, 2))(X_input)

    X = Conv2D(8, (5, 5), strides = (1, 1), name = 'conv')(X)
    X = BatchNormalization(axis = 3, name = 'bn1')(X)
    X = Activation('relu')(X)

    X = MaxPooling2D((2, 2), name='max_pool')(X)

    X = Conv2D(16, (5, 5), strides = (1, 1), name = 'conv2')(X)
    X = BatchNormalization(axis = 3, name = 'bn2')(X)
    X = Activation('relu')(X)

    X = MaxPooling2D((2, 2), name='max_pool2')(X)

    X = Flatten()(X)
    X = Dense(120, activation='relu', name='fc1')(X)
    X = Dense(84, activation='relu', name='fc2')(X)
    X = Dense(7, activation='softmax', name='output')(X)

    model = Model(inputs = X_input, outputs = X, name='CancerModel')

    return model


CancerModel((224,224,3)).summary() #It works fine

Also works fine using

CancerModel(np.ones((5,120,120,3)).shape[1:]).summary()

Upvotes: 1

Related Questions