{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np         \nimport pandas as pd        \nfrom random import randint\nfrom tqdm import tqdm      \n\nimport cv2\nimport matplotlib.pyplot as plt\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input/dog-breed-identification\"]).decode(\"utf8\"))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-19T06:57:35.391154Z","iopub.execute_input":"2022-03-19T06:57:35.391593Z","iopub.status.idle":"2022-03-19T06:57:35.4212Z","shell.execute_reply.started":"2022-03-19T06:57:35.391562Z","shell.execute_reply":"2022-03-19T06:57:35.42002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing import image\nfrom sklearn.preprocessing import label_binarize\nfrom sklearn.model_selection import train_test_split\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2022-03-19T06:57:38.731884Z","iopub.execute_input":"2022-03-19T06:57:38.732643Z","iopub.status.idle":"2022-03-19T06:57:38.7384Z","shell.execute_reply.started":"2022-03-19T06:57:38.732591Z","shell.execute_reply":"2022-03-19T06:57:38.737498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(\"../input/dog-breed-identification/labels.csv\")\nprint(df_train.shape)\ndf_train.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-03-19T06:58:01.533315Z","iopub.execute_input":"2022-03-19T06:58:01.533715Z","iopub.status.idle":"2022-03-19T06:58:01.609091Z","shell.execute_reply.started":"2022-03-19T06:58:01.533676Z","shell.execute_reply":"2022-03-19T06:58:01.607868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"breeds_all = df_train[\"breed\"]\nbreed_counts = breeds_all.value_counts()\nbreed_counts.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T06:58:12.127619Z","iopub.execute_input":"2022-03-19T06:58:12.128222Z","iopub.status.idle":"2022-03-19T06:58:12.143193Z","shell.execute_reply.started":"2022-03-19T06:58:12.128174Z","shell.execute_reply":"2022-03-19T06:58:12.142359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-03-19T07:22:01.147066Z","iopub.status.idle":"2022-03-19T07:22:01.147939Z","shell.execute_reply.started":"2022-03-19T07:22:01.14772Z","shell.execute_reply":"2022-03-19T07:22:01.147745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_images(images, classes):\n    assert len(images) == len(classes) == 9\n\n    fig, axes = plt.subplots(3, 3,figsize=(60,60),sharex=True)\n    fig.subplots_adjust(hspace=0.3, wspace=0.3)\n   \n    for i, ax in enumerate(axes.flat):\n        \n        ax.imshow(cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB).reshape(img_width,img_height,3), cmap='hsv')    \n        xlabel = \"Breed: {0}\".format(classes[i])\n    \n        ax.set_xlabel(xlabel)\n        ax.xaxis.label.set_size(60)\n        ax.set_xticks([])\n        ax.set_yticks([])\n\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T06:59:27.227334Z","iopub.execute_input":"2022-03-19T06:59:27.227643Z","iopub.status.idle":"2022-03-19T06:59:27.236496Z","shell.execute_reply.started":"2022-03-19T06:59:27.227608Z","shell.execute_reply":"2022-03-19T06:59:27.23513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_width=250\nimg_height=250\nimages=[]\nclasses=[]\nfor f, breed in tqdm(df_train.values):\n    img = cv2.imread('../input/dog-breed-identification/train/{}.jpg'.format(f))\n    classes.append(breed)\n    images.append(cv2.resize(img, (img_width, img_height)))","metadata":{"execution":{"iopub.status.busy":"2022-03-19T07:00:06.135401Z","iopub.execute_input":"2022-03-19T07:00:06.1364Z","iopub.status.idle":"2022-03-19T07:02:01.803192Z","shell.execute_reply.started":"2022-03-19T07:00:06.136332Z","shell.execute_reply":"2022-03-19T07:02:01.802181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plot some example images\nrandom_numbers = [randint(0, len(images)) for p in range(0,9)]\nprint(random_numbers)\nimages_to_show = [images[i] for i in random_numbers]\nclasses_to_show = [classes[i] for i in random_numbers]\nprint(\"Images to show: {0}\".format(len(images_to_show)))\nprint(\"Classes to show: {0}\".format(len(classes_to_show)))\n\n#plot the images\nplot_images(images_to_show, classes_to_show)","metadata":{"execution":{"iopub.status.busy":"2022-03-19T07:02:07.911078Z","iopub.execute_input":"2022-03-19T07:02:07.91171Z","iopub.status.idle":"2022-03-19T07:02:09.842854Z","shell.execute_reply.started":"2022-03-19T07:02:07.911668Z","shell.execute_reply":"2022-03-19T07:02:09.838161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CLASS_NAMES = ['scottish_deerhound','maltese_dog','bernese_mountain_dog']\nlabels = df_train[(df_train['breed'].isin(CLASS_NAMES))]\nlabels = labels.reset_index()\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T07:24:39.625394Z","iopub.execute_input":"2022-03-19T07:24:39.62601Z","iopub.status.idle":"2022-03-19T07:24:39.656094Z","shell.execute_reply.started":"2022-03-19T07:24:39.625967Z","shell.execute_reply":"2022-03-19T07:24:39.655231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_data = np.zeros((len(labels), 224, 224, 3), dtype='float32')\n\nY_data = label_binarize(labels['breed'], classes = CLASS_NAMES)\n\n# Reading and converting image to numpy array and normalizing dataset\nfor i in tqdm(range(len(labels))):\n    img = image.load_img('../input/dog-breed-identification/train/%s.jpg' % labels['id'][i], target_size=(224, 224))\n    img = image.img_to_array(img)\n    x = np.expand_dims(img.copy(), axis=0)\n    X_data[i] = x / 255.0\n    \n# Printing train image and one hot encode shape & size\nprint('\\nTrain Images shape: ',X_data.shape,' size: {:,}'.format(X_data.size))\nprint('One-hot encoded output shape: ',Y_data.shape,' size: {:,}'.format(Y_data.size))","metadata":{"execution":{"iopub.status.busy":"2022-03-19T07:24:42.059891Z","iopub.execute_input":"2022-03-19T07:24:42.060519Z","iopub.status.idle":"2022-03-19T07:24:45.466871Z","shell.execute_reply.started":"2022-03-19T07:24:42.06047Z","shell.execute_reply":"2022-03-19T07:24:45.465855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters = 64, kernel_size = (5,5), activation ='relu', input_shape = (224,224,3)))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters = 32, kernel_size = (3,3), activation ='relu', kernel_regularizer = 'l2'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters = 16, kernel_size = (7,7), activation ='relu', kernel_regularizer = 'l2'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters = 8, kernel_size = (5,5), activation ='relu', kernel_regularizer = 'l2'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(128, activation = \"relu\", kernel_regularizer = 'l2'))\nmodel.add(Dense(64, activation = \"relu\", kernel_regularizer = 'l2'))\nmodel.add(Dense(len(CLASS_NAMES), activation = \"softmax\"))\n\nmodel.compile(loss = 'categorical_crossentropy', optimizer = Adam(0.0001),metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T07:24:58.565684Z","iopub.execute_input":"2022-03-19T07:24:58.566019Z","iopub.status.idle":"2022-03-19T07:24:58.679979Z","shell.execute_reply.started":"2022-03-19T07:24:58.565981Z","shell.execute_reply":"2022-03-19T07:24:58.67889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_and_val, X_test, Y_train_and_val, Y_test = train_test_split(X_data, Y_data, test_size = 0.1)\n# Splitting the training data set into training and validation data sets\n","metadata":{"execution":{"iopub.status.busy":"2022-03-19T07:25:01.877091Z","iopub.execute_input":"2022-03-19T07:25:01.877389Z","iopub.status.idle":"2022-03-19T07:25:01.970187Z","shell.execute_reply.started":"2022-03-19T07:25:01.87736Z","shell.execute_reply":"2022-03-19T07:25:01.96934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, Y_train, Y_val = train_test_split(X_train_and_val, Y_train_and_val, test_size = 0.2)","metadata":{"execution":{"iopub.status.busy":"2022-03-19T07:25:04.043081Z","iopub.execute_input":"2022-03-19T07:25:04.04337Z","iopub.status.idle":"2022-03-19T07:25:04.114269Z","shell.execute_reply.started":"2022-03-19T07:25:04.043342Z","shell.execute_reply":"2022-03-19T07:25:04.11317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 100\nbatch_size = 128\n\nhistory = model.fit(X_train, Y_train, batch_size = batch_size, epochs = epochs, \n                    validation_data = (X_val, Y_val))","metadata":{"execution":{"iopub.status.busy":"2022-03-19T07:25:07.141169Z","iopub.execute_input":"2022-03-19T07:25:07.141445Z","iopub.status.idle":"2022-03-19T07:42:40.05564Z","shell.execute_reply.started":"2022-03-19T07:25:07.141416Z","shell.execute_reply":"2022-03-19T07:42:40.054782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 5))\nplt.plot(history.history['accuracy'], color='r')\nplt.plot(history.history['val_accuracy'], color='b')\nplt.title('Model Accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epochs')\nplt.legend(['train', 'val'])\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T07:43:17.783532Z","iopub.execute_input":"2022-03-19T07:43:17.784384Z","iopub.status.idle":"2022-03-19T07:43:18.032193Z","shell.execute_reply.started":"2022-03-19T07:43:17.784326Z","shell.execute_reply":"2022-03-19T07:43:18.031195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred = model.predict(X_test)\nscore = model.evaluate(X_test, Y_test)\nprint('Accuracy over the test set: \\n ', round((score[1]*100), 2), '%')","metadata":{"execution":{"iopub.status.busy":"2022-03-19T07:44:00.303545Z","iopub.execute_input":"2022-03-19T07:44:00.30386Z","iopub.status.idle":"2022-03-19T07:44:01.118211Z","shell.execute_reply.started":"2022-03-19T07:44:00.303824Z","shell.execute_reply":"2022-03-19T07:44:01.117182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_test[1,:,:,:])\nplt.show()\n\n# Finding max value from predition list and comaparing original value vs predicted\nprint(\"Originally : \",labels['breed'][np.argmax(Y_test[1])])\nprint(\"Predicted : \",labels['breed'][np.argmax(Y_pred[1])])","metadata":{"execution":{"iopub.status.busy":"2022-03-19T07:43:40.48385Z","iopub.execute_input":"2022-03-19T07:43:40.484122Z","iopub.status.idle":"2022-03-19T07:43:40.714194Z","shell.execute_reply.started":"2022-03-19T07:43:40.484093Z","shell.execute_reply":"2022-03-19T07:43:40.713069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}