Converting the image to tensor on GPU

Can we load in PyTorch images from the disk to GPU and then possible convert them fast to tensor?

Hi,

You should load the image in a Tensor then send it to the GPU.
Can you be a bit more precise on what you want to do exactly?

Just loading a file into GPU memory directly and converting it to a tensor. Does this work? Or loading bunch of files to be efficient.

How do you load a file into GPU memory directly?

This is what I was hopping to get the idea @albanD :slight_smile:

Ho, well you can’t :smiley:

To make sure you get enough data for your training procedure, we usually use DataLoader with multiple workers. That way you have multiple processes loading and preprocessing data.
You should check the examples in the doc for how to do that.

OK, what you said has lot of sense.
I thought there is a way and that PyTorch has some routines doing basically this:

// Declare data storage
std::vector<float> my_data;
float *d_my_data;
 
// Read input file
std::ifstream fin("my_data_file.txt");
while (!fin.eof()) {
	float data;
	fin >> data;
	if (!fin.eof()) {
		my_data.push_back(data);
	}
}
fin.close();
 
// Allocate memory on the GPU and copy data
cudaMalloc((void **)&d_my_data, my_data.size()*sizeof(float));
cudaMemcpy(d_my_data, my_data.data(), my_data.size()*sizeof(float),
		   cudaMemcpyHostToDevice);
 
// Do something with the data
my_CUDA_kernel<<<num_blocks,num_threads>>>(d_my_data);
 
// Copy data back to the host
cudaMemcpy(my_data.data(), d_my_data, my_data.size()*sizeof(float),
		   cudaMemcpyDeviceToHost);
 
// Free the GPU memory when your done
cudaFree(d_my_data);

This code I just saw, but the idea to ask the question was a blitz.
I don’t know if this is comparable with super duper DataLoader, but I guess this would not be the path you would recommend, right?

Ho sorry I were using python !
In cpp, I would just read the file directly into a cpu tensor instead of a vector.
Then send that Tensor on the GPU.

Note that you can always get the pointer to the gpu data from the pytorch cuda tensor to use in your own kernel.

Some example code for the cpp extensions include working with cuda Tensors here. You can ignore the part that exports it to python.