Ram crashing when transforming form numpy to torch tensor

I am having a problem now, I am running my code on google colab with 12 GB Ram, I have images stored at numpy files and I want to transfer them to tensors, but the ram is crashing.

This is my code:

 X_train, X_val, y_train, y_val = train_test_split(X,y, test_size = 0.10, random_state = 87, stratify = y)

X_train = torch.from_numpy(X_train).float().cuda() # transform to torch tensor
y_train = torch.from_numpy(y_train).float().cuda()

X_val = torch.from_numpy(X_val).float().cuda()
y_val = torch.from_numpy(y_val).float().cuda()

These calls torch.from_numpy(X_train).float() will create a new copy on the host if the tensor is not already in float32 and will thus increase the memory usage.
Would it be possible to directly load the numpy arrays in their desired dtype?
Alternatively, you might want to move the tensor to the GPU and transform it tofloat32 there, which would of course launch another copy kernel, but would reduce the host memory usage.

1 Like

Thanks a lot! it was too helpful :heart_eyes: