{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","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)\nimport matplotlib.pyplot as plt\n%matplotlib inline \nimport cv2\nimport os \nfrom tqdm import tqdm\n\nimport tensorflow as tf\nfrom keras.models import Sequential\nfrom keras.layers import Convolution2D as Conv2D\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom keras.optimizers import Adam\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport itertools\nfrom random import shuffle\n\n\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 the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_data_dir = \"../input/train\"\ntest_data_dir = \"../input/test\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"425ab3cccb09f1bf0469c45eddf7b4f26c4594c9"},"cell_type":"code","source":"def label(img):\n    word_label = img.split('.')[-3]\n    if word_label == \"cat\":\n        return [1,0]\n    elif word_label ==\"dog\":\n        return [0,1]\n    \n\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"458c5d3af515a1d30abf35457aa0eda32af6bf48"},"cell_type":"code","source":"def create_training_data():\n    training_data = []\n    for img in tqdm(os.listdir(train_data_dir)):\n        lable = label(img)\n        path = os.path.join(train_data_dir, img)\n        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n        img = cv2.resize(img, (64,64))\n        training_data.append([np.array(img), np.array(lable)])\n    shuffle(training_data)\n    return training_data\n\ndef create_test_data():\n    test_data = []\n    for img in tqdm(os.listdir(test_data_dir)):\n        path = os.path.join(test_data_dir, img)\n        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n        img = cv2.resize(img, (64,64))\n        test_data.append([np.array(img)])\n    return test_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"617e69552a4817d39a2c71cab45f4ac4b7492e47"},"cell_type":"code","source":"training_data = create_training_data()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f3ecf224b6f13d9e3805475dbd21cf7b041623e0"},"cell_type":"code","source":"testing_data = create_test_data()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0891d48c9e9073fe5466d9e5289c512b57e0163c"},"cell_type":"code","source":"training_data = training_data[:4000]\nX_train = np.array([i[0] for i in training_data])\ny_train = np.array([i[1] for i in training_data])\n\nX_train= X_train/255.0\nX_train = X_train.reshape(-1, 64,64,1)\n\nX_test = np.array([i[0] for i in testing_data])\n\nX_test = X_test/255.0\nX_test = X_test.reshape(-1, 64,64,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2a74bd69bd32a3d06745038c30224ff626457a39"},"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state = 2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"34c127d248d4992a30bd7a8e37233b8d193e48bc"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, (5,5), padding = \"same\", activation = \"relu\", input_shape = (64,64,1)))\nmodel.add(MaxPool2D(pool_size = (2,2), strides = (1,1)))\nmodel.add(Dropout(0.2))\nmodel.add(Conv2D(64, (3,3), padding = \"same\", activation = \"relu\"))\nmodel.add(MaxPool2D(pool_size = (2,2), strides = (1,1)))\nmodel.add(Dropout(0.3))\n\nmodel.add(Flatten())\nmodel.add(Dense(128, activation = \"relu\"))\nmodel.add(Dense(50, activation = \"relu\"))\nmodel.add(Dense(2, activation = \"softmax\"))\n\noptimizer = Adam(lr = 0.0001)\nmodel.compile(optimizer = optimizer, loss = \"categorical_crossentropy\", metrics = [\"accuracy\"])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7e3ae70a065ab8a43bd0ab1adcc1b898af079219"},"cell_type":"code","source":"epochs = 50\nbatch_size = 200\nmodel.fit(X_train, y_train, batch_size = batch_size,epochs = epochs, validation_data = (X_val, y_val), verbose=2)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"af30e2a872e228ee608a4253f98e475abe970bb5"},"cell_type":"code","source":"model.predict(X_test, verbose =2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"73f7666763db366263b4fbcc682e819adeafd38a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}