{"cells":[{"metadata":{"collapsed":true,"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":false},"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\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"collapsed":true},"cell_type":"code","source":"%matplotlib inline\nimport numpy as np\nimport pandas as pd\nimport datetime as dt\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.axes_grid1 import ImageGrid\nfrom os import listdir, makedirs\nfrom os.path import join, exists, expanduser\nfrom tqdm import tqdm\nfrom sklearn.metrics import log_loss, accuracy_score\nfrom keras.preprocessing import image\n\nfrom keras.applications import xception\nfrom keras.applications import inception_v3\nfrom keras.applications.vgg16 import preprocess_input, decode_predictions\nfrom sklearn.linear_model import LogisticRegression","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"4b36c757-ea82-42b8-9915-91988708eee3","_uuid":"3775186f258a9a457f599ea16a3bd16047f9b601","trusted":true},"cell_type":"code","source":"inception_v3_weights = \"../input/keras-pretrained-models/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5\"\nxception_weights = \"../input/keras-pretrained-models/xception_weights_tf_dim_ordering_tf_kernels_notop.h5\"\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"534a1be2-fc0a-439e-a12f-ff1f70dff950","_uuid":"1c99b472f894d5f858491b7c76e241759b2fffc7","trusted":true,"collapsed":true},"cell_type":"code","source":"labels = pd.read_csv(\"../input/dog-breed-identification/labels.csv\")\nsample_submssion = pd.read_csv(\"../input/dog-breed-identification/sample_submission.csv\")\nprint(len(listdir(\"../input/dog-breed-identification/train\")),len(labels))\nprint(len(listdir(\"../input/dog-breed-identification/test\")),len(sample_submssion))","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"5941fb64-a342-48b2-8b86-db7cd9374b7a","trusted":true,"_uuid":"bc37f70f478af077e7e1c386f28ee416d52e3659","_kg_hide-input":true},"cell_type":"code","source":"def read_img(img_id,train_or_test,size):\n    img = image.load_img(join(\"../input/dog-breed-identification\",train_or_test,\"%s.jpg\" % img_id),\n                        target_size=size)\n    img = image.img_to_array(img)\n    return img","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"9bcaee3d-d03e-4a0f-a04f-b683bb43414c","_uuid":"db1827a23be42f6a49d00278312b56b4cca0d0c5","trusted":true,"collapsed":true},"cell_type":"code","source":"labels.head()","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"78d062e4-ea4a-4d6d-a675-23e682325b97","_uuid":"83db36f7ac6f9647223f7eee2ba51b3431afe64e","trusted":true},"cell_type":"code","source":"target_series = pd.Series(labels['breed'])\none_hot = pd.get_dummies(target_series,sparse=True)\none_hot_labels = np.asarray(one_hot)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"1f3c811a-6c76-4e74-a0cb-d939b765c32b","_uuid":"0253925e972b2b9e8f7a2f51cd790eeae617993f","trusted":true,"collapsed":true},"cell_type":"code","source":"#INPUT_SIZE = 299\n#输入设置为299，内存开销太大了\ny_train = []\nINPUT_SIZE = 260\nPOOLING = 'avg'\ny_train = np.zeros((len(labels),120),dtype=np.uint8)\nx_train = np.zeros((len(labels),INPUT_SIZE,INPUT_SIZE,3),dtype='float32')\nfor i,img_id in tqdm(enumerate(labels['id'])):\n    img = read_img(img_id,'train',(INPUT_SIZE,INPUT_SIZE))\n    x = xception.preprocess_input(np.expand_dims(img.copy(),axis=0))\n    x_train[i] = x\n    label = one_hot_labels[i]\n    y_train[i] = label\nprint(\"Train Images shape: {} size: {:,}\".format(x_train.shape,x_train.size))\nprint(\"Target Images shape: {} size: {:,}\".format(y_train.shape,y_train.size))","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"dea6f914-ba0d-47bb-bce8-8b244197c2b8","_uuid":"ca606ea62a080fbb4f53fb7418590257a78dbe54","trusted":false},"cell_type":"code","source":"np.save(\"x_train.npy\",x_train)\nnp.save(\"y_train.npy\",y_train)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"9403904b-5e10-418c-9997-474b8b0694be","_uuid":"47931246b010038f2c2904cbc8bca0ba14aa3d35","trusted":true,"collapsed":true},"cell_type":"code","source":"from sklearn.cross_validation import train_test_split\nX_train,X_test,Y_train,Y_test = train_test_split(x_train,y_train,test_size=0.3,random_state=7)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"d4822079-524b-474e-a009-4092c540425b","_uuid":"773d0ba8eb2570cebd288688d5718598a1587339","trusted":true},"cell_type":"code","source":"\nxception_bottleneck = xception.Xception(weights=xception_weights, include_top=False, pooling=POOLING)\ntrain_x_bf = xception_bottleneck.predict(X_train, batch_size=32, verbose=1)\nvalid_x_bf = xception_bottleneck.predict(X_test, batch_size=32, verbose=1)\nprint('Xception train bottleneck features shape: {} size: {:,}'.format(train_x_bf.shape, train_x_bf.size))\nprint('Xception valid bottleneck features shape: {} size: {:,}'.format(valid_x_bf.shape, valid_x_bf.size))","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"8313ab66-a8e0-4404-972f-6e23768cf333","_uuid":"b99bf75713f762dcbc207b027839023fbc66f9ec","trusted":false},"cell_type":"code","source":"logreg = LogisticRegression(multi_class='multinomial', solver='lbfgs', random_state=7)\nlogreg.fit(train_x_bf, (Y_train* range(120)).sum(axis=1))\nvalid_probs = logreg.predict_proba(valid_x_bf)\nvalid_preds = logreg.predict(valid_x_bf)\nprint('Validation Xception LogLoss {}'.format(log_loss(Y_test, valid_probs)))\nprint('Validation Xception Accuracy {}'.format(accuracy_score((Y_test * range(120)).sum(axis=1), valid_preds)))","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"cd45743f-5de7-4915-9e17-a252380c5c7c","_uuid":"f336a1acbe92948e7a9e3e70e691aada5ee9f34e","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"language_info":{"name":"python","version":"3.6.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}