{"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)\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","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import cv2                 # working with, mainly resizing, images\nimport numpy as np         # dealing with arrays\nimport os                  # dealing with directories\nfrom random import shuffle # mixing up off currently ordered data that might lead our network astray in training.\nfrom tqdm import tqdm      # a nice pretty percentage bar for tasks.(for representation purposes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"033caf69e9316a2497d2c1f38af4868ae6eb6d22"},"cell_type":"code","source":"TRAIN_DIR = '../input/train'\nTEST_DIR = '../input/test'\n\nIMG_SIZE = 50            # we later resize the image to 50x50 for computation\nLR=1e-3    #0.001 \n\nMODEL_NAME = 'dogsvscats-{}-{}.model'.format(LR, '2conv-basic') \n# just so we remember which saved model is which as we are going to change and tweek few things as we go through this,check for the sizes\n# when we load a model into a neural net which differs in size then we get an error","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f203705ab849fa1b118eebcdd7f99e904640fce1","collapsed":true},"cell_type":"code","source":"# converting the labels to one-hot array\ndef label_img(img):\n    # dog.89.png\n    word_label=img.split('.')[-3]   # -3 is the dog;-2 is 89 and -1 is png\n    if word_label=='cat':\n        return [1,0]\n    elif word_label=='dog':\n        return [0,1]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8420eca2cc3a73834a836cdb970b675acd149fd8"},"cell_type":"markdown","source":"tqdm provides a , way to measure where you are in a process, rather than printing things out at intervals...etc, it gives a progress bar."},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"24de30ede6958a919429512769435e8b9ec563af"},"cell_type":"code","source":"def create_train_data():\n    training_data=[]\n    for img in tqdm(os.listdir(TRAIN_DIR)):  \n        label= label_img(img)\n        path=os.path.join(TRAIN_DIR,img)\n        img=cv2.imread(path,cv2.IMREAD_GRAYSCALE)           # o is for gray scale(cv2.IMREAD_GRAYSCALE)\n        img=cv2.resize(img,(IMG_SIZE,IMG_SIZE))\n        \n        training_data.append([np.array(img),np.array(label)])\n    shuffle(training_data)\n    np.save('train_data.npy',training_data)\n    return training_data","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"36c6a0a4fd066d201fd405374b0f0d418a9680e1"},"cell_type":"markdown","source":"When we've gone through all of the images, we shuffle them, then save. Shuffle modifies a variable in place, so there's no need to re-define it here.\n\nWith this function, we will both save, and return the array data. This way, if we just change the neural network's structure, and not something with the images, like image size..etc..then we can just load the array file and save some processing time. While we're here, we might as well also make a function to process the testing data. This is the actual competition test data, NOT the data that we'll use to check the accuracy of our algorithm as we test. This data has no label."},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"5a3c3c67de85ccd742bc57f87dff330e1873a7f0"},"cell_type":"code","source":"def process_test_data():\n    testing_data=[]\n    for img in tqdm(os.listdir(TEST_DIR)):\n        path=os.path.join(TEST_DIR,img)\n\n        #83.png\n        img_num=img.split('.')[0]\n        img=cv2.imread(path,cv2.IMREAD_GRAYSCALE)\n        img=cv2.resize(img,(IMG_SIZE,IMG_SIZE))\n        testing_data.append([np.array(img),img_num])\n    shuffle(testing_data)\n    np.save('test_data.npy',testing_data)\n    return testing_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f480561f4543ee72babe435ed3e6db8025667a8"},"cell_type":"code","source":"train_data=create_train_data()\n# once we have trained we do not need to retrain it instead we can just use np.load('train_data.npy')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"6f927599c03d6aeda471a53661ad36a9572cdbfa"},"cell_type":"code","source":"import tflearn\nfrom tflearn.layers.conv import conv_2d, max_pool_2d\nfrom tflearn.layers.core import input_data, dropout, fully_connected\nfrom tflearn.layers.estimator import regression\n\nconvnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')\n\nconvnet = conv_2d(convnet, 32, 5, activation='relu')\nconvnet = max_pool_2d(convnet, 5)\n\nconvnet = conv_2d(convnet, 64, 5, activation='relu')\nconvnet = max_pool_2d(convnet, 5)\n\nconvnet = fully_connected(convnet, 1024, activation='relu')\nconvnet = dropout(convnet, 0.8)\n\nconvnet = fully_connected(convnet, 2, activation='softmax')\nconvnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')\n\nmodel = tflearn.DNN(convnet, tensorboard_dir='log')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8ae99264d3764bc3c5baa506c21d33e5a686d229"},"cell_type":"markdown","source":"We wont be training our model from scratch every time.Maybe first you just want to see how 3 epochs trains, but then, after 3, maybe you're done, or maybe you want to see about 5 epochs. We want to be saving our model after every session, and reloading it if we have a saved version, so we add this:"},{"metadata":{"trusted":true,"_uuid":"132d3fb828d03bc4268838852d9ae2f7b381938d","collapsed":true},"cell_type":"code","source":"if os.path.exists('{}.meta'.format(MODEL_NAME)):\n    model.load(MODEL_NAME)\n    print('model loaded!')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"106559e732e1588bdfe26be07afa851b8e4a8942"},"cell_type":"code","source":"train=train_data[:-500]\ntest=train_data[-500:]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f5c231f1eb20502e0715bad02e6fba818a0a1c30"},"cell_type":"markdown","source":"array[:,0] and array[:,1] did NOT work for me here. Not sure what I'm doing wrong, so I do this instead to separate my features and labels:"},{"metadata":{"trusted":true,"_uuid":"761d6a9305fbf5441a8a7a0cfc4875e1ff8fa4eb","collapsed":true},"cell_type":"code","source":"X=np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)\nY=[i[1] for i in train]\n\ntest_x=np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)\ntest_y=[i[1] for i in test]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"670626b807f5dccf8f40dd0db7c21167eeac5bf8"},"cell_type":"code","source":"model.fit({'input': X}, {'targets': Y}, n_epoch=2, validation_set=({'input': test_x}, {'targets': test_y}), \n    snapshot_step=50000, show_metric=True, run_id=MODEL_NAME)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"dc322abe752469ad7e5b856e187fe183ff45be52"},"cell_type":"markdown","source":"well 49% doesniot give us anything\n\nif you haven't made accuracy progress in the first 3 epochs, you're probably not going to at all, unless it's due to overfitment. So now we try to increase the size of the model"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"44014d93608bb0ed5a6963261bea406f0a7b42c0"},"cell_type":"code","source":"import tensorflow as tf\ntf.reset_default_graph()   #we need to reset the graph instance, since we're doing this in a continuous environment","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2f089228c84efb2857cbcdf59e14ac7e843470c5"},"cell_type":"code","source":"convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')\n\nconvnet = conv_2d(convnet, 32, 5, activation='relu')\nconvnet = max_pool_2d(convnet, 5)\n\nconvnet = conv_2d(convnet, 64, 5, activation='relu')\nconvnet = max_pool_2d(convnet, 5)\n\nconvnet = conv_2d(convnet, 32, 5, activation='relu')\nconvnet = max_pool_2d(convnet, 5)\n\nconvnet = conv_2d(convnet, 64, 5, activation='relu')\nconvnet = max_pool_2d(convnet, 5)\n\nconvnet = conv_2d(convnet, 32, 5, activation='relu')\nconvnet = max_pool_2d(convnet, 5)\n\nconvnet = conv_2d(convnet, 64, 5, activation='relu')\nconvnet = max_pool_2d(convnet, 5)\n\nconvnet = fully_connected(convnet, 1024, activation='relu')\nconvnet = dropout(convnet, 0.8)\n\nconvnet = fully_connected(convnet, 2, activation='softmax')\nconvnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')\n\nmodel = tflearn.DNN(convnet, tensorboard_dir='log')\n\n\n\nif os.path.exists('{}.meta'.format(MODEL_NAME)):\n    model.load(MODEL_NAME)\n    print('model loaded!')\n\ntrain = train_data[:-500]\ntest = train_data[-500:]\n\nX = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)\nY = [i[1] for i in train]\n\ntest_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)\ntest_y = [i[1] for i in test]\n\nmodel.fit({'input': X}, {'targets': Y}, n_epoch=4, validation_set=({'input': test_x}, {'targets': test_y}), \n    snapshot_step=500, show_metric=True, run_id=MODEL_NAME)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"f9aa3c1cd619e237d253f07ee1a581d2c0bc6815"},"cell_type":"markdown","source":"Here the accuracy has improved after increasing the size of the network but it may not always be so as incresing size after a limit tends to overfit the data.We could have use dropouts to minimise this."},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"46128f7eee6739c83d7e5fe58aaea47e762972b0"},"cell_type":"code","source":"model.save(MODEL_NAME)     # we save the model of 84.39%","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0280c5b84a3dd5ea44c8497869fb86736d5f76cd"},"cell_type":"code","source":"# if you need to create the data:\ntest_data = process_test_data()\n# if you already have some saved:\n#test_data = np.load('test_data.npy')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee56059d8dbec26f569d781c008064386f316e48"},"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nfig=plt.figure()\n\nfor num,data in enumerate(test_data[:12]):\n    # cat: [1,0]\n    # dog: [0,1]\n    \n    img_num = data[1]\n    img_data = data[0]\n    \n    y = fig.add_subplot(3,4,num+1)\n    orig = img_data\n    data = img_data.reshape(IMG_SIZE,IMG_SIZE,1)\n    #model_out = model.predict([data])[0]\n    model_out = model.predict([data])[0]\n    \n    if np.argmax(model_out) == 1: str_label='Dog'\n    else: str_label='Cat'\n        \n    y.imshow(orig,cmap='gray')\n    plt.title(str_label)\n    y.axes.get_xaxis().set_visible(False)\n    y.axes.get_yaxis().set_visible(False)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"847facaa5aed5658e1f54201f8e00da34ed77e5c"},"cell_type":"code","source":"# To make a submission to kaggle\n'''\nwith open('submission_file.csv','w') as f:\n    f.write('id,label\\n')\n            \nwith open('submission_file.csv','a') as f:\n    for data in tqdm(test_data):\n        img_num = data[1]\n        img_data = data[0]\n        orig = img_data\n        data = img_data.reshape(IMG_SIZE,IMG_SIZE,1)\n        model_out = model.predict([data])[0]\n        f.write('{},{}\\n'.format(img_num,model_out[1]))'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"58f88e6691f83c61576b82536fa83b8a3e1876f0"},"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}