How to run large images on gpu

yichao_Liu: <p>I have 12000 1024<em>512 images as dataset. Initially, I resize them to 224</em>112, but the results are terrible. Since these images are scattering images (physics images), I think the resolution may have some information, So I decided to use large images to run the prediction model (predicting some parameters). But when I only use 10 layers networks, it run out of memory. If someone can help me solve this, I’m very thankful.</p>
<p>I planed to load a batch into gpu and train it, then load another batch into gpu and train. But I don’t know how to implement…</p>

RaLo4: <p>First up I would recommend using square images if possible. For example 224 x 224.</p>
<p>On how to train on your gpu with a specific batch size:<br>
When defining a dataloader you can specify a batch size like so:</p>
<pre><code class="lang-auto">batch_size = 96
train_loader = torch.utils.data.DataLoader(train_set, batch_size=batch_size, 
                                           shuffle=True, num_workers=workers)
</code></pre>
<p>In the training loop you can then get your individual batches by doing something like:</p>
<pre><code class="lang-auto">device = torch.device('cuda:0')
for epoch in range(n_epochs):
    """training loop for all epochs"""
    for i, data in enumerate(train_loader, 0):
        """training loop for one batch"""
        #get images and labels
        inputs, labels = data
        #move data to gpu
        inputs, labels = inputs.to(device), labels.to(device)
</code></pre>

yichao_Liu: <aside class="quote no-group" data-username="RaLo4" data-post="2" data-topic="89909">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/65b543/48.png" class="avatar"> RaLo4:</div>
<blockquote>
<p>First up I would recommend using square images if possible. For example 224 x 224.</p>
<p>On how to train on your gpu with a specific batch size:<br>
When defining a dataloader you can specify a batch size like so:</p>
</blockquote>
</aside>
<p>Thank you so much, I did so. But still can’t finish one epochs. I’m wondering how ImageNet runs on gpu. Because 124G data can be ran on one gpu.</p>

RaLo4: <aside class="quote no-group" data-username="yichao_Liu" data-post="3" data-topic="89909">
<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/yichao_liu/48/18684_2.png" class="avatar"> yichao_Liu:</div>
<blockquote>
<p>I did so. But still can’t finish one epochs.</p>
</blockquote>
</aside>
<p>If you get a CUDA out of memory error when running on GPU you can try using a smaller batch size.<br>
The 96 I used was just there as an example.</p>
<aside class="quote no-group" data-username="yichao_Liu" data-post="3" data-topic="89909">
<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/yichao_liu/48/18684_2.png" class="avatar"> yichao_Liu:</div>
<blockquote>
<p>I’m wondering how ImageNet runs on gpu. Because 124G data can be ran on one gpu.</p>
</blockquote>
</aside>
<p>It is oftentimes run on multiple GPUs but you can still run it on only one GPU, you just need a small enough batch size.<br>
Because there is always just one batch at a times being put onto the GPU instead of all images, the total amount of images in the dataset does not matter.<br>
Just use any form of dataloader, like the one I showed you above, with a small enough batch size and it should work.</p>

yichao_Liu: <aside class="quote no-group" data-username="RaLo4" data-post="4" data-topic="89909">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/65b543/48.png" class="avatar"> RaLo4:</div>
<blockquote>
<p>It is oftentimes run on multiple GPUs but you can still run it on only one GPU, you just need a small enough batch size.<br>
Because there is always just one batch at a times being put onto the GPU instead of all images, the total amount of images in the dataset does not matter.<br>
Just use any form of dataloader, like the one I showed you above, with a small enough batch size and it should work.</p>
</blockquote>
</aside>
<p>Thing is this dataset is a customed dataset. I wrote a dataloader for it (not Pytorch DataLoader), but I think maybe it’s the problem with that I load all the dataset in <em>init</em> function.</p>

