{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":86375,"databundleVersionId":9802769,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.applications import MobileNetV2\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom tensorflow.keras.metrics import Precision, Recall\nfrom tensorflow.keras.regularizers import l2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T11:33:10.345949Z","iopub.execute_input":"2024-11-15T11:33:10.347177Z","iopub.status.idle":"2024-11-15T11:33:10.355027Z","shell.execute_reply.started":"2024-11-15T11:33:10.347123Z","shell.execute_reply":"2024-11-15T11:33:10.353741Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_dir = '/kaggle/input/dl-2024-autum-exp-2/food-11_DL_2024'\ntrain_labeled_dir = os.path.join(base_dir, 'training/labeled')\nval_dir = os.path.join(base_dir, 'validation')\ntest_dir = os.path.join(base_dir, 'testing/00')\n\nimg_size = (128, 128)\nbatch_size = 32","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T11:33:28.593320Z","iopub.execute_input":"2024-11-15T11:33:28.593775Z","iopub.status.idle":"2024-11-15T11:33:28.600821Z","shell.execute_reply.started":"2024-11-15T11:33:28.593731Z","shell.execute_reply":"2024-11-15T11:33:28.599472Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True\n)\n\ntrain_generator = train_datagen.flow_from_directory(\n    train_labeled_dir,\n    target_size=img_size,\n    batch_size=batch_size,\n    class_mode='categorical'\n)\n\nval_datagen = ImageDataGenerator(rescale=1./255)\nval_generator = val_datagen.flow_from_directory(\n    val_dir,\n    target_size=img_size,\n    batch_size=batch_size,\n    class_mode='categorical'\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T11:33:52.837948Z","iopub.execute_input":"2024-11-15T11:33:52.838453Z","iopub.status.idle":"2024-11-15T11:33:53.011543Z","shell.execute_reply.started":"2024-11-15T11:33:52.838405Z","shell.execute_reply":"2024-11-15T11:33:53.010313Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(128, 128, 3))\nbase_model.trainable = False  ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T11:34:12.774265Z","iopub.execute_input":"2024-11-15T11:34:12.774740Z","iopub.status.idle":"2024-11-15T11:34:13.743867Z","shell.execute_reply.started":"2024-11-15T11:34:12.774698Z","shell.execute_reply":"2024-11-15T11:34:13.742615Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = base_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(128, activation='relu', kernel_regularizer=l2(0.01))(x)  # L2 regularization\nx = Dropout(0.5)(x)\noutput = Dense(11, activation='softmax')(x)\n\n\nmodel = Model(inputs=base_model.input, outputs=output)\nmodel.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy', Precision(), Recall()])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T11:34:40.082265Z","iopub.execute_input":"2024-11-15T11:34:40.082755Z","iopub.status.idle":"2024-11-15T11:34:40.157960Z","shell.execute_reply.started":"2024-11-15T11:34:40.082706Z","shell.execute_reply":"2024-11-15T11:34:40.156669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)\nlr_scheduler = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, verbose=1, min_lr=1e-6)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T11:35:05.572189Z","iopub.execute_input":"2024-11-15T11:35:05.572709Z","iopub.status.idle":"2024-11-15T11:35:05.579458Z","shell.execute_reply.started":"2024-11-15T11:35:05.572660Z","shell.execute_reply":"2024-11-15T11:35:05.578019Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    epochs=20,\n    validation_data=val_generator,\n    callbacks=[early_stopping, lr_scheduler]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T11:36:37.540194Z","iopub.execute_input":"2024-11-15T11:36:37.540736Z","iopub.status.idle":"2024-11-15T11:47:40.848131Z","shell.execute_reply.started":"2024-11-15T11:36:37.540688Z","shell.execute_reply":"2024-11-15T11:47:40.846671Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_model.trainable = True\nfor layer in base_model.layers[:100]:  \n    layer.trainable = False\n\nmodel.compile(optimizer=Adam(learning_rate=0.00001), loss='categorical_crossentropy', metrics=['accuracy', Precision(), Recall()])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T11:47:40.851044Z","iopub.execute_input":"2024-11-15T11:47:40.851461Z","iopub.status.idle":"2024-11-15T11:47:40.883428Z","shell.execute_reply.started":"2024-11-15T11:47:40.851418Z","shell.execute_reply":"2024-11-15T11:47:40.882259Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_fine = model.fit(\n    train_generator,\n    epochs=10,\n    validation_data=val_generator,\n    callbacks=[early_stopping, lr_scheduler]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T11:47:40.884849Z","iopub.execute_input":"2024-11-15T11:47:40.885267Z","iopub.status.idle":"2024-11-15T11:51:23.941723Z","shell.execute_reply.started":"2024-11-15T11:47:40.885222Z","shell.execute_reply":"2024-11-15T11:51:23.940328Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.plot(history.history['accuracy'], label='Train Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.legend()\nplt.title('Training and Validation Accuracy')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T11:51:23.944685Z","iopub.execute_input":"2024-11-15T11:51:23.945156Z","iopub.status.idle":"2024-11-15T11:51:24.298323Z","shell.execute_reply.started":"2024-11-15T11:51:23.945102Z","shell.execute_reply":"2024-11-15T11:51:24.297186Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_datagen.flow_from_directory(\n    directory=os.path.dirname(test_dir),\n    classes=[os.path.basename(test_dir)],\n    target_size=img_size,\n    batch_size=1,\n    class_mode=None,\n    shuffle=False\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T11:51:24.299601Z","iopub.execute_input":"2024-11-15T11:51:24.300013Z","iopub.status.idle":"2024-11-15T11:51:24.335819Z","shell.execute_reply.started":"2024-11-15T11:51:24.299968Z","shell.execute_reply":"2024-11-15T11:51:24.334764Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_predictions = model.predict(test_generator, verbose=1)\npredicted_classes = np.argmax(test_predictions, axis=1)\nclass_labels = {v: k for k, v in train_generator.class_indices.items()}\npredicted_labels = [class_labels[i] for i in predicted_classes]\n\nprint(predicted_labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T11:51:24.337327Z","iopub.execute_input":"2024-11-15T11:51:24.337853Z","iopub.status.idle":"2024-11-15T11:51:38.892998Z","shell.execute_reply.started":"2024-11-15T11:51:24.337770Z","shell.execute_reply":"2024-11-15T11:51:38.891598Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_ids = [int(os.path.splitext(os.path.basename(f))[0]) for f in test_generator.filenames]\n\nsubmission = pd.DataFrame({\n    'Id': test_ids,\n    'Category': predicted_classes\n})\n\nsubmission = submission.sort_values(by='Id')\n\nsubmission.to_csv('submission.csv', index=False)\nprint(\"Submission file saved!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-15T11:52:35.401498Z","iopub.execute_input":"2024-11-15T11:52:35.402061Z","iopub.status.idle":"2024-11-15T11:52:35.418327Z","shell.execute_reply.started":"2024-11-15T11:52:35.402009Z","shell.execute_reply":"2024-11-15T11:52:35.416691Z"}},"outputs":[],"execution_count":null}]}