-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathxception.py
More file actions
36 lines (29 loc) · 1 KB
/
xception.py
File metadata and controls
36 lines (29 loc) · 1 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
import timm
from lightning.pytorch import LightningModule
from torch.nn import BCEWithLogitsLoss
from torch.optim import Adam
class Xception(LightningModule):
def __init__(self, lr, distributed=False):
super(Xception, self).__init__()
self.lr = lr
self.model = timm.create_model('xception', pretrained=True, num_classes=1)
self.loss_fn = BCEWithLogitsLoss()
self.distributed = distributed
def forward(self, x):
x = self.model(x)
return x
def training_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = self.loss_fn(y_hat, y.unsqueeze(1))
self.log('train_loss', loss)
return loss
def validation_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = self.loss_fn(y_hat, y.unsqueeze(1))
self.log('val_loss', loss)
return loss
def configure_optimizers(self):
optimizer = Adam(self.parameters(), lr=self.lr)
return [optimizer]