forked from rosinality/vq-vae-2-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_pixelsnail.py
executable file
·143 lines (111 loc) · 3.95 KB
/
train_pixelsnail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import argparse
import numpy as np
import torch
from torch import nn, optim
from torch.utils.data import DataLoader
from tqdm import tqdm
from dataset import LMDBDataset
from pixelsnail import PixelSNAIL
from scheduler import CycleScheduler
def train(args, epoch, loader, model, optimizer, scheduler, device):
loader = tqdm(loader)
criterion = nn.CrossEntropyLoss()
for i, (top, bottom, label) in enumerate(loader):
model.zero_grad()
top = top.to(device)
if args.hier == 'top':
target = top
out, _ = model(top)
elif args.hier == 'bottom':
bottom = bottom.to(device)
target = bottom
out, _ = model(bottom, condition=top)
loss = criterion(out, target)
loss.backward()
if scheduler is not None:
scheduler.step()
optimizer.step()
_, pred = out.max(1)
correct = (pred == target).float()
accuracy = correct.sum() / target.numel()
lr = optimizer.param_groups[0]['lr']
loader.set_description(
(
f'epoch: {epoch + 1}; loss: {loss.item():.5f}; '
f'acc: {accuracy:.5f}; lr: {lr:.5f}'
)
)
class PixelTransform:
def __init__(self):
pass
def __call__(self, input):
ar = np.array(input)
return torch.from_numpy(ar).long()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--batch', type=int, default=32)
parser.add_argument('--epoch', type=int, default=420)
parser.add_argument('--hier', type=str, default='top')
parser.add_argument('--lr', type=float, default=3e-4)
parser.add_argument('--channel', type=int, default=256)
parser.add_argument('--n_res_block', type=int, default=4)
parser.add_argument('--n_res_channel', type=int, default=256)
parser.add_argument('--n_out_res_block', type=int, default=0)
parser.add_argument('--n_cond_res_block', type=int, default=3)
parser.add_argument('--dropout', type=float, default=0.1)
parser.add_argument('--sched', type=str)
parser.add_argument('--ckpt', type=str)
parser.add_argument('path', type=str)
args = parser.parse_args()
print(args)
device = 'cuda'
dataset = LMDBDataset(args.path)
loader = DataLoader(
dataset, batch_size=args.batch, shuffle=True, num_workers=4, drop_last=True
)
ckpt = {}
if args.ckpt is not None:
ckpt = torch.load(args.ckpt)
args = ckpt['args']
if args.hier == 'top':
model = PixelSNAIL(
[32, 32],
512,
args.channel,
5,
4,
args.n_res_block,
args.n_res_channel,
dropout=args.dropout,
n_out_res_block=args.n_out_res_block,
)
elif args.hier == 'bottom':
model = PixelSNAIL(
[64, 64],
512,
args.channel,
5,
4,
args.n_res_block,
args.n_res_channel,
attention=False,
dropout=args.dropout,
n_cond_res_block=args.n_cond_res_block,
cond_res_channel=args.n_res_channel,
)
if 'model' in ckpt:
model.load_state_dict(ckpt['model'])
model = nn.DataParallel(model)
model = model.to(device)
optimizer = optim.Adam(model.parameters(), lr=args.lr)
scheduler = None
if args.sched == 'cycle':
scheduler = CycleScheduler(
optimizer, args.lr, n_iter=len(loader) * args.epoch, momentum=None
)
for i in range(args.epoch):
train(args, i, loader, model, optimizer, scheduler, device)
torch.save(
{'model': model.module.state_dict(), 'args': args},
f'checkpoint/pixelsnail_{args.hier}_{str(i + 1).zfill(3)}.pt',
)