Dev Mehta
Dev Mehta

Reputation: 33

Merge multiple .npy files into single .npy file

I have a folder in which I have 100+ .npy files. The path to this folder is '/content/drive/MyDrive/lung_cancer/subset0/trainImages'.

This folder has the .npy files as shown in the image the .npy files

The shape of each of these .npy files is (3,512,512)

I want to combine all of these files into one single file with the name trainImages.npy so that I can train my unet model with it.

My unet model takes input of the shape (1,512,512). I will load the above trainImages.npy file into imgs_train as below to pass it as input into unet model

imgs_train = np.load(working_path+"trainImages.npy").astype(np.float32)

Can someone please tell me how do i concatenate all those .npy files into one single .npy file?? Thanks.

Upvotes: 0

Views: 1935

Answers (1)

Dev Mehta
Dev Mehta

Reputation: 33

So I found the answer out by myself and I am attaching the code below if anyone needs it. Change it according to your needs..

import os
import numpy as np
path = '/content/drive/MyDrive/lung_cancer/subset0/trainImages/'
trainImages = []
for i in os.listdir(path):
  data = np.load(path+i)
  trainImages.append(data)

Upvotes: 2

Related Questions