{"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":"# Dog vs Cat Classification\n#### Author: Concyclics\n### ***Upvote*** me please if you thought it's helpful","metadata":{"papermill":{"duration":0.037341,"end_time":"2022-02-26T07:36:22.137018","exception":false,"start_time":"2022-02-26T07:36:22.099677","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import gc\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport zipfile\nimport pandas as pd\nimport numpy as np\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport pylab","metadata":{"papermill":{"duration":6.337385,"end_time":"2022-02-26T07:36:28.510912","exception":false,"start_time":"2022-02-26T07:36:22.173527","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-03T05:41:06.371082Z","iopub.execute_input":"2022-03-03T05:41:06.371451Z","iopub.status.idle":"2022-03-03T05:41:11.874106Z","shell.execute_reply.started":"2022-03-03T05:41:06.371358Z","shell.execute_reply":"2022-03-03T05:41:11.873367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1. Load datas","metadata":{"papermill":{"duration":0.034008,"end_time":"2022-02-26T07:36:28.581086","exception":false,"start_time":"2022-02-26T07:36:28.547078","status":"completed"},"tags":[]}},{"cell_type":"code","source":"split=0.2\nseed=19260817\nbatch_size=32\nimg_size=(128,128)\n\ntrain_ds = keras.utils.image_dataset_from_directory(\n    '../input/dog-vs-cat-classification/train/train',\n    labels=\"inferred\",\n    label_mode=\"int\",\n    color_mode=\"rgb\",\n    batch_size=batch_size,\n    image_size=img_size,\n    shuffle=True,\n    seed=seed,\n    validation_split=split,\n    subset='training',\n)\nval_ds = keras.utils.image_dataset_from_directory(\n    '../input/dog-vs-cat-classification/train/train',\n    labels=\"inferred\",\n    label_mode=\"int\",\n    color_mode=\"rgb\",\n    batch_size=batch_size,\n    image_size=img_size,\n    shuffle=True,\n    seed=seed,\n    validation_split=split,\n    subset='validation',\n)\n","metadata":{"papermill":{"duration":26.444841,"end_time":"2022-02-26T07:36:55.06029","exception":false,"start_time":"2022-02-26T07:36:28.615449","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-03T05:41:18.216236Z","iopub.execute_input":"2022-03-03T05:41:18.216941Z","iopub.status.idle":"2022-03-03T05:41:35.494085Z","shell.execute_reply.started":"2022-03-03T05:41:18.216904Z","shell.execute_reply":"2022-03-03T05:41:35.493388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1.1 visualize some images","metadata":{"papermill":{"duration":0.035494,"end_time":"2022-02-26T07:36:55.134136","exception":false,"start_time":"2022-02-26T07:36:55.098642","status":"completed"},"tags":[]}},{"cell_type":"code","source":"label_names={0:'cat',1:'dog'}\n\nfig, ax = plt.subplots(figsize=(15, 7))\nfor img, label in train_ds.take(1):\n    for i in range(12):\n        plt.subplot(3, 4,i+1)\n        plt.imshow(img.numpy()[i]/255)\n        plt.axis(\"off\")\n        plt.title(\"label: \"+label_names[label.numpy()[i]])\n        #rint(\"label:\", label.numpy()[i])\nplt.show()","metadata":{"papermill":{"duration":4.6339,"end_time":"2022-02-26T07:36:59.803763","exception":false,"start_time":"2022-02-26T07:36:55.169863","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-03T05:41:37.952692Z","iopub.execute_input":"2022-03-03T05:41:37.953196Z","iopub.status.idle":"2022-03-03T05:41:39.231497Z","shell.execute_reply.started":"2022-03-03T05:41:37.953159Z","shell.execute_reply":"2022-03-03T05:41:39.230842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. EfficientNet model","metadata":{"papermill":{"duration":0.043813,"end_time":"2022-02-26T07:36:59.892445","exception":false,"start_time":"2022-02-26T07:36:59.848632","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"### 2.1 history plot display","metadata":{"papermill":{"duration":0.043856,"end_time":"2022-02-26T07:36:59.979052","exception":false,"start_time":"2022-02-26T07:36:59.935196","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\ndef plot_history(history,title=''):  \n    if title!='':\n        title+=' '\n    #Ploting the Loss and Accuracy Curves\n    fig, ax = plt.subplots(nrows = 1, ncols = 2, figsize = (16,6))\n    #Loss\n    sns.lineplot(data = history.history['loss'], label = 'Training Loss', ax = ax[0])\n    sns.lineplot(data = history.history['val_loss'], label = 'Validation Loss', ax = ax[0])\n    ax[0].legend(loc = 'upper right')\n    ax[0].set_title(title+'Loss')\n    #Accuracy\n    sns.lineplot(data = history.history['accuracy'], label = 'Training Accuracy', ax = ax[1])\n    sns.lineplot(data = history.history['val_accuracy'], label = 'Validation Accuracy', ax = ax[1])\n    ax[1].legend(loc = 'lower right')\n    ax[1].set_title(title+'Accuracy')","metadata":{"papermill":{"duration":0.695391,"end_time":"2022-02-26T07:37:00.720304","exception":false,"start_time":"2022-02-26T07:37:00.024913","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-03T05:41:43.166257Z","iopub.execute_input":"2022-03-03T05:41:43.166928Z","iopub.status.idle":"2022-03-03T05:41:43.734845Z","shell.execute_reply.started":"2022-03-03T05:41:43.166895Z","shell.execute_reply":"2022-03-03T05:41:43.734111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.2 augmentation","metadata":{"papermill":{"duration":0.04863,"end_time":"2022-02-26T07:37:00.814621","exception":false,"start_time":"2022-02-26T07:37:00.765991","status":"completed"},"tags":[]}},{"cell_type":"code","source":"data_augmentation = keras.Sequential(\n    [\n        #layers.RandomFlip(\"horizontal\"),\n        #layers.RandomRotation(0.2),\n        #layers.RandomTranslation(0.14,0.14),\n        #layers.RandomZoom(0.2),\n        #layers.RandomContrast(0.2),\n    ]\n)","metadata":{"papermill":{"duration":0.083476,"end_time":"2022-02-26T07:37:00.942915","exception":false,"start_time":"2022-02-26T07:37:00.859439","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-03T05:41:45.080434Z","iopub.execute_input":"2022-03-03T05:41:45.081262Z","iopub.status.idle":"2022-03-03T05:41:45.104748Z","shell.execute_reply.started":"2022-03-03T05:41:45.081212Z","shell.execute_reply":"2022-03-03T05:41:45.104036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.3 savebest callback and load","metadata":{"papermill":{"duration":0.045088,"end_time":"2022-02-26T07:37:01.032446","exception":false,"start_time":"2022-02-26T07:37:00.987358","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def saveBest(name:str='model',patient=10):\n    CB = [\n        keras.callbacks.ModelCheckpoint(\n        filepath=name+\".keras\",\n        save_best_only=True,\n        monitor=\"val_loss\"),\n\n        keras.callbacks.EarlyStopping(\n        monitor='val_loss',\n        min_delta=0.0005,\n        patience=patient)\n    ]\n    \n    return CB\n\ndef loadBest(name:str='model'):\n    return keras.models.load_model(name+\".keras\")","metadata":{"papermill":{"duration":0.052726,"end_time":"2022-02-26T07:37:01.130153","exception":false,"start_time":"2022-02-26T07:37:01.077427","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-03T05:41:46.731316Z","iopub.execute_input":"2022-03-03T05:41:46.73248Z","iopub.status.idle":"2022-03-03T05:41:46.74382Z","shell.execute_reply.started":"2022-03-03T05:41:46.732435Z","shell.execute_reply":"2022-03-03T05:41:46.742814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.4 load EfficientNet model","metadata":{"papermill":{"duration":0.043981,"end_time":"2022-02-26T07:37:01.217763","exception":false,"start_time":"2022-02-26T07:37:01.173782","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"view of fficientNet structure","metadata":{}},{"cell_type":"code","source":"import tensorflow.keras.applications as efn\n\nEfficientNetB=list(range(8))\nEfficientNetB[0] = efn.EfficientNetB0(weights=None, include_top=True, input_shape=img_size+(3,), classes=2)\nEfficientNetB[1] = efn.EfficientNetB1(weights=None, include_top=True, input_shape=img_size+(3,), classes=2)\nEfficientNetB[2] = efn.EfficientNetB2(weights=None, include_top=True, input_shape=img_size+(3,), classes=2)\nEfficientNetB[3] = efn.EfficientNetB3(weights=None, include_top=True, input_shape=img_size+(3,), classes=2)\nEfficientNetB[4] = efn.EfficientNetB4(weights=None, include_top=True, input_shape=img_size+(3,), classes=2)\nEfficientNetB[5] = efn.EfficientNetB5(weights=None, include_top=True, input_shape=img_size+(3,), classes=2)\nEfficientNetB[6] = efn.EfficientNetB6(weights=None, include_top=True, input_shape=img_size+(3,), classes=2)\nEfficientNetB[7] = efn.EfficientNetB7(weights=None, include_top=True, input_shape=img_size+(3,), classes=2)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-03T05:42:30.826008Z","iopub.execute_input":"2022-03-03T05:42:30.826282Z","iopub.status.idle":"2022-03-03T05:42:50.860608Z","shell.execute_reply.started":"2022-03-03T05:42:30.826252Z","shell.execute_reply":"2022-03-03T05:42:50.859857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"usingModel=[0,1,2,3,4,5,6,7]","metadata":{"execution":{"iopub.status.busy":"2022-03-03T05:43:19.973288Z","iopub.execute_input":"2022-03-03T05:43:19.973656Z","iopub.status.idle":"2022-03-03T05:43:19.980823Z","shell.execute_reply.started":"2022-03-03T05:43:19.973613Z","shell.execute_reply":"2022-03-03T05:43:19.979747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names=['EfficientNetB'+str(i) for i in range(0,8)]\nnames","metadata":{"execution":{"iopub.status.busy":"2022-03-03T05:43:21.563218Z","iopub.execute_input":"2022-03-03T05:43:21.563755Z","iopub.status.idle":"2022-03-03T05:43:21.56986Z","shell.execute_reply.started":"2022-03-03T05:43:21.56372Z","shell.execute_reply":"2022-03-03T05:43:21.56915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getModel(EfficientNet):\n    inputs = keras.Input(shape=img_size+(3,))\n    #x = data_augmentation(inputs)\n    outputs = EfficientNet(inputs)\n    model = keras.Model(inputs, outputs)\n    model.compile(optimizer=\"rmsprop\",\n              loss=\"sparse_categorical_crossentropy\",\n              metrics=[\"accuracy\"])\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-03-03T05:43:57.515141Z","iopub.execute_input":"2022-03-03T05:43:57.515464Z","iopub.status.idle":"2022-03-03T05:43:57.520453Z","shell.execute_reply.started":"2022-03-03T05:43:57.515434Z","shell.execute_reply":"2022-03-03T05:43:57.519692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def trainModel(model,name:str='model'):\n    \n    history = model.fit(\n    train_ds,\n    epochs=10,\n    validation_data=val_ds,\n    callbacks=saveBest(name)\n    )\n    \n    return history","metadata":{"execution":{"iopub.status.busy":"2022-03-03T05:43:59.162917Z","iopub.execute_input":"2022-03-03T05:43:59.163717Z","iopub.status.idle":"2022-03-03T05:43:59.168476Z","shell.execute_reply.started":"2022-03-03T05:43:59.16367Z","shell.execute_reply":"2022-03-03T05:43:59.16769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EfficientNetB[0].summary()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.5 train models","metadata":{}},{"cell_type":"code","source":"\nhistorys={}\nfor i in usingModel:\n    model = getModel(EfficientNetB[i])\n    history = trainModel(model,name=names[i])\n    historys[names[i]]=history\n    ","metadata":{"execution":{"iopub.status.busy":"2022-03-03T05:45:24.606162Z","iopub.execute_input":"2022-03-03T05:45:24.606473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for name in historys:\n    plot_history(historys[name],title=name)\n    plt.savefig(name+'.png')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.6 Compare models","metadata":{"papermill":{"duration":1.601396,"end_time":"2022-02-26T08:04:54.370763","exception":false,"start_time":"2022-02-26T08:04:52.769367","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def getLossTable():\n    list=[]\n    for i in usingModel:\n        model=loadBest(names[i])\n        test=model.evaluate(val_ds)\n        list.append(test)\n    return list","metadata":{"execution":{"iopub.status.busy":"2022-03-02T13:40:28.784522Z","iopub.execute_input":"2022-03-02T13:40:28.785066Z","iopub.status.idle":"2022-03-02T13:40:28.790163Z","shell.execute_reply.started":"2022-03-02T13:40:28.785026Z","shell.execute_reply":"2022-03-02T13:40:28.789438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LossTable=getLossTable()","metadata":{"execution":{"iopub.status.busy":"2022-03-02T13:47:01.184417Z","iopub.execute_input":"2022-03-02T13:47:01.184687Z","iopub.status.idle":"2022-03-02T13:51:12.060761Z","shell.execute_reply.started":"2022-03-02T13:47:01.184645Z","shell.execute_reply":"2022-03-02T13:51:12.059964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"usingModelName=[names[i] for i in usingModel]\nusingModelName","metadata":{"execution":{"iopub.status.busy":"2022-03-02T13:51:18.93242Z","iopub.execute_input":"2022-03-02T13:51:18.932701Z","iopub.status.idle":"2022-03-02T13:51:18.938254Z","shell.execute_reply.started":"2022-03-02T13:51:18.932655Z","shell.execute_reply":"2022-03-02T13:51:18.937466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('precision',10)\ncompare_table=pd.DataFrame(LossTable,columns=['loss','accuracy'],index=usingModelName)\ncm = sns.light_palette(\"blue\", as_cmap=True)\ncompare_table.head(len(usingModel)).style.background_gradient(cmap=cm)","metadata":{"papermill":{"duration":1.729451,"end_time":"2022-02-26T08:05:39.84941","exception":false,"start_time":"2022-02-26T08:05:38.119959","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-02T13:51:22.386521Z","iopub.execute_input":"2022-03-02T13:51:22.38718Z","iopub.status.idle":"2022-03-02T13:51:22.406951Z","shell.execute_reply.started":"2022-03-02T13:51:22.38714Z","shell.execute_reply":"2022-03-02T13:51:22.406158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. Prediction","metadata":{"papermill":{"duration":1.713335,"end_time":"2022-02-26T08:05:46.9054","exception":false,"start_time":"2022-02-26T08:05:45.192065","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"### 3.1 select the best one","metadata":{}},{"cell_type":"code","source":"bestModelName=compare_table.idxmax()['accuracy']\nmodel=loadBest(bestModelName)","metadata":{"execution":{"iopub.status.busy":"2022-03-02T13:53:40.383658Z","iopub.execute_input":"2022-03-02T13:53:40.383937Z","iopub.status.idle":"2022-03-02T13:53:48.664117Z","shell.execute_reply.started":"2022-03-02T13:53:40.383907Z","shell.execute_reply":"2022-03-02T13:53:48.663327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3.2 prepare testing data","metadata":{"papermill":{"duration":1.660845,"end_time":"2022-02-26T08:05:50.244081","exception":false,"start_time":"2022-02-26T08:05:48.583236","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import os\ntest_filenames = os.listdir(\"../input/dog-vs-cat-classification/test/test\")\ntest_df = pd.DataFrame({\n    'id': test_filenames\n})\nnb_samples = test_df.shape[0]\nnb_samples","metadata":{"papermill":{"duration":2.255387,"end_time":"2022-02-26T08:05:54.200855","exception":false,"start_time":"2022-02-26T08:05:51.945468","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-02T13:53:53.505409Z","iopub.execute_input":"2022-03-02T13:53:53.505809Z","iopub.status.idle":"2022-03-02T13:53:53.521627Z","shell.execute_reply.started":"2022-03-02T13:53:53.505774Z","shell.execute_reply":"2022-03-02T13:53:53.520946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator, load_img\ntest_gen = ImageDataGenerator()\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"../input/dog-vs-cat-classification/test/test\", \n    x_col='id',\n    y_col=None,\n    class_mode=None,\n    target_size=img_size,\n    batch_size=batch_size,\n    shuffle=False\n)","metadata":{"papermill":{"duration":17.703897,"end_time":"2022-02-26T08:06:13.560959","exception":false,"start_time":"2022-02-26T08:05:55.857062","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-02T13:53:55.552627Z","iopub.execute_input":"2022-03-02T13:53:55.552948Z","iopub.status.idle":"2022-03-02T13:53:58.809378Z","shell.execute_reply.started":"2022-03-02T13:53:55.552919Z","shell.execute_reply":"2022-03-02T13:53:58.808665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3.3 predict and view","metadata":{"papermill":{"duration":1.988293,"end_time":"2022-02-26T08:06:17.277618","exception":false,"start_time":"2022-02-26T08:06:15.289325","status":"completed"},"tags":[]}},{"cell_type":"code","source":"predict = model.predict(test_generator)\ntest_df['labels'] = np.argmax(predict, axis=-1)","metadata":{"papermill":{"duration":68.228708,"end_time":"2022-02-26T08:07:27.197917","exception":false,"start_time":"2022-02-26T08:06:18.969209","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-02T13:54:01.108989Z","iopub.execute_input":"2022-03-02T13:54:01.109641Z","iopub.status.idle":"2022-03-02T13:55:05.714397Z","shell.execute_reply.started":"2022-03-02T13:54:01.109597Z","shell.execute_reply":"2022-03-02T13:55:05.71358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"papermill":{"duration":1.741948,"end_time":"2022-02-26T08:07:30.640572","exception":false,"start_time":"2022-02-26T08:07:28.898624","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-02T13:55:14.817328Z","iopub.execute_input":"2022-03-02T13:55:14.817586Z","iopub.status.idle":"2022-03-02T13:55:14.834097Z","shell.execute_reply.started":"2022-03-02T13:55:14.817556Z","shell.execute_reply":"2022-03-02T13:55:14.833345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_names={0:'cat',1:'dog'}\nfig, ax = plt.subplots(figsize=(15, 7))\ni=0\nfor index, row in test_df.sample(12).iterrows():\n    i+=1\n    plt.subplot(3, 4,i)\n    img=plt.imread('../input/dog-vs-cat-classification/test/test/'+row['id'])\n    plt.imshow(img)\n    plt.axis(\"off\")\n    plt.title(\"label: \"+label_names[label.numpy()[i]])\n    #rint(\"label:\", label.numpy()[i])\nplt.show()","metadata":{"papermill":{"duration":2.962592,"end_time":"2022-02-26T08:07:39.778072","exception":false,"start_time":"2022-02-26T08:07:36.81548","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-02T13:55:43.886761Z","iopub.execute_input":"2022-03-02T13:55:43.887203Z","iopub.status.idle":"2022-03-02T13:55:44.87607Z","shell.execute_reply.started":"2022-03-02T13:55:43.887168Z","shell.execute_reply":"2022-03-02T13:55:44.875483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3.4 submit","metadata":{"papermill":{"duration":2.045901,"end_time":"2022-02-26T08:07:44.176443","exception":false,"start_time":"2022-02-26T08:07:42.130542","status":"completed"},"tags":[]}},{"cell_type":"code","source":"test_df.set_index('id')\ntest_df.to_csv('submission.csv',index=False)","metadata":{"papermill":{"duration":1.745191,"end_time":"2022-02-26T08:07:47.634162","exception":false,"start_time":"2022-02-26T08:07:45.888971","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-02T13:56:25.575767Z","iopub.execute_input":"2022-03-02T13:56:25.576486Z","iopub.status.idle":"2022-03-02T13:56:25.602319Z","shell.execute_reply.started":"2022-03-02T13:56:25.576437Z","shell.execute_reply":"2022-03-02T13:56:25.601571Z"},"trusted":true},"execution_count":null,"outputs":[]}]}