MIC-DKFZ / medicaldetectiontoolkit

The Medical Detection Toolkit contains 2D + 3D implementations of prevalent object detectors such as Mask R-CNN, Retina Net, Retina U-Net, as well as a training and inference framework focused on dealing with medical images.
Apache License 2.0
1.31k stars 297 forks source link

convert_seg_to_bounding_box_coordinates: ValueError: setting an array element with a sequence #74

Closed sophie-isobel closed 5 years ago

sophie-isobel commented 5 years ago

Hi @pfjaeger, thanks for sharing your great work on this repository.

I am having the same issue as @paul-bd had, I also only have data labelled as foreground (e.g. 1 vs. 0) and I am getting the following errors from convert_seg_to_bounding_box_coordinates

...
  File "/content/gdrive/My Drive/Dissertation/medicaldetectiontoolkit-master (1)/batchgenerators/batchgenerators/transforms/utility_transforms.py", line 229, in __call__
    data_dict = convert_seg_to_bounding_box_coordinates(data_dict, self.dim, self.get_rois_from_seg_flag, class_specific_seg_flag=self.class_specific_seg_flag)
  File "/content/gdrive/My Drive/Dissertation/medicaldetectiontoolkit-master (1)/batchgenerators/batchgenerators/augmentations/utils.py", line 518, in convert_seg_to_bounding_box_coordinates
    data_dict['class_target'][b] = [data_dict['class_target'][b]] * n_cands
ValueError: cannot copy sequence with size 12 to array axis with dimension 1
...

I followed your advice and set all get_rois_from_seg_flag=True in data_loader.py, I have also set get_rois_from_seg_flag=True in the batchgenerator files: transforms -> utility_transforms.py and augmentations -> utils.py.

And I have set it to just draw random samples from all patients in training data for batch generation (line 225):

...
def generate_train_batch(self):

        batch_data, batch_segs, batch_pids, batch_targets, batch_patient_labels = [], [], [], [], []
        class_targets_list =  [v['class_target'] for (k, v) in self._data.items()]

        #if self.cf.head_classes > 2:
            # samples patients towards equilibrium of foreground classes on a roi-level (after randomly sampling the ratio "batch_sample_slack).
        #    batch_ixs = dutils.get_class_balanced_patients(
        #        class_targets_list, self.batch_size, self.cf.head_classes - 1, slack_factor=self.cf.batch_sample_slack)
        #else:
        batch_ixs = np.random.choice(len(class_targets_list), self.batch_size)

        patients = list(self._data.items())
...

When I change line 235 to batch_targets.append(0) I still get ValueError: setting an array element with a sequence:

...
  File "/content/gdrive/My Drive/Dissertation/medicaldetectiontoolkit-master/batchgenerators/batchgenerators/transforms/utility_transforms.py", line 229, in __call__
    data_dict = convert_seg_to_bounding_box_coordinates(data_dict, self.dim, self.get_rois_from_seg_flag, class_specific_seg_flag=self.class_specific_seg_flag)
  File "/content/gdrive/My Drive/Dissertation/medicaldetectiontoolkit-master/batchgenerators/batchgenerators/augmentations/utils.py", line 518, in convert_seg_to_bounding_box_coordinates
    data_dict['class_target'][b] = [data_dict['class_target'][b]] * n_cands
ValueError: setting an array element with a sequence.
...

note, my class targets are set as 0 for each individual pid in data:

...
    data = OrderedDict()
    for ix, pid in enumerate(pids):
        targets = [0]
        data[pid] = {'data': imgs[ix], 'seg': segs[ix], 'pid': pid, 'class_target': targets}

    return data
...

Would you know why I am still getting this error? thanks in advance.

Originally posted by @sophie-isobel in https://github.com/pfjaeger/medicaldetectiontoolkit/issues/11#issuecomment-522223331

sophie-isobel commented 5 years ago

update: I converted the data_dict['class_targets'] in the batchgenerator - augmentations - utils.py file from a numpy array to a list, so that the sequence of n candidates could be added.

See the printed outputs below.

image

This is now training and testing with no errors and I think looking at the results it is doing the right thing!