Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] authored and lantiga committed Mar 4, 2024
1 parent e29a8b1 commit 273650f
Show file tree
Hide file tree
Showing 176 changed files with 821 additions and 943 deletions.
6 changes: 3 additions & 3 deletions examples/app/dag/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def __init__(self, models_paths: list):
)

# Step 3: Create the work to train the models_paths in parallel.
self.dict = Dict(
**{model_path.split(".")[-1]: ModelWork(model_path, parallel=True) for model_path in models_paths}
)
self.dict = Dict(**{
model_path.split(".")[-1]: ModelWork(model_path, parallel=True) for model_path in models_paths
})

# Step 4: Some element to track components progress.
self.has_completed = False
Expand Down
12 changes: 5 additions & 7 deletions examples/app/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ def setup(self):
def predict(self, request):
image = base64.b64decode(request.image.encode("utf-8"))
image = Image.open(io.BytesIO(image))
transforms = torchvision.transforms.Compose(
[
torchvision.transforms.Resize(224),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
]
)
transforms = torchvision.transforms.Compose([
torchvision.transforms.Resize(224),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
image = transforms(image)
image = image.to(self._device)
prediction = self._model(image.unsqueeze(0))
Expand Down
12 changes: 5 additions & 7 deletions examples/app/server_with_auto_scaler/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ def setup(self):
self._model = torchvision.models.resnet18(pretrained=True).to(self._device)

def predict(self, requests: BatchRequestModel):
transforms = torchvision.transforms.Compose(
[
torchvision.transforms.Resize(224),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
]
)
transforms = torchvision.transforms.Compose([
torchvision.transforms.Resize(224),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
images = []
for request in requests.inputs:
image = app.components.serve.types.image.Image.deserialize(request.image)
Expand Down
17 changes: 8 additions & 9 deletions examples/fabric/dcgan/train_fabric.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Code adapted from the official PyTorch DCGAN tutorial:
https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html
"""

import os
import time
from pathlib import Path
Expand Down Expand Up @@ -55,14 +56,12 @@ def main():
root=dataroot,
split="all",
download=True,
transform=transforms.Compose(
[
transforms.Resize(image_size),
transforms.CenterCrop(image_size),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]
),
transform=transforms.Compose([
transforms.Resize(image_size),
transforms.CenterCrop(image_size),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]),
)

# Create the dataloader
Expand Down Expand Up @@ -227,7 +226,7 @@ def __init__(self):
nn.ReLU(True),
# state size. (ngf) x 32 x 32
nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False),
nn.Tanh()
nn.Tanh(),
# state size. (nc) x 64 x 64
)

Expand Down
17 changes: 8 additions & 9 deletions examples/fabric/dcgan/train_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Code adapted from the official PyTorch DCGAN tutorial:
https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html
"""

import os
import random
import time
Expand Down Expand Up @@ -55,14 +56,12 @@ def main():
root=dataroot,
split="all",
download=True,
transform=transforms.Compose(
[
transforms.Resize(image_size),
transforms.CenterCrop(image_size),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]
),
transform=transforms.Compose([
transforms.Resize(image_size),
transforms.CenterCrop(image_size),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]),
)

# Create the dataloader
Expand Down Expand Up @@ -236,7 +235,7 @@ def __init__(self):
nn.ReLU(True),
# state size. (ngf) x 32 x 32
nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False),
nn.Tanh()
nn.Tanh(),
# state size. (nc) x 64 x 64
)

Expand Down
1 change: 1 addition & 0 deletions examples/fabric/meta_learning/train_fabric.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Run it with:
lightning run model train_fabric.py --accelerator=cuda --devices=2 --strategy=ddp
"""

import cherry
import learn2learn as l2l
import torch
Expand Down
1 change: 1 addition & 0 deletions examples/fabric/meta_learning/train_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Run it with:
torchrun --nproc_per_node=2 --standalone train_torch.py
"""

import os
import random

Expand Down
12 changes: 4 additions & 8 deletions examples/fabric/reinforcement_learning/train_fabric.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,10 @@ def main(args: argparse.Namespace):
)

# Environment setup
envs = gym.vector.SyncVectorEnv(
[
make_env(
args.env_id, args.seed + rank * args.num_envs + i, rank, args.capture_video, logger.log_dir, "train"
)
for i in range(args.num_envs)
]
)
envs = gym.vector.SyncVectorEnv([
make_env(args.env_id, args.seed + rank * args.num_envs + i, rank, args.capture_video, logger.log_dir, "train")
for i in range(args.num_envs)
])
assert isinstance(envs.single_action_space, gym.spaces.Discrete), "only discrete action space is supported"

# Define the agent and the optimizer and setup them with Fabric
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ def player(args, world_collective: TorchCollective, player_trainer_collective: T
)

# Environment setup
envs = gym.vector.SyncVectorEnv(
[make_env(args.env_id, args.seed + i, 0, args.capture_video, log_dir, "train") for i in range(args.num_envs)]
)
envs = gym.vector.SyncVectorEnv([
make_env(args.env_id, args.seed + i, 0, args.capture_video, log_dir, "train") for i in range(args.num_envs)
])
assert isinstance(envs.single_action_space, gym.spaces.Discrete), "only discrete action space is supported"

