Reputation: 85
I try to use the @
in the class method. like this
class Dataset:
@parse_func
def get_next_batch(self):
return self.generator.__next__()
and the parse function like this:
def parse_func(load_batch):
def wrapper(**para):
batch_files_path, batch_masks_path, batch_label = load_batch(**para)
batch_images = []
batch_masks = []
for (file_path, mask_path) in zip(batch_files_path, batch_masks_path):
image = cv2.imread(file_path)
mask = cv2.imread(mask_path)
batch_images.append(image)
batch_masks.append(mask)
return np.asarray(batch_images, np.float32), np.asarray(batch_masks, np.uint8), batch_label
return wrapper
However, when I call dataset.get_next_batch()
, it will raise a exception
as followed.
Traceback (most recent call last): TypeError: wrapper() takes exactly 0 arguments (1 given)
Do you know why raise this error and any solution? Thank you very much!
Upvotes: 0
Views: 90
Reputation: 29071
The function wrapper(**kwargs)
accepts named arguments only. However, in instance methods, the self
is automatically passed as the first positional argument. Since your method does not accept positional arguments, it fails.
You could edit to wrapper(self, **kwargs)
or, more general wrapper(*args, **kwargs)
. However, the way you are using it, it is not clear what those arguments are.
Upvotes: 1
Reputation: 675
Just simply change
def parse_func(load_batch):
def wrapper(*para):
batch_files_path, batch_masks_path, batch_label = load_batch(*para)
batch_images = []
batch_masks = []
for (file_path, mask_path) in zip(batch_files_path, batch_masks_path):
image = cv2.imread(file_path)
mask = cv2.imread(mask_path)
batch_images.append(image)
batch_masks.append(mask)
return np.asarray(batch_images, np.float32), np.asarray(batch_masks, np.uint8), batch_label
return wrapper()
@
symbol mean a decorator function. Here, it means parse_func(get_next_batch)
. So if the wrapper using the keyword params (**para
), you just want to pass some params to the wrapper but you don't actually except for the self
args. So here I replace the params to positional params *para
.
Upvotes: 0