Float can't be cast to the desired output type Int

Hello Everyone,

I am fairly new to fastai and I apologize if this problem seems fairly rudimentary. I’ve been struggling with this for a couple of days now and no amount of breaking my head or trawling google and these forums has helped.

My task involves creating a binary classifier for DICOM images. My code is as follows:

mgmt = DataBlock(blocks=(ImageBlock(cls=PILDicom), CategoryBlock),
                   get_x=lambda x: Path(x[4]),
                   splitter=RandomSplitter(valid_pct=0.2, seed=42),
                   get_y=lambda x:x[5],
                   item_tfms = [Resize(size= 512, resamples= (Image.NONE,0))],
                   batch_tfms=[*aug_transforms(size=224),Normalize.from_stats(*imagenet_stats)])

dls = mgmt.dataloaders(df.values)

learn = cnn_learner(dls, resnet34, metrics = accuracy)

Everything works just fine till here. I’ve followed the fast ai medical imaging tutorial to get to this point. The next bit is where everything breaks.

learn.fit_one_cycle(1)

This throws up a massive error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-65-4dfb24161c57> in <module>
----> 1 learn.fit_one_cycle(1)

/opt/conda/lib/python3.7/site-packages/fastai/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
    110     scheds = {'lr': combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
    111               'mom': combined_cos(pct_start, *(self.moms if moms is None else moms))}
--> 112     self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
    113 
    114 # Cell

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
    209             self.opt.set_hypers(lr=self.lr if lr is None else lr)
    210             self.n_epoch = n_epoch
--> 211             self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
    212 
    213     def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    158 
    159     def _with_events(self, f, event_type, ex, final=noop):
--> 160         try: self(f'before_{event_type}');  f()
    161         except ex: self(f'after_cancel_{event_type}')
    162         self(f'after_{event_type}');  final()

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _do_fit(self)
    200         for epoch in range(self.n_epoch):
    201             self.epoch=epoch
--> 202             self._with_events(self._do_epoch, 'epoch', CancelEpochException)
    203 
    204     def fit(self, n_epoch, lr=None, wd=None, cbs=None, reset_opt=False):

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    158 
    159     def _with_events(self, f, event_type, ex, final=noop):
--> 160         try: self(f'before_{event_type}');  f()
    161         except ex: self(f'after_cancel_{event_type}')
    162         self(f'after_{event_type}');  final()

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _do_epoch(self)
    194 
    195     def _do_epoch(self):
--> 196         self._do_epoch_train()
    197         self._do_epoch_validate()
    198 

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _do_epoch_train(self)
    186     def _do_epoch_train(self):
    187         self.dl = self.dls.train
--> 188         self._with_events(self.all_batches, 'train', CancelTrainException)
    189 
    190     def _do_epoch_validate(self, ds_idx=1, dl=None):

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    158 
    159     def _with_events(self, f, event_type, ex, final=noop):
--> 160         try: self(f'before_{event_type}');  f()
    161         except ex: self(f'after_cancel_{event_type}')
    162         self(f'after_{event_type}');  final()

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in all_batches(self)
    164     def all_batches(self):
    165         self.n_iter = len(self.dl)
--> 166         for o in enumerate(self.dl): self.one_batch(*o)
    167 
    168     def _do_one_batch(self):

/opt/conda/lib/python3.7/site-packages/fastai/data/load.py in __iter__(self)
    107         self.before_iter()
    108         self.__idxs=self.get_idxs() # called in context of main process (not workers/subprocesses)
--> 109         for b in _loaders[self.fake_l.num_workers==0](self.fake_l):
    110             if self.device is not None: b = to_device(b, self.device)
    111             yield self.after_batch(b)

/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in __next__(self)
    433         if self._sampler_iter is None:
    434             self._reset()
--> 435         data = self._next_data()
    436         self._num_yielded += 1
    437         if self._dataset_kind == _DatasetKind.Iterable and \

