I have a python function named grids() with keyword self in arguments, and another python function named build_targets(). I want to use gridsize and anchor_vec from grids() to another python function named build_targets(). Both functions are following:
def grids(self, img_size=(608,608), gridsize=(19, 19), device='cpu', type=torch.float32):
nx, ny = gridsize # x and y grid size
self.nx = nx
self.ny = ny
if isinstance(img_size, int):
self.img_size = int(img_size)
else:
self.img_size = max(img_size)
self.stride = self.img_size / max(gridsize)
yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
self.register_buffer('grid_xy', torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).to(device))
self.anchor_vec = self.anchors.to(device) / self.stride
self.register_buffer('gridsize',torch.Tensor(gridsize).to(device))
def build_targets(model, targets):
for i in layers_list:
ng, anchor_vec = gridsize, anchor_vec # from grids()
Is there any way to use gridsize and anchor_vec in build_targets() or how do I call it in build_target() function?
Any comments would be highly appreciated!
selfis not a keyword. In your code, it is merely the name of the first argument to your methods. That is the convention in Python, since this argument gets passed the instance when a method is called from an instance.grids()is not a function in a class, why do you haveselfas first argument? that does not make sense. also the callsself.nxin that method will not work because you try to reference to class variables (unless you pass an object as first argument when callinggridswhich is very unlikely). you should check out the basics to python classes. you probably want a class with both methods as functions in your class so you can write and access class variables likeanchor_vecin your functions.