Cnn with training image from folder in desktop

Sam3: <p>Hi ,</p>
<p>i made cnn with 6 conv layers and i want to train it by images in Folder in desktop but i have this error:<br>
RuntimeError: Found 0 files in subfolders of: /Users/lionardo/Desktop/Training/1<br>
Supported extensions are: .jpg,.jpeg,.png,.ppm,.bmp,.pgm,.tif,.tiff,.webp.</p>
<p>I don,t understand why this error?</p>
<p>My code:<br>
<span class="hashtag">#Import</span> packages<br>
import torch.nn as nn<br>
import torch<br>
from torch.utils.data import Dataset, DataLoader</p>
<p>import numpy as np<br>
from torchvision import transforms<br>
from torch.autograd import Variable<br>
from PIL import Image<br>
import torchvision.transforms.functional as TF<br>
from torchvision.models.vgg import vgg19<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
import torch<br>
import torch.nn as nn<br>
import torch.utils.data as Data<br>
import torchvision      # dataset<br>
import matplotlib.pyplot as plt<br>
import scipy<br>
import numpy as np<br>
import argparse<br>
import numpy<br>
import scipy<br>
from pytorch_msssim import ssim, ms_ssim, SSIM, MS_SSIM<br>
import pprint<br>
from scipy.ndimage import correlate<br>
from scipy.ndimage.filters import gaussian_gradient_magnitude</p>
<p><span class="hashtag">#reproducible</span><br>
torch.manual_seed(1)</p>
<h1>Hyper Parameters</h1>
<p>EPOCH = 1<br>
BATCH_SIZE = 50<br>
LR = 0.001<br>
def load_dataset():<br>
data_path = ‘/Users/lionardo/Desktop/Training/PET/1’<br>
data_path = ‘/Users/lionardo/Desktop/Training/MRI/2’<br>
train_dataset = torchvision.datasets.ImageFolder(<br>
root=data_path,<br>
transform=torchvision.transforms.ToTensor()<br>
)</p>
<pre><code>train_loader = torch.utils.data.DataLoader(
    train_dataset,
    batch_size=50,
    num_workers=0,
    shuffle=True
)
return train_loader
</code></pre>
<p><span class="hashtag">#for</span> batch_idx, (data, target) in enumerate(load_dataset()):</p>
<p><span class="hashtag">#train</span> data</p>
<p><span class="hashtag">#Dataset</span> MNIST</p>
<h1>Number of datapoints: 60000</h1>
<h1>Root location: ./mnist/</h1>
<h1>Split: Train</h1>
<pre><code>#StandardTransform
</code></pre>
<p><span class="hashtag">#Transform:</span> ToTensor()</p>
<pre><code>#testing data
</code></pre>
<p>test_data = torchvision.datasets.ImageFolder(root= ‘/Users/lionardo/Desktop/Training/1’)</p>
<p>test_data = torchvision.datasets.ImageFolder( root= ‘/Users/lionardo/Desktop/Training/2’)</p>
<p><span class="hashtag">#load</span> the data<br>
train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)</p>
<p><span class="hashtag">#define</span> the test data#<br>
test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor)[:200]/255. <span class="hashtag">#shape</span> from (2000, 28, 28) to (2000, 1, 28, 28), value in range (0,1)</p>
<p>test_y = test_data.test_labels[:200]</p>
<p><span class="hashtag">#define</span> the network<br>
class CNN(nn.Module):<br>
def  <strong>init</strong>(self):<br>
super(CNN, self).<strong>init</strong>()<br>
<span class="hashtag">#mri</span> lf layer 1<br>
self.conv1 = nn.Sequential(  # input shape (1, 28, 28)<br>
nn.Conv2d(<br>
in_channels=1,      # input height<br>
out_channels=16,    # n_filters<br>
kernel_size=9, # filter size<br>
stride=1,           # filter movement/step<br>
padding=2           #<br>
))</p>
<pre><code>    self.batch1 = nn.BatchNorm2d(16),
           #output shape (16, 28, 28)
    self.relu1 =  nn.LeakyReLU(0.2, True)    #activation
    
    #mri hf layer 1 
    self.conv2 = nn.Sequential(  # input shape (16, 28, 28)
        nn.Conv2d(
            in_channels  = 16,
            out_channels = 32, 
            kernel_size  = 9,
            stride       = 1,
            padding      = 2
         ))
    
    self.batch2 = nn.BatchNorm2d(32),      #output shape (1,28,28)
    self.relu2 = nn.LeakyReLU(0.2,True)
    
    self.conv3 = nn.Sequential(  # input shape (16, 28, 28)
        nn.Conv2d(
            in_channels  = 32,
            out_channels = 64, 
            kernel_size  = 9,
            stride       = 1,
            padding      = 2
             ))
    self.batch3 = nn.BatchNorm2d(64),      #output shape (1,28,28)
    self.relu3 = nn.LeakyReLU(0.2,True)
    
    self.conv4 = nn.Sequential(  # input shape (16, 28, 28)
        nn.Conv2d(
            in_channels  = 64,
            out_channels = 32, 
            kernel_size  = 9,
            stride       = 1,
            padding      = 2
             ))
    
    self.batch4 = nn.BatchNorm2d(32),      #output shape (1,28,28)
    self.relu4 = nn.LeakyReLU(0.2,True)
    
    self.conv5 = nn.Sequential(  # input shape (16, 28, 28)
        nn.Conv2d(
            in_channels  = 32,
            out_channels = 16, 
            kernel_size  = 9,
            stride       = 1,
            padding      = 2
        ))
    
    self.batch5 = nn.BatchNorm2d(16),      #output shape (1,28,28)
    self.relu5 = nn.LeakyReLU(0.2,True)
    
    self.conv6 = nn.Sequential(      # input shape (16, 28, 28)
        nn.Conv2d(
            in_channels  = 16,
            out_channels = 1, 
            kernel_size  = 9,
            stride       = 1,
            padding      = 2
             ))
    
    self.batch6 = nn.BatchNorm2d(1),      #output shape (1,28,28)
    self.relu6 = nn.LeakyReLU(0.2,True)
    

def forward(self, x,y):
   
    
    x1 = x.reshape(-1,1,28,28) #careful: need to change by yourself
    x1= self.conv1(x)
    x1 = self.batch1(x)
    x1 = slef.relu1(x)
    # x = x.reshape(-1,1,28,28) #careful: need to change by yourself
    x1 = self.conv2(x)
    x1 = self.batch2(x)
    x1 = slef.relu2(x)
    x1 = self.conv3(x)
    x1 = self.batch3(x)
    x1 = slef.relu3(x)
    x1 = self.conv4(x)
    x1 = self.batch4(x)
    x1 = slef.relu4(x)
    x1 = self.conv5(x)
    x1 = self.batch5(x)
    x1 = slef.relu5(x)
    x1 = self.conv6(x)
    x1 = self.batch6(x)
    x1 = slef.relu6(x)
    
    x2 = y.reshape(-1,1,28,28) #careful: need to change by yourself
    x2= self.conv1(y)
    x2 = self.batch1(y)
    x2 = slef.relu1(y)
    # x = x.reshape(-1,1,28,28) #careful: need to change by yourself
    x2 = self.conv2(y)
    x2 = self.batch2(y)
    x2 = slef.relu2(y)
    x2 = self.conv3(y)
    x2 = self.batch3(y)
    x2 = slef.relu3(y)
    x2 = self.conv4(y)
    x2 = self.batch4(y)
    x2 = slef.relu4(y)
    x2 = self.conv5(y)
    x2 = self.batch5(y)
    x2 = slef.relu5(y)
    x2 = self.conv6(y)
    x2 = self.batch6(y)
    x2 = slef.relu6(y)
    
    x = torch.cat((x1, x2), 1)
    x = torch.tanh(x)
    x = x.reshape(-1)
    #x = x.view(x.size(0), -1)   #  (batch_size, 32 * 7 * 7)
    return x

    #execute the network
</code></pre>
<h1>reference:</h1>
<h1><a href="https://medium.com/unit8-machine-learning-publication/computing-the-jacobian-matrix-of-a-neural-network-in-python-4f162e5db180" rel="nofollow noopener">https://medium.com/unit8-machine-learning-publication/computing-the-jacobian-matrix-of-a-neural-network-in-python-4f162e5db180</a></h1>
<h1><a href="https://stackoverflow.com/questions/59161001/using-autograd-to-compute-jacobian-matrix-of-outputs-with-respect-to-inputs" rel="nofollow noopener">https://stackoverflow.com/questions/59161001/using-autograd-to-compute-jacobian-matrix-of-outputs-with-respect-to-inputs</a></h1>
<p>cnn = CNN()<br>
print(cnn)</p>
<p><span class="hashtag">#define</span> the optimizers and loss functions<br>
optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)   # optimize all cnn parameters<br>
loss_func = SSIM()  # the target label is not one-hotted</p>
<h1>training</h1>
<p>for epoch in range(EPOCH):<br>
for step, (b_x , b_y) in enumerate(train_loader):<br>
<span class="hashtag">#b_x</span> = b_x.reshape(-1)<br>
# b_y= b_y.reshape(-1)<br>
output = cnn(b_x+b_y)               # cnn output<br>
loss = loss_func(output, b_x)+ loss_func(output,b_y)#+ cross entropy loss<br>
optimizer.zero_grad()           # clear gradients for this training step<br>
loss.backward()                 # backpropagation, compute gradients<br>
optimizer.step()                # apply gradients</p>
<pre><code>    #upload a test image
</code></pre>
<p>image = Image.open(’/Users/lionardo/Desktop/MRI.png’)<br>
<span class="hashtag">#image</span> = Image.open(‘test.jpeg’).convert(‘L’)<br>
x1 = TF.to_tensor(image)<br>
x1 = x1.unsqueeze(0)<br>
x1 = x1.reshape(-1)<br>
x1.requires_grad = True</p>
<p>print(x1.shape)</p>
<p>image = Image.open(’/Users/lionardo/Desktop/PET.png’)<br>
<span class="hashtag">#image</span> = Image.open(‘test.jpeg’).convert(‘L’)<br>
x2 = TF.to_tensor(image)<br>
x2 = x2.unsqueeze(0)<br>
x2 = x2.reshape(-1)<br>
x2.requires_grad = True</p>
<p>y = cnn(x1+x2)</p>

simaiden: <p>Try with using the full path. eg C:/Users/lionardo/Desktop/Training/</p>