Extending medical.imaging module

medical.imaging module is pretty neat, I personally used it in the latest RSNA comp - it was pretty fast and easy to prep a useful dataset. The goal for this thread is to share ideas on how to extend this module to cover more potential use cases. Current module is a good start but it can be much much better and general.

As an initial step I did some work on top of the current module to enable preparing 3D datasets with ROI Struct files.

Here I am attaching the link for an example POC notebook

Edit: New notebook in new fastai2 repo.

If it looks good I can add tests and integrate it into the original module notebook.


Thanks for creating this thread @kcturgutlu - good idea!

I don’t have enough context to understand what is happening in that notebook. Maybe you could try to add some prose and structure to it, explaining what the dataset is, where it comes from, what you’re doing with it and why, what ROI Struct files are, etc?

The current medical.imaging module is just a minimal set of bits I needed for the RSNA comp. Before it’s released we’ll be adding similar information to the functionality we already have.


If we are interested in extending the library for other imaging modalities (ex: histopathology) would we discuss that on this thread?

I will add the mentioned explanations to the notebook then, thanks for feedback :slight_smile:

Yes, I think we can discuss extensions for any medical imaging modality here as long as it requires special care. For example, if your histopathology images are already in jpeg format there won’t be any extensions needed for that use case, although if there are certain things specific to histopathology imaging, e.g. a specific data augmentation technique then I think medical.imaging module would be the right place.


Yes. Another thing is histopathology images could be very large (ex: whole-slide images). So something I would probably implement would be a function to divide the large images into multiple smaller images, which is probably an important preprocessing step. So there are definitely are functions that could be implemented as part of the medical imaging module.

1 Like

I wonder if that would be a generic transform for any large image, e.g. satellite etc…:thinking:


I updated the notebook link, hopefully now it will be easier to understand the what and why components :slight_smile:

Regarding whole slide images, I found this paper corresponding to this github code. Here the authors divide every slide image into tiles and make a prediction using Resnet34 with Multiple Instance Learning. Basically, it consist on give the whole bunch of tiles coming from the same image a single label and find the one which represent best that label.
They also use a RNN to make the whole slide prediction using the weights from the last conv layer of the Resnet34 as input.
I think there are a couple of things that would be great to implement in v2 which I think it would be kind of feasible. First, the tile and train on the fly which could be specific for this big images. Then, having the CNN+RNN structure would be awesome for this kind of studies and could be also implemented for the RSNA competition, for example.


In addition to tiling, while the Campanella te al. Approach is based on labeling the whole slide, usually masks are provided to label different regions of the slide (e.g. like in Camelyon and BACH challenges). A way to classify tiles basing on masks could be useful. Normally they are provided as OME XML files with polygons, or as binary masks in TIFF, at the same or lower size than the original slide.


Important Issue: https://github.com/pydicom/pydicom/issues/963 (closed 9 days ago as of writing this)

Today I spent around 5 hours to see why I wasn’t able to get parallel -> ProcessPoolExectuor work for my DICOM data pipelines. It turned out to be a pickle issue. Since we patch DICOM Dataset class to add new methods and properties, pickle dump and loads operations fail in pydicom version 1.3.0. It seemed to be fixed in master.

Posting it here so that we are aware of it and if someone else faces the following error: A process in the process pool was terminated abruptly

1 Like

I am trying to understand the purpose of _cast_dicom_special, I believe it casts pydicom types but leaves it if base class type is object.

def _cast_dicom_special(x):
    cls = type(x)
    if not cls.__module__.startswith('pydicom'): return x
    if cls.__base__ == object: return x
    return cls.__base__(x)

I have the following error when trying to save some metadata using a variant of as_dict(): ArrowInvalid: ('Could not convert Anon^0243 with type PersonName3: did not recognize Python value type when inferring an Arrow data type', 'Conversion failed for column PatientName with type object')

Would it be fine to cast values to str if their __base__ is object? Unfortunately, I am a bit confused about the logic we have here. Thanks in advance!

@kcturgutlu really I was trying to do whatever was needed to get my dicom meta data working for RSNA kaggle comp. I’m sure there’s lots of things it still can’t handle. So see if you can figure out what’s needed here for you. I doubt you can just cast anything to str, since that’s going to change numeric types too. Instead, figure out what PersonName3 type is really, and how you can check that in a general way.

1 Like

Hi @kcturgutlu . Thanks for great efforts. I am very interested in this module. If you explain more about dataset and what problem you are trying target with this dataset, I will appreciate it.

Of course! Glad you like :slight_smile: Please see the notebook I shared in my fork of fasta2i - in the top of this thread see this part.

I am working hard in this notebook to be self-explanatory since in fastai dev we have module, docs and tests in the same notebook. Please explore it and let me know what you think :slight_smile: All feedback and questions are more than welcome.

I wonder if anything useful from PyTorch3D could be integrated?

1 Like