Problems with the FloatList class in a multi dimension image regression model

(Marceau Clavel) #1

Hello fast.ai fellows ! :slight_smile:

I am experiencing a difficulty in a personal project.
I would like to analyse skin types. To do so, I have several face pictures that I labeled, and each face have several parameters.

I am aiming at a n-dimentional regression, in image analysis.
To do so, I have first implemented a function that returns a list of scores for each features of an image.

def get_float_labels(input):
    ...
    return label_list

This is an example of what I get from this function in a 5-dimension feature space:

print(get_float_labels(input1))
# [0.0, 1.0, 2.0, 3.0, 4.0]

Then I want to create a data structure using the DataBlock API, using the class FloatList:

data = (ImageList.from_folder(DATASET_PATH)
 .split_by_rand_pct()
 .label_from_func(get_float_labels, label_cls=FloatList)
 .transform(get_transforms(), size=size)
 .databunch(bs=bs)) 
data1.normalize(imagenet_stats)

But when I run this code, I get the following error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-42-b8fad7b3f100> in <module>
      1 data = (ImageList.from_folder(DATASET_PATH)
      2  .split_by_rand_pct()
----> 3  .label_from_func(get_float_labels, label_cls=FloatList)
      4  .transform(get_transforms(), size=124)
      5  .databunch(bs=bs)) 

/opt/anaconda3/lib/python3.7/site-packages/fastai/data_block.py in _inner(*args, **kwargs)
    461         assert isinstance(fv, Callable)
    462         def _inner(*args, **kwargs):
--> 463             self.train = ft(*args, from_item_lists=True, **kwargs)
    464             assert isinstance(self.train, LabelList)
    465             kwargs['label_cls'] = self.train.y.__class__

/opt/anaconda3/lib/python3.7/site-packages/fastai/data_block.py in label_from_func(self, func, label_cls, **kwargs)
    285     def label_from_func(self, func:Callable, label_cls:Callable=None, **kwargs)->'LabelList':
    286         "Apply `func` to every input to get its label."
--> 287         return self._label_from_list([func(o) for o in self.items], label_cls=label_cls, **kwargs)
    288 
    289     def label_from_folder(self, label_cls:Callable=None, **kwargs)->'LabelList':

/opt/anaconda3/lib/python3.7/site-packages/fastai/data_block.py in _label_from_list(self, labels, label_cls, from_item_lists, **kwargs)
    261         labels = array(labels, dtype=object)
    262         label_cls = self.get_label_cls(labels, label_cls=label_cls, **kwargs)
--> 263         y = label_cls(labels, path=self.path, **kwargs)
    264         res = self._label_list(x=self, y=y)
    265         return res

<ipython-input-39-183745fc07ca> in __init__(self, items, log, classes, **kwargs)
      2     "`ItemList` suitable for storing the floats in items for regression. Will add a `log` if this flag is `True`."
      3     def __init__(self, items:Iterator, log:bool=False, classes:Collection=None, **kwargs):
----> 4         super().__init__(np.array(items, dtype=np.float32), **kwargs)
      5         print("uu")
      6         self.log = log

ValueError: setting an array element with a sequence.

I do not understand how the code works, because the variable items is a bs sized array containing feature lists (of length 5), and then np.array(items, dtype=np.float32) will not work.

I think that I missunderstand how batch-size interven when usin the label_cls FloatList

I hope my problem will be understandable
Thank you

0 Likes

(Marceau Clavel) #2

After a deeper investigation, I found my mistake:
I had 1 item that had an empty list as float_labels…

When we pass an array of lists to a np.array, if all the lists are not the same size, the exception ValueError: setting an array element with a sequence. is raised.

0 Likes