{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":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 re \nimport cv2 \nimport glob\nimport matplotlib.pyplot as plt \nimport 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\nfrom tqdm import tqdm \nfrom random import shuffle\nimport os\ntrain = os.listdir(\"../input/train\")\ntest  = os.listdir(\"../input/test\")\ntrain_dir = \"../input/train\"\ntest_dir = \"../input/test\"\nHeight = 50 \nWidth = 50 \ncwd = os.getcwd() \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":"def get_label(img):\n    label = img.split('.')[-3] \n    if label == 'cat':\n        return [1,0]\n    elif label == 'dog':\n        return [0,1] ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"ca8c83d84fe8f696f6ac8648467e5b007ac40451"},"cell_type":"code","source":"def process_train_data():\n    train_data = [] \n    path = os.path.join(cwd, train_dir,'*g')\n    imgs = glob.glob(path)\n    for img in tqdm(imgs):\n        labels = get_label(img.split('/')[6])  \n        img = cv2.imread(img,0) \n        img = cv2.resize(img,(Height,Width)) \n        train_data.append([np.array(img), np.array(labels)])\n    \n    shuffle(train_data)\n    np.save('train_data.npy', train_data)\n    return train_data    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"1a13f050e441ce7e43674d592a7ea3092ba2e34a"},"cell_type":"code","source":"def process_test_data():\n    test_data = [] \n    path = os.path.join(cwd, test_dir,'*g')\n    imgs = glob.glob(path)\n    for img in tqdm(imgs):\n        img_idx = img.split('/')[6].split('.')[0]\n        img = cv2.imread(img,0)\n        img = cv2.resize(img,(Height,Width))\n        test_data.append([np.array(img), img_idx])\n    shuffle(test_data)\n    np.save('test_data.npy', test_data)\n    return test_data ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6a76f434299060d50fc0018181f64420b10747e8","collapsed":true},"cell_type":"code","source":"train_data = process_train_data()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d7f6cbfddac56408f53ebcd7c88d34f65f609c3d","collapsed":true},"cell_type":"code","source":"#Model \nconvnet = input_data(shape=[None, Height, Width, 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, 128, 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)\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=1e-3, loss='categorical_crossentropy', name='targets')\n\nmodel = tflearn.DNN(convnet)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"1d46b59da529e368d8ea184f746b5fac7cfa5e70"},"cell_type":"code","source":"train = train_data[:-500]\nvalidation = train_data[-500:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2acca5edd850b610fc504301a2557ee9208eb017","collapsed":true},"cell_type":"code","source":"X = np.array([i[0] for i in train]).reshape(-1,Height,Width,1)\nY = [i[1] for i in train] \nX_val = np.array([i[0] for i in validation]).reshape(-1,Height,Width,1)\nY_val = [i[1] for i in validation]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dd33805f2828b0babbc46397b5344e704c8d636b","collapsed":true},"cell_type":"code","source":"#training model \nmodel.fit({'input': X}, {'targets': Y}, n_epoch=15, validation_set=({'input': X_val}, {'targets': Y_val}), \n    snapshot_step=500, show_metric=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"081acb733ed00600f43c5339ac48365b36584af5","collapsed":true},"cell_type":"code","source":"test_data = process_test_data() \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e0e869991017b1dcd5d01ddcd4d97b991265b4aa","collapsed":true},"cell_type":"code","source":"#competeing!! \ntest_data = process_test_data() \ntest_data = np.load('test_data.npy')\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(Height,Width,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":"d524385e8060fbf1b007206251cd4431c62ab434"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"08feb35847102576df04db124534425b384240e2"},"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.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}