This repository has been archived by the owner on Nov 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_coarse.py
185 lines (130 loc) · 6.57 KB
/
train_coarse.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
print('Loading Libraries...')
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torch.nn.functional as F
import os
from PIL import Image
import numpy as np
import time
from tqdm import tqdm
from dali_dataloader import AugmentationPipeline, ImageOpener, MyCustomDataloader
from nvidia.dali.plugin.pytorch import DALIGenericIterator
from kornia.filters import sobel
from coarse_definition import CoarseMatteGenerator
from train_utils import *
device = 'cuda'
#PSEUDOCODE
#Given a directory full of directories full of HD short clips rendered out as PNG sequences with alpha channels
#Given a directory full of directories full of HD short clips rendered out as PNG sequences of different possible backgrounds
#Given a defined model architecture with any transfer learning set up in the __init__() function
#Given optimizer(s) for the model
#LOOP some number of training examples.
#Generate INPUT FRAME PACKET using Dataset and DataLoader
#ZERO GRADIENTS.
#FEED FORWARD the input frame packet into the MODEL.
#COMPUTE LOSS(ES) using ALPHA FRAME PACKET and model OUTPUTS, with the accuracy of the middle (target) frame weighted much more than those surrounding it.
#COMPUTE GRADIENT(S)
#STEP OPTIMIZERS ONCE
#Once training loop has completed, SAVE STATE DICT of model to drive.
print('Initializing Dataset...')
dataset_params = {
'bg_dir':'train_set_2/backgrounds/',
'fg_dir':'train_set_2/foregrounds/',
}
batch_size = 3
#Initialize the dataset and the loader to feed the data to the network.
ImageFeeder = ImageOpener(\
fg_dir = 'dataset/train/fgr/', \
bg_dir = 'dataset/train/bgr/', \
alpha_dir = 'dataset/train/pha/',
batch_size = batch_size)
Pipeline = AugmentationPipeline(\
dataset = ImageFeeder, \
num_threads = 4, \
device_id = 0,
batch_size = batch_size)
Pipeline.build()
Loader = DALIGenericIterator(pipelines = [Pipeline], output_map = ['fg', 'bg', 'bprime', 'alpha'])
DALIDataloader = MyCustomDataloader(Loader)
print('Initializing Network...')
#Initialize the network which will produce a coarse alpha (1 chan), foreground (3 chan) confidence map (1 chan), and a number of hidden channels (num_hidden_channels chan)...
coarse = CoarseMatteGenerator().train().to(device)
use_amp = False
#track how many batches have been done for things like periodic outputs and eventually scheduling.
iteration = 0
coarse_parameters = [\
{'params': coarse.Encoder.parameters(), 'lr': 0.0001},\
{'params': coarse.ASPP.parameters(), 'lr': 0.0005},\
{'params': coarse.Decoder.parameters(), 'lr': 0.0005}\
]
coarse_opt = torch.optim.Adam(coarse_parameters, lr = 0.0001)
print('\nTraining...')
L1Loss = nn.L1Loss()
MSELoss = nn.MSELoss()
for iteration in tqdm(range(600000)):
with torch.cuda.amp.autocast(enabled = use_amp):
real_background, real_foreground, real_bprime, real_alpha = next(DALIDataloader)
"""
real_foreground = real_foreground.to(device)
real_background = real_background.to(device)
real_alpha = real_alpha.to(device)
real_bprime = real_bprime.to(device)
"""
#Composite the augmented foreground onto the augmented background according to the augmented alpha.
composite_tensor = composite(real_background, real_foreground, real_alpha)
#return the input tensor (composite plus b-prime) and the alpha_tensor. The input tensor is just a bunch of channels, the real_alpha is the central (singular) alpha
#corresponding to the target frame.
input_tensor = torch.cat([composite_tensor, real_bprime], 1)
coarse_input = F.interpolate(input_tensor, size = [input_tensor.shape[-2]//4, input_tensor.shape[-1]//4])
#Get a downsampled version of the alpha for grading the coarse network on
real_coarse_alpha = F.interpolate(real_alpha, size = [real_alpha.shape[-2]//4, real_alpha.shape[-1]//4])
#Generate a fake coarse alpha, along with a guessed error map and some hidden channel data. Oh yeah and the foreground residual
fake_coarse = coarse(coarse_input)
fake_coarse_alpha = torch.clamp(fake_coarse[:, 0:1], 0, 1)
fake_coarse_foreground_residual = fake_coarse[:, 1:4]
fake_coarse_error = torch.clamp(fake_coarse[:, 4:5], 0, 1)
fake_coarse_hidden_channels = torch.relu(fake_coarse[:,5:])
real_coarse_composite = F.interpolate(composite_tensor, size = [composite_tensor.shape[-2]//4, composite_tensor.shape[-1]//4])
fake_coarse_foreground = torch.clamp(real_coarse_composite + fake_coarse_foreground_residual, 0, 1)
#The real error map is calculated as the squared difference between the real alpha and the fake alpha.
real_coarse_error = torch.clamp(torch.abs(real_coarse_alpha.detach()-fake_coarse_alpha.detach()), 0, 1)
#construct the fake foreground
#fake_coarse_foreground = torch.clamp(real_coarse_composite[:, dataset_params["comp_context_depth"]*3:dataset_params["comp_context_depth"]*3 + 3] + fake_coarse_foreground_residual, 0, 1)
foreground_penalty_zone = real_coarse_alpha > 0
real_coarse_foreground = F.interpolate(real_foreground, size = [real_foreground.shape[-2]//4, real_foreground.shape[-1]//4])
coarse_sobel = sobel(fake_coarse_alpha)
real_sobel = sobel(real_coarse_alpha)
#The loss of the coarse network is L1 loss of coarse alpha, L1 loss of coarse error, and L1 loss (only where real_alpha >0.1) of coarse foreground.
coarse_loss = \
L1Loss(fake_coarse_alpha,real_coarse_alpha) + \
MSELoss(fake_coarse_error,real_coarse_error) + \
L1Loss((real_coarse_foreground * foreground_penalty_zone), (fake_coarse_foreground * foreground_penalty_zone)) + \
L1Loss(coarse_sobel,real_sobel)
#if it's before the training cutoff, then the loss is just for the coarse network.
coarse_opt.zero_grad()
coarse_loss.backward()
coarse_opt.step()
#For keeping track of the outputs so I can look through them to see the network is working right.
iteration += 1
if(iteration % 1000 == 0):
image = fake_coarse_alpha[0]
image = transforms.ToPILImage()(image)
image.save(f'outputs7/{iteration}C_fake_coarse_alpha.jpg')
image = real_coarse_alpha[0]
image = transforms.ToPILImage()(image)
image.save(f'outputs7/{iteration}B_real_alpha.jpg')
image = fake_coarse_foreground[0]
image = transforms.ToPILImage()(image)
image.save(f'outputs7/{iteration}A_fake_foreground.jpg')
image = fake_coarse_error[0]
image = transforms.ToPILImage()(image)
image.save(f'outputs7/{iteration}D_fake_error.jpg')
image = real_coarse_composite[0]
image = transforms.ToPILImage()(image)
image.save(f'outputs7/{iteration}E_coarse_composite.jpg')
if(iteration % 1000 == 0):
print(coarse_loss)
if(iteration % 15000 == 0):
torch.save(coarse, f"./model_saves/proper_coarse_epoch{iteration}.zip")
print('\nTraining completed successfully.')