{"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":"# Import  library","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-05-28T15:38:34.831836Z","iopub.execute_input":"2022-05-28T15:38:34.832483Z","iopub.status.idle":"2022-05-28T15:39:26.487728Z","shell.execute_reply.started":"2022-05-28T15:38:34.832443Z","shell.execute_reply":"2022-05-28T15:39:26.486861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport tensorflow as tf\nimport zipfile\nimport pandas as pd\nimport numpy as np\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport pylab","metadata":{"execution":{"iopub.status.busy":"2022-05-28T15:39:26.490835Z","iopub.execute_input":"2022-05-28T15:39:26.491030Z","iopub.status.idle":"2022-05-28T15:39:32.132784Z","shell.execute_reply.started":"2022-05-28T15:39:26.491007Z","shell.execute_reply":"2022-05-28T15:39:32.132017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load and process data","metadata":{}},{"cell_type":"code","source":"split=0.2\nseed=19522315\nbatch_size=256\nimg_size=(224,224)\n\ntrain_ds = keras.utils.image_dataset_from_directory(\n    '../input/classification-of-plants-of-southeast-asia/bali-26_train/bali-26_train',\n    labels=\"inferred\",\n    label_mode=\"int\",\n    color_mode=\"rgb\",\n    batch_size=batch_size,\n    image_size=img_size,\n    shuffle=True,\n    seed=seed,\n    validation_split=split,\n    subset='training',\n)\nval_ds = keras.utils.image_dataset_from_directory(\n    '../input/classification-of-plants-of-southeast-asia/bali-26_train/bali-26_train',\n    labels=\"inferred\",\n    label_mode=\"int\",\n    color_mode=\"rgb\",\n    batch_size=batch_size,\n    image_size=img_size,\n    shuffle=True,\n    seed=seed,\n    validation_split=split,\n    subset='validation',\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-28T15:39:32.134266Z","iopub.execute_input":"2022-05-28T15:39:32.134546Z","iopub.status.idle":"2022-05-28T15:39:48.859284Z","shell.execute_reply.started":"2022-05-28T15:39:32.134510Z","shell.execute_reply":"2022-05-28T15:39:48.857698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_augmentation = keras.Sequential([])","metadata":{"execution":{"iopub.status.busy":"2022-05-28T15:39:48.861304Z","iopub.execute_input":"2022-05-28T15:39:48.861622Z","iopub.status.idle":"2022-05-28T15:39:48.885403Z","shell.execute_reply.started":"2022-05-28T15:39:48.861583Z","shell.execute_reply":"2022-05-28T15:39:48.884706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create and train model","metadata":{}},{"cell_type":"code","source":"CB = [\n            keras.callbacks.ModelCheckpoint(\n            filepath=\"fine_tuning.keras\",\n            save_best_only=True,\n            monitor=\"val_loss\"),\n\n            keras.callbacks.EarlyStopping(\n            monitor='val_loss',\n            min_delta=0.0005,\n            patience=3)\n]","metadata":{"execution":{"iopub.status.busy":"2022-05-28T15:39:48.886519Z","iopub.execute_input":"2022-05-28T15:39:48.886780Z","iopub.status.idle":"2022-05-28T15:39:48.892801Z","shell.execute_reply.started":"2022-05-28T15:39:48.886746Z","shell.execute_reply":"2022-05-28T15:39:48.891808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Mobilenet","metadata":{}},{"cell_type":"code","source":"mobile_net  = keras.applications.MobileNet(\n    weights=\"imagenet\",\n    include_top=False)\n\nmobile_net.trainable = False\n\nprint(\"This is the number of trainable weights \"\n      \"before freezing the conv base:\", len(mobile_net.trainable_weights))\n\nmobile_net.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-28T15:39:48.893917Z","iopub.execute_input":"2022-05-28T15:39:48.894565Z","iopub.status.idle":"2022-05-28T15:39:51.573831Z","shell.execute_reply.started":"2022-05-28T15:39:48.894527Z","shell.execute_reply":"2022-05-28T15:39:51.573109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = keras.Input(shape=img_size+(3,))\nx = data_augmentation(inputs)\nx = keras.applications.mobilenet.preprocess_input(x)\nx = mobile_net(x)\nx = layers.Flatten()(x)\nx = layers.BatchNormalization()(x)\nx = layers.Dense(512,activation='relu')(x)\nx = layers.BatchNormalization()(x)\nx = layers.Dropout(0.3)(x)\nx = layers.Dense(128,activation='relu')(x)\n\noutputs = layers.Dense(26, activation=\"softmax\")(x)\nmobile_net = keras.Model(inputs, outputs)\nmobile_net.compile(optimizer=\"adam\",\n              loss=\"sparse_categorical_crossentropy\",\n              metrics=[\"accuracy\"])\nmobile_net.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-28T15:39:51.575030Z","iopub.execute_input":"2022-05-28T15:39:51.575294Z","iopub.status.idle":"2022-05-28T15:39:51.826523Z","shell.execute_reply.started":"2022-05-28T15:39:51.575260Z","shell.execute_reply":"2022-05-28T15:39:51.825845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_mobilenet = mobile_net.fit(\n    train_ds,\n    epochs=10,\n    validation_data=val_ds,\n    callbacks=CB\n    )\nmobile_net = keras.models.load_model(\"fine_tuning.keras\")","metadata":{"execution":{"iopub.status.busy":"2022-05-28T15:39:51.827833Z","iopub.execute_input":"2022-05-28T15:39:51.828077Z","iopub.status.idle":"2022-05-28T16:19:54.878655Z","shell.execute_reply.started":"2022-05-28T15:39:51.828041Z","shell.execute_reply":"2022-05-28T16:19:54.877852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(history_mobilenet.history).plot(figsize=(8,5))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-28T16:19:54.880063Z","iopub.execute_input":"2022-05-28T16:19:54.880319Z","iopub.status.idle":"2022-05-28T16:19:55.171583Z","shell.execute_reply.started":"2022-05-28T16:19:54.880285Z","shell.execute_reply":"2022-05-28T16:19:55.170927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submit","metadata":{}},{"cell_type":"code","source":"import os\ntest_filenames = os.listdir(\"../input/classification-of-plants-of-southeast-asia/bali-26_test/bali-26_test\")\ntest_df = pd.DataFrame({'id': test_filenames})\nnb_samples = test_df.shape[0]\nnb_samples\n8000\nfrom keras.preprocessing.image import ImageDataGenerator, load_img\ntest_gen = ImageDataGenerator()\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"../input/classification-of-plants-of-southeast-asia/bali-26_test/bali-26_test\", \n    x_col='id',\n    # y_col=None,\n    class_mode=None,\n    target_size=img_size,\n    batch_size=batch_size,\n    shuffle=False\n)\npredict = mobile_net.predict(test_generator)\ntest_df['category'] = np.argmax(predict, axis=-1)\nname=train_ds.class_names\nclass_name=test_df['category'].tolist()\nclass_name=[name[i] for i in class_name]\ntest_df['category']=class_name\ntest_df.to_csv(\"submit.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-05-28T16:19:55.174288Z","iopub.execute_input":"2022-05-28T16:19:55.174710Z","iopub.status.idle":"2022-05-28T16:22:25.421634Z","shell.execute_reply.started":"2022-05-28T16:19:55.174672Z","shell.execute_reply":"2022-05-28T16:22:25.420913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = pd.read_csv(\"./submit.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-05-28T16:22:25.422979Z","iopub.execute_input":"2022-05-28T16:22:25.423242Z","iopub.status.idle":"2022-05-28T16:22:25.436049Z","shell.execute_reply.started":"2022-05-28T16:22:25.423207Z","shell.execute_reply":"2022-05-28T16:22:25.435225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-28T16:22:25.437420Z","iopub.execute_input":"2022-05-28T16:22:25.437762Z","iopub.status.idle":"2022-05-28T16:22:25.451446Z","shell.execute_reply.started":"2022-05-28T16:22:25.437727Z","shell.execute_reply":"2022-05-28T16:22:25.450844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = pred.drop(axis=1, columns='Unnamed: 0')\npred.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-28T16:22:25.452673Z","iopub.execute_input":"2022-05-28T16:22:25.452950Z","iopub.status.idle":"2022-05-28T16:22:25.464831Z","shell.execute_reply.started":"2022-05-28T16:22:25.452917Z","shell.execute_reply":"2022-05-28T16:22:25.464117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred.to_csv('./submit2.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-28T16:22:25.467333Z","iopub.execute_input":"2022-05-28T16:22:25.467533Z","iopub.status.idle":"2022-05-28T16:22:25.482609Z","shell.execute_reply.started":"2022-05-28T16:22:25.467510Z","shell.execute_reply":"2022-05-28T16:22:25.481994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save weight\n","metadata":{}},{"cell_type":"code","source":"mobile_net.save_weights(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-05-28T16:22:25.483982Z","iopub.execute_input":"2022-05-28T16:22:25.484435Z","iopub.status.idle":"2022-05-28T16:22:25.711773Z","shell.execute_reply.started":"2022-05-28T16:22:25.484399Z","shell.execute_reply":"2022-05-28T16:22:25.711070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2","metadata":{"execution":{"iopub.status.busy":"2022-05-28T16:22:25.712874Z","iopub.execute_input":"2022-05-28T16:22:25.713107Z","iopub.status.idle":"2022-05-28T16:22:26.029854Z","shell.execute_reply.started":"2022-05-28T16:22:25.713075Z","shell.execute_reply":"2022-05-28T16:22:26.029167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img = cv2.imread('../input/classification-of-plants-of-southeast-asia/bali-26_test/bali-26_test/1.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-05-28T16:22:26.031070Z","iopub.execute_input":"2022-05-28T16:22:26.031404Z","iopub.status.idle":"2022-05-28T16:22:26.070202Z","shell.execute_reply.started":"2022-05-28T16:22:26.031368Z","shell.execute_reply":"2022-05-28T16:22:26.069569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(test_img)","metadata":{"execution":{"iopub.status.busy":"2022-05-28T16:22:26.071443Z","iopub.execute_input":"2022-05-28T16:22:26.071702Z","iopub.status.idle":"2022-05-28T16:22:26.552688Z","shell.execute_reply.started":"2022-05-28T16:22:26.071666Z","shell.execute_reply":"2022-05-28T16:22:26.552013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2022-05-28T16:35:14.082764Z","iopub.execute_input":"2022-05-28T16:35:14.083022Z","iopub.status.idle":"2022-05-28T16:35:14.088859Z","shell.execute_reply.started":"2022-05-28T16:35:14.082994Z","shell.execute_reply":"2022-05-28T16:35:14.087821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\nFileLink(r'/kaggle/working/model.h5')","metadata":{"execution":{"iopub.status.busy":"2022-05-28T16:36:09.107281Z","iopub.execute_input":"2022-05-28T16:36:09.107556Z","iopub.status.idle":"2022-05-28T16:36:09.116014Z","shell.execute_reply.started":"2022-05-28T16:36:09.107527Z","shell.execute_reply":"2022-05-28T16:36:09.115221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}