[v3 CycleGAN Notebook] maximum recursion depth exceeded while calling a Python object

I am trying to run v3 CycleGAN notebook and got into the following error when inspecting databunch object, could anyone advise how I should solve this bug?

>> data.show_batch(rows=2)
>> data.train_ds
---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
/userhome/31/h3509807/anaconda3/envs/fastai-2020/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

/userhome/31/h3509807/anaconda3/envs/fastai-2020/lib/python3.6/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    397                         if cls is not object \
    398                                 and callable(cls.__dict__.get('__repr__')):
--> 399                             return _repr_pprint(obj, self, cycle)
    400 
    401             return _default_pprint(obj, self, cycle)

/userhome/31/h3509807/anaconda3/envs/fastai-2020/lib/python3.6/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    687     """A pprint that just redirects to the normal repr function."""
    688     # Find newlines and replace them with p.break_()
--> 689     output = repr(obj)
    690     for idx,output_line in enumerate(output.splitlines()):
    691         if idx:

/userhome/31/h3509807/anaconda3/envs/fastai-2020/lib/python3.6/site-packages/fastai/data_block.py in __repr__(self)
    616         items = [self[i] for i in range(min(5,len(self.items)))]
    617         res = f'{self.__class__.__name__} ({len(self.items)} items)\n'
--> 618         res += f'x: {self.x.__class__.__name__}\n{show_some([i[0] for i in items])}\n'
    619         res += f'y: {self.y.__class__.__name__}\n{show_some([i[1] for i in items])}\n'
    620         return res + f'Path: {self.path}'

/userhome/31/h3509807/anaconda3/envs/fastai-2020/lib/python3.6/site-packages/fastai/core.py in show_some(items, n_max, sep)
    376     "Return the representation of the first  `n_max` elements in `items`."
    377     if items is None or len(items) == 0: return ''
--> 378     res = sep.join([f'{o}' for o in items[:n_max]])
    379     if len(items) > n_max: res += '...'
    380     return res

/userhome/31/h3509807/anaconda3/envs/fastai-2020/lib/python3.6/site-packages/fastai/core.py in <listcomp>(.0)
    376     "Return the representation of the first  `n_max` elements in `items`."
    377     if items is None or len(items) == 0: return ''
--> 378     res = sep.join([f'{o}' for o in items[:n_max]])
    379     if len(items) > n_max: res += '...'
    380     return res

/userhome/31/h3509807/anaconda3/envs/fastai-2020/lib/python3.6/site-packages/fastai/core.py in __repr__(self)
    180     "Base item type in the fastai library."
    181     def __init__(self, data:Any): self.data=self.obj=data
--> 182     def __repr__(self)->str: return f'{self.__class__.__name__} {str(self)}'
    183     def show(self, ax:plt.Axes, **kwargs):
    184         "Subclass this method if you want to customize the way this `ItemBase` is shown on `ax`."

... last 1 frames repeated, from the frame below ...

/userhome/31/h3509807/anaconda3/envs/fastai-2020/lib/python3.6/site-packages/fastai/core.py in __repr__(self)
    180     "Base item type in the fastai library."
    181     def __init__(self, data:Any): self.data=self.obj=data
--> 182     def __repr__(self)->str: return f'{self.__class__.__name__} {str(self)}'
    183     def show(self, ax:plt.Axes, **kwargs):
    184         "Subclass this method if you want to customize the way this `ItemBase` is shown on `ax`."

RecursionError: maximum recursion depth exceeded while calling a Python object

I think that happens when you don’t define a repr method in the custom classes you are using

1 Like

Add this method to your ImageTuple

class ImageTuple(ItemBase):
        def __repr__(self):
         return f'{self.__class__.__name__}{(self.img1.shape, self.img2.shape)}'
1 Like

Thanks! @ozgur @juvian
The error is gone after I define repr method (as follows):

class ImageTuple(ItemBase):
    def __init__(self, img1, img2):
        self.img1,self.img2 = img1,img2
        self.obj,self.data = (img1,img2),[-1+2*img1.data,-1+2*img2.data]
    
    def apply_tfms(self, tfms, **kwargs):
        self.img1 = self.img1.apply_tfms(tfms, **kwargs)
        self.img2 = self.img2.apply_tfms(tfms, **kwargs)
        return self
    
    def to_one(self): return Image(0.5+torch.cat(self.data,2)/2)
    
    def __repr__(self):
        return f'{self.__class__.__name__}{(self.img1.shape, self.img2.shape)}'

1 Like