{"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[:3]:\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":"2021-10-27T03:59:14.631985Z","iopub.execute_input":"2021-10-27T03:59:14.632859Z","iopub.status.idle":"2021-10-27T03:59:14.666951Z","shell.execute_reply.started":"2021-10-27T03:59:14.63274Z","shell.execute_reply":"2021-10-27T03:59:14.665701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q -o '/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip'","metadata":{"execution":{"iopub.status.busy":"2021-10-27T03:59:14.66969Z","iopub.execute_input":"2021-10-27T03:59:14.670194Z","iopub.status.idle":"2021-10-27T03:59:26.101262Z","shell.execute_reply.started":"2021-10-27T03:59:14.670152Z","shell.execute_reply":"2021-10-27T03:59:26.100085Z"},"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":"2021-10-27T03:59:26.10365Z","iopub.execute_input":"2021-10-27T03:59:26.104441Z","iopub.status.idle":"2021-10-27T03:59:32.017096Z","shell.execute_reply.started":"2021-10-27T03:59:26.104395Z","shell.execute_reply":"2021-10-27T03:59:32.015754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test/10007.jpg","metadata":{"execution":{"iopub.status.busy":"2021-10-27T03:59:32.020407Z","iopub.execute_input":"2021-10-27T03:59:32.021077Z","iopub.status.idle":"2021-10-27T03:59:32.027374Z","shell.execute_reply.started":"2021-10-27T03:59:32.021033Z","shell.execute_reply":"2021-10-27T03:59:32.026205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train/cat.4511.jpg","metadata":{"execution":{"iopub.status.busy":"2021-10-27T03:59:32.032058Z","iopub.execute_input":"2021-10-27T03:59:32.032762Z","iopub.status.idle":"2021-10-27T03:59:32.042451Z","shell.execute_reply.started":"2021-10-27T03:59:32.032714Z","shell.execute_reply":"2021-10-27T03:59:32.041286Z"},"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":"2021-10-27T03:59:32.044542Z","iopub.execute_input":"2021-10-27T03:59:32.045268Z","iopub.status.idle":"2021-10-27T03:59:32.132882Z","shell.execute_reply.started":"2021-10-27T03:59:32.045226Z","shell.execute_reply":"2021-10-27T03:59:32.132054Z"},"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":"2021-10-27T03:59:32.134133Z","iopub.execute_input":"2021-10-27T03:59:32.134456Z","iopub.status.idle":"2021-10-27T03:59:32.331366Z","shell.execute_reply.started":"2021-10-27T03:59:32.134401Z","shell.execute_reply":"2021-10-27T03:59:32.330332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = train['path'].apply(lambda x: x.split('/')[1])\na","metadata":{"execution":{"iopub.status.busy":"2021-10-27T03:59:32.336783Z","iopub.execute_input":"2021-10-27T03:59:32.339407Z","iopub.status.idle":"2021-10-27T03:59:32.386824Z","shell.execute_reply.started":"2021-10-27T03:59:32.339362Z","shell.execute_reply":"2021-10-27T03:59:32.385557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['target'] = train['path'].apply(lambda x: x.split('/')[1].split('.')[0])","metadata":{"execution":{"iopub.status.busy":"2021-10-27T03:59:32.388064Z","iopub.execute_input":"2021-10-27T03:59:32.388344Z","iopub.status.idle":"2021-10-27T03:59:32.431191Z","shell.execute_reply.started":"2021-10-27T03:59:32.388306Z","shell.execute_reply":"2021-10-27T03:59:32.430181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2021-10-27T03:59:32.436458Z","iopub.execute_input":"2021-10-27T03:59:32.43718Z","iopub.status.idle":"2021-10-27T03:59:32.461692Z","shell.execute_reply.started":"2021-10-27T03:59:32.437132Z","shell.execute_reply":"2021-10-27T03:59:32.460649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\nidg2 = ImageDataGenerator(horizontal_flip = True,\n                        brightness_range = [0.2, 1.0])\n\nidg = ImageDataGenerator()","metadata":{"execution":{"iopub.status.busy":"2021-10-27T05:18:14.048257Z","iopub.execute_input":"2021-10-27T05:18:14.049104Z","iopub.status.idle":"2021-10-27T05:18:14.0562Z","shell.execute_reply.started":"2021-10-27T05:18:14.049046Z","shell.execute_reply":"2021-10-27T05:18:14.054978Z"},"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,\n                                   random_state = 42, stratify = train['target'])","metadata":{"execution":{"iopub.status.busy":"2021-10-27T03:59:37.639653Z","iopub.execute_input":"2021-10-27T03:59:37.640309Z","iopub.status.idle":"2021-10-27T03:59:38.330344Z","shell.execute_reply.started":"2021-10-27T03:59:37.640248Z","shell.execute_reply":"2021-10-27T03:59:38.329354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_valid['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-10-27T03:59:38.334088Z","iopub.execute_input":"2021-10-27T03:59:38.334342Z","iopub.status.idle":"2021-10-27T03:59:38.344419Z","shell.execute_reply.started":"2021-10-27T03:59:38.33431Z","shell.execute_reply":"2021-10-27T03:59:38.343126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-10-27T03:59:38.349503Z","iopub.execute_input":"2021-10-27T03:59:38.350211Z","iopub.status.idle":"2021-10-27T03:59:38.36513Z","shell.execute_reply.started":"2021-10-27T03:59:38.350165Z","shell.execute_reply":"2021-10-27T03:59:38.364115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = idg2.flow_from_dataframe(x_train, x_col = 'path',\n                                         y_col = 'target', target_size = (100,100))","metadata":{"execution":{"iopub.status.busy":"2021-10-27T05:18:52.142245Z","iopub.execute_input":"2021-10-27T05:18:52.142549Z","iopub.status.idle":"2021-10-27T05:18:52.353598Z","shell.execute_reply.started":"2021-10-27T05:18:52.142517Z","shell.execute_reply":"2021-10-27T05:18:52.351812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator = idg.flow_from_dataframe(x_valid, x_col = 'path',\n                                         y_col = 'target', target_size = (100,100))","metadata":{"execution":{"iopub.status.busy":"2021-10-27T03:59:38.930452Z","iopub.execute_input":"2021-10-27T03:59:38.931541Z","iopub.status.idle":"2021-10-27T03:59:38.998455Z","shell.execute_reply.started":"2021-10-27T03:59:38.931494Z","shell.execute_reply":"2021-10-27T03:59:38.997443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator","metadata":{"execution":{"iopub.status.busy":"2021-10-27T03:59:39.000017Z","iopub.execute_input":"2021-10-27T03:59:39.000343Z","iopub.status.idle":"2021-10-27T03:59:39.00818Z","shell.execute_reply.started":"2021-10-27T03:59:39.000271Z","shell.execute_reply":"2021-10-27T03:59:39.006872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\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":"2021-10-27T05:19:00.568245Z","iopub.execute_input":"2021-10-27T05:19:00.568897Z","iopub.status.idle":"2021-10-27T05:19:04.258463Z","shell.execute_reply.started":"2021-10-27T05:19:00.56886Z","shell.execute_reply":"2021-10-27T05:19:04.256935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import Sequential\n\nfrom tensorflow.keras.layers import *\n\nfrom tensorflow.keras.applications import EfficientNetB1","metadata":{"execution":{"iopub.status.busy":"2021-10-27T05:32:01.573477Z","iopub.execute_input":"2021-10-27T05:32:01.57408Z","iopub.status.idle":"2021-10-27T05:32:01.57975Z","shell.execute_reply.started":"2021-10-27T05:32:01.574022Z","shell.execute_reply":"2021-10-27T05:32:01.57844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eb0 = EfficientNetB1(include_top = False, pooling = 'avg')","metadata":{"execution":{"iopub.status.busy":"2021-10-27T05:32:01.583056Z","iopub.execute_input":"2021-10-27T05:32:01.583736Z","iopub.status.idle":"2021-10-27T05:32:04.921085Z","shell.execute_reply.started":"2021-10-27T05:32:01.583683Z","shell.execute_reply":"2021-10-27T05:32:04.91996Z"},"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, \n                            lr = 0.01))","metadata":{"execution":{"iopub.status.busy":"2021-10-27T05:32:04.923597Z","iopub.execute_input":"2021-10-27T05:32:04.9243Z","iopub.status.idle":"2021-10-27T05:32:06.399225Z","shell.execute_reply.started":"2021-10-27T05:32:04.924253Z","shell.execute_reply":"2021-10-27T05:32:06.398283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-27T05:32:06.400664Z","iopub.execute_input":"2021-10-27T05:32:06.401422Z","iopub.status.idle":"2021-10-27T05:32:06.435396Z","shell.execute_reply.started":"2021-10-27T05:32:06.401373Z","shell.execute_reply":"2021-10-27T05:32:06.434137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_generator, epochs = 5,\n         validation_data = valid_generator)","metadata":{"execution":{"iopub.status.busy":"2021-10-27T05:32:06.43885Z","iopub.execute_input":"2021-10-27T05:32:06.439328Z","iopub.status.idle":"2021-10-27T05:38:34.083601Z","shell.execute_reply.started":"2021-10-27T05:32:06.439279Z","shell.execute_reply":"2021-10-27T05:38:34.082471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.DataFrame({'path' : glob.glob('test/*')})\n\ntest","metadata":{"execution":{"iopub.status.busy":"2021-10-27T05:38:34.085593Z","iopub.execute_input":"2021-10-27T05:38:34.085989Z","iopub.status.idle":"2021-10-27T05:38:34.14989Z","shell.execute_reply.started":"2021-10-27T05:38:34.085905Z","shell.execute_reply":"2021-10-27T05:38:34.148854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = idg.flow_from_dataframe(test, x_col = 'path',\n                                        y_col = None, \n                                        class_mode = None,\n                                        target_size = (100,100),\n                                        shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2021-10-27T05:38:34.151403Z","iopub.execute_input":"2021-10-27T05:38:34.15178Z","iopub.status.idle":"2021-10-27T05:38:34.274052Z","shell.execute_reply.started":"2021-10-27T05:38:34.151738Z","shell.execute_reply":"2021-10-27T05:38:34.272495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = model.predict(test_generator, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2021-10-27T05:38:34.275396Z","iopub.execute_input":"2021-10-27T05:38:34.2757Z","iopub.status.idle":"2021-10-27T05:39:14.812942Z","shell.execute_reply.started":"2021-10-27T05:38:34.275659Z","shell.execute_reply":"2021-10-27T05:39:14.811983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result","metadata":{"execution":{"iopub.status.busy":"2021-10-27T05:39:14.814847Z","iopub.execute_input":"2021-10-27T05:39:14.815567Z","iopub.status.idle":"2021-10-27T05:39:14.824006Z","shell.execute_reply.started":"2021-10-27T05:39:14.815521Z","shell.execute_reply":"2021-10-27T05:39:14.822724Z"},"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":"2021-10-27T05:39:14.827803Z","iopub.execute_input":"2021-10-27T05:39:14.82815Z","iopub.status.idle":"2021-10-27T05:39:16.34828Z","shell.execute_reply.started":"2021-10-27T05:39:14.828099Z","shell.execute_reply":"2021-10-27T05:39:16.347164Z"},"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":"2021-10-27T05:39:16.350526Z","iopub.execute_input":"2021-10-27T05:39:16.351258Z","iopub.status.idle":"2021-10-27T05:39:16.378071Z","shell.execute_reply.started":"2021-10-27T05:39:16.351208Z","shell.execute_reply":"2021-10-27T05:39:16.376969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['id'] = test['path'].apply(lambda x : x.split('/')[1].split('.')[0] )","metadata":{"execution":{"iopub.status.busy":"2021-10-27T05:39:16.381582Z","iopub.execute_input":"2021-10-27T05:39:16.382629Z","iopub.status.idle":"2021-10-27T05:39:16.402747Z","shell.execute_reply.started":"2021-10-27T05:39:16.382585Z","shell.execute_reply":"2021-10-27T05:39:16.401559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['label'] = result[:,1].clip(0.005, 0.995)\nsub","metadata":{"execution":{"iopub.status.busy":"2021-10-27T05:39:16.406841Z","iopub.execute_input":"2021-10-27T05:39:16.407153Z","iopub.status.idle":"2021-10-27T05:39:16.423317Z","shell.execute_reply.started":"2021-10-27T05:39:16.407103Z","shell.execute_reply":"2021-10-27T05:39:16.42189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('aug-sgd.csv', index = 0)","metadata":{"execution":{"iopub.status.busy":"2021-10-27T05:39:16.425272Z","iopub.execute_input":"2021-10-27T05:39:16.426348Z","iopub.status.idle":"2021-10-27T05:39:16.47138Z","shell.execute_reply.started":"2021-10-27T05:39:16.426299Z","shell.execute_reply":"2021-10-27T05:39:16.470441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}