DL-Art-School/codes/models/steps/tecogan_injectors.py

41 lines
1.7 KiB
Python
Raw Normal View History

2020-09-20 22:24:23 +00:00
import models.steps.injectors as injectors
2020-09-25 22:38:23 +00:00
import torch
2020-09-20 22:24:23 +00:00
# Uses a generator to synthesize a sequence of images from [in] and injects the results into a list [out]
2020-09-25 22:38:23 +00:00
# Images are fed in sequentially forward and back, resulting in len([out])=2*len([in])-1 (last element is not repeated).
# All computation is done with torch.no_grad().
2020-09-20 22:24:23 +00:00
class RecurrentImageGeneratorSequenceInjector(injectors.Injector):
def __init__(self, opt, env):
super(RecurrentImageGeneratorSequenceInjector, self).__init__(opt, env)
def forward(self, state):
gen = self.env['generators'][self.opt['generator']]
results = []
2020-09-25 22:38:23 +00:00
with torch.no_grad():
recurrent_input = torch.zeros_like(state[self.input][0])
# Go forward in the sequence first.
for input in state[self.input]:
recurrent_input = gen(input, recurrent_input)
results.append(recurrent_input)
2020-09-20 22:24:23 +00:00
2020-09-25 22:38:23 +00:00
# Now go backwards, skipping the last element (it's already stored in recurrent_input)
it = reversed(range(len(results) - 1))
for i in it:
recurrent_input = gen(results[i], recurrent_input)
results.append(recurrent_input)
new_state = {self.output: results}
2020-09-20 22:24:23 +00:00
return new_state
class ImageFlowInjector(injectors.Injector):
def __init__(self, opt, env):
# Requires building this custom cuda kernel. Only require it if explicitly needed.
from models.networks.layers.resample2d_package.resample2d import Resample2d
super(ImageFlowInjector, self).__init__(opt, env)
self.resample = Resample2d()
def forward(self, state):
return self.resample(state[self.opt['in']], state[self.opt['flow']])