{"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)\nfrom zipfile import ZipFile\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-07-11T09:05:10.165426Z","iopub.execute_input":"2022-07-11T09:05:10.166061Z","iopub.status.idle":"2022-07-11T09:05:10.174497Z","shell.execute_reply.started":"2022-07-11T09:05:10.166026Z","shell.execute_reply":"2022-07-11T09:05:10.172937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nimport cv2\nimport random\nimport pickle\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.datasets import cifar10\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Activation, Flatten\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:05:12.890336Z","iopub.execute_input":"2022-07-11T09:05:12.890758Z","iopub.status.idle":"2022-07-11T09:05:19.712137Z","shell.execute_reply.started":"2022-07-11T09:05:12.890722Z","shell.execute_reply":"2022-07-11T09:05:19.710962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip', 'r') as zip:\n    #zip.printdir()\n    zip.extractall()\n    print('done')\n\nwith ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip', 'r') as zip:\n    #zip.printdir()\n    zip.extractall()\n    print('done')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:05:24.379971Z","iopub.execute_input":"2022-07-11T09:05:24.380604Z","iopub.status.idle":"2022-07-11T09:05:41.394686Z","shell.execute_reply.started":"2022-07-11T09:05:24.380568Z","shell.execute_reply":"2022-07-11T09:05:41.393635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = '/kaggle/working/train'\nfilename = os.listdir(PATH)\nIMG_SIZE = 100\n\nplt.figure(figsize=(10,10))\nfor i in range(1, 7):\n    img_array = cv2.imread(os.path.join(PATH, filename[i]))\n    resize_image = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))\n    plt.subplot(3,3, i)\n    image = cv2.cvtColor(resize_image, cv2.COLOR_BGR2RGB)\n    #plt.figure()\n    plt.axis('off')\n    plt.imshow(image)  ","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:06:04.136741Z","iopub.execute_input":"2022-07-11T09:06:04.137135Z","iopub.status.idle":"2022-07-11T09:06:04.591791Z","shell.execute_reply.started":"2022-07-11T09:06:04.137101Z","shell.execute_reply":"2022-07-11T09:06:04.590879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_data = []\nIMG_SIZE = 100\npath = '/kaggle/working/train'\nfilenames = os.listdir('/kaggle/working/train')\nfor img in tqdm(filenames):\n    try:\n        if img.find('cat') == -1:\n            category = 0\n        else:\n            category = 1\n        \n        img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)\n        new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))\n        training_data.append([new_array, category])\n    except Exception as e:\n        pass","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:06:24.915818Z","iopub.execute_input":"2022-07-11T09:06:24.916476Z","iopub.status.idle":"2022-07-11T09:06:48.120487Z","shell.execute_reply.started":"2022-07-11T09:06:24.916426Z","shell.execute_reply":"2022-07-11T09:06:48.119144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor i in range(1, 7):\n    plt.subplot(3,3,i)\n    plt.axis('off')\n    if training_data[10+i][1] == 0:\n        plt.title('Dog')\n    else:\n        plt.title('Cat')\n    plt.imshow(training_data[10+i][0], cmap='gray_r')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:06:56.011631Z","iopub.execute_input":"2022-07-11T09:06:56.012205Z","iopub.status.idle":"2022-07-11T09:06:56.540654Z","shell.execute_reply.started":"2022-07-11T09:06:56.012168Z","shell.execute_reply":"2022-07-11T09:06:56.539693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testing_data = []\nIMG_SIZE = 100\n\npath = '/kaggle/working/test'\nfilenames = os.listdir('/kaggle/working/test')\nfor img in tqdm(filenames):\n    try:        \n        img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)\n        new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))\n        testing_data.append([new_array])\n    except Exception as e:\n        pass","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:07:01.637294Z","iopub.execute_input":"2022-07-11T09:07:01.637886Z","iopub.status.idle":"2022-07-11T09:07:12.902345Z","shell.execute_reply.started":"2022-07-11T09:07:01.637848Z","shell.execute_reply":"2022-07-11T09:07:12.901290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.shuffle(training_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:07:22.692612Z","iopub.execute_input":"2022-07-11T09:07:22.693189Z","iopub.status.idle":"2022-07-11T09:07:22.722563Z","shell.execute_reply.started":"2022-07-11T09:07:22.693151Z","shell.execute_reply":"2022-07-11T09:07:22.721671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = []\ny = []\n\nfor features, label  in training_data:\n    X.append(features)\n    y.append(label)\n    \nX = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:07:27.128091Z","iopub.execute_input":"2022-07-11T09:07:27.128820Z","iopub.status.idle":"2022-07-11T09:07:27.286910Z","shell.execute_reply.started":"2022-07-11T09:07:27.128779Z","shell.execute_reply":"2022-07-11T09:07:27.285692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X/255.0\n\nX = np.array(X)\ny = np.array(y)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:07:33.088134Z","iopub.execute_input":"2022-07-11T09:07:33.088742Z","iopub.status.idle":"2022-07-11T09:07:34.670480Z","shell.execute_reply.started":"2022-07-11T09:07:33.088698Z","shell.execute_reply":"2022-07-11T09:07:34.669442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state=50)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:07:36.965499Z","iopub.execute_input":"2022-07-11T09:07:36.966053Z","iopub.status.idle":"2022-07-11T09:07:37.653959Z","shell.execute_reply.started":"2022-07-11T09:07:36.966009Z","shell.execute_reply":"2022-07-11T09:07:37.652766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel = Sequential()\n\nmodel.add(Conv2D(256, (3,3), input_shape=X_train.shape[1:]))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(256, (3,3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors\n\nmodel.add(Dense(64))\nmodel.add(Dense(1))\n\nmodel.add(Activation('sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:07:39.637367Z","iopub.execute_input":"2022-07-11T09:07:39.639428Z","iopub.status.idle":"2022-07-11T09:07:42.656804Z","shell.execute_reply.started":"2022-07-11T09:07:39.639382Z","shell.execute_reply":"2022-07-11T09:07:42.655821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'],)\n\nhistory = model.fit(X_train, y_train, batch_size=32, epochs=15, validation_data = (x_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:07:45.769874Z","iopub.execute_input":"2022-07-11T09:07:45.770223Z","iopub.status.idle":"2022-07-11T09:10:32.537819Z","shell.execute_reply.started":"2022-07-11T09:07:45.770192Z","shell.execute_reply":"2022-07-11T09:10:32.535661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"score = model.evaluate(x_test, y_test, verbose=0)\nprint('Test Loss:', score[0])\nprint('Test accuracy:', score[1])","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:10:40.674192Z","iopub.execute_input":"2022-07-11T09:10:40.674892Z","iopub.status.idle":"2022-07-11T09:10:44.236708Z","shell.execute_reply.started":"2022-07-11T09:10:40.674854Z","shell.execute_reply":"2022-07-11T09:10:44.235697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 100\ntest = []\n\nfor features  in testing_data:\n    test.append(features)\n    \ntest = np.array(test).reshape(-1, IMG_SIZE, IMG_SIZE, 1)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:11:14.560950Z","iopub.execute_input":"2022-07-11T09:11:14.561380Z","iopub.status.idle":"2022-07-11T09:11:14.657308Z","shell.execute_reply.started":"2022-07-11T09:11:14.561343Z","shell.execute_reply":"2022-07-11T09:11:14.656293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test/255.0\n\ntest = np.array(test)\nprediction = model.predict(test) ","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:11:16.698475Z","iopub.execute_input":"2022-07-11T09:11:16.698840Z","iopub.status.idle":"2022-07-11T09:11:24.478379Z","shell.execute_reply.started":"2022-07-11T09:11:16.698808Z","shell.execute_reply":"2022-07-11T09:11:24.477315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')\nmy_submission['label'] = prediction\nmy_submission['label'] = my_submission['label'].round(1)\nmy_submission.to_csv('my_submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:11:33.162970Z","iopub.execute_input":"2022-07-11T09:11:33.164204Z","iopub.status.idle":"2022-07-11T09:11:33.207148Z","shell.execute_reply.started":"2022-07-11T09:11:33.164157Z","shell.execute_reply":"2022-07-11T09:11:33.206028Z"},"trusted":true},"execution_count":null,"outputs":[]}]}