/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _next_data(self)
   1063             if len(self._task_info[self._rcvd_idx]) == 2:
   1064                 data = self._task_info.pop(self._rcvd_idx)[1]
-> 1065                 return self._process_data(data)
   1066 
   1067             assert not self._shutdown and self._tasks_outstanding > 0

/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _process_data(self, data)
   1109         self._try_put_index()
   1110         if isinstance(data, ExceptionWrapper):
-> 1111             data.reraise()
   1112         return data
   1113 

/opt/conda/lib/python3.7/site-packages/torch/_utils.py in reraise(self)
    426             # have message field
    427             raise self.exc_type(message=msg)
--> 428         raise self.exc_type(msg)
    429 
    430 

RuntimeError: Caught RuntimeError in DataLoader worker process 1.
Original Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 198, in _worker_loop
    data = fetcher.fetch(index)
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 34, in fetch
    data = next(self.dataset_iter)
  File "/opt/conda/lib/python3.7/site-packages/fastai/data/load.py", line 118, in create_batches
    yield from map(self.do_batch, self.chunkify(res))
  File "/opt/conda/lib/python3.7/site-packages/fastai/data/load.py", line 144, in do_batch
    def do_batch(self, b): return self.retain(self.create_batch(self.before_batch(b)), b)
  File "/opt/conda/lib/python3.7/site-packages/fastai/data/load.py", line 143, in create_batch
    def create_batch(self, b): return (fa_collate,fa_convert)[self.prebatched](b)
  File "/opt/conda/lib/python3.7/site-packages/fastai/data/load.py", line 50, in fa_collate
    else type(t[0])([fa_collate(s) for s in zip(*t)]) if isinstance(b, Sequence)
  File "/opt/conda/lib/python3.7/site-packages/fastai/data/load.py", line 50, in <listcomp>
    else type(t[0])([fa_collate(s) for s in zip(*t)]) if isinstance(b, Sequence)
  File "/opt/conda/lib/python3.7/site-packages/fastai/data/load.py", line 49, in fa_collate
    return (default_collate(t) if isinstance(b, _collate_types)
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py", line 55, in default_collate
    return torch.stack(batch, 0, out=out)
  File "/opt/conda/lib/python3.7/site-packages/fastai/torch_core.py", line 329, in __torch_function__
    res = super().__torch_function__(func, types, args=args, kwargs=kwargs)
  File "/opt/conda/lib/python3.7/site-packages/torch/tensor.py", line 995, in __torch_function__
    ret = func(*args, **kwargs)
RuntimeError: result type Float can't be cast to the desired output type Int

The gist of which is: Float can’t be cast to the desired output type Int. I’ve tried converting the y labels to float/int(as suggested on someone’s post on Stack), but to no avail. It feels like the solution has to be something really stupid and silly, but I’m at my wits ends now. Please help.

Are you following the tutorial exactly, or are you using your own data? The get_x and get_y values in your DataBlock are different than those in the tutorial.

If using your own data, what are the types of df[4] and df[5]?

I’m using the tutorial as guide for my project. My data is different, but I’ve made it so that my dataframe is similar to the one used in the tutorial. df[4] contains the path to the images and df[5] contains the labels.

Hmm, my bet is that it’s something in the DataFrame dtype. What’s the output from df.head() and df.describe()?

This is what the DataFrame looks like. The column MGMT_value contains the output labels which is just 0s and 1s. The other four columns all contain paths to images. I’m currently trying to use only one of the columns as input which is why get_x = df[4].

I’ve also tried with MGMT_value as float, but to no avail.

One difference between your dataframe and the one from the tutorial is that the tutorial target column is of dtype object. You’ve tried Float and Int64, but maybe you just need to make it an object instead.

You can do this by changing get_y to:

get_y=lambda x: str(x[5])

I haven’t dug into the CategoryBlock set of transforms, but my hunch is that it wants a str and not a number.

Tried. It’s the same result :sleepy: