RuntimeError , no enough ram

Alymostafa: <p>i use this CNN architecture to classify images (input : 224,224) , (output: 2 )</p>
<pre><code class="lang-auto">class ConvNet(nn.Module):
    def init(self, num_classes=2):
        super(ConvNet, self).init()
        self.layer1 = nn.Sequential(
            nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.layer2 = nn.Sequential(
            nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
            #nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=5, stride=0))

        self.drop_out = nn.Dropout()

        self.fc1 = nn.Linear(15488, 1000)
        self.fc2 = nn.Linear(1000 ,2)

    def forward(self, x):
        x.float()
        #x = x.permute(0,3,1,2)
        out = self.layer1(x)
        out = self.layer2(out)
        out = out.reshape(out.size(0), -1)
        out = self.drop_out(out)
        out = self.fc1(out)
        out = self.fc2(out)

        return out
</code></pre>
<p>and it gives me :</p>
<pre><code class="lang-auto">RuntimeError: [enforce fail at ..\c10\core\CPUAllocator.cpp:72] data. DefaultCPUAllocator: not enough memory: you tried to allocate 38816153600 bytes. Buy new RAM!
</code></pre>
<p>cpu : intel xeon® e5-2696 v3 , 2.30ghz<br>
ram : 110gb</p>

ptrblck: <p>How large is your batch size to create this error and how much of your RAM is already in use when running this script?</p>

Alymostafa: <p>batch size : 5</p>
<aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="77379">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>how much of your RAM is already in use when running this script?</p>
</blockquote>
</aside>
<p>110 of 110</p>

ptrblck: <p>If your main RAM is already filled before executing this script, how much swap do you have?<br>
Note that using the swap will hit your performance pretty hard.</p>

Alymostafa: <p>before running this script ram is : 5 of 110<br>
after running  this script ram is : 110 of 110<br>
memory : 96<br>
with swap : 120</p>

ptrblck: <p>For an input of <code>[5, 1, 224, 224]</code>, your forward pass will use ~120MB.</p>
<p>Could you post an executable code snippet to reproduce the &gt;105GB memory allocation?</p>

Alymostafa: <aside class="quote no-group" data-username="ptrblck" data-post="6" data-topic="77379">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>Could you post an executable code snippet to reproduce the &gt;105GB memory allocation?</p>
</blockquote>
</aside>
<p>can you specify what do you mean by an executable code snippet to reproduce the &gt;105GB memory allocation ?</p>

ptrblck: <p>A code snippet I could run on my machine to reproduce the large memory allocation.<br>
At the moment I’m using your model definition and a random input in the shape <code>[5, 1, 224, 224]</code> in the forward pass, which only uses 120MB, which is far off of the reported 105GB.</p>

Alymostafa: <p>I couldn’t fully know which piece of code should be sent so this is the full repo on github.<br>
please before you run it make sure to put the “covid” folder alongside the “other” folder in a folder called “data”<br>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="16" height="16">
      <a href="https://github.com/lnBrTgAaaL/Pattern-Rocognition-COVID-19" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars0.githubusercontent.com/u/58595056?s=400&amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/lnBrTgAaaL/Pattern-Rocognition-COVID-19" target="_blank" rel="nofollow noopener">lnBrTgAaaL/Pattern-Rocognition-COVID-19</a></h3>

<p>Contribute to lnBrTgAaaL/Pattern-Rocognition-COVID-19 development by creating an account on GitHub.</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<br>
your help is very much appreciated</p>

ptrblck: <p>It seems you are directly loading all images to your RAM in this cell:</p>
<pre><code class="lang-python">print("[INFO] loading images...")
imagePaths = list(paths.list_images(args["dataset_1"]))
data = []
labels = []
for imagePath in imagePaths:
    label = imagePath.split(os.path.sep)[-2]
    image = cv2.imread(imagePath)
    image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    image = cv2.resize(image, (224, 224))
    data.append(image)
    labels.append(label)
data = np.array(data) / 255.0
labels = np.array(labels)
</code></pre>
<p>Depending on the number of images and their size, this might take a lot of memory.<br>
If you are dealing with a large dataset, it’s recommended to lazily load the batches using a <code>Dataset</code> and <code>DataLoader</code> as described in <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html">this tutorial</a>.<br>
How many images does your dataset have and how large is each image after preprocessing?</p>

Alymostafa: <p>1075 images with an average of 400kb \ image , i am using this so i can make sure that the picture is in grayscaler not RBG.so what can we use instead for this matter ?</p>