I am using a Convolutional Neural Network (CNN) for image detection of 30 different kinds of fruits. The dataset I have currently consists of "train" and "test" folders, each of them having 30 sub directories for the 30 different classes.
"train" folder has in total 671 jpg files while "test" folder has in total 300 jpg files.
The Python code that I have written to achieve image detection is as follows-
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
import matplotlib.pyplot as plt
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, Dropout
from keras.layers.convolutional import MaxPooling2D
from keras import backend as K
from sklearn.metrics import accuracy_score, precision_score, recall_score
# Read in images from 'train' folder-
train_datagen = ImageDataGenerator(
rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True
)
train_generator = train_datagen.flow_from_directory(
directory=r"./train/", target_size=(420, 420), color_mode="rgb",
batch_size=30, class_mode="categorical", shuffle=True
)
# O/P-
# Found 671 images belonging to 30 classes.
# Read in images from 'test' folder-
test_datagen = ImageDataGenerator(
rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True
)
valid_generator = test_datagen.flow_from_directory(
directory=r"./test/", target_size=(420, 420), color_mode="rgb", batch_size=30,
class_mode="categorical", shuffle=True
)
# O/P-
# Found 300 images belonging to 30 classes.
# Dimensions of our image(s)-
img_width, img_height = 420, 420
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
print("\ninput_shape = {0}\n\n".format(input_shape))
# input_shape = (420, 420, 3)
# Build the CNN-
model = Sequential()
# model.add(Conv2D(32, (5, 5), input_shape = (32, 32, 3), activation = 'relu'))
model.add(Conv2D(32, (3, 3), input_shape = input_shape, activation = 'relu'))
# model.add(Conv2D(32, (3, 3), activation = 'relu'))
# model.add(Dense(40, activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.2))
# model.add(Conv2D(64, (3, 3), activation = 'relu'))
model.add(Conv2D(64, (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
# model.add(Dense(512, activation = 'relu'))
model.add(Dense(128, activation = 'relu'))
model.add(Dense(30, activation = 'softmax'))
# Compiling the model-
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics = ['accuracy'])
STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
STEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size
model.fit_generator(generator=train_generator, steps_per_epoch=STEP_SIZE_TRAIN, epochs=5)
When I attempt to execute this code, I get the following message-
Using TensorFlow backend. Found 671 images belonging to 30 classes. Found 300 images belonging to 30 classes.
input_shape = (420, 420, 3)
Epoch 1/5 2019-02-12 14:48:18.088495: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 2019-02-12 14:48:23.270184: W tensorflow/core/framework/allocator.cc:122] Allocation of 670940160 exceeds 10% of system memory. 2019-02-12 14:48:31.747262: W tensorflow/core/framework/allocator.cc:122] Allocation of 670940160 exceeds 10% of system memory.
After this, my system hangs and I have to restart the system. This has happened already 4 times. My system has Intel Core i5 @ 2.2 GHz with 8 GB RAM.
What's going wrong?