{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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%matplotlib inline\nimport os\nfrom tqdm import tqdm\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport numpy as np\nimport time\nfrom datetime import timedelta\nimport math\nimport cv2 \nimport re\nfrom tensorflow.python.platform import gfile\n\nprint(os.listdir(\"../input\"))\nprint(\"Train data set:\", len(os.listdir(\"../input/dog-breed-identification/train\")))\nprint(\"Test data set:\", len(os.listdir(\"../input/dog-breed-identification/test\")))\nprint(os.listdir(\"../input/vgg16\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"44d5ba2f0e89f24533aefbef523ebdc80e176e57"},"cell_type":"markdown","source":"**Read the labels**"},{"metadata":{"trusted":true,"_uuid":"c5bec06377f0461798deab56bd51f5806d55e1c4"},"cell_type":"code","source":"# all training images\ntrain_dir = '../input/dog-breed-identification/train/'\nmodel_dir = '../input/'\nlist_images = [train_dir+f for f in os.listdir(train_dir) if re.search('jpg|JPG', f)]\n\nprint(list_images[0:4])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fded3b36fa4c1a52138ccc7ef03dc79b47b67ebe"},"cell_type":"code","source":"train_dogs = pd.read_csv('../input/dog-breed-identification/labels.csv')\n#train_dogs['image_path'] = list_images\ntrain_dogs.head(5)\n\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7867ee53f02298d53618608f2d618c5117ce3698"},"cell_type":"markdown","source":"**Count number of breeds**"},{"metadata":{"trusted":true,"_uuid":"9d8be45460da4cfb8be82d95f5313958c2abfc96"},"cell_type":"code","source":"br_labels = train_dogs.groupby(\"breed\").count()\nbr_labels = br_labels.rename(columns = {\"id\" : \"count\"})\nbr_labels = br_labels.sort_values(\"count\", ascending=False)\nbr_labels.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"722e18219fb6761b5b10641176e6c1a943300c7a"},"cell_type":"markdown","source":"**Distribution of the breeds**"},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"1c7c68cc84d1bb5e1b273dac7e312922a97a92ab"},"cell_type":"code","source":"yy = pd.value_counts(train_dogs['breed'])\n\n\nfig, ax = plt.subplots()\nfig.set_size_inches(15,9)\nsns.set_style(\"whitegrid\")\n\nax = sns.barplot(x = yy.index, y = yy, data = train_dogs)\nax.set_xticklabels(ax.get_xticklabels(), rotation = 90, fontsize = 10)\nax.set(xlabel='Dog Breed', ylabel='Count')\nax.set_title('Distribution of the Dog Breeds')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0da0e215b1f2c6a0df667bafae785eb1488e74ed"},"cell_type":"markdown","source":"**One hot encoding the labels**"},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"4207b3117edecd68bde0ff689c23f5e95dce0fcc"},"cell_type":"code","source":"target_lables = train_dogs['breed']\none_hot = pd.get_dummies(target_lables, sparse = True)\none_hot_labels = np.asarray(one_hot)\n\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9f2e892caae36256d0b2b5c3abe1a0ccdbc449b2"},"cell_type":"markdown","source":"**Image parameters to be used**"},{"metadata":{"trusted":true,"_uuid":"ac6d3cd0ff6023b06de9dc4e76743ef332b6685b"},"cell_type":"code","source":"\nIMG_SIZE = 224\n\nIMG_SHAPE = (IMG_SIZE, IMG_SIZE)\n\n\nn_classes = 120","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"986c43ec3c2da8db9bb76082ce4376a40bfd79ba"},"cell_type":"markdown","source":"**Function to plot the images**"},{"metadata":{"trusted":true,"_uuid":"cf7310ca1f358d3439a153a7346555c5b8f41258","scrolled":true},"cell_type":"code","source":"x_train = []\ny_train = []\ny_orig_label = []\norig_label = []\ni = 0 \n\nfor f, breed in tqdm(train_dogs.values):\n    img = cv2.imread('../input/dog-breed-identification/train/{}.jpg'.format(f))\n    label = one_hot_labels[i]\n    orig_label = target_lables[i]\n    x_train.append(cv2.resize(img, (IMG_SIZE, IMG_SIZE)))\n    y_train.append(label)\n    y_orig_label.append(orig_label)\n    i += 1\n\n \n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2e1304ca83c209ccf92fb0effa30da2581204de"},"cell_type":"code","source":"y_train_raw = np.array(y_train, np.uint8)\nx_train_raw = np.array(x_train, np.float32) / 255.\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"73d04e31bb7dc13de64c0667b053a41ab9fedd9d"},"cell_type":"code","source":"def plot_images(images, cls_true, cls_pred=None):\n    \n    # Create figure with 3x3 sub-plots.\n    fig, axes = plt.subplots(2, 3)\n    fig.subplots_adjust(hspace=0.8, wspace=0.8)\n\n    for i, ax in enumerate(axes.flat):\n        # Plot image.\n        ax.imshow(images[i], cmap='binary')\n\n        # Show true classes.\n        xlabel = \"True: {0}\".format(cls_true[i])\n        \n        # Show the classes as the label on the x-axis.\n        ax.set_xlabel(xlabel)\n        \n        # Remove ticks from the plot.\n        ax.set_xticks([])\n        ax.set_yticks([])\n    \n    # Ensure the plot is shown correctly with multiple plots\n    # in a single Notebook cell.\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b6e3619ec2f28de8aa0861f944584b62031f431"},"cell_type":"code","source":"train_img = x_train[0:6]\nlabel_img = y_orig_label[0:6]\nplot_images(images=train_img, cls_true=label_img)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"337c265cbd73f5a4309d12e69e0b1ed8178ad82d"},"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder, LabelEncoder\npath_train = '../input/dog-breed-identification/train'\n#Generator\ndef generator(df, path):\n    \n    while 1:\n        for i in range(int(df.shape[0])):\n            img_path = os.path.join(path, df.iloc[i]['id']+ '.jpg')\n    \n            img = image.load_img(img_path, target_size=(224, 224))\n            x = image.img_to_array(img)\n            x = np.expand_dims(x, axis=0)\n            x = preprocess_input(x)\n            #y = df.iloc[i]['breed']\n            #y = onehot.transform(y).toarray()\n            #print(img.shape,np.array([y]).shape)\n            yield (x)\n                    \ngen_1 = generator(train_dogs, path_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f70ae897f26cbbf89cb4f34bc2033e1011dc25d","scrolled":true},"cell_type":"code","source":"from keras.models import Model\nfrom keras.applications.vgg16 import VGG16\nfrom keras.preprocessing import image\nfrom keras.optimizers import SGD, Adam\nfrom keras.applications.resnet50 import preprocess_input, decode_predictions\nfrom keras.layers import Dense, GlobalAveragePooling2D\nfrom keras.layers import Dense, Dropout, Flatten\nfrom keras.applications.vgg19 import VGG19\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"41c737a8be0f753dd2376a2de747aba0c56fd65d"},"cell_type":"code","source":"num_class = y_train_raw.shape[1]\n\nprint(num_class)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2f8447bd80513277aa9dc9ec670444a3ff6c6aba"},"cell_type":"code","source":"vgg16_weights = '../input/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels.h5'\nbase_model = VGG16(weights=vgg16_weights, input_shape=(224, 224, 3))\n\n\n#base_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"50df4e4f0498681b3a75ec153ee09454d5d02b89"},"cell_type":"code","source":"optimizer = Adam(lr=0.0001)\nmodel = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output)\nmodel.compile(loss='categorical_crossentropy',\n                  optimizer=optimizer,\n                  metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0ac9a4b7b1430c574fc60d5b3a0c9f366ff91444"},"cell_type":"code","source":"feature = model.predict_generator(gen_1,steps=10221, verbose=1)\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"495599559189e0a5fce97cda91d54d93e5f2ea57"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(feature, train_dogs.iloc[:10221]['breed'])\n\nprint(X_train[0:4])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1c99848b652674ea12543f198d4577949ec8072e"},"cell_type":"code","source":"#Import svm model\nfrom sklearn import svm\n\n#Create a svm Classifier\nclf = svm.SVC(kernel='linear') # Linear Kernel\n\n#Train the model using the training sets\nclf.fit(X_train, y_train)\n\n#Predict the response for test dataset\ny_pred = clf.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"72a4dc73b0f091a01f334582fd2980f5789e0c54"},"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\nacc = accuracy_score(y_test, y_pred)\n\nprint(\"Incredible accuracy of : \",acc)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"07fd1f14949ec80bfc4c0a4a2fa35434ecc0b115"},"cell_type":"code","source":"# Use the sample submission file to set up the test data - x_test\nsubmit_data = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3062e26583e4875ad074bd0013a22fb2fc63fd7d"},"cell_type":"code","source":"# Creae the x_test\nx_test = []\nfor i in tqdm(submit_data['id'].values):\n     img = cv2.imread('../input/dog-breed-identification/test/{}.jpg'.format(i))\n     x_test.append(cv2.resize(img, (224, 224)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aaf746edbb810dbbf07724c51a8eb349048208eb"},"cell_type":"code","source":"path_test = '../input/dog-breed-identification/test/'\ngen_test = generator(submit_data, path_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"44ae9d630fab0015951393c70634782f631c06f2"},"cell_type":"code","source":"feature_test = model.predict_generator(gen_test,steps=10357, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1aec23037300502c4e06cf6990226ada6fecd7ff"},"cell_type":"code","source":"predictions = clf.predict(feature_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a0697a68a26b3c250665ded669d141f655fd2029"},"cell_type":"code","source":"col_names = one_hot.columns.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"76b6600c17b290c25fa3dccd877099a0399caf58"},"cell_type":"code","source":"submission_results = pd.DataFrame(predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8def2b73c1912f777813214293dada4318919bda"},"cell_type":"code","source":"submission_results[0:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7c5793e0014c7e90bea5fe95c88c7105b2bc66e2"},"cell_type":"code","source":"submission_results.insert(0, 'id', submit_data['id'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2bc7a3b483234a96a5fd6cea1ee9f17877f25688"},"cell_type":"code","source":"submission_results.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ed4e5b83ac9b7500692df6157f0d8bb9b53352ab"},"cell_type":"markdown","source":""},{"metadata":{"_uuid":"f656ebb78351b77a7faea64c2046bc08760c16ab"},"cell_type":"markdown","source":""}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}