{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Demo: Load a model from RobustBench\n",
    "\n",
    "Load a pre-trained model from RobustBench and evaluate the model's robustness\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install -q git+https://github.com/RobustBench/robustbench.git@2d630bc9e8d1cf50d46a4dda65fd36850e3ef769"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import aiml\n",
    "from aiml.evaluation.evaluate import evaluate\n",
    "from robustbench.data import load_cifar10\n",
    "from robustbench.utils import load_model\n",
    "import torch\n",
    "from torch.utils.data import TensorDataset\n",
    "import torchvision as tv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Files already downloaded and verified\n"
     ]
    }
   ],
   "source": [
    "# x_test, y_test = load_cifar10(n_examples=50)\n",
    "# x_test=torch.Tensor(x_test)\n",
    "# y_test=torch.Tensor(y_test)\n",
    "# dataset = TensorDataset(x_test, y_test)\n",
    "\n",
    "\n",
    "dataset_train = tv.datasets.CIFAR10('./data', download=True, train=True)\n",
    "dataset_test = tv.datasets.CIFAR10('./data', download=True, train=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Download started: path=models\\cifar10\\Linf\\Carmon2019Unlabeled.pt (gdrive_id=15tUx-gkZMYx7BfEOw1GY5OKC-jECIsPQ)\n",
      "Download finished: path=models\\cifar10\\Linf\\Carmon2019Unlabeled.pt (gdrive_id=15tUx-gkZMYx7BfEOw1GY5OKC-jECIsPQ)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "model = load_model(model_name='Carmon2019Unlabeled')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Files already downloaded and verified\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'TensorDataset' object has no attribute 'classes'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32mc:\\Users\\takuy\\project399\\capstone-project-team-7-1\\examples\\demo_robustbench.ipynb Cell 6\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> <a href='vscode-notebook-cell:/c%3A/Users/takuy/project399/capstone-project-team-7-1/examples/demo_robustbench.ipynb#W4sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m evaluate(model,input_train_data\u001b[39m=\u001b[39;49mdataset,input_test_data\u001b[39m=\u001b[39;49mdataset)\n",
      "File \u001b[1;32mc:\\Users\\takuy\\miniconda3\\envs\\lightning_test\\lib\\site-packages\\aiml\\evaluation\\evaluate.py:35\u001b[0m, in \u001b[0;36mevaluate\u001b[1;34m(input_model, input_train_data, input_test_data, input_shape, clip_values, nb_classes, batch_size_attack, num_threads_attack, batch_size_train, batch_size_test)\u001b[0m\n\u001b[0;32m     32\u001b[0m risk_levels \u001b[39m=\u001b[39m [\u001b[39m\"\u001b[39m\u001b[39mLOW\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39mMEDIUM\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39mHIGH\u001b[39m\u001b[39m\"\u001b[39m]\n\u001b[0;32m     33\u001b[0m risk_eval \u001b[39m=\u001b[39m risk_levels[\u001b[39m2\u001b[39m]  \u001b[39m# Default risk evaluation is high\u001b[39;00m\n\u001b[1;32m---> 35\u001b[0m result_list \u001b[39m=\u001b[39m get_accuracy_results(\n\u001b[0;32m     36\u001b[0m     input_model,\n\u001b[0;32m     37\u001b[0m     input_train_data,\n\u001b[0;32m     38\u001b[0m     input_test_data,\n\u001b[0;32m     39\u001b[0m     input_shape,\n\u001b[0;32m     40\u001b[0m     clip_values,\n\u001b[0;32m     41\u001b[0m     nb_classes,\n\u001b[0;32m     42\u001b[0m     batch_size_attack,\n\u001b[0;32m     43\u001b[0m     num_threads_attack,\n\u001b[0;32m     44\u001b[0m     batch_size_train,\n\u001b[0;32m     45\u001b[0m     batch_size_test,\n\u001b[0;32m     46\u001b[0m )\n\u001b[0;32m     48\u001b[0m \u001b[39m# Algorithm to determine risk evaluation\u001b[39;00m\n\u001b[0;32m     49\u001b[0m white_box_average \u001b[39m=\u001b[39m calculate_average(result_list)\n",
      "File \u001b[1;32mc:\\Users\\takuy\\miniconda3\\envs\\lightning_test\\lib\\site-packages\\aiml\\evaluation\\get_accuracy_results.py:39\u001b[0m, in \u001b[0;36mget_accuracy_results\u001b[1;34m(input_model, input_train_data, input_test_data, input_shape, clip_values, nb_classes, batch_size_attack, num_threads_attack, batch_size_train, batch_size_test)\u001b[0m\n\u001b[0;32m     36\u001b[0m model \u001b[39m=\u001b[39m load_model(input_model)\n\u001b[0;32m     38\u001b[0m \u001b[39m# Create and load surrogate model\u001b[39;00m\n\u001b[1;32m---> 39\u001b[0m model \u001b[39m=\u001b[39m create_surrogate_model(model, input_test_data)\n\u001b[0;32m     41\u001b[0m \u001b[39mif\u001b[39;00m input_train_data \u001b[39m!=\u001b[39m \u001b[39mNone\u001b[39;00m:\n\u001b[0;32m     42\u001b[0m     dataset_train \u001b[39m=\u001b[39m load_test_set(input_train_data)\n",
      "File \u001b[1;32mc:\\Users\\takuy\\miniconda3\\envs\\lightning_test\\lib\\site-packages\\aiml\\surrogate_model\\create_surrogate_model.py:41\u001b[0m, in \u001b[0;36mcreate_surrogate_model\u001b[1;34m(model, dataset)\u001b[0m\n\u001b[0;32m     39\u001b[0m oracle \u001b[39m=\u001b[39m LogSoftmaxModule(model)\n\u001b[0;32m     40\u001b[0m \u001b[39m# Using the PyTorch implementation\u001b[39;00m\n\u001b[1;32m---> 41\u001b[0m substitute \u001b[39m=\u001b[39m create_vgg16_bn_cifar10(\u001b[39mlen\u001b[39m(dataset\u001b[39m.\u001b[39;49mclasses))\n\u001b[0;32m     42\u001b[0m \u001b[39m# Check the cell above. Note that without log function. The loss doesn't seem correct.\u001b[39;00m\n\u001b[0;32m     43\u001b[0m loss_fn \u001b[39m=\u001b[39m nn\u001b[39m.\u001b[39mKLDivLoss(reduction\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mbatchmean\u001b[39m\u001b[39m\"\u001b[39m, log_target\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m)\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'TensorDataset' object has no attribute 'classes'"
     ]
    }
   ],
   "source": [
    "evaluate(model,input_train_data=dataset_train,input_test_data=dataset_test)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "lightning_test",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