# Define the agent
Expand Down
24 changes: 11 additions & 13 deletions examples/fabric/reinforcement_learning/train_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,17 @@ def main(args: argparse.Namespace):
)

# Environment setup
envs = gym.vector.SyncVectorEnv(
[
make_env(
args.env_id,
args.seed + global_rank * args.num_envs + i,
global_rank,
args.capture_video,
logger.log_dir if global_rank == 0 else None,
"train",
)
for i in range(args.num_envs)
]
)
envs = gym.vector.SyncVectorEnv([
make_env(
args.env_id,
args.seed + global_rank * args.num_envs + i,
global_rank,
args.capture_video,
logger.log_dir if global_rank == 0 else None,
"train",
)
for i in range(args.num_envs)
])
assert isinstance(envs.single_action_space, gym.spaces.Discrete), "only discrete action space is supported"

# Define the agent and the optimizer and setup them with DistributedDataParallel
Expand Down
1 change: 1 addition & 0 deletions examples/pytorch/basics/autoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
To run: python autoencoder.py --trainer.max_epochs=50
"""

from os import path
from typing import Optional, Tuple

Expand Down
1 change: 1 addition & 0 deletions examples/pytorch/basics/backbone_image_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
To run: python backbone_image_classifier.py --trainer.max_epochs=50
"""

from os import path
from typing import Optional

Expand Down
26 changes: 11 additions & 15 deletions examples/pytorch/domain_templates/computer_vision_fine_tuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,12 @@ def normalize_transform(self):

@property
def train_transform(self):
return transforms.Compose(
[
transforms.Resize((224, 224)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
self.normalize_transform,
]
)
return transforms.Compose([
transforms.Resize((224, 224)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
self.normalize_transform,
])

@property
def valid_transform(self):
Expand Down Expand Up @@ -269,13 +267,11 @@ def add_arguments_to_parser(self, parser):
parser.link_arguments("data.batch_size", "model.batch_size")
parser.link_arguments("finetuning.milestones", "model.milestones")
parser.link_arguments("finetuning.train_bn", "model.train_bn")
parser.set_defaults(
{
"trainer.max_epochs": 15,
"trainer.enable_model_summary": False,
"trainer.num_sanity_val_steps": 0,
}
)
parser.set_defaults({
"trainer.max_epochs": 15,
"trainer.enable_model_summary": False,
"trainer.num_sanity_val_steps": 0,
})


def cli_main():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
tensorboard --logdir default
"""

from argparse import ArgumentParser, Namespace

import numpy as np
Expand Down
15 changes: 7 additions & 8 deletions examples/pytorch/domain_templates/imagenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
python imagenet.py fit --help
"""

import os
from typing import Optional

Expand Down Expand Up @@ -139,14 +140,12 @@ def setup(self, stage: str):
train_dir = os.path.join(self.data_path, "train")
self.train_dataset = datasets.ImageFolder(
train_dir,
transforms.Compose(
[
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
]
),
transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
]),
)
# all stages will use the eval dataset
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
Expand Down
1 change: 1 addition & 0 deletions examples/pytorch/domain_templates/reinforce_learn_ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
[3] https://github.com/sid-sundrani/ppo_lightning
"""

import argparse
from typing import Callable, Iterator, List, Tuple

Expand Down
12 changes: 4 additions & 8 deletions examples/pytorch/domain_templates/semantic_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,10 @@ def __init__(
self.net = UNet(
num_classes=19, num_layers=self.num_layers, features_start=self.features_start, bilinear=self.bilinear
)
self.transform = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize(
mean=[0.35675976, 0.37380189, 0.3764753], std=[0.32064945, 0.32098866, 0.32325324]
),
]
)
self.transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.35675976, 0.37380189, 0.3764753], std=[0.32064945, 0.32098866, 0.32325324]),
])
self.trainset = KITTI(self.data_path, split="train", transform=self.transform)
self.validset = KITTI(self.data_path, split="valid", transform=self.transform)

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
c) validate packages and publish to PyPI
"""

import contextlib
import glob
import logging
Expand Down
1 change: 1 addition & 0 deletions src/lightning/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Root package info."""

import logging
import sys

Expand Down
1 change: 1 addition & 0 deletions src/lightning/app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Root package info."""

import logging
import os

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
run_once runs your app through one cycle of the event loop and then terminates
"""

import io
import os
from contextlib import redirect_stdout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
2. call .run()
"""

from placeholdername.component import TemplateComponent


Expand Down
8 changes: 5 additions & 3 deletions src/lightning/app/cli/pl-app-template/core/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,11 @@ def _collect_logger_metadata(self, trainer: "pl.Trainer") -> None:
for logger in trainer.loggers:
metadata = {"class_name": logger.__class__.__name__}
if isinstance(logger, WandbLogger) and not logger._offline:
metadata.update(
{"username": logger.experiment.entity, "project_name": logger.name, "run_id": logger.version}
)
metadata.update({
"username": logger.experiment.entity,
"project_name": logger.name,
"run_id": logger.version,
})

if metadata and metadata not in self.work.logger_metadatas:
self.work.logger_metadatas.append(metadata)
Expand Down
Loading

0 comments on commit 273650f

Please sign in to comment.