How can i solve my "out of Gpu memory"problem on training

username: Valerien
cooked: <aside class="onebox allowlistedgeneric">
  <header class="source">
      <img src="https://colab.research.google.com/img/favicon.ico?vrz=colab-20201111-085601-RC02_341903846" class="site-icon" width="16" height="16">
      <a href="https://colab.research.google.com/github/pytorch/vision/blob/temp-tutorial/tutorials/torchvision_finetuning_instance_segmentation.ipynb#scrollTo=at-h4OWK0aoc" target="_blank" rel="noopener nofollow ugc">colab.research.google.com</a>
  </header>
  <article class="onebox-body">
    <img src="https://colab.research.google.com/img/colab_favicon_256px.png" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://colab.research.google.com/github/pytorch/vision/blob/temp-tutorial/tutorials/torchvision_finetuning_instance_segmentation.ipynb#scrollTo=at-h4OWK0aoc" target="_blank" rel="noopener nofollow ugc">Google Colaboratory</a></h3>



  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>i try to use pre-trained maskrcnn_resnet50_fpn for my dataset .First of all i run this whole code in colab.İt is working on google colab because they have enough gpu memory.But i dont have that much gpu memory.I have 6 gb nvidia rtx 2060.When i run this code on my computer its says out of gpu error on training.How can i solve this problem? i tried to decrease batch size but as you can see its already 2 and it does not solve my problem .</p>

username: Eiphodos
cooked: <p>Try using mixed precision, you need pytorch-nightly to import their amp module.<br>
</p><aside class="onebox allowlistedgeneric">
  <header class="source">
      <a href="https://pytorch.org/docs/stable/amp.html" target="_blank" rel="noopener nofollow ugc">pytorch.org</a>
  </header>
  <article class="onebox-body">
    <img src="" class="thumbnail" width="" height="">

<h3><a href="https://pytorch.org/docs/stable/amp.html" target="_blank" rel="noopener nofollow ugc">Automatic Mixed Precision package - torch.cuda.amp — PyTorch 1.7.0 documentation</a></h3>



  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


username: Valerien
cooked: <aside class="quote no-group" data-username="Eiphodos" data-post="2" data-topic="102680">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eiphodos/48/30974_2.png" class="avatar"> Eiphodos:</div>
<blockquote>
<p>pytorch-nightly</p>
</blockquote>
</aside>
<p>Thanks for the answer.I try to implement what you are saying to this algorithm on colab.But i can’t be able to implement that can you guide me.<br>
For example where should i write the</p>
<p>optimizer.zero_grad()</p>
<p>line.</p>
<p>If you open the link which i send you and if you read the code and tell me that what should i do.</p>
<p>Thanks in advance</p>

username: Eiphodos
cooked: <p>Looking at the code you are trying to run, optimizer.zero_grad() is probably located in engine/train_one_epoch and you will also need to edit the forward function in the actual model.<br>
See <a href="https://pytorch.org/docs/stable/notes/amp_examples.html#amp-examples" class="inline-onebox" rel="noopener nofollow ugc">Automatic Mixed Precision examples — PyTorch 1.7.0 documentation</a> for use case examples.</p>
<p>I want to point out that it might be difficult to correctly enable AMP if you are unfamiliar with the code you are trying to run and it is not guaranteed that you can fit the model and data into GPU mem even after enabling it.</p>
<p>Another course of action could therefore be to add a transform to downscale the images you are training the model on in get_transform. There you could append this: <a href="https://pytorch.org/docs/stable/torchvision/transforms.html#torchvision.transforms.Resize" class="inline-onebox" rel="noopener nofollow ugc">torchvision.transforms — PyTorch 1.7.0 documentation</a> and experiment a bit with sizes. By downscaling the size of the image it is possible that you could run the model on your graphic card.</p>