0

I'm building a neural network to classify images that have an email address written on them. The positive folder contains images with email addresses written on top of the picture, in different fonts, colors, sizes and positions.

The negative folder contains images without text on top and also images with text on top that doesn't have the format of an email address (no @ sign).

The pictures are 300 x 225 x 3 (rgb).

It should be a simple a simple classification task (the NN should be able to pick up that when there's an @, the image has an email) but my model isn't performing well. It's stuck at 83% test accuracy after 25 epochs. Also, it's taking 10 hours to train, which sounds excessive to me.

Can you help me to analyse the structure of my CNN and suggest improvements (or help me avoid pitfalls)?

The model I wrote is this:

from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.preprocessing.image import ImageDataGenerator


input_size = (64, 48)


# Initialising the CNN
classifier = Sequential()

# Step 1 - Convolution
classifier.add(Conv2D(32, (3, 3), input_shape = (*input_size, 3), activation = 'relu'))

# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Step 3 - Flattening
classifier.add(Flatten())

# Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))

# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# Part 2 - Fitting the CNN to the images


train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('./training_Set',
                                                 target_size = input_size,
                                                 batch_size = 32,
                                                 class_mode = 'binary')

test_set = test_datagen.flow_from_directory('./test_set',
                                            target_size = input_size,
                                            batch_size = 32,
                                            class_mode = 'binary')

classifier.fit_generator(training_set,
                         steps_per_epoch = 8000,
                         epochs = 25,
                         validation_data = test_set,
                         validation_steps = 2000)
  • Do you have any idea of the size of the @ in pixels?? Intuition tells me that if you add a Conv layer with kernel size about the same size of the @ you'll get more success, the amount of filters for that layer is a mistery though. If you happen to initialize those filters in the format of an @ it would be even better. (Yep, that sounds like cheating, but who cares?) -- A possible strategy is to create very little images only with the @ or another symbol and create a little model just to identify the @. Then you append the convolutional part of this little model to your current model. – Daniel Möller Jul 04 '17 at 13:10
  • the size of the @ sign varies from picture to picture. – Javier Ventajas Hernández Jul 04 '17 at 13:14
  • You mention that test accuracy is stuck at 83%. What are corresponding train loss and accuracy? – Sergii Gryshkevych Jul 04 '17 at 13:28
  • train loss: 0.35, train accuracy: 0.85, validation loss: 0.42, validation accuracy: 0.83. Do you have an idea of why it's taking so long to train? – Javier Ventajas Hernández Jul 04 '17 at 14:23
  • Training time depends on how big is the dataset, as time is linear in the dataset size. To improve the model I would add more layers, add more parameters. – Dr. Snoopy Jul 04 '17 at 23:39

0 Answers0