mirror of
https://github.com/clearml/clearml
synced 2025-06-26 18:16:07 +00:00
Fix jsonargparse support (#403)
This commit is contained in:
@@ -11,5 +11,5 @@ class Main:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
Task.init(project_name="examples", task_name="jsonargparse command", auto_connect_frameworks={"pytorch_lightning": False})
|
||||
Task.init(project_name="examples", task_name="jsonargparse command")
|
||||
print(CLI(Main))
|
||||
@@ -10,7 +10,7 @@ class Arg2:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
Task.init(project_name="examples", task_name="jsonargparse nested namespaces", auto_connect_frameworks={"pytorch-lightning": False})
|
||||
Task.init(project_name="examples", task_name="jsonargparse nested namespaces")
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument("--arg1.opt1", default="from default 1")
|
||||
parser.add_argument("--arg1.opt2", default="from default 2")
|
||||
103
examples/frameworks/jsonargparse/pytorch_lightning_cli.py
Normal file
103
examples/frameworks/jsonargparse/pytorch_lightning_cli.py
Normal file
@@ -0,0 +1,103 @@
|
||||
# Copyright The PyTorch Lightning team.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# Notice that this file has been modified to examplify the use of
|
||||
# ClearML when used with PyTorch Lightning
|
||||
|
||||
import torch
|
||||
import torchvision.transforms as T
|
||||
from torch.nn import functional as F
|
||||
import torch.nn as nn
|
||||
from torchmetrics import Accuracy
|
||||
|
||||
from torchvision.datasets.mnist import MNIST
|
||||
from pytorch_lightning import LightningModule
|
||||
from pytorch_lightning.utilities.cli import LightningCLI
|
||||
from clearml import Task
|
||||
|
||||
|
||||
class Net(nn.Module):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.conv1 = nn.Conv2d(1, 32, 3, 1)
|
||||
self.conv2 = nn.Conv2d(32, 64, 3, 1)
|
||||
self.dropout1 = nn.Dropout(0.25)
|
||||
self.dropout2 = nn.Dropout(0.5)
|
||||
self.fc1 = nn.Linear(9216, 128)
|
||||
self.fc2 = nn.Linear(128, 10)
|
||||
|
||||
def forward(self, x):
|
||||
x = self.conv1(x)
|
||||
x = F.relu(x)
|
||||
x = self.conv2(x)
|
||||
x = F.relu(x)
|
||||
x = F.max_pool2d(x, 2)
|
||||
x = self.dropout1(x)
|
||||
x = torch.flatten(x, 1)
|
||||
x = self.fc1(x)
|
||||
x = F.relu(x)
|
||||
x = self.dropout2(x)
|
||||
x = self.fc2(x)
|
||||
output = F.log_softmax(x, dim=1)
|
||||
return output
|
||||
|
||||
|
||||
class ImageClassifier(LightningModule):
|
||||
def __init__(self, model=None, lr=1.0, gamma=0.7, batch_size=32):
|
||||
super().__init__()
|
||||
self.save_hyperparameters(ignore="model")
|
||||
self.model = model or Net()
|
||||
self.test_acc = Accuracy()
|
||||
|
||||
def forward(self, x):
|
||||
return self.model(x)
|
||||
|
||||
def training_step(self, batch, batch_idx):
|
||||
x, y = batch
|
||||
logits = self.forward(x)
|
||||
loss = F.nll_loss(logits, y.long())
|
||||
return loss
|
||||
|
||||
def test_step(self, batch, batch_idx):
|
||||
x, y = batch
|
||||
logits = self.forward(x)
|
||||
loss = F.nll_loss(logits, y.long())
|
||||
self.test_acc(logits, y)
|
||||
self.log("test_acc", self.test_acc)
|
||||
self.log("test_loss", loss)
|
||||
|
||||
def configure_optimizers(self):
|
||||
optimizer = torch.optim.Adadelta(self.model.parameters(), lr=self.hparams.lr)
|
||||
return [optimizer], [torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=self.hparams.gamma)]
|
||||
|
||||
@property
|
||||
def transform(self):
|
||||
return T.Compose([T.ToTensor(), T.Normalize((0.1307,), (0.3081,))])
|
||||
|
||||
def prepare_data(self) -> None:
|
||||
MNIST("./data", download=True)
|
||||
|
||||
def train_dataloader(self):
|
||||
train_dataset = MNIST("./data", train=True, download=False, transform=self.transform)
|
||||
return torch.utils.data.DataLoader(train_dataset, batch_size=self.hparams.batch_size)
|
||||
|
||||
def test_dataloader(self):
|
||||
test_dataset = MNIST("./data", train=False, download=False, transform=self.transform)
|
||||
return torch.utils.data.DataLoader(test_dataset, batch_size=self.hparams.batch_size)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
Task.add_requirements('requirements.txt')
|
||||
task = Task.init(project_name="example", task_name="pytorch_lightning_jsonargparse")
|
||||
LightningCLI(ImageClassifier, seed_everything_default=42, save_config_overwrite=True, run=True)
|
||||
12
examples/frameworks/jsonargparse/pytorch_lightning_cli.yml
Normal file
12
examples/frameworks/jsonargparse/pytorch_lightning_cli.yml
Normal file
@@ -0,0 +1,12 @@
|
||||
trainer:
|
||||
callbacks:
|
||||
- class_path: pytorch_lightning.callbacks.LearningRateMonitor
|
||||
init_args:
|
||||
logging_interval: epoch
|
||||
- class_path: pytorch_lightning.callbacks.ModelCheckpoint
|
||||
init_args:
|
||||
filename: best
|
||||
save_last: False
|
||||
save_top_k: 1
|
||||
monitor: loss
|
||||
mode: min
|
||||
@@ -1,2 +1,7 @@
|
||||
clearml
|
||||
jsonargparse
|
||||
jsonargparse
|
||||
pytorch_lightning
|
||||
torch
|
||||
torchmetrics
|
||||
torchvision
|
||||
docstring_parser
|
||||
|
||||
Reference in New Issue
Block a user