RaLo4: <aside class="quote no-group" data-username="yichao_Liu" data-post="5" data-topic="89909">
<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/yichao_liu/48/18684_2.png" class="avatar"> yichao_Liu:</div>
<blockquote>
<p>Thing is this dataset is a customed dataset. I wrote a dataloader for it (not Pytorch DataLoader), but I think maybe it’s the problem with that I load all the dataset in <em>init</em> function.</p>
</blockquote>
</aside>
<p>It doesn’t really matter that you use your on dataloader. You just have to make sure that you only move one batch at a time on to the GPU. Like I did here:</p>
<aside class="quote no-group" data-username="RaLo4" data-post="2" data-topic="89909">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/65b543/48.png" class="avatar"> RaLo4:</div>
<blockquote>
<pre><code class="lang-auto">    for i, data in enumerate(train_loader, 0):
        """training loop for one batch"""
        #get images and labels
        inputs, labels = data
        #move data to gpu
        inputs, labels = inputs.to(device), labels.to(device)
</code></pre>
</blockquote>
</aside>
<p>As long as <em>inputs</em> and <em>labels</em> is always just one batch it should which kind of dataloader they come from.</p>
<p>Also! Check if you are maybe putting different tensors onto the GPU and keeping them there.<br>
If you for example do <code>total_loss += loss</code>  instead of  <code>total_loss += loss.item()</code> for logging your loss, your GPU memory will fill up.</p>

yichao_Liu: <p>Following is my code, it’s same as you said, but it still has out of memory issue:</p>
<pre><code class="lang-auto">&gt; for batch_idx, (inputs, targets) in enumerate(trainloader):
&gt;         cur_iter = (epoch - 1) * len(trainloader) + batch_idx        
&gt;         gpu_tracker.track()
&gt;         if use_cuda:
&gt;               inputs, targets = inputs.cuda(non_blocking=True), targets.cuda(non_blocking=True)  # GPU settings    
&gt;         optimizer.zero_grad()
&gt;         inputs, targets = Variable(inputs, requires_grad=True), Variable(targets)
</code></pre>

RaLo4: <p>Depending on what pytorch version you are on Variable() is actually long deprecated and I am not sure you need non_blocking to be True for your application, but all in all <strong>your code should work</strong>.<br>
You might wanna check if you have other tensors on the GPU. If you do anything with the tensor that is your models output, like logging, you might wanna check if it gets freed correctly, like I mentioned here:</p>
<aside class="quote no-group" data-username="RaLo4" data-post="6" data-topic="89909">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/65b543/48.png" class="avatar"> RaLo4:</div>
<blockquote>
<p>Check if you are maybe putting different tensors onto the GPU and keeping them there.<br>
If you for example do <code>total_loss += loss</code> instead of <code>total_loss += loss.item()</code> for logging your loss, your GPU memory will fill up.</p>
</blockquote>
</aside>

yichao_Liu: <p>Thank you so much for your patient explaination. I also did as you told me, but still have memory problem. I strongly doubt that pytorch load all the dataset in gpu which are 12000 896 * 448 images (48G). But ImageNet has 124G and can run on 18 G gpus. I have 2 nodes which is 48 G for each node. I can’t even run it with batch size 1.<br>
And how can I check if there are other tensors on gpu? Thank you.</p>
<pre><code class="lang-auto">else: # logging for classification
            #_, predicted = torch.max(out.data, 1) for classification
            total += targets.size(0)
            #correct += predicted.eq(targets.data).cpu().sum() for classification
            loss_total+=loss

            log_dict = {"iter": cur_iter, "loss": loss.item(), "epoch": epoch}
            train_log.write("{}\n".format(json.dumps(log_dict)))
            train_log.flush()

</code></pre>

RaLo4: <p>That’s what I meant.<br>
Try changing:</p>
<aside class="quote no-group" data-username="yichao_Liu" data-post="9" data-topic="89909">
<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/yichao_liu/48/18684_2.png" class="avatar"> yichao_Liu:</div>
<blockquote>
<p><code>loss_total+=loss</code></p>
</blockquote>
</aside>
<p>to:<br>
<code>loss_total+=loss.item()</code></p>

yichao_Liu: <p>Hi, thank you so much, I fixed it. there really has other tensors on the gpu.</p>