but got RuntimeError: CUDA out of memory. Tried to allocate 16.00 MiB (GPU 0; 5.80 GiB total capacity; 4.52 GiB already allocated; 4.56 MiB free; 4.53 GiB reserved in total by PyTorch)
in this line model.to(device)
As the error message describes your GPU doesn’t have enough free memory and cannot allocate new memory to push the model to the GPU.
Try to kill other processes which might consume memory and rerun your script.
You could try to del unneeded tensors before creating the model.
However, it depends on your use case if deleting tensors etc. from the device would actually work or if you would need them.
where exactly i should del the tensor , please
the error exactly in this line vec=model(preprocess(image).unsqueeze(0).cuda()) i used a counter after vec1 and got the tensor for the first image then throw this error
It seems to error now moved from the model.to() operation to the actual forward pass.
If the only data on the GPU are the model’s parameters and the input tensor, your model might be too large for your device. You could try to use e.g. torch.utils.checkpoint to trade compute for memory.
I don’t know how high the peak memory usage for your entire workflow is, but I would assume that your system has more host RAM than the GPU (6GB), so it’s worth a try.