{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"filepath = '../input/dog-breed-identification/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#problem\n## identify breed of the dog , given image of dog\n\n# evaluation\n# predict probablities of each dog breed of each test image\n\n# features\n# Some information about data :\n# we are dealing with unstructured data (images) , so its probably best we use deep learning / transfer learning\n# 120 breeds - 120 classes\n# 10000+ images in training set\n# 10000+ images in test set\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nprint('Tensorflow version:', tf.__version__)\nimport tensorflow_hub as hub\nprint('Tensorflow hub version:', hub.__version__)\n\n# check for gpu availability\n\nprint(\"GPU available\" if tf.config.list_physical_devices(\"GPU\") else \"Not available\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nlabelsdf = pd.read_csv(filepath+'labels.csv')\nprint(labelsdf.describe())\nprint(labelsdf.head())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labelsdf.breed.value_counts().plot.bar(figsize=(20,10));","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## view an image\nfrom IPython.display import Image\nImage(filepath+'train/09839ef1c5a5a5b3acb61c4093cab07f.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## Getting images and their labels\n\nfilenames = [filepath+\"train/\"+fname+\".jpg\" for fname in labelsdf['id']]\nfilenames","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(filenames)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## check number of file names matches with actual images files\n\nimport os\nif len(os.listdir(filepath+\"train/\"))  == len(filenames):\n    print(\"file names match actual\")\nelse:\n    print(\"doesn't match\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Image(filenames[9000])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nlabels = labelsdf['breed']\nlabels = np.array(labels)\nlabels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"uniquebreeds = np.unique(labels)\nuniquebreeds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# convert label to boolean arr\n\nprint(labels[0])\nprint(labels[0]==uniquebreeds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bool_labels = [label == uniquebreeds for label in labels]\nlen(bool_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## turning bool labels into int\n\nprint(labels[0])\nprint(np.where(labels[0]==uniquebreeds))  ## index where label occurs\nprint(bool_labels[0].argmax()) # index where label occurs in boolean array\nprint(bool_labels[0].astype(int))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create validation set\n\nx = filenames\ny = bool_labels\n\nnumimages = 1000\n\nfrom sklearn.model_selection import train_test_split\n\nxtrain, xval, ytrain, yval = train_test_split(x[:numimages], y[:numimages], test_size=0.2, random_state=42)\n\nprint(len(xtrain), len(xval), len(ytrain), len(yval))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## preprocess images\n## turning images into tensors\n\n#convert image to numpy array\nfrom matplotlib.pyplot import imread\nimage = imread(filenames[42])\nprint(image.shape)\ntf.constant(image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"imgsize=224\ndef processimage(imagepath, imgsize=imgsize):\n    \"\"\"\n    Take imagepath as input and turn image into tensor\n    \"\"\"\n    # Read in an image\n    image = tf.io.read_file(imagepath)\n    # turn jpeg image into numerical tensor with 3 channels (RGB)\n    image = tf.image.decode_jpeg(image, channels=3)\n    #convert color channels values from 0-255 to 0-1 values\n    image = tf.image.convert_image_dtype(image, tf.float32)\n    # resize the image to desired value (224, 224)\n    image = tf.image.resize(image, size=[imgsize, imgsize])\n    \n    return image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## turning data into batches\n## we use batch size=32\n## tensor tuples (image, label)\n\ndef get_image_label(image_path, label):\n    image = processimage(image_path)\n    return image, label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# turn data into batches\nbatchsize=32\n\ndef create_data_batches(x, y=None, batchsize=batchsize, valid_data=False, test_data=False):\n    \n    ## shuffles training data, but does not shuffle validation data\n    \n    if test_data:\n        print(\"creating test data batches :\")\n        data = tf.data.Dataset.from_tensor_slices((tf.constant(x)))\n        databatch = data.map(processimage).batch(batchsize)\n        return databatch\n    elif valid_data:\n        print(\"creating validation data batches:\")\n        data = tf.data.Dataset.from_tensor_slices((tf.constant(x), tf.constant(y)))\n        databatch = data.map(get_image_label).batch(batchsize)\n        return databatch\n    else:\n        print(\"creating train data batches:\")\n        #convert to tensors\n        data=tf.data.Dataset.from_tensor_slices((tf.constant(x), tf.constant(y)))\n        \n        #shuffling\n        data=data.shuffle(buffer_size=len(x))\n        data = data.map(get_image_label)\n        \n        #batches\n        databatch=data.batch(batchsize)\n        return databatch\n    \n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data = create_data_batches(xtrain, ytrain)\nval_data = create_data_batches(xval, yval, valid_data=True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(train_data.element_spec) \nprint(val_data.element_spec)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## data visualization\nimport matplotlib.pyplot as plt\ndef show25images(images, labels):\n    plt.figure(figsize=(10,10))\n    for i in range(25):\n        #create subplots\n        ax=plt.subplot(5, 5, i+1)\n        plt.imshow(images[i])\n        plt.title(uniquebreeds[labels[i].argmax()])\n        plt.axis('off')\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images, train_labels = next(train_data.as_numpy_iterator())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(train_images), len(train_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"uniquebreeds[y[4].argmax()]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show25images(train_images, train_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"val_images, val_labels = next(val_data.as_numpy_iterator())\nshow25images(val_images, val_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# define inputs and outputs for the model\n\nimgsize=224\n\ninputshape = [None, imgsize, imgsize, 3]  # batch, height, width, color channels\noutputshape = len(uniquebreeds)\n\n## setup model url from tensorflow hub\n\nmodel_url = \"https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/classification/4\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## keral model\n\ndef create_model(inputshape=inputshape, outputshape=outputshape, modelurl=model_url):\n    print(\"Building model with : \", modelurl)\n    \n    model=tf.keras.Sequential([hub.KerasLayer(modelurl), \n                               tf.keras.layers.Dense(units=outputshape, activation='softmax')\n                              ])\n    \n    model.compile(loss=tf.keras.losses.CategoricalCrossentropy(),\n                 optimizer=tf.keras.optimizers.Adam(),\n                 metrics=['accuracy'])\n    \n    model.build(inputshape)\n    \n    return model\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = create_model()\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## callbacks\n\n#load tensorboard\n\n%load_ext tensorboard\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import datetime\n\n# function to build tensor board callback\n\ndef create_tensorboard_callback():\n    ## create log dir for tensorboard logs\n    logdir=os.path.join(\"/kaggle/working/logs\", datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\"))\n    \n    return tf.keras.callbacks.TensorBoard(logdir)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## early stopping callback\n\nearly_stopping = tf.keras.callbacks.EarlyStopping(monitor=\"val_accuracy\", patience=3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"numepochs=100 ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def train_model():\n    model = create_model()\n    tensorboard = create_tensorboard_callback()\n    model.fit(x=train_data, epochs=numepochs, validation_data=val_data, validation_freq=1, \n              callbacks=[tensorboard, early_stopping])\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = train_model()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## prevent overfitting\n\n## tensorboard magic function\n\n%tensorboard --logdir /kaggle/working/logs","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = model.predict(val_data, verbose=1)\npredictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.sum(predictions[0]) , np.sum(predictions[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.max(predictions[0]),  np.max(predictions[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"index=42\nprint(f\"max :\", np.max(predictions[index]))\nprint(f\"sum :\", np.sum(predictions[index]))\nprint(f\"max index: \", np.argmax(predictions[index]))\nprint(f\"prediction label : \", uniquebreeds[np.argmax(predictions[index])])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_prediction_label(predprob):\n    return uniquebreeds[np.argmax(predprob)]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_label=get_prediction_label(predictions[9])\npred_label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"val_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## unbatch dataset\n\ndef unbatchify(data):\n    images_ = []\n    labels_ = []\n\n    for image, label in data.unbatch().as_numpy_iterator():\n        images_.append(image)\n        labels_.append(uniquebreeds[np.argmax(label)])\n    return images_, labels_","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"valimg, vallab = unbatchify(val_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"get_prediction_label(vallab[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plotpred(predprob, labels, images, n=1):\n    pred_prob, truelabel, image = predprob[n], labels[n], images[n]\n    predlabel = get_prediction_label(pred_prob)\n    plt.imshow(image)\n    plt.xticks([])\n    plt.yticks([])\n    if (predlabel == truelabel):\n        color=\"green\"\n    else:\n        color=\"red\"\n        \n    plt.title(f\"{predlabel} {np.max(pred_prob)*100:2.0f}% {truelabel}\" , color=color)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plotpred(predprob=predictions, labels=vallab, images=valimg, n=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plotpredconf(predprob, labels, n=1):\n    pred_prob , truelabel = predprob[n], labels[n]\n    pred_label = get_prediction_label(pred_prob)\n    # top 10 prediction indexes\n    top_10_pred_ind = pred_prob.argsort()[-10:][::-1]\n    top_10_pred_values = pred_prob[top_10_pred_ind]\n    top10predlabels = uniquebreeds[top_10_pred_ind]\n    \n    topplot = plt.bar(np.arange(len(top10predlabels)), top_10_pred_values, color=\"grey\")\n    plt.xticks(np.arange(len(top10predlabels)), labels=top10predlabels, rotation='vertical')\n    \n    if np.isin(truelabel,top10predlabels):\n        print(\"yes\")\n        topplot[np.argmax(top10predlabels==truelabel)].set_color('green')\n    else:\n        pass\n    \n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plotpredconf(predictions, vallab,99)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions[0][predictions[0].argsort()[-10:][::-1]]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions[0].max()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"imult=0\nrows=3\ncols=2\nnumimg = rows * cols\nplt.figure(figsize=(10*cols, 5*rows))\nfor i in range(numimg):\n    plt.subplot(rows, 2*cols, 2*i+1)\n    plotpred(predictions, vallab, valimg, n=i+imult)\n    plt.subplot(rows, 2*cols, 2*i+2)\n    plotpredconf(predictions, vallab, n=i+imult)\nplt.tight_layout(h_pad=1.0)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}