-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
221 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 13 additions & 7 deletions
20
diffusion_models/utils/dataloaders.py → diffusion_models/utils/datasets.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,22 @@ | ||
from typing import Callable, Optional | ||
from torchvision.datasets import MNIST | ||
from torchvision.transforms import Compose, ToTensor, Normalize | ||
from typing import Callable, Optional, Tuple | ||
from torchvision.datasets import MNIST, CIFAR10 | ||
from torchvision.transforms import Compose, ToTensor, Normalize, Resize | ||
from typing import Any | ||
|
||
class MNISTTrainLoader(MNIST): | ||
class UnconditionedCifar10Dataset(CIFAR10): | ||
def __init__(self, root: str, train: bool = True, transform: Callable[..., Any] | None = None, target_transform: Callable[..., Any] | None = None, download: bool = False) -> None: | ||
transform = Compose([ToTensor(), Normalize((0.1307,), (0.3081,))]) | ||
transform = Compose([ToTensor(), Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) | ||
download = True | ||
super().__init__(root, train, transform, target_transform, download) | ||
|
||
class MNISTTrainDataset(MNIST): | ||
def __init__(self, root: str, train: bool = True, transform: Callable[..., Any] | None = None, target_transform: Callable[..., Any] | None = None, download: bool = False) -> None: | ||
transform = Compose([ToTensor(), Normalize((0.1307,), (0.3081,)), Resize((32,32))]) | ||
download = True | ||
super().__init__(root, train, transform, target_transform, download) | ||
|
||
class MNISTTestLoader(MNIST): | ||
class MNISTTestDataset(MNIST): | ||
def __init__(self, root: str, train: bool = True, transform: Callable[..., Any] | None = None, target_transform: Callable[..., Any] | None = None, download: bool = False) -> None: | ||
transform = Compose([ToTensor(), Normalize((0.1307,), (0.3081,))]) | ||
download = False | ||
download = True | ||
super().__init__(root, train, transform, target_transform, download) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
class dotdict(dict): | ||
"""dot.notation access to dictionary attributes""" | ||
__getattr__ = dict.get | ||
__getattr__ = dict.__getitem__ | ||
__setattr__ = dict.__setitem__ | ||
__delattr__ = dict.__delitem__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import context | ||
from utils.datasets import UnconditionedCifar10Dataset | ||
from torch.utils.data import DataLoader | ||
|
||
ds = UnconditionedCifar10Dataset("./data") | ||
dl = DataLoader(ds, batch_size=10) | ||
|
||
k = next(iter(dl)) | ||
print(type(k)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import context | ||
from torchvision.transforms import ToTensor, Compose, Normalize | ||
from torch.utils.data import DataLoader | ||
import torch | ||
import torch.nn as nn | ||
from models.mnist_enc import MNISTEncoder | ||
from models.unet import UNet | ||
from models.diffusion import DiffusionModel, ForwardDiffusion | ||
import numpy as np | ||
from time import time | ||
from utils.trainer import DiscriminativeTrainer, GenerativeTrainer | ||
import torch.multiprocessing as mp | ||
import os | ||
from utils.mp_setup import DDP_Proc_Group | ||
from utils.datasets import MNISTTrainDataset, UnconditionedCifar10Dataset | ||
from utils.helpers import dotdict | ||
import wandb | ||
import torch.nn.functional as F | ||
|
||
config = dotdict( | ||
total_epochs = 2, | ||
batch_size = 1000, | ||
learning_rate = 0.001, | ||
device_type = "cpu", | ||
dataset = MNISTTrainDataset, | ||
architecture = DiffusionModel, | ||
backbone = UNet, | ||
in_channels = 1, | ||
backbone_enc_depth = 4, | ||
kernel_size = 3, | ||
dropout = 0.5, | ||
forward_diff = ForwardDiffusion, | ||
max_timesteps = 1000, | ||
t_start = 0.0001, | ||
t_end = 0.02, | ||
schedule_type = "linear", | ||
time_enc_dim = 256, | ||
optimizer = torch.optim.Adam, | ||
data_path = os.path.abspath("./data"), | ||
checkpoint_folder = os.path.abspath(os.path.join("./data/checkpoints")), | ||
#data_path = "/itet-stor/peerli/net_scratch", | ||
#checkpoint_folder = "/itet-stor/peerli/net_scratch/mnist_checkpoints", | ||
save_every = 10, | ||
loss_func = F.mse_loss, | ||
log_wandb = False | ||
) | ||
|
||
backbone = UNet(4) | ||
fwd_diff = ForwardDiffusion(timesteps=1000) | ||
model = DiffusionModel(backbone, fwd_diff) | ||
|
||
def load_train_objs(config): | ||
train_set = config.dataset(config.data_path) | ||
model = config.architecture( | ||
config.backbone( | ||
num_encoding_blocks = config.backbone_enc_depth, | ||
in_channels = config.in_channels, | ||
kernel_size = config.kernel_size, | ||
dropout = config.dropout, | ||
time_emb_size = config.time_enc_dim | ||
), | ||
config.forward_diff( | ||
config.max_timesteps, | ||
config.t_start, | ||
config.t_end, | ||
config.schedule_type | ||
), | ||
config.time_enc_dim | ||
) | ||
optimizer = config.optimizer(model.parameters(), lr=config.learning_rate) | ||
return train_set, model, optimizer | ||
|
||
def training(rank, world_size, config): | ||
if (rank == 0) and (config.log_wandb): | ||
wandb.init(project="mnist_trials", config=config, save_code=True) | ||
dataset, model, optimizer = load_train_objs(config) | ||
trainer = GenerativeTrainer( | ||
model, | ||
dataset, | ||
config.loss_func, | ||
optimizer, | ||
rank, | ||
config.batch_size, | ||
config.save_every, | ||
config.checkpoint_folder, | ||
config.device_type, | ||
config.log_wandb | ||
) | ||
trainer.train(config.total_epochs) | ||
|
||
if __name__ == "__main__": | ||
if config.device_type == "cuda": | ||
world_size = torch.cuda.device_count() | ||
print("Device Count:", world_size) | ||
mp.spawn(DDP_Proc_Group(training), args=(world_size, config), nprocs=world_size) | ||
else: | ||
training(0, 0, config) |
Oops, something went wrong.