{"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, Dropout\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.applications import ResNet50","metadata":{"execution":{"iopub.status.busy":"2022-09-26T23:54:36.962787Z","iopub.execute_input":"2022-09-26T23:54:36.964234Z","iopub.status.idle":"2022-09-26T23:54:36.971879Z","shell.execute_reply.started":"2022-09-26T23:54:36.964180Z","shell.execute_reply":"2022-09-26T23:54:36.970423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/4-animal-classification'","metadata":{"execution":{"iopub.status.busy":"2022-09-26T23:54:40.189857Z","iopub.execute_input":"2022-09-26T23:54:40.190321Z","iopub.status.idle":"2022-09-26T23:54:40.195326Z","shell.execute_reply.started":"2022-09-26T23:54:40.190285Z","shell.execute_reply":"2022-09-26T23:54:40.194041Z"},"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-09-26T23:54:43.044214Z","iopub.execute_input":"2022-09-26T23:54:43.044605Z","iopub.status.idle":"2022-09-26T23:54:43.338793Z","shell.execute_reply.started":"2022-09-26T23:54:43.044572Z","shell.execute_reply":"2022-09-26T23:54:43.337745Z"},"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-09-26T23:54:47.841593Z","iopub.execute_input":"2022-09-26T23:54:47.842121Z","iopub.status.idle":"2022-09-26T23:54:48.141343Z","shell.execute_reply.started":"2022-09-26T23:54:47.842074Z","shell.execute_reply":"2022-09-26T23:54:48.140193Z"},"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-09-26T23:54:52.656027Z","iopub.execute_input":"2022-09-26T23:54:52.656554Z","iopub.status.idle":"2022-09-26T23:54:54.786345Z","shell.execute_reply.started":"2022-09-26T23:54:52.656509Z","shell.execute_reply":"2022-09-26T23:54:54.784948Z"},"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                                                                zoom_range = 0.2,\n                                                                horizontal_flip=True, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T23:55:01.789049Z","iopub.execute_input":"2022-09-26T23:55:01.790256Z","iopub.status.idle":"2022-09-26T23:55:01.798075Z","shell.execute_reply.started":"2022-09-26T23:55:01.790199Z","shell.execute_reply":"2022-09-26T23:55:01.796189Z"},"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=50)\n\nval_ds = image_datagen.flow_from_directory(\n        path+'/train',\n        subset='validation',\n        target_size=(224 , 224),\n        batch_size=50 )","metadata":{"execution":{"iopub.status.busy":"2022-09-26T23:55:06.612175Z","iopub.execute_input":"2022-09-26T23:55:06.613203Z","iopub.status.idle":"2022-09-26T23:55:06.936090Z","shell.execute_reply.started":"2022-09-26T23:55:06.613145Z","shell.execute_reply":"2022-09-26T23:55:06.934834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Building the model","metadata":{}},{"cell_type":"code","source":"new_model = tf.keras.applications.xception.Xception(input_shape=(224 , 224, 3), \n                                               include_top=(False),\n                                               weights='imagenet')","metadata":{"execution":{"iopub.status.busy":"2022-09-26T23:55:11.194030Z","iopub.execute_input":"2022-09-26T23:55:11.194416Z","iopub.status.idle":"2022-09-26T23:55:15.997525Z","shell.execute_reply.started":"2022-09-26T23:55:11.194385Z","shell.execute_reply":"2022-09-26T23:55:15.996434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model.trainable = False\n\nmodelx = Sequential()\nmodelx.add(new_model)\nmodelx.add(GlobalAveragePooling2D())\nmodelx.add(Flatten())\nmodelx.add(Dense(512, activation=\"relu\"))\nmodelx.add(Dense(1024, activation=\"relu\"))\nmodelx.add(Dropout(0.5))\nmodelx.add(Dense(4, activation=\"softmax\" , name=\"classification\"))","metadata":{"execution":{"iopub.status.busy":"2022-09-26T23:59:27.710503Z","iopub.execute_input":"2022-09-26T23:59:27.710941Z","iopub.status.idle":"2022-09-26T23:59:28.095316Z","shell.execute_reply.started":"2022-09-26T23:59:27.710904Z","shell.execute_reply":"2022-09-26T23:59:28.094131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelx.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.0005,momentum=0.9), \n            loss='categorical_crossentropy',\n            metrics = ['accuracy'])\n\nmodelx.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-26T23:59:30.159894Z","iopub.execute_input":"2022-09-26T23:59:30.160707Z","iopub.status.idle":"2022-09-26T23:59:30.184220Z","shell.execute_reply.started":"2022-09-26T23:59:30.160668Z","shell.execute_reply":"2022-09-26T23:59:30.183048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. Training and evaluating","metadata":{}},{"cell_type":"code","source":"history = modelx.fit(train_ds , validation_data = val_ds , epochs = 30)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T23:59:33.544117Z","iopub.execute_input":"2022-09-26T23:59:33.544530Z","iopub.status.idle":"2022-09-27T00:26:24.580233Z","shell.execute_reply.started":"2022-09-26T23:59:33.544497Z","shell.execute_reply":"2022-09-27T00:26:24.579095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loop fine-tuning dari seluruh model\nnew_model.trainable = True\n\nmodelx.summary()\n\nmodelx.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.0005,momentum=0.9), \n            loss='categorical_crossentropy',\n            metrics = ['accuracy'])\n\nhistory = modelx.fit(train_ds , validation_data = val_ds , epochs = 50)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T00:26:44.358110Z","iopub.execute_input":"2022-09-27T00:26:44.358504Z","iopub.status.idle":"2022-09-27T01:18:11.664940Z","shell.execute_reply.started":"2022-09-27T00:26:44.358470Z","shell.execute_reply":"2022-09-27T01:18:11.663633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelx.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-09-27T01:19:12.160012Z","iopub.execute_input":"2022-09-27T01:19:12.160435Z","iopub.status.idle":"2022-09-27T01:19:23.977731Z","shell.execute_reply.started":"2022-09-27T01:19:12.160401Z","shell.execute_reply":"2022-09-27T01:19:23.976769Z"},"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-09-27T01:24:27.826891Z","iopub.execute_input":"2022-09-27T01:24:27.827993Z","iopub.status.idle":"2022-09-27T01:24:27.833846Z","shell.execute_reply.started":"2022-09-27T01:24:27.827947Z","shell.execute_reply":"2022-09-27T01:24:27.832666Z"},"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-09-27T01:24:31.195846Z","iopub.execute_input":"2022-09-27T01:24:31.196461Z","iopub.status.idle":"2022-09-27T01:24:31.226686Z","shell.execute_reply.started":"2022-09-27T01:24:31.196408Z","shell.execute_reply":"2022-09-27T01:24:31.225654Z"},"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 = modelx.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-09-27T01:24:45.905012Z","iopub.execute_input":"2022-09-27T01:24:45.905871Z","iopub.status.idle":"2022-09-27T01:26:11.127425Z","shell.execute_reply.started":"2022-09-27T01:24:45.905793Z","shell.execute_reply":"2022-09-27T01:26:11.126308Z"},"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-09-27T01:28:50.927256Z","iopub.execute_input":"2022-09-27T01:28:50.927738Z","iopub.status.idle":"2022-09-27T01:28:50.939137Z","shell.execute_reply.started":"2022-09-27T01:28:50.927701Z","shell.execute_reply":"2022-09-27T01:28:50.938127Z"},"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-09-27T01:28:57.268048Z","iopub.execute_input":"2022-09-27T01:28:57.268436Z","iopub.status.idle":"2022-09-27T01:28:57.278316Z","shell.execute_reply.started":"2022-09-27T01:28:57.268404Z","shell.execute_reply":"2022-09-27T01:28:57.277143Z"},"trusted":true},"execution_count":null,"outputs":[]}]}