{"cells":[{"metadata":{"_cell_guid":"83a56525-8a8c-cf2a-1579-edf14ade6228"},"cell_type":"markdown","source":"HandsOn Deep Learning using TFLearn"},{"metadata":{"_cell_guid":"0604cdb8-8aa6-405e-b2aa-6101d92b6d65"},"cell_type":"markdown","source":""},{"metadata":{"_cell_guid":"7ced165e-3a9e-a004-6813-1b31953b3b7b","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\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"d31ded52-6516-ae9a-9598-944aad9ea2b2","trusted":true},"cell_type":"code","source":"\nimport cv2\nimport numpy as np\nimport os\nfrom random import shuffle\n\nTRAIN_DIR = \"../input/train/train\"\nTEST_DIR = \"../input/test/test\"\nIMG_SIZE = 50\nLR = 1e-3\nMODEL_NAME = 'dogsvscats1-{}-{}.model'.format(LR,'2conv-basic-video')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"9cd32ba7-7067-4d25-9777-a1d5e55f9bf9","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\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"43d07169-5005-489c-ac53-4566f48e18bf","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"374734eb-3506-d73b-d7d7-0c14baa6d10d","trusted":true},"cell_type":"code","source":"def label_img(img):\n    word_label = img.split('.')[-3]\n    print(word_label)\n    if word_label == 'cat': return [1,0]\n    elif word_label == 'dog': return [0,1]","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"247222aa-b6f9-a7b7-e1ee-985f5fed0a76","trusted":true},"cell_type":"code","source":"def create_train_data():\n    training_data = []\n    for img in os.listdir(TRAIN_DIR):\n        path = os.path.join(TRAIN_DIR,img)\n        print(path)\n        label = label_img(img)\n        print(label)\n        #path = os.path.join(TRAIN_DIR,img)\n        #print(path)\n        img = cv2.resize(cv2.imread(path,cv2.IMREAD_GRAYSCALE),(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\n   \n    ","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"ab147c06-d86c-6dbf-5edf-79871dceea89","trusted":true},"cell_type":"code","source":"print(TRAIN_DIR)\nprint(TEST_DIR)\n#os.listdir(TRAIN_DIR)\n#os.listdir(TEST_DIR)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"0c1c9355-8592-4375-a9ff-24f741125d05","trusted":true},"cell_type":"code","source":"def process_test_data():\n    testing_data = []\n    for img in os.listdir(TEST_DIR):\n        path = os.path.join(TEST_DIR,img)\n        img_num = img.split('.')[0]\n        img = cv2.resize(cv2.imread(path,cv2.IMREAD_GRAYSCALE),(IMG_SIZE,IMG_SIZE))\n        testing_data.append([np.array(img),img_num])\n    np.save('test_data.npy', testing_data)  \n    return testing_data\n\n    ","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"95fc6c4b-3318-91a9-dd4d-5698ae8241ec","trusted":true},"cell_type":"code","source":"train_data = create_train_data()\n# if you already have train data you could say \n#train_data = np.load('train_data.npy')\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"1a95b0d1-80d9-13b2-4530-88bc8759e21f","trusted":true},"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\nimport tensorflow as tf\ntf.reset_default_graph()\n\nconvnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')\n\nconvnet = conv_2d(convnet, 32, 2, activation='relu')\nconvnet = max_pool_2d(convnet, 2)\n\nconvnet = conv_2d(convnet, 64, 2, activation='relu')\nconvnet = max_pool_2d(convnet, 2)\n\nconvnet = conv_2d(convnet, 32, 2, activation='relu')\nconvnet = max_pool_2d(convnet, 2)\n\nconvnet = conv_2d(convnet, 64, 2, activation='relu')\nconvnet = max_pool_2d(convnet, 2)\n\nconvnet = conv_2d(convnet, 32, 2, activation='relu')\nconvnet = max_pool_2d(convnet, 2)\n\nconvnet = conv_2d(convnet, 64, 2, activation='relu')\nconvnet = max_pool_2d(convnet, 2)\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","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"84131b89-9f04-84ad-b620-28a514fc16f0","trusted":true},"cell_type":"code","source":"os.path.exists('{}.meta'.format(MODEL_NAME))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"f950b178-bd24-bb1c-0b9d-4ce59438556d","trusted":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":{"_cell_guid":"20a524dc-a1b5-f1cc-d432-b5858c69dad5","trusted":true},"cell_type":"code","source":"train = train_data[:-500]\ntest = train_data[-500:]","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"915ec6c5-35b3-bc85-787d-ab147a1aece6","trusted":true},"cell_type":"code","source":"X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)\nY = np.array([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 = np.array([i[1] for i in test])","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"032339be-61c2-0df9-ae16-ecb3611905b4","trusted":true},"cell_type":"code","source":"model.fit({'input': X}, {'targets': Y}, n_epoch=5, validation_set=({'input': test_x}, {'targets': test_y}), \n    snapshot_step=500, show_metric=True, run_id=MODEL_NAME)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"e82a54eb-da0a-f0cb-0419-ae2f5b41bb99","trusted":false},"cell_type":"code","source":"#tensorboard --logdir=foo:D:\\DogsVsCats\\log","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"f0fee67e-9740-1e88-51a9-bbc6b118afbc","trusted":true},"cell_type":"code","source":"model.save(MODEL_NAME)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"552cfecc-f0ee-0b55-f51c-cf2d5a8efe67","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ntest_data = process_test_data()\n\nfig = plt.figure()\n\nfor num,data in enumerate(test_data[:12]):\n    # cat = [1,0]\n    #dog = [0,1]\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    \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    \n    plt.title(str_label)\n    y.axes.get_xaxis().set_visible(False)\n    y.axes.get_yaxis().set_visible(False)\nplt.show()\n","execution_count":null,"outputs":[]}],"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":1}