{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"latex_envs":{"LaTeX_envs_menu_present":true,"autoclose":false,"autocomplete":true,"bibliofile":"biblio.bib","cite_by":"apalike","current_citInitial":1,"eqLabelWithNumbers":true,"eqNumInitial":1,"hotkeys":{"equation":"Ctrl-E","itemize":"Ctrl-I"},"labels_anchors":false,"latex_user_defs":false,"report_style_numbering":false,"user_envs_cfg":false},"toc":{"base_numbering":1,"nav_menu":{},"number_sections":true,"sideBar":true,"skip_h1_title":false,"title_cell":"Table of Contents","title_sidebar":"Contents","toc_cell":false,"toc_position":{},"toc_section_display":true,"toc_window_display":true},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":75134,"databundleVersionId":8246441,"sourceType":"competition"}],"dockerImageVersionId":30684,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Environment & GPU Check","metadata":{}},{"cell_type":"code","source":"import sys\ntype(sys.path)\nfor path in sys.path:\n    print(path)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:34:52.369745Z","iopub.execute_input":"2024-04-15T15:34:52.370374Z","iopub.status.idle":"2024-04-15T15:34:52.375417Z","shell.execute_reply.started":"2024-04-15T15:34:52.370341Z","shell.execute_reply":"2024-04-15T15:34:52.374482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nimport cv2\nimport tensorflow as tf\nfrom tensorflow.python.client import device_lib\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, accuracy_score\nimport itertools\n\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization\nfrom tensorflow.keras.optimizers import RMSprop, Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\n\nfrom tensorflow.keras.applications import densenet, efficientnet, resnet50, xception, inception_v3, vgg16\n\nsns.set(style='white', context='notebook', palette='pastel')\nnp.random.seed(42)\n\nIMSIZE = 224\nBATCH_SIZE = 16","metadata":{"_cell_guid":"f67b9393-8ea1-4e23-b856-2ce149cfe421","_execution_state":"idle","_uuid":"72334cb006d02a4bcfc2a2fe622524eba824c6f8","execution":{"iopub.status.busy":"2024-04-15T15:34:53.598745Z","iopub.execute_input":"2024-04-15T15:34:53.599427Z","iopub.status.idle":"2024-04-15T15:35:07.472033Z","shell.execute_reply.started":"2024-04-15T15:34:53.599396Z","shell.execute_reply":"2024-04-15T15:35:07.471238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_available_gpus():\n    local_device_protos = device_lib.list_local_devices()\n    print(local_device_protos)\n    return [x.name for x in local_device_protos if x.device_type == \"GPU\"]\n\nget_available_gpus()","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:36:29.385041Z","iopub.execute_input":"2024-04-15T15:36:29.386014Z","iopub.status.idle":"2024-04-15T15:36:29.686331Z","shell.execute_reply.started":"2024-04-15T15:36:29.385980Z","shell.execute_reply":"2024-04-15T15:36:29.685260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir(\"/kaggle/input/seefoodfhj-2024/\"))\n\nbase_path = \"/kaggle/input/seefoodfhj-2024/\"\ntrain_path = \"/kaggle/input/seefoodfhj-2024/train/\"\ntest_path = \"/kaggle/input/seefoodfhj-2024/test/\"\nextension = \".jpg\"\n\ntrain_ids_all = pd.read_csv(base_path+\"train.csv\")\ntest_ids_all = pd.read_csv(base_path+\"test.csv\")\n\nlabelnames = pd.read_csv(base_path+\"labelnames.csv\")\n\ndef plot_diag_hist(dataframe, title='NoTitle'):\n    f, ax = plt.subplots(figsize=(15, 4))\n    ax = sns.countplot(x=\"label\", data=dataframe, palette=\"GnBu_d\")\n    sns.despine()\n    plt.title(title)\n    plt.show()\n\nplot_diag_hist(train_ids_all, title=\"Labels Training Data\")\n\nprint(\"Shape of Training Data: {}\".format(train_ids_all.shape))\nprint(\"Shape of Test Data: {}\\n\".format(test_ids_all.shape))\n\ndef get_full_path_train(idcode):\n    return \"{}{}{}\".format(train_path,idcode,extension)\n\ndef get_full_path_test(idcode):\n    return \"{}{}{}\".format(test_path,idcode,extension)\n\n\ntrain_ids_all[\"path\"] = train_ids_all[\"id_code\"].apply(lambda x: get_full_path_train(x))\ntest_ids_all[\"path\"] = test_ids_all[\"id_code\"].apply(lambda x: get_full_path_test(x))","metadata":{"_cell_guid":"5e51d00e-62fd-4141-bf73-50ac4f2da7d0","_execution_state":"idle","_uuid":"84bbd5ab8d7895bd430d5ecfe2f7ddf77baa7b74","execution":{"iopub.status.busy":"2024-04-15T15:37:19.350503Z","iopub.execute_input":"2024-04-15T15:37:19.350859Z","iopub.status.idle":"2024-04-15T15:37:21.034347Z","shell.execute_reply.started":"2024-04-15T15:37:19.350831Z","shell.execute_reply":"2024-04-15T15:37:21.033429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labelnames.at[1,\"labelname\"]","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:37:24.360890Z","iopub.execute_input":"2024-04-15T15:37:24.361731Z","iopub.status.idle":"2024-04-15T15:37:24.368530Z","shell.execute_reply.started":"2024-04-15T15:37:24.361694Z","shell.execute_reply":"2024-04-15T15:37:24.367466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ids_all.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:37:24.902894Z","iopub.execute_input":"2024-04-15T15:37:24.903622Z","iopub.status.idle":"2024-04-15T15:37:24.917389Z","shell.execute_reply.started":"2024-04-15T15:37:24.903591Z","shell.execute_reply":"2024-04-15T15:37:24.916282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids_all.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:37:25.801328Z","iopub.execute_input":"2024-04-15T15:37:25.801962Z","iopub.status.idle":"2024-04-15T15:37:25.811588Z","shell.execute_reply.started":"2024-04-15T15:37:25.801931Z","shell.execute_reply":"2024-04-15T15:37:25.810527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image(image_path):\n    img = cv2.imread(image_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = cv2.resize(img, (IMSIZE, IMSIZE))\n    return img\n\ndef load_images_as_tensor(image_path, dtype=np.uint8):\n    data = load_image(image_path).reshape((IMSIZE*IMSIZE,1))\n    return data.flatten()\n\ndef show_image(image_path, figsize=None, title=None):\n    image = load_image(image_path)\n    if figsize is not None:\n        fig = plt.figure(figsize=figsize)\n    if image.ndim == 1:\n        plt.imshow(np.reshape(image, (IMSIZE,-1)),cmap='gray')\n    elif image.ndim == 2:\n        plt.imshow(image,cmap='gray')\n    elif image.ndim == 3:\n        if image.shape[2] == 1:\n            image = image[:,:,0]\n            plt.imshow(image,cmap='gray')\n        elif image.shape[2] == 3:\n            plt.imshow(image)\n        else:\n            print(\"Invalid image dimension\")\n    if title is not None:\n        plt.title(title)\n        \ndef show_image_tensor(image, figsize=None, title=None):\n    if figsize is not None:\n        fig = plt.figure(figsize=figsize)\n    if image.ndim == 1:\n        plt.imshow(np.reshape(image, (IMSIZE,-1)),cmap='gray')\n    elif image.ndim == 2:\n        plt.imshow(image,cmap='gray')\n    elif image.ndim == 3:\n        if image.shape[2] == 1:\n            image = image[:,:,0]\n            plt.imshow(image,cmap='gray')\n        elif image.shape[2] == 3:\n            plt.imshow(image)\n        else:\n            print(\"Invalid image dimension\")\n    if title is not None:\n        plt.title(title)\n        \ndef show_Nimages(image_filenames, classifications, scale=1):\n    N=len(image_filenames)\n    fig = plt.figure(figsize=(25/scale, 16/scale))\n    for i in range(N):\n        ax = fig.add_subplot(1, N, i + 1, xticks=[], yticks=[])\n        show_image(image_filenames[i], title=\"C:{}\".format(classifications[i]))\n        \ndef show_Nrandomimages(N=10):\n    indices = (np.random.rand(N)*train_ids_all.shape[0]).astype(int)\n    show_Nimages(train_ids_all[\"path\"][indices].values, train_ids_all[\"label\"][indices].values)\n    \ndef show_Nimages_of_class(classification=0, N=10):\n    print(\"{} images of class {} = {}\".format(N, classification, labelnames.at[classification,\"labelname\"]))\n    indices = train_ids_all[train_ids_all[\"label\"] == classification].sample(N).index\n    show_Nimages(train_ids_all[\"path\"][indices].values, train_ids_all[\"label\"][indices].values)\n    \ndef show_Nerrorimages(imgs, pred, true, delta_prob=[], scale=1):\n    N=len(imgs)\n    fig = plt.figure(figsize=(25/scale, 16/scale))\n    for i in range(N):\n        ax = fig.add_subplot(1, N, i + 1, xticks=[], yticks=[])\n        if (delta_prob!=[]):\n            show_image_tensor(imgs[i], title=\"P:{} T:{} d:{:.2f}\".format(pred[i], true[i], delta_prob[i]))\n        else:\n            show_image_tensor(imgs[i], title=\"P:{} T:{}\".format(pred[i], true[i]))","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:37:29.349269Z","iopub.execute_input":"2024-04-15T15:37:29.349621Z","iopub.status.idle":"2024-04-15T15:37:29.370999Z","shell.execute_reply.started":"2024-04-15T15:37:29.349594Z","shell.execute_reply":"2024-04-15T15:37:29.369869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_index = 2477\nshow_image(train_ids_all[\"path\"][test_index], title=\"Class = {}\".format(train_ids_all[\"label\"][test_index]))","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:37:31.279059Z","iopub.execute_input":"2024-04-15T15:37:31.279872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_Nrandomimages(10)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:37:54.273036Z","iopub.execute_input":"2024-04-15T15:37:54.273656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_Nimages_of_class(classification=43)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:38:20.233574Z","iopub.execute_input":"2024-04-15T15:38:20.233939Z","iopub.status.idle":"2024-04-15T15:38:21.295698Z","shell.execute_reply.started":"2024-04-15T15:38:20.233910Z","shell.execute_reply":"2024-04-15T15:38:21.294679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split Training and Validation Set","metadata":{}},{"cell_type":"code","source":"train_ids_all[:3]","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:38:27.909697Z","iopub.execute_input":"2024-04-15T15:38:27.910525Z","iopub.status.idle":"2024-04-15T15:38:27.920430Z","shell.execute_reply.started":"2024-04-15T15:38:27.910491Z","shell.execute_reply":"2024-04-15T15:38:27.919455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# only use a part of dataset for development, finally use all for training\ntrain_ids_all_working, train_ids_all_notused = train_test_split(train_ids_all, test_size=0.75, random_state=42, stratify=train_ids_all[['label']])\n#train_ids_all_working = train_ids_all","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:40:20.280168Z","iopub.execute_input":"2024-04-15T15:40:20.281092Z","iopub.status.idle":"2024-04-15T15:40:20.727250Z","shell.execute_reply.started":"2024-04-15T15:40:20.281058Z","shell.execute_reply":"2024-04-15T15:40:20.726412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, validation_df = train_test_split(train_ids_all_working, test_size=0.1)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:40:20.728741Z","iopub.execute_input":"2024-04-15T15:40:20.729036Z","iopub.status.idle":"2024-04-15T15:40:20.737679Z","shell.execute_reply.started":"2024-04-15T15:40:20.729011Z","shell.execute_reply":"2024-04-15T15:40:20.736904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import Data from Directory","metadata":{}},{"cell_type":"code","source":"from tqdm import tqdm\n\ndef load_training_data(image_filenames):\n    N = image_filenames.shape[0]\n    #N=100\n    train_X = np.zeros((N,IMSIZE,IMSIZE,3), dtype=np.float32)\n    for i in tqdm(range(N)):\n        img = load_image(image_filenames.iloc[i])\n        train_X[i] = np.array(img, np.float32)/255\n    return train_X","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:40:21.549035Z","iopub.execute_input":"2024-04-15T15:40:21.549779Z","iopub.status.idle":"2024-04-15T15:40:21.555440Z","shell.execute_reply.started":"2024-04-15T15:40:21.549745Z","shell.execute_reply":"2024-04-15T15:40:21.554364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X = load_training_data(train_df[\"path\"])\ntrain_y = train_df[\"label\"].values\nvalidation_X = load_training_data(validation_df[\"path\"])\nvalidation_y = validation_df[\"label\"].values","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:40:22.029745Z","iopub.execute_input":"2024-04-15T15:40:22.030082Z","iopub.status.idle":"2024-04-15T15:43:23.692373Z","shell.execute_reply.started":"2024-04-15T15:40:22.030049Z","shell.execute_reply":"2024-04-15T15:43:23.691472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_X.shape)\nprint(validation_X.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:55:39.184863Z","iopub.execute_input":"2024-04-15T15:55:39.185777Z","iopub.status.idle":"2024-04-15T15:55:39.190155Z","shell.execute_reply.started":"2024-04-15T15:55:39.185745Z","shell.execute_reply":"2024-04-15T15:55:39.189249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y = train_df[\"label\"].values\nvalidation_y = validation_df[\"label\"].values\n\n# Encode labels to one hot vectors\nprint(train_y.shape)\ntrain_y_cat = to_categorical(train_y, num_classes = 101)\nprint(train_y_cat.shape)\n\nprint(validation_y.shape)\nvalidation_y_cat = to_categorical(validation_y, num_classes = 101)\nprint(validation_y_cat.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:55:40.979732Z","iopub.execute_input":"2024-04-15T15:55:40.980621Z","iopub.status.idle":"2024-04-15T15:55:40.989564Z","shell.execute_reply.started":"2024-04-15T15:55:40.980586Z","shell.execute_reply":"2024-04-15T15:55:40.988550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image Data Generator (From Memory & From Hard Disk)","metadata":{}},{"cell_type":"markdown","source":"# Common Functions","metadata":{}},{"cell_type":"code","source":"def plot_nice_confusion_matrix(y_true, y_pred):\n    cm = confusion_matrix(y_true, y_pred)\n    fig, ax = plt.subplots(figsize=(18,18))\n    sns.heatmap(cm, annot=True, fmt='d', linewidths=.5,  cbar=False, ax=ax, cmap=plt.cm.copper)\n    plt.ylabel('true label')\n    plt.xlabel('predicted label')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:55:44.034056Z","iopub.execute_input":"2024-04-15T15:55:44.034411Z","iopub.status.idle":"2024-04-15T15:55:44.040419Z","shell.execute_reply.started":"2024-04-15T15:55:44.034383Z","shell.execute_reply":"2024-04-15T15:55:44.039320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# NN Definition","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import DenseNet121, InceptionV3, ResNet50, VGG16\nfrom tensorflow.keras.layers import GlobalAveragePooling2D\n\nreduceLR = ReduceLROnPlateau(\n    monitor='val_loss',\n    factor=0.5,\n    patience=7,\n    min_lr=1e-6,\n    verbose=1,\n    mode='min'\n)\n\nearlyStopping = EarlyStopping(\n    monitor='val_loss',\n    patience=10,\n    verbose=1,\n    mode='min',\n    restore_best_weights=True\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T15:55:48.327081Z","iopub.execute_input":"2024-04-15T15:55:48.327796Z","iopub.status.idle":"2024-04-15T15:55:48.333604Z","shell.execute_reply.started":"2024-04-15T15:55:48.327764Z","shell.execute_reply":"2024-04-15T15:55:48.332607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the optimizer\nmy_optimizer = RMSprop(learning_rate=1e-4)\n#my_optimizer = Adam(lr=1e-5)","metadata":{"_cell_guid":"a4c55409-6a65-400a-b5e8-a1dc535429c0","_execution_state":"idle","_uuid":"420c704367b397b8255fefe9d882b35ac8929b95"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# NN Training & Visualization","metadata":{}},{"cell_type":"code","source":"def plot_training_history(history):\n    history_df = pd.DataFrame(history.history)\n    f = plt.figure(figsize=(25,5))\n    ax = f.add_subplot(121)\n    ax.plot(history_df[\"loss\"], label=\"loss\")\n    ax.plot(history_df[\"val_loss\"], label = \"val_loss\")\n    ax.legend()\n    ax = f.add_subplot(122)\n    ax.plot(history_df[\"acc\"], label=\"acc\")\n    ax.plot(history_df[\"val_acc\"], label=\"val_acc\")\n    ax.legend()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate on Validation Data","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict Test Data & Submit","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#define test generator \"test_data_generator\"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_y_pred_proba = model.predict_generator(test_data_generator, \n                                            steps=np.ceil(float(test_ids_all.shape[0]) / float(BATCH_SIZE)),\n                                            verbose=1)\n\ntest_y_pred = np.argmax(test_y_pred_proba, axis=1)\nnn_results = pd.Series(test_y_pred,name=\"label\")\nsubmission = pd.concat([test_ids_all[\"id_code\"],nn_results], axis = 1)\n\nsubmission.to_csv(\"food_submission_simple_model.csv\",index=False)\n\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}