-
Notifications
You must be signed in to change notification settings - Fork 0
/
resnet_verification.py
69 lines (57 loc) · 2.28 KB
/
resnet_verification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import torch
import torchvision
import pytorch_lightning as pl
import urllib.request
from urllib.error import HTTPError
from PIL import Image
from torch.utils.data import Dataset, DataLoader
from pytorch_lightning.callbacks import LearningRateMonitor, ModelCheckpoint
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data as data
import torch.optim as optim
import torchmetrics
import pandas as pd
from torchvision import transforms
class LabeledData(torch.utils.data.Dataset):
def __init__(self,manifest,label_map,transform):
self.manifest_df = pd.read_csv(manifest)
self.transform=transform
self.label_map = label_map
def __len__(self):
return(len(self.manifest_df))
def __getitem__(self,idx):
img = Image.open(self.manifest_df.loc[idx,"path"]).convert('RGB')
if self.transform:
img = self.transform(img)
label = torch.tensor(self.label_map[self.manifest_df.loc[idx,"label"]])
return (img,label)
class FullySupervisedModel(pl.LightningModule):
def __init__(self, lr, weight_decay, num_classes, max_epochs=500):
super().__init__()
self.save_hyperparameters()
self.convnet = torchvision.models.resnet18(pretrained=True)
self.convnet.fc = torch.nn.Linear(512,num_classes)
self.loss = torch.nn.CrossEntropyLoss()
self.val_acc = torchmetrics.Accuracy()
def configure_optimizers(self):
optimizer = optim.Adam(self.parameters(),
lr=self.hparams.lr,
weight_decay=self.hparams.weight_decay)
lr_scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer,
T_max=self.hparams.max_epochs,
eta_min=self.hparams.lr/50)
return [optimizer], [lr_scheduler]
def forward(self,x):
return self.convnet(x)
def training_step(self, batch, batch_idx):
img, labels = batch
logits = self(img)
return self.loss(logits, labels)
def validation_step(self, batch, batch_idx):
img, labels = batch
logits = self(img)
self.val_acc(logits, labels)
self.log("val_acc",self.val_acc,on_step=False,on_epoch=True,prog_bar=True)