{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"5b94a78d-9a7f-bcc6-e2f3-78c1f5274035"},"outputs":[],"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 or currently ordered data that might lead our network astray in training.\nfrom tqdm import tqdm      # a nice pretty percentage bar for tasks. Thanks to viewer Daniel Bühler for this suggestion"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"d3bc131c-aa2d-6225-2ac5-6eba3e5a4dce"},"outputs":[],"source":"TRAIN_DIR = '../input/train'\nTEST_DIR = '../input/test'\nIMG_SIZE = 50\nLR = 1e-3"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"aded728f-a996-d952-abaf-c9d49ce855a0"},"outputs":[],"source":"MODEL_NAME = 'dogsvscats-{}-{}.model'.format(LR, '2conv-basic') # just so we remember which saved model is which, sizes must match"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"8907ec7f-91cb-1400-a048-451260b24eb9"},"outputs":[],"source":"def label_img(img):\n    word_label = img.split('.')[-3]\n    # conversion to one-hot array [cat,dog]\n    #                            [much cat, no dog]\n    if word_label == 'cat': return [1,0]\n    #                             [no cat, very doggo]\n    elif word_label == 'dog': return [0,1]\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"48519d86-dc5b-c7d2-4b66-885c10cf7323"},"outputs":[],"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)\n        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\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"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"8dd45997-2f97-b2e2-53ba-dd1a76ed946f"},"outputs":[],"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        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        \n    shuffle(testing_data)\n    np.save('test_data.npy', testing_data)\n    return testing_data"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"8ec9f8fa-151b-fc28-6a97-74e0e360b27c"},"outputs":[],"source":"train_data = create_train_data()\n# If you have already created the dataset:\n#train_data = np.load('train_data.npy')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"d667c97c-ea2d-2437-510e-5d5c83ec4aba"},"outputs":[],"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')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"1e8bab16-b09d-0995-ec53-f296dbdffa8a"},"outputs":[],"source":"if os.path.exists('{}.meta'.format(MODEL_NAME)):\n    model.load(MODEL_NAME)\n    print('model loaded!')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"bd7ca640-fe96-ccc8-0e1f-b1d9306eb55b"},"outputs":[],"source":"train = train_data[:-500]\ntest = train_data[-500:]"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"3e9d57cc-97d1-69a7-b594-df31ebff725f"},"outputs":[],"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]"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"e62f1138-eed3-fb29-4df4-930456558b47"},"outputs":[],"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)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"639cbca5-2ef6-419a-7d16-8765ffae2497"},"outputs":[],"source":"import tensorflow as tf\ntf.reset_default_graph()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"6f4c5051-d93e-4fe4-fd3b-eabb114ed8e8"},"outputs":[],"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)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"d55e0d98-7c40-cc78-311b-b2588c2a6378"},"outputs":[],"source":"model.save(MODEL_NAME)\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"9019a43d-b150-9277-e4dc-8410d6d046e8"},"outputs":[],"source":"import matplotlib.pyplot as plt\n\n# 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')\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()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"74ef2ad2-1db6-5cfc-bb61-57fc09ee04c7"},"outputs":[],"source":"with 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]))"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"2bba416c-b8c1-feec-7b11-1cab0e920bdf"},"outputs":[],"source":""}],"metadata":{"_change_revision":0,"_is_fork":false,"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.0"}},"nbformat":4,"nbformat_minor":0}