Get_y contains 2 functions, but must contain 1 (one for each target)

Last week it was working fine, not sure why now I have this error? Any idea how to create a pipeline with the following functions get_y=[attrgetter(“name”), RegexLabeller(pat=r’^(.)_\d+…$’)],?

solids = DataBlock(
    blocks=(ImageBlock, CategoryBlock), 
    get_items=get_image_files, 
    splitter=RandomSplitter(valid_pct=0.3, seed=42),
    get_y=[attrgetter("name"), RegexLabeller(pat=r'^(.*)_\d+\..*$')],
    item_tfms=Resize(128))



ValueError                                Traceback (most recent call last)
<ipython-input-79-68317656f5d7> in <module>
      4     splitter=RandomSplitter(valid_pct=0.3, seed=42),
      5     get_y=[attrgetter("name"), RegexLabeller(pat=r'^(.*)_\d+\..*$')],
----> 6     item_tfms=Resize(128))

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastcore/foundation.py in _init(self, *args, **kwargs)
    155                 if isinstance(arg,MethodType): arg = MethodType(arg.__func__, self)
    156                 setattr(self, k, arg)
--> 157         old_init(self, *args, **kwargs)
    158     functools.update_wrapper(_init, old_init)
    159     cls.__init__ = use_kwargs(cls._methods)(_init)

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/data/block.py in __init__(self, blocks, dl_type, getters, n_inp, item_tfms, batch_tfms, **kwargs)
     77             n_targs = len(self.getters) - self.n_inp
     78             if len(L(self.get_y)) != n_targs:
---> 79                 raise ValueError(f'get_y contains {len(L(self.get_y))} functions, but must contain {n_targs} (one for each target)\n{self._msg}')
     80             self.getters[self.n_inp:] = L(self.get_y)
     81 

**ValueError: get_y contains 2 functions, but must contain 1 (one for each target)**

** If you wanted to compose several transforms in your getter don’t forget to wrap them in a Pipeline.**

ValueError: get_y contains 2 functions, but must contain 1 (one for each target) If you wanted to compose several transforms in your getter don't forget to wrap them in a Pipeline.

Like the message says just wrap the get_y in a Pipeline
get_y=Pipeline([attrgetter(“name”), RegexLabeller(pat=r’^(.)_\d+…$’)]), :slight_smile:

2 Likes

Thanks @barnacl thank you so much,
I think I am still doing something wrong :sweat_smile: :persevere:

I think something was adjusted in how the get_y works to adjust for multi-input and output. Try wrapping it inside a double array.

IE:

[[attrgetter(“name”), RegexLabeller(pat=r’^(.)_\d+…$’)]]
1 Like

Still issue :frowning:

Don’t wrap it in a pipeline.

1 Like

:pleading_face: still “invalid”

1 Like

Aha doh so just realized what we’re having to do and @barnacl is correct with his approach. When it says wrap it means use parenthesis around it ()

So it should be like so (taken from the nb50):

pets = DataBlock(blocks=(ImageBlock, CategoryBlock), 
                 get_items=get_image_files, 
                 splitter=RandomSplitter(),
                 get_y=Pipeline([attrgetter("name"), RegexLabeller(pat = r'^(.*)_\d+.jpg$')]),
                 item_tfms=Resize(128),
                 batch_tfms=aug_transforms())

Notice the Pipeline() with it. Just tested on the master branch so can confirm this works :slight_smile:

1 Like

Yes, Now it is perfect. Thank you so much both

the error is ". i should have wrapped the code in back ticks :slight_smile:

2 Likes

you could replace get_y with get_y=RegexLabeller(pat = r'^.*/(.*)_\d+.jpg$'),that should work too

1 Like