From 71a772cbfef55952f4cf7e3f0bf8c2bdb606af23 Mon Sep 17 00:00:00 2001 From: mikigom Date: Thu, 7 Mar 2019 14:29:05 +0900 Subject: [PATCH 1/4] Add class SingletonInstance --- MNIST-pytorch/util.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/MNIST-pytorch/util.py b/MNIST-pytorch/util.py index f6eabea..17fb473 100644 --- a/MNIST-pytorch/util.py +++ b/MNIST-pytorch/util.py @@ -62,3 +62,10 @@ def meanVar(self,opt,mean,var): self.vis.image(var[0].clip(0,1),win="{0}_varinit".format(opt.model), opts={ "title": "{0} (TEST_var_init)".format(opt.model) }) self.vis.image(var[1].clip(0,1),win="{0}_varwarped".format(opt.model), opts={ "title": "{0} (TEST_var_warped)".format(opt.model) }) +class SingletonInstance(type): + _instance = None + + def __call__(cls, *args, **kwargs): + if not cls._instance: + cls._instance = super(SingletonInstance, cls).__call__(*args, **kwargs) + return cls._instance From 851cace25039af086db6fc2ec055734b784dcdf5 Mon Sep 17 00:00:00 2001 From: mikigom Date: Thu, 7 Mar 2019 14:32:24 +0900 Subject: [PATCH 2/4] Add class WarpParameters --- MNIST-pytorch/warp.py | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/MNIST-pytorch/warp.py b/MNIST-pytorch/warp.py index c3730fc..f27e1d1 100644 --- a/MNIST-pytorch/warp.py +++ b/MNIST-pytorch/warp.py @@ -4,6 +4,24 @@ import util + +class WarpParameters(metaclass=SingletonInstance): + def __init__(self, opt): + refMtrx = torch.from_numpy(opt.refMtrx).cuda() + self.refMtrx = refMtrx.repeat(opt.batchSize, 1, 1) + + # warp the canonical coordinates + X, Y = np.meshgrid(np.linspace(-1, 1, opt.W), np.linspace(-1, 1, opt.H)) + X, Y = X.flatten(), Y.flatten() + XYhom = np.stack([X, Y, np.ones_like(X)], axis=1).T + XYhom = np.tile(XYhom, [opt.batchSize, 1, 1]).astype(np.float32) + XYhom = torch.from_numpy(XYhom).cuda() + self.XYhom = XYhom + + self.O = torch.zeros(opt.batchSize, dtype=torch.float32).cuda() + self.I = torch.ones(opt.batchSize, dtype=torch.float32).cuda() + + # fit (affine) warp between two sets of points def fit(Xsrc,Xdst): ptsN = len(Xsrc) @@ -33,8 +51,9 @@ def inverse(opt,p): # convert warp parameters to matrix def vec2mtrx(opt,p): - O = torch.zeros(opt.batchSize,dtype=torch.float32).cuda() - I = torch.ones(opt.batchSize,dtype=torch.float32).cuda() + warp_parameters = WarpParameters(opt) + O = warp_parameters.O + I = warp_parameters.I if opt.warpType=="translation": tx,ty = torch.unbind(p,dim=1) pMtrx = torch.stack([torch.stack([I,O,tx],dim=-1), @@ -71,16 +90,10 @@ def mtrx2vec(opt,pMtrx): # warp the image def transformImage(opt,image,pMtrx): - refMtrx = torch.from_numpy(opt.refMtrx).cuda() - refMtrx = refMtrx.repeat(opt.batchSize,1,1) - transMtrx = refMtrx.matmul(pMtrx) - # warp the canonical coordinates - X,Y = np.meshgrid(np.linspace(-1,1,opt.W),np.linspace(-1,1,opt.H)) - X,Y = X.flatten(),Y.flatten() - XYhom = np.stack([X,Y,np.ones_like(X)],axis=1).T - XYhom = np.tile(XYhom,[opt.batchSize,1,1]).astype(np.float32) - XYhom = torch.from_numpy(XYhom).cuda() - XYwarpHom = transMtrx.matmul(XYhom) + warp_parameters = WarpParameters(opt) + transMtrx = warp_parameters.refMtrx.matmul(pMtrx) + XYwarpHom = transMtrx.matmul(warp_parameters.XYhom) + XwarpHom,YwarpHom,ZwarpHom = torch.unbind(XYwarpHom,dim=1) Xwarp = (XwarpHom/(ZwarpHom+1e-8)).reshape(opt.batchSize,opt.H,opt.W) Ywarp = (YwarpHom/(ZwarpHom+1e-8)).reshape(opt.batchSize,opt.H,opt.W) From a1ea0c916301b58402bcde90fa5ca7e1897a7825 Mon Sep 17 00:00:00 2001 From: mikigom Date: Thu, 7 Mar 2019 14:34:07 +0900 Subject: [PATCH 3/4] Fix typo --- MNIST-pytorch/warp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MNIST-pytorch/warp.py b/MNIST-pytorch/warp.py index f27e1d1..9aa0b86 100644 --- a/MNIST-pytorch/warp.py +++ b/MNIST-pytorch/warp.py @@ -5,7 +5,7 @@ import util -class WarpParameters(metaclass=SingletonInstance): +class WarpParameters(metaclass=util.SingletonInstance): def __init__(self, opt): refMtrx = torch.from_numpy(opt.refMtrx).cuda() self.refMtrx = refMtrx.repeat(opt.batchSize, 1, 1) From 5282c28a2c2bef7b7a74cd5ae37fbdafec69d29b Mon Sep 17 00:00:00 2001 From: mikigom Date: Thu, 7 Mar 2019 14:41:05 +0900 Subject: [PATCH 4/4] Tensor.data operation is not required --- MNIST-pytorch/data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MNIST-pytorch/data.py b/MNIST-pytorch/data.py index be43034..eaf3b18 100644 --- a/MNIST-pytorch/data.py +++ b/MNIST-pytorch/data.py @@ -81,7 +81,7 @@ def evalTest(opt,data,geometric,classifier): # make training batch image = data["image"][idx].cuda() label = data["label"][idx].cuda() - image.data.unsqueeze_(dim=1) + image.unsqueeze_(dim=1) # generate perturbation pInit = genPerturbations(opt) pInitMtrx = warp.vec2mtrx(opt,pInit)