1

I have read all those images, which I am going to use for binary classification, and stored them in two different NumPy arrays. Now, I need to one hot encode these images, and then feed it to a Neural network.

I don't understand how I can one hot encode two different numpy arrays and then feed them to a neural network.

array_1 contains all the images that will be labelled as 1, and array_2 contains all the images that will be labelled as 0.

John
  • 47
  • 5

1 Answers1

0

The Python package platipy has functionality to encode (multiple-valued) label maps.

To install:

pip install -U pip
pip install platipy

Here is a short example:

import SimpleITK as sitk
from platipy.imaging.label.utils import binary_encode_structure_list

img_label_1 = sitk.ReadImage("img_label_1.nii.gz")
img_label_2 = sitk.ReadImage("img_label_2.nii.gz")
img_label_3 = sitk.ReadImage("img_label_3.nii.gz")
# etc., for however many labels you have

label_list = [img_label_1, img_label_2, img_label_3]

img_encoded = binary_encode_structure_list(label_list)

If you need to use numpy, then you can just convert this SimpleITK image into a 3D numpy array:

arr_encoded = sitk.GetArrayFromImage(img_encoded)

N.B. You can also decode an encoded label map (e.g. the output of your NN) using tools in platipy:

from platipy.imaging.label.utils import binary_decode_image

label_list = binary_decode_image(img_prediction_encoded)

Hope this helps!

Robbie
  • 4,672
  • 1
  • 19
  • 24