{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","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\nfrom IPython.core.interactiveshell import InteractiveShell\nInteractiveShell.ast_node_interactivity = \"all\"\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":"import cv2                 \nimport os                  \nfrom tqdm import tqdm\nfrom random import shuffle\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!unzip ../input/dogs-vs-cats-redux-kernels-edition/train.zip -d train\n!unzip ../input/dogs-vs-cats-redux-kernels-edition/test.zip -d test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN_DIR = '../working/train/train/'\nTEST_DIR = '../working/test/test/'","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0af8dce6c6251892316c8b618bf1906f901a84ab","trusted":true},"cell_type":"code","source":"IMG_SIZE = 64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images = [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR)] # use this for full dataset\ntrain_dogs =   [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'dog' in i]\ntrain_cats =   [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'cat' in i]\n\ntest_images =  [TEST_DIR+i for i in os.listdir(TEST_DIR)]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bf417a41ec868563e785ce8eb94c1378a007ddfc","trusted":true},"cell_type":"code","source":"len(train_cats), len(train_dogs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"type(train_cats)\ntrain_cats[1]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a7b4ddd56a5c6afdf72c067cbf481a3908012d3e","trusted":true},"cell_type":"code","source":"#Creating training and validation splits\ntrain_list = train_cats[:10000] + train_dogs[:10000]\nval_list = train_cats[10000:] + train_dogs[10000:]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7b05ea5b748a4376c14802c3deab38bb52d22e7f","trusted":true},"cell_type":"code","source":"len(train_list), len(val_list)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4cdbe21640f736f1bf0d6b1e6388e20e1aceeacb","trusted":true},"cell_type":"code","source":"#Function for defining label\ndef label_img(img):\n    if 'cat' in img: return [0, 1]\n    elif 'dog' in img: return [1, 0]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9bd0649abce0323da619ccdd3829f2fad0fa7f95","trusted":true},"cell_type":"code","source":"#Function for resizing images\ndef create_train_data(train_list):\n    training_data = []\n    for img in tqdm(train_list):\n        label = label_img(img)\n        path = img\n        img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)\n        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n        training_data.append([np.array(img),label])\n    shuffle(training_data)\n    #np.save('train_data.npy', training_data)\n    return training_data","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ef15d28e7e33e1fbfbbe3939b363518066c29a7d","trusted":true},"cell_type":"code","source":"train_list[0]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"15135c267b5e0ba218393dd9ff882757149b45a2","scrolled":true,"trusted":true},"cell_type":"code","source":"#Creating array with data\ntrain = create_train_data(train_list)\nval = create_train_data(val_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def fullprint(*args, **kwargs):\n  from pprint import pprint\n  import numpy\n  opt = numpy.get_printoptions()\n  numpy.set_printoptions(threshold=numpy.inf)\n  pprint(*args, **kwargs)\n  numpy.set_printoptions(**opt)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e3c126207970a5d1bb63a80dcae80b6c42e52bbf","scrolled":true,"trusted":true},"cell_type":"code","source":"fullprint(train[1])\nlen(train)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"00639b11746bd9d840ed58c6d3d01d9e0a40212d","trusted":true},"cell_type":"code","source":"train[1000][1]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0978c870f75cd8caa2200f4daa12cc75e71a8719","trusted":true},"cell_type":"code","source":"#Creating training ad Validation arrays\nX = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE)\nY = [i[1] for i in train]\n\nval_X = np.array([i[0] for i in val]).reshape(-1,IMG_SIZE,IMG_SIZE)\nval_Y = [i[1] for i in val]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e3f8d348776024fb48a4afda1af5d04fb93dba58","trusted":true},"cell_type":"code","source":"#Scaling data for neural network\nX = X/float(255)\nval_X = val_X/float(255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X.shape\nfullprint(X[0])\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f6cd0a98ad0dd45d50e4e16480f36c6edd858e34","trusted":true},"cell_type":"code","source":"Y = np.asarray(Y)\n\nval_Y = np.asarray(val_Y)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4d740dbe6b4814cf11270273eb74cbfc40b8c6a0","trusted":true},"cell_type":"code","source":"X.shape, val_X.shape\nY.shape, val_Y.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"11c90e93ba999798859338f77e71b30da50578d7","trusted":true},"cell_type":"code","source":"#Importing keras libraries\nimport keras\nfrom keras.layers import Input, Dense, Flatten, Dropout\nfrom keras.optimizers import SGD, Adam\nfrom keras.models import Model","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b0d6efa5e25bb9fba8633c0655b45c01893b857a","trusted":true},"cell_type":"code","source":"#Network Architecture\nx_input = Input(shape=(IMG_SIZE, IMG_SIZE))\nx = Flatten()(x_input)\n#x = Dense(4096, activation=\"relu\")(x)\n#x = Dropout(0.2)(x)\n#x = Dense(8096, activation=\"relu\")(x)\nx = Dense(1024, activation=\"relu\")(x)\nx = Dense(256, activation=\"relu\")(x)\nx = Dense(64, activation=\"relu\")(x)\nx = Dense(16, activation=\"relu\")(x)\nx_out = Dense(2, activation=\"softmax\")(x)\n\n#Specifying input and output\nmodel = Model(inputs=x_input, outputs=x_out)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"aa710764162ef1e854165e567a94bd22f1fa5046","trusted":true},"cell_type":"code","source":"sgd = SGD(lr=0.001, momentum=0.0, decay=0.0, nesterov=False)\n#adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)\n\nmodel.compile(optimizer=sgd, loss = \"binary_crossentropy\", metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Input size * number of nodes + number of nodes for bias\n1024*64*64","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b6c77eca42acd74d698939f4442dda8d31f0b8fd","scrolled":true,"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"46f805c7e7332c998dfd7ada92972a162acf4d12","scrolled":true,"trusted":true},"cell_type":"code","source":"history = model.fit(x=X, \n                    y=Y, \n                    validation_data=(val_X, val_Y), \n                    epochs = 80, \n                    batch_size=128, \n                    verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(history.history.keys())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ca531b281c6d5b63bd62c98148db18dfbb1c918f","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()\n# summarize history for loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()","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":4}