{"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":"# 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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-05T14:36:39.774927Z","iopub.execute_input":"2023-03-05T14:36:39.775625Z","iopub.status.idle":"2023-03-05T14:36:45.279758Z","shell.execute_reply.started":"2023-03-05T14:36:39.775595Z","shell.execute_reply":"2023-03-05T14:36:45.278783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Input\nfrom keras.layers import MaxPooling2D, ZeroPadding2D, Conv2D, Flatten\nfrom keras.optimizers import Adam, SGD\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:36:45.284456Z","iopub.execute_input":"2023-03-05T14:36:45.284695Z","iopub.status.idle":"2023-03-05T14:36:47.956788Z","shell.execute_reply.started":"2023-03-05T14:36:45.284664Z","shell.execute_reply":"2023-03-05T14:36:47.955557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import img_to_array\n","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:36:47.958177Z","iopub.execute_input":"2023-03-05T14:36:47.958828Z","iopub.status.idle":"2023-03-05T14:36:48.01405Z","shell.execute_reply.started":"2023-03-05T14:36:47.958796Z","shell.execute_reply":"2023-03-05T14:36:48.012932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import load_img","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:36:48.018123Z","iopub.execute_input":"2023-03-05T14:36:48.018472Z","iopub.status.idle":"2023-03-05T14:36:48.023835Z","shell.execute_reply.started":"2023-03-05T14:36:48.018424Z","shell.execute_reply":"2023-03-05T14:36:48.022101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1=pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\ndf1.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:36:48.025475Z","iopub.execute_input":"2023-03-05T14:36:48.025814Z","iopub.status.idle":"2023-03-05T14:36:48.056755Z","shell.execute_reply.started":"2023-03-05T14:36:48.025786Z","shell.execute_reply":"2023-03-05T14:36:48.055528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_images(images, classes):\n    assert len(images) == len(classes) == 9\n    \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        \n        ax.set_xlabel(xlabel)\n        ax.xaxis.label.set_size(60)\n        ax.set_xticks([])\n        ax.set_yticks([])\n    \n  \n    \n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:36:48.057602Z","iopub.execute_input":"2023-03-05T14:36:48.057859Z","iopub.status.idle":"2023-03-05T14:36:48.066421Z","shell.execute_reply.started":"2023-03-05T14:36:48.057832Z","shell.execute_reply":"2023-03-05T14:36:48.065469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from random import randint\nfrom tqdm import tqdm \nimport cv2\nimg_width=250\nimg_height=250\nimages=[]\nclasses=[]\nfor f, breed in tqdm(df1.values):\n    img = cv2.imread('/kaggle/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":"2023-03-05T14:36:48.068094Z","iopub.execute_input":"2023-03-05T14:36:48.068422Z","iopub.status.idle":"2023-03-05T14:37:30.741245Z","shell.execute_reply.started":"2023-03-05T14:36:48.068386Z","shell.execute_reply":"2023-03-05T14:37:30.740076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_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\nplot_images(images_to_show, classes_to_show)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:37:30.742618Z","iopub.execute_input":"2023-03-05T14:37:30.742899Z","iopub.status.idle":"2023-03-05T14:37:33.147794Z","shell.execute_reply.started":"2023-03-05T14:37:30.74287Z","shell.execute_reply":"2023-03-05T14:37:33.146671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.breed.unique()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:38:45.482829Z","iopub.execute_input":"2023-03-05T14:38:45.48321Z","iopub.status.idle":"2023-03-05T14:38:45.492865Z","shell.execute_reply.started":"2023-03-05T14:38:45.483176Z","shell.execute_reply":"2023-03-05T14:38:45.491578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_ohe=to_categorical(y_train)\ny_test_ohe=to_categorical(y_test)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:33:56.678158Z","iopub.execute_input":"2023-03-05T14:33:56.678579Z","iopub.status.idle":"2023-03-05T14:33:56.684635Z","shell.execute_reply.started":"2023-03-05T14:33:56.678543Z","shell.execute_reply":"2023-03-05T14:33:56.683227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CLASS_NAMES = list(df1.breed.unique())\nlabels = df1[(df1['breed'].isin(CLASS_NAMES))]\nlabels = labels.reset_index()\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:38:58.627291Z","iopub.execute_input":"2023-03-05T14:38:58.627727Z","iopub.status.idle":"2023-03-05T14:38:58.644291Z","shell.execute_reply.started":"2023-03-05T14:38:58.627688Z","shell.execute_reply":"2023-03-05T14:38:58.643048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.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 keras.optimizers import Adam\nX_data = np.zeros((len(labels), 224, 224, 3), dtype='float32')\nY_data = label_binarize(labels['breed'], classes = CLASS_NAMES)\nfor i in tqdm(range(len(labels))):\n    img = image.load_img('/kaggle/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\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":"2023-03-05T14:39:05.339908Z","iopub.execute_input":"2023-03-05T14:39:05.340318Z","iopub.status.idle":"2023-03-05T14:39:54.661552Z","shell.execute_reply.started":"2023-03-05T14:39:05.34028Z","shell.execute_reply":"2023-03-05T14:39:54.660066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_data.shape)\nprint(Y_data.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:40:03.784808Z","iopub.execute_input":"2023-03-05T14:40:03.785247Z","iopub.status.idle":"2023-03-05T14:40:03.792151Z","shell.execute_reply.started":"2023-03-05T14:40:03.7852Z","shell.execute_reply":"2023-03-05T14:40:03.790679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test=train_test_split(X_data,Y_data,test_size=0.15)\nprint(X_train.shape)\nprint(y_train.shape)\nprint(X_test.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:40:07.312163Z","iopub.execute_input":"2023-03-05T14:40:07.312583Z","iopub.status.idle":"2023-03-05T14:40:08.373003Z","shell.execute_reply.started":"2023-03-05T14:40:07.312546Z","shell.execute_reply":"2023-03-05T14:40:08.371603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:40:12.208803Z","iopub.execute_input":"2023-03-05T14:40:12.209148Z","iopub.status.idle":"2023-03-05T14:40:12.215415Z","shell.execute_reply.started":"2023-03-05T14:40:12.209113Z","shell.execute_reply":"2023-03-05T14:40:12.214083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, Input, Dense, MaxPool2D, Activation, Flatten, Dropout\nfrom tensorflow.keras import regularizers","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:42:13.20338Z","iopub.execute_input":"2023-03-05T14:42:13.203778Z","iopub.status.idle":"2023-03-05T14:42:13.210187Z","shell.execute_reply.started":"2023-03-05T14:42:13.203746Z","shell.execute_reply":"2023-03-05T14:42:13.20912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(filters=10, kernel_size=(3,3),strides=(1,1),padding='same',\n                kernel_regularizer=regularizers.L1L2(l1=1,l2=0)))\nmodel.add(Activation(\"relu\"))\nmodel.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\nmodel.add(Dropout(0.1))\nmodel.add(Flatten())\nmodel.add(Dense(120,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:43:01.300678Z","iopub.execute_input":"2023-03-05T14:43:01.301052Z","iopub.status.idle":"2023-03-05T14:43:01.427174Z","shell.execute_reply.started":"2023-03-05T14:43:01.301022Z","shell.execute_reply":"2023-03-05T14:43:01.425515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"categorical_crossentropy\",optimizer='adam',metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:43:30.123709Z","iopub.execute_input":"2023-03-05T14:43:30.12412Z","iopub.status.idle":"2023-03-05T14:43:30.155527Z","shell.execute_reply.started":"2023-03-05T14:43:30.124082Z","shell.execute_reply":"2023-03-05T14:43:30.154508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x=X_train,\n          y=y_train,\n          batch_size=1000,\n          validation_data=(X_test,y_test),\n         epochs=50)","metadata":{"execution":{"iopub.status.busy":"2023-03-05T14:44:09.897664Z","iopub.execute_input":"2023-03-05T14:44:09.899008Z","iopub.status.idle":"2023-03-05T15:59:35.153859Z","shell.execute_reply.started":"2023-03-05T14:44:09.89894Z","shell.execute_reply":"2023-03-05T15:59:35.152318Z"},"trusted":true},"execution_count":null,"outputs":[]}]}