{"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":"import numpy as np\nimport os\nimport torch\nfrom torchvision.datasets import CIFAR10\nfrom torchvision.models import resnet50\nfrom torch import nn, utils, Tensor\nimport torch.nn.functional as F\nfrom torchvision import transforms\nfrom torchmetrics.functional import accuracy\nimport pytorch_lightning as pl","metadata":{"execution":{"iopub.status.busy":"2022-12-09T06:33:15.898876Z","iopub.execute_input":"2022-12-09T06:33:15.899288Z","iopub.status.idle":"2022-12-09T06:33:15.906087Z","shell.execute_reply.started":"2022-12-09T06:33:15.899249Z","shell.execute_reply":"2022-12-09T06:33:15.904865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = CIFAR10(os.getcwd(), download=True, train = True, transform=transforms.ToTensor())\ntrain_loader = utils.data.DataLoader(train_dataset, batch_size = 128,shuffle = True)\n\ntest_dataset = CIFAR10(os.getcwd(), download=True, train = False, transform=transforms.ToTensor())\ntest_loader = utils.data.DataLoader(test_dataset, batch_size = 16)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T05:54:03.744304Z","iopub.execute_input":"2022-12-09T05:54:03.744721Z","iopub.status.idle":"2022-12-09T05:54:05.484861Z","shell.execute_reply.started":"2022-12-09T05:54:03.744686Z","shell.execute_reply":"2022-12-09T05:54:05.483792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CIFAR10Classifier(pl.LightningModule):\n    def __init__(self):\n        super().__init__()\n        \n        backbone = resnet50(pretrained = True)\n        num_filters = backbone.fc.in_features\n        layers = list(backbone.children())[:-1]\n        self.feature_extractor = nn.Sequential(*layers)\n        \n        num_classes = 10\n        self.classifier = nn.Linear(num_filters, num_classes)\n\n    def forward(self, x):\n        self.feature_extractor.eval()\n        with torch.no_grad():\n            representations = self.feature_extractor(x).flatten(1)\n        x = self.classifier(representations)\n        return x.softmax(dim = 1)\n    \n    def configure_optimizers(self):\n        optimizer = torch.optim.Adam(self.parameters(), lr=0.0005)\n        return optimizer\n            \n    def training_step(self, batch, batch_idx):\n        x, y = batch\n        pred = self.forward(x)\n        loss = F.cross_entropy(pred,y)\n        acc = accuracy(torch.argmax(pred, dim = 1), y)\n        self.log('train_loss', loss)\n        self.log('train_acc', acc)\n        return loss\n    \n    def test_step(self, batch, batch_idx):\n        x, y = batch\n        pred = self.forward(x)\n        loss = F.cross_entropy(pred,y)\n        acc = accuracy(torch.argmax(pred, dim = 1), y)\n        self.log('test_loss', loss)\n        self.log('test_acc', acc)\n\n        ","metadata":{"execution":{"iopub.status.busy":"2022-12-09T06:43:04.745570Z","iopub.execute_input":"2022-12-09T06:43:04.745973Z","iopub.status.idle":"2022-12-09T06:43:04.758532Z","shell.execute_reply.started":"2022-12-09T06:43:04.745938Z","shell.execute_reply":"2022-12-09T06:43:04.757557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer = pl.Trainer(accelerator = 'gpu', devices = 2, max_epochs = 20, \n                     weights_save_path = '/kaggle/working/model',\n                     logger = pl.loggers.CSVLogger(save_dir = 'logs/'))\nclassifier = CIFAR10Classifier()","metadata":{"execution":{"iopub.status.busy":"2022-12-09T06:43:08.362123Z","iopub.execute_input":"2022-12-09T06:43:08.362517Z","iopub.status.idle":"2022-12-09T06:43:14.364352Z","shell.execute_reply.started":"2022-12-09T06:43:08.362483Z","shell.execute_reply":"2022-12-09T06:43:14.363262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.fit(model = classifier, train_dataloaders = train_loader)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T06:43:16.582893Z","iopub.execute_input":"2022-12-09T06:43:16.584020Z","iopub.status.idle":"2022-12-09T06:50:34.745095Z","shell.execute_reply.started":"2022-12-09T06:43:16.583968Z","shell.execute_reply":"2022-12-09T06:50:34.743683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.test(classifier, test_loader)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T06:53:07.777725Z","iopub.execute_input":"2022-12-09T06:53:07.778781Z","iopub.status.idle":"2022-12-09T06:53:27.076239Z","shell.execute_reply.started":"2022-12-09T06:53:07.778713Z","shell.execute_reply":"2022-12-09T06:53:27.074845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/working/logs/lightning_logs/version_0')","metadata":{"execution":{"iopub.status.busy":"2022-12-08T17:22:37.933773Z","iopub.execute_input":"2022-12-08T17:22:37.934149Z","iopub.status.idle":"2022-12-08T17:22:37.941984Z","shell.execute_reply.started":"2022-12-08T17:22:37.934115Z","shell.execute_reply":"2022-12-08T17:22:37.940889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf = pd.read_csv('/kaggle/working/logs/lightning_logs/version_0/metrics.csv')\ndf.head(100)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T17:23:41.171059Z","iopub.execute_input":"2022-12-08T17:23:41.171500Z","iopub.status.idle":"2022-12-08T17:23:41.187371Z","shell.execute_reply.started":"2022-12-08T17:23:41.171462Z","shell.execute_reply":"2022-12-08T17:23:41.185970Z"},"trusted":true},"execution_count":null,"outputs":[]}]}