{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.utils import to_categorical\n\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, BatchNormalization, Dropout\nfrom keras.layers import Dense, Flatten\n\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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 in \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 \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nprint(os.listdir('../input'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!unzip /kaggle/input/train.zip -d /kaggle/working/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!unzip /kaggle/input/test.zip -d /kaggle/working/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"category = [\"cat\", \"dog\"]\n\nEPOCHS                  = 50\nIMGSIZE                 = 128\nBATCH_SIZE              = 32\nSTOPPING_PATIENCE       = 15\nVERBOSE                 = 1\nMODEL_NAME              = 'cnn_50epochs_imgsize128'\nOPTIMIZER               = 'adam'\nTRAINING_DIR            = '/kaggle/working/train'\nTEST_DIR                = '/kaggle/working/test'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for img in os.listdir(TRAINING_DIR)[7890:]:\n    img_path = os.path.join(TRAINING_DIR, img)\n    img_arr = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)\n    img_arr = cv2.resize(img_arr, (IMGSIZE, IMGSIZE))\n    plt.imshow(img_arr, cmap='gray')\n    plt.title(img.split('.')[0])\n    break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_train_data(path):\n    X = []\n    y = []\n    for img in os.listdir(path):\n        if img == os.listdir(path)[7889]:\n            continue\n        img_path = os.path.join(path, img)\n        img_arr = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)\n        img_arr = cv2.resize(img_arr, (IMGSIZE, IMGSIZE))\n        img_arr = img_arr / 255.0\n        cat = np.where(img.split('.')[0] == 'dog', 1, 0)\n        \n        X.append(img_arr)\n        y.append(cat)\n    \n    X = np.array(X).reshape(-1, IMGSIZE, IMGSIZE, 1)\n    y = np.array(y)\n    \n    return X, y        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X, y = create_train_data(TRAINING_DIR)\n\nprint(f\"features shape {X.shape}.\\nlabel shape {y.shape}.\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y = to_categorical(y, 2)\nprint(f\"features shape {X.shape}.\\nlabel shape {y.shape}.\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train , X_test, y_train, y_test = train_test_split(X, y, test_size=1/3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=(28,28,1)))\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test))","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}