{"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, ReLU, Conv2D, DepthwiseConv2D, GlobalAveragePooling2D, BatchNormalization\n\nfrom tensorflow.keras.losses import CategoricalCrossentropy\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras import Model","metadata":{"execution":{"iopub.status.busy":"2023-07-17T01:46:28.721388Z","iopub.execute_input":"2023-07-17T01:46:28.721765Z","iopub.status.idle":"2023-07-17T01:46:28.730299Z","shell.execute_reply.started":"2023-07-17T01:46:28.721732Z","shell.execute_reply":"2023-07-17T01:46:28.729018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/4-animal-classification'","metadata":{"execution":{"iopub.status.busy":"2023-07-17T01:45:40.311364Z","iopub.execute_input":"2023-07-17T01:45:40.312006Z","iopub.status.idle":"2023-07-17T01:45:40.317574Z","shell.execute_reply.started":"2023-07-17T01:45:40.311970Z","shell.execute_reply":"2023-07-17T01:45:40.316176Z"},"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":"2023-07-17T01:45:42.373952Z","iopub.execute_input":"2023-07-17T01:45:42.374955Z","iopub.status.idle":"2023-07-17T01:45:43.078043Z","shell.execute_reply.started":"2023-07-17T01:45:42.374913Z","shell.execute_reply":"2023-07-17T01:45:43.077026Z"},"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":"2023-07-17T01:45:45.595355Z","iopub.execute_input":"2023-07-17T01:45:45.595728Z","iopub.status.idle":"2023-07-17T01:45:45.811802Z","shell.execute_reply.started":"2023-07-17T01:45:45.595696Z","shell.execute_reply":"2023-07-17T01:45:45.810806Z"},"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":"2023-07-17T01:45:48.138974Z","iopub.execute_input":"2023-07-17T01:45:48.140136Z","iopub.status.idle":"2023-07-17T01:45:49.588155Z","shell.execute_reply.started":"2023-07-17T01:45:48.140094Z","shell.execute_reply":"2023-07-17T01:45:49.586931Z"},"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":"2023-07-17T01:45:52.338676Z","iopub.execute_input":"2023-07-17T01:45:52.339067Z","iopub.status.idle":"2023-07-17T01:45:52.345021Z","shell.execute_reply.started":"2023-07-17T01:45:52.339034Z","shell.execute_reply":"2023-07-17T01:45:52.343990Z"},"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":"2023-07-17T01:45:52.716194Z","iopub.execute_input":"2023-07-17T01:45:52.716584Z","iopub.status.idle":"2023-07-17T01:45:53.058663Z","shell.execute_reply.started":"2023-07-17T01:45:52.716551Z","shell.execute_reply":"2023-07-17T01:45:53.057578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Building the model","metadata":{}},{"cell_type":"code","source":"input_img = Input(shape=(224, 224, 3))\nx = Conv2D(filters=32, kernel_size=(3,3), strides=(2,2), padding='same')(input_img)\nx = BatchNormalization()(x)\nx = ReLU()(x)","metadata":{"execution":{"iopub.status.busy":"2023-07-17T01:52:40.222787Z","iopub.execute_input":"2023-07-17T01:52:40.223813Z","iopub.status.idle":"2023-07-17T01:52:40.252006Z","shell.execute_reply.started":"2023-07-17T01:52:40.223764Z","shell.execute_reply":"2023-07-17T01:52:40.251070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convolution_block(input_layer, strides, filters):\n    x = DepthwiseConv2D(kernel_size=3, strides=strides, padding='same')(input_layer)\n    x = BatchNormalization()(x)\n    x = ReLU()(x)\n    \n    x = Conv2D(filters=filters, kernel_size=1, strides=1)(x)\n    x = BatchNormalization()(x)\n    x = ReLU()(x)\n    \n    return x","metadata":{"execution":{"iopub.status.busy":"2023-07-17T01:52:41.419572Z","iopub.execute_input":"2023-07-17T01:52:41.419975Z","iopub.status.idle":"2023-07-17T01:52:41.427171Z","shell.execute_reply.started":"2023-07-17T01:52:41.419942Z","shell.execute_reply":"2023-07-17T01:52:41.426000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# MobileNet model\nclasses = 4\nx = convolution_block(x, filters = 64, strides = 1)\nx = convolution_block(x, filters = 128, strides = 2)\nx = convolution_block(x, filters = 128, strides = 1)\nx = convolution_block(x, filters = 256, strides = 2)\nx = convolution_block(x, filters = 256, strides = 1)\nx = convolution_block(x, filters = 512, strides = 2)\n\nfor i in range (5):\n     x = convolution_block(x, filters = 512, strides = 1)\n\nx = convolution_block(x, filters = 1024, strides = 2)\nx = convolution_block(x, filters = 1024, strides = 1)\nx = GlobalAveragePooling2D()(x)\n\nout = Dense (units = classes, activation = 'softmax')(x)\nmobilenet_tf = Model(inputs=input_img, outputs=out)","metadata":{"execution":{"iopub.status.busy":"2023-07-17T01:52:41.723094Z","iopub.execute_input":"2023-07-17T01:52:41.723719Z","iopub.status.idle":"2023-07-17T01:52:42.190485Z","shell.execute_reply.started":"2023-07-17T01:52:41.723685Z","shell.execute_reply":"2023-07-17T01:52:42.189478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mobilenet_tf.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), \n            loss='categorical_crossentropy',\n            metrics = ['accuracy'])\n\nmobilenet_tf.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-17T01:52:50.098817Z","iopub.execute_input":"2023-07-17T01:52:50.099868Z","iopub.status.idle":"2023-07-17T01:52:50.138046Z","shell.execute_reply.started":"2023-07-17T01:52:50.099809Z","shell.execute_reply":"2023-07-17T01:52:50.136763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. Training and evaluating","metadata":{}},{"cell_type":"code","source":"history = mobilenet_tf.fit(train_ds , validation_data = val_ds , epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2023-07-17T01:52:51.906598Z","iopub.execute_input":"2023-07-17T01:52:51.907346Z","iopub.status.idle":"2023-07-17T02:02:02.601678Z","shell.execute_reply.started":"2023-07-17T01:52:51.907300Z","shell.execute_reply":"2023-07-17T02:02:02.600586Z"},"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":"2023-07-17T01:52:15.759200Z","iopub.status.idle":"2023-07-17T01:52:15.760014Z","shell.execute_reply.started":"2023-07-17T01:52:15.759712Z","shell.execute_reply":"2023-07-17T01:52:15.759740Z"},"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":"2023-07-17T01:52:15.761651Z","iopub.status.idle":"2023-07-17T01:52:15.762770Z","shell.execute_reply.started":"2023-07-17T01:52:15.762508Z","shell.execute_reply":"2023-07-17T01:52:15.762533Z"},"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":"2023-07-17T01:52:15.764038Z","iopub.status.idle":"2023-07-17T01:52:15.764923Z","shell.execute_reply.started":"2023-07-17T01:52:15.764638Z","shell.execute_reply":"2023-07-17T01:52:15.764665Z"},"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":"2023-07-17T01:52:15.766327Z","iopub.status.idle":"2023-07-17T01:52:15.773136Z","shell.execute_reply.started":"2023-07-17T01:52:15.772928Z","shell.execute_reply":"2023-07-17T01:52:15.772949Z"},"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":"2023-07-17T01:52:15.775373Z","iopub.status.idle":"2023-07-17T01:52:15.776405Z","shell.execute_reply.started":"2023-07-17T01:52:15.776116Z","shell.execute_reply":"2023-07-17T01:52:15.776144Z"},"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":"2023-07-17T01:52:15.777737Z","iopub.status.idle":"2023-07-17T01:52:15.778529Z","shell.execute_reply.started":"2023-07-17T01:52:15.778278Z","shell.execute_reply":"2023-07-17T01:52:15.778303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}