Error in VAE Using Image Input

PyTorch2603: <p>Hi everyone. I am trying to create a VAE which takes in different images from a dataset as the input, but am getting an error.</p>
<p>Here is the code:<br>
from <a href="http://skimage.io" rel="noopener nofollow ugc">skimage.io</a> import imread<br>
import matplotlib.pyplot as plt<br>
import matplotlib.gridspec as gridspec<br>
import numpy as np<br>
import torch<br>
import torch.utils.data<br>
from torch import nn, optim<br>
from torch.autograd import Variable<br>
from torch.nn import functional as F<br>
from torchvision import datasets, transforms<br>
from torchvision.utils import save_image<br>
from PIL import Image<br>
import os<br>
import ast<br>
import numpy as np<br>
import pywt<br>
import matplotlib.pyplot as plt<br>
from PIL import Image as PImage<br>
os.chdir(‘D:/bob/’)<br>
SAVEDIR = “D:/bob/myfiles/image/output/”<br>
IMAGEDIR=“D:/bob/myfiles/dataset”<br>
device=“cpu”<br>
class VAE(nn.Module):<br>
def <strong>init</strong>(self, x_dim, h_dim, z_dim):<br>
super(VAE, self).<strong>init</strong>()</p>
<pre><code>    # Encoder
    self.fc1 = nn.Linear(x_dim, h_dim)
    self.fc2_mu = nn.Linear(h_dim, z_dim)
    self.fc2_logvar = nn.Linear(h_dim, z_dim)

    # Decoder
    self.fc3 = nn.Linear(z_dim, h_dim)
    self.fc4 = nn.Linear(h_dim, x_dim)

def encoder(self, x):
    h = F.relu(self.fc1(x))
    return self.fc2_mu(h), self.fc2_logvar(h)

def sampling(self, mu, logvar):
    std = torch.exp(0.5 * logvar)
    eps = torch.randn_like(std)
    return eps.mul(std).add_(mu) # return z sample

def decoder(self, z):
    h = F.relu(self.fc3(z))
    return F.sigmoid(self.fc4(h))

def forward(self, x):
    mu, logvar = self.encoder(x.view(-1, 4096))
    z = self.sampling(mu, logvar)
    return self.decoder(z), mu, 
</code></pre>
<p>vae = VAE(x_dim=4096, h_dim=400, z_dim=20).to(device)<br>
optimizer = optim.Adam(vae.parameters(), lr=0.001)</p>
<h1>
<a name="reconstruction-error-kl-divergence-losses-1" class="anchor" href="#reconstruction-error-kl-divergence-losses-1"></a>reconstruction error + KL divergence losses</h1>
<p>def loss_function(recon_x, x, mu, logvar):<br>
BCE = F.binary_cross_entropy(recon_x, x.view(-1, 784), reduction=‘sum’)<br>
KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())<br>
return BCE + KLD<br>
dirpath = os.listdir(IMAGEDIR)<br>
def train(epoch):<br>
vae.train()<br>
train_loss = 0<br>
for subdir in dirpath:<br>
full_subdir= IMAGEDIR + ‘/’ + subdir + ‘/’<br>
imageList = os.listdir(full_subdir)<br>
for image in imageList:<br>
fullpath = full_subdir + image<br>
inputImage = imread(fullpath)[:,:]<br>
optimizer.zero_grad()<br>
recon_batch, mu, logvar = vae(inputImage)<br>
loss = loss_function(recon_batch, inputImage, mu, logvar)<br>
loss.backward()<br>
train_loss += loss.item()<br>
optimizer.step()<br>
train(7)</p>
<p>When I try to run the code, this is the error I get:<br>
ValueError: Type must be a sub-type of ndarray type<br>
This comes from the line that says:     mu, logvar = self.encoder(x.view(-1, 4096)) in the forward function. Does anyone have any solutions as to how I can resolve this issue so my code will run properly? Thanks!</p>

ptrblck: <p>Based on the error message you are trying to call <code>view</code> on a numpy array instead of a PyTorch tensor, which raises this error since these calls are not performing the same operation:</p>
<pre><code class="lang-python"># works on a tensor
x = torch.randn(2, 2, 4096)
x.view(-1, 4096)

# fails on a numpy array
x = np.random.randn(2, 2, 4096)
x.view(-1, 4096)
# ValueError: Type must be a sub-type of ndarray type
</code></pre>
<p>Pass a tensor to the model and it should work.</p>

PyTorch2603: <p>Oh okay, thank you for the response!</p>