{"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 tensorflow as tf\nimport tensorflow_hub as hub","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-03T14:33:50.309052Z","iopub.execute_input":"2022-10-03T14:33:50.310164Z","iopub.status.idle":"2022-10-03T14:33:59.386918Z","shell.execute_reply.started":"2022-10-03T14:33:50.310047Z","shell.execute_reply":"2022-10-03T14:33:59.385201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#GETTING DATA READY ","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nlabels_csv=pd.read_csv(\"../input/dog-breed-identification/labels.csv\")\nprint(labels_csv.describe())\nlabels_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:34:14.26595Z","iopub.execute_input":"2022-10-03T14:34:14.266787Z","iopub.status.idle":"2022-10-03T14:34:14.358402Z","shell.execute_reply.started":"2022-10-03T14:34:14.266744Z","shell.execute_reply":"2022-10-03T14:34:14.35709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_csv['breed'].value_counts().plot.bar(figsize=(20,10))","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:34:25.066836Z","iopub.execute_input":"2022-10-03T14:34:25.067344Z","iopub.status.idle":"2022-10-03T14:34:27.668495Z","shell.execute_reply.started":"2022-10-03T14:34:25.067302Z","shell.execute_reply":"2022-10-03T14:34:27.666832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Image\nImage(\"../input/dog-breed-identification/train/001cdf01b096e06d78e9e5112d419397.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:35:13.015076Z","iopub.execute_input":"2022-10-03T14:35:13.015708Z","iopub.status.idle":"2022-10-03T14:35:13.042531Z","shell.execute_reply.started":"2022-10-03T14:35:13.015665Z","shell.execute_reply":"2022-10-03T14:35:13.041461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename=[\"../input/dog-breed-identification/train/\"+ fname +\".jpg\" for fname in labels_csv['id']]\n","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:37:21.110825Z","iopub.execute_input":"2022-10-03T14:37:21.111302Z","iopub.status.idle":"2022-10-03T14:37:21.121954Z","shell.execute_reply.started":"2022-10-03T14:37:21.111267Z","shell.execute_reply":"2022-10-03T14:37:21.120681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(filename)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:37:21.797861Z","iopub.execute_input":"2022-10-03T14:37:21.798376Z","iopub.status.idle":"2022-10-03T14:37:21.806107Z","shell.execute_reply.started":"2022-10-03T14:37:21.798336Z","shell.execute_reply":"2022-10-03T14:37:21.80497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename[1]","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:56:18.734993Z","iopub.execute_input":"2022-10-03T14:56:18.735517Z","iopub.status.idle":"2022-10-03T14:56:18.744386Z","shell.execute_reply.started":"2022-10-03T14:56:18.735481Z","shell.execute_reply":"2022-10-03T14:56:18.742874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nlen(os.listdir(\"../input/dog-breed-identification/train\"))","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:37:22.094574Z","iopub.execute_input":"2022-10-03T14:37:22.095559Z","iopub.status.idle":"2022-10-03T14:37:22.112227Z","shell.execute_reply.started":"2022-10-03T14:37:22.095504Z","shell.execute_reply":"2022-10-03T14:37:22.110604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image(filename[9000])","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:37:22.76226Z","iopub.execute_input":"2022-10-03T14:37:22.762752Z","iopub.status.idle":"2022-10-03T14:37:22.787597Z","shell.execute_reply.started":"2022-10-03T14:37:22.762709Z","shell.execute_reply":"2022-10-03T14:37:22.785851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_csv.iloc[9000]['breed']","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:37:31.04245Z","iopub.execute_input":"2022-10-03T14:37:31.042927Z","iopub.status.idle":"2022-10-03T14:37:31.053501Z","shell.execute_reply.started":"2022-10-03T14:37:31.042891Z","shell.execute_reply":"2022-10-03T14:37:31.051645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_csv['breed'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:37:36.256766Z","iopub.execute_input":"2022-10-03T14:37:36.257426Z","iopub.status.idle":"2022-10-03T14:37:36.27278Z","shell.execute_reply.started":"2022-10-03T14:37:36.257229Z","shell.execute_reply":"2022-10-03T14:37:36.271649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#OUR LABELS into BOOLEAN VALUES","metadata":{}},{"cell_type":"code","source":"import numpy as np\nlabels=np.array(labels_csv['breed'])\nlabels","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:37:54.997535Z","iopub.execute_input":"2022-10-03T14:37:54.998052Z","iopub.status.idle":"2022-10-03T14:37:55.009961Z","shell.execute_reply.started":"2022-10-03T14:37:54.997989Z","shell.execute_reply":"2022-10-03T14:37:55.00822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if len(labels)==len(filename):\n  print(\"yes\")\nelse:\n  print(\"no\")","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:38:10.113276Z","iopub.execute_input":"2022-10-03T14:38:10.113767Z","iopub.status.idle":"2022-10-03T14:38:10.120656Z","shell.execute_reply.started":"2022-10-03T14:38:10.113731Z","shell.execute_reply":"2022-10-03T14:38:10.119573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_breeds=np.unique(labels)\nboolean_values=[label==unique_breeds for label in labels]\nboolean_values[:2]","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:38:19.661147Z","iopub.execute_input":"2022-10-03T14:38:19.661724Z","iopub.status.idle":"2022-10-03T14:38:19.738071Z","shell.execute_reply.started":"2022-10-03T14:38:19.661678Z","shell.execute_reply":"2022-10-03T14:38:19.736405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#VALIDATION SPLIT","metadata":{}},{"cell_type":"code","source":"NUM_IMAGES=1000 #@param{type:\"slider\" ,min:1000,max:10000,step:1000}","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:38:37.528953Z","iopub.execute_input":"2022-10-03T14:38:37.529507Z","iopub.status.idle":"2022-10-03T14:38:37.535786Z","shell.execute_reply.started":"2022-10-03T14:38:37.529468Z","shell.execute_reply":"2022-10-03T14:38:37.534451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=filename\ny=boolean_values\nfrom sklearn.model_selection import train_test_split\nX_train,X_valid,y_train,y_valid=train_test_split(X[:NUM_IMAGES],y[:NUM_IMAGES],test_size=0.2,random_state=1)\nlen(y_valid)\nX_train[0]","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:38:50.019849Z","iopub.execute_input":"2022-10-03T14:38:50.0203Z","iopub.status.idle":"2022-10-03T14:38:50.424953Z","shell.execute_reply.started":"2022-10-03T14:38:50.02022Z","shell.execute_reply":"2022-10-03T14:38:50.423283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#TURNING IMAGES INTO TESNORS","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimage=plt.imread(filename[1])\nimage.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:39:25.885386Z","iopub.execute_input":"2022-10-03T14:39:25.886039Z","iopub.status.idle":"2022-10-03T14:39:25.911521Z","shell.execute_reply.started":"2022-10-03T14:39:25.885969Z","shell.execute_reply":"2022-10-03T14:39:25.910658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.constant(image)[:2]","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:39:31.792001Z","iopub.execute_input":"2022-10-03T14:39:31.792391Z","iopub.status.idle":"2022-10-03T14:39:31.850842Z","shell.execute_reply.started":"2022-10-03T14:39:31.792358Z","shell.execute_reply":"2022-10-03T14:39:31.84994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tensor1(fp):\n  image=plt.imread(fp)\n  image=tf.constant(image)\n  image=tf.image.convert_image_dtype(image,tf.float32)\n  image=tf.image.resize(image,(244,244))\n  return image\n\n#another moethod only using tensorflow\ndef tensor(fp):\n  image=tf.io.read_file(fp) #or we can use plt.imread(fp)\n  image=tf.image.decode_jpeg(image,3) #or we can use image=tf.constant(image)\n  image=tf.image.convert_image_dtype(image,tf.float32)\n  image=tf.image.resize(image,(224,224))\n  return image\n","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:39:40.028612Z","iopub.execute_input":"2022-10-03T14:39:40.029052Z","iopub.status.idle":"2022-10-03T14:39:40.0381Z","shell.execute_reply.started":"2022-10-03T14:39:40.029Z","shell.execute_reply":"2022-10-03T14:39:40.036638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#BATCHES ARE REQUIRED","metadata":{}},{"cell_type":"code","source":"def get_tuple(image_path,label):\n  image=tensor(image_path)\n  return image,label","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:39:55.569949Z","iopub.execute_input":"2022-10-03T14:39:55.571488Z","iopub.status.idle":"2022-10-03T14:39:55.577959Z","shell.execute_reply.started":"2022-10-03T14:39:55.571372Z","shell.execute_reply":"2022-10-03T14:39:55.576137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p=get_tuple(filename[100],y[100])\ntype(p)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:39:59.406983Z","iopub.execute_input":"2022-10-03T14:39:59.407536Z","iopub.status.idle":"2022-10-03T14:39:59.479472Z","shell.execute_reply.started":"2022-10-03T14:39:59.40746Z","shell.execute_reply":"2022-10-03T14:39:59.477921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#TURNING OUR DATA INTO BATCHES","metadata":{}},{"cell_type":"code","source":"BATCH_SIZE=32\n\ndef data_batches(X,y=None,batch_size=BATCH_SIZE,test_data=False,validation_data=False):\n  if test_data:\n    print(\"test_data\")\n    data=tf.data.Dataset.from_tensor_slices(tf.constant(X)) #we use this unbatch the data\n    data=data.map(tensor).batch(BATCH_SIZE)    # we use this to map and bacth data of batch_size=2\n    return data\n  elif validation_data:\n    print(\"Validation_data\")\n    data=tf.data.Dataset.from_tensor_slices((tf.constant(X),\n                                            tf.constant(y)))\n    data=data.map(get_tuple).batch(BATCH_SIZE)\n    return data\n  else:\n    print(\"train_data\")\n    data=tf.data.Dataset.from_tensor_slices((tf.constant(X),\n                                            tf.constant(y)))\n    data=data.shuffle(buffer_size=len(X))\n    data=data.map(get_tuple).batch(BATCH_SIZE)\n    return data\n    ","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:40:26.863804Z","iopub.execute_input":"2022-10-03T14:40:26.864446Z","iopub.status.idle":"2022-10-03T14:40:26.876361Z","shell.execute_reply.started":"2022-10-03T14:40:26.864365Z","shell.execute_reply":"2022-10-03T14:40:26.874869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data=data_batches(X_train,y_train)\nvalidation_data=data_batches(X_valid,y_valid,validation_data=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:40:32.285932Z","iopub.execute_input":"2022-10-03T14:40:32.286364Z","iopub.status.idle":"2022-10-03T14:40:32.476654Z","shell.execute_reply.started":"2022-10-03T14:40:32.286332Z","shell.execute_reply":"2022-10-03T14:40:32.474889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_data)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:40:37.588814Z","iopub.execute_input":"2022-10-03T14:40:37.58926Z","iopub.status.idle":"2022-10-03T14:40:37.599173Z","shell.execute_reply.started":"2022-10-03T14:40:37.589226Z","shell.execute_reply":"2022-10-03T14:40:37.597815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.element_spec,validation_data.element_spec","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:40:43.916811Z","iopub.execute_input":"2022-10-03T14:40:43.917236Z","iopub.status.idle":"2022-10-03T14:40:43.924983Z","shell.execute_reply.started":"2022-10-03T14:40:43.917199Z","shell.execute_reply":"2022-10-03T14:40:43.924112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images,train_labels=next(train_data.as_numpy_iterator())\ntrain_images,train_labels","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:41:06.578982Z","iopub.execute_input":"2022-10-03T14:41:06.579443Z","iopub.status.idle":"2022-10-03T14:41:06.811934Z","shell.execute_reply.started":"2022-10-03T14:41:06.579408Z","shell.execute_reply":"2022-10-03T14:41:06.810141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_images,validation_labels=next(validation_data.as_numpy_iterator())","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:41:20.165902Z","iopub.execute_input":"2022-10-03T14:41:20.16633Z","iopub.status.idle":"2022-10-03T14:41:20.443899Z","shell.execute_reply.started":"2022-10-03T14:41:20.166298Z","shell.execute_reply":"2022-10-03T14:41:20.442516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ndef visualize(images,labels):\n  plt.figure(figsize=(10,10))\n  for i in range(16):\n    ax=plt.subplot(4,4,i+1)\n    plt.imshow(images[i])\n    plt.title(unique_breeds[labels[i].argmax()])\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:41:26.946251Z","iopub.execute_input":"2022-10-03T14:41:26.9467Z","iopub.status.idle":"2022-10-03T14:41:26.953131Z","shell.execute_reply.started":"2022-10-03T14:41:26.946659Z","shell.execute_reply":"2022-10-03T14:41:26.952245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize(validation_images,validation_labels)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:41:34.009451Z","iopub.execute_input":"2022-10-03T14:41:34.009927Z","iopub.status.idle":"2022-10-03T14:41:35.231656Z","shell.execute_reply.started":"2022-10-03T14:41:34.009891Z","shell.execute_reply":"2022-10-03T14:41:35.229786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize(train_images,train_labels)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:41:45.18233Z","iopub.execute_input":"2022-10-03T14:41:45.1828Z","iopub.status.idle":"2022-10-03T14:41:46.735354Z","shell.execute_reply.started":"2022-10-03T14:41:45.18276Z","shell.execute_reply":"2022-10-03T14:41:46.733729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#BULDING A MODEL","metadata":{}},{"cell_type":"code","source":"INPUT_SIZE=[None,224,224,3]\nOUTPUT_SIZE=len(unique_breeds)\nmodel_url=\"https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/classification/5\"\ndef create_model(INPUT_SIZE=[None,224,224,3],OUTPUT_SIZE=len(unique_breeds),model_url=\"https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/classification/5\"):\n\n  #setting up the layers\n  model=tf.keras.Sequential([\n      hub.KerasLayer(model_url),\n      tf.keras.layers.Dense(units=OUTPUT_SIZE,\n                            activation='softmax')])\n  \n  #compile the model\n  model.compile(\n      loss=tf.keras.losses.CategoricalCrossentropy(),\n      optimizer=tf.keras.optimizers.Adam(),\n      metrics=['accuracy']\n  )\n\n  #bulid the model\n  model.build(INPUT_SIZE)\n\n  return model","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:42:17.301868Z","iopub.execute_input":"2022-10-03T14:42:17.303467Z","iopub.status.idle":"2022-10-03T14:42:17.312967Z","shell.execute_reply.started":"2022-10-03T14:42:17.303398Z","shell.execute_reply":"2022-10-03T14:42:17.311468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=create_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:42:22.68665Z","iopub.execute_input":"2022-10-03T14:42:22.687205Z","iopub.status.idle":"2022-10-03T14:42:29.042185Z","shell.execute_reply.started":"2022-10-03T14:42:22.687159Z","shell.execute_reply":"2022-10-03T14:42:29.040243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"CREATING CALL BACKS","metadata":{}},{"cell_type":"code","source":"%load_ext tensorboard","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:42:39.340753Z","iopub.execute_input":"2022-10-03T14:42:39.341248Z","iopub.status.idle":"2022-10-03T14:42:39.356744Z","shell.execute_reply.started":"2022-10-03T14:42:39.341205Z","shell.execute_reply":"2022-10-03T14:42:39.355717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import datetime\n \n# we tensorboard to check and save the progress\n\ndef call_Teansorboard():\n  #create a log folder in mydrive to store logs\n  log_dir=os.path.join(\"./logs\",\n                       datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\"))\n  return tf.keras.callbacks.TensorBoard(log_dir)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:48:01.478784Z","iopub.execute_input":"2022-10-03T14:48:01.479277Z","iopub.status.idle":"2022-10-03T14:48:01.488808Z","shell.execute_reply.started":"2022-10-03T14:48:01.479238Z","shell.execute_reply":"2022-10-03T14:48:01.487232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlystopping=tf.keras.callbacks.EarlyStopping(monitor='val_accuracy',\n                                               patience=3)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:48:02.338303Z","iopub.execute_input":"2022-10-03T14:48:02.338764Z","iopub.status.idle":"2022-10-03T14:48:02.345065Z","shell.execute_reply.started":"2022-10-03T14:48:02.338731Z","shell.execute_reply":"2022-10-03T14:48:02.343505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 100 #@param {type:\"slider\", min:10, max:100, step:10}","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:48:02.823101Z","iopub.execute_input":"2022-10-03T14:48:02.823566Z","iopub.status.idle":"2022-10-03T14:48:02.829145Z","shell.execute_reply.started":"2022-10-03T14:48:02.823531Z","shell.execute_reply":"2022-10-03T14:48:02.827993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_model():\n  #create a model\n  model=create_model()\n\n  #tensorboard\n  tensorboard=call_Teansorboard()\n\n  #fit model\n  model.fit(x=train_data,\n            epochs=epochs,\n            validation_data=validation_data,\n            validation_freq=1,\n            callbacks=[tensorboard,earlystopping])\n  return model","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:48:03.225579Z","iopub.execute_input":"2022-10-03T14:48:03.226089Z","iopub.status.idle":"2022-10-03T14:48:03.234511Z","shell.execute_reply.started":"2022-10-03T14:48:03.226038Z","shell.execute_reply":"2022-10-03T14:48:03.232576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=train_model()","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:48:03.702949Z","iopub.execute_input":"2022-10-03T14:48:03.703479Z","iopub.status.idle":"2022-10-03T14:52:25.78358Z","shell.execute_reply.started":"2022-10-03T14:48:03.703441Z","shell.execute_reply":"2022-10-03T14:52:25.78214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"EVALUATING A MODEL","metadata":{}},{"cell_type":"code","source":"%tensorboard --logdir drive/MyDrive/logs","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:52:47.687369Z","iopub.execute_input":"2022-10-03T14:52:47.687868Z","iopub.status.idle":"2022-10-03T14:52:53.288138Z","shell.execute_reply.started":"2022-10-03T14:52:47.687821Z","shell.execute_reply":"2022-10-03T14:52:53.286526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"PREDICTIONS","metadata":{}},{"cell_type":"code","source":"predictions=model.predict(validation_data,verbose=1)\nlen(predictions)# ---> these are probability of unique breeds","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:52:53.290495Z","iopub.execute_input":"2022-10-03T14:52:53.291459Z","iopub.status.idle":"2022-10-03T14:52:57.834002Z","shell.execute_reply.started":"2022-10-03T14:52:53.291412Z","shell.execute_reply":"2022-10-03T14:52:57.832508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_labels_predicted(prediction):\n  li=[]\n  for i in range(len(prediction)):\n    li.append(unique_breeds[prediction[i].argmax()])\n  return li\n\ndef get_label_predicted(predict):\n  return unique_breeds[predict.argmax()]\n\n\n\ndef unbatch(data):\n  data_images=[]\n  data_labels=[]\n  for images,labels in data.unbatch().as_numpy_iterator():\n    data_images.append(images)\n    data_labels.append(labels)\n  return data_images,get_labels_predicted(data_labels)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:52:57.835987Z","iopub.execute_input":"2022-10-03T14:52:57.836766Z","iopub.status.idle":"2022-10-03T14:52:57.846947Z","shell.execute_reply.started":"2022-10-03T14:52:57.83672Z","shell.execute_reply":"2022-10-03T14:52:57.844859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_images,valid_labels=unbatch(validation_data)\nlen(valid_images)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:52:57.850028Z","iopub.execute_input":"2022-10-03T14:52:57.850516Z","iopub.status.idle":"2022-10-03T14:52:58.521752Z","shell.execute_reply.started":"2022-10-03T14:52:57.850464Z","shell.execute_reply":"2022-10-03T14:52:58.520104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"PLOTTING THE PREDICTIONS","metadata":{}},{"cell_type":"code","source":"def plot(predic,val_labels,val_images,n=1):\n  plt.imshow(val_images[n])\n  plt.xticks([])\n  plt.yticks([])\n\n  if get_label_predicted(predic[n])==val_labels[n]:\n    color='green'\n  else:\n    color='red'\n  \n  plt.axis('off')\n  plt.title(\"{} {:2.0f}% {}\".format(get_label_predicted(predic[n]),max(predic[n])*100,val_labels[n]),color=color)\n\n\ndef predictions_wrong_count(valid_labels,predictions):\n  count=0\n  for i in range(len(predictions)):\n    if valid_labels[i]==get_label_predicted(predictions[i]):\n      count+=1\n  return count\n\n\n\ndef top_10_possibilities_plot(predic,val_labels,n=1):\n\n  top_10_possibilities_indexes=predic[n].argsort()[-10:][::-1]\n\n  top_10_possibilities_values=predic[n][top_10_possibilities_indexes]\n\n  top_10_possibilities_labels=unique_breeds[top_10_possibilities_indexes]\n\n  top_plot=plt.bar(np.arange(len(top_10_possibilities_labels)),top_10_possibilities_values,color='grey')\n\n  plt.xticks(np.arange(len(top_10_possibilities_labels)),top_10_possibilities_labels,rotation='vertical')\n\n  if np.isin(val_labels[n],top_10_possibilities_labels):\n    top_plot[np.argmax(top_10_possibilities_labels==val_labels[n])].set_color('green')\n  else:\n    pass\n  ","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:52:58.523442Z","iopub.execute_input":"2022-10-03T14:52:58.52382Z","iopub.status.idle":"2022-10-03T14:52:58.538675Z","shell.execute_reply.started":"2022-10-03T14:52:58.52378Z","shell.execute_reply":"2022-10-03T14:52:58.536844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(predictions,valid_labels,valid_images,191)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:53:04.037501Z","iopub.execute_input":"2022-10-03T14:53:04.038038Z","iopub.status.idle":"2022-10-03T14:53:04.212624Z","shell.execute_reply.started":"2022-10-03T14:53:04.037977Z","shell.execute_reply":"2022-10-03T14:53:04.210283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_wrong_count(valid_labels,predictions)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:53:04.549143Z","iopub.execute_input":"2022-10-03T14:53:04.549628Z","iopub.status.idle":"2022-10-03T14:53:04.559918Z","shell.execute_reply.started":"2022-10-03T14:53:04.549591Z","shell.execute_reply":"2022-10-03T14:53:04.558289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_10_possibilities_plot(predictions,valid_labels,131)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:53:14.607307Z","iopub.execute_input":"2022-10-03T14:53:14.607772Z","iopub.status.idle":"2022-10-03T14:53:14.882488Z","shell.execute_reply.started":"2022-10-03T14:53:14.607738Z","shell.execute_reply":"2022-10-03T14:53:14.881463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i_multiplier = 20\nnum_rows = 3\nnum_cols = 2\nnum_images = num_rows*num_cols\nplt.figure(figsize=(7.5*num_cols, 4*num_rows))\nfor i in range(num_images):\n  plt.subplot(num_rows, 2*num_cols, 2*i+1)\n  plot(predictions,\n            valid_labels,\n            valid_images,\n            i+i_multiplier)\n  plt.subplot(num_rows, 2*num_cols, 2*i+2)\n  top_10_possibilities_plot(predictions,\n                 valid_labels,\n                 i+i_multiplier)\nplt.tight_layout(h_pad=1.0)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:53:16.949319Z","iopub.execute_input":"2022-10-03T14:53:16.949752Z","iopub.status.idle":"2022-10-03T14:53:18.570989Z","shell.execute_reply.started":"2022-10-03T14:53:16.949715Z","shell.execute_reply":"2022-10-03T14:53:18.570094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"LOADING AND SAVING THE MODEL","metadata":{}},{"cell_type":"code","source":"def save_model(model, suffix=None):\n  # Create a model directory pathname with current time\n  modeldir = os.path.join(\"./\",\n                          datetime.datetime.now().strftime(\"%Y%m%d-%H%M%s\"))\n  model_path = modeldir + \"-\" + suffix + \".h5\" # save format of model\n  print(f\"Saving model to: {model_path}...\")\n  model.save(model_path)\n  return model_path","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:53:26.516472Z","iopub.execute_input":"2022-10-03T14:53:26.516923Z","iopub.status.idle":"2022-10-03T14:53:26.525361Z","shell.execute_reply.started":"2022-10-03T14:53:26.51689Z","shell.execute_reply":"2022-10-03T14:53:26.523745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_model(model_path):\n  print(f\"Loading saved model from: {model_path}\")\n  model = tf.keras.models.load_model(model_path, \n                                     custom_objects={\"KerasLayer\":hub.KerasLayer})\n  return model","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:53:34.887666Z","iopub.execute_input":"2022-10-03T14:53:34.888124Z","iopub.status.idle":"2022-10-03T14:53:34.89576Z","shell.execute_reply.started":"2022-10-03T14:53:34.888087Z","shell.execute_reply":"2022-10-03T14:53:34.894132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_model(model, suffix=\"1000-images-mobilenetv2-Adam\")","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:53:35.194987Z","iopub.execute_input":"2022-10-03T14:53:35.195464Z","iopub.status.idle":"2022-10-03T14:53:35.432852Z","shell.execute_reply.started":"2022-10-03T14:53:35.195429Z","shell.execute_reply":"2022-10-03T14:53:35.431111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_1000_image_model = load_model('./20221003-14531664808815-1000-images-mobilenetv2-Adam.h5')","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:54:09.06091Z","iopub.execute_input":"2022-10-03T14:54:09.06143Z","iopub.status.idle":"2022-10-03T14:54:11.338608Z","shell.execute_reply.started":"2022-10-03T14:54:09.061389Z","shell.execute_reply":"2022-10-03T14:54:11.336987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(validation_data)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:54:16.800444Z","iopub.execute_input":"2022-10-03T14:54:16.80091Z","iopub.status.idle":"2022-10-03T14:54:20.295268Z","shell.execute_reply.started":"2022-10-03T14:54:16.800873Z","shell.execute_reply":"2022-10-03T14:54:20.294064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_1000_image_model.evaluate(validation_data)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:54:22.380586Z","iopub.execute_input":"2022-10-03T14:54:22.381113Z","iopub.status.idle":"2022-10-03T14:54:27.166306Z","shell.execute_reply.started":"2022-10-03T14:54:22.381075Z","shell.execute_reply":"2022-10-03T14:54:27.165283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"BULIDING MODEL ON TOTAL DATA","metadata":{}},{"cell_type":"code","source":"final_train_data=data_batches(X,y)\nlen(final_train_data) #--> no.of bacthes  each batch contain 32 entries of data","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:54:31.883965Z","iopub.execute_input":"2022-10-03T14:54:31.884505Z","iopub.status.idle":"2022-10-03T14:54:31.980995Z","shell.execute_reply.started":"2022-10-03T14:54:31.884466Z","shell.execute_reply":"2022-10-03T14:54:31.979454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model=create_model()\nearlystopping=tf.keras.callbacks.EarlyStopping(monitor='accuracy',\n                                               patience=3)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:54:32.949207Z","iopub.execute_input":"2022-10-03T14:54:32.949642Z","iopub.status.idle":"2022-10-03T14:54:36.402753Z","shell.execute_reply.started":"2022-10-03T14:54:32.949604Z","shell.execute_reply":"2022-10-03T14:54:36.401153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_model(final_model, suffix=\"final-image-set-mobilenetv2-Adam\")","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:54:41.441228Z","iopub.execute_input":"2022-10-03T14:54:41.442565Z","iopub.status.idle":"2022-10-03T14:54:41.65265Z","shell.execute_reply.started":"2022-10-03T14:54:41.442509Z","shell.execute_reply":"2022-10-03T14:54:41.651067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"TEST DATA","metadata":{}},{"cell_type":"code","source":"import os\ntest_path = \"../input/dog-breed-identification/test/\"\ntest_file_name=[test_path+fname for fname in os.listdir(test_path)]\ntest_file_name[:10]","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:56:33.881036Z","iopub.execute_input":"2022-10-03T14:56:33.881403Z","iopub.status.idle":"2022-10-03T14:56:33.896945Z","shell.execute_reply.started":"2022-10-03T14:56:33.881373Z","shell.execute_reply":"2022-10-03T14:56:33.895617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_file_name)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T15:00:01.682537Z","iopub.execute_input":"2022-10-03T15:00:01.683153Z","iopub.status.idle":"2022-10-03T15:00:01.692615Z","shell.execute_reply.started":"2022-10-03T15:00:01.683098Z","shell.execute_reply":"2022-10-03T15:00:01.690968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data=data_batches(test_file_name,test_data=True)\ntest_data","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:56:36.149159Z","iopub.execute_input":"2022-10-03T14:56:36.149983Z","iopub.status.idle":"2022-10-03T14:56:36.198785Z","shell.execute_reply.started":"2022-10-03T14:56:36.149939Z","shell.execute_reply":"2022-10-03T14:56:36.197048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"PREDICTING FOR TEST_DATA","metadata":{}},{"cell_type":"code","source":"final_prediction1=final_model.predict(test_data,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T14:56:39.697308Z","iopub.execute_input":"2022-10-03T14:56:39.697728Z","iopub.status.idle":"2022-10-03T15:00:01.679422Z","shell.execute_reply.started":"2022-10-03T14:56:39.697694Z","shell.execute_reply":"2022-10-03T15:00:01.677842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_prediction1[:10]","metadata":{"execution":{"iopub.status.busy":"2022-10-03T15:00:06.743344Z","iopub.execute_input":"2022-10-03T15:00:06.743806Z","iopub.status.idle":"2022-10-03T15:00:06.756009Z","shell.execute_reply.started":"2022-10-03T15:00:06.74377Z","shell.execute_reply":"2022-10-03T15:00:06.754092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_csv=pd.DataFrame(columns=['id']+list(unique_breeds))","metadata":{"execution":{"iopub.status.busy":"2022-10-03T15:00:15.000435Z","iopub.execute_input":"2022-10-03T15:00:15.000864Z","iopub.status.idle":"2022-10-03T15:00:15.01569Z","shell.execute_reply.started":"2022-10-03T15:00:15.000828Z","shell.execute_reply":"2022-10-03T15:00:15.014254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids = [os.path.splitext(path)[0] for path in os.listdir(test_path)]","metadata":{"execution":{"iopub.status.busy":"2022-10-03T15:00:17.870292Z","iopub.execute_input":"2022-10-03T15:00:17.871377Z","iopub.status.idle":"2022-10-03T15:00:17.902591Z","shell.execute_reply.started":"2022-10-03T15:00:17.871335Z","shell.execute_reply":"2022-10-03T15:00:17.901577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_csv['id']=test_ids","metadata":{"execution":{"iopub.status.busy":"2022-10-03T15:00:19.69866Z","iopub.execute_input":"2022-10-03T15:00:19.699128Z","iopub.status.idle":"2022-10-03T15:00:19.722764Z","shell.execute_reply.started":"2022-10-03T15:00:19.699095Z","shell.execute_reply":"2022-10-03T15:00:19.721116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_csv[list(unique_breeds)]=final_prediction1","metadata":{"execution":{"iopub.status.busy":"2022-10-03T15:00:20.195786Z","iopub.execute_input":"2022-10-03T15:00:20.196218Z","iopub.status.idle":"2022-10-03T15:00:20.973742Z","shell.execute_reply.started":"2022-10-03T15:00:20.196184Z","shell.execute_reply":"2022-10-03T15:00:20.972666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-03T15:00:20.975685Z","iopub.execute_input":"2022-10-03T15:00:20.977441Z","iopub.status.idle":"2022-10-03T15:00:21.017171Z","shell.execute_reply.started":"2022-10-03T15:00:20.977377Z","shell.execute_reply":"2022-10-03T15:00:21.01565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(submission_csv)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T15:00:29.863798Z","iopub.execute_input":"2022-10-03T15:00:29.864256Z","iopub.status.idle":"2022-10-03T15:00:29.872662Z","shell.execute_reply.started":"2022-10-03T15:00:29.864221Z","shell.execute_reply":"2022-10-03T15:00:29.871306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_csv.to_csv(\"./submission_csv.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T15:00:46.42955Z","iopub.execute_input":"2022-10-03T15:00:46.430067Z","iopub.status.idle":"2022-10-03T15:00:47.92451Z","shell.execute_reply.started":"2022-10-03T15:00:46.430001Z","shell.execute_reply":"2022-10-03T15:00:47.92285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}