{"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":"<h1>CutMix: Regularization Strategy to Train Strong Classifiers\nwith Localizable Features</h1>\n\nThrough this notebook we will try to understand CutMix. \n\nLink of the paper \n* https://arxiv.org/pdf/1905.04899.pdf","metadata":{}},{"cell_type":"markdown","source":"# Import Library","metadata":{"_uuid":"fe76d1d1ded592430e7548feacfa38dc42f085d9"}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom tqdm.auto import tqdm\nfrom glob import glob\nimport time, gc\nimport cv2\n\nfrom tensorflow import keras\nimport matplotlib.image as mpimg\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Model\nfrom keras.models import clone_model\nfrom keras.layers import Dense,Conv2D,Flatten,MaxPool2D,Dropout,BatchNormalization, Input\nfrom keras.optimizers import Adam, RMSprop\nfrom keras.callbacks import ReduceLROnPlateau\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport PIL.Image as Image, PIL.ImageDraw as ImageDraw, PIL.ImageFont as ImageFont\nfrom matplotlib import pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-11-08T17:43:03.948166Z","iopub.execute_input":"2022-11-08T17:43:03.948410Z","iopub.status.idle":"2022-11-08T17:43:06.046659Z","shell.execute_reply.started":"2022-11-08T17:43:03.948361Z","shell.execute_reply":"2022-11-08T17:43:06.045517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nfrom keras.preprocessing.image import ImageDataGenerator, load_img\nfrom keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nimport os\nprint(os.listdir(\"../input\"))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-08T17:43:06.047980Z","iopub.execute_input":"2022-11-08T17:43:06.048263Z","iopub.status.idle":"2022-11-08T17:43:06.059626Z","shell.execute_reply.started":"2022-11-08T17:43:06.048193Z","shell.execute_reply":"2022-11-08T17:43:06.058768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\n\nzip_files = ['test1', 'train']\n# Will unzip the files so that you can see them..\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"../input/{}.zip\".format(zip_file),\"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))","metadata":{"execution":{"iopub.status.busy":"2022-11-08T17:43:06.060850Z","iopub.execute_input":"2022-11-08T17:43:06.061441Z","iopub.status.idle":"2022-11-08T17:43:24.069869Z","shell.execute_reply.started":"2022-11-08T17:43:06.061177Z","shell.execute_reply":"2022-11-08T17:43:24.068998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2022-11-08T17:45:43.522835Z","iopub.execute_input":"2022-11-08T17:45:43.523131Z","iopub.status.idle":"2022-11-08T17:45:44.456932Z","shell.execute_reply.started":"2022-11-08T17:45:43.523079Z","shell.execute_reply":"2022-11-08T17:45:44.456095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pwd","metadata":{"execution":{"iopub.status.busy":"2022-11-08T17:45:44.458581Z","iopub.execute_input":"2022-11-08T17:45:44.458858Z","iopub.status.idle":"2022-11-08T17:45:44.466785Z","shell.execute_reply.started":"2022-11-08T17:45:44.458812Z","shell.execute_reply":"2022-11-08T17:45:44.466055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('../input'))","metadata":{"execution":{"iopub.status.busy":"2022-11-08T17:45:44.468031Z","iopub.execute_input":"2022-11-08T17:45:44.468560Z","iopub.status.idle":"2022-11-08T17:45:44.478578Z","shell.execute_reply.started":"2022-11-08T17:45:44.468513Z","shell.execute_reply":"2022-11-08T17:45:44.477959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define Constants","metadata":{}},{"cell_type":"code","source":"FAST_RUN = False\nIMAGE_WIDTH=128\nIMAGE_HEIGHT=128\nIMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)\nIMAGE_CHANNELS=3","metadata":{"execution":{"iopub.status.busy":"2022-11-08T17:45:46.650937Z","iopub.execute_input":"2022-11-08T17:45:46.651428Z","iopub.status.idle":"2022-11-08T17:45:46.658021Z","shell.execute_reply.started":"2022-11-08T17:45:46.651191Z","shell.execute_reply":"2022-11-08T17:45:46.656992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare Traning Data","metadata":{"_uuid":"7335a579cc0268fba5d34d6f7558f33c187eedb3"}},{"cell_type":"code","source":"filenames = os.listdir(\"train\")\ncategories = []\nfor filename in filenames:\n    category = filename.split('.')[0]\n    if category == 'dog':\n        categories.append(1)\n    else:\n        categories.append(0)\n\ndf = pd.DataFrame({\n    'filename': filenames,\n    'category': categories\n})","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2022-11-08T17:45:47.691451Z","iopub.execute_input":"2022-11-08T17:45:47.691766Z","iopub.status.idle":"2022-11-08T17:45:47.739139Z","shell.execute_reply.started":"2022-11-08T17:45:47.691708Z","shell.execute_reply":"2022-11-08T17:45:47.738262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"_uuid":"915bb9ba7063ab4d5c07c542419ae119003a5f98","execution":{"iopub.status.busy":"2022-11-08T17:45:48.192134Z","iopub.execute_input":"2022-11-08T17:45:48.192473Z","iopub.status.idle":"2022-11-08T17:45:48.216238Z","shell.execute_reply.started":"2022-11-08T17:45:48.192419Z","shell.execute_reply":"2022-11-08T17:45:48.215040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.tail()","metadata":{"_uuid":"72bf69e817f67f5a2eaff8561217e22077248553","execution":{"iopub.status.busy":"2022-11-08T17:45:48.755701Z","iopub.execute_input":"2022-11-08T17:45:48.755991Z","iopub.status.idle":"2022-11-08T17:45:48.771478Z","shell.execute_reply.started":"2022-11-08T17:45:48.755938Z","shell.execute_reply":"2022-11-08T17:45:48.770503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### See Total In count","metadata":{"_uuid":"a999484fc35b73373fafe2253ae9db7ff46fdb90"}},{"cell_type":"code","source":"df['category'].value_counts().plot.bar()","metadata":{"_uuid":"fa26f0bc7a6d835a24989790b20f3c6f32946f45","execution":{"iopub.status.busy":"2022-11-08T17:45:49.759809Z","iopub.execute_input":"2022-11-08T17:45:49.760147Z","iopub.status.idle":"2022-11-08T17:45:50.025200Z","shell.execute_reply.started":"2022-11-08T17:45:49.760093Z","shell.execute_reply":"2022-11-08T17:45:50.024343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From our data we have 12000 cats and 12000 dogs","metadata":{"_uuid":"3a08da58107777a1dd05c4a4bf5c484484923cac"}},{"cell_type":"markdown","source":"# See sample image","metadata":{"_uuid":"400a293df3c8499059d9175f3915187074efd971"}},{"cell_type":"code","source":"sample = random.choice(filenames)\nimage = load_img(\"train/\"+sample)\nplt.imshow(image)","metadata":{"_uuid":"602b40f7353871cb161c60b5237f0da0096b2f47","execution":{"iopub.status.busy":"2022-11-08T17:45:55.808288Z","iopub.execute_input":"2022-11-08T17:45:55.808590Z","iopub.status.idle":"2022-11-08T17:45:56.123943Z","shell.execute_reply.started":"2022-11-08T17:45:55.808540Z","shell.execute_reply":"2022-11-08T17:45:56.123128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build Model\n\n<img src=\"https://i.imgur.com/ebkMGGu.jpg\" width=\"100%\"/>","metadata":{"_uuid":"b244e6b7715a04fc6df92dd6dfa3d35c473ca600"}},{"cell_type":"markdown","source":"* **Input Layer**: It represent input image data. It will reshape image into single diminsion array. Example your image is 64x64 = 4096, it will convert to (4096,1) array.\n* **Conv Layer**: This layer will extract features from image.\n* **Pooling Layer**: This layerreduce the spatial volume of input image after convolution.\n* **Fully Connected Layer**: It connect the network from a layer to another layer\n* **Output Layer**: It is the predicted values layer. ","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization\n\nmodel = Sequential()\n\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape=(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(2, activation='softmax')) # 2 because we have cat and dog classes\n\nmodel.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])\n\nmodel.summary()","metadata":{"_uuid":"8c9f833c1441b657c779844912d0b8028218d454","execution":{"iopub.status.busy":"2022-11-08T17:46:00.705033Z","iopub.execute_input":"2022-11-08T17:46:00.705362Z","iopub.status.idle":"2022-11-08T17:46:03.018679Z","shell.execute_reply.started":"2022-11-08T17:46:00.705306Z","shell.execute_reply":"2022-11-08T17:46:03.017300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Callbacks","metadata":{"_uuid":"bd496f6c65888a969be3703135b0b03a8a1190c8"}},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"_uuid":"9aa032f0f6da539d23918890d2d131cc3aac8c7a","execution":{"iopub.status.busy":"2022-11-08T17:46:06.458029Z","iopub.execute_input":"2022-11-08T17:46:06.458446Z","iopub.status.idle":"2022-11-08T17:46:06.463503Z","shell.execute_reply.started":"2022-11-08T17:46:06.458363Z","shell.execute_reply":"2022-11-08T17:46:06.462129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Early Stop**\n\nTo prevent over fitting we will stop the learning after 10 epochs and val_loss value not decreased","metadata":{"_uuid":"76c9ba4fb7f930c96b2c3e0d6b68ed9fa6a4227b"}},{"cell_type":"code","source":"earlystop = EarlyStopping(patience=10)","metadata":{"_uuid":"3421c5ec428da6c0d8cc1184179a9caff1e01d1c","execution":{"iopub.status.busy":"2022-11-08T17:46:07.672852Z","iopub.execute_input":"2022-11-08T17:46:07.673141Z","iopub.status.idle":"2022-11-08T17:46:07.677037Z","shell.execute_reply.started":"2022-11-08T17:46:07.673089Z","shell.execute_reply":"2022-11-08T17:46:07.676090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Learning Rate Reduction**\n\nWe will reduce the learning rate when then accuracy not increase for 2 steps","metadata":{"_uuid":"51d3fe52e911286433cedf6e47332948a253361e"}},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy', \n                                            patience=2, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)","metadata":{"_uuid":"8010a5661ad8924d2db24af0f3c00b1593b38901","execution":{"iopub.status.busy":"2022-11-08T18:37:12.750847Z","iopub.execute_input":"2022-11-08T18:37:12.751143Z","iopub.status.idle":"2022-11-08T18:37:12.755975Z","shell.execute_reply.started":"2022-11-08T18:37:12.751089Z","shell.execute_reply":"2022-11-08T18:37:12.755252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [earlystop, learning_rate_reduction]","metadata":{"_uuid":"a79cc604199469789f183096d863f7248e5f6aab","execution":{"iopub.status.busy":"2022-11-08T18:37:13.051818Z","iopub.execute_input":"2022-11-08T18:37:13.052055Z","iopub.status.idle":"2022-11-08T18:37:13.055833Z","shell.execute_reply.started":"2022-11-08T18:37:13.052007Z","shell.execute_reply":"2022-11-08T18:37:13.054745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare data","metadata":{}},{"cell_type":"markdown","source":"Because we will use image genaretor `with class_mode=\"categorical\"`. We need to convert column category into string. Then imagenerator will convert it one-hot encoding which is good for our classification. \n\nSo we will convert 1 to dog and 0 to cat","metadata":{}},{"cell_type":"code","source":"df[\"category\"] = df[\"category\"].replace({0: 'cat', 1: 'dog'}) ","metadata":{"execution":{"iopub.status.busy":"2022-11-08T18:37:13.783499Z","iopub.execute_input":"2022-11-08T18:37:13.783789Z","iopub.status.idle":"2022-11-08T18:37:13.793184Z","shell.execute_reply.started":"2022-11-08T18:37:13.783739Z","shell.execute_reply":"2022-11-08T18:37:13.792450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, validate_df = train_test_split(df, test_size=0.20, random_state=42)\ntrain_df = train_df.reset_index(drop=True)\nvalidate_df = validate_df.reset_index(drop=True)","metadata":{"_uuid":"4eeb7af8dcf02c4ef5ca744c8305c51a2f5cedef","execution":{"iopub.status.busy":"2022-11-08T18:37:14.039827Z","iopub.execute_input":"2022-11-08T18:37:14.040116Z","iopub.status.idle":"2022-11-08T18:37:14.051097Z","shell.execute_reply.started":"2022-11-08T18:37:14.040058Z","shell.execute_reply":"2022-11-08T18:37:14.050291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['category'].value_counts().plot.bar()","metadata":{"_uuid":"b84836337441705eda9d2e655665ffa14d9feead","execution":{"iopub.status.busy":"2022-11-08T18:37:14.329768Z","iopub.execute_input":"2022-11-08T18:37:14.329995Z","iopub.status.idle":"2022-11-08T18:37:14.561245Z","shell.execute_reply.started":"2022-11-08T18:37:14.329948Z","shell.execute_reply":"2022-11-08T18:37:14.560306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df['category'].value_counts().plot.bar()","metadata":{"_uuid":"19cf03f9a3c39532d6e2d06bd30be49a5afd9d57","execution":{"iopub.status.busy":"2022-11-08T18:37:14.675513Z","iopub.execute_input":"2022-11-08T18:37:14.675744Z","iopub.status.idle":"2022-11-08T18:37:14.897642Z","shell.execute_reply.started":"2022-11-08T18:37:14.675698Z","shell.execute_reply":"2022-11-08T18:37:14.896833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train = train_df.shape[0]\ntotal_validate = validate_df.shape[0]\nbatch_size=15","metadata":{"_uuid":"ae3dec0361f0443132d0309d3b883ee80070cf9f","execution":{"iopub.status.busy":"2022-11-08T18:37:15.024190Z","iopub.execute_input":"2022-11-08T18:37:15.024447Z","iopub.status.idle":"2022-11-08T18:37:15.028084Z","shell.execute_reply.started":"2022-11-08T18:37:15.024398Z","shell.execute_reply":"2022-11-08T18:37:15.027252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CutMixImageDataGenerator():\n    def __init__(self, generator1, generator2, img_size, batch_size):\n        self.batch_index = 0\n        self.samples = generator1.samples\n        self.class_indices = generator1.class_indices\n        self.generator1 = generator1\n        self.generator2 = generator2\n        self.img_size = img_size\n        self.batch_size = batch_size\n\n    def reset_index(self):  # Ordering Reset (If Shuffle is True, Shuffle Again)\n        self.generator1._set_index_array()\n        self.generator2._set_index_array()\n\n    def reset(self):\n        self.batch_index = 0\n        self.generator1.reset()\n        self.generator2.reset()\n        self.reset_index()\n\n    def get_steps_per_epoch(self):\n        quotient, remainder = divmod(self.samples, self.batch_size)\n        return (quotient + 1) if remainder else quotient\n    \n    def __len__(self):\n        self.get_steps_per_epoch()\n    \n#     def __iter__(self):\n#         return self\n\n    def __next__(self):\n        if self.batch_index == 0: self.reset()\n\n        crt_idx = self.batch_index * self.batch_size\n        if self.samples > crt_idx + self.batch_size:\n            self.batch_index += 1\n        else:  # If current index over number of samples\n            self.batch_index = 0\n\n        reshape_size = self.batch_size\n        last_step_start_idx = (self.get_steps_per_epoch()-1) * self.batch_size\n        if crt_idx == last_step_start_idx:\n            reshape_size = self.samples - last_step_start_idx\n            \n        X_1, y_1 = self.generator1.next()\n        X_2, y_2 = self.generator2.next()\n        reshape_size = min(X_1.shape[0], X_2.shape[0])\n        X_1 = X_1[:reshape_size]\n        y_1 = y_1[:reshape_size]\n        \n        X_2 = X_2[:reshape_size]\n        y_2 = y_2[:reshape_size]\n\n        if X_1.shape[0] != X_2.shape[0]:\n            print(X_1.shape,X_2.shape,  reshape_size)\n        \n        cut_ratio = np.random.beta(a=1, b=1, size=reshape_size)\n        cut_ratio = np.clip(cut_ratio, 0.2, 0.8)\n        label_ratio = cut_ratio.reshape(reshape_size, 1)\n        cut_img = X_2\n\n        X = X_1\n        for i in range(reshape_size):\n            cut_size = int((self.img_size-1) * cut_ratio[i])\n            y1 = random.randint(0, (self.img_size-1) - cut_size)\n            x1 = random.randint(0, (self.img_size-1) - cut_size)\n            #print(y1, 'this is y1')\n            y2 = y1 + cut_size\n            x2 = x1 + cut_size\n            cut_arr = cut_img[i][y1:y2, x1:x2]\n            cutmix_img = X_1[i]\n            cutmix_img[y1:y2, x1:x2] = cut_arr\n            X[i] = cutmix_img        \n        y = y_1 * (1 - (label_ratio ** 2)) + y_2 * (label_ratio ** 2)\n\n        return X, y","metadata":{"execution":{"iopub.status.busy":"2022-11-08T18:54:37.853209Z","iopub.execute_input":"2022-11-08T18:54:37.853530Z","iopub.status.idle":"2022-11-08T18:54:37.866323Z","shell.execute_reply.started":"2022-11-08T18:54:37.853479Z","shell.execute_reply":"2022-11-08T18:54:37.865315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1= np.array([1,0])\ny2 = np.array([1,0])\nlr = 0.56038959\ny1*(1-(lr**2)) + y2*(lr**2)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T18:37:19.077861Z","iopub.execute_input":"2022-11-08T18:37:19.078175Z","iopub.status.idle":"2022-11-08T18:37:19.084442Z","shell.execute_reply.started":"2022-11-08T18:37:19.078121Z","shell.execute_reply":"2022-11-08T18:37:19.083317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T18:37:19.538336Z","iopub.execute_input":"2022-11-08T18:37:19.538615Z","iopub.status.idle":"2022-11-08T18:37:19.551665Z","shell.execute_reply.started":"2022-11-08T18:37:19.538563Z","shell.execute_reply":"2022-11-08T18:37:19.550812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df.head(2)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = f\"train/\"\n#test_path = f\"../input/input-{self.locker['comp_name']}/\" + \"test_images/\"\ntrain_datagen = ImageDataGenerator(rescale=1.0 / 255)\nvalid_datagen = ImageDataGenerator(rescale=1.0 / 255)        \n        \ntrain_datagen1 = train_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory=image_path,\n    target_size=(128, 128),  # images are resized to (28,28)\n    x_col='filename', #self.locker[\"id_name\"],\n    y_col='category', #self.locker[\"target_name\"],\n    batch_size=32,\n    seed=42,\n    shuffle=True,  # Required for cutmix\n    class_mode=\"categorical\",  # \"binary\"\n)\ntrain_datagen2 = train_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory=image_path,\n    target_size=(128, 128),  # images are resized to (28,28)\n    x_col='filename', #self.locker[\"id_name\"],\n    y_col='category', #self.locker[\"target_name\"],\n    batch_size=32,\n    seed=42,\n    shuffle=True,  # Required for cutmix\n    class_mode=\"categorical\",  # \"binary\"\n)\ntrain_dataset = CutMixImageDataGenerator(\n    generator1= train_datagen1, #train_generator1,\n    generator2= train_datagen2, #train_generator2,\n    img_size=128,\n    batch_size=32,\n)\nvalid_dataset = valid_datagen.flow_from_dataframe(\n    dataframe=validate_df,\n    directory=image_path,\n    target_size=(128, 128),  # images are resized to (28,28)\n    x_col='filename', #self.locker[\"id_name\"],\n    y_col='category', #self.locker[\"target_name\"],\n    batch_size=32,\n    seed=42,\n    shuffle=True,\n    class_mode=\"categorical\",  # \"binary\"\n    )","metadata":{"execution":{"iopub.status.busy":"2022-11-08T18:54:05.956744Z","iopub.execute_input":"2022-11-08T18:54:05.957043Z","iopub.status.idle":"2022-11-08T18:54:06.274623Z","shell.execute_reply.started":"2022-11-08T18:54:05.956993Z","shell.execute_reply":"2022-11-08T18:54:06.273574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 50\n        \nhistory = model.fit_generator(\n    train_dataset,\n    steps_per_epoch= train_dataset.get_steps_per_epoch(),\n    epochs=epochs,\n    verbose=2,\n    validation_data=valid_dataset,\n    validation_steps=8,\n    callbacks = callbacks\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T18:42:30.658708Z","iopub.execute_input":"2022-11-08T18:42:30.659003Z","iopub.status.idle":"2022-11-08T18:46:46.172667Z","shell.execute_reply.started":"2022-11-08T18:42:30.658950Z","shell.execute_reply":"2022-11-08T18:46:46.171865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate_generator(\n    generator=valid_dataset, steps=1\n)  # 1 to make it perfectly divisible","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(40, 40))\nfig.subplots_adjust(hspace=0.1, wspace=0.1)\nrow = 6\ncol = 6\na,b = next(train_dataset)\nfor i,(im1, l1) in enumerate(zip(a,b)):\n    plot_num = i+1\n    ax = fig.add_subplot(row, col, plot_num, xticks=[], yticks=[], label=f\"Level {l1}\")\n    ax.set_title(f\"Level {l1}\")\n    ax.imshow(im1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T18:47:05.456676Z","iopub.execute_input":"2022-11-08T18:47:05.456969Z","iopub.status.idle":"2022-11-08T18:47:09.537816Z","shell.execute_reply.started":"2022-11-08T18:47:05.456917Z","shell.execute_reply":"2022-11-08T18:47:09.537162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save Model","metadata":{"_uuid":"aa1fbc4081ae0de2993188b2bf658a0be5bc0687"}},{"cell_type":"code","source":"model.save_weights(\"model.h5\")","metadata":{"_uuid":"67575a4decdaf79a915d23151626b784ffa82642","execution":{"iopub.status.busy":"2022-11-08T18:47:09.538952Z","iopub.execute_input":"2022-11-08T18:47:09.539380Z","iopub.status.idle":"2022-11-08T18:47:09.660229Z","shell.execute_reply.started":"2022-11-08T18:47:09.539335Z","shell.execute_reply":"2022-11-08T18:47:09.659189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Virtualize Training","metadata":{"_uuid":"1b76c0a9040bc0babf0a453e567e41e22f8a1e0e"}},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))\nax1.plot(history.history['loss'], color='b', label=\"Training loss\")\nax1.plot(history.history['val_loss'], color='r', label=\"validation loss\")\nax1.set_xticks(np.arange(1, epochs, 1))\nax1.set_yticks(np.arange(0, 1, 0.1))\n\nax2.plot(history.history['acc'], color='b', label=\"Training accuracy\")\nax2.plot(history.history['val_acc'], color='r',label=\"Validation accuracy\")\nax2.set_xticks(np.arange(1, epochs, 1))\n\nlegend = plt.legend(loc='best', shadow=True)\nplt.tight_layout()\nplt.show()","metadata":{"_uuid":"79055f2dc3e2abb47bea758e0464c86ca42ab431","execution":{"iopub.status.busy":"2022-11-08T18:47:47.586866Z","iopub.execute_input":"2022-11-08T18:47:47.587162Z","iopub.status.idle":"2022-11-08T18:47:48.185016Z","shell.execute_reply.started":"2022-11-08T18:47:47.587108Z","shell.execute_reply":"2022-11-08T18:47:48.184276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare Testing Data","metadata":{"_uuid":"764dc66e4b2bc558f3a0f90b80bb802f5b3d45a8"}},{"cell_type":"code","source":"test_filenames = os.listdir(\"test1\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nnb_samples = test_df.shape[0]","metadata":{"_uuid":"c35e70d3e1e4834dbbf840fa0ea08c049bfcd915","execution":{"iopub.status.busy":"2022-11-08T18:48:16.010471Z","iopub.execute_input":"2022-11-08T18:48:16.010764Z","iopub.status.idle":"2022-11-08T18:48:16.024631Z","shell.execute_reply.started":"2022-11-08T18:48:16.010711Z","shell.execute_reply":"2022-11-08T18:48:16.023931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nb_samples","metadata":{"execution":{"iopub.status.busy":"2022-11-08T18:48:24.604708Z","iopub.execute_input":"2022-11-08T18:48:24.605004Z","iopub.status.idle":"2022-11-08T18:48:24.613076Z","shell.execute_reply.started":"2022-11-08T18:48:24.604948Z","shell.execute_reply":"2022-11-08T18:48:24.612076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Testing Generator","metadata":{"_uuid":"291bc3996dce8d05e174b27d64f03996d3e8038e"}},{"cell_type":"code","source":"image_path","metadata":{"execution":{"iopub.status.busy":"2022-11-08T18:49:22.578061Z","iopub.execute_input":"2022-11-08T18:49:22.578374Z","iopub.status.idle":"2022-11-08T18:49:22.583880Z","shell.execute_reply.started":"2022-11-08T18:49:22.578314Z","shell.execute_reply":"2022-11-08T18:49:22.583059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T18:50:43.308522Z","iopub.execute_input":"2022-11-08T18:50:43.308804Z","iopub.status.idle":"2022-11-08T18:50:43.319478Z","shell.execute_reply.started":"2022-11-08T18:50:43.308751Z","shell.execute_reply":"2022-11-08T18:50:43.318435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\nbatch_size = 32\n\ntest_dataset = test_datagen.flow_from_dataframe(\n    dataframe=test_df,\n    directory='test1/',\n    target_size=(128, 128),  # images are resized to (28,28)\n    x_col='filename', #self.locker[\"id_name\"],\n    y_col=None, #self.locker[\"target_name\"],\n    batch_size=batch_size,\n    seed=42,\n    shuffle=False,\n    class_mode=None,  # \"binary\"\n    )\n","metadata":{"_uuid":"52249ec1c35fb1be3adef386be245de3794e55aa","execution":{"iopub.status.busy":"2022-11-08T18:51:14.057377Z","iopub.execute_input":"2022-11-08T18:51:14.057680Z","iopub.status.idle":"2022-11-08T18:51:14.164297Z","shell.execute_reply.started":"2022-11-08T18:51:14.057627Z","shell.execute_reply":"2022-11-08T18:51:14.163320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict","metadata":{"_uuid":"2fa580afca2931ec5ce374e732d8c1789d03f2ed"}},{"cell_type":"code","source":"predict = model.predict_generator(test_dataset, steps=np.ceil(nb_samples/batch_size))","metadata":{"_uuid":"4782eb23fa7d003f0e2415d995894017edb2d896","execution":{"iopub.status.busy":"2022-11-08T18:58:51.412859Z","iopub.execute_input":"2022-11-08T18:58:51.413193Z","iopub.status.idle":"2022-11-08T18:59:29.509768Z","shell.execute_reply.started":"2022-11-08T18:58:51.413135Z","shell.execute_reply":"2022-11-08T18:59:29.508745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For categoral classication the prediction will come with probability of each category. So we will pick the category that have the highest probability with numpy average max","metadata":{}},{"cell_type":"code","source":"predict.shape, test_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-08T18:59:41.298843Z","iopub.execute_input":"2022-11-08T18:59:41.299228Z","iopub.status.idle":"2022-11-08T18:59:41.304628Z","shell.execute_reply.started":"2022-11-08T18:59:41.299145Z","shell.execute_reply":"2022-11-08T18:59:41.303892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'] = np.argmax(predict, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T18:59:43.165118Z","iopub.execute_input":"2022-11-08T18:59:43.165526Z","iopub.status.idle":"2022-11-08T18:59:43.171503Z","shell.execute_reply.started":"2022-11-08T18:59:43.165373Z","shell.execute_reply":"2022-11-08T18:59:43.170595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We will convert the predict category back into our generator classes by using `train_generator.class_indices`. It is the classes that image generator map while converting data into computer vision","metadata":{}},{"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_dataset.class_indices.items())\ntest_df['category'] = test_df['category'].replace(label_map)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T19:00:04.731395Z","iopub.execute_input":"2022-11-08T19:00:04.731688Z","iopub.status.idle":"2022-11-08T19:00:04.739555Z","shell.execute_reply.started":"2022-11-08T19:00:04.731637Z","shell.execute_reply":"2022-11-08T19:00:04.738594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From our prepare data part. We map data with `{1: 'dog', 0: 'cat'}`. Now we will map the result back to dog is 1 and cat is 0","metadata":{}},{"cell_type":"code","source":"test_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })","metadata":{"execution":{"iopub.status.busy":"2022-11-08T19:00:11.048911Z","iopub.execute_input":"2022-11-08T19:00:11.049203Z","iopub.status.idle":"2022-11-08T19:00:11.060416Z","shell.execute_reply.started":"2022-11-08T19:00:11.049151Z","shell.execute_reply":"2022-11-08T19:00:11.059345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Virtaulize Result","metadata":{"_uuid":"b00add65fe765529e637c3a9904d710bb7eff1d8"}},{"cell_type":"code","source":"test_df['category'].value_counts().plot.bar()","metadata":{"_uuid":"d0bf6dd5ff344092fa0121f70bdd60fa5a40e29c","execution":{"iopub.status.busy":"2022-11-08T19:00:14.270722Z","iopub.execute_input":"2022-11-08T19:00:14.271017Z","iopub.status.idle":"2022-11-08T19:00:14.523281Z","shell.execute_reply.started":"2022-11-08T19:00:14.270963Z","shell.execute_reply":"2022-11-08T19:00:14.522139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### See predicted result with images","metadata":{"_uuid":"ce72a83f80d6e012b12b82c8ee3365d671a3b307"}},{"cell_type":"code","source":"sample_test = test_df.head(18)\nsample_test.head()\nplt.figure(figsize=(12, 24))\nfor index, row in sample_test.iterrows():\n    filename = row['filename']\n    category = row['category']\n    img = load_img(\"test1/\"+filename, target_size=IMAGE_SIZE)\n    plt.subplot(6, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename + '(' + \"{}\".format(category) + ')' )\nplt.tight_layout()\nplt.show()","metadata":{"_uuid":"98b41dc83075e6297137fb45bf703c313dd4ae28","execution":{"iopub.status.busy":"2022-11-08T19:00:18.010630Z","iopub.execute_input":"2022-11-08T19:00:18.010910Z","iopub.status.idle":"2022-11-08T19:00:21.996906Z","shell.execute_reply.started":"2022-11-08T19:00:18.010860Z","shell.execute_reply":"2022-11-08T19:00:21.996169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{"_uuid":"d1ca25943e73aa20a37f9fb8670ee430caeaaf1f"}},{"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"_uuid":"cce9f3e2ffff0693d79d84590ed71fbbca7c3c7c","execution":{"iopub.status.busy":"2022-11-08T11:43:33.024061Z","iopub.execute_input":"2022-11-08T11:43:33.024472Z","iopub.status.idle":"2022-11-08T11:43:33.258631Z","shell.execute_reply.started":"2022-11-08T11:43:33.024424Z","shell.execute_reply":"2022-11-08T11:43:33.257818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}