Tensor data type change

salmankh47: <p>Ok. I have something weird going on. I want to calculate perceptual loss of an autoencoder. I store the activation maps of generated image and ground truth  in two different dictionaries. Apparently after converting the activation maps from (Batch x C x H x W) → (Batch x H x W x C) one activation map is of type torch.float32 and the other is float32. But both activation maps goes through same function. How did this happened?<br>
I am attaching the code below</p>
<pre><code class="lang-auto">def enc_percept_loss(model, image, gt):
    enc_activation_img = {}
    enc_activation_gt = {}
    def get_activation_enc(name, dest):
        def hook(model, input, output):
            dest[name] = output.detach()
            
        return hook
    
    
    encoder.convA.register_forward_hook(get_activation_enc('convA', enc_activation_img))
    encoder.l1A.register_forward_hook(get_activation_enc('l1A', enc_activation_img))
    encoder.l2A.register_forward_hook(get_activation_enc('l2A', enc_activation_img))
    encoder.lnA.register_forward_hook(get_activation_enc('lnA', enc_activation_img))
    
    z_img = model(image)
    for k in enc_activation_img.keys():
        enc_activation_img[k] = convert_img(enc_activation_img[k])
        #print(type(enc_activation_img[k]))
    
    
    encoder.convA.register_forward_hook(get_activation_enc('convA', enc_activation_gt))
    encoder.l1A.register_forward_hook(get_activation_enc('l1A', enc_activation_gt))
    encoder.l2A.register_forward_hook(get_activation_enc('l2A', enc_activation_gt))
    encoder.lnA.register_forward_hook(get_activation_enc('lnA', enc_activation_gt))
    
    
   
    
    z_gt = model(gt)
    for k in enc_activation_gt.keys():
        enc_activation_gt[k] = convert_img(enc_activation_gt[k])
        #print(type(enc_activation_gt[k]))

        
    print(enc_activation_img['l1A'].dtype) 
    print(enc_activation_gt['l1A'].dtype) 
    return enc_activation_gt, enc_activation_img
    
</code></pre>
<p>and <code>convert_img</code> is</p>
<pre><code class="lang-auto">def convert_img(input_tensor):
    if input_tensor.device.type == 'cuda':
        input_tensor = input_tensor.to('cpu')
    output_tensor = input_tensor.detach().numpy()
    output_tensor = output_tensor.transpose(0, 2, 3 ,1)
    return output_tensor
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/6/f6f26236099a2143be4230eb36147b826b3eb954.png" data-download-href="https://discuss.pytorch.org/uploads/default/f6f26236099a2143be4230eb36147b826b3eb954" title="discusison"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/6/f6f26236099a2143be4230eb36147b826b3eb954.png" alt="discusison" data-base62-sha1="zeALgc9o63oOsXjV7uJWyj8BFC4" width="690" height="181" data-dominant-color="F2F3F2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">discusison</span><span class="informations">841×221 13.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>The former seems to be a tensor while the latter was transformed into a numpy array.<br>
I guess that the for loops might not be executed in the same way so you could add e.g. print statements to <code>convert_image</code> to verify if both <code>dict</code>s are indeed transformed.</p>