3
votes

I know how CNNs work, including the purpose of each layer(Dropout, Pooling etc). However, when designing a CNN for a new dataset, I have no idea how many Conv-Relu-Pool layers to use, how many Dense layers I'm supposed to use before finally getting my output or how many kernels to use in each Convolutional Layer. I know that all of this stuff is somewhat experimental and a sure shot can't be given to design the CNN, but is there any thumb rule that I can keep in mind while doing so? Also, is there a paper where I can get answers to these questions?

I have tried Googling all these issues, the answers have always ended up confusing me even more.

Thank you in advance.

1
I have the same feeling as youdasdasd

1 Answers

0
votes

Best thing for you to do is to use the Models, which are already proved to be efficient, which we call, Pre-Trained Models.

Some of such Pre-Trained CNN Models, are MobileNet (tf.keras.applications.MobileNetV2), VGGNET (tf.keras.applications.vgg19, tf.keras.applications.vgg16), ResNet (tf.keras.applications.resnet50), etc..

ImageNet is a huge Dataset with Millions of Records and Thousand Classes and the above mentioned Models performed very well on that Data, with more than 90% Accuracy.

All you have to do is to reuse those Models using Transfer Learning and fit it to your Data, by replacing the Output Layer or Couple of Dense Layers of the Pre-Trained Model with Output Layer specific to your Data.

Complete Code for Utilizing MobileNetV2 Model for Flower Dataset is shown below:

import tensorflow as tf
import datetime
import numpy as np
import os

tf.__version__ #'2.1.0'

URL = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"

zip_file = tf.keras.utils.get_file(origin= URL, 
                                   fname="flower_photos.tgz", 
                                   extract=True)

base_dir = os.path.join(os.path.dirname(zip_file), 'flower_photos')


# Create a DataGenerator
from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(rescale=1. / 255, validation_split=0.2,
                             featurewise_center=True,
                             featurewise_std_normalization=True,
                             rotation_range=20,
                             width_shift_range=0.2,
                             height_shift_range=0.2,
                             horizontal_flip=True)

train_generator = datagen.flow_from_directory(
        base_dir,
        target_size=(224, 224),
        batch_size=32,
        subset = 'training',
        class_mode='categorical')

validation_generator = datagen.flow_from_directory(
        base_dir,
        target_size=(224, 224),
        batch_size=32,
        subset = 'validation',
        class_mode='categorical')

#Functional API
#Import MobileNet V2 with pre-trained weights AND exclude fully connected layers
IMG_SIZE = 224

from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras import Model


IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)

# Create the base model from the pre-trained model MobileNet V2
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                               include_top=False,
                                               weights='imagenet')

# Add Global Average Pooling Layer
x = base_model.output
x = GlobalAveragePooling2D()(x)

# Add a Output Layer
my_mobilenetv2_output = Dense(5, activation='softmax')(x)

# Combine whole Neural Network
my_mobilenetv2_model = Model(inputs=base_model.input, outputs=my_mobilenetv2_output)

my_mobilenetv2_model.compile(loss='categorical_crossentropy',
              optimizer= tf.keras.optimizers.RMSprop(lr=0.0001),
              metrics=['accuracy'])

my_mobilenetv2_model.summary()