{"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":"2023-07-28T08:52:15.764142Z","iopub.execute_input":"2023-07-28T08:52:15.764424Z","iopub.status.idle":"2023-07-28T08:52:15.783129Z","shell.execute_reply.started":"2023-07-28T08:52:15.764397Z","shell.execute_reply":"2023-07-28T08:52:15.782125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-07-28T08:52:15.784901Z","iopub.execute_input":"2023-07-28T08:52:15.785250Z","iopub.status.idle":"2023-07-28T08:52:17.138028Z","shell.execute_reply.started":"2023-07-28T08:52:15.785225Z","shell.execute_reply":"2023-07-28T08:52:17.136782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport random\nimport pickle\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-07-28T08:52:17.139969Z","iopub.execute_input":"2023-07-28T08:52:17.140731Z","iopub.status.idle":"2023-07-28T08:52:17.378608Z","shell.execute_reply.started":"2023-07-28T08:52:17.140691Z","shell.execute_reply":"2023-07-28T08:52:17.377509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import 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":"2023-07-28T08:52:17.380348Z","iopub.execute_input":"2023-07-28T08:52:17.381202Z","iopub.status.idle":"2023-07-28T08:52:27.182629Z","shell.execute_reply.started":"2023-07-28T08:52:17.381161Z","shell.execute_reply":"2023-07-28T08:52:27.181604Z"},"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')","metadata":{"execution":{"iopub.status.busy":"2023-07-28T08:52:27.188938Z","iopub.execute_input":"2023-07-28T08:52:27.191687Z","iopub.status.idle":"2023-07-28T08:52:37.392458Z","shell.execute_reply.started":"2023-07-28T08:52:27.191648Z","shell.execute_reply":"2023-07-28T08:52:37.391377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with 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":"2023-07-28T08:52:37.394044Z","iopub.execute_input":"2023-07-28T08:52:37.394412Z","iopub.status.idle":"2023-07-28T08:52:43.006644Z","shell.execute_reply.started":"2023-07-28T08:52:37.394378Z","shell.execute_reply":"2023-07-28T08:52:43.004625Z"},"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":"2023-07-28T08:52:43.008255Z","iopub.execute_input":"2023-07-28T08:52:43.008611Z","iopub.status.idle":"2023-07-28T08:52:43.762496Z","shell.execute_reply.started":"2023-07-28T08:52:43.008576Z","shell.execute_reply":"2023-07-28T08:52:43.757205Z"},"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":"2023-07-28T08:52:43.764174Z","iopub.execute_input":"2023-07-28T08:52:43.764542Z","iopub.status.idle":"2023-07-28T08:53:08.138767Z","shell.execute_reply.started":"2023-07-28T08:52:43.764510Z","shell.execute_reply":"2023-07-28T08:53:08.137824Z"},"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":"2023-07-28T08:53:08.141186Z","iopub.execute_input":"2023-07-28T08:53:08.141797Z","iopub.status.idle":"2023-07-28T08:53:08.840927Z","shell.execute_reply.started":"2023-07-28T08:53:08.141760Z","shell.execute_reply":"2023-07-28T08:53:08.839837Z"},"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":"2023-07-28T08:53:08.842314Z","iopub.execute_input":"2023-07-28T08:53:08.842767Z","iopub.status.idle":"2023-07-28T08:53:20.407833Z","shell.execute_reply.started":"2023-07-28T08:53:08.842734Z","shell.execute_reply":"2023-07-28T08:53:20.406828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(training_data)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T08:53:20.409348Z","iopub.execute_input":"2023-07-28T08:53:20.410381Z","iopub.status.idle":"2023-07-28T08:53:20.417916Z","shell.execute_reply.started":"2023-07-28T08:53:20.410341Z","shell.execute_reply":"2023-07-28T08:53:20.416711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.shuffle(training_data)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T08:53:20.419610Z","iopub.execute_input":"2023-07-28T08:53:20.420046Z","iopub.status.idle":"2023-07-28T08:53:20.450263Z","shell.execute_reply.started":"2023-07-28T08:53:20.420009Z","shell.execute_reply":"2023-07-28T08:53:20.449063Z"},"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":"2023-07-28T08:53:20.451752Z","iopub.execute_input":"2023-07-28T08:53:20.453359Z","iopub.status.idle":"2023-07-28T08:53:20.577038Z","shell.execute_reply.started":"2023-07-28T08:53:20.453325Z","shell.execute_reply":"2023-07-28T08:53:20.575975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pickle_out =open(\"X.pickle\", \"wb\")\npickle.dump(X, pickle_out)\npickle_out.close()\n\npickle_out = open(\"y.pickle\", \"wb\")\npickle.dump(y, pickle_out)\npickle_out.close()","metadata":{"execution":{"iopub.status.busy":"2023-07-28T08:53:20.582086Z","iopub.execute_input":"2023-07-28T08:53:20.582396Z","iopub.status.idle":"2023-07-28T08:53:20.992586Z","shell.execute_reply.started":"2023-07-28T08:53:20.582371Z","shell.execute_reply":"2023-07-28T08:53:20.991549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pickle_in = open(\"X.pickle\", \"rb\")\nX = pickle.load(pickle_in)\n\npickle_in = open(\"y.pickle\", \"rb\")\ny = pickle.load(pickle_in)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T08:53:20.994209Z","iopub.execute_input":"2023-07-28T08:53:20.994586Z","iopub.status.idle":"2023-07-28T08:53:21.139040Z","shell.execute_reply.started":"2023-07-28T08:53:20.994551Z","shell.execute_reply":"2023-07-28T08:53:21.137926Z"},"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":"2023-07-28T08:53:21.140788Z","iopub.execute_input":"2023-07-28T08:53:21.141176Z","iopub.status.idle":"2023-07-28T08:53:22.488841Z","shell.execute_reply.started":"2023-07-28T08:53:21.141140Z","shell.execute_reply":"2023-07-28T08:53:22.487841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T08:53:22.490342Z","iopub.execute_input":"2023-07-28T08:53:22.491300Z","iopub.status.idle":"2023-07-28T08:53:22.499665Z","shell.execute_reply.started":"2023-07-28T08:53:22.491265Z","shell.execute_reply":"2023-07-28T08:53:22.498382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T08:53:22.501456Z","iopub.execute_input":"2023-07-28T08:53:22.501844Z","iopub.status.idle":"2023-07-28T08:53:22.511488Z","shell.execute_reply.started":"2023-07-28T08:53:22.501811Z","shell.execute_reply":"2023-07-28T08:53:22.510346Z"},"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.2, random_state=50)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T08:53:22.513506Z","iopub.execute_input":"2023-07-28T08:53:22.513842Z","iopub.status.idle":"2023-07-28T08:53:23.102766Z","shell.execute_reply.started":"2023-07-28T08:53:22.513811Z","shell.execute_reply":"2023-07-28T08:53:23.101649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X_train)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T08:53:23.104713Z","iopub.execute_input":"2023-07-28T08:53:23.105143Z","iopub.status.idle":"2023-07-28T08:53:23.112547Z","shell.execute_reply.started":"2023-07-28T08:53:23.105083Z","shell.execute_reply":"2023-07-28T08:53:23.111297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_train)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T08:53:23.114643Z","iopub.execute_input":"2023-07-28T08:53:23.115037Z","iopub.status.idle":"2023-07-28T08:53:23.126632Z","shell.execute_reply.started":"2023-07-28T08:53:23.114990Z","shell.execute_reply":"2023-07-28T08:53:23.125678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = 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()) \n\nmodel.add(Dense(64))\nmodel.add(Dense(1))\n\nmodel.add(Activation('sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2023-07-28T08:53:23.128543Z","iopub.execute_input":"2023-07-28T08:53:23.128897Z","iopub.status.idle":"2023-07-28T08:53:27.817810Z","shell.execute_reply.started":"2023-07-28T08:53:23.128867Z","shell.execute_reply":"2023-07-28T08:53:27.816802Z"},"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":"2023-07-28T08:53:27.819099Z","iopub.execute_input":"2023-07-28T08:53:27.819458Z","iopub.status.idle":"2023-07-28T09:02:35.922781Z","shell.execute_reply.started":"2023-07-28T08:53:27.819425Z","shell.execute_reply":"2023-07-28T09:02:35.921517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-07-28T09:02:35.924663Z","iopub.execute_input":"2023-07-28T09:02:35.925219Z","iopub.status.idle":"2023-07-28T09:02:39.213271Z","shell.execute_reply.started":"2023-07-28T09:02:35.925181Z","shell.execute_reply":"2023-07-28T09:02:39.212254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_dict = history.history\n\nloss_values = history_dict['loss']\nval_loss_values = history_dict['val_loss']\n\nepochs = range(1, len(loss_values)+1)\n\nline1 = plt.plot(epochs, val_loss_values, label ='Validation/Test Loss')\nline2 = plt.plot(epochs, loss_values, label='Training Loss')\nplt.setp(line1, linewidth = 2.0, marker='+', markersize=10.0)\nplt.setp(line2, linewidth=2.0, marker='4', markersize=10.0)\nplt.legend()\nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-28T09:02:39.214630Z","iopub.execute_input":"2023-07-28T09:02:39.215062Z","iopub.status.idle":"2023-07-28T09:02:39.805516Z","shell.execute_reply.started":"2023-07-28T09:02:39.215029Z","shell.execute_reply":"2023-07-28T09:02:39.804418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_dict = history.history\n\nacc_values = history_dict['accuracy']\nval_acc_values = history_dict['val_accuracy']\nepochs = range(1, len(loss_values)+1)\n\nline1 = plt.plot(epochs, val_loss_values, label='Validation/Test Accuracy')\nline2 = plt.plot(epochs, acc_values, label ='Training Accuracy')\n\nplt.setp(line1, linewidth=2.0, marker='+', markersize=10.0)\nplt.setp(line2, linewidth=2.0, marker='4', markersize=10.0)\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.grid(True)\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-28T09:02:39.806936Z","iopub.execute_input":"2023-07-28T09:02:39.807367Z","iopub.status.idle":"2023-07-28T09:02:40.109089Z","shell.execute_reply.started":"2023-07-28T09:02:39.807334Z","shell.execute_reply":"2023-07-28T09:02:40.108062Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T09:02:40.110376Z","iopub.execute_input":"2023-07-28T09:02:40.110745Z","iopub.status.idle":"2023-07-28T09:02:40.170668Z","shell.execute_reply.started":"2023-07-28T09:02:40.110711Z","shell.execute_reply":"2023-07-28T09:02:40.169585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test/255.0\n\ntest = np.array(test)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T09:02:40.172270Z","iopub.execute_input":"2023-07-28T09:02:40.172635Z","iopub.status.idle":"2023-07-28T09:02:42.249854Z","shell.execute_reply.started":"2023-07-28T09:02:40.172602Z","shell.execute_reply":"2023-07-28T09:02:42.248535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(test) ","metadata":{"execution":{"iopub.status.busy":"2023-07-28T09:02:42.251432Z","iopub.execute_input":"2023-07-28T09:02:42.251794Z","iopub.status.idle":"2023-07-28T09:02:53.748103Z","shell.execute_reply.started":"2023-07-28T09:02:42.251757Z","shell.execute_reply":"2023-07-28T09:02:53.746912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction\nprediction.shape\nmy_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')\nmy_submission.head()\nmy_submission['label'] = prediction\nmy_submission['label'] = my_submission['label'].map(lambda x: 1 if x >= 0.5 else 0 )\nmy_submission.head()\nmy_submission['label'] = my_submission['label'].round(1)\nmy_submission.to_csv('my_submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T09:02:53.750348Z","iopub.execute_input":"2023-07-28T09:02:53.750990Z","iopub.status.idle":"2023-07-28T09:02:53.848979Z","shell.execute_reply.started":"2023-07-28T09:02:53.750950Z","shell.execute_reply":"2023-07-28T09:02:53.847905Z"},"trusted":true},"execution_count":null,"outputs":[]}]}