{"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 tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout\nimport os\nimport cv2\nfrom zipfile import ZipFile\nfrom skimage.io import imread\nfrom PIL import Image\nfrom keras.preprocessing.image import ImageDataGenerator\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":"with ZipFile('/kaggle/input/dogs-vs-cats/train.zip', 'r') as z:\n    z.extractall()\n\ntrain = []\n\nfor filename in os.listdir('train'):\n    img_array = cv2.imread('train/' + filename, cv2.IMREAD_GRAYSCALE)\n    img_array = cv2.resize(img_array, (64,64))\n    if filename[:3] == 'cat':\n        label = 0\n    else:\n        label = 1\n    train.append((img_array, label))\n\nnp.random.shuffle(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = []\ny = []\n\nfor features,label in train:\n    X.append(features)\n    y.append(label)\n\nX = (np.array(X)/255.0).reshape(-1,64,64,1)\ny = np.array(y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = keras.models.Sequential([\n    \n    Conv2D(64, (3,3), input_shape=X.shape[1:], padding='same', activation='relu'),\n    MaxPooling2D((2,2), padding='same'),\n    Dropout(0.25),\n    Conv2D(64, (3,3), padding='same', activation='relu'),\n    MaxPooling2D((2,2), padding='same'),\n    \n    Conv2D(64, (3,3), padding='same', activation='relu'),\n    MaxPooling2D((2,2)),\n    Dropout(0.25),\n    Conv2D(64, (3,3), padding='same', activation='relu'),\n    MaxPooling2D((2,2)),\n\n    Flatten(),\n    Dense(256, activation='relu'),\n    Dropout(0.25),\n    Dense(1, activation='sigmoid')\n    \n])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile('adam', 'binary_crossentropy', ['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(X, y, validation_split=0.15, epochs=12)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_val = X[:22500], X[22500:]\ny_train, y_val = y[:22500], y[22500:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#gen = ImageDataGenerator(rotation_range=20, width_shift_range=0.2, \n#                         height_shift_range=0.2, shear_range=0.2, \n#                         zoom_range=0.1, channel_shift_range=10, horizontal_flip=True)\n\ngen = ImageDataGenerator(rotation_range=20, horizontal_flip=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit_generator(gen.flow(X_train, y_train, batch_size=32),\n                   validation_data=(X_val, y_val),\n                    steps_per_epoch=len(X_train) // 32,\n                    epochs=20,\n                    shuffle=True\n                   )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p = model.predict(X_val)\np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug_iter = gen.flow(X)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"count = 0\nfor i in aug_iter[0]:\n    plt.imshow(i.reshape(64,64))\n    plt.show()\n    count += 1\n    if count == 10:\n        break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}