{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.8.17","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":6799,"databundleVersionId":4225553,"sourceType":"competition"}],"dockerImageVersionId":30529,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import math, re, os\nimport tensorflow as tf\nimport numpy as np\nfrom matplotlib import pyplot as plt\nfrom kaggle_datasets import KaggleDatasets\nfrom sklearn.metrics import f1_score, precision_score, recall_score, confusion_matrix\nimport matplotlib.image as mpimg\nimport os     \nimport shutil  \nfrom IPython.display import clear_output\n\n\n\n!pip install cleverhans\nfrom cleverhans.tf2.utils import optimize_linear,clip_eta\n\n# detect and init the TPU\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()\n\n# instantiate a distribution strategy\ntpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)\n\nprint(\"Tensorflow version \" + tf.__version__)\n\n\nprint(\"REPLICAS: \", tpu_strategy.num_replicas_in_sync)\nprint('DEVICES AVAILABLE: {}'.format(tpu_strategy.num_replicas_in_sync))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-22T20:49:29.405522Z","iopub.execute_input":"2023-12-22T20:49:29.406396Z","iopub.status.idle":"2023-12-22T20:49:37.313879Z","shell.execute_reply.started":"2023-12-22T20:49:29.406362Z","shell.execute_reply":"2023-12-22T20:49:37.312978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"\n# import pandas as pd\n\n# mapping = pd.read_csv(\"/kaggle/input/imagenet-object-localization-challenge/LOC_synset_mapping.txt\",header = None, sep = ' ', names=[\"folder\", \"label1\",\"label2\",\"label3\",\"label4\",\"label5\",\"label6\"], on_bad_lines='skip')\n\n# mapping['label1'] = mapping['label1']+' ' +mapping['label2']\n\n# print(mapping)\n\nmapping = {}\nimages = []\n\nwith open('/kaggle/input/imagenet-object-localization-challenge/LOC_synset_mapping.txt', 'r') as f:\n    lines = f.readlines()\n    for line in lines:\n        mapping[line.split()[0]] = ' '.join(line.split(',')[0].split()[1:])\n        \nwith open('/kaggle/input/imagenet-object-localization-challenge/ILSVRC/ImageSets/CLS-LOC/train_cls.txt', 'r') as f:\n    lines = f.readlines()\n    for line in lines:\n        images.append(line.split()[0])\n\nbasedir = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/\"\n\nplt.imshow(mpimg.imread(basedir+images[500939]+\".JPEG\"))\nprint(mapping[images[500939].split('/')[0]])\n","metadata":{"execution":{"iopub.status.busy":"2023-12-22T20:49:37.315668Z","iopub.execute_input":"2023-12-22T20:49:37.315989Z","iopub.status.idle":"2023-12-22T20:49:38.620401Z","shell.execute_reply.started":"2023-12-22T20:49:37.315961Z","shell.execute_reply":"2023-12-22T20:49:38.619606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basedir = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/\"\n\nlist_subdir = [ f.path for f in os.scandir(basedir) if f.is_dir() ]\nlist_subdir = sorted(list_subdir)\nprint(list_subdir[:21])","metadata":{"execution":{"iopub.status.busy":"2023-12-22T20:49:38.621568Z","iopub.execute_input":"2023-12-22T20:49:38.621842Z","iopub.status.idle":"2023-12-22T20:49:38.628181Z","shell.execute_reply.started":"2023-12-22T20:49:38.621819Z","shell.execute_reply":"2023-12-22T20:49:38.627378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nshutil.rmtree(\"/kaggle/working/data\")\nos.mkdir(\"/kaggle/working/data\")\nfor i in list_subdir[:21]:\n    name_file = i.split('/')[-1]\n    #os.mkdir(\"/kaggle/working/data/\"+name_file)\n    shutil.copytree(i,\"/kaggle/working/data/\"+name_file)\n#shutil.rmtree(\"/kaggle/working/data\")","metadata":{"execution":{"iopub.status.busy":"2023-12-22T20:49:38.629205Z","iopub.execute_input":"2023-12-22T20:49:38.629458Z","iopub.status.idle":"2023-12-22T20:51:50.656085Z","shell.execute_reply.started":"2023-12-22T20:49:38.629436Z","shell.execute_reply":"2023-12-22T20:51:50.654756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = \"/kaggle/working/data\"\n\ntrain_set = tf.keras.utils.image_dataset_from_directory(data_dir,image_size=(224, 224),batch_size=16 * tpu_strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2023-12-22T20:51:50.658847Z","iopub.execute_input":"2023-12-22T20:51:50.659182Z","iopub.status.idle":"2023-12-22T20:51:52.089042Z","shell.execute_reply.started":"2023-12-22T20:51:50.659155Z","shell.execute_reply":"2023-12-22T20:51:52.087757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_set)\nimages, labels = next(iter(train_set)) # get one batch\n\nplt.figure(figsize=(10, 10))\nfor i in range(9):\n    ax = plt.subplot(3, 3, i + 1)\n    plt.imshow(images[i].numpy().astype(\"uint8\"))\n    plt.title(mapping[train_set.class_names[labels[i]]])\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-12-22T20:51:52.090186Z","iopub.execute_input":"2023-12-22T20:51:52.09046Z","iopub.status.idle":"2023-12-22T20:51:53.525523Z","shell.execute_reply.started":"2023-12-22T20:51:52.090436Z","shell.execute_reply":"2023-12-22T20:51:53.524398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set_preprocessed = train_set.map(lambda x, y : (tf.keras.applications.mobilenet.preprocess_input(x), y) )","metadata":{"execution":{"iopub.status.busy":"2023-12-22T20:51:53.526768Z","iopub.execute_input":"2023-12-22T20:51:53.527073Z","iopub.status.idle":"2023-12-22T20:51:53.561647Z","shell.execute_reply.started":"2023-12-22T20:51:53.527047Z","shell.execute_reply":"2023-12-22T20:51:53.560727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nclass CustomModel(tf.keras.Model):\n    def __init__(self, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n        self.loss_tracker = tf.keras.metrics.SparseCategoricalCrossentropy(name=\"loss\")\n#         self.mae_metric = tf.keras.metrics.MeanAbsoluteError(name=\"mae\")\n        self.sparscat_metric=tf.keras.metrics.SparseCategoricalAccuracy(name='sparse_categorical_accuracy')\n    \n    def train_step(self, data):\n        # Unpack the data. Its structure depends on your model and\n        # on what you pass to `fit()`.\n        x, y = data\n        print('.')\n        true_x=x\n        eps=0.3\n        rand_minmax = eps\n        norm=np.inf\n        nb_iter=5\n        eps_iter=0.08\n        eta = tf.zeros_like(x)\n\n        # Clip eta\n        eta = clip_eta(eta, norm, eps)\n        x = true_x + eta\n\n        i = 0\n        result={}\n\n        while i < nb_iter:\n            with tf.GradientTape(persistent=True) as tape:\n                tape.watch(x)\n                predictions = self(x,training=True)\n                loss=self.compute_loss(y=y,y_pred=predictions)\n\n            gradients = tape.gradient(loss, self.trainable_variables)\n            adv_grad = tape.gradient(loss, x)\n            del tape\n            self.optimizer.apply_gradients(zip(gradients, self.trainable_variables))\n            print(y)\n            for metric in self.metrics:\n                metric.update_state(y, predictions)\n            # Return a dict mapping metric names to current value\n            result= {m.name: m.result() for m in self.metrics}\n\n            x = self.fast_gradient_method(\n                grad=adv_grad,\n                x=x,\n                eps=eps_iter,\n                norm=norm,\n                y=y\n            )\n\n            # Clipping perturbation eta to norm norm ball\n            eta = x - true_x\n            eta = clip_eta(eta, norm, eps)\n            x = true_x + eta\n\n\n            i += 1\n        return result\n\n    def fast_gradient_method(self, grad, x, eps, norm, y,):\n        # cast to tensor if provided as numpy array\n        x = tf.cast(x, tf.float32)\n\n        optimal_perturbation = optimize_linear(grad, eps, norm)\n        # Add perturbation to original example to obtain adversarial example\n        adv_x = x + optimal_perturbation\n\n        return adv_x\n    @property\n    def metrics(self):\n        # We list our `Metric` objects here so that `reset_states()` can be\n        # called automatically at the start of each epoch\n        # or at the start of `evaluate()`.\n        # If you don't implement this property, you have to call\n        # `reset_states()` yourself at the time of your choosing.\n        return [ self.loss_tracker,self.sparscat_metric]\n    def test_step(self, data):\n        # Unpack the data\n        x, y = data\n        # Compute predictions\n        y_pred = self(x, training=False)\n        # Updates the metrics tracking the loss\n        self.compute_loss(y=y, y_pred=y_pred)\n        # Update the metrics.\n        for metric in self.metrics:\n                metric.update_state(y, y_pred)\n        # Return a dict mapping metric names to current value.\n        # Note that it will include the loss (tracked in self.metrics).\n        return {m.name: m.result() for m in self.metrics}","metadata":{"execution":{"iopub.status.busy":"2023-12-22T20:51:53.562791Z","iopub.execute_input":"2023-12-22T20:51:53.563042Z","iopub.status.idle":"2023-12-22T20:51:53.576572Z","shell.execute_reply.started":"2023-12-22T20:51:53.56302Z","shell.execute_reply":"2023-12-22T20:51:53.575717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nclass PlotLearning(tf.keras.callbacks.Callback):\n    \"\"\"\n    Callback to plot the learning curves of the model during training.\n    \"\"\"\n    def on_train_begin(self, logs={}):\n        self.metrics = {}\n        for metric in logs:\n            self.metrics[metric] = []\n\n    def on_train_batch_end(self, batch, logs={}):\n        # Storing metrics\n        for metric in logs:\n            if metric in self.metrics:\n                self.metrics[metric].append(logs.get(metric))\n            else:\n                self.metrics[metric] = [logs.get(metric)]\n        \n        # Plotting\n        metrics = [x for x in logs if 'val' not in x]\n        \n        f, axs = plt.subplots(1, len(metrics), figsize=(15,5))\n        clear_output(wait=True)\n\n        for i, metric in enumerate(metrics):\n            axs[i].plot(range(1, len(self.metrics[metric]) + 1), \n                        self.metrics[metric], \n                        label=metric)\n            if logs[ metric]:\n                axs[i].plot(range(1, len(self.metrics[metric]) + 1), \n                            self.metrics[ metric], \n                            label= metric)\n                \n            axs[i].legend()\n            axs[i].grid()\n\n        plt.tight_layout()\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-22T20:51:53.577605Z","iopub.execute_input":"2023-12-22T20:51:53.577883Z","iopub.status.idle":"2023-12-22T20:51:53.591716Z","shell.execute_reply.started":"2023-12-22T20:51:53.577859Z","shell.execute_reply":"2023-12-22T20:51:53.590967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"with tpu_strategy.scope():\n    densenet=tf.keras.applications.mobilenet.MobileNet(\n    include_top=True,\n    weights=None,\n    input_shape=(224,224 ,3),\n    classifier_activation='softmax',\n    classes=21\n    )\n    model = CustomModel(inputs=densenet.input,outputs=densenet.output)\n\n    model.compile(optimizer=\"adam\",loss = \"sparse_categorical_crossentropy\")","metadata":{"execution":{"iopub.status.busy":"2023-12-22T21:03:27.413402Z","iopub.execute_input":"2023-12-22T21:03:27.413779Z","iopub.status.idle":"2023-12-22T21:03:29.762465Z","shell.execute_reply.started":"2023-12-22T21:03:27.41375Z","shell.execute_reply":"2023-12-22T21:03:29.761486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import  EarlyStopping,ModelCheckpoint\ncb = [EarlyStopping(patience=5, monitor='sparse_categorical_accuracy', mode='auto' ,restore_best_weights=True,min_delta = 0.01,verbose = True),\n      ModelCheckpoint(\"/kaggle/working/adv_densenet\",save_best_only=True,monitor = 'loss',save_freq=500,save_weights_only = 1),PlotLearning()]\n\nhistory =  model.fit(train_set_preprocessed,callbacks = cb ,verbose = True, epochs = 50 ,batch_size=16 * tpu_strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2023-12-22T22:36:42.792869Z","iopub.execute_input":"2023-12-22T22:36:42.793344Z","iopub.status.idle":"2023-12-22T23:23:40.623022Z","shell.execute_reply.started":"2023-12-22T22:36:42.793304Z","shell.execute_reply":"2023-12-22T23:23:40.621863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/saving3')","metadata":{"execution":{"iopub.status.busy":"2023-12-22T23:26:34.239527Z","iopub.execute_input":"2023-12-22T23:26:34.240633Z","iopub.status.idle":"2023-12-22T23:26:46.526457Z","shell.execute_reply.started":"2023-12-22T23:26:34.240588Z","shell.execute_reply":"2023-12-22T23:26:46.52524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}