{"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":"2022-05-04T02:12:19.690732Z","iopub.execute_input":"2022-05-04T02:12:19.691315Z","iopub.status.idle":"2022-05-04T02:12:19.699249Z","shell.execute_reply.started":"2022-05-04T02:12:19.691276Z","shell.execute_reply":"2022-05-04T02:12:19.698434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from zipfile import ZipFile\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport cv2\nimport random\nimport pickle\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:12:19.700985Z","iopub.execute_input":"2022-05-04T02:12:19.701837Z","iopub.status.idle":"2022-05-04T02:12:20.928114Z","shell.execute_reply.started":"2022-05-04T02:12:19.701780Z","shell.execute_reply":"2022-05-04T02:12:20.927352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\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\nfrom tensorflow.keras.optimizers import SGD","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:12:20.929942Z","iopub.execute_input":"2022-05-04T02:12:20.930200Z","iopub.status.idle":"2022-05-04T02:12:25.833021Z","shell.execute_reply.started":"2022-05-04T02:12:20.930166Z","shell.execute_reply":"2022-05-04T02:12:25.832222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Training data unzip after unzip training image folder is **/kaggle/working/train**","metadata":{}},{"cell_type":"code","source":"with ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip', 'r') as zip:\n    zip.extractall()\n    print('done')","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:12:25.834470Z","iopub.execute_input":"2022-05-04T02:12:25.835239Z","iopub.status.idle":"2022-05-04T02:12:37.670530Z","shell.execute_reply.started":"2022-05-04T02:12:25.835199Z","shell.execute_reply":"2022-05-04T02:12:37.669734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Testing data unzip after testing image folder is **/kaggle/working/testing**","metadata":{}},{"cell_type":"code","source":"with ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip', 'r') as zip:\n    zip.extractall()\n    print('done')","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:12:37.672742Z","iopub.execute_input":"2022-05-04T02:12:37.673488Z","iopub.status.idle":"2022-05-04T02:12:43.207413Z","shell.execute_reply.started":"2022-05-04T02:12:37.673446Z","shell.execute_reply":"2022-05-04T02:12:43.205906Z"},"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-05-04T02:12:43.209181Z","iopub.execute_input":"2022-05-04T02:12:43.209394Z","iopub.status.idle":"2022-05-04T02:12:43.682271Z","shell.execute_reply.started":"2022-05-04T02:12:43.209368Z","shell.execute_reply":"2022-05-04T02:12:43.680287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_data = []\nx_train=  []\ny_train = []\n\nfor files in tqdm(os.listdir(PATH)):\n    try:\n        \n        if files.find('cat') == -1:\n            category = 0\n        else:\n            category = 1   \n        img_array = cv2.imread(os.path.join(PATH, files), cv2.IMREAD_GRAYSCALE)\n        new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))\n        training_data.append([new_array, category])\n        x_train.append([new_array])\n        y_train.append([category])\n    except Exception as e:\n        pass\n\n","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:12:43.683233Z","iopub.execute_input":"2022-05-04T02:12:43.683445Z","iopub.status.idle":"2022-05-04T02:13:07.702577Z","shell.execute_reply.started":"2022-05-04T02:12:43.683417Z","shell.execute_reply":"2022-05-04T02:13:07.701744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.array(x_train)\ny_train = np.array(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:13:07.704070Z","iopub.execute_input":"2022-05-04T02:13:07.704533Z","iopub.status.idle":"2022-05-04T02:13:07.830897Z","shell.execute_reply.started":"2022-05-04T02:13:07.704495Z","shell.execute_reply":"2022-05-04T02:13:07.830139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(training_data)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:13:07.832035Z","iopub.execute_input":"2022-05-04T02:13:07.834251Z","iopub.status.idle":"2022-05-04T02:13:07.839603Z","shell.execute_reply.started":"2022-05-04T02:13:07.834218Z","shell.execute_reply":"2022-05-04T02:13:07.838912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.reshape(25000, 100, 100, 1)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:13:07.840920Z","iopub.execute_input":"2022-05-04T02:13:07.841343Z","iopub.status.idle":"2022-05-04T02:13:07.849427Z","shell.execute_reply.started":"2022-05-04T02:13:07.841299Z","shell.execute_reply":"2022-05-04T02:13:07.848532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:13:07.853395Z","iopub.execute_input":"2022-05-04T02:13:07.853816Z","iopub.status.idle":"2022-05-04T02:13:07.861756Z","shell.execute_reply.started":"2022-05-04T02:13:07.853770Z","shell.execute_reply":"2022-05-04T02:13:07.860896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testing_data = []\nPATH = '/kaggle/working/test'\n\nfor files in tqdm(os.listdir(PATH)):\n    try:\n        img_array = cv2.imread(os.path.join(PATH, files), 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-05-04T02:13:07.863266Z","iopub.execute_input":"2022-05-04T02:13:07.863688Z","iopub.status.idle":"2022-05-04T02:13:19.067329Z","shell.execute_reply.started":"2022-05-04T02:13:07.863648Z","shell.execute_reply":"2022-05-04T02:13:19.066520Z"},"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-05-04T02:13:19.069819Z","iopub.execute_input":"2022-05-04T02:13:19.070251Z","iopub.status.idle":"2022-05-04T02:13:19.458351Z","shell.execute_reply.started":"2022-05-04T02:13:19.070212Z","shell.execute_reply":"2022-05-04T02:13:19.453673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.seed(45)\nrandom.shuffle(training_data)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:13:19.459751Z","iopub.execute_input":"2022-05-04T02:13:19.460181Z","iopub.status.idle":"2022-05-04T02:13:19.489133Z","shell.execute_reply.started":"2022-05-04T02:13:19.460140Z","shell.execute_reply":"2022-05-04T02:13:19.488220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('length of training data', str(len(training_data)))\nprint('Shape of image', training_data[0][0].shape)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:13:19.490970Z","iopub.execute_input":"2022-05-04T02:13:19.491281Z","iopub.status.idle":"2022-05-04T02:13:19.502552Z","shell.execute_reply.started":"2022-05-04T02:13:19.491237Z","shell.execute_reply":"2022-05-04T02:13:19.501615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Total training data image is 25000 and one image size is 100x100. But we don't want use complete image. ","metadata":{}},{"cell_type":"code","source":"X = []\ny = []\n\nfor features, labels in training_data:\n    X.append(features.reshape(100,100, 1))\n    y.append(labels)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:13:19.503674Z","iopub.execute_input":"2022-05-04T02:13:19.504971Z","iopub.status.idle":"2022-05-04T02:13:19.545898Z","shell.execute_reply.started":"2022-05-04T02:13:19.504929Z","shell.execute_reply":"2022-05-04T02:13:19.544989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(x_train,y_train, test_size = 0.3, random_state=51)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:13:19.547492Z","iopub.execute_input":"2022-05-04T02:13:19.548074Z","iopub.status.idle":"2022-05-04T02:13:19.632215Z","shell.execute_reply.started":"2022-05-04T02:13:19.548023Z","shell.execute_reply":"2022-05-04T02:13:19.631457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:13:19.633677Z","iopub.execute_input":"2022-05-04T02:13:19.633950Z","iopub.status.idle":"2022-05-04T02:13:19.639487Z","shell.execute_reply.started":"2022-05-04T02:13:19.633916Z","shell.execute_reply":"2022-05-04T02:13:19.638759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:13:19.640901Z","iopub.execute_input":"2022-05-04T02:13:19.641365Z","iopub.status.idle":"2022-05-04T02:13:19.655016Z","shell.execute_reply.started":"2022-05-04T02:13:19.641317Z","shell.execute_reply":"2022-05-04T02:13:19.654286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train/255.0\nX_test = X_test/255.0","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:13:19.656226Z","iopub.execute_input":"2022-05-04T02:13:19.657094Z","iopub.status.idle":"2022-05-04T02:13:20.281476Z","shell.execute_reply.started":"2022-05-04T02:13:19.657050Z","shell.execute_reply":"2022-05-04T02:13:20.280736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n\ny_train = to_categorical(y_train)\ny_test = to_categorical(y_test)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:13:20.282864Z","iopub.execute_input":"2022-05-04T02:13:20.283129Z","iopub.status.idle":"2022-05-04T02:13:20.289697Z","shell.execute_reply.started":"2022-05-04T02:13:20.283093Z","shell.execute_reply":"2022-05-04T02:13:20.288707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[0]","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:13:20.291314Z","iopub.execute_input":"2022-05-04T02:13:20.291865Z","iopub.status.idle":"2022-05-04T02:13:20.301722Z","shell.execute_reply.started":"2022-05-04T02:13:20.291809Z","shell.execute_reply":"2022-05-04T02:13:20.300810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create model\nmodel = Sequential()\n\nmodel.add(Conv2D(32, kernel_size = (5,5), activation='relu', input_shape = (100,100, 1)))\n\nmodel.add(Conv2D(64, kernel_size = (5,5), activation='relu'))\n\nmodel.add(MaxPooling2D(pool_size=(4,4)))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(512, activation='relu'))\n\nmodel.add(Dense(2, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:16:17.578271Z","iopub.execute_input":"2022-05-04T02:16:17.578544Z","iopub.status.idle":"2022-05-04T02:16:17.625276Z","shell.execute_reply.started":"2022-05-04T02:16:17.578512Z","shell.execute_reply":"2022-05-04T02:16:17.624528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer=SGD(0.001), metrics =['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:16:22.535633Z","iopub.execute_input":"2022-05-04T02:16:22.535969Z","iopub.status.idle":"2022-05-04T02:16:22.547509Z","shell.execute_reply.started":"2022-05-04T02:16:22.535934Z","shell.execute_reply":"2022-05-04T02:16:22.546684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:16:24.744256Z","iopub.execute_input":"2022-05-04T02:16:24.744529Z","iopub.status.idle":"2022-05-04T02:16:24.754942Z","shell.execute_reply.started":"2022-05-04T02:16:24.744497Z","shell.execute_reply":"2022-05-04T02:16:24.754275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:13:23.004373Z","iopub.execute_input":"2022-05-04T02:13:23.004797Z","iopub.status.idle":"2022-05-04T02:13:23.011755Z","shell.execute_reply.started":"2022-05-04T02:13:23.004758Z","shell.execute_reply":"2022-05-04T02:13:23.010893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nepochs = 10\n\nhistory = model.fit(X_train, y_train, batch_size=batch_size, epochs = epochs, verbose=1,\n                   validation_data = (X_test, y_test))\n\nscore = model.evaluate(X_test, y_test, verbose=0)\nprint('Test loss:', score[0])\nprint('Test accuracy:', score[0])","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:23:11.831116Z","iopub.execute_input":"2022-05-04T02:23:11.831387Z","iopub.status.idle":"2022-05-04T02:24:42.388330Z","shell.execute_reply.started":"2022-05-04T02:23:11.831356Z","shell.execute_reply":"2022-05-04T02:24:42.387509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting our loss charts\n# Use the history object we created to get our saved perormace result.\nhistory_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.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:35:14.514503Z","iopub.execute_input":"2022-05-04T02:35:14.515024Z","iopub.status.idle":"2022-05-04T02:35:14.746983Z","shell.execute_reply.started":"2022-05-04T02:35:14.514951Z","shell.execute_reply":"2022-05-04T02:35:14.746154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Accuracy Charts","metadata":{}},{"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":"2022-05-04T02:44:06.296204Z","iopub.execute_input":"2022-05-04T02:44:06.296473Z","iopub.status.idle":"2022-05-04T02:44:06.499445Z","shell.execute_reply.started":"2022-05-04T02:44:06.296442Z","shell.execute_reply":"2022-05-04T02:44:06.498673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}