How to clear Cuda memory in PyTorchHow to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory?How to get the current time in PythonHow can I make a time delay in Python?How do I sort a dictionary by value?How to make a chain of function decorators?How to make a flat list out of list of listsHow do I list all files of a directory?
Why were the Night's Watch required to be celibate?
What are the slash markings on Gatwick's 08R/26L?
Why does the UK have more political parties than the US?
Adding strings in lists together
How was Apollo supposed to rendezvous in the case of a lunar abort?
What does it mean when you think without speaking?
Can't connect to Internet in bash using Mac OS
What was this black-and-white film set in the Arctic or Antarctic where the monster/alien gets fried in the end?
Did airlines fly their aircraft slower in response to oil prices in the 1970s?
Select row of data if next row contains zero
Mother abusing my finances
The deliberate use of misleading terminology
How can I grammatically understand "Wir über uns"?
Is floating in space similar to falling under gravity?
Thousands and thousands of words
What is the indigenous Russian word for a wild boar?
Why is there a need to modify system call tables in linux?
What are the problems in teaching guitar via Skype?
Is it possible to change original filename of an exe?
Intuition behind eigenvalues of an adjacency matrix
What is game ban VS VAC ban in steam?
Tic-Tac-Toe for the terminal
What caused the tendency for conservatives to not support climate change regulations?
Is a hash a zero-knowledge proof?
How to clear Cuda memory in PyTorch
How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory?How to get the current time in PythonHow can I make a time delay in Python?How do I sort a dictionary by value?How to make a chain of function decorators?How to make a flat list out of list of listsHow do I list all files of a directory?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to get the output of a neural network which I have already trained. The input is an image of the size 300x300. I am using a batch size of 1, but I still get a CUDA error: out of memory
error after I have successfully got the output for 25 images.
I searched for some solutions online and came across torch.cuda.empty_cache()
. But this still doesn't seem to solve the problem.
This is the code I am using.
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
train_x = torch.tensor(train_x, dtype=torch.float32).view(-1, 1, 300, 300)
train_x = train_x.to(device)
dataloader = torch.utils.data.DataLoader(train_x, batch_size=1, shuffle=False)
right = []
for i, left in enumerate(dataloader):
print(i)
temp = model(left).view(-1, 1, 300, 300)
right.append(temp.to('cpu'))
del temp
torch.cuda.empty_cache()
This for loop
runs for 25 times every time before giving the memory error.
Every time, I am sending a new image in the network for computation. So, I don't really need to store the previous computation results in the GPU after every iteration in the loop. Is there any way to achieve this?
Any help will be appreciated. Thanks.
python pytorch
add a comment |
I am trying to get the output of a neural network which I have already trained. The input is an image of the size 300x300. I am using a batch size of 1, but I still get a CUDA error: out of memory
error after I have successfully got the output for 25 images.
I searched for some solutions online and came across torch.cuda.empty_cache()
. But this still doesn't seem to solve the problem.
This is the code I am using.
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
train_x = torch.tensor(train_x, dtype=torch.float32).view(-1, 1, 300, 300)
train_x = train_x.to(device)
dataloader = torch.utils.data.DataLoader(train_x, batch_size=1, shuffle=False)
right = []
for i, left in enumerate(dataloader):
print(i)
temp = model(left).view(-1, 1, 300, 300)
right.append(temp.to('cpu'))
del temp
torch.cuda.empty_cache()
This for loop
runs for 25 times every time before giving the memory error.
Every time, I am sending a new image in the network for computation. So, I don't really need to store the previous computation results in the GPU after every iteration in the loop. Is there any way to achieve this?
Any help will be appreciated. Thanks.
python pytorch
add a comment |
I am trying to get the output of a neural network which I have already trained. The input is an image of the size 300x300. I am using a batch size of 1, but I still get a CUDA error: out of memory
error after I have successfully got the output for 25 images.
I searched for some solutions online and came across torch.cuda.empty_cache()
. But this still doesn't seem to solve the problem.
This is the code I am using.
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
train_x = torch.tensor(train_x, dtype=torch.float32).view(-1, 1, 300, 300)
train_x = train_x.to(device)
dataloader = torch.utils.data.DataLoader(train_x, batch_size=1, shuffle=False)
right = []
for i, left in enumerate(dataloader):
print(i)
temp = model(left).view(-1, 1, 300, 300)
right.append(temp.to('cpu'))
del temp
torch.cuda.empty_cache()
This for loop
runs for 25 times every time before giving the memory error.
Every time, I am sending a new image in the network for computation. So, I don't really need to store the previous computation results in the GPU after every iteration in the loop. Is there any way to achieve this?
Any help will be appreciated. Thanks.
python pytorch
I am trying to get the output of a neural network which I have already trained. The input is an image of the size 300x300. I am using a batch size of 1, but I still get a CUDA error: out of memory
error after I have successfully got the output for 25 images.
I searched for some solutions online and came across torch.cuda.empty_cache()
. But this still doesn't seem to solve the problem.
This is the code I am using.
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
train_x = torch.tensor(train_x, dtype=torch.float32).view(-1, 1, 300, 300)
train_x = train_x.to(device)
dataloader = torch.utils.data.DataLoader(train_x, batch_size=1, shuffle=False)
right = []
for i, left in enumerate(dataloader):
print(i)
temp = model(left).view(-1, 1, 300, 300)
right.append(temp.to('cpu'))
del temp
torch.cuda.empty_cache()
This for loop
runs for 25 times every time before giving the memory error.
Every time, I am sending a new image in the network for computation. So, I don't really need to store the previous computation results in the GPU after every iteration in the loop. Is there any way to achieve this?
Any help will be appreciated. Thanks.
python pytorch
python pytorch
edited Mar 24 at 9:55
talonmies
60.3k17138205
60.3k17138205
asked Mar 24 at 9:38
ntdntd
485
485
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I figured out where I was going wrong. I am posting the solution as an answer for others who might be struggling with the same problem.
Basically, what PyTorch does is that it creates a computational graph whenever I pass the data through my network and stores the computations on the GPU memory, in case I want to calculate the gradient during backpropagation. But since I only wanted to perform a forward propagation, I simply needed to specify torch.no_grad()
for my model.
Thus, the for loop in my code could be rewritten as:
for i, left in enumerate(dataloader):
print(i)
with torch.no_grad():
temp = model(left).view(-1, 1, 300, 300)
right.append(temp.to('cpu'))
del temp
torch.cuda.empty_cache()
Specifying no_grad()
to my model tells PyTorch that I don't want to store any previous computations, thus freeing my GPU space.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55322434%2fhow-to-clear-cuda-memory-in-pytorch%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I figured out where I was going wrong. I am posting the solution as an answer for others who might be struggling with the same problem.
Basically, what PyTorch does is that it creates a computational graph whenever I pass the data through my network and stores the computations on the GPU memory, in case I want to calculate the gradient during backpropagation. But since I only wanted to perform a forward propagation, I simply needed to specify torch.no_grad()
for my model.
Thus, the for loop in my code could be rewritten as:
for i, left in enumerate(dataloader):
print(i)
with torch.no_grad():
temp = model(left).view(-1, 1, 300, 300)
right.append(temp.to('cpu'))
del temp
torch.cuda.empty_cache()
Specifying no_grad()
to my model tells PyTorch that I don't want to store any previous computations, thus freeing my GPU space.
add a comment |
I figured out where I was going wrong. I am posting the solution as an answer for others who might be struggling with the same problem.
Basically, what PyTorch does is that it creates a computational graph whenever I pass the data through my network and stores the computations on the GPU memory, in case I want to calculate the gradient during backpropagation. But since I only wanted to perform a forward propagation, I simply needed to specify torch.no_grad()
for my model.
Thus, the for loop in my code could be rewritten as:
for i, left in enumerate(dataloader):
print(i)
with torch.no_grad():
temp = model(left).view(-1, 1, 300, 300)
right.append(temp.to('cpu'))
del temp
torch.cuda.empty_cache()
Specifying no_grad()
to my model tells PyTorch that I don't want to store any previous computations, thus freeing my GPU space.
add a comment |
I figured out where I was going wrong. I am posting the solution as an answer for others who might be struggling with the same problem.
Basically, what PyTorch does is that it creates a computational graph whenever I pass the data through my network and stores the computations on the GPU memory, in case I want to calculate the gradient during backpropagation. But since I only wanted to perform a forward propagation, I simply needed to specify torch.no_grad()
for my model.
Thus, the for loop in my code could be rewritten as:
for i, left in enumerate(dataloader):
print(i)
with torch.no_grad():
temp = model(left).view(-1, 1, 300, 300)
right.append(temp.to('cpu'))
del temp
torch.cuda.empty_cache()
Specifying no_grad()
to my model tells PyTorch that I don't want to store any previous computations, thus freeing my GPU space.
I figured out where I was going wrong. I am posting the solution as an answer for others who might be struggling with the same problem.
Basically, what PyTorch does is that it creates a computational graph whenever I pass the data through my network and stores the computations on the GPU memory, in case I want to calculate the gradient during backpropagation. But since I only wanted to perform a forward propagation, I simply needed to specify torch.no_grad()
for my model.
Thus, the for loop in my code could be rewritten as:
for i, left in enumerate(dataloader):
print(i)
with torch.no_grad():
temp = model(left).view(-1, 1, 300, 300)
right.append(temp.to('cpu'))
del temp
torch.cuda.empty_cache()
Specifying no_grad()
to my model tells PyTorch that I don't want to store any previous computations, thus freeing my GPU space.
answered Mar 25 at 14:24
ntdntd
485
485
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55322434%2fhow-to-clear-cuda-memory-in-pytorch%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown