{"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":"markdown","source":"# DOG BREED CLASSIFICATION USING TRANSFER LEARNING","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport tensorflow_hub as hub\nfrom tqdm import tqdm\nimport os\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-31T19:12:24.031577Z","iopub.execute_input":"2023-03-31T19:12:24.032092Z","iopub.status.idle":"2023-03-31T19:12:32.908818Z","shell.execute_reply.started":"2023-03-31T19:12:24.032048Z","shell.execute_reply":"2023-03-31T19:12:32.907693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df = pd.read_csv(\"/kaggle/input/dog-breed-identification/labels.csv\")\nlabel_df","metadata":{"execution":{"iopub.status.busy":"2023-03-31T19:12:41.256204Z","iopub.execute_input":"2023-03-31T19:12:41.256907Z","iopub.status.idle":"2023-03-31T19:12:41.303265Z","shell.execute_reply.started":"2023-03-31T19:12:41.256868Z","shell.execute_reply":"2023-03-31T19:12:41.302170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## PREPARING IMAGES AND LABELS","metadata":{}},{"cell_type":"code","source":"labels = label_df[\"breed\"]\nunique_breeds = np.unique(labels)\n# ONE HOT ENCODING LABELS\nboolean_labels = [label == unique_breeds for label in labels]\n\n# MAKING A LIST OF ALL FILE NAMES\nfilenames=list(\"/kaggle/input/dog-breed-identification/train/\"+ label_df[\"id\"]+\".jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-03-31T19:12:43.969131Z","iopub.execute_input":"2023-03-31T19:12:43.969492Z","iopub.status.idle":"2023-03-31T19:12:44.031186Z","shell.execute_reply.started":"2023-03-31T19:12:43.969460Z","shell.execute_reply":"2023-03-31T19:12:44.030219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUM_IMAGES = len(label_df)\n\n# TRAIN TEST SPLIT\nx=filenames\ny=boolean_labels\n\nfrom sklearn.model_selection import train_test_split\nxtrain,xval,ytrain,yval=train_test_split(x[:NUM_IMAGES],y[:NUM_IMAGES],\n                                         test_size=0.15,random_state=42)\nlen(xtrain),len(ytrain),len(xval),len(yval)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T19:42:31.340807Z","iopub.execute_input":"2023-03-31T19:42:31.341193Z","iopub.status.idle":"2023-03-31T19:42:31.373124Z","shell.execute_reply.started":"2023-03-31T19:42:31.341160Z","shell.execute_reply":"2023-03-31T19:42:31.371736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nfig, ax = plt.subplots(2,2,figsize = (10,10))\nfor i in range(0,2):\n    ax[i][0].imshow(Image.open(xtrain[i]))\n    ax[i][0].set_title(f\"{unique_breeds[np.argmax(ytrain[i])]}\")\n    ax[i][1].imshow(Image.open(xtrain[i+3]))\n    ax[i][1].set_title(f\"{unique_breeds[np.argmax(ytrain[i+3])]}\")","metadata":{"execution":{"iopub.status.busy":"2023-03-31T20:12:39.550386Z","iopub.execute_input":"2023-03-31T20:12:39.550774Z","iopub.status.idle":"2023-03-31T20:12:40.478505Z","shell.execute_reply.started":"2023-03-31T20:12:39.550739Z","shell.execute_reply":"2023-03-31T20:12:40.477626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE=224\n# A FUNCTION TO PROCESS AN IMAGE AND CONVERT IT INTO A TENSOR\ndef image_process(image_path):\n    #Reading an image\n    image=tf.io.read_file(image_path)\n    #Turning to tensors\n    image=tf.image.decode_jpeg(image,channels=3)\n    #Convert the colour value to 0-1\n    image=tf.image.convert_image_dtype(image,tf.float32)\n    #Resize the image\n    image=tf.image.resize(image,size=[IMG_SIZE,IMG_SIZE])\n    return image\n\ndef get_labels(image_path,label):\n    image = image_process(image_path)\n    return image,label","metadata":{"execution":{"iopub.status.busy":"2023-03-31T19:42:34.263563Z","iopub.execute_input":"2023-03-31T19:42:34.264363Z","iopub.status.idle":"2023-03-31T19:42:34.271593Z","shell.execute_reply.started":"2023-03-31T19:42:34.264321Z","shell.execute_reply":"2023-03-31T19:42:34.270323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# CREATING DATA BATCHES\nBATCH_SIZE = 32\ndef create_data_batches(x,y=None,batch_size = BATCH_SIZE,valid_data = False, test_data = False):\n    if test_data:\n        print(\"CREATING TEST BATCHES ...\")\n        data = tf.data.Dataset.from_tensor_slices((tf.constant(x)))\n        data_batch = data.map(image_process).batch(BATCH_SIZE)\n        return data_batch\n\n    elif valid_data:\n        print(\"CREATING VALIDATION BATCHES ...\")\n        data = tf.data.Dataset.from_tensor_slices((tf.constant(x),tf.constant(y)))\n        data_batch = data.map(get_labels).batch(BATCH_SIZE)\n        return data_batch\n\n    else :\n        print(\"CREATING TRAINING BATCHES ...\")\n        data = tf.data.Dataset.from_tensor_slices((tf.constant(x),tf.constant(y)))\n        data = data.shuffle(buffer_size = len(x))\n        data_batch = data.map(get_labels).batch(BATCH_SIZE)\n        return data_batch","metadata":{"execution":{"iopub.status.busy":"2023-03-31T19:42:36.081150Z","iopub.execute_input":"2023-03-31T19:42:36.082099Z","iopub.status.idle":"2023-03-31T19:42:36.090291Z","shell.execute_reply.started":"2023-03-31T19:42:36.082047Z","shell.execute_reply":"2023-03-31T19:42:36.089081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = create_data_batches(xtrain,y=ytrain)\nvalid_data = create_data_batches(xval,y=yval,valid_data=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T19:42:37.573906Z","iopub.execute_input":"2023-03-31T19:42:37.574916Z","iopub.status.idle":"2023-03-31T19:42:37.672633Z","shell.execute_reply.started":"2023-03-31T19:42:37.574870Z","shell.execute_reply":"2023-03-31T19:42:37.671474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT_SHAPE = [None,IMG_SIZE,IMG_SIZE,3]\nOUTPUT_SHAPE = len(unique_breeds)\nMODEL_URL = \"https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/classification/5\"","metadata":{"execution":{"iopub.status.busy":"2023-03-31T19:42:39.910790Z","iopub.execute_input":"2023-03-31T19:42:39.911491Z","iopub.status.idle":"2023-03-31T19:42:39.916914Z","shell.execute_reply.started":"2023-03-31T19:42:39.911455Z","shell.execute_reply":"2023-03-31T19:42:39.915738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## DEFINING MODEL","metadata":{}},{"cell_type":"code","source":"def create_model(input_shape = INPUT_SHAPE,output_shape=OUTPUT_SHAPE,model_url = MODEL_URL):\n    print(\"BUILDING MODEL WITH \",MODEL_URL)\n\n    # SETUP MODEL LAYERS\n    model = tf.keras.Sequential([\n      hub.KerasLayer(MODEL_URL),\n      tf.keras.layers.Dense(units=OUTPUT_SHAPE,activation=\"softmax\")\n    ])\n\n    # COMPILING THE MODEL\n    model.compile(\n      loss = tf.keras.losses.CategoricalCrossentropy(),\n      optimizer = tf.keras.optimizers.Adam(),\n      metrics = [\"accuracy\"]\n    )\n\n    #BUILD THE MODEL\n    model.build(INPUT_SHAPE)\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-03-31T19:53:41.676888Z","iopub.execute_input":"2023-03-31T19:53:41.677276Z","iopub.status.idle":"2023-03-31T19:53:41.684122Z","shell.execute_reply.started":"2023-03-31T19:53:41.677245Z","shell.execute_reply":"2023-03-31T19:53:41.683060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T19:59:58.698511Z","iopub.execute_input":"2023-03-31T19:59:58.699199Z","iopub.status.idle":"2023-03-31T20:00:00.317424Z","shell.execute_reply.started":"2023-03-31T19:59:58.699159Z","shell.execute_reply":"2023-03-31T20:00:00.316322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## MODEL TRAINING","metadata":{}},{"cell_type":"code","source":"checkpointer = tf.keras.callbacks.ModelCheckpoint('/kaggle/working/DogBreedClassifier.h5', verbose=1, save_best_only=True)\n\nresults = model.fit(train_data,\n                    validation_data = valid_data, \n                    epochs=10,\n                    callbacks=checkpointer\n                    )","metadata":{"execution":{"iopub.status.busy":"2023-03-31T20:00:00.491495Z","iopub.execute_input":"2023-03-31T20:00:00.492125Z","iopub.status.idle":"2023-03-31T20:05:51.397747Z","shell.execute_reply.started":"2023-03-31T20:00:00.492082Z","shell.execute_reply":"2023-03-31T20:05:51.396456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_model = tf.keras.models.load_model(\"/kaggle/working/DogBreedClassifier.h5\", custom_objects = {\"KerasLayer\":hub.KerasLayer})\npred = loaded_model.predict(valid_data,verbose =1 )\n","metadata":{"execution":{"iopub.status.busy":"2023-03-31T20:05:51.400345Z","iopub.execute_input":"2023-03-31T20:05:51.400839Z","iopub.status.idle":"2023-03-31T20:05:58.206043Z","shell.execute_reply.started":"2023-03-31T20:05:51.400797Z","shell.execute_reply":"2023-03-31T20:05:58.204994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def unbatch(data):\n    images =[]\n    labels =[]\n    for image,label in data.unbatch().as_numpy_iterator():\n        images.append(image)\n        labels.append(label)\n    return images,labels\n\nval_images,val_labels = unbatch(valid_data)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T20:05:58.207665Z","iopub.execute_input":"2023-03-31T20:05:58.208027Z","iopub.status.idle":"2023-03-31T20:06:01.740498Z","shell.execute_reply.started":"2023-03-31T20:05:58.207997Z","shell.execute_reply":"2023-03-31T20:06:01.739445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## VISUALISING OUR PREDICTIONS\n","metadata":{}},{"cell_type":"code","source":"choice = np.random.randint(0,600,4)\nfig, ax = plt.subplots(2,2, figsize = (10, 10))\nax[0][0].imshow(val_images[choice[0]])\nax[0][0].set_title(f\"TRUE: {unique_breeds[np.argmax(val_labels[choice[0]])]}, PRED: {unique_breeds[np.argmax(pred[choice[0]])]}\")\nax[0][1].imshow(val_images[choice[1]])\nax[0][1].set_title(f\"TRUE: {unique_breeds[np.argmax(val_labels[choice[1]])]}, PRED: {unique_breeds[np.argmax(pred[choice[1]])]}\")\n\nax[1][0].imshow(val_images[choice[2]])\nax[1][0].set_title(f\"TRUE: {unique_breeds[np.argmax(val_labels[choice[2]])]}, PRED: {unique_breeds[np.argmax(pred[choice[2]])]}\")\nax[1][1].imshow(val_images[choice[3]])\nax[1][1].set_title(f\"TRUE: {unique_breeds[np.argmax(val_labels[choice[3]])]}, PRED: {unique_breeds[np.argmax(pred[choice[3]])]}\")","metadata":{"execution":{"iopub.status.busy":"2023-03-31T20:06:56.032301Z","iopub.execute_input":"2023-03-31T20:06:56.033331Z","iopub.status.idle":"2023-03-31T20:06:56.990420Z","shell.execute_reply.started":"2023-03-31T20:06:56.033280Z","shell.execute_reply":"2023-03-31T20:06:56.986267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}