{"cells":[{"metadata":{"_cell_guid":"141438c5-bc29-489b-a120-08a610f1da95","_uuid":"10f5c4c7227173a855be2ea0b0954c7a4bb90183"},"cell_type":"markdown","source":"Deep Learning method to differentiate dogs and cats in pictures\n深度学习区分猫狗图片程序\n###  https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition\n##### jiuzhang lecture"},{"metadata":{"_cell_guid":"591467a0-ad40-4876-998a-ef02a2e17ead","_uuid":"e538ad3ad4362dd9a089263502a724133bd059f1","collapsed":true,"trusted":true},"cell_type":"code","source":"import cv2 # please install cv module: pip3 install opencv-python, or python -m pip install opencv-python\nimport numpy as np \nimport os \nfrom random import shuffle # for randomization \nfrom tqdm import tqdm # for progress tracking\nimport matplotlib.pyplot as plt ","execution_count":19,"outputs":[]},{"metadata":{"_cell_guid":"4361fb6e-8b4e-445c-88ae-e286f2ee284e","_uuid":"103c5d0a30402a28b556b6d68af9ac1a0da2298e","collapsed":true,"trusted":true},"cell_type":"code","source":"train_dir = '../input/train/'\ntest_dir = '../input/test/'\nimg_size = 50\nlr = 1e-3","execution_count":20,"outputs":[]},{"metadata":{"_cell_guid":"2faeef3a-f5da-4eab-9e32-3aef57fe5092","_uuid":"dc1704fcc629ae7dcf4d72797952d63f7cd7043b","collapsed":true},"cell_type":"markdown","source":"将label变成每个类别的概率（类似于哑变量）\nConvert label for each image to probability, similar to dummy variable"},{"metadata":{"_cell_guid":"db206027-aa57-4178-98c4-9059402a2e2f","_uuid":"1b6271d603fd241801df0b457b26bbc7a05d5f2e","collapsed":true,"trusted":true},"cell_type":"code","source":"def label_img(img):\n    word_label = img.split('.')[-3]\n    if word_label == 'cat': return [1,0]\n    elif word_label == 'dog': return [0,1]","execution_count":21,"outputs":[]},{"metadata":{"_cell_guid":"e2c469a6-cee9-448f-a584-e268d490ddf4","_uuid":"616c08229e464d1850a930fa49f937c1a38a6df7"},"cell_type":"markdown","source":"处理训练数据，大约需要30秒\nTrain the traning data, approx. 30sec"},{"metadata":{"_cell_guid":"5a1da08e-d8bf-4ed0-bfac-ada868f3678d","_uuid":"805c4bca3e60a73a5aacd1fdda8ee5eca0ab13ec","collapsed":true,"trusted":true},"cell_type":"code","source":"def create_train_data():\n    training_data = []\n    for img in tqdm(os.listdir(train_dir)):\n        if (not img.endswith('.jpg')):\n            continue\n        label = label_img(img)\n        path = os.path.join(train_dir, img)\n        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)  # 读入灰度图 read grayscale\n        img = cv2.resize(img, (img_size, img_size) )  # 将图片变成统一大小 resize an image to preset size\n        training_data.append([np.array(img), np.array(label)])\n    shuffle(training_data)\n    return training_data\n        ","execution_count":22,"outputs":[]},{"metadata":{"_cell_guid":"bc8a9fa3-92b4-4dcf-95c4-6d2d4073744f","_uuid":"bc44db55358633136a5d4399825ad57ae6fcbd97","trusted":true},"cell_type":"code","source":"train_data = create_train_data()","execution_count":23,"outputs":[]},{"metadata":{"_cell_guid":"b107631b-2ca5-4a9d-ad59-520e80f2e08f","_uuid":"29c99e3720b3bc80cc7240d0396fe3290cef643f"},"cell_type":"markdown","source":"处理测试数据，注意没有label信息\nProcess test data, note there's no label"},{"metadata":{"_cell_guid":"108a668f-b005-4211-a885-98d73a2517ce","_uuid":"a523d3a0042569429f10b53e69305e8a0df1d632","collapsed":true,"trusted":true},"cell_type":"code","source":"def process_test_data():\n    testing_data = []\n    for img in tqdm(os.listdir(test_dir)):\n        if (not img.endswith('.jpg')):\n            continue\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    return testing_data","execution_count":24,"outputs":[]},{"metadata":{"_cell_guid":"c6f6f064-bee9-4bcf-84e6-de024c2129b0","_uuid":"287ce69b126238438a7bb0b895a0f162e0e62b18"},"cell_type":"markdown","source":"导入基于Tensorflow的高级深度学习库tflearn\nNeed to import Tensorflow model, tflearn"},{"metadata":{"_cell_guid":"498239ca-052f-465a-88a5-329ac373c2aa","_uuid":"864e9aba96ff217b6bd2a8a908377df9c4f5f81d","trusted":true,"collapsed":true},"cell_type":"code","source":"import tflearn # 需要安装tensorflow，然后安装tflearn\nfrom tflearn.layers.conv import conv_2d, max_pool_2d  # 2维CNN以及最大采样\nfrom tflearn.layers.core import input_data, dropout, fully_connected # 输入层，dropout，全连接层\nfrom tflearn.layers.estimator import regression # cross entropy层","execution_count":25,"outputs":[]},{"metadata":{"_cell_guid":"f879784a-60ba-40b7-acdc-cf9347cb038e","_uuid":"343b9e0f3eab97a2991a780fa7724a9f07ad74b4"},"cell_type":"markdown","source":"注意：如果多次运行不同的网络结构图，每次需要先清空图\nWarning: before every run, need to empty first using reset_default_graph"},{"metadata":{"_cell_guid":"688e226a-539e-4f2a-bb46-4e669e37471d","_uuid":"5c81ec910cd2bffebdfddb04e0c7ca116088784d","collapsed":true,"trusted":true},"cell_type":"code","source":"import tensorflow as tf\ntf.reset_default_graph()","execution_count":26,"outputs":[]},{"metadata":{"_cell_guid":"3a07c843-c6b8-4ce7-a706-2393e4949f70","_uuid":"2e38ea4daddde06aee53bf6a7e2144a7b8a48e88","collapsed":true,"trusted":true},"cell_type":"code","source":"convnet = input_data(shape = [None, img_size, img_size, 1], name = 'input')","execution_count":27,"outputs":[]},{"metadata":{"_cell_guid":"48e4cc9d-ee9b-4061-99cd-01b2cbc269c3","_uuid":"49f08ccf7dfec64267031fd87543253e558c37c8"},"cell_type":"markdown","source":"3个CNN及2个max pooling层\n\n-- conv_2d(convnet, 32, 5, activation='relu')表示建立32个filter，每一个大小都是5x5xchannel。因为默认使用\"SAME\" padding,加了0使得每个点都可以对应filter左上角\n\n-- max_pool_2d(convnet, 5)表示对于输出的每个5x5的区域求最大值"},{"metadata":{"_cell_guid":"0c78848f-c13d-4ce3-a127-19bd0cb30c18","_uuid":"887d159d1e98bfa22a05c128b15e5bcf8ffa8846","trusted":true,"collapsed":true},"cell_type":"code","source":"convnet = 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)\n\n# 这里这样设置，是借鉴前人，确实有效果","execution_count":28,"outputs":[]},{"metadata":{"_cell_guid":"8344604c-82ec-47fa-bd42-58d1cb8b28bf","_uuid":"1db2ab11c09c3edb10e410f87be1493c42146e72"},"cell_type":"markdown","source":"两个全连接层与预测层"},{"metadata":{"_cell_guid":"b4a4691f-89a7-4ebe-8b53-7da8c88a2e94","_uuid":"e1d449c84b4dbc42c30a78dee6cdd83faea85ba7","collapsed":true,"trusted":true},"cell_type":"code","source":"convnet = 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')","execution_count":29,"outputs":[]},{"metadata":{"_cell_guid":"b7765604-d154-4539-9978-5e2b7b310f78","_uuid":"61aa750ba548209506ed9eee8027bea91f87ff07","collapsed":true,"trusted":true},"cell_type":"code","source":"model = tflearn.DNN(convnet, tensorboard_dir='log')","execution_count":30,"outputs":[]},{"metadata":{"_cell_guid":"3d7d62d7-7aaf-421d-8677-e093b4209b9c","_uuid":"80f7e3b6e0301ebd02fb79cf3a9ba4f7ae612b6c","collapsed":true,"trusted":true},"cell_type":"code","source":"train = train_data[:-500]","execution_count":31,"outputs":[]},{"metadata":{"_cell_guid":"90551dff-fa36-495a-8868-6b60cc0e7396","_uuid":"daacfe03d65342e8206bf686f0b27d8e9fe4dafe","collapsed":true,"trusted":true},"cell_type":"code","source":"test = train_data[-500:] # for validation","execution_count":32,"outputs":[]},{"metadata":{"_cell_guid":"1870c703-9886-4730-b2f4-488cd6b253c1","_uuid":"8f1a384ebfbc2b973617926341cd979ac7832108","collapsed":true,"trusted":true},"cell_type":"code","source":"X = np.array([i[0] for i in train], dtype=np.float64).reshape(-1, img_size, img_size, 1)\ny = np.array([i[1] for i in train], dtype=np.float64)\nXtest = np.array([i[0] for i in test], dtype=np.float64).reshape(-1, img_size, img_size, 1)\nytest = np.array([i[1] for i in test], dtype=np.float64)\n# test is validation here","execution_count":33,"outputs":[]},{"metadata":{"_cell_guid":"7dfaf5e1-62bf-458e-a790-4f0b5897364c","_uuid":"d556fe4390bb7cd8d6d0ea49fb34b66e9448f7a1"},"cell_type":"markdown","source":"在训练集上训练，无GPU大约需要2-3分钟"},{"metadata":{"_cell_guid":"a990450e-bc4f-4d56-8502-bb1d6f79792a","_uuid":"e190954aefc818610e508b90a7237f572ab00354","trusted":true},"cell_type":"code","source":"model.fit({'input': X}, {'targets': y}, n_epoch=3, validation_set=({'input': Xtest}, {'targets': ytest}), snapshot_step=500, show_metric=True, run_id='model' )","execution_count":34,"outputs":[]},{"metadata":{"_cell_guid":"e92277b8-800a-43b0-aa2a-324215efed0d","_uuid":"be03d2dba0e485a91367e963937cf2733543f665"},"cell_type":"markdown","source":"读入测试数据，大约需要30秒"},{"metadata":{"_cell_guid":"6b478864-040a-481a-a6ca-1d9b7db1e39e","_uuid":"2ac9a24152fa0fc970019a52db89c8e501609b7f","trusted":true},"cell_type":"code","source":"test_data = process_test_data()","execution_count":35,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e6c55c6d855312fd3d58862592dc8893dc5b1407"},"cell_type":"code","source":"# validation results\npred = np.array([0 if x[0] > x[1] else 1 for x in model.predict(Xtest)])\nanswer = np.array([0 if x[0] > x[1] else 1 for x in ytest])\nsum(pred == answer) / len(pred) * 100.0","execution_count":36,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"76231b17f196e76cb11df97a7ae90782b50f4a2e"},"cell_type":"code","source":"answer","execution_count":37,"outputs":[]},{"metadata":{"_cell_guid":"25bfc9ef-c940-4aae-aa19-0270fb1815d4","_uuid":"4d984aafdfe5c99c82535b89c37559d94e425fa5","collapsed":true},"cell_type":"markdown","source":"输出测试数据上一些例子的预测\nNow we can test some cases from test dataset"},{"metadata":{"_cell_guid":"1c1e8043-0ed0-4889-9d84-8c23726ae108","_uuid":"8708197e4a58909c8041c345da6ee855ee5fe4f9","trusted":true},"cell_type":"code","source":"fig = plt.figure()\nfor num, data in enumerate(test_data[:16]):\n    img_num = data[1]\n    img_data = data[0]\n    y = fig.add_subplot(4, 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    if np.argmax(model_out) == 1: \n        label = 'Dog'\n    else: \n        label = 'Cat'\n    \n    y.imshow(orig, cmap='gray')\n    plt.title(label)\n    y.axes.get_xaxis().set_visible(False)\n    y.axes.get_yaxis().set_visible(False)\n    \n    #print(num, data, label)\n    \nplt.tight_layout()\nplt.show()","execution_count":38,"outputs":[]},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"faefb57bce3f90193975434f759a31da154d1403"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"b2f1d2f4-aebb-414b-b7c1-9999d448dc70","_uuid":"68e99a7d14f31e84a317efe2e8f721d173c72efc","collapsed":true,"trusted":true},"cell_type":"code","source":"#pyenv install -list","execution_count":39,"outputs":[]},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"e15f30e7d5442b183792f23e973688074e597cfa"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a406ba07b625ae3136231818668557dd8d0b508f"},"cell_type":"code","source":"#logistic regression\n'''\nlr_Xtrain = [np.ravelx[0] for x in train]\nlr_Xtrain = np.asmatrix(lr_Xtrain)\nlr_Ytrain = np.ravel(x[1][0] for x in train)\nlr_Xtrain = lr_Xtrain / 255.0\n'''\n","execution_count":40,"outputs":[]},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"e465841d6c676c5fece926654578973249449ce3"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"0e190720fc467346504b5ad9c16636980d153684"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"e5b09c31f1e67b1ac82d4b40c4882ded26491467"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"a95fa40d0f520aae67a912c7c51861b0673c4923"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"9e89322ec6dd193aed0acf3b439f330a815c7acb"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"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.4"}},"nbformat":4,"nbformat_minor":1}