By Samyak Jain
This packet aims at distinguishing between different categories of images. This is Image Classification in PYTHON using KERAS and CNN.
The Project code is available in notebook format as well as Python script.
The Dataset has been taken from Kaggle and is automatically downloaded inside the project. (The user needs to have kaggle Api setup in its computer. For more info, refer https://github.com/Kaggle/kaggle-api#:~:text=API%20credentials,file%20containing%20your%20API%20credentials.)
Link of the dataset for manual downloading - https://www.kaggle.com/puneet6060/intel-image-classification
We will perform model training in 3 steps:
1. Downloading Data from the net.
2. Extracting data into a useful form.
3. Preprocessing the data so performance increases.
4. Defining the model for training.
5. Training the model with the data.
6. Evaluating success.
This is the main API that is responsible for downloading the data from the net onto the local machine. It downloads the data in a zip format.
!kaggle datasets download -d puneet6060/intel-image-classification
To extract the data into the same location -
path = os.path.join(folder_path, "intel-image-classification.zip") ziap = zipfile.ZipFile(path) ziap.extractall(folder_path)
where folder_path is the location of the folder.
Creating ImageDataGenerator for feeding images directly from directory into the model on the fly -
datagen = ImageDataGenerator( validation_split = 0.2, rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
test_datagen = ImageDataGenerator( rescale = 1./255) train_generator = datagen.flow_from_directory( train_path, subset ='training', target_size = (100, 100), batch_size = 128, class_mode = 'categorical') val_generator = datagen.flow_from_directory( train_path, subset ='validation', target_size=(100, 100), batch_size= 128, class_mode='categorical') test_generator = test_datagen.flow_from_directory( test_path, target_size=(100, 100), batch_size= 64, class_mode='categorical')
No Preprocessing is needed as it is done while defining ImageDataGenerators.
Defining Model includes 2 Steps-
1. Creating the Model.
2. Compiling the model with appropriate loss and optimizer.
CREATING THE MODEL -
model = keras.Sequential([ layers.Conv2D(32, (5,5), activation = 'relu', input_shape = (100, 100, 3)), layers.MaxPooling2D(2), layers.Conv2D(64, (3,3), activation = 'relu'), layers.MaxPooling2D(2), layers.Conv2D(128, (3,3), activation = 'relu'), layers.Conv2D(128, (3,3), activation = 'relu'), layers.MaxPooling2D(2), layers.Conv2D(256, (3,3), activation = 'relu'), layers.Conv2D(256, (3,3), activation = 'relu', padding = "SAME"), layers.Flatten(), layers.Dense(64, activation = 'relu'), layers.Dropout(0.4), layers.Dense(128, activation = 'relu'), layers.Dropout(0.4), layers.Dense(6, activation = 'softmax') ])
The end layer has 6 outputs leading to 6 categories.
COMPILING THE MODEL -
model.compile(loss = 'categorical_crossentropy', optimizer = keras.optimizers.Adam(), metrics = ['acc'])
The model is compiled using RMSProp Optimizer and Categorical Cross-Entropy Loss.
history = model.fit(train_generator, epochs = 80, steps_per_epoch= 60, validation_data = val_generator, verbose = 1)
The model is trained for 80 epochs and validated using the validationDataGenerator.
history2 = model.evaluate(test_generator)
After training the model has -
Training Accuray => 93.0%
Training Loss => 0.522
Validation Accuray => 84.0%
Validation Loss => 0.209
Test Accuray => 84.2%
Test Loss => 0.5360
Submitted by Samyak Jain (samyak1230)
Download packets of source code on Coders Packet
Comments