{"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\nfrom zipfile import ZipFile\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-30T04:17:56.269103Z","iopub.execute_input":"2023-07-30T04:17:56.269790Z","iopub.status.idle":"2023-07-30T04:17:56.316333Z","shell.execute_reply.started":"2023-07-30T04:17:56.269670Z","shell.execute_reply":"2023-07-30T04:17:56.315013Z"},"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-30T04:17:56.319193Z","iopub.execute_input":"2023-07-30T04:17:56.320050Z","iopub.status.idle":"2023-07-30T04:17:57.698275Z","shell.execute_reply.started":"2023-07-30T04:17:56.320000Z","shell.execute_reply":"2023-07-30T04:17:57.696831Z"},"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-30T04:17:57.700307Z","iopub.execute_input":"2023-07-30T04:17:57.701055Z","iopub.status.idle":"2023-07-30T04:17:57.968857Z","shell.execute_reply.started":"2023-07-30T04:17:57.701016Z","shell.execute_reply":"2023-07-30T04:17:57.966830Z"},"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-30T04:17:57.970474Z","iopub.execute_input":"2023-07-30T04:17:57.970911Z","iopub.status.idle":"2023-07-30T04:18:08.374142Z","shell.execute_reply.started":"2023-07-30T04:17:57.970874Z","shell.execute_reply":"2023-07-30T04:18:08.372628Z"},"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-30T04:18:08.378378Z","iopub.execute_input":"2023-07-30T04:18:08.379332Z","iopub.status.idle":"2023-07-30T04:18:20.562728Z","shell.execute_reply.started":"2023-07-30T04:18:08.379283Z","shell.execute_reply":"2023-07-30T04:18:20.561238Z"},"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-30T04:18:20.564379Z","iopub.execute_input":"2023-07-30T04:18:20.564877Z","iopub.status.idle":"2023-07-30T04:18:26.801213Z","shell.execute_reply.started":"2023-07-30T04:18:20.564840Z","shell.execute_reply":"2023-07-30T04:18:26.799917Z"},"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-30T04:18:26.803120Z","iopub.execute_input":"2023-07-30T04:18:26.804361Z","iopub.status.idle":"2023-07-30T04:18:27.544018Z","shell.execute_reply.started":"2023-07-30T04:18:26.804311Z","shell.execute_reply":"2023-07-30T04:18:27.542529Z"},"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-30T04:18:27.545578Z","iopub.execute_input":"2023-07-30T04:18:27.546069Z","iopub.status.idle":"2023-07-30T04:18:54.972199Z","shell.execute_reply.started":"2023-07-30T04:18:27.546033Z","shell.execute_reply":"2023-07-30T04:18:54.970560Z"},"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')\n","metadata":{"execution":{"iopub.status.busy":"2023-07-30T04:18:54.974481Z","iopub.execute_input":"2023-07-30T04:18:54.975446Z","iopub.status.idle":"2023-07-30T04:18:55.813018Z","shell.execute_reply.started":"2023-07-30T04:18:54.975401Z","shell.execute_reply":"2023-07-30T04:18:55.811608Z"},"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-30T04:18:55.815112Z","iopub.execute_input":"2023-07-30T04:18:55.816460Z","iopub.status.idle":"2023-07-30T04:19:09.509464Z","shell.execute_reply.started":"2023-07-30T04:18:55.816402Z","shell.execute_reply":"2023-07-30T04:19:09.507893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(training_data)","metadata":{"execution":{"iopub.status.busy":"2023-07-30T04:19:09.511601Z","iopub.execute_input":"2023-07-30T04:19:09.512575Z","iopub.status.idle":"2023-07-30T04:19:09.520020Z","shell.execute_reply.started":"2023-07-30T04:19:09.512527Z","shell.execute_reply":"2023-07-30T04:19:09.518707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.shuffle(training_data)","metadata":{"execution":{"iopub.status.busy":"2023-07-30T04:19:09.521660Z","iopub.execute_input":"2023-07-30T04:19:09.522992Z","iopub.status.idle":"2023-07-30T04:19:09.560522Z","shell.execute_reply.started":"2023-07-30T04:19:09.522947Z","shell.execute_reply":"2023-07-30T04:19:09.559220Z"},"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-30T04:19:09.562412Z","iopub.execute_input":"2023-07-30T04:19:09.563930Z","iopub.status.idle":"2023-07-30T04:19:09.798771Z","shell.execute_reply.started":"2023-07-30T04:19:09.563882Z","shell.execute_reply":"2023-07-30T04:19:09.797592Z"},"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-30T04:19:09.805296Z","iopub.execute_input":"2023-07-30T04:19:09.805743Z","iopub.status.idle":"2023-07-30T04:19:10.529024Z","shell.execute_reply.started":"2023-07-30T04:19:09.805695Z","shell.execute_reply":"2023-07-30T04:19:10.527640Z"},"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-30T04:19:10.530548Z","iopub.execute_input":"2023-07-30T04:19:10.530973Z","iopub.status.idle":"2023-07-30T04:19:10.738555Z","shell.execute_reply.started":"2023-07-30T04:19:10.530938Z","shell.execute_reply":"2023-07-30T04:19:10.736708Z"},"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-30T04:19:10.740944Z","iopub.execute_input":"2023-07-30T04:19:10.741453Z","iopub.status.idle":"2023-07-30T04:19:13.626080Z","shell.execute_reply.started":"2023-07-30T04:19:10.741413Z","shell.execute_reply":"2023-07-30T04:19:13.624760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X)","metadata":{"execution":{"iopub.status.busy":"2023-07-30T04:19:13.627834Z","iopub.execute_input":"2023-07-30T04:19:13.628366Z","iopub.status.idle":"2023-07-30T04:19:13.635980Z","shell.execute_reply.started":"2023-07-30T04:19:13.628323Z","shell.execute_reply":"2023-07-30T04:19:13.634743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y)","metadata":{"execution":{"iopub.status.busy":"2023-07-30T04:19:13.637477Z","iopub.execute_input":"2023-07-30T04:19:13.637944Z","iopub.status.idle":"2023-07-30T04:19:13.652625Z","shell.execute_reply.started":"2023-07-30T04:19:13.637912Z","shell.execute_reply":"2023-07-30T04:19:13.651262Z"},"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-30T04:19:13.654352Z","iopub.execute_input":"2023-07-30T04:19:13.654770Z","iopub.status.idle":"2023-07-30T04:19:14.644411Z","shell.execute_reply.started":"2023-07-30T04:19:13.654708Z","shell.execute_reply":"2023-07-30T04:19:14.643290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X_train)","metadata":{"execution":{"iopub.status.busy":"2023-07-30T04:19:14.645837Z","iopub.execute_input":"2023-07-30T04:19:14.646965Z","iopub.status.idle":"2023-07-30T04:19:14.654405Z","shell.execute_reply.started":"2023-07-30T04:19:14.646929Z","shell.execute_reply":"2023-07-30T04:19:14.653313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_train)","metadata":{"execution":{"iopub.status.busy":"2023-07-30T04:19:14.655726Z","iopub.execute_input":"2023-07-30T04:19:14.656079Z","iopub.status.idle":"2023-07-30T04:19:14.669594Z","shell.execute_reply.started":"2023-07-30T04:19:14.656051Z","shell.execute_reply":"2023-07-30T04:19:14.668321Z"},"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()) # this converts our 3D feature maps to 1D featu\n\nmodel.add(Dense(64))\nmodel.add(Dense(1))\n\nmodel.add(Activation('sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2023-07-30T04:19:14.671075Z","iopub.execute_input":"2023-07-30T04:19:14.671440Z","iopub.status.idle":"2023-07-30T04:19:15.058613Z","shell.execute_reply.started":"2023-07-30T04:19:14.671412Z","shell.execute_reply":"2023-07-30T04:19:15.057195Z"},"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-30T04:19:15.060088Z","iopub.execute_input":"2023-07-30T04:19:15.060511Z"},"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_dict = history.history\n\n# Extract the loss and validation losses\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":{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test/255.0\n\ntest = np.array(test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(test) ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction.shape","metadata":{"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')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission['label'] = prediction","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission['label'] = my_submission['label'].map(lambda x: 1 if x >= 0.5 else 0 )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission['label'] = my_submission['label'].round(1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission.to_csv('my_submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}