Speeding up a script that uses Img2Vec library

Mona_Jalal: <p>Is there a way to run this across 4 GPUs or speed it up any other way? The script takes a really long time. Also, I have 4 GPUs each with 11G of GPU memory. However, this code only takes ~1G of one of the GPUs.</p>
<pre><code class="lang-auto">from img2vec_pytorch import Img2Vec
from PIL import Image
import torch
import glob
import os

# # Initialize Img2Vec with GPU
img2vec = Img2Vec(cuda=True)
for dirpath, dirname, filename in os.walk('/SeaExp/ABC/'):
    if dirpath.endswith('XYZ'):
        images_full_path = dirpath + '/*.jpeg'
        for img in glob.glob(images_full_path):
            rgb_img = Image.open(img)
            vec = img2vec.get_vec(rgb_img, tensor=True)
            vec_name = os.path.basename(img)[:-5] + '.pt'
            dir_name = os.path.dirname(img) 
            torch.save(vec, dir_name + '/' + vec_name)

</code></pre>
<p>cross-posted here since I thought I’d get more feedback from PyTorch community.</p><aside class="onebox githubissue" data-onebox-src="https://github.com/christiansafka/img2vec/issues/39">
  <header class="source">

      <a href="https://github.com/christiansafka/img2vec/issues/39" target="_blank" rel="noopener nofollow ugc">github.com/christiansafka/img2vec</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/christiansafka/img2vec/issues/39" target="_blank" rel="noopener nofollow ugc">Speeding up a script that uses Img2Vec</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2021-12-01" data-time="03:06:13" data-timezone="UTC">03:06AM - 01 Dec 21 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/monajalal" target="_blank" rel="noopener nofollow ugc">
          <img alt="monajalal" src="https://avatars.githubusercontent.com/u/1892917?v=4" class="onebox-avatar-inline" width="20" height="20">
          monajalal
        </a>
      </div>
    </div>

    <div class="labels">
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">Is there a way to run this across 4 GPUs or speed it up any other way? The scrip<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">t takes a really long time. Also, I have 4 GPUs each with 11G of GPU memory. However, this code only takes ~1G of one of the GPUs.

```
from img2vec_pytorch import Img2Vec
from PIL import Image
import torch
import glob
import os

# # Initialize Img2Vec with GPU
img2vec = Img2Vec(cuda=True)
for dirpath, dirname, filename in os.walk('/SeaExp/ABC/'):
    if dirpath.endswith('XYZ'):
        images_full_path = dirpath + '/*.jpeg'
        for img in glob.glob(images_full_path):
            rgb_img = Image.open(img)
            vec = img2vec.get_vec(rgb_img, tensor=True)
            vec_name = os.path.basename(img)[:-5] + '.pt'
            dir_name = os.path.dirname(img) 
            torch.save(vec, dir_name + '/' + vec_name)

```</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


ptrblck: <p>Try to pass the images as a list instead of each image separately.<br>
Batching the workload would generally increase the device utilization and would yield a speedup.</p>