-
Notifications
You must be signed in to change notification settings - Fork 230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add remap labels kwarg to RandomFlip #409
Comments
Would be good to support this in a convenient way. The method of using a If it's included in the saggital_remap = {
"label_map_name_a": [(left_id0, right_id0), (left_id1, right_id1), ... ],
"label_map_name_b: [...]
} Another direction is to have some official way to associate the integer ids in a label map with their names and optionally a left/right specifier. A method The advantage is that it would be possible to figure out the left <-> right remapping automatically within the Definitely more complicated to go that route though. I think a |
Actually, shouldn't there already be a kwarg for this in
This is what I meant by the dict of dicts, I guess I meant a dict whose keys are label map names and whose values are remappings. I think it would be more intuitive than a dict with list of tuples as values.
Yeah I think this would be nice. Might as well use a new |
Maybe the label map could optionally take a path to a CSV file like this. The laterality would ideally be inferred from the path, so the format of the table is consistent. That is what 3D Slicer uses, and I think also MITK. Probably others, as well. |
Interestingly, I've been trying to use the above approach (Flip + Remap) on the brain parcellation notebook on Colab, but didn't work: num_augmentations = 10
results = []
import pandas as pd
df = pd.read_csv('GIFNiftyNet.ctbl', sep=' ', names=['Label', 'Name', *'RGBA'])
mapping = {}
for row in df.itertuples():
if 'Left' in row.Name:
mapping[row.Label] = df[df.Name == f'Right-{row.Name[5:]}'].Label.values[0]
elif 'Right' in row.Name:
mapping[row.Label] = df[df.Name == f'Left-{row.Name[6:]}'].Label.values[0]
flip = tio.Compose((
tio.RandomFlip(flip_probability=1),
tio.RemapLabels(mapping),
),
p=0.5,
)
resample = tio.OneOf({
tio.RandomAffine(image_interpolation='nearest'): 0.75,
tio.RandomElasticDeformation(image_interpolation='nearest'): 0.25,
})
augment = tio.Compose((flip, resample))
for _ in trange(num_augmentations):
augmented = augment(preprocessed)
input_tensor = augmented.t1.data[None].to(device)
with torch.cuda.amp.autocast():
logits = model(input_tensor)
output_tensor = logits.argmax(dim=1, keepdim=True).cpu()
augmented.t1.set_data(output_tensor[0])
back = augmented.apply_inverse_transform(warn=True)
results.append(back.t1.data)
result = torch.stack(results).long()
print(result[..., 100, 108, 120]) I get about 50% predictions of left gray matter and 50% right on that voxel, meaning that something is not working. Anyway, I'll need to debug that. Also, the current way to do this is a bit awkward, we should have some higher-level features. But that's a different issue. |
Yes, since it's compatible with the Maybe a list of dictionaries would be a better parameterization to fully support inclusion/exclusion:
Would be better in cases where you need that level of control. Although I'm not sure this is any less complicated than composing a Maybe the compose approach would be a bit more clear if there was a I think I ran into the same issue from your code snippet. The problem is that This is a bit off topic, but I think the way the |
Ah, sure. Do you mean there's no need for a new kwarg because we can already use include/exclude?
Yeah I'd say let's add support for something simple and modify it for more complex use cases later, if necessary.
SwapLabels sounds nice, but it would indeed be very a very thin wrapper of RemapLabels. Not sure it would add much value.
Exactly, that was my issue. Thanks!
Yes, this should be discussed in a new issue. I think inferring the image type from the data type is not a good idea. See #375. I'm not sure to understand what the motivation to include an argument to |
馃殌 Feature
Add remap labels kwarg to RandomFlip.
Motivation
When an image is flipped around the sagittal plane, anatomical labels in a label map lose meaning. For example, if a label map represents "left eye" with 2 and "right eye" with 3, the labels will be misleading if the image is flipped.
Pitch
I think it would be nice to add a kwarg that applies some remapping using the new
RemapLabels
transform only if the flipping happens.Alternatives
The way I would do this now is using
Compose
withp=flip_probability
and put inside aRandomFlip
withflip_probability=1
and aRemapLabels
.Maybe one more kwarg is needed to specify the name of the label map that would be affected by this. Or the kwarg could be either a dict (applied to all label maps) or a dict of dicts.
What do you think, @efirdc?
The text was updated successfully, but these errors were encountered: