{"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":"2021-06-25T13:57:41.782361Z","iopub.execute_input":"2021-06-25T13:57:41.782700Z","iopub.status.idle":"2021-06-25T13:57:41.794130Z","shell.execute_reply.started":"2021-06-25T13:57:41.782619Z","shell.execute_reply":"2021-06-25T13:57:41.793058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:57:43.523653Z","iopub.execute_input":"2021-06-25T13:57:43.523975Z","iopub.status.idle":"2021-06-25T13:57:48.340760Z","shell.execute_reply.started":"2021-06-25T13:57:43.523942Z","shell.execute_reply":"2021-06-25T13:57:48.339988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd && ls","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:57:50.330039Z","iopub.execute_input":"2021-06-25T13:57:50.330373Z","iopub.status.idle":"2021-06-25T13:57:50.976821Z","shell.execute_reply.started":"2021-06-25T13:57:50.330343Z","shell.execute_reply":"2021-06-25T13:57:50.975965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Unzip + Import data","metadata":{}},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile('../input/dogs-vs-cats/test1.zip', 'r') as zip_ref:\n    zip_ref.extractall('./')","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:57:54.253732Z","iopub.execute_input":"2021-06-25T13:57:54.254065Z","iopub.status.idle":"2021-06-25T13:58:01.214056Z","shell.execute_reply.started":"2021-06-25T13:57:54.254033Z","shell.execute_reply":"2021-06-25T13:58:01.213217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile('../input/dogs-vs-cats/train.zip', 'r') as zip_ref:\n    zip_ref.extractall('./')","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:58:03.178332Z","iopub.execute_input":"2021-06-25T13:58:03.178649Z","iopub.status.idle":"2021-06-25T13:58:15.520844Z","shell.execute_reply.started":"2021-06-25T13:58:03.178621Z","shell.execute_reply":"2021-06-25T13:58:15.520030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Categorize each file so we can create the labels\n* first try marked as comment, didn't use because of efficiency problems","metadata":{}},{"cell_type":"code","source":"y_datset = []\nx_datset = []\n#col_names = ['file', 'cat/dog']\n#datset = pd.DataFrame(columns=col_names)\nmin_dim = 0\n\nfor file in os.listdir('./train'):\n    x_datset.append(file)   \n    if file.startswith('cat'):\n        y_datset.append('0')\n        #datset.loc[len(datset)] = [file, 0]\n    else:\n        y_datset.append('1')\n        #datset.loc[len(datset)] = [file, 1]\nprint(y_datset[:5])\n\ntrain_df = pd.DataFrame({'filename': x_datset, 'class': y_datset})\nprint(train_df.head())","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:58:16.992210Z","iopub.execute_input":"2021-06-25T13:58:16.992533Z","iopub.status.idle":"2021-06-25T13:58:17.038566Z","shell.execute_reply.started":"2021-06-25T13:58:16.992502Z","shell.execute_reply":"2021-06-25T13:58:17.037558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create TestSet to evaluate","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n\nx_train, x_test= train_test_split(train_df, test_size=0.33, random_state=42)\nx_train = x_train.reset_index(drop=True)\nx_test = x_test.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:58:19.617349Z","iopub.execute_input":"2021-06-25T13:58:19.617662Z","iopub.status.idle":"2021-06-25T13:58:20.200495Z","shell.execute_reply.started":"2021-06-25T13:58:19.617633Z","shell.execute_reply":"2021-06-25T13:58:20.199672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_testImg = plt.imread('./train/'+x_test.iloc[0,0])\n#print(x_testImg[0])\nplt.imshow(x_testImg, cmap=plt.cm.binary)\nplt.show()\nprint(x_test['class'])","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:58:21.070029Z","iopub.execute_input":"2021-06-25T13:58:21.070465Z","iopub.status.idle":"2021-06-25T13:58:21.947606Z","shell.execute_reply.started":"2021-06-25T13:58:21.070424Z","shell.execute_reply":"2021-06-25T13:58:21.946807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_trainImg = plt.imread('./train/'+x_train.iloc[0,0])\nprint(x_trainImg[0])\nplt.imshow(x_trainImg, cmap=plt.cm.binary)\nplt.show()\nprint(x_train['class'])","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:58:24.273597Z","iopub.execute_input":"2021-06-25T13:58:24.273941Z","iopub.status.idle":"2021-06-25T13:58:24.454987Z","shell.execute_reply.started":"2021-06-25T13:58:24.273898Z","shell.execute_reply":"2021-06-25T13:58:24.454015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing Data","metadata":{}},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\n# create two generators to augment/scale input data\ntrain_datagen = ImageDataGenerator(rescale=1./255, horizontal_flip=True)\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\n# create batches of data\ndir_train = './train'\nx_col = 'filename'\ny_col='class'\nclass_mode = 'binary'\ntarget_size=(224,224)\ntrain_generator = train_datagen.flow_from_dataframe(x_train, directory=dir_train, x_col=x_col, y_col=y_col, class_mode=class_mode, target_size=target_size)\ntest_generator = test_datagen.flow_from_dataframe(x_test, directory=dir_train, x_col=x_col, y_col=y_col, class_mode=class_mode, target_size=target_size)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:58:27.515456Z","iopub.execute_input":"2021-06-25T13:58:27.515774Z","iopub.status.idle":"2021-06-25T13:58:27.840753Z","shell.execute_reply.started":"2021-06-25T13:58:27.515745Z","shell.execute_reply":"2021-06-25T13:58:27.839815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(train_generator))","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:58:29.259539Z","iopub.execute_input":"2021-06-25T13:58:29.259890Z","iopub.status.idle":"2021-06-25T13:58:29.264386Z","shell.execute_reply.started":"2021-06-25T13:58:29.259860Z","shell.execute_reply":"2021-06-25T13:58:29.263341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, labels = next(train_generator)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:58:30.021556Z","iopub.execute_input":"2021-06-25T13:58:30.021874Z","iopub.status.idle":"2021-06-25T13:58:30.105674Z","shell.execute_reply.started":"2021-06-25T13:58:30.021843Z","shell.execute_reply":"2021-06-25T13:58:30.104837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotImages(images_arr):\n    fig, axes = plt.subplots(1, 5, figsize=(20,20))\n    axes = axes.flatten()\n    for img, ax in zip( images_arr, axes):\n        ax.imshow(img)\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:58:31.199093Z","iopub.execute_input":"2021-06-25T13:58:31.199422Z","iopub.status.idle":"2021-06-25T13:58:31.205221Z","shell.execute_reply.started":"2021-06-25T13:58:31.199394Z","shell.execute_reply":"2021-06-25T13:58:31.204242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotImages(img)\nprint(labels)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:58:32.056425Z","iopub.execute_input":"2021-06-25T13:58:32.056828Z","iopub.status.idle":"2021-06-25T13:58:33.014416Z","shell.execute_reply.started":"2021-06-25T13:58:32.056792Z","shell.execute_reply":"2021-06-25T13:58:33.008640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = plt.imread('./train/dog.12493.jpg')\nprint(img.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:58:36.534839Z","iopub.execute_input":"2021-06-25T13:58:36.535177Z","iopub.status.idle":"2021-06-25T13:58:36.543567Z","shell.execute_reply.started":"2021-06-25T13:58:36.535148Z","shell.execute_reply":"2021-06-25T13:58:36.542245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Model","metadata":{}},{"cell_type":"code","source":"model = keras.Sequential()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:58:38.610424Z","iopub.execute_input":"2021-06-25T13:58:38.610743Z","iopub.status.idle":"2021-06-25T13:58:40.549862Z","shell.execute_reply.started":"2021-06-25T13:58:38.610715Z","shell.execute_reply":"2021-06-25T13:58:40.549043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(keras.layers.Conv2D(32, (3, 3), padding='same',input_shape=(224,224,3), activation='relu'))\n#model.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.MaxPool2D(pool_size=(2, 2)))\n\nmodel.add(keras.layers.Conv2D(64, (3, 3), padding='same', activation='relu'))\n#model.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.MaxPool2D(pool_size=(2, 2)))\n\nmodel.add(keras.layers.Conv2D(64, (3, 3), padding='same', activation='relu'))\n#model.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.MaxPool2D(pool_size=(2, 2)))\n\nmodel.add(keras.layers.Conv2D(128, (3, 3), padding='same', activation='relu'))\n#model.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.MaxPool2D(pool_size=(2, 2)))\n\nmodel.add(keras.layers.Flatten())\nmodel.add(keras.layers.Dense(512, activation='relu'))\nmodel.add(keras.layers.Dense(1, activation='sigmoid'))\n\nmodel.compile(optimizer='adam', loss=\"binary_crossentropy\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:58:41.695878Z","iopub.execute_input":"2021-06-25T13:58:41.696217Z","iopub.status.idle":"2021-06-25T13:58:41.805147Z","shell.execute_reply.started":"2021-06-25T13:58:41.696187Z","shell.execute_reply":"2021-06-25T13:58:41.804345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#eS = keras.callbacks.EarlyStopping(min_delta=1, patience=0.0025)\nhist = model.fit_generator(generator=train_generator, validation_data=test_generator, epochs=5, steps_per_epoch=50)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:58:43.229707Z","iopub.execute_input":"2021-06-25T13:58:43.230167Z","iopub.status.idle":"2021-06-25T14:02:07.063849Z","shell.execute_reply.started":"2021-06-25T13:58:43.230137Z","shell.execute_reply":"2021-06-25T14:02:07.063018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(hist.history)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:16:40.561050Z","iopub.execute_input":"2021-06-25T14:16:40.561404Z","iopub.status.idle":"2021-06-25T14:16:40.566643Z","shell.execute_reply.started":"2021-06-25T14:16:40.561371Z","shell.execute_reply":"2021-06-25T14:16:40.565475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare + Predict test1.zip","metadata":{}},{"cell_type":"code","source":"x_col_test1 = []\nfor file in os.listdir('./test1'):\n    x_col_test1.append(file)\n    \n\ndir_test = './test1'\nx_test1 = pd.DataFrame({'filename': x_col_test1})\ntest1_generator = test_datagen.flow_from_dataframe(x_test1, directory=dir_test, x_col=x_col, y_col=None, class_mode=None, target_size=target_size)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:16:42.616484Z","iopub.execute_input":"2021-06-25T14:16:42.616800Z","iopub.status.idle":"2021-06-25T14:16:42.728361Z","shell.execute_reply.started":"2021-06-25T14:16:42.616769Z","shell.execute_reply":"2021-06-25T14:16:42.727478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test1_generator)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:16:43.566623Z","iopub.execute_input":"2021-06-25T14:16:43.566955Z","iopub.status.idle":"2021-06-25T14:17:27.706484Z","shell.execute_reply.started":"2021-06-25T14:16:43.566905Z","shell.execute_reply":"2021-06-25T14:17:27.705635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(pred.size)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:17:49.445725Z","iopub.execute_input":"2021-06-25T14:17:49.446078Z","iopub.status.idle":"2021-06-25T14:17:49.452442Z","shell.execute_reply.started":"2021-06-25T14:17:49.446048Z","shell.execute_reply":"2021-06-25T14:17:49.450088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(pred))\nprint(np.argmax(pred,1))\n\nsubm = pd.DataFrame({'id': np.arange(1,12501), 'label': np.argmax(pred,1)})\nsubm.to_csv('submission.csv', index=False)\nprint(subm)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:17:50.608288Z","iopub.execute_input":"2021-06-25T14:17:50.608637Z","iopub.status.idle":"2021-06-25T14:17:50.860015Z","shell.execute_reply.started":"2021-06-25T14:17:50.608607Z","shell.execute_reply":"2021-06-25T14:17:50.859101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}