Atcold / NYU-DLSP20

NYU Deep Learning Spring 2020
https://atcold.github.io/NYU-DLSP20/
Other
6.66k stars 2.22k forks source link

Autoencoders code: NameError: name 'img_bad' is not defined. How to fix? #841

Closed ghost closed 1 year ago

ghost commented 1 year ago

Hi!

When I'm trying to run the code 10-autoencoder.ipynb I get an error in the second to last cell, which is:

# Inpaint with Telea and Navier-Stokes methods

dst_TELEA = list()
dst_NS = list()

for i in range(3, 7):
    corrupted_img = ((img_bad.data.cpu()[i].view(28, 28) / 4 + 0.5) * 255).byte().numpy()
    mask = 2 - noise.cpu()[i].view(28, 28).byte().numpy()
    dst_TELEA.append(inpaint(corrupted_img, mask, 3, INPAINT_TELEA))
    dst_NS.append(inpaint(corrupted_img, mask, 3, INPAINT_NS))

tns_TELEA = [torch.from_numpy(d) for d in dst_TELEA]
tns_NS = [torch.from_numpy(d) for d in dst_NS]

TELEA = torch.stack(tns_TELEA).float()
NS = torch.stack(tns_NS).float()

The error saying:

NameError                                 Traceback (most recent call last)
Cell In[24], line 7
      4 dst_NS = list()
      6 for i in range(3, 7):
----> 7     corrupted_img = ((img_bad.data.cpu()[i].view(28, 28) / 4 + 0.5) * 255).byte().numpy()
      8     mask = 2 - noise.cpu()[i].view(28, 28).byte().numpy()
      9     dst_TELEA.append(inpaint(corrupted_img, mask, 3, INPAINT_TELEA))

NameError: name 'img_bad' is not defined

How could I fix this? Thanks!

Atcold commented 1 year ago

If you read the notebook you'll see at some point:

Comment or un-comment out a few lines of code to seamlessly switch between standard AE and denoising one.

# Train standard or denoising autoencoder (AE)

num_epochs = 20
# do = nn.Dropout()  # comment out for standard AE
for epoch in range(num_epochs):
    for data in dataloader:
        img, _ = data
        img = img.to(device)
        img = img.view(img.size(0), -1)
        # noise = do(torch.ones(img.shape)).to(device)
        # img_bad = (img * noise).to(device)  # comment out for standard AE
        # ===================forward=====================
        output = model(img)  # feed <img> (for std AE) or <img_bad> (for denoising AE)
        loss = criterion(output, img.data)
Atcold commented 1 year ago

Here you can find a more updated version of the notebook, used in a more recent edition of the course.