Custom Dataset using Tifffile python library

Hi to all,
My first message here and brand new to pytorch and AI.
I have trained a model and now I want to load unseen images to my model so I can segmentate them.
To do that I self defined a dataset class ‘Mydataset’ that gets the directory of images and read the files by using the library tifffile and make some transformations to them.
I work with 3d stacks of images that have tif format.
My problem is that ‘Mydataset’ read only the first slice of the 3d stack but not all the slices from the stuck. Normally I would like with the MyUnseen dataset to read all the slices in my stacks normally I have stacks of 250 slices so in the end my dataset need to have a length of 250. I really appreciate if anyone can help me with this issue thanks in advance.

class MyUnseen(Dataset):

def __init__(self,raw_directory):


def __getitem__(self,idx):

    images=normalize_custom(images) # Apply custom function normalize 



    return (images_ten)
def __len__(self):
    return self.data_len

I’m not sure how to interpret your custom Dataset as it seems as if only a single image was read in:


or is this method able to read multiple images?

Yes tifffile.imread is able to read also 3d stacks of tiffiles . And it returns it in numpy array format.
So out it seems that work out is the below I dont know if it is an easier solution or more logical. I append the images after transformations in a list inside a loop and outside the loop I use torch.stack.
(Actually this solution was found in another post in this forum :face_with_hand_over_mouth:)

def __getitem__(self,idx):
    for i,file in tqdm(enumerate(self.images)):
        images=normalize_custom(file) # Apply custom function normalize 

Thanks for clarifying.
In your current code it seems you are loading and processing all images in each __getitem__ call, which would mean that setting batch_size=1 in the DataLoader would give you the entire dataset. Is this what you really want?

My model was trained using DataParallel then I saved the model.module.state_dict() .
In inference part I load the saved model by using ,
DEVICE=torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)

model_trained = model_trained.cuda(0)

model_trained = nn.DataParallel(model_trained)


For making my predictions I load my 3d stack of tif files and I make the necessary transformations from numpy to torch.tensor and I name my images unseen_tensor
Then I use

But I get an RuntimeError Caught RuntimeError in replica 0 ond device 0
RuntimeError: CUDA out of memory…

So I thought If I use a dataset class and dataloader and then use a loop to make my predictions I could overcome this error.
I hope now makes more sense what I am trying to do
Thanks again for your help

Split this line:


into a loop and iterate the data:

preds = []
with torch.no_grad():
    for input in unseen_tensor: # you can use another logic here to create mini batches etc.
        pred = model(input)
preds = torch.stack(preds)

Thanks that helped a lot. From 10 hours for segmentating my dataset with this method took only 30 minutes. So the predictions are running under cpu right ? I mean with the pred.detach().cpu() ?

No, the output prediction is detached and moved to the host to avoid storing these tensors on the GPU.

Ok thank you! I will need to have a closer look to the documentation of detach and host.