Home > OS >  Keras - Dense and Conv2D values
Keras - Dense and Conv2D values

Time:04-30

Images that I would like to use to train the network are about the size of 4000px*3000px and about 40k of them, sorted in 250 classes. I have made a CNN shown below:

model = keras.Sequential([
    layers.Input((imgHeight, imgWidth, 1)),
    layers.Conv2D(16, 3, padding = 'same'), # filters, kernel_size
    layers.Conv2D(32, 3, padding = 'same'),
    layers.MaxPooling2D(),
    layers.Flatten(),
    layers.Dense(250),
])

How do I figure out what layers.Conv2D(*16*, ...), a value I need? How do I figure out what layers.Dense(*250*), a value I need?

Because I can't start the training process, I'm running out of memory.

CodePudding user response:

The output shape of the Flatten() layer is 96 Million, and so the final dense layer of your model has 24 Billion parameters, this is why you are running out of memory. There are some steps you can take to fix this issue:
Try resizing your images to a smaller shape, if 4000x3000x1 isn't necessary, 160x160x1 would be a good choice.
Try using more Conv2D layers followed by a MaxPool2D layer to decrease the size of the input, and then finally at the end, use a Flatten layer followed by a Dense layer.
For example:

model = keras.Sequential([
    layers.Input((160, 160, 1)),
    layers.Conv2D(32, 3, padding = 'same'),
    layers.Conv2D(32, 3, padding = 'same'),
    layers.MaxPooling2D((2,2)),

    layers.Conv2D(64, 3, padding = 'same'),
    layers.Conv2D(64, 3, padding = 'same'),
    layers.MaxPooling2D((2,2)),

    layers.Conv2D(128, 3, padding = 'same'),
    layers.Conv2D(128, 3, padding = 'same'),
    layers.MaxPooling2D((2,2)),

    layers.Conv2D(256, 3, padding = 'same'),
    layers.Conv2D(256, 3, padding = 'same'),
    layers.MaxPooling2D((2,2)),

    layers.Flatten(),

    layers.Dense(512),
    layers.Dense(250),
])

This type of architecture will work well if you are doing a classification task, and will not run out of memory.

  • Related