{"cells": [{"metadata": {}, "cell_type": "markdown", "source": ["# \u8f7d\u5165\u6570\u636e\u96c6"]}, {"metadata": {"collapsed": true}, "source": ["import cv2\n", "import numpy as np\n", "from tqdm import tqdm\n", "import pandas as pd"], "cell_type": "code", "outputs": [], "execution_count": 1}, {"metadata": {}, "source": ["df = pd.read_csv('labels.csv')\n", "df.head()"], "cell_type": "code", "outputs": [], "execution_count": 2}, {"metadata": {"collapsed": true}, "source": ["n = len(df)\n", "breed = set(df['breed'])\n", "n_class = len(breed)\n", "class_to_num = dict(zip(breed, range(n_class)))\n", "num_to_class = dict(zip(range(n_class), breed))"], "cell_type": "code", "outputs": [], "execution_count": 3}, {"metadata": {}, "source": ["width = 299\n", "X = np.zeros((n, width, width, 3), dtype=np.uint8)\n", "y = np.zeros((n, n_class), dtype=np.uint8)\n", "for i in tqdm(range(n)):\n", "    X[i] = cv2.resize(cv2.imread('train/%s.jpg' % df['id'][i]), (width, width))\n", "    y[i][class_to_num[df['breed'][i]]] = 1"], "cell_type": "code", "outputs": [], "execution_count": 4}, {"metadata": {}, "cell_type": "markdown", "source": ["# \u6570\u636e\u96c6\u53ef\u89c6\u5316"]}, {"metadata": {}, "source": ["import random\n", "import matplotlib.pyplot as plt\n", "\n", "%matplotlib inline\n", "%config InlineBackend.figure_format = 'retina'\n", "\n", "plt.figure(figsize=(12, 6))\n", "for i in range(8):\n", "    random_index = random.randint(0, n-1)\n", "    plt.subplot(2, 4, i+1)\n", "    plt.imshow(X[random_index][:,:,::-1])\n", "    plt.title(num_to_class[y[random_index].argmax()])"], "cell_type": "code", "outputs": [], "execution_count": 5}, {"metadata": {}, "cell_type": "markdown", "source": ["# \u5bfc\u51fa\u7279\u5f81"]}, {"metadata": {}, "source": ["from keras.layers import *\n", "from keras.models import *\n", "from keras.applications import *\n", "from keras.optimizers import *\n", "from keras.regularizers import *\n", "from keras.applications.inception_v3 import preprocess_input"], "cell_type": "code", "outputs": [], "execution_count": 6}, {"metadata": {"collapsed": true}, "source": ["def get_features(MODEL, data=X):\n", "    cnn_model = MODEL(include_top=False, input_shape=(width, width, 3), weights='imagenet')\n", "    \n", "    inputs = Input((width, width, 3))\n", "    x = inputs\n", "    x = Lambda(preprocess_input, name='preprocessing')(x)\n", "    x = cnn_model(x)\n", "    x = GlobalAveragePooling2D()(x)\n", "    cnn_model = Model(inputs, x)\n", "\n", "    features = cnn_model.predict(data, batch_size=64, verbose=1)\n", "    return features"], "cell_type": "code", "outputs": [], "execution_count": 7}, {"metadata": {}, "source": ["inception_features = get_features(InceptionV3, X)\n", "xception_features = get_features(Xception, X)\n", "features = np.concatenate([inception_features, xception_features], axis=-1)"], "cell_type": "code", "outputs": [], "execution_count": 8}, {"metadata": {}, "cell_type": "markdown", "source": ["# \u8bad\u7ec3\u6a21\u578b"]}, {"metadata": {}, "source": ["inputs = Input(features.shape[1:])\n", "x = inputs\n", "x = Dropout(0.5)(x)\n", "x = Dense(n_class, activation='softmax')(x)\n", "model = Model(inputs, x)\n", "model.compile(optimizer='adam',\n", "              loss='categorical_crossentropy',\n", "              metrics=['accuracy'])\n", "h = model.fit(features, y, batch_size=128, epochs=10, validation_split=0.1)"], "cell_type": "code", "outputs": [], "execution_count": 9}, {"metadata": {}, "cell_type": "markdown", "source": ["# \u53ef\u89c6\u5316\u6a21\u578b"]}, {"metadata": {}, "source": ["from IPython.display import SVG\n", "from keras.utils.vis_utils import model_to_dot\n", "\n", "SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))"], "cell_type": "code", "outputs": [], "execution_count": 10}, {"metadata": {}, "cell_type": "markdown", "source": ["# \u53ef\u89c6\u5316\u8bad\u7ec3\u8fc7\u7a0b"]}, {"metadata": {}, "source": ["import matplotlib.pyplot as plt\n", "\n", "%matplotlib inline\n", "%config InlineBackend.figure_format = 'retina'\n", "\n", "plt.figure(figsize=(10, 4))\n", "plt.subplot(1, 2, 1)\n", "plt.plot(h.history['loss'])\n", "plt.plot(h.history['val_loss'])\n", "plt.legend(['loss', 'val_loss'])\n", "plt.ylabel('loss')\n", "plt.xlabel('epoch')\n", "\n", "plt.subplot(1, 2, 2)\n", "plt.plot(h.history['acc'])\n", "plt.plot(h.history['val_acc'])\n", "plt.legend(['acc', 'val_acc'])\n", "plt.ylabel('acc')\n", "plt.xlabel('epoch')"], "cell_type": "code", "outputs": [], "execution_count": 15}, {"metadata": {}, "cell_type": "markdown", "source": ["# \u8f7d\u5165\u6d4b\u8bd5\u96c6"]}, {"metadata": {"collapsed": true}, "source": ["df2 = pd.read_csv('sample_submission.csv')"], "cell_type": "code", "outputs": [], "execution_count": 16}, {"metadata": {}, "source": ["n_test = len(df2)\n", "X_test = np.zeros((n_test, width, width, 3), dtype=np.uint8)\n", "for i in tqdm(range(n_test)):\n", "    X_test[i] = cv2.resize(cv2.imread('test/%s.jpg' % df2['id'][i]), (width, width))"], "cell_type": "code", "outputs": [], "execution_count": 17}, {"metadata": {}, "cell_type": "markdown", "source": ["# \u5bfc\u51fa\u6d4b\u8bd5\u96c6\u7279\u5f81"]}, {"metadata": {}, "source": ["inception_features = get_features(InceptionV3, X_test)\n", "xception_features = get_features(Xception, X_test)\n", "features_test = np.concatenate([inception_features, xception_features], axis=-1)"], "cell_type": "code", "outputs": [], "execution_count": 18}, {"metadata": {}, "cell_type": "markdown", "source": ["# \u9884\u6d4b\u6d4b\u8bd5\u96c6\u5e76\u8f93\u51fa\u7ed3\u679c"]}, {"metadata": {"collapsed": true}, "source": ["y_pred = model.predict(features_test, batch_size=128)"], "cell_type": "code", "outputs": [], "execution_count": 19}, {"metadata": {"collapsed": true}, "source": ["for b in breed:\n", "    df2[b] = y_pred[:,class_to_num[b]]"], "cell_type": "code", "outputs": [], "execution_count": 20}, {"metadata": {"collapsed": true}, "source": ["df2.to_csv('pred.csv', index=None)"], "cell_type": "code", "outputs": [], "execution_count": 21}, {"metadata": {"collapsed": true}, "source": [], "cell_type": "code", "outputs": [], "execution_count": null}], "metadata": {"language_info": {"version": "2.7.12", "pygments_lexer": "ipython2", "mimetype": "text/x-python", "codemirror_mode": {"version": 2, "name": "ipython"}, "file_extension": ".py", "nbconvert_exporter": "python", "name": "python"}, "kernelspec": {"display_name": "Python 2", "language": "python", "name": "python2"}}, "nbformat_minor": 1, "nbformat": 4}