Image Classification Build a CNN based image classifier, train and test the model using the following dataset. You can download the Intel Image Classification dataset at:Kaggle(Links to an external...

1 answer below »


Image Classification



Build a CNN based image classifier, train and test the model using the following dataset. You can download the Intel Image Classification dataset at:Kaggle(Links to an external site.).
https://www.kaggle.com/puneet6060/intel-image-classification/data#



Dataset:


1. This is image data of Natural Scenes around the world.


2. This Data contains around 25k images of size 150x150 distributed under 6 categories.


{'buildings' -> 0,
'forest' -> 1,
'glacier' -> 2,
'mountain' -> 3,
'sea' -> 4,
'street' -> 5 }


The Train, Test and Prediction data are separated in zip files. There are around 14k images in Train, 3k in Test, and 7k in Prediction.


Tasks


1. Describe the CNN model of the classification. You need to describe each layer including activation functions, filters, pooling function etc. Give a visualization of your model. The classifier needs to classify each image as one of the 6 categories: building, forest, glacier, mountain, sea, street.


2. The python code of the classification implementation and validation.


3. 10 fold cross-validation results, including a confusion matrix for the final result.


4. Build another classifier based on transfer learning of a pretrained model such as AlexNet. Compare the classification performance to the previous classifier you built. Describe the transfer learning process.


5. Submit code and project report that answers the above four questions including test results and visualizations.

Answered Same DayAug 05, 2021

Answer To: Image Classification Build a CNN based image classifier, train and test the model using the...

Raj answered on Aug 06 2021
164 Votes
{
"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 = ImageDat
aGenerator( 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":...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here