{"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":"code","source":"import pandas as pd\nimport numpy as np\nimport json\nimport os\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport json\nimport PIL\nimport skimage.io\nimport seaborn as sn\nfrom collections import Counter\nimport tensorflow as tf\nimport collections\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow import keras\nimport tensorflow as tf\nfrom tensorflow.keras import layers, Sequential\nfrom tensorflow.keras.layers import Conv2D\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.layers import MaxPooling2D\nfrom tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.layers import Activation\nfrom tensorflow.keras.layers import Dropout\nfrom tensorflow.keras.layers import Flatten\nfrom tensorflow.keras.layers import Input\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import BatchNormalization\nfrom tensorflow.keras.layers import AveragePooling2D\nfrom tensorflow.keras.layers import GlobalAveragePooling2D","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:05.386605Z","iopub.execute_input":"2022-02-27T10:27:05.387394Z","iopub.status.idle":"2022-02-27T10:27:11.241223Z","shell.execute_reply.started":"2022-02-27T10:27:05.387263Z","shell.execute_reply":"2022-02-27T10:27:11.240374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device_name = tf.test.gpu_device_name()\nif \"GPU\" not in device_name:\n    print(\"GPU device not found\")\nprint('Found GPU at: {}'.format(device_name))","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:11.24411Z","iopub.execute_input":"2022-02-27T10:27:11.245018Z","iopub.status.idle":"2022-02-27T10:27:13.136534Z","shell.execute_reply.started":"2022-02-27T10:27:11.244974Z","shell.execute_reply":"2022-02-27T10:27:13.13511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This part is inspired from https://www.kaggle.com/mpwolke/kaggle-herbarium-2022-long-tailed-learning","metadata":{}},{"cell_type":"code","source":"\nTRAIN_DIR = \"../input/herbarium-2022-fgvc9/train_images/\"\nTEST_DIR = \"../input/herbarium-2022-fgvc9/test_images/\"\n\nwith open(\"../input/herbarium-2022-fgvc9/train_metadata.json\") as json_file:\n    train_meta = json.load(json_file)\nwith open(\"../input/herbarium-2022-fgvc9/test_metadata.json\") as json_file:\n    test_meta = json.load(json_file)","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:13.138193Z","iopub.execute_input":"2022-02-27T10:27:13.138721Z","iopub.status.idle":"2022-02-27T10:27:26.348817Z","shell.execute_reply.started":"2022-02-27T10:27:13.138681Z","shell.execute_reply":"2022-02-27T10:27:26.347964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploration","metadata":{}},{"cell_type":"code","source":"train_meta.keys()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:26.350393Z","iopub.execute_input":"2022-02-27T10:27:26.35069Z","iopub.status.idle":"2022-02-27T10:27:26.359222Z","shell.execute_reply.started":"2022-02-27T10:27:26.350654Z","shell.execute_reply":"2022-02-27T10:27:26.358415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('the number of images in the training set : ', len(train_meta['images']))\nprint('the number of annotations in the training set :', len(train_meta['annotations']))","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:26.361414Z","iopub.execute_input":"2022-02-27T10:27:26.361703Z","iopub.status.idle":"2022-02-27T10:27:26.370474Z","shell.execute_reply.started":"2022-02-27T10:27:26.361666Z","shell.execute_reply":"2022-02-27T10:27:26.369627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('The total number of classes : ', len(set([annotation[\"category_id\"] for annotation in train_meta[\"annotations\"]])))","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:26.372578Z","iopub.execute_input":"2022-02-27T10:27:26.37284Z","iopub.status.idle":"2022-02-27T10:27:26.460133Z","shell.execute_reply.started":"2022-02-27T10:27:26.372807Z","shell.execute_reply":"2022-02-27T10:27:26.458625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\ncategories = []\npaths = []\n\nfor annotation, image in zip(train_meta[\"annotations\"], train_meta[\"images\"]):\n    #assert annotation[\"image_id\"] == image[\"id\"]\n    ids.append(image[\"image_id\"]) #Read above print metadata samples from each key\n    categories.append(annotation[\"category_id\"])\n    paths.append(image[\"file_name\"])\n        \ndf_meta = pd.DataFrame({\"id\": ids, \"category\": categories, \"path\": paths})","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:26.461411Z","iopub.execute_input":"2022-02-27T10:27:26.461773Z","iopub.status.idle":"2022-02-27T10:27:27.358991Z","shell.execute_reply.started":"2022-02-27T10:27:26.461731Z","shell.execute_reply":"2022-02-27T10:27:27.358214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_meta","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:27.360512Z","iopub.execute_input":"2022-02-27T10:27:27.36077Z","iopub.status.idle":"2022-02-27T10:27:27.432653Z","shell.execute_reply.started":"2022-02-27T10:27:27.360735Z","shell.execute_reply":"2022-02-27T10:27:27.431696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_meta['category'].value_counts()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:27.43425Z","iopub.execute_input":"2022-02-27T10:27:27.434541Z","iopub.status.idle":"2022-02-27T10:27:27.45509Z","shell.execute_reply.started":"2022-02-27T10:27:27.434502Z","shell.execute_reply":"2022-02-27T10:27:27.454098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"unbalanced classes !","metadata":{"editable":false}},{"cell_type":"code","source":"d_categories = {category[\"category_id\"]: category[\"scientificName\"] for category in train_meta[\"categories\"]}\nd_families = {category[\"category_id\"]: category[\"family\"] for category in train_meta[\"categories\"]}\nd_genus = {category[\"category_id\"]: category[\"genus\"] for category in train_meta[\"categories\"]}\nd_species = {category[\"category_id\"]: category[\"species\"] for category in train_meta[\"categories\"]}\n\ndf_meta[\"category_name\"] = df_meta[\"category\"].map(d_categories)\ndf_meta[\"family_name\"] = df_meta[\"category\"].map(d_families)\ndf_meta[\"genus_name\"] = df_meta[\"category\"].map(d_genus)\ndf_meta[\"species_name\"] = df_meta[\"category\"].map(d_species)\ndf_meta","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:27.456222Z","iopub.execute_input":"2022-02-27T10:27:27.457039Z","iopub.status.idle":"2022-02-27T10:27:27.569951Z","shell.execute_reply.started":"2022-02-27T10:27:27.45699Z","shell.execute_reply":"2022-02-27T10:27:27.569223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_train_batch(paths, categories, families, genus, species):\n    plt.figure(figsize=(16, 16))\n    \n    for ind, info in enumerate(zip(paths, categories, families, genus, species)):\n        path, category, family, genus, species = info\n        \n        plt.subplot(2, 3, ind + 1)\n        \n        image = cv2.imread(os.path.join(\"../input/herbarium-2022-fgvc9/train_images\", path))\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n        plt.imshow(image)\n        \n        plt.title(\n            f\"FAMILY: {family} GENUS: {genus}\\n{species}\", \n            fontsize=10,\n        )\n        plt.axis(\"off\")\n    \n    plt.show()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:27.571288Z","iopub.execute_input":"2022-02-27T10:27:27.571612Z","iopub.status.idle":"2022-02-27T10:27:27.578953Z","shell.execute_reply.started":"2022-02-27T10:27:27.571571Z","shell.execute_reply":"2022-02-27T10:27:27.577926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_by_id(df, _id=None):\n    tmp = df.sample(6)\n    if _id is not None:\n        tmp = df[df[\"category\"] == _id].sample(6)\n\n    visualize_train_batch(\n        tmp[\"path\"].tolist(), \n        tmp[\"category_name\"].tolist(),\n        tmp[\"family_name\"].tolist(),\n        tmp[\"genus_name\"].tolist(),\n        tmp[\"species_name\"].tolist(),\n    )","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:27.58046Z","iopub.execute_input":"2022-02-27T10:27:27.581017Z","iopub.status.idle":"2022-02-27T10:27:27.589056Z","shell.execute_reply.started":"2022-02-27T10:27:27.580979Z","shell.execute_reply":"2022-02-27T10:27:27.588219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(4):\n    visualize_by_id(df_meta, i)\n    ","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:27.589945Z","iopub.execute_input":"2022-02-27T10:27:27.59014Z","iopub.status.idle":"2022-02-27T10:27:33.23485Z","shell.execute_reply.started":"2022-02-27T10:27:27.590109Z","shell.execute_reply":"2022-02-27T10:27:33.23347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_meta","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:33.239159Z","iopub.execute_input":"2022-02-27T10:27:33.240139Z","iopub.status.idle":"2022-02-27T10:27:33.263248Z","shell.execute_reply.started":"2022-02-27T10:27:33.240093Z","shell.execute_reply":"2022-02-27T10:27:33.262322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing","metadata":{"editable":false}},{"cell_type":"code","source":"img = os.path.join(\"../input/herbarium-2022-fgvc9/train_images\", '000/00/00000__001.jpg')","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:33.264797Z","iopub.execute_input":"2022-02-27T10:27:33.265468Z","iopub.status.idle":"2022-02-27T10:27:33.272572Z","shell.execute_reply.started":"2022-02-27T10:27:33.265416Z","shell.execute_reply":"2022-02-27T10:27:33.271501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv2.imread(img)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:33.273732Z","iopub.execute_input":"2022-02-27T10:27:33.274752Z","iopub.status.idle":"2022-02-27T10:27:33.300327Z","shell.execute_reply.started":"2022-02-27T10:27:33.27471Z","shell.execute_reply":"2022-02-27T10:27:33.299495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv2.imread(img).shape","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:33.301992Z","iopub.execute_input":"2022-02-27T10:27:33.302268Z","iopub.status.idle":"2022-02-27T10:27:33.319576Z","shell.execute_reply.started":"2022-02-27T10:27:33.302233Z","shell.execute_reply":"2022-02-27T10:27:33.318917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(img)\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nw =299\nh =299\nres_img = cv2.resize(img, (w,h), interpolation = cv2.INTER_AREA)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:33.320669Z","iopub.execute_input":"2022-02-27T10:27:33.321304Z","iopub.status.idle":"2022-02-27T10:27:33.3456Z","shell.execute_reply.started":"2022-02-27T10:27:33.321266Z","shell.execute_reply":"2022-02-27T10:27:33.344919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_img.shape","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:33.348715Z","iopub.execute_input":"2022-02-27T10:27:33.349221Z","iopub.status.idle":"2022-02-27T10:27:33.355821Z","shell.execute_reply.started":"2022-02-27T10:27:33.349188Z","shell.execute_reply":"2022-02-27T10:27:33.355037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(16,6))\nplt.subplot(1, 2, 1)\nplt.imshow(img)\nplt.title(\"original size : {}\".format(img.shape))\nplt.subplot(1, 2, 2)\n\nplt.imshow(res_img)\nplt.title(\"Resized shape : {}\".format(res_img.shape))\nplt.suptitle(\"Changing the image size\", fontsize=20)\nplt.show()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:33.357323Z","iopub.execute_input":"2022-02-27T10:27:33.357785Z","iopub.status.idle":"2022-02-27T10:27:33.815669Z","shell.execute_reply.started":"2022-02-27T10:27:33.357746Z","shell.execute_reply":"2022-02-27T10:27:33.814989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_yuv = cv2.cvtColor(img,cv2.COLOR_BGR2YUV)\nplt.figure(figsize = (16,6))\nplt.subplot(1, 2, 1)\n\nplt.hist(img_yuv.flatten(), bins=range(256))\nplt.title('Luminance distribution')\nplt.subplot(1, 2, 2)\nplt.imshow(img_yuv)\n\nplt.show()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:33.816991Z","iopub.execute_input":"2022-02-27T10:27:33.81741Z","iopub.status.idle":"2022-02-27T10:27:34.619755Z","shell.execute_reply.started":"2022-02-27T10:27:33.81737Z","shell.execute_reply":"2022-02-27T10:27:34.619005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Equalization\nimg_yuv[:, : ,0] = cv2.equalizeHist(img_yuv[:, : ,0])\nimg_equ = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2RGB)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:34.621152Z","iopub.execute_input":"2022-02-27T10:27:34.62158Z","iopub.status.idle":"2022-02-27T10:27:34.634124Z","shell.execute_reply.started":"2022-02-27T10:27:34.621541Z","shell.execute_reply":"2022-02-27T10:27:34.633113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(16,6))\nplt.subplot(1, 2,1)\nplt.hist(img_equ.flatten(), bins=range(256))\nplt.title('Luminance distribution after equalization')\nplt.subplot(1, 2, 2)\n\nplt.imshow(img_equ[:, : , ::-1])\nplt.title('Image after equalization')\nplt.show()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:34.637229Z","iopub.execute_input":"2022-02-27T10:27:34.637655Z","iopub.status.idle":"2022-02-27T10:27:35.438948Z","shell.execute_reply.started":"2022-02-27T10:27:34.637617Z","shell.execute_reply":"2022-02-27T10:27:35.438241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rotation_range=30,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n     zoom_range=0.2,\n    shear_range=0.1,\n    horizontal_flip=True,\n    fill_mode='nearest', cval = 125)\n\nx = img\nx = x.reshape((1,) + x.shape)\n\naug_ = datagen.flow(x)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:35.440098Z","iopub.execute_input":"2022-02-27T10:27:35.440462Z","iopub.status.idle":"2022-02-27T10:27:35.450683Z","shell.execute_reply.started":"2022-02-27T10:27:35.440416Z","shell.execute_reply":"2022-02-27T10:27:35.449814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aug_images = [next(aug_)[0].astype(np.uint8) for i in range(12)]\n\nfig, axes = plt.subplots(3,4,figsize= (20,20))\naxes = axes.flatten()\nfor ig, ax in zip(aug_images,axes):\n    ax.imshow(ig)\n    ax.axis('off')\n    \nplt.suptitle(\"Data augmentation\",fontsize=24)\nplt.show()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:35.451952Z","iopub.execute_input":"2022-02-27T10:27:35.452315Z","iopub.status.idle":"2022-02-27T10:27:38.619034Z","shell.execute_reply.started":"2022-02-27T10:27:35.452277Z","shell.execute_reply":"2022-02-27T10:27:38.618308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_cnn(categories, num_categories, w,h):\n    \n    \"\"\"\"\n    Preprocessing images for neural networks\n    \"\"\"\n    \"\"\"\n    params : categories and num_categories for fils to be processed\n    w =  width of the image\n    h = height of the image\n    \n    \"\"\"\n    \n    list_img = []\n    labels = []\n    for cat,num_cat in zip(categories, num_categories):\n        for ig in os.listdir(os.path.join(\"../input/herbarium-2022-fgvc9/train_images\", cat, num_cat)) :\n            img = cv2.imread(os.path.join(\"../input/herbarium-2022-fgvc9/train_images\", cat, num_cat, ig)) # read image\n            img = cv2.resize(img, (w,h), interpolation=cv2.INTER_LINEAR) # resize image\n            #equilization\n            img_yuv = cv2.cvtColor(img,cv2.COLOR_RGB2YUV)\n            img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0])\n            img_equ = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2RGB)\n\n            list_img.append(img_equ)\n            labels.append(ig.split('__')[0])\n        \n    return list_img, labels\n              ","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:38.620631Z","iopub.execute_input":"2022-02-27T10:27:38.621477Z","iopub.status.idle":"2022-02-27T10:27:38.632064Z","shell.execute_reply.started":"2022-02-27T10:27:38.621419Z","shell.execute_reply":"2022-02-27T10:27:38.631421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_meta['cat_first'] = df_meta['path'].apply(lambda x : x.split('/')[0])\ndf_meta['cat_second'] = df_meta['path'].apply(lambda x : x.split('/')[1])","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:38.633638Z","iopub.execute_input":"2022-02-27T10:27:38.634457Z","iopub.status.idle":"2022-02-27T10:27:39.654563Z","shell.execute_reply.started":"2022-02-27T10:27:38.634399Z","shell.execute_reply":"2022-02-27T10:27:39.653736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_meta.head()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:39.656422Z","iopub.execute_input":"2022-02-27T10:27:39.657004Z","iopub.status.idle":"2022-02-27T10:27:39.673605Z","shell.execute_reply.started":"2022-02-27T10:27:39.656963Z","shell.execute_reply":"2022-02-27T10:27:39.67275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We will train the model on only the 20 most known classes.","metadata":{"editable":false}},{"cell_type":"code","source":"index_most = df_meta['category'].value_counts().head(20).index.tolist()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:39.675162Z","iopub.execute_input":"2022-02-27T10:27:39.675446Z","iopub.status.idle":"2022-02-27T10:27:39.688474Z","shell.execute_reply.started":"2022-02-27T10:27:39.675395Z","shell.execute_reply":"2022-02-27T10:27:39.687616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_meta['category_name'].value_counts().head(20).index","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:39.690101Z","iopub.execute_input":"2022-02-27T10:27:39.690676Z","iopub.status.idle":"2022-02-27T10:27:39.794553Z","shell.execute_reply.started":"2022-02-27T10:27:39.690587Z","shell.execute_reply":"2022-02-27T10:27:39.793668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_meta_most = df_meta[df_meta['category'].isin(index_most)]","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:39.796201Z","iopub.execute_input":"2022-02-27T10:27:39.796514Z","iopub.status.idle":"2022-02-27T10:27:40.040875Z","shell.execute_reply.started":"2022-02-27T10:27:39.796476Z","shell.execute_reply":"2022-02-27T10:27:40.039958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_meta_most['category'].unique()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:40.042479Z","iopub.execute_input":"2022-02-27T10:27:40.042772Z","iopub.status.idle":"2022-02-27T10:27:40.446913Z","shell.execute_reply.started":"2022-02-27T10:27:40.042733Z","shell.execute_reply":"2022-02-27T10:27:40.446009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_meta_most['cat_first'].unique()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:40.448726Z","iopub.execute_input":"2022-02-27T10:27:40.449288Z","iopub.status.idle":"2022-02-27T10:27:40.459107Z","shell.execute_reply.started":"2022-02-27T10:27:40.449247Z","shell.execute_reply":"2022-02-27T10:27:40.458297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_first_ = ['002', '009', '011', '011', '027', '028', '028', '040', '046', '046', '087', '088','088', '100', '108', '109', '125', '125', '125', '125']","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:40.46217Z","iopub.execute_input":"2022-02-27T10:27:40.462369Z","iopub.status.idle":"2022-02-27T10:27:40.469169Z","shell.execute_reply.started":"2022-02-27T10:27:40.462344Z","shell.execute_reply":"2022-02-27T10:27:40.468386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(cat_first_)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:40.470695Z","iopub.execute_input":"2022-02-27T10:27:40.470971Z","iopub.status.idle":"2022-02-27T10:27:40.478315Z","shell.execute_reply.started":"2022-02-27T10:27:40.470933Z","shell.execute_reply":"2022-02-27T10:27:40.477402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_meta_most['cat_second'].unique()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:40.480332Z","iopub.execute_input":"2022-02-27T10:27:40.48067Z","iopub.status.idle":"2022-02-27T10:27:40.489688Z","shell.execute_reply.started":"2022-02-27T10:27:40.480631Z","shell.execute_reply":"2022-02-27T10:27:40.487888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_meta_most['cat_second'].unique().shape","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:40.491278Z","iopub.execute_input":"2022-02-27T10:27:40.491598Z","iopub.status.idle":"2022-02-27T10:27:40.498288Z","shell.execute_reply.started":"2022-02-27T10:27:40.491559Z","shell.execute_reply":"2022-02-27T10:27:40.497491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"X, y = preprocess_cnn(cat_first_, df_meta_most['cat_second'].unique(), 299,299)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:27:40.499907Z","iopub.execute_input":"2022-02-27T10:27:40.500477Z","iopub.status.idle":"2022-02-27T10:28:04.345605Z","shell.execute_reply.started":"2022-02-27T10:27:40.500414Z","shell.execute_reply":"2022-02-27T10:28:04.344745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.array(X)\ny = np.array(y)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:28:04.349085Z","iopub.execute_input":"2022-02-27T10:28:04.349317Z","iopub.status.idle":"2022-02-27T10:28:04.498721Z","shell.execute_reply.started":"2022-02-27T10:28:04.34929Z","shell.execute_reply":"2022-02-27T10:28:04.497891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('the number of processed images :', len(y))","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:28:04.500512Z","iopub.execute_input":"2022-02-27T10:28:04.500943Z","iopub.status.idle":"2022-02-27T10:28:04.506115Z","shell.execute_reply.started":"2022-02-27T10:28:04.500907Z","shell.execute_reply":"2022-02-27T10:28:04.505361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[:5]","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:28:04.511893Z","iopub.execute_input":"2022-02-27T10:28:04.512862Z","iopub.status.idle":"2022-02-27T10:28:04.519233Z","shell.execute_reply.started":"2022-02-27T10:28:04.512822Z","shell.execute_reply":"2022-02-27T10:28:04.518391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#we mix categories\n\nl = np.arange(len(y))\nnp.random.seed(42)\nnp.random.shuffle(l)\n\nX = X[l]\ny = y[l]","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:28:04.520812Z","iopub.execute_input":"2022-02-27T10:28:04.521143Z","iopub.status.idle":"2022-02-27T10:28:04.654944Z","shell.execute_reply.started":"2022-02-27T10:28:04.521106Z","shell.execute_reply":"2022-02-27T10:28:04.653978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[:5]","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:28:04.656282Z","iopub.execute_input":"2022-02-27T10:28:04.656605Z","iopub.status.idle":"2022-02-27T10:28:04.66284Z","shell.execute_reply.started":"2022-02-27T10:28:04.656562Z","shell.execute_reply":"2022-02-27T10:28:04.661816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.astype(int)\nencoder = LabelEncoder()\ny = encoder.fit_transform(y)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:28:04.664596Z","iopub.execute_input":"2022-02-27T10:28:04.665095Z","iopub.status.idle":"2022-02-27T10:28:05.495219Z","shell.execute_reply.started":"2022-02-27T10:28:04.665051Z","shell.execute_reply":"2022-02-27T10:28:05.494423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split train and test\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, stratify= y,random_state = 42)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:28:05.496582Z","iopub.execute_input":"2022-02-27T10:28:05.496847Z","iopub.status.idle":"2022-02-27T10:28:06.475385Z","shell.execute_reply.started":"2022-02-27T10:28:05.496812Z","shell.execute_reply":"2022-02-27T10:28:06.47456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Size of training set', X_train.shape[0])\nprint('Size of test set', X_test.shape[0])","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:28:06.476673Z","iopub.execute_input":"2022-02-27T10:28:06.477507Z","iopub.status.idle":"2022-02-27T10:28:06.484231Z","shell.execute_reply.started":"2022-02-27T10:28:06.477465Z","shell.execute_reply":"2022-02-27T10:28:06.483261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import backend as K\n\ndef recall_m(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    recall = true_positives / (possible_positives + K.epsilon())\n    return recall\n\ndef precision_m(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    return precision\n\ndef f1_m(y_true, y_pred):\n    precision = precision_m(y_true, y_pred)\n    recall = recall_m(y_true, y_pred)\n    return 2*((precision*recall)/(precision+recall+K.epsilon())) ","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:28:06.485394Z","iopub.execute_input":"2022-02-27T10:28:06.48594Z","iopub.status.idle":"2022-02-27T10:28:06.497309Z","shell.execute_reply.started":"2022-02-27T10:28:06.485902Z","shell.execute_reply":"2022-02-27T10:28:06.496603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\n#1er block\nmodel.add(Conv2D(filters=16,kernel_size=(3,3), padding='same',use_bias=False,input_shape=(299,299,3),\n         activation = 'relu'))\nmodel.add(Conv2D(filters=16,kernel_size=(3,3), padding='same',use_bias=False,input_shape=(299,299,3),\n         activation = 'relu'))\nmodel.add(BatchNormalization(scale=False))\nmodel.add(MaxPooling2D(pool_size=(4, 4),strides=(4, 4),padding='same'))\nmodel.add(Dropout(0.25))\n\n# 2eme block\nmodel.add(Conv2D(filters=32,kernel_size=(3,3), padding='same',use_bias=False, activation ='relu'))\nmodel.add(Conv2D(filters=32,kernel_size=(3,3), padding='same',use_bias=False, activation ='relu'))\nmodel.add(BatchNormalization(scale=False))\nmodel.add(MaxPooling2D(pool_size=(4, 4),strides=(4, 4),padding='same'))\nmodel.add(Dropout(0.25))\n\n# 3eme block\nmodel.add(Conv2D(filters=64, kernel_size=(3,3), padding='same', use_bias=False, activation ='relu'))\nmodel.add(Conv2D(filters=64, kernel_size=(3,3), padding='same', use_bias=False, activation ='relu'))\nmodel.add(BatchNormalization(axis=3, scale=False))\nmodel.add(Flatten())\n\n\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(len(cat_first_), activation = 'softmax'))","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:28:06.500251Z","iopub.execute_input":"2022-02-27T10:28:06.500483Z","iopub.status.idle":"2022-02-27T10:28:06.954041Z","shell.execute_reply.started":"2022-02-27T10:28:06.500453Z","shell.execute_reply":"2022-02-27T10:28:06.952232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:28:06.955187Z","iopub.execute_input":"2022-02-27T10:28:06.955425Z","iopub.status.idle":"2022-02-27T10:28:06.969626Z","shell.execute_reply.started":"2022-02-27T10:28:06.955388Z","shell.execute_reply":"2022-02-27T10:28:06.968656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', \n  loss='sparse_categorical_crossentropy',\n  metrics=['accuracy', f1_m])","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:28:06.97093Z","iopub.execute_input":"2022-02-27T10:28:06.971706Z","iopub.status.idle":"2022-02-27T10:28:06.986788Z","shell.execute_reply.started":"2022-02-27T10:28:06.971674Z","shell.execute_reply":"2022-02-27T10:28:06.986004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=30,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    shear_range=0.1,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest',\n    validation_split=0.2)\n\n\n#validation and test\ntest_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:28:06.988341Z","iopub.execute_input":"2022-02-27T10:28:06.988635Z","iopub.status.idle":"2022-02-27T10:28:06.994081Z","shell.execute_reply.started":"2022-02-27T10:28:06.988599Z","shell.execute_reply":"2022-02-27T10:28:06.993054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_cnn = model.fit(train_datagen.flow(X_train, y_train,batch_size=16,subset='training'),\n    validation_data= train_datagen.flow(X_train, y_train,batch_size=8,subset='validation'),\n    batch_size=32, epochs=20,verbose=2)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:28:06.995718Z","iopub.execute_input":"2022-02-27T10:28:06.996219Z","iopub.status.idle":"2022-02-27T10:34:55.487242Z","shell.execute_reply.started":"2022-02-27T10:28:06.996182Z","shell.execute_reply":"2022-02-27T10:34:55.485133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize =(13,5))\nplt.plot(history_cnn.history['accuracy'])\nplt.plot(history_cnn.history['val_accuracy'], color=\"orange\")\nplt.title('CNN model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-02-27T10:34:55.492472Z","iopub.execute_input":"2022-02-27T10:34:55.493234Z","iopub.status.idle":"2022-02-27T10:34:55.733029Z","shell.execute_reply.started":"2022-02-27T10:34:55.493195Z","shell.execute_reply":"2022-02-27T10:34:55.732307Z"},"trusted":true},"execution_count":null,"outputs":[]}]}