#!/usr/bin/env python # coding: utf-8 # In[ ]: # ## CNN classifier for the planesnet data # In[ ]: ## import packages import numpy as np # linear algebra import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv) import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.preprocessing import MinMaxScaler from keras.models import Sequential, Model from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Activation, Dropout, Flatten from tensorflow.keras.layers import BatchNormalization from tensorflow.keras.optimizers import Adam from keras.callbacks import LearningRateScheduler from keras.regularizers import l2, l1 import math # In[ ]: ## Mount drive folder from google.colab import drive drive.mount('/content/drive') # In[ ]: ## Read data in json format data = pd.read_json('/content/drive/MyDrive/CommonFiles/MUSA650-Data/planesnet.json') # In[ ]: ## View the dataframe data.head() # In[ ]: ## Read image data into a 2D matrix X = np.array(data.data.tolist()) X.shape # In[ ]: ## Reshape the image data to 20x20 image patches with 3 channels (RGB) X = X.reshape(32000, 3, 20, 20) X.shape # In[ ]: ## Standard format for RGB is "the channels at the end"; move the axis for channels to the end X = np.moveaxis(X, 1, 3) X.shape # In[ ]: ## View few images tmpI = X[46,:,:,:] plt.imshow(tmpI) plt.show() # In[ ]: ## Read the labels y = np.array(data['labels']) y.shape # In[ ]: ## Split the data into training ans testing sets X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size = 0.20) ## Scale the data scalar = MinMaxScaler() scalar.fit(X_tr.reshape(X_tr.shape[0], -1)) X_tr = scalar.transform(X_tr.reshape(X_tr.shape[0], -1)).reshape(X_tr.shape) X_te = scalar.transform(X_te.reshape(X_te.shape[0], -1)).reshape(X_te.shape) # In[ ]: ## Show data size print(X_tr.shape) print(y_tr.shape) print(X_te.shape) print(y_te.shape) # In[ ]: ## Get the shape of a single sample dshape = X_tr.shape[1:] dshape # In[ ]: ## Construct the model model = Sequential() model.add(Conv2D(32, kernel_size=(3, 3), strides=(1,1), input_shape=dshape)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(MaxPooling2D((2,2))) model.add(Conv2D(32, kernel_size=(3, 3), strides=(1,1))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(MaxPooling2D((2,2))) model.add(Flatten()) model.add(Dense(64, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1, activation='sigmoid')) print(model.summary()) # In[ ]: ## Compile the model OPT = Adam(learning_rate=0.0001) NUM_EPOCH = 15 B_SIZE = 64 model.compile(loss='binary_crossentropy', optimizer=OPT, metrics=['accuracy']) # In[ ]: ## Train the model mdl_tr = model.fit(X_tr, y_tr, batch_size=B_SIZE, epochs=NUM_EPOCH, shuffle=True, validation_data=(X_te, y_te)) # In[ ]: ## Plot the learning stats fig, ax = plt.subplots(1,2, figsize=[18,6]) ax[0].plot(range(1, NUM_EPOCH+1), mdl_tr.history['loss'], c='blue', label='Training loss') ax[0].plot(range(1, NUM_EPOCH+1), mdl_tr.history['val_loss'], c='red', label='Validation loss') ax[0].legend() ax[0].set_xlabel('epochs') ax[1].plot(range(1, NUM_EPOCH+1), mdl_tr.history['accuracy'], c='blue', label='Training accuracy') ax[1].plot(range(1, NUM_EPOCH+1), mdl_tr.history['val_accuracy'], c='red', label='Validation accuracy') ax[1].legend() ax[1].set_xlabel('epochs')