{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["\u6df1\u5ea6\u5b66\u4e60\u533a\u5206\u732b\u72d7\u56fe\u7247\u7a0b\u5e8f"]}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "source": ["import cv2 # \u673a\u5668\u89c6\u89c9\u5e93\uff0c\u5b89\u88c5\u8bf7\u7528pip3 install opencv-python\n", "import numpy as np # \u6570\u503c\u8ba1\u7b97\u5e93\n", "import os # \u7cfb\u7edf\u5e93\n", "from random import shuffle # \u968f\u673a\u6570\u636e\u5e93 \n", "from tqdm import tqdm # \u8f93\u51fa\u8fdb\u5ea6\u5e93\n", "import matplotlib.pyplot as plt # \u5e38\u7528\u753b\u56fe\u5e93"], "outputs": []}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "source": ["train_dir = '../input/train/'\n", "test_dir = '../input/test/'\n", "img_size = 50\n", "lr = 1e-3"], "outputs": []}, {"cell_type": "markdown", "metadata": {}, "source": ["\u5c06label\u53d8\u6210\u6bcf\u4e2a\u7c7b\u522b\u7684\u6982\u7387\uff08\u7c7b\u4f3c\u4e8e\u54d1\u53d8\u91cf\uff09"]}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "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]"], "outputs": []}, {"cell_type": "markdown", "metadata": {}, "source": ["\u5904\u7406\u8bad\u7ec3\u6570\u636e\uff0c\u5927\u7ea6\u9700\u898130\u79d2"]}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "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)  # \u8bfb\u5165\u7070\u5ea6\u56fe\n", "        img = cv2.resize(img, (img_size, img_size) )  # \u5c06\u56fe\u7247\u53d8\u6210\u7edf\u4e00\u5927\u5c0f\n", "        training_data.append([np.array(img), np.array(label)])\n", "    shuffle(training_data)\n", "    return training_data\n", "        "], "outputs": []}, {"cell_type": "code", "execution_count": null, "metadata": {}, "source": ["train_data = create_train_data()"], "outputs": []}, {"cell_type": "markdown", "metadata": {}, "source": ["\u5904\u7406\u6d4b\u8bd5\u6570\u636e\uff0c\u6ce8\u610f\u6ca1\u6709label\u4fe1\u606f"]}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "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"], "outputs": []}, {"cell_type": "markdown", "metadata": {}, "source": ["\u5bfc\u5165\u57fa\u4e8eTensorflow\u7684\u9ad8\u7ea7\u6df1\u5ea6\u5b66\u4e60\u5e93tflearn"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "source": ["import tflearn # \u9700\u8981\u5b89\u88c5tensorflow\uff0c\u7136\u540e\u5b89\u88c5tflearn\n", "from tflearn.layers.conv import conv_2d, max_pool_2d  # 2\u7ef4CNN\u4ee5\u53ca\u6700\u5927\u91c7\u6837\n", "from tflearn.layers.core import input_data, dropout, fully_connected # \u8f93\u5165\u5c42\uff0cdropout\uff0c\u5168\u8fde\u63a5\u5c42\n", "from tflearn.layers.estimator import regression # cross entropy\u5c42"], "outputs": []}, {"cell_type": "markdown", "metadata": {}, "source": ["\u6ce8\u610f\uff1a\u5982\u679c\u591a\u6b21\u8fd0\u884c\u4e0d\u540c\u7684\u7f51\u7edc\u7ed3\u6784\u56fe\uff0c\u6bcf\u6b21\u9700\u8981\u5148\u6e05\u7a7a\u56fe"]}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "source": ["import tensorflow as tf\n", "tf.reset_default_graph()"], "outputs": []}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "source": ["convnet = input_data(shape = [None, img_size, img_size, 1], name = 'input')"], "outputs": []}, {"cell_type": "markdown", "metadata": {}, "source": ["3\u4e2aCNN\u53ca2\u4e2amax pooling\u5c42\n", "\n", "-- conv_2d(convnet, 32, 5, activation='relu')\u8868\u793a\u5efa\u7acb32\u4e2afilter\uff0c\u6bcf\u4e00\u4e2a\u5927\u5c0f\u90fd\u662f5x5xchannel\u3002\u56e0\u4e3a\u9ed8\u8ba4\u4f7f\u7528\"SAME\" padding,\u52a0\u4e860\u4f7f\u5f97\u6bcf\u4e2a\u70b9\u90fd\u53ef\u4ee5\u5bf9\u5e94filter\u5de6\u4e0a\u89d2\n", "\n", "-- max_pool_2d(convnet, 5)\u8868\u793a\u5bf9\u4e8e\u8f93\u51fa\u7684\u6bcf\u4e2a5x5\u7684\u533a\u57df\u6c42\u6700\u5927\u503c"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "source": ["convnet = conv_2d(convnet, 32, 5, activation='relu')\n", "convnet = max_pool_2d(convnet, 5)\n", "\n", "convnet = conv_2d(convnet, 64, 5, activation='relu')\n", "convnet = max_pool_2d(convnet, 5)\n", "\n", "convnet = conv_2d(convnet, 128, 5, activation='relu')\n", "convnet = max_pool_2d(convnet, 5)\n", "\n", "convnet = conv_2d(convnet, 64, 5, activation='relu')\n", "convnet = max_pool_2d(convnet, 5)\n", "\n", "convnet = conv_2d(convnet, 32, 5, activation='relu')\n", "convnet = max_pool_2d(convnet, 5)"], "outputs": []}, {"cell_type": "markdown", "metadata": {}, "source": ["\u4e24\u4e2a\u5168\u8fde\u63a5\u5c42\u4e0e\u9884\u6d4b\u5c42"]}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "source": ["convnet = fully_connected(convnet, 1024, activation = 'relu')\n", "convnet = dropout(convnet, 0.8)\n", "\n", "convnet = fully_connected(convnet, 2, activation='softmax')\n", "convnet = regression(convnet, optimizer='adam', learning_rate = lr, loss='categorical_crossentropy', name='targets')"], "outputs": []}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "source": ["model = tflearn.DNN(convnet, tensorboard_dir='log')"], "outputs": []}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "source": ["train = train_data[:-500]"], "outputs": []}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "source": ["test = train_data[-500:]"], "outputs": []}, {"cell_type": "code", "execution_count": null, "metadata": {}, "source": ["X = np.array([i[0] for i in train], dtype=np.float64).reshape(-1, img_size, img_size, 1)\n", "y = np.array([i[1] for i in train], dtype=np.float64)\n", "Xtest = np.array([i[0] for i in test], dtype=np.float64).reshape(-1, img_size, img_size, 1)\n", "ytest = np.array([i[1] for i in test], dtype=np.float64)"], "outputs": []}, {"cell_type": "markdown", "metadata": {}, "source": ["\u5728\u8bad\u7ec3\u96c6\u4e0a\u8bad\u7ec3\uff0c\u65e0GPU\u5927\u7ea6\u9700\u89812-3\u5206\u949f"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "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' )"], "outputs": []}, {"cell_type": "markdown", "metadata": {}, "source": ["\u8bfb\u5165\u6d4b\u8bd5\u6570\u636e\uff0c\u5927\u7ea6\u9700\u898130\u79d2"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "source": ["test_data = process_test_data()"], "outputs": []}, {"cell_type": "markdown", "metadata": {"collapsed": true}, "source": ["\u8f93\u51fa\u6d4b\u8bd5\u6570\u636e\u4e0a\u4e00\u4e9b\u4f8b\u5b50\u7684\u9884\u6d4b"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "source": ["fig = plt.figure()\n", "for 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", "plt.tight_layout()\n", "plt.show()"], "outputs": []}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "source": [], "outputs": []}], "nbformat_minor": 1, "metadata": {"kernelspec": {"display_name": "Python 3", "name": "python3", "language": "python"}, "language_info": {"mimetype": "text/x-python", "file_extension": ".py", "name": "python", "codemirror_mode": {"name": "ipython", "version": 3}, "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1"}}, "nbformat": 4}