riskiem
riskiem

Reputation: 307

CNN Issues with Max Pooling

I have a dataset of 40X40X7 images. I have the following model architecture and implementation.

def conv_block(inputs, n_filter, regularizer, common_args):
    x = tf.keras.layers.Conv2D(filters=n_filter,
                               kernel_size=3,
                               strides=1,
                               padding='same',
                               activation='relu',
                               kernel_regularizer=regularizer,
                               **common_args)(inputs)  
    x = tf.keras.layers.Conv2D(filters=n_filter,
                               kernel_size=3,
                               strides=1,
                               padding='same',
                               activation='relu',
                               kernel_regularizer=regularizer,
                               **common_args)(inputs)  
    x = tf.keras.layers.MaxPooling2D((2, 2))(x)
    return x

def dense_block(inputs, n_filter, regularizer, drop_rate, common_args):
    x = tf.keras.layers.Dense(32 * n_filter,
                              activation='relu',
                              kernel_regularizer=regularizer,
                              **common_args)(inputs)
    x = tf.keras.layers.Dropout(drop_rate)(x)
    x = tf.keras.layers.Dense(32 * n_filter,
                              activation='relu',
                              kernel_regularizer=regularizer,
                              **common_args)(inputs)
    x = tf.keras.layers.Dropout(drop_rate)(x)
    x = tf.keras.layers.Dense(8 * n_filter, activation='relu',
                              kernel_regularizer=regularizer,
                              **common_args)(x)
    x = tf.keras.layers.Dropout(drop_rate)(x)
    return x

def make_level_model(img_size, n_bands, l2, nf, dr, seed):
    regularizer = tf.keras.regularizers.l2(l2)
    initializer = tf.keras.initializers.glorot_normal(seed=seed)
    common_args = {"kernel_initializer": initializer}

    inputs = tf.keras.layers.Input(shape=(img_size, img_size, n_bands))
    x = conv_block(inputs, nf, regularizer, common_args)
    x = conv_block(x, nf * 2, regularizer, common_args)
    x = conv_block(x, nf * 4, regularizer, common_args)

    x = tf.keras.layers.Flatten()(x)
    x = dense_block(x, nf, regularizer, dr, common_args)
    output = tf.keras.layers.Dense(1, **common_args)(x)

    model = tf.keras.Model(inputs=inputs, outputs=output)
    return model

# Define the hyperparameters
l2 = 1e-5
lr = 1e-4
bs = 64
ds = 200
nf = 32
dr = 0.5
img_size = 40
n_bands = 7
epochs = 200
with_feature = False
seed = 42

# Define the learning rate schedule
lr_schedule = tf.keras.optimizers.schedules.InverseTimeDecay(
    lr,
    decay_steps=train_count*bs,
    decay_rate=1,
    staircase=True)

# Define the optimizer
optimizer = tf.keras.optimizers.Adam(lr_schedule)

# Create the model
model = make_level_model(img_size, n_bands, l2, nf, dr,seed)
model.compile(optimizer=optimizer, loss="mean_squared_error", metrics=[RSquare()])

# Train the model
history = model.fit(
    train_dataset.batch(bs),
    epochs=epochs,
    validation_data=val_dataset.batch(bs),
    callbacks=[
        tf.keras.callbacks.ModelCheckpoint(
            filepath='Ageb200040by40Patch/weights.best.model_3Kernel_2000target120002conv2dense2.hdf5',
            monitor='val_r_square', mode='max', save_weights_only=True, save_best_only=True),
        tf.keras.callbacks.EarlyStopping(monitor='val_r_square', min_delta=0, patience=epochs // 2,
                                         mode='max')
    ]
)

I keep getting the following error:

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_8133/4129889730.py in <cell line: 25>() 23 24 # Create the model ---> 25 model = make_level_model(img_size, n_bands, l2, nf, dr,seed) 26 model.compile(optimizer=optimizer, loss="mean_squared_error", metrics=[RSquare()]) 27

/tmp/ipykernel_8133/1120897256.py in make_level_model(img_size, n_bands, l2, nf, dr, seed) 7 x = conv_block(inputs, nf, regularizer, common_args) 8 x = conv_block(x, nf * 2, regularizer, common_args) ----> 9 x = conv_block(x, nf * 4, regularizer, common_args) 10 11 x = tf.keras.layers.Flatten()(x)

/tmp/ipykernel_8133/1783471186.py in conv_block(inputs, n_filter, regularizer, common_args) 14 kernel_regularizer=regularizer, 15 **common_args)(inputs)
---> 16 x = tf.keras.layers.MaxPooling2D((2, 2))(x) 17 return x

~/anaconda3/envs/tensorflow2_p310/lib/python3.10/site-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

~/anaconda3/envs/tensorflow2_p310/lib/python3.10/site-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def, extract_traceback) 1965 except errors.InvalidArgumentError as e: 1966 # Convert to ValueError for backwards compatibility. -> 1967 raise ValueError(e.message) 1968 1969 # Record the current Python stack trace as the creating stacktrace of this

ValueError: Exception encountered when calling layer "max_pooling2d_29" (type MaxPooling2D).

Negative dimension size caused by subtracting 2 from 1 for '{{node max_pooling2d_29/MaxPool}} = MaxPoolT=DT_FLOAT, data_format="NCHW", explicit_paddings=[], ksize=[1, 1, 2, 2], padding="VALID", strides=[1, 1, 2, 2]' with input shapes: [?,128,10,1].

Call arguments received by layer "max_pooling2d_29" (type MaxPooling2D): • inputs=tf.Tensor(shape=(None, 128, 10, 1), dtype=float32)

I am not sure what is the issue. I think I have a big enough size for the images at 40X40X7.

Upvotes: 0

Views: 32

Answers (1)

Crysers
Crysers

Reputation: 535

Not sure if this solves your problem, but it looks like that your second convolution is operating once more on inputs instead of x (on which it should I assume). The same is true for your second dense layer.

Make sure, the data_format parameter of your MaxPool2D is set correct and your input dataframe train_dataset is also truely 40X40X7 and you did not mix any channels.

Upvotes: 0

Related Questions