Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20% Speed-up by Singleton Parameter Class in PyTorch #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MNIST-pytorch/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions MNIST-pytorch/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 25 additions & 12 deletions MNIST-pytorch/warp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@

import util


class WarpParameters(metaclass=util.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)
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down