{"metadata":{"language_info":{"name":"python","nbconvert_exporter":"python","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"mimetype":"text/x-python","version":"3.6.4","pygments_lexer":"ipython3"},"kernelspec":{"display_name":"Python 3","name":"python3","language":"python"}},"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","collapsed":true,"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"},"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.","cell_type":"code","outputs":[],"execution_count":null},{"metadata":{"_uuid":"54bf49b0709c071984bb967a66a3707b63bba189","_cell_guid":"81ef98c5-0c27-436e-9cec-c9cf0d7a10fc"},"source":"**1、加载必要的库文件**","cell_type":"markdown"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0"},"source":"import numpy as np\nimport pandas as pd\nimport keras\nfrom keras.applications.vgg19 import VGG19\nfrom keras.models import Model\nfrom keras.layers import Dense,Dropout,Flatten\n\nimport os\nfrom tqdm import tqdm\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split\nimport cv2\n\nfrom subprocess import check_output\nprint(check_output([\"ls\",\"../input\"]).decode(\"utf8\"))","cell_type":"code","outputs":[],"execution_count":1},{"metadata":{"_uuid":"7debf283e51fe268e0c6502b8078c8cbce7a67b9","_cell_guid":"65a8000b-37d6-4a47-b4f7-9ae99c6a85ad"},"source":"2、读取csv文件，获取信息","cell_type":"markdown"},{"metadata":{"_uuid":"5ca6e279791f0d08119ae159dba0af7f3dd37bf0","collapsed":true,"_cell_guid":"cfa6a9d3-2305-4818-9c90-2d592fa291c7"},"source":"df_train = pd.read_csv(\"../input/dog-breed-identification/labels.csv\")\ndf_test = pd.read_csv(\"../input/dog-breed-identification/sample_submission.csv\")","cell_type":"code","outputs":[],"execution_count":2},{"metadata":{"_uuid":"d83993c302d8ab83ce6fe0918b654bc05e08645d","collapsed":true,"_cell_guid":"c5f36c73-3fbb-4e41-a9f1-2f49df0cef96"},"source":"df_train.sample(10)","cell_type":"code","outputs":[],"execution_count":null},{"metadata":{"_uuid":"17c598fc0c73221a4f019c37eeba8afd97572d5b","_cell_guid":"8c30b983-82da-43b8-a63f-2a1292ac6326"},"source":"3、数据的归一化处理","cell_type":"markdown"},{"metadata":{"_uuid":"9d55c505f01766d891c329ce050c43ee5756e09f","collapsed":true,"_cell_guid":"bc1be22a-01d5-4072-813d-b7e442c4ba94"},"source":"target_series = pd.Series(df_train['breed'])\none_hot = pd.get_dummies(target_series,sparse=True)","cell_type":"code","outputs":[],"execution_count":3},{"metadata":{"_uuid":"820d1980cbb4a6a4ecb4aee6a37c6c02fc1a9636","collapsed":true,"_cell_guid":"0f525981-0851-4e2a-9547-aeb167165f47"},"source":"one_hot_labels = np.asarray(one_hot)\n#one_hot_labels","cell_type":"code","outputs":[],"execution_count":4},{"metadata":{"_uuid":"0f327567fbd278286ce0fcc0f6269fcf7db6ba36","collapsed":true,"_cell_guid":"7fae8cbb-29ea-4101-a1f2-f0c2bd69af38"},"source":"im_size = 90\nx_train = []\ny_train = []\nx_test = []","cell_type":"code","outputs":[],"execution_count":5},{"metadata":{"_uuid":"9975525313adfd46fcd6ec269248cb45253f3cfd","_cell_guid":"990aa4da-1cda-4b30-9f15-499e9a65e714"},"source":"i = 0\nfor f,breed in tqdm(df_train.values):\n    img = cv2.imread(\"../input/dog-breed-identification/train/{}.jpg\".format(f))\n    label = one_hot_labels[i]\n    x_train.append(cv2.resize(img,(im_size,im_size)))\n    y_train.append(label)\n    i += 1","cell_type":"code","outputs":[],"execution_count":6},{"metadata":{"_uuid":"c5d19534e2ff04b9d5ec10105c43c00f58f871e9","collapsed":true,"_cell_guid":"84e8f977-1a07-42ef-8584-5689c3df7c05"},"source":"for f in tqdm(df_test['id'].values):\n    img = cv2.imread(\"../input/dog-breed-identification/test/{}.jpg\".format(f))\n    x_test.append(cv2.resize(img,(im_size,im_size)))","cell_type":"code","outputs":[],"execution_count":null},{"metadata":{"_uuid":"9088fc21f3e9d48a6f5f3e80f87fb02b0dbd4852","collapsed":true,"_cell_guid":"17d85856-3183-4e4a-894e-11377f439ca9"},"source":"y_train_raw = np.array(y_train,np.uint8)\nx_train_raw = np.array(x_train,np.float32) / 255.\nx_test = np.array(x_test,np.float32) / 255.","cell_type":"code","outputs":[],"execution_count":7},{"metadata":{"_uuid":"b5e44687cd8e0c0aa058d5f5b52ebc86d6f4f355","collapsed":true,"_cell_guid":"d6fc142f-2d90-45fc-a42d-462d27a19ed9"},"source":"print(x_train_raw.shape)\nprint(y_train_raw.shape)\n#print(x_test.shape)","cell_type":"code","outputs":[],"execution_count":null},{"metadata":{"_uuid":"fd3e364e773c1beeba2098fa0f4df0673171a74e","collapsed":true,"_cell_guid":"bb4e9211-a458-4730-9b94-595337e253ea"},"source":"num_class = y_train_raw.shape[1]","cell_type":"code","outputs":[],"execution_count":null},{"metadata":{"_uuid":"3232cf2bdde441a52e7f3e1006ebad42f44f2642","collapsed":true,"_cell_guid":"e565bb19-3a83-4722-938f-806c38d583db"},"source":"X_train,X_vaild,Y_train,Y_vaild = train_test_split(x_train_raw,y_train_raw,test_size=0.3,random_state=7)","cell_type":"code","outputs":[],"execution_count":8},{"metadata":{"_uuid":"a811ece520a3a31cc78b7e0d8733b07726df92c6","collapsed":true,"_cell_guid":"ea7cebe9-7be6-4392-84d3-6c58680acac7"},"source":"vgg16_weights_path = '../input/vgg19/vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5'","cell_type":"code","outputs":[],"execution_count":9},{"metadata":{"_uuid":"c5ca1310c6b866af7114a34ba23ecace14c31ef1","_cell_guid":"9c494509-dc89-4965-b01c-f334c548daee"},"source":"base_model = VGG19(weights=vgg16_weights_path,include_top=False,input_shape=(im_size,im_size,3))\nx = base_model.output\nx = Flatten()(x)\npredictions = Dense(120,activation='softmax')(x)\nmodel = Model(inputs=base_model.input,outputs=predictions)\n\nfor layer in base_model.layers:\n    layer.trainable = False\n\nmodel.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])\ncallbacks_list = [keras.callbacks.EarlyStopping(monitor=\"val_acc\",patience=3,verbose=1)]\nmodel.summary()\n\n","cell_type":"code","outputs":[],"execution_count":10},{"metadata":{"_uuid":"c6b173816ca10ddf2c658cd3ee9c1e0c23d67324","_cell_guid":"da6467c1-1e91-4304-ab31-1fa121e89612"},"source":"model.fit(X_train,Y_train,epochs=20,validation_data=(X_vaild,Y_vaild),verbose=1)","cell_type":"code","outputs":[],"execution_count":13},{"metadata":{"_uuid":"72f8304d83f7e293c50a646bd0ab7f4c8125424a","collapsed":true,"_cell_guid":"65033cfb-b11c-4981-8874-180e8136d123"},"source":"","cell_type":"code","outputs":[],"execution_count":null}],"nbformat_minor":1,"nbformat":4}