{"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":"\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sb\n  \nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\n  \nimport cv2\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras import layers\nfrom functools import partial\n  \nimport warnings\nwarnings.filterwarnings('ignore')\nAUTO = tf.data.experimental.AUTOTUNE","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-02T15:23:55.978445Z","iopub.execute_input":"2023-09-02T15:23:55.978915Z","iopub.status.idle":"2023-09-02T15:24:06.983200Z","shell.execute_reply.started":"2023-09-02T15:23:55.978874Z","shell.execute_reply":"2023-09-02T15:24:06.982079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:09.463172Z","iopub.execute_input":"2023-09-02T15:24:09.463911Z","iopub.status.idle":"2023-09-02T15:24:09.523068Z","shell.execute_reply.started":"2023-09-02T15:24:09.463876Z","shell.execute_reply":"2023-09-02T15:24:09.521846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:10.385917Z","iopub.execute_input":"2023-09-02T15:24:10.386588Z","iopub.status.idle":"2023-09-02T15:24:10.392864Z","shell.execute_reply.started":"2023-09-02T15:24:10.386553Z","shell.execute_reply":"2023-09-02T15:24:10.391934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndf['breed'].nunique()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:11.127502Z","iopub.execute_input":"2023-09-02T15:24:11.128549Z","iopub.status.idle":"2023-09-02T15:24:11.139674Z","shell.execute_reply.started":"2023-09-02T15:24:11.128502Z","shell.execute_reply":"2023-09-02T15:24:11.138688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\ndf['breed'].value_counts().plot.bar()\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:11.672293Z","iopub.execute_input":"2023-09-02T15:24:11.672658Z","iopub.status.idle":"2023-09-02T15:24:12.206903Z","shell.execute_reply.started":"2023-09-02T15:24:11.672627Z","shell.execute_reply":"2023-09-02T15:24:12.205908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndf['filepath'] = '/kaggle/input/dog-breed-identification/train/' + df['id'] + '.jpg'\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:12.577752Z","iopub.execute_input":"2023-09-02T15:24:12.578144Z","iopub.status.idle":"2023-09-02T15:24:12.597825Z","shell.execute_reply.started":"2023-09-02T15:24:12.578111Z","shell.execute_reply":"2023-09-02T15:24:12.596859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10)) \nfor i in range(12):\n    plt.subplot(4, 3, i + 1)\n    k = np.random.randint(0, len(df))\n    img = cv2.imread(df.loc[k, 'filepath'])\n    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    plt.imshow(img_rgb)\n    plt.title(df.loc[k, 'breed'])\n    plt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:13.497873Z","iopub.execute_input":"2023-09-02T15:24:13.498582Z","iopub.status.idle":"2023-09-02T15:24:15.211911Z","shell.execute_reply.started":"2023-09-02T15:24:13.498548Z","shell.execute_reply":"2023-09-02T15:24:15.210562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder()\ndf['breed'] = le.fit_transform(df['breed'])\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:15.213393Z","iopub.execute_input":"2023-09-02T15:24:15.213740Z","iopub.status.idle":"2023-09-02T15:24:15.232920Z","shell.execute_reply.started":"2023-09-02T15:24:15.213709Z","shell.execute_reply":"2023-09-02T15:24:15.231815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = df['filepath']\ntarget = df['breed']\n\nX_train, X_val, Y_train, Y_val= train_test_split(features, target, test_size=0.15, random_state=10)\n\nX_train.shape, X_val.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:15.545913Z","iopub.execute_input":"2023-09-02T15:24:15.546634Z","iopub.status.idle":"2023-09-02T15:24:15.558773Z","shell.execute_reply.started":"2023-09-02T15:24:15.546583Z","shell.execute_reply":"2023-09-02T15:24:15.557622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import albumentations as A\ntransform_train = A.Compose([\n    A.VerticalFlip(p=0.2),\n    A.HorizontalFlip(p=0.7),\n    A.CoarseDropout(p=0.5),\n    A.RandomGamma(p=0.5),\n    A.RandomBrightnessContrast(p=1)\n    \n])","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:16.114060Z","iopub.execute_input":"2023-09-02T15:24:16.115106Z","iopub.status.idle":"2023-09-02T15:24:16.948070Z","shell.execute_reply.started":"2023-09-02T15:24:16.115063Z","shell.execute_reply":"2023-09-02T15:24:16.946974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mg = cv2.imread(\"/kaggle/input/dog-breed-identification/train/00792e341f3c6eb33663e415d0715370.jpg\")\nplt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:16.951757Z","iopub.execute_input":"2023-09-02T15:24:16.952495Z","iopub.status.idle":"2023-09-02T15:24:17.345976Z","shell.execute_reply.started":"2023-09-02T15:24:16.952453Z","shell.execute_reply":"2023-09-02T15:24:17.344951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"augments = [A.VerticalFlip(p=1), A.HorizontalFlip(p=1), A.CoarseDropout(p=1), A.CLAHE(p=1)]\n\nplt.subplots(figsize=(10, 10))\nfor i, aug in enumerate(augments):\n    plt.subplot(2, 2, i+1)\n    aug_img = aug(image=img)['image']\n    plt.imshow(aug_img)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:17.534760Z","iopub.execute_input":"2023-09-02T15:24:17.537671Z","iopub.status.idle":"2023-09-02T15:24:18.756943Z","shell.execute_reply.started":"2023-09-02T15:24:17.537623Z","shell.execute_reply":"2023-09-02T15:24:18.755631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def aug_fn(img):\n    aug_data = transform_train(image=img)\n    aug_img = aug_data['image']\n    return aug_img\n\n@tf.function\ndef process_data(img, label):\n    aug_img = tf.numpy_function(aug_fn, \n                                [img], \n                                Tout=tf.float32)\n    return img, label\n\ndef decode_image(filepath, label=None):\n    img = tf.io.read_file(filepath)\n    img = tf.image.decode_jpeg(img)\n    img = tf.image.resize(img, [128, 128])\n    img = tf.cast(img, tf.float32)/255\n    \n    if label == None:\n        return img\n    \n    return img, tf.one_hot(indices=label,\n                          depth=120,\n                          dtype=tf.float32)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:18.758983Z","iopub.execute_input":"2023-09-02T15:24:18.759446Z","iopub.status.idle":"2023-09-02T15:24:18.771691Z","shell.execute_reply.started":"2023-09-02T15:24:18.759410Z","shell.execute_reply":"2023-09-02T15:24:18.770556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = (\n    tf.data.Dataset\n    .from_tensor_slices((X_train, Y_train))\n    .map(decode_image, num_parallel_calls=AUTO)\n    .map(partial(process_data), num_parallel_calls=AUTO)\n    .batch(32)\n    .prefetch(AUTO)\n\n)\n\nval_ds =(\n    tf.data.Dataset\n    .from_tensor_slices((X_val, Y_val))\n    .map(decode_image,  num_parallel_calls=AUTO)\n    .batch(32)\n    .prefetch(AUTO)\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:19.156599Z","iopub.execute_input":"2023-09-02T15:24:19.156993Z","iopub.status.idle":"2023-09-02T15:24:22.463990Z","shell.execute_reply.started":"2023-09-02T15:24:19.156959Z","shell.execute_reply":"2023-09-02T15:24:22.462911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img, label in train_ds.take(1):\n    print(img.shape, label.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:22.466291Z","iopub.execute_input":"2023-09-02T15:24:22.466774Z","iopub.status.idle":"2023-09-02T15:24:22.882121Z","shell.execute_reply.started":"2023-09-02T15:24:22.466738Z","shell.execute_reply":"2023-09-02T15:24:22.880730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import InceptionV3\n\npre_trained_model = InceptionV3(\n    input_shape=(128, 128, 3),\n    weights = 'imagenet',\n    include_top = False\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:22.884785Z","iopub.execute_input":"2023-09-02T15:24:22.885173Z","iopub.status.idle":"2023-09-02T15:24:26.762190Z","shell.execute_reply.started":"2023-09-02T15:24:22.885139Z","shell.execute_reply":"2023-09-02T15:24:26.761118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(pre_trained_model.layers)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:26.765958Z","iopub.execute_input":"2023-09-02T15:24:26.766345Z","iopub.status.idle":"2023-09-02T15:24:26.774334Z","shell.execute_reply.started":"2023-09-02T15:24:26.766315Z","shell.execute_reply":"2023-09-02T15:24:26.773336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in pre_trained_model.layers:\n    layer.trainable = False\n    \nlast_layer = pre_trained_model.get_layer('mixed7')\nprint('last layer output shape:' , last_layer.output_shape)\nlast_output = last_layer.output","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:26.775760Z","iopub.execute_input":"2023-09-02T15:24:26.776639Z","iopub.status.idle":"2023-09-02T15:24:26.809563Z","shell.execute_reply.started":"2023-09-02T15:24:26.776599Z","shell.execute_reply":"2023-09-02T15:24:26.807794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model Architecture\n\nx = tf.keras.layers.Flatten()(last_output)\nx = tf.keras.layers.Dense(256, activation='relu')(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Dense(256, activation='relu')(x)\nx = tf.keras.layers.Dropout(0.3)(x)\nx = tf.keras.layers.BatchNormalization()(x)\n\noutput = tf.keras.layers.Dense(120, activation='softmax')(x)\n\nmodel = keras.Model(pre_trained_model.input, output)\n\nmodel.compile(\n    optimizer = 'adam',\n    loss = keras.losses.CategoricalCrossentropy(from_logits=False),\n    metrics = ['AUC']\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:34.032836Z","iopub.execute_input":"2023-09-02T15:24:34.033451Z","iopub.status.idle":"2023-09-02T15:24:34.182207Z","shell.execute_reply.started":"2023-09-02T15:24:34.033405Z","shell.execute_reply":"2023-09-02T15:24:34.181082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau\n\nclass myCallback(tf.keras.callbacks.Callback):\n    def on_epoch_end(self, epoch, logs={}):\n        if logs.get('val_auc') > 0.99:\n            print('\\n validation accuracy has reached upto 90% so, stopping further training.')\n            self.model.stop_training = True\n            \nes = EarlyStopping(patience=3,\n                  monitor='val_auc',\n                  restore_best_weights=True)\n\nlr = ReduceLROnPlateau(monitor='val_loss',\n                      patience=2,\n                      factor=0.5,\n                      varbose=1)\n            ","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:35.232123Z","iopub.execute_input":"2023-09-02T15:24:35.233295Z","iopub.status.idle":"2023-09-02T15:24:35.240735Z","shell.execute_reply.started":"2023-09-02T15:24:35.233253Z","shell.execute_reply":"2023-09-02T15:24:35.239488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_ds,\n                  validation_data=val_ds,\n                  epochs=50,\n                  verbose=1,\n                  callbacks = [es, lr, myCallback()])","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:24:36.823880Z","iopub.execute_input":"2023-09-02T15:24:36.824593Z","iopub.status.idle":"2023-09-02T15:27:55.379160Z","shell.execute_reply.started":"2023-09-02T15:24:36.824557Z","shell.execute_reply":"2023-09-02T15:27:55.377958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nhistory_df = pd.DataFrame(history.history)\nhistory_df.loc[:, ['loss', 'val_loss']].plot()\nhistory_df.loc[:, ['auc', 'val_auc']].plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T15:29:56.249783Z","iopub.execute_input":"2023-09-02T15:29:56.250179Z","iopub.status.idle":"2023-09-02T15:29:56.856000Z","shell.execute_reply.started":"2023-09-02T15:29:56.250146Z","shell.execute_reply":"2023-09-02T15:29:56.854967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}