Basic functions and classes usful to reproduce research papers.
def explode_types(o):
'''Like fastcore explode_types, but only shows __name__ of type.'''
if not is_listy(o): return type(o).__name__
return {type(o).__name__: [explode_types(o_) for o_ in o]}
def explode_lens(o):
if is_listy(o):
if all(is_listy(o_) for o_ in o):
return [explode_lens(o_) for o_ in o]
else: return len(o)
test_eq(explode_lens([[1,4], [[5,6,7], [1]]]), [2, [3, 1]])
def explode_shapes(o):
if not is_listy(o): return tuple(bind(getattr, arg0, 'shape')(o))
return [explode_shapes(o_) for o_ in o]
test_eq(explode_shapes([tensor([1,4]), [tensor([[4,5],[7,8]]), tensor([6])]]), [(2,), [(2,2), (1,)]])
def explode_ranges(o):
if not is_listy(o): return (float(o.min()), float(o.max()))
return [explode_ranges(o_) for o_ in o]
explode_ranges([tensor([1,4]), [tensor([[4,5],[7,8]]), tensor([6])]])
def pexpt(o): print(explode_types(o))
def pexpl(o): print(explode_lens(o))
def pexps(o): print(explode_shapes(o))
path = untar_data(URLs.PETS)
files = get_image_files(path/"images")
ImageNTuple will only show tha images if all of them are of same size, and if all of them are tensors.
imt2 = ImageNTuple.create((files[0], files[1]))
explode_types(imt2)
imt2 = ImageNTuple.create((files[0], files[1]))
imt2 = Resize(224)(imt2)
imt2 = ToTensor()(imt2)
imt2.show();
imt3 = ImageNTuple.create((files[0], files[1], files[2]))
imt3 = Resize(224)(imt3)
imt3 = ToTensor()(imt3)
ax = imt3.show()
test_eq(len(imt2), 2)
test_eq(len(imt3), 3)
Test that the ConditionalGenerator can generate images from one image
gen_base = basic_critic(32, 3)
gen_base = nn.Sequential(*list(gen_base.children())[:-2])
unet = DynamicUnet(gen_base, 3, (32, 32))
gen = ConditionalGenerator(unet)
out = gen(torch.rand(1, 3, 32, 32))
test_eq(explode_shapes(out), [(1, 3, 32, 32), (1, 3, 32, 32)])
test_eq(type(out), ImageNTuple)
Test that the ConditionalGenerator can generate an image from two images
gen_base = basic_critic(32, 6)
gen_base = nn.Sequential(*list(gen_base.children())[:-2])
unet = DynamicUnet(gen_base, 3, (32, 32))
gen = ConditionalGenerator(unet)
dl = DataLoader(dataset=([imt2]), bs=1, after_item=IntToFloatTensor())
b = first(dl)
out = gen(b)
test_eq(type(out), ImageNTuple)
test_eq(len(out), 3)
critic = gan_critic(n_channels=6, nf=64)
scritic = SiameseCritic(critic)
To export a GANLearner
we need to set the learner into gen_mode
and before recreating the optimization function we should call set_freeze_model
to unfreeze all the parameters in the model so the old opt state can be loaded properly.
# @patch
# def predict(self:GANLearner, item, rm_type_tfms=None, with_input=False):
# dl = self.dls.test_dl([item], rm_type_tfms=rm_type_tfms, num_workers=0)
# inp,preds,_,dec_preds = self.get_preds(dl=dl, with_input=True, with_decoded=True)
# i = getattr(self.dls, 'n_inp', -1)
# inp = (inp,) if i==1 else tuplify(inp)
# n_out = len(self.dls.tls) - i
# dec_preds = (dec_preds,) if n_out==1 else tuplify(dec_preds)
# dec = self.dls.decode_batch(inp + dec_preds)[0]
# dec_inp,dec_targ = map(detuplify, [dec[:i],dec[i:]])
# res = dec_targ,dec_preds[0],preds[0]
# if with_input: res = (dec_inp,) + res
# return res
# def tuplify(o:ImageNTuple, *kwargs): return (o,)
Original Inception's weights for FID by mseitzer
renorm_stats = (2*torch.tensor(imagenet_stats[0])-1).tolist(), (2*torch.tensor(imagenet_stats[1])).tolist()
x = TensorImage(torch.rand(1, 3, 32, 32))
xn = Normalize(0.5, 0.5)(x)
xin = Normalize.from_stats(*imagenet_stats, cuda=False)(x)
xn2in = Normalize.from_stats(*renorm_stats, cuda=False)(xn)
test(x, xn, nequals)
test(x, xin, nequals)
test(xin, xn, nequals)
test(xin, xn2in, all_equal)
path_base = untar_data(URLs.FACADES_BASE)
item_tfms=Resize(286, ResizeMethod.Squish, resamples=(Image.NEAREST, Image.NEAREST)),
batch_tfms=[Normalize.from_stats(0.5*torch.ones(3), 0.5*torch.ones(3)),
*aug_transforms(size=256, mult=0.0, max_lighting=0, p_lighting=0, mode='nearest')]
dls = CGANDataLoaders.from_path_ext(path_base.parent, ['base', 'extended'], item_tfms=item_tfms, batch_tfms=batch_tfms, bs=1)
dls.show_batch()
item_tfms=Resize(286),
dls = CGANDataLoaders.from_paths('coco_under_water', 'coco', train='test2017', valid='val2017',
item_tfms=item_tfms, bs=16, num_workers=16, n_inp=-1)
dls.show_batch(max_n=2)
# def basic_nested_repr(flds=None):
# if isinstance(flds, str): flds = re.split(', *', flds)
# flds = list(flds or [])
# def _f(self):
# sig = ', '.join(f'{o}={maybe_attr(nested_attr(self,o), "__name__")}' for o in flds)
# return f'{self.__class__.__name__}({sig})'
# return _f
learn = synth_learner()
gl = GatherLogs()
with learn.added_cbs(gl):
#gl.set_experiment_name('test1')
learn.fit(2)
gl.df
with learn.added_cbs(gl):
gl.set_experiment_name('test2')
learn.fit(2)
gl.df
gl.plot_metric();
gl.plot_time();
learn = synth_learner()
with learn.added_cbs(RunNBatches()):
learn.fit(5)
with learn.added_cbs(RunNBatches(n=1, no_valid=False)):
learn.fit(5)