Deepdream error: element 0 of tensors does not require grad and does not have a grad_fn

dangmanhtruong1995: <p>Hi everyone.<br>
I’m trying to code the deepdream example, here is my code:</p>
<pre><code class="lang-auto">import numpy as np
from PIL import Image
import glob
import cv2
import os
from os.path import join as pjoin
from pdb import set_trace
import copy
import scipy

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt

import torch
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
import torch.optim as optim
import torch.nn as nn

from torch.utils.data import DataLoader, Dataset, random_split, TensorDataset, WeightedRandomSampler
from torchvision.transforms import Compose, ToTensor, Normalize, Resize, ToPILImage, CenterCrop, RandomResizedCrop
from torchvision import transforms
from torchvision.datasets import ImageFolder
from torchvision.models import resnet18, inception_v3
try:
  from torchvision.models.utils import load_state_dict_from_url
except ImportError:
  from torch.hub import load_state_dict_from_url

import albumentations as albu
from albumentations.pytorch import ToTensorV2

import requests
from PIL import Image
import io
import cv2

from pdb import set_trace
from collections import OrderedDict

# Detect if we have a GPU available
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

URL = "https://i.imgur.com/aGBdQyK.jpg"

response = requests.get(URL).content
im = Image.open(io.BytesIO(response))
img =  np.array(im)
img = img[:, :, ::-1].copy() # RGB to BGR
img = img.astype(np.float32)
img /= 255.0

torch_img = torch.from_numpy(img)
torch_img = torch.permute(torch_img, (2, 0, 1))
torch_img = torch.unsqueeze(torch_img, 0)
torch_img = nn.Parameter(torch_img.clone().requires_grad_(True).to(DEVICE))

pretrained_model = torchvision.models.inception_v3(pretrained=True)
activation = OrderedDict()
def get_activation(name):
    def hook(model, input, output):
        activation[name] = output.detach()
    return hook
pretrained_model.maxpool2.register_forward_hook(get_activation('mixed4'))
pretrained_model.Mixed_5d.register_forward_hook(get_activation('mixed5'))
pretrained_model.Mixed_6e.register_forward_hook(get_activation('mixed6'))
pretrained_model.Mixed_7c.register_forward_hook(get_activation('mixed7'))

pretrained_model = pretrained_model.to(DEVICE)
torch_img = torch_img.to(DEVICE)

opt = optim.Adam([torch_img.requires_grad_()], lr=step)

for iter_idx in range(iterations):
    # Calculate loss
    out = pretrained_model(torch_img)
    activation2 = activation.copy()
    loss = 0
    for layer_name in layer_list:
        scaling = 1.0*activation[layer_name].numel()
        # set_trace()
        rms_loss = nn.MSELoss()
        # loss += (layer_settings[layer_name]*torch.sum(torch.square(activation2[layer_name])))/scaling
        loss += (layer_settings[layer_name]*rms_loss(activation2[layer_name], torch.zeros_like(activation[layer_name]).to(DEVICE)))/scaling
        # set_trace()
    # set_trace()
    loss = (-1)*loss
    # loss.requires_grad = True
#     if loss &gt; max_loss:
#         break
    # Gradient ascent
    opt.zero_grad()
    loss.backward()  
    opt.step()
    
    print("Epoch: %d. Loss: %f" % (iter_idx, loss.item()))
    
    pass
</code></pre>
<p>However, I got this error:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[30], line 95
     90     # loss.requires_grad = True
     91 #     if loss &gt; max_loss:
     92 #         break
     93     # Gradient ascent
     94     opt.zero_grad()
---&gt; 95     loss.backward()  
     96     opt.step()
     98     print("Epoch: %d. Loss: %f" % (iter_idx, loss.item()))

File /opt/conda/lib/python3.10/site-packages/torch/_tensor.py:487, in Tensor.backward(self, gradient, retain_graph, create_graph, inputs)
    477 if has_torch_function_unary(self):
    478     return handle_torch_function(
    479         Tensor.backward,
    480         (self,),
   (...)
    485         inputs=inputs,
    486     )
--&gt; 487 torch.autograd.backward(
    488     self, gradient, retain_graph, create_graph, inputs=inputs
    489 )

File /opt/conda/lib/python3.10/site-packages/torch/autograd/__init__.py:200, in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)
    195     retain_graph = create_graph
    197 # The reason we repeat same the comment below is that
    198 # some Python versions print out the first line of a multi-line function
    199 # calls in the traceback and some print out the last line
--&gt; 200 Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
    201     tensors, grad_tensors_, retain_graph, create_graph, inputs,
    202     allow_unreachable=True, accumulate_grad=True)

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
</code></pre>
<p>Does anyone know how to fix it ? Thanks a lot.</p>

ptrblck: <p>You are explicitly detaching all your stored activations, which will not allow you to backpropagate through them.</p>

dangmanhtruong1995: <p>Hi, thanks a lot, it works now! I copied it from the last code I wrote which ran fine but for some reason in my current code there was .detach() for some reason.</p>