{"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":"# 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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-20T04:02:24.916415Z","iopub.execute_input":"2022-05-20T04:02:24.917105Z","iopub.status.idle":"2022-05-20T04:02:24.92402Z","shell.execute_reply.started":"2022-05-20T04:02:24.917061Z","shell.execute_reply":"2022-05-20T04:02:24.92334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# /kaggle/input/dogs-vs-cats-redux-kernels-edition/train\n\n!unzip -q -o '/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip'","metadata":{"execution":{"iopub.status.busy":"2022-05-20T04:02:24.95741Z","iopub.execute_input":"2022-05-20T04:02:24.95783Z","iopub.status.idle":"2022-05-20T04:02:35.536025Z","shell.execute_reply.started":"2022-05-20T04:02:24.957801Z","shell.execute_reply":"2022-05-20T04:02:35.534787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q -o '/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip'","metadata":{"execution":{"iopub.status.busy":"2022-05-20T04:02:35.538248Z","iopub.execute_input":"2022-05-20T04:02:35.53854Z","iopub.status.idle":"2022-05-20T04:02:41.322467Z","shell.execute_reply.started":"2022-05-20T04:02:35.538485Z","shell.execute_reply":"2022-05-20T04:02:41.321466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\nImage.open('train/cat.4511.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-05-20T04:02:41.324196Z","iopub.execute_input":"2022-05-20T04:02:41.324503Z","iopub.status.idle":"2022-05-20T04:02:41.377055Z","shell.execute_reply.started":"2022-05-20T04:02:41.324464Z","shell.execute_reply":"2022-05-20T04:02:41.376276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\n\ntrain = pd.DataFrame({'path' : glob.glob('train/*')})\ntrain.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T04:02:41.378739Z","iopub.execute_input":"2022-05-20T04:02:41.378973Z","iopub.status.idle":"2022-05-20T04:02:41.478764Z","shell.execute_reply.started":"2022-05-20T04:02:41.37894Z","shell.execute_reply":"2022-05-20T04:02:41.477988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['target'] = train['path'].apply(lambda x : x.split('/')[1].split('.')[0])\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-05-20T04:02:41.480184Z","iopub.execute_input":"2022-05-20T04:02:41.480433Z","iopub.status.idle":"2022-05-20T04:02:41.515356Z","shell.execute_reply.started":"2022-05-20T04:02:41.480399Z","shell.execute_reply":"2022-05-20T04:02:41.514752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.DataFrame({'path' : glob.glob('test/*')})\ntest","metadata":{"execution":{"iopub.status.busy":"2022-05-20T04:02:41.516729Z","iopub.execute_input":"2022-05-20T04:02:41.517019Z","iopub.status.idle":"2022-05-20T04:02:41.56735Z","shell.execute_reply.started":"2022-05-20T04:02:41.516981Z","shell.execute_reply":"2022-05-20T04:02:41.56648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\nidg = ImageDataGenerator()\n\nidg2 = ImageDataGenerator(horizontal_flip = True, brightness_range = [0.2, 1.0],\n                         vertical_flip = True)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:21:12.013133Z","iopub.execute_input":"2022-05-20T05:21:12.013396Z","iopub.status.idle":"2022-05-20T05:21:12.018242Z","shell.execute_reply.started":"2022-05-20T05:21:12.013361Z","shell.execute_reply":"2022-05-20T05:21:12.017054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train, x_valid = train_test_split(train, test_size = 0.2, random_state = 42,\n                                   stratify = train['target'])","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:21:16.208332Z","iopub.execute_input":"2022-05-20T05:21:16.209104Z","iopub.status.idle":"2022-05-20T05:21:16.259601Z","shell.execute_reply.started":"2022-05-20T05:21:16.209062Z","shell.execute_reply":"2022-05-20T05:21:16.258621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_valid['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-05-20T04:02:47.487581Z","iopub.execute_input":"2022-05-20T04:02:47.488061Z","iopub.status.idle":"2022-05-20T04:02:47.501239Z","shell.execute_reply.started":"2022-05-20T04:02:47.488022Z","shell.execute_reply":"2022-05-20T04:02:47.500556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-05-20T04:02:47.503636Z","iopub.execute_input":"2022-05-20T04:02:47.504024Z","iopub.status.idle":"2022-05-20T04:02:47.526385Z","shell.execute_reply.started":"2022-05-20T04:02:47.503979Z","shell.execute_reply":"2022-05-20T04:02:47.52573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = idg2.flow_from_dataframe(x_train, x_col = 'path', y_col = 'target',\n                                         target_size = (100,100))","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:21:37.505938Z","iopub.execute_input":"2022-05-20T05:21:37.506279Z","iopub.status.idle":"2022-05-20T05:21:37.81404Z","shell.execute_reply.started":"2022-05-20T05:21:37.50624Z","shell.execute_reply":"2022-05-20T05:21:37.813344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator = idg.flow_from_dataframe(x_valid, x_col = 'path', y_col = 'target',\n                                         target_size = (100,100))","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:21:40.863202Z","iopub.execute_input":"2022-05-20T05:21:40.864075Z","iopub.status.idle":"2022-05-20T05:21:40.920658Z","shell.execute_reply.started":"2022-05-20T05:21:40.864019Z","shell.execute_reply":"2022-05-20T05:21:40.919907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.figure(figsize = (12,12))\nfor i in range(0,15):\n    plt.subplot(5,3,i+1)\n    for x, y in train_generator:\n        image = x[0]\n        plt.imshow(image.astype('uint8'))\n        break\n# plt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:21:41.130797Z","iopub.execute_input":"2022-05-20T05:21:41.131019Z","iopub.status.idle":"2022-05-20T05:21:43.717828Z","shell.execute_reply.started":"2022-05-20T05:21:41.130994Z","shell.execute_reply":"2022-05-20T05:21:43.717189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.applications import EfficientNetB0","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:23:25.081676Z","iopub.execute_input":"2022-05-20T05:23:25.082307Z","iopub.status.idle":"2022-05-20T05:23:25.088333Z","shell.execute_reply.started":"2022-05-20T05:23:25.082265Z","shell.execute_reply":"2022-05-20T05:23:25.087459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eb0 = EfficientNetB0(include_top = False, pooling = 'avg')","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:23:25.266762Z","iopub.execute_input":"2022-05-20T05:23:25.266969Z","iopub.status.idle":"2022-05-20T05:23:26.679632Z","shell.execute_reply.started":"2022-05-20T05:23:25.266945Z","shell.execute_reply":"2022-05-20T05:23:26.678891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(eb0)\nmodel.add(Dense(2, activation = 'softmax'))\n\nfrom tensorflow.keras.optimizers import SGD\n\nmodel.compile(metrics = ['acc'], loss = 'categorical_crossentropy',\n             optimizer = SGD(momentum = 0.9, nesterov = True, lr = 0.01 ))","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:23:26.681177Z","iopub.execute_input":"2022-05-20T05:23:26.681409Z","iopub.status.idle":"2022-05-20T05:23:27.610297Z","shell.execute_reply.started":"2022-05-20T05:23:26.681375Z","shell.execute_reply":"2022-05-20T05:23:27.60898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:23:27.61444Z","iopub.execute_input":"2022-05-20T05:23:27.616975Z","iopub.status.idle":"2022-05-20T05:23:27.641373Z","shell.execute_reply.started":"2022-05-20T05:23:27.616933Z","shell.execute_reply":"2022-05-20T05:23:27.640431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"20000/32","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:23:27.644089Z","iopub.execute_input":"2022-05-20T05:23:27.644483Z","iopub.status.idle":"2022-05-20T05:23:27.650031Z","shell.execute_reply.started":"2022-05-20T05:23:27.644448Z","shell.execute_reply":"2022-05-20T05:23:27.649244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_generator, epochs = 3, validation_data = valid_generator )","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:23:27.65148Z","iopub.execute_input":"2022-05-20T05:23:27.652168Z","iopub.status.idle":"2022-05-20T05:28:11.331206Z","shell.execute_reply.started":"2022-05-20T05:23:27.652122Z","shell.execute_reply":"2022-05-20T05:28:11.330467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = idg.flow_from_dataframe(test, x_col = 'path', y_col = None,\n                                         class_mode = None, target_size = (100,100),\n                                         shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:28:11.332981Z","iopub.execute_input":"2022-05-20T05:28:11.333247Z","iopub.status.idle":"2022-05-20T05:28:11.432428Z","shell.execute_reply.started":"2022-05-20T05:28:11.333207Z","shell.execute_reply":"2022-05-20T05:28:11.431442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = model.predict(test_generator , verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:28:11.433881Z","iopub.execute_input":"2022-05-20T05:28:11.434152Z","iopub.status.idle":"2022-05-20T05:28:53.565988Z","shell.execute_reply.started":"2022-05-20T05:28:11.434117Z","shell.execute_reply":"2022-05-20T05:28:53.565191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:28:53.567384Z","iopub.execute_input":"2022-05-20T05:28:53.567661Z","iopub.status.idle":"2022-05-20T05:28:53.574471Z","shell.execute_reply.started":"2022-05-20T05:28:53.567627Z","shell.execute_reply":"2022-05-20T05:28:53.573722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import load_img\n\n\nn = 10\nfor i, (index, row) in enumerate(test.iterrows()):\n    if i >= n:\n        break\n    fig = plt.figure(figsize=(8, 32))\n    img = load_img(row['path'], target_size=(100, 100))\n    subfig = fig.add_subplot(n, 1, i + 1)\n    pred = result[i][0]\n    pred_label = 'cat' if pred > 0.5 else 'dog'\n    pred = pred if pred > 0.5 else 1-pred\n    plt.title('Looks like a {0} with probability {1}'.format(pred_label, pred))\n    f = plt.imshow(img)\n    f.axes.get_xaxis().set_visible(False)\n    f.axes.get_yaxis().set_visible(False)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:28:53.575893Z","iopub.execute_input":"2022-05-20T05:28:53.57644Z","iopub.status.idle":"2022-05-20T05:28:54.576177Z","shell.execute_reply.started":"2022-05-20T05:28:53.576402Z","shell.execute_reply":"2022-05-20T05:28:54.575411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')\nsub","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:28:54.581491Z","iopub.execute_input":"2022-05-20T05:28:54.583681Z","iopub.status.idle":"2022-05-20T05:28:54.614688Z","shell.execute_reply.started":"2022-05-20T05:28:54.583621Z","shell.execute_reply":"2022-05-20T05:28:54.613686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:28:54.617481Z","iopub.execute_input":"2022-05-20T05:28:54.617967Z","iopub.status.idle":"2022-05-20T05:28:54.631545Z","shell.execute_reply.started":"2022-05-20T05:28:54.617938Z","shell.execute_reply":"2022-05-20T05:28:54.630656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['id'] = test['path'].apply(lambda x : x.split('/')[1].split('.')[0])\n\nsub['label'] = result[:,1].clip(0.005, 0.995)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:28:54.63506Z","iopub.execute_input":"2022-05-20T05:28:54.63527Z","iopub.status.idle":"2022-05-20T05:28:54.650685Z","shell.execute_reply.started":"2022-05-20T05:28:54.635242Z","shell.execute_reply":"2022-05-20T05:28:54.649874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('base_aug.csv', index = 0)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:28:54.652093Z","iopub.execute_input":"2022-05-20T05:28:54.656454Z","iopub.status.idle":"2022-05-20T05:28:54.693707Z","shell.execute_reply.started":"2022-05-20T05:28:54.656419Z","shell.execute_reply":"2022-05-20T05:28:54.692804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}