Spaces:
Runtime error
Runtime error
soutrik
commited on
Commit
Β·
f057c2a
1
Parent(s):
b40c62a
resnet model and experiments
Browse files
configs/experiment/{catdog_experiment_convnext.yaml β catdog_experiment_resnet.yaml}
RENAMED
|
File without changes
|
configs/model/{catdog_classifier_convnext.yaml β catdog_classifier_resnet.yaml}
RENAMED
|
File without changes
|
src/models/catdog_model_resnet.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import lightning as L
|
| 2 |
+
import torch.nn.functional as F
|
| 3 |
+
from torch import optim
|
| 4 |
+
from torchmetrics.classification import Accuracy, F1Score
|
| 5 |
+
import timm
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class ResnetClassifier(L.LightningModule):
|
| 10 |
+
def __init__(
|
| 11 |
+
self,
|
| 12 |
+
num_classes: int = 2, # Binary classification with two classes
|
| 13 |
+
lr: float = 1e-3,
|
| 14 |
+
weight_decay: float = 1e-5,
|
| 15 |
+
factor: float = 0.1,
|
| 16 |
+
patience: int = 10,
|
| 17 |
+
min_lr: float = 1e-6,
|
| 18 |
+
):
|
| 19 |
+
super().__init__()
|
| 20 |
+
self.save_hyperparameters()
|
| 21 |
+
|
| 22 |
+
# Vision Transformer model initialization
|
| 23 |
+
self.model = timm.create_model(
|
| 24 |
+
"efficientnet_b0", pretrained=True, num_classes=num_classes
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Define accuracy and F1 metrics for binary classification
|
| 28 |
+
self.train_acc = Accuracy(task="binary")
|
| 29 |
+
self.val_acc = Accuracy(task="binary")
|
| 30 |
+
self.test_acc = Accuracy(task="binary")
|
| 31 |
+
|
| 32 |
+
self.train_f1 = F1Score(task="binary")
|
| 33 |
+
self.val_f1 = F1Score(task="binary")
|
| 34 |
+
self.test_f1 = F1Score(task="binary")
|
| 35 |
+
|
| 36 |
+
def forward(self, x):
|
| 37 |
+
return self.model(x)
|
| 38 |
+
|
| 39 |
+
def _shared_step(self, batch, stage):
|
| 40 |
+
x, y = batch
|
| 41 |
+
logits = self(x) # Model output shape: [batch_size, num_classes]
|
| 42 |
+
loss = F.cross_entropy(logits, y) # Cross-entropy for binary classification
|
| 43 |
+
preds = torch.argmax(logits, dim=1) # Predicted class (0 or 1)
|
| 44 |
+
|
| 45 |
+
# Update and log metrics
|
| 46 |
+
acc = getattr(self, f"{stage}_acc")
|
| 47 |
+
f1 = getattr(self, f"{stage}_f1")
|
| 48 |
+
acc(preds, y)
|
| 49 |
+
f1(preds, y)
|
| 50 |
+
|
| 51 |
+
# Logging of metrics and loss
|
| 52 |
+
self.log(f"{stage}_loss", loss, prog_bar=True, on_epoch=True)
|
| 53 |
+
self.log(f"{stage}_acc", acc, prog_bar=True, on_epoch=True)
|
| 54 |
+
self.log(f"{stage}_f1", f1, prog_bar=True, on_epoch=True)
|
| 55 |
+
|
| 56 |
+
return loss
|
| 57 |
+
|
| 58 |
+
def training_step(self, batch, batch_idx):
|
| 59 |
+
return self._shared_step(batch, "train")
|
| 60 |
+
|
| 61 |
+
def validation_step(self, batch, batch_idx):
|
| 62 |
+
self._shared_step(batch, "val")
|
| 63 |
+
|
| 64 |
+
def test_step(self, batch, batch_idx):
|
| 65 |
+
self._shared_step(batch, "test")
|
| 66 |
+
|
| 67 |
+
def configure_optimizers(self):
|
| 68 |
+
optimizer = optim.AdamW(
|
| 69 |
+
self.parameters(),
|
| 70 |
+
lr=self.hparams.lr,
|
| 71 |
+
weight_decay=self.hparams.weight_decay,
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
scheduler = optim.lr_scheduler.ReduceLROnPlateau(
|
| 75 |
+
optimizer,
|
| 76 |
+
mode="min",
|
| 77 |
+
factor=self.hparams.factor,
|
| 78 |
+
patience=self.hparams.patience,
|
| 79 |
+
min_lr=self.hparams.min_lr,
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
return {
|
| 83 |
+
"optimizer": optimizer,
|
| 84 |
+
"lr_scheduler": {
|
| 85 |
+
"scheduler": scheduler,
|
| 86 |
+
"monitor": "val_loss",
|
| 87 |
+
"interval": "epoch",
|
| 88 |
+
},
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
model = ViTTinyClassifier()
|
| 94 |
+
print(model)
|
todo.md
CHANGED
|
@@ -1,11 +1,8 @@
|
|
| 1 |
**__Pytorch Lightning Classifier with Hydra DVC and Linting and Pytest Deployed on AWS EC2__**:
|
| 2 |
-
-
|
| 3 |
-
-
|
| 4 |
-
-
|
| 5 |
-
-
|
| 6 |
-
-
|
| 7 |
-
-
|
| 8 |
-
-
|
| 9 |
-
- Dockerized application and tested via docker-compose
|
| 10 |
-
- Deployed on AWS EC2 instance using github actions
|
| 11 |
-
- Github actions for CI/CD and docker image push to elastic container registry (ECR)
|
|
|
|
| 1 |
**__Pytorch Lightning Classifier with Hydra DVC and Linting and Pytest Deployed on AWS EC2__**:
|
| 2 |
+
- Find and train a new model on a same dataset
|
| 3 |
+
- Imbibe the concept of torchscript for model deployment
|
| 4 |
+
- New hydra configuration for the model with new experiment and all the configurations
|
| 5 |
+
- create gradio app.py for UI deployment
|
| 6 |
+
- create a new github action for first train and test a model then deploy it on AWS EC2
|
| 7 |
+
- For Aws ec2 automatic start of the server train and test the code then deploy the model on Huggingface spaces and then close the server
|
| 8 |
+
- Use Boto and dvc for s3 bucket transfer of pytorch model and data files for training and testing the model on AWS EC2 server
|
|
|
|
|
|
|
|
|