-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
56 lines (41 loc) · 1.73 KB
/
models.py
File metadata and controls
56 lines (41 loc) · 1.73 KB
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
import torch
import torch.nn as nn
from torchvision import models
import pytorch_lightning as pl
from torchmetrics import Accuracy
class LightningResNet18(pl.LightningModule):
def __init__(self, num_classes: int = 1000, learning_rate: float = 1e-3):
super(LightningResNet18, self).__init__()
self.save_hyperparameters()
# Load a pre-trained ResNet18 model
self.model = models.resnet18()
# Replace the final fully connected layer
self.model.fc = nn.Linear(self.model.fc.in_features, num_classes)
# Define a criterion for loss computation
self.criterion = nn.CrossEntropyLoss()
# Define a metric for accuracy computation
self.accuracy = Accuracy(
task='MULTICLASS',
num_classes=num_classes,
)
def forward(self, x):
return self.model(x)
def training_step(self, batch, batch_idx):
images, labels = batch
outputs = self(images)
loss = self.criterion(outputs, labels)
# Log training loss and accuracy
self.log('train_loss', loss)
self.log('train_acc', self.accuracy(outputs, labels), prog_bar=True)
return loss
def validation_step(self, batch, batch_idx):
images, labels = batch
outputs = self(images)
loss = self.criterion(outputs, labels)
# Log validation loss and accuracy
self.log('val_loss', loss, prog_bar=True)
self.log('val_acc', self.accuracy(outputs, labels), prog_bar=True)
return loss
def configure_optimizers(self):
optimizer = torch.optim.Adam(self.parameters(), lr=self.hparams.learning_rate)
return optimizer