from diffusers import UNet2DModel
import pickle,gzip,math,os,time,shutil,torch,random,logging
import fastcore.all as fc,matplotlib as mpl,numpy as np,matplotlib.pyplot as plt
from collections.abc import Mapping
from pathlib import Path
from functools import partial
from fastcore.foundation import L
import torchvision.transforms.functional as TF,torch.nn.functional as F
from torch import tensor,nn,optim
from torch.utils.data import DataLoader,default_collate
from torch.nn import init
from torch.optim import lr_scheduler
from fastAIcourse.datasets import *
from fastAIcourse.conv import *
from fastAIcourse.learner import *
from fastAIcourse.activations import *
from fastAIcourse.init import *
from fastAIcourse.sgd import *
from fastAIcourse.resnet import *
from fastAIcourse.augment import *
from fastAIcourse.accel import *
CIFAR 10 image classifications
CIFAR 10 image classifications
from torcheval.metrics import MulticlassAccuracy
from datasets import load_dataset,load_dataset_builder
'image.cmap'] = 'gray_r'
mpl.rcParams[ logging.disable(logging.WARNING)
= 'img','label'
xl,yl = "cifar10"
name = load_dataset(name)
dsd
@inplace
def transformi(b): b[xl] = [TF.to_tensor(o)-0.5 for o in b[xl]]
= 32
bs = dsd.with_transform(transformi)
tds = DataLoaders.from_dd(tds, bs, num_workers=8) dls
= dls.train
dt = next(iter(dt)) xb,yb
xb.shape
torch.Size([32, 3, 32, 32])
25]+0.5) show_images(xb[:
from types import SimpleNamespace
def linear_sched(betamin=0.0001,betamax=0.02,n_steps=1000):
= torch.linspace(betamin, betamax, n_steps)
beta return SimpleNamespace(a=1.-beta, abar=(1.-beta).cumprod(dim=0), sig=beta.sqrt())
= 1000
n_steps = linear_sched(betamax=0.01)
lin_abar = lin_abar.abar
alphabar = lin_abar.a
alpha = lin_abar.sig sigma
def noisify(x0, αΎ±):
= x0.device
device = len(x0)
n = torch.randint(0, n_steps, (n,), dtype=torch.long)
t = torch.randn(x0.shape, device=device)
Ξ΅ = αΎ±[t].reshape(-1, 1, 1, 1).to(device)
αΎ±_t = αΎ±_t.sqrt()*x0 + (1-αΎ±_t).sqrt()*Ξ΅
xt return (xt, t.to(device)), Ξ΅
= noisify(xb[:25],alphabar)
(xt,t),Ξ΅ t
tensor([ 26, 335, 620, 924, 950, 113, 378, 14, 210, 954, 231, 572, 315, 295, 567, 706, 749, 876, 73, 111, 899, 213, 541, 769, 287])
= fc.map_ex(t[:25], '{}')
titles 25].clip(-0.5, 0.5) + 0.5, imsize=1.5, titles=titles) show_images(xt[:
Training
class UNet(UNet2DModel):
def forward(self, x): return super().forward(*x).sample
def init_ddpm(model):
for o in model.down_blocks:
for p in o.resnets:
p.conv2.weight.data.zero_()for p in fc.L(o.downsamplers): init.orthogonal_(p.conv.weight)
for o in model.up_blocks:
for p in o.resnets: p.conv2.weight.data.zero_()
model.conv_out.weight.data.zero_()
def collate_ddpm(b): return noisify(default_collate(b)[xl], alphabar)
def dl_ddpm(ds, nw=4): return DataLoader(ds, batch_size=bs, collate_fn=collate_ddpm, num_workers=nw)
= DataLoaders(dl_ddpm(tds['train']), dl_ddpm(tds['test'])) dls
# The model we've been using for FashionMNIST
= UNet(in_channels=3, out_channels=3, block_out_channels=(32, 64, 128, 256), norm_num_groups=8)
model sum(p.numel() for p in model.parameters())
15891907
# The default is a much larger model:
= UNet(in_channels=3, out_channels=3)
model sum(p.numel() for p in model.parameters())
274056163
# Free up some memory clean_mem()
= 1e-3
lr = 1
epochs = partial(optim.AdamW, eps=1e-5)
opt_func = epochs * len(dls.train)
tmax = partial(lr_scheduler.OneCycleLR, max_lr=lr, total_steps=tmax)
sched = [DeviceCB(), MixedPrecision(), ProgressCB(plot=True), MetricsCB(), BatchSchedCB(sched)]
cbs = UNet(in_channels=3, out_channels=3)
model
init_ddpm(model)= Learner(model, dls, nn.MSELoss(), lr=lr, cbs=cbs, opt_func=opt_func) learn
learn.fit(epochs)
from tqdm import tqdm
@torch.no_grad()
def sample(model, sz):
= next(model.parameters())
ps = torch.randn(sz).to(ps)
x_t = []
preds for t in reversed(tqdm(range(n_steps))):
= torch.full((x_t.shape[0],), t, device=ps.device, dtype=torch.long)
t_batch = (torch.randn(x_t.shape) if t > 0 else torch.zeros(x_t.shape)).to(ps)
z = alphabar[t-1] if t > 0 else torch.tensor(1)
αΎ±_t1 = 1-alphabar[t]
bΜ_t = 1-αΎ±_t1
bΜ_t1 = model((x_t, t_batch))
noise = ((x_t - bΜ_t.sqrt() * noise)/alphabar[t].sqrt())
x_0_hat = x_0_hat * αΎ±_t1.sqrt()*(1-alpha[t])/bΜ_t + x_t * alpha[t].sqrt()*bΜ_t1/bΜ_t + sigma[t]*z
x_t float().cpu())
preds.append(x_t.return preds
= sample(model, (bs, 3, 32, 32)) samples
= (samples[-1] + 0.5).clamp(0,1)
s 16], imsize=1.5) show_images(s[:
W&B CB
import wandb
class WandBCB(MetricsCB):
=100
orderdef __init__(self, config, *ms, project='ddpm_cifar10', **metrics):
fc.store_attr()super().__init__(*ms, **metrics)
def before_fit(self, learn): wandb.init(project=self.project, config=self.config)
def after_fit(self, learn): wandb.finish()
def _log(self, d):
if self.train:
'train_'+m:float(d[m]) for m in self.all_metrics})
wandb.log({else:
'val_'+m:float(d[m]) for m in self.all_metrics})
wandb.log({'samples':self.sample_figure(learn)})
wandb.log({print(d)
def sample_figure(self, learn):
with torch.no_grad():
= sample(learn.model, (16, 3, 32, 32))
samples = (samples[-1] + 0.5).clamp(0,1)
s
plt.clf()= get_grid(16)
fig, axs for im,ax in zip(s[:16], axs.flat): show_image(im, ax=ax)
return fig
def after_batch(self, learn):
super().after_batch(learn)
'loss':learn.loss}) wandb.log({
= 1e-3
lr = 10
epochs = partial(optim.AdamW, eps=1e-5)
opt_func = epochs * len(dls.train)
tmax = partial(lr_scheduler.OneCycleLR, max_lr=lr, total_steps=tmax)
sched = WandBCB(config={'lr':lr, 'epochs':epochs, 'comments':'default unet logging test'})
wandbcb = [DeviceCB(), MixedPrecision(), ProgressCB(plot=True), wandbcb, BatchSchedCB(sched)]
cbs = model = UNet(in_channels=3, out_channels=3)
model
init_ddpm(model)= Learner(model, dls, nn.MSELoss(), lr=lr, cbs=cbs, opt_func=opt_func) learn
learn.fit(epochs)
Failed to detect the name of this notebook, you can set it manually with the WANDB_NOTEBOOK_NAME environment variable to enable code saving.
wandb: Currently logged in as: johnowhitaker. Use `wandb login --relogin` to force relogin
wandb version 0.13.9 is available! To upgrade, please run:
$ pip install wandb --upgrade
Tracking run with wandb version 0.13.3
Run data is saved locally in
/home/ubuntu/new_course22p2_folder/nbs/wandb/run-20230119_052202-1jgoyqoq
{'loss': '0.062', 'epoch': 0, 'train': 'train'}
{'loss': '0.029', 'epoch': 0, 'train': 'eval'}
{'loss': '0.028', 'epoch': 1, 'train': 'train'}
{'loss': '0.028', 'epoch': 1, 'train': 'eval'}
{'loss': '0.027', 'epoch': 2, 'train': 'train'}
{'loss': '0.028', 'epoch': 2, 'train': 'eval'}
{'loss': '0.026', 'epoch': 3, 'train': 'train'}
{'loss': '0.026', 'epoch': 3, 'train': 'eval'}
{'loss': '0.026', 'epoch': 4, 'train': 'train'}
{'loss': '0.026', 'epoch': 4, 'train': 'eval'}
{'loss': '0.025', 'epoch': 5, 'train': 'train'}
{'loss': '0.025', 'epoch': 5, 'train': 'eval'}
{'loss': '0.025', 'epoch': 6, 'train': 'train'}
{'loss': '0.024', 'epoch': 6, 'train': 'eval'}
{'loss': '0.024', 'epoch': 7, 'train': 'train'}
{'loss': '0.024', 'epoch': 7, 'train': 'eval'}
{'loss': '0.024', 'epoch': 8, 'train': 'train'}
{'loss': '0.025', 'epoch': 8, 'train': 'eval'}
{'loss': '0.024', 'epoch': 9, 'train': 'train'}
{'loss': '0.024', 'epoch': 9, 'train': 'eval'}
Waiting for W&B process to finish... (success).
Run history:
loss | ββ ββ ββββββ βββ β ββββββ β ββββ βββββββ ββββββββ |
train_loss | ββββββββββ |
val_loss | ββββββββββ |
Run summary:
loss | 0.01746 |
train_loss | 0.024 |
val_loss | 0.024 |
Synced serene-wildflower-15: https://wandb.ai/johnowhitaker/ddpm_cifar10/runs/1jgoyqoq
Synced 6 W&B file(s), 10 media file(s), 0 artifact file(s) and 0 other file(s)
Synced 6 W&B file(s), 10 media file(s), 0 artifact file(s) and 0 other file(s)
Find logs at:
./wandb/run-20230119_052202-1jgoyqoq/logs
<Figure size 640x480 with 0 Axes>
<Figure size 1200x1200 with 0 Axes>
<Figure size 1200x1200 with 0 Axes>
<Figure size 1200x1200 with 0 Axes>
<Figure size 1200x1200 with 0 Axes>
<Figure size 1200x1200 with 0 Axes>
<Figure size 1200x1200 with 0 Axes>
<Figure size 1200x1200 with 0 Axes>
<Figure size 1200x1200 with 0 Axes>
<Figure size 1200x1200 with 0 Axes>