{"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":"!pip install tensorflow\n!pip install keras","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:45:56.035045Z","iopub.execute_input":"2023-09-25T15:45:56.035654Z","iopub.status.idle":"2023-09-25T15:46:59.322715Z","shell.execute_reply.started":"2023-09-25T15:45:56.035624Z","shell.execute_reply":"2023-09-25T15:46:59.321526Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Importing required libraries\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nimport gc\nimport tensorflow as tf\nfrom tqdm.autonotebook import tqdm\nimport numpy as np\nimport pandas as pd\nfrom keras import Sequential\nfrom keras.callbacks import EarlyStopping\nfrom keras.optimizers import Adam, SGD\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.layers import Flatten, Dense, BatchNormalization, Activation, Dropout, Lambda, Input, GlobalAveragePooling2D\nfrom keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing import image\nfrom keras import regularizers\nfrom tensorflow.keras.models import Model\nfrom keras.applications.imagenet_utils import decode_predictions\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\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-25T15:45:56.025853Z","iopub.execute_input":"2023-09-25T15:45:56.026259Z","iopub.status.idle":"2023-09-25T15:45:56.033379Z","shell.execute_reply.started":"2023-09-25T15:45:56.026227Z","shell.execute_reply":"2023-09-25T15:45:56.032541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#reading labels csv file\n\nlabels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:47:35.976167Z","iopub.execute_input":"2023-09-25T15:47:35.976567Z","iopub.status.idle":"2023-09-25T15:47:36.008996Z","shell.execute_reply.started":"2023-09-25T15:47:35.976536Z","shell.execute_reply":"2023-09-25T15:47:36.003698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#describe\nlabels.describe()","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:47:38.170440Z","iopub.execute_input":"2023-09-25T15:47:38.170831Z","iopub.status.idle":"2023-09-25T15:47:38.194436Z","shell.execute_reply.started":"2023-09-25T15:47:38.170801Z","shell.execute_reply":"2023-09-25T15:47:38.193098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#function to show bar length\ndef barw(ax): \n    \n    for p in ax.patches:\n        val = p.get_width() #height of the bar\n        x = p.get_x()+ p.get_width() # x- position \n        y = p.get_y() + p.get_height()/2 #y-position\n        ax.annotate(round(val,2),(x,y))\n        \n#finding top dog brands\n\nplt.figure(figsize = (15,30))\nax0 =sns.countplot(y=labels['breed'],order=labels['breed'].value_counts().index)\nbarw(ax0)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:47:52.101923Z","iopub.execute_input":"2023-09-25T15:47:52.102428Z","iopub.status.idle":"2023-09-25T15:47:54.056225Z","shell.execute_reply.started":"2023-09-25T15:47:52.102392Z","shell.execute_reply":"2023-09-25T15:47:54.055362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lets check one image\nfrom IPython.display import display, Image\nImage(\"../input/dog-breed-identification/train/0162107acd8f2588c0944b791d61bb0c.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:51:17.871803Z","iopub.execute_input":"2023-09-25T15:51:17.872260Z","iopub.status.idle":"2023-09-25T15:51:17.888684Z","shell.execute_reply.started":"2023-09-25T15:51:17.872229Z","shell.execute_reply":"2023-09-25T15:51:17.887157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check dataset correct or not\nimport os\nif len(os.listdir('/kaggle/input/dog-breed-identification/train/')) == len(labels['id']):\n    print('Number of file matches number of actual images!')\nelse:\n    print('Number of file doesnot matches number of actual images!!')","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:53:12.046781Z","iopub.execute_input":"2023-09-25T15:53:12.047259Z","iopub.status.idle":"2023-09-25T15:53:12.290849Z","shell.execute_reply.started":"2023-09-25T15:53:12.047217Z","shell.execute_reply":"2023-09-25T15:53:12.289652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create list of alphabetically sorted labels.\nclasses = sorted(list(set(labels['breed'])))\nn_classes = len(classes)\n\n# Map each label string to an integer label.\nclass_to_num = dict(zip(classes, range(n_classes)))\n\n# Print the total unique breed count\nprint('Total unique breed {}'.format(n_classes))\n","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:59:10.365473Z","iopub.execute_input":"2023-09-25T15:59:10.365915Z","iopub.status.idle":"2023-09-25T15:59:10.374587Z","shell.execute_reply.started":"2023-09-25T15:59:10.365884Z","shell.execute_reply":"2023-09-25T15:59:10.373315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (331,331,3)\n\n\ndef images_to_array(directory, label_dataframe, target_size = input_shape):\n    \n    image_labels = label_dataframe['breed']\n    images = np.zeros([len(label_dataframe), target_size[0], target_size[1], target_size[2]],dtype=np.uint8) #as we have huge data and limited ram memory. uint8 takes less memory\n    y = np.zeros([len(label_dataframe),1],dtype = np.uint8)\n    \n    for ix, image_name in enumerate(tqdm(label_dataframe['id'].values)):\n        img_dir = os.path.join(directory, image_name + '.jpg')\n        img = load_img(img_dir, target_size = target_size)\n#         img = np.expand_dims(img, axis=0)\n#         img = processed_image_resnet(img)\n#         img = img/255\n        images[ix]=img\n#         images[ix] = img_to_array(img)\n        del img\n        \n        dog_breed = image_labels[ix]\n        y[ix] = class_to_num[dog_breed]\n    \n    y = to_categorical(y)\n    \n    return images,y","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:03:18.217169Z","iopub.execute_input":"2023-09-25T16:03:18.217593Z","iopub.status.idle":"2023-09-25T16:03:18.227079Z","shell.execute_reply.started":"2023-09-25T16:03:18.217558Z","shell.execute_reply":"2023-09-25T16:03:18.225322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X,y = images_to_array('/kaggle/input/dog-breed-identification/train', labels[:])","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:03:35.614763Z","iopub.execute_input":"2023-09-25T16:03:35.615252Z","iopub.status.idle":"2023-09-25T16:05:39.789166Z","shell.execute_reply.started":"2023-09-25T16:03:35.615215Z","shell.execute_reply":"2023-09-25T16:05:39.787774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# np.where(y[5]==1)[0][0]\n\n# lets check some dogs and their breeds\nn=25\n\n# setup the figure \nplt.figure(figsize=(20,20))\n\nfor i in range(n):\n    ax = plt.subplot(5, 5, i+1)\n    plt.title(classes[np.where(y[i] ==1)[0][0]])\n    plt.imshow(X[i].astype('int32')) # .astype('int32') ---> as imshow() needs integer data to read the image\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:08:48.848751Z","iopub.execute_input":"2023-09-25T16:08:48.849160Z","iopub.status.idle":"2023-09-25T16:08:53.119468Z","shell.execute_reply.started":"2023-09-25T16:08:48.849128Z","shell.execute_reply":"2023-09-25T16:08:53.117554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ansdjkasdkjadjknskjnkanjnsak","metadata":{},"execution_count":null,"outputs":[]}]}