Skip to content

Commit

Permalink
Fix "RuntimeError: CUDA Error: out of memory"
Browse files Browse the repository at this point in the history
# Problem
According to this [issue](arnoweng#27)
I also forked this repo and try running on my Colab project. The same problem arose: `RuntimeError: CUDA Error: out of memory`.

# Solution
As far as my knowledge, the problem happened because some section did not require `grad` yet still did anyway. Thus, [`with no_grad()`](https://pytorch.org/docs/stable/generated/torch.no_grad.html) should be presented.
  • Loading branch information
icekang authored Nov 15, 2020
1 parent 3d45ee7 commit 7adeda3
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ def main():
gt = torch.cat((gt, target), 0)
bs, n_crops, c, h, w = inp.size()
input_var = torch.autograd.Variable(inp.view(-1, c, h, w).cuda(), volatile=True)
output = model(input_var)
output_mean = output.view(bs, n_crops, -1).mean(1)
pred = torch.cat((pred, output_mean.data), 0)
with torch.no_grad(): # Fix CUDA out of memory
output = model(input_var)
output_mean = output.view(bs, n_crops, -1).mean(1)
pred = torch.cat((pred, output_mean.data), 0)

AUROCs = compute_AUCs(gt, pred)
AUROC_avg = np.array(AUROCs).mean()
Expand Down Expand Up @@ -126,4 +127,4 @@ def forward(self, x):


if __name__ == '__main__':
main()
main()

0 comments on commit 7adeda3

Please sign in to comment.