Answer To: Image Classification Build a CNN based image classifier, train and test the model using the...
Raj answered on Aug 06 2021
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import matplotlib.pyplot as plt\n",
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
"from keras.callbacks import ReduceLROnPlateau\n",
"import numpy as np\n",
"from sklearn.metrics import confusion_matrix"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 14034 images belonging to 6 classes.\n",
"Found 3000 images belonging to 6 classes.\n"
]
}
],
"source": [
"\n",
"#Preprocessing Images to convert them ImageDataGenerator\n",
"\n",
"train_DIR = \"seg_train/seg_train/\"\n",
"\n",
"train_datagen = ImageDataGenerator( rescale = 1.0/255,\n",
" width_shift_range=0.2,\n",
" height_shift_range=0.2,\n",
" zoom_range=0.2,\n",
" horizontal_flip=True,\n",
" fill_mode='nearest')\n",
"\n",
"\n",
"train_generator = train_datagen.flow_from_directory(train_DIR,\n",
" batch_size=32,\n",
" class_mode='categorical',\n",
" target_size=(150, 150))\n",
"\n",
"test_DIR = \"seg_test/seg_test/\"\n",
"test_datagen = ImageDataGenerator(rescale = 1.0/255)\n",
"\n",
"\n",
"test_generator = test_datagen.flow_from_directory(test_DIR,\n",
" batch_size=64,\n",
" class_mode='categorical',\n",
" target_size=(150, 150))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### CNN Model"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"#CNN model\n",
"\n",
"def evaluate(train_generator,test_generator):\n",
"\n",
" model = tf.keras.models.Sequential([\n",
" tf.keras.layers.Conv2D(8, (3,3), activation=tf.nn.relu,input_shape=(150, 150, 3)),\n",
" tf.keras.layers.MaxPooling2D(2,2),\n",
"\n",
" tf.keras.layers.Conv2D(16, (3,3), activation=tf.nn.relu),\n",
" tf.keras.layers.MaxPooling2D(2,2),\n",
"\n",
" tf.keras.layers.Conv2D(32, (3,3), activation=tf.nn.relu),\n",
" tf.keras.layers.MaxPooling2D(2,2),\n",
"\n",
" tf.keras.layers.Conv2D(64, (3,3), activation=tf.nn.relu,padding = 'Same'),\n",
" tf.keras.layers.MaxPooling2D(2,2),\n",
"\n",
" tf.keras.layers.Flatten(),\n",
"\n",
" tf.keras.layers.Dense(512, activation=tf.nn.relu),\n",
" #tf.keras.layers.Dropout(0.2),\n",
" tf.keras.layers.Dense(6, activation = tf.nn.softmax)\n",
" ])\n",
"\n",
" #model.summary()\n",
" learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',\n",
" patience=2,\n",
" verbose=1,\n",
" factor=0.5,\n",
" min_lr=0.000003)\n",
"\n",
" model.compile(loss = 'categorical_crossentropy', optimizer= tf.keras.optimizers.Adam(), metrics=['acc'])\n",
"\n",
" model.fit(train_generator,\n",
" epochs = 1,\n",
" verbose = 1,\n",
" validation_data = test_generator,\n",
" callbacks=[learning_rate_reduction])\n",
" Y_pred=model.predict_generator(test_generator)\n",
" y_pred=np.argmax(Y_pred,axis=1)\n",
" \n",
" mat=confusion_matrix(test_generator.classes, y_pred)\n",
" _,value_acc=model.evaluate(test_generator,verbose=1)\n",
" return value_acc,mat\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"439/439 [==============================] - 218s 497ms/step - loss: 1.0970 - acc: 0.5622 - val_loss: 0.8704 - val_acc: 0.6520\n",
"WARNING:tensorflow:From :39: Model.predict_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Please use Model.predict, which supports generators.\n",
"47/47 [==============================] - 6s 134ms/step - loss: 0.8704 - acc: 0.6520\n",
"\n",
"Accuracy of 0 65.20000100135803\n",
"\n",
"\n",
"439/439 [==============================] - 138s 315ms/step - loss: 1.0750 - acc: 0.5720 - val_loss: 0.7337 - val_acc: 0.7227\n",
"47/47 [==============================] - 6s 134ms/step - loss: 0.7337 - acc: 0.7227\n",
"\n",
"Accuracy of 1 72.26666808128357\n",
"\n",
"\n",
"439/439 [==============================] - 138s 314ms/step - loss: 1.1238 - acc: 0.5494 - val_loss: 0.8966 - val_acc: 0.6660\n",
"47/47 [==============================] - 6s 133ms/step - loss: 0.8966 - acc: 0.6660\n",
"\n",
"Accuracy of 2 66.60000085830688\n",
"\n",
"\n",
"439/439 [==============================] - 140s 318ms/step - loss: 1.1325 - acc: 0.5546 - val_loss: 0.7678 - val_acc: 0.7247\n",
"47/47 [==============================] - 6s 135ms/step - loss: 0.7678 - acc: 0.7247\n",
"\n",
"Accuracy of 3 72.46666550636292\n",
"\n",
"\n",
"439/439 [==============================] - 141s 321ms/step - loss: 1.0715 - acc: 0.5774 - val_loss: 0.9035 - val_acc: 0.6567\n",
"47/47 [==============================] - 6s 135ms/step - loss: 0.9035 - acc: 0.6567\n",
"\n",
"Accuracy of 4 65.66666960716248\n",
"\n",
"\n",
"439/439 [==============================] - 140s 319ms/step - loss: 1.0560 - acc: 0.5828 - val_loss: 0.8588 - val_acc: 0.6707\n",
"47/47 [==============================] - 6s 136ms/step - loss: 0.8588 - acc: 0.6707\n",
"\n",
"Accuracy of 5 67.06666946411133\n",
"\n",
"\n",
"439/439 [==============================] - 139s 317ms/step - loss: 1.0448 - acc: 0.5792 - val_loss: 0.7447 - val_acc: 0.7393\n",
"47/47 [==============================] - 6s 136ms/step - loss: 0.7447 - acc: 0.7393\n",
"\n",
"Accuracy of 6 73.93333315849304\n",
"\n",
"\n",
"439/439 [==============================] - 138s 315ms/step - loss: 1.0699 - acc: 0.5680 - val_loss: 0.9382 - val_acc: 0.6510\n",
"47/47 [==============================] - 6s 136ms/step - loss: 0.9382 - acc: 0.6510\n",
"\n",
"Accuracy of 7 65.10000228881836\n",
"\n",
"\n",
"439/439 [==============================] - 139s 317ms/step - loss: 1.0426 - acc: 0.5833 - val_loss: 0.8434 - val_acc: 0.6770\n",
"47/47 [==============================] - 6s 137ms/step - loss: 0.8434 - acc: 0.6770\n",
"\n",
"Accuracy of 8 67.69999861717224\n",
"\n",
"\n",
"439/439 [==============================] - 138s 314ms/step - loss: 1.1529 - acc: 0.5405 - val_loss: 0.8928 - val_acc: 0.6603\n",
"47/47 [==============================] - 6s 135ms/step - loss: 0.8928 - acc: 0.6603\n",
"\n",
"Accuracy of 9 66.03333353996277\n",
"\n",
"\n",
"Estimated Final Accuracy 0.682 (0.032)\n",
"Confusiton matrix of last result\n",
"[[ 78 47 74 50 77 111]\n",
" [ 95 49 96 78 67 89]\n",
" [101 68 103 64 91 126]\n",
" [105 67 98 72 78 105]\n",
" [ 92 69 89 64 78 118]\n",
" [103 54 108 78 73 85]]\n"
]
}
],
"source": [
"#applying Kfolds technique\n",
"k_folds =10\n",
"cv_scores, mat_list = list(), list()\n",
"for _ in range(k_folds):\n",
" \n",
" val_acc, mat = evaluate(train_generator,test_generator)\n",
" print(f'\\nAccuracy of {_} - %.3f\\n\\n' %(val_acc*100))\n",
" cv_scores.append(val_acc)\n",
" mat_list.append(mat)\n",
" \n",
" \n",
"print('Estimated Final Accuracy %.3f (%.3f)' % (np.mean(cv_scores)*100, np.std(cv_scores)))\n",
"\n",
"#confusion matrix\n",
"print('Confusiton matrix of last result')\n",
"print(mat_list[-1])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"image/png":...