{"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\nfrom tensorflow.keras.applications import ResNet50","metadata":{"execution":{"iopub.status.busy":"2022-09-25T08:12:11.146958Z","iopub.execute_input":"2022-09-25T08:12:11.14741Z","iopub.status.idle":"2022-09-25T08:12:17.995748Z","shell.execute_reply.started":"2022-09-25T08:12:11.147334Z","shell.execute_reply":"2022-09-25T08:12:17.994578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/4-animal-classification'","metadata":{"execution":{"iopub.status.busy":"2022-09-25T08:12:22.004163Z","iopub.execute_input":"2022-09-25T08:12:22.004826Z","iopub.status.idle":"2022-09-25T08:12:22.009521Z","shell.execute_reply.started":"2022-09-25T08:12:22.004788Z","shell.execute_reply":"2022-09-25T08:12:22.008488Z"},"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-25T08:12:44.182201Z","iopub.execute_input":"2022-09-25T08:12:44.182587Z","iopub.status.idle":"2022-09-25T08:12:44.199233Z","shell.execute_reply.started":"2022-09-25T08:12:44.182551Z","shell.execute_reply":"2022-09-25T08:12:44.19832Z"},"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-25T08:14:30.546933Z","iopub.execute_input":"2022-09-25T08:14:30.54806Z","iopub.status.idle":"2022-09-25T08:14:30.757029Z","shell.execute_reply.started":"2022-09-25T08:14:30.548015Z","shell.execute_reply":"2022-09-25T08:14:30.756088Z"},"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-25T08:14:34.390207Z","iopub.execute_input":"2022-09-25T08:14:34.390602Z","iopub.status.idle":"2022-09-25T08:14:35.65584Z","shell.execute_reply.started":"2022-09-25T08:14:34.390568Z","shell.execute_reply":"2022-09-25T08:14:35.654577Z"},"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-25T09:31:16.973676Z","iopub.execute_input":"2022-09-25T09:31:16.974063Z","iopub.status.idle":"2022-09-25T09:31:16.980714Z","shell.execute_reply.started":"2022-09-25T09:31:16.974029Z","shell.execute_reply":"2022-09-25T09:31:16.979655Z"},"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-09-25T09:31:18.221585Z","iopub.execute_input":"2022-09-25T09:31:18.221969Z","iopub.status.idle":"2022-09-25T09:31:18.43564Z","shell.execute_reply.started":"2022-09-25T09:31:18.221934Z","shell.execute_reply":"2022-09-25T09:31:18.434626Z"},"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-25T11:06:18.290676Z","iopub.execute_input":"2022-09-25T11:06:18.291046Z","iopub.status.idle":"2022-09-25T11:06:19.330392Z","shell.execute_reply.started":"2022-09-25T11:06:18.291011Z","shell.execute_reply":"2022-09-25T11:06:19.329383Z"},"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(Dense(4, activation=\"softmax\" , name=\"classification\"))","metadata":{"execution":{"iopub.status.busy":"2022-09-25T11:11:55.629697Z","iopub.execute_input":"2022-09-25T11:11:55.630084Z","iopub.status.idle":"2022-09-25T11:11:56.060473Z","shell.execute_reply.started":"2022-09-25T11:11:55.630052Z","shell.execute_reply":"2022-09-25T11:11:56.059347Z"},"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-25T11:37:41.04917Z","iopub.execute_input":"2022-09-25T11:37:41.049633Z","iopub.status.idle":"2022-09-25T11:37:41.069693Z","shell.execute_reply.started":"2022-09-25T11:37:41.049604Z","shell.execute_reply":"2022-09-25T11:37:41.068668Z"},"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 = 20)","metadata":{"execution":{"iopub.status.busy":"2022-09-25T11:15:44.054282Z","iopub.execute_input":"2022-09-25T11:15:44.054962Z","iopub.status.idle":"2022-09-25T11:32:28.077003Z","shell.execute_reply.started":"2022-09-25T11:15:44.054924Z","shell.execute_reply":"2022-09-25T11:32:28.076046Z"},"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 = 10)","metadata":{"execution":{"iopub.status.busy":"2022-09-25T11:38:18.642509Z","iopub.execute_input":"2022-09-25T11:38:18.643769Z","iopub.status.idle":"2022-09-25T11:48:16.841563Z","shell.execute_reply.started":"2022-09-25T11:38:18.643716Z","shell.execute_reply":"2022-09-25T11:48:16.840641Z"},"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-25T11:49:37.263145Z","iopub.execute_input":"2022-09-25T11:49:37.26355Z","iopub.status.idle":"2022-09-25T11:49:47.796402Z","shell.execute_reply.started":"2022-09-25T11:49:37.263514Z","shell.execute_reply":"2022-09-25T11:49:47.789322Z"},"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-24T15:48:47.33095Z","iopub.execute_input":"2022-09-24T15:48:47.331436Z","iopub.status.idle":"2022-09-24T15:48:47.33714Z","shell.execute_reply.started":"2022-09-24T15:48:47.33139Z","shell.execute_reply":"2022-09-24T15:48:47.335921Z"},"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-24T15:48:49.867184Z","iopub.execute_input":"2022-09-24T15:48:49.867633Z","iopub.status.idle":"2022-09-24T15:48:49.90393Z","shell.execute_reply.started":"2022-09-24T15:48:49.867594Z","shell.execute_reply":"2022-09-24T15:48:49.902882Z"},"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-09-24T15:49:09.135934Z","iopub.execute_input":"2022-09-24T15:49:09.13631Z","iopub.status.idle":"2022-09-24T15:49:54.042242Z","shell.execute_reply.started":"2022-09-24T15:49:09.136278Z","shell.execute_reply":"2022-09-24T15:49:54.041293Z"},"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-24T15:50:01.22964Z","iopub.execute_input":"2022-09-24T15:50:01.230039Z","iopub.status.idle":"2022-09-24T15:50:01.241397Z","shell.execute_reply.started":"2022-09-24T15:50:01.230007Z","shell.execute_reply":"2022-09-24T15:50:01.240206Z"},"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-24T15:50:14.757207Z","iopub.execute_input":"2022-09-24T15:50:14.757848Z","iopub.status.idle":"2022-09-24T15:50:14.768132Z","shell.execute_reply.started":"2022-09-24T15:50:14.757807Z","shell.execute_reply":"2022-09-24T15:50:14.766995Z"},"trusted":true},"execution_count":null,"outputs":[]}]}