I'm trying to load a model partially (i.e., instead of loading all the layers at once, I'm just trying to load the first couple layers of the network). Here is my code:
import torch
unet = my_unet(in_ch=5, out_ch=1).cuda()
enc = torch.nn.Sequential(*list(unet.children())[:10])
del unet # Comment this line if you wnat to load unet and see output later
print(torch.cuda.empty_cache())
x = np.random.randn(1, 5, 320, 320)
x = (x - np.min(x))/(np.max(x) - np.min(x))
out = enc(torch.FloatTensor(x).cuda())
# out = unet(torch.FloatTensor(x).cuda())
print(out.size())
If I run this script, it is interrupted by CUDA out of memory
error. But if I load the whole my_unet
model(by commenting the 3rd and the 7th line and uncommenting the 8th line) it works fine and prints the out.size() (1, 1, 320, 320) as expected. What am I doing wrong here? Why can I load the whole model but can not load part of the same model which should require less memory? Is there any workaround?