{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 1. Importing packages","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport cv2\nimport random\nimport os\n\nfrom tensorflow.keras.layers import Input , Dense , Flatten , GlobalAveragePooling2D\nfrom tensorflow.keras.models import Sequential","metadata":{"execution":{"iopub.status.busy":"2022-08-21T19:22:57.015591Z","iopub.execute_input":"2022-08-21T19:22:57.016072Z","iopub.status.idle":"2022-08-21T19:22:57.022541Z","shell.execute_reply.started":"2022-08-21T19:22:57.016029Z","shell.execute_reply":"2022-08-21T19:22:57.021303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/4-animal-classification'","metadata":{"execution":{"iopub.status.busy":"2022-08-21T19:22:57.028955Z","iopub.execute_input":"2022-08-21T19:22:57.029847Z","iopub.status.idle":"2022-08-21T19:22:57.034721Z","shell.execute_reply.started":"2022-08-21T19:22:57.029806Z","shell.execute_reply":"2022-08-21T19:22:57.033558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Visualising data","metadata":{}},{"cell_type":"code","source":"names = []\nnums = []\ndata = {'Name of class':[],'Number of samples':[]}\n\nfor i in os.listdir(path+'/train'):\n    nums.append(len(os.listdir(path+'/train/'+i)))\n    names.append(i)\n\ndata['Name of class']+=names\ndata['Number of samples']+=nums\n\ndf = pd.DataFrame(data)\ndf","metadata":{"execution":{"iopub.status.busy":"2022-08-21T19:22:57.064584Z","iopub.execute_input":"2022-08-21T19:22:57.064933Z","iopub.status.idle":"2022-08-21T19:22:57.086391Z","shell.execute_reply.started":"2022-08-21T19:22:57.064904Z","shell.execute_reply":"2022-08-21T19:22:57.085406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(x=df['Name of class'],y=df['Number of samples'])","metadata":{"execution":{"iopub.status.busy":"2022-08-21T19:22:57.088150Z","iopub.execute_input":"2022-08-21T19:22:57.089147Z","iopub.status.idle":"2022-08-21T19:22:57.262662Z","shell.execute_reply.started":"2022-08-21T19:22:57.089109Z","shell.execute_reply":"2022-08-21T19:22:57.261689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = os.listdir(path+'/train')\n\nplt.figure(figsize=(30 , 30))\nfor x in range(10):\n    i = random.randint(0,3)                    # getting the class\n    images = os.listdir(path+'/train'+'/'+classes[i])\n    j = random.randint(0,600)                  # getting the image\n    image = cv2.imread(path+'/train'+'/'+classes[i]+'/'+images[j])\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    ax = plt.subplot(5, 5, x + 1)\n    plt.imshow(image)\n    plt.title(classes[i])\n    plt.axis(\"off\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T19:22:57.264663Z","iopub.execute_input":"2022-08-21T19:22:57.265704Z","iopub.status.idle":"2022-08-21T19:22:58.516003Z","shell.execute_reply.started":"2022-08-21T19:22:57.265665Z","shell.execute_reply":"2022-08-21T19:22:58.515112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Preparing the data","metadata":{}},{"cell_type":"code","source":"image_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale = 1./255 , rotation_range=20,\n                                                                width_shift_range=0.2,\n                                                                height_shift_range=0.2,\n                                                                horizontal_flip=True, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T19:22:58.517220Z","iopub.execute_input":"2022-08-21T19:22:58.518233Z","iopub.status.idle":"2022-08-21T19:22:58.524526Z","shell.execute_reply.started":"2022-08-21T19:22:58.518190Z","shell.execute_reply":"2022-08-21T19:22:58.523249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = image_datagen.flow_from_directory(\n        path+'/train',\n        subset='training',\n        target_size=(224 , 224),\n        batch_size=32)\n\nval_ds = image_datagen.flow_from_directory(\n        path+'/train',\n        subset='validation',\n        target_size=(224 , 224),\n        batch_size=32 )","metadata":{"execution":{"iopub.status.busy":"2022-08-21T19:22:58.527088Z","iopub.execute_input":"2022-08-21T19:22:58.528119Z","iopub.status.idle":"2022-08-21T19:22:58.745622Z","shell.execute_reply.started":"2022-08-21T19:22:58.528082Z","shell.execute_reply":"2022-08-21T19:22:58.744641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Building the model","metadata":{}},{"cell_type":"code","source":"# i'll be using inception_v3 for this model , along with 2 extra dense layers and the output layer\n\nmobilenet = tf.keras.applications.mobilenet.MobileNet(input_shape=(224 , 224, 3),\n                                           include_top=False,\n                                           weights='imagenet')\n\nmodel = Sequential()\nmodel.add(mobilenet)\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Flatten())\nmodel.add(Dense(1024, activation=\"relu\"))\nmodel.add(Dense(512, activation=\"relu\"))\nmodel.add(Dense(4, activation=\"softmax\" , name=\"classification\"))","metadata":{"execution":{"iopub.status.busy":"2022-08-21T19:22:58.747198Z","iopub.execute_input":"2022-08-21T19:22:58.747560Z","iopub.status.idle":"2022-08-21T19:22:59.445779Z","shell.execute_reply.started":"2022-08-21T19:22:58.747524Z","shell.execute_reply":"2022-08-21T19:22:59.444823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.0005,momentum=0.9), \n            loss='categorical_crossentropy',\n            metrics = ['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T19:22:59.447213Z","iopub.execute_input":"2022-08-21T19:22:59.447558Z","iopub.status.idle":"2022-08-21T19:22:59.466262Z","shell.execute_reply.started":"2022-08-21T19:22:59.447522Z","shell.execute_reply":"2022-08-21T19:22:59.465265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. Training and evaluating","metadata":{}},{"cell_type":"code","source":"history = model.fit(train_ds , validation_data = val_ds , epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T19:22:59.467768Z","iopub.execute_input":"2022-08-21T19:22:59.468170Z","iopub.status.idle":"2022-08-21T19:31:27.567433Z","shell.execute_reply.started":"2022-08-21T19:22:59.468131Z","shell.execute_reply":"2022-08-21T19:31:27.566476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(val_ds)\n\nplt.figure()\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.legend(['loss','val_loss'],loc='upper right')\nplt.show()\nplt.figure()\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.legend(['accuracy','val_accuracy'],loc='upper right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T19:31:27.572208Z","iopub.execute_input":"2022-08-21T19:31:27.574637Z","iopub.status.idle":"2022-08-21T19:31:38.867382Z","shell.execute_reply.started":"2022-08-21T19:31:27.574599Z","shell.execute_reply":"2022-08-21T19:31:38.866408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6. Preparing for submission","metadata":{}},{"cell_type":"code","source":"sub_csv ='../input/4-animal-classification/Sample_submission.csv'\npath_test = '../input/4-animal-classification/test/test'","metadata":{"execution":{"iopub.status.busy":"2022-08-21T19:49:26.986499Z","iopub.execute_input":"2022-08-21T19:49:26.987189Z","iopub.status.idle":"2022-08-21T19:49:26.992222Z","shell.execute_reply.started":"2022-08-21T19:49:26.987151Z","shell.execute_reply":"2022-08-21T19:49:26.990729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub = pd.read_csv(sub_csv)\nimage_id = df_sub['ID']\ndf_sub.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T20:44:26.462722Z","iopub.execute_input":"2022-08-21T20:44:26.463460Z","iopub.status.idle":"2022-08-21T20:44:26.479671Z","shell.execute_reply.started":"2022-08-21T20:44:26.463425Z","shell.execute_reply":"2022-08-21T20:44:26.478767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# each image will be treated individually and saved in a list which is then merged with the id of it's correspondence\n\nfrom tensorflow.keras.preprocessing.image import load_img\n\n\n# making a list for our predictions\nlabels = []\n\n\nfor i in image_id:\n    image = load_img(path_test+'/'+str(i)+'.jpg', target_size=(224, 224))\n    img = np.array(image)\n    img = img / 255.0\n    img = img.reshape(1,224,224,3)\n    label = model.predict(img)\n    label_id = label[0].tolist()\n    \n    labels.append(label_id.index(max(label_id)))\n","metadata":{"execution":{"iopub.status.busy":"2022-08-21T20:44:29.344391Z","iopub.execute_input":"2022-08-21T20:44:29.344758Z","iopub.status.idle":"2022-08-21T20:45:06.502031Z","shell.execute_reply.started":"2022-08-21T20:44:29.344726Z","shell.execute_reply":"2022-08-21T20:45:06.501016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we will replace the previous empty columns with the new predictions    \n    \ndf_sub['Label'] = labels\ndf_sub.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T20:45:21.364987Z","iopub.execute_input":"2022-08-21T20:45:21.365614Z","iopub.status.idle":"2022-08-21T20:45:21.378002Z","shell.execute_reply.started":"2022-08-21T20:45:21.365578Z","shell.execute_reply":"2022-08-21T20:45:21.376746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# saving file\ndf_sub.to_csv('submission_file.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T20:50:32.283560Z","iopub.execute_input":"2022-08-21T20:50:32.283949Z","iopub.status.idle":"2022-08-21T20:50:32.292115Z","shell.execute_reply.started":"2022-08-21T20:50:32.283917Z","shell.execute_reply":"2022-08-21T20:50:32.290849Z"},"trusted":true},"execution_count":null,"outputs":[]}]}