#!/usr/bin/env python # coding: utf-8 # # Loading DICOM images into PyTorch # # > Recently I needed to load DICOM files for modeling and I've learned about handly library - imageio. It can handle a lot of different file types effortlessly, one of them DICOM volumetrtic datasets. # # - toc: false # - hide: false # - badges: true # - comments: true # - categories: [ML, medical] # In[1]: # install imageio if you haven't already get_ipython().system('pip install imageio') # In[2]: import imageio import torch # As an example, I've downloaded some dicom files from [this site](https://www.visus.com/en/downloads/jivex-dicom-viewer.html) # In[3]: get_ipython().system('curl "https://www.visus.com/fileadmin/content/pictures/Downloads/JiveX_DICOME_Viewer/case1.zip" > "case1.zip"') get_ipython().system('unzip -q case1.zip') # and simply pass the folder to imageio like this: # In[4]: np_arr = imageio.volread('case1') # ### turn into torch tensor # As I prefert to work with PyTorch tensors... # In[5]: dicom_torch = torch.from_numpy(np_arr) # In[6]: dicom_torch.shape # ... and this is how it looks like # In[7]: import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') # In[8]: plt.imshow(dicom_torch[10])