Spaces:
Runtime error
Runtime error
Commit
·
e4f516c
1
Parent(s):
b57661c
Update model.py
Browse files
model.py
CHANGED
|
@@ -31,6 +31,7 @@ class ResBlock(nn.Module):
|
|
| 31 |
out = self.relu(out)
|
| 32 |
return out
|
| 33 |
|
|
|
|
| 34 |
class LightningDavidNet(LightningModule):
|
| 35 |
|
| 36 |
def __init__(self,data_dir=PATH_DATASETS, hidden_size=16, learning_rate=2e-4,kernel_size=3, stride=1, padding=1, downsample = None):
|
|
@@ -75,7 +76,6 @@ class LightningDavidNet(LightningModule):
|
|
| 75 |
x = self.r2(x)
|
| 76 |
x=residual+x
|
| 77 |
x = self.maxPool(x)
|
| 78 |
-
# # x = self.avgpool(x)
|
| 79 |
x = x.view(-1,512)
|
| 80 |
x = self.fc1(x)
|
| 81 |
x = F.log_softmax(x, dim=1)
|
|
@@ -83,41 +83,40 @@ class LightningDavidNet(LightningModule):
|
|
| 83 |
|
| 84 |
def training_step(self, batch, batch_idx):
|
| 85 |
x,y = batch
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
return loss
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
def configure_optimizers(self):
|
| 91 |
optimizer = torch.optim.Adam(self.parameters(), lr=0.03, weight_decay=1e-4)
|
| 92 |
-
|
| 93 |
-
scheduler_dict = {
|
| 94 |
-
"scheduler": torch.optim.lr_scheduler.OneCycleLR(
|
| 95 |
optimizer,
|
| 96 |
-
|
|
|
|
| 97 |
epochs=self.trainer.max_epochs,
|
| 98 |
-
steps_per_epoch=
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
return
|
| 103 |
-
|
| 104 |
-
# lr_scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer, step_size=1)
|
| 105 |
-
# return [optimizer], [lr_scheduler]
|
| 106 |
-
# return optimizer
|
| 107 |
|
| 108 |
def validation_step(self, batch, batch_idx):
|
| 109 |
-
|
| 110 |
-
logits = self(x)
|
| 111 |
-
loss = F.cross_entropy(logits, y)
|
| 112 |
-
preds = torch.argmax(logits,dim = 1)
|
| 113 |
-
self.accuracy(preds,y)
|
| 114 |
-
self.log("val_loss",loss, prog_bar = True)
|
| 115 |
-
self.log("val_arr",self.accuracy,prog_bar = True)
|
| 116 |
|
| 117 |
def test_step(self,batch,batch_idx):
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
def predict_step(self, batch, batch_idx, dataloader_idx=0):
|
| 121 |
-
x,y = batch
|
| 122 |
-
output = self(x)
|
| 123 |
-
return x,y,output.argmax(dim=1),output
|
|
|
|
| 31 |
out = self.relu(out)
|
| 32 |
return out
|
| 33 |
|
| 34 |
+
|
| 35 |
class LightningDavidNet(LightningModule):
|
| 36 |
|
| 37 |
def __init__(self,data_dir=PATH_DATASETS, hidden_size=16, learning_rate=2e-4,kernel_size=3, stride=1, padding=1, downsample = None):
|
|
|
|
| 76 |
x = self.r2(x)
|
| 77 |
x=residual+x
|
| 78 |
x = self.maxPool(x)
|
|
|
|
| 79 |
x = x.view(-1,512)
|
| 80 |
x = self.fc1(x)
|
| 81 |
x = F.log_softmax(x, dim=1)
|
|
|
|
| 83 |
|
| 84 |
def training_step(self, batch, batch_idx):
|
| 85 |
x,y = batch
|
| 86 |
+
y_pred = self(x)
|
| 87 |
+
loss = F.cross_entropy(y_pred, y)
|
| 88 |
+
acc = self.accuracy(y_pred, y)
|
| 89 |
+
|
| 90 |
+
self.log('train_loss', loss, prog_bar=True, on_step=False, on_epoch=True)
|
| 91 |
+
self.log('train_acc', acc, prog_bar=True, on_step=False, on_epoch=True)
|
| 92 |
+
|
| 93 |
return loss
|
| 94 |
+
|
| 95 |
+
def evaluate(self, batch, stage=None):
|
| 96 |
+
x, y = batch
|
| 97 |
+
y_test_pred = self(x)
|
| 98 |
+
loss = F.cross_entropy(y_test_pred, y)
|
| 99 |
+
acc = self.accuracy(y_test_pred, y)
|
| 100 |
+
|
| 101 |
+
if stage:
|
| 102 |
+
self.log(f"{stage}_loss", loss, prog_bar=True)
|
| 103 |
+
self.log(f"{stage}_acc", acc, prog_bar=True)
|
| 104 |
|
| 105 |
def configure_optimizers(self):
|
| 106 |
optimizer = torch.optim.Adam(self.parameters(), lr=0.03, weight_decay=1e-4)
|
| 107 |
+
scheduler = torch.optim.lr_scheduler.OneCycleLR(
|
|
|
|
|
|
|
| 108 |
optimizer,
|
| 109 |
+
max_lr= 5.38E-02, #self.hparams.lr,
|
| 110 |
+
pct_start = 5/self.trainer.max_epochs,
|
| 111 |
epochs=self.trainer.max_epochs,
|
| 112 |
+
steps_per_epoch=len(train_loader),
|
| 113 |
+
div_factor=100,verbose=False,
|
| 114 |
+
three_phase=False
|
| 115 |
+
)
|
| 116 |
+
return ([optimizer],[scheduler])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
def validation_step(self, batch, batch_idx):
|
| 119 |
+
self.evaluate(batch, "val")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
def test_step(self,batch,batch_idx):
|
| 122 |
+
self.evaluate(batch, "test")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|