Reconstructing input image from layers of a CNN












0












$begingroup$


I've been trying to implement neural style transfer as described in this paper here According to the paper,




we can visualise the information at different processing stages in the
CNN by reconstructing the input image from only knowing the network’s
responses in a particular layer.




My question is, how exactly does one go about reconstructing image from a single layer?
I'm implementing this in pytorch. I've the output from layer conv4_2 stored in a tensor of shape [1,512,50,50] but how do I visualize this?



Here's a part of my code, if that helps.



vgg = models.vgg19(pretrained=True).features

for param in vgg.parameters():
param.requires_grad_(False)

device = torch.device("cpu")

vgg.to(device)

content_img = Image.open("image3.jpg").convert('RGB')
style_img = Image.open("image5.jpg").convert('RGB')
content_img = transformation(content_img).to(device)
style_img = transformation(style_img).to(device)

def get_features(image, model):

layers = {'0': 'conv1_1', '5': 'conv2_1', '10': 'conv3_1',
'19': 'conv4_1', '21': 'conv4_2', '28': 'conv5_1'}
x = image
features = {}

for name, layer in model._modules.items():
x = layer(x)

if name in layers:
features[layers[name]] = x

return features

content_img_features = get_features(content_img, vgg)
style_img_features = get_features(style_img, vgg)

target_content = content_img_features['conv4_2']


How do I reconstruct the image from the output of conv4_2?









share









$endgroup$

















    0












    $begingroup$


    I've been trying to implement neural style transfer as described in this paper here According to the paper,




    we can visualise the information at different processing stages in the
    CNN by reconstructing the input image from only knowing the network’s
    responses in a particular layer.




    My question is, how exactly does one go about reconstructing image from a single layer?
    I'm implementing this in pytorch. I've the output from layer conv4_2 stored in a tensor of shape [1,512,50,50] but how do I visualize this?



    Here's a part of my code, if that helps.



    vgg = models.vgg19(pretrained=True).features

    for param in vgg.parameters():
    param.requires_grad_(False)

    device = torch.device("cpu")

    vgg.to(device)

    content_img = Image.open("image3.jpg").convert('RGB')
    style_img = Image.open("image5.jpg").convert('RGB')
    content_img = transformation(content_img).to(device)
    style_img = transformation(style_img).to(device)

    def get_features(image, model):

    layers = {'0': 'conv1_1', '5': 'conv2_1', '10': 'conv3_1',
    '19': 'conv4_1', '21': 'conv4_2', '28': 'conv5_1'}
    x = image
    features = {}

    for name, layer in model._modules.items():
    x = layer(x)

    if name in layers:
    features[layers[name]] = x

    return features

    content_img_features = get_features(content_img, vgg)
    style_img_features = get_features(style_img, vgg)

    target_content = content_img_features['conv4_2']


    How do I reconstruct the image from the output of conv4_2?









    share









    $endgroup$















      0












      0








      0





      $begingroup$


      I've been trying to implement neural style transfer as described in this paper here According to the paper,




      we can visualise the information at different processing stages in the
      CNN by reconstructing the input image from only knowing the network’s
      responses in a particular layer.




      My question is, how exactly does one go about reconstructing image from a single layer?
      I'm implementing this in pytorch. I've the output from layer conv4_2 stored in a tensor of shape [1,512,50,50] but how do I visualize this?



      Here's a part of my code, if that helps.



      vgg = models.vgg19(pretrained=True).features

      for param in vgg.parameters():
      param.requires_grad_(False)

      device = torch.device("cpu")

      vgg.to(device)

      content_img = Image.open("image3.jpg").convert('RGB')
      style_img = Image.open("image5.jpg").convert('RGB')
      content_img = transformation(content_img).to(device)
      style_img = transformation(style_img).to(device)

      def get_features(image, model):

      layers = {'0': 'conv1_1', '5': 'conv2_1', '10': 'conv3_1',
      '19': 'conv4_1', '21': 'conv4_2', '28': 'conv5_1'}
      x = image
      features = {}

      for name, layer in model._modules.items():
      x = layer(x)

      if name in layers:
      features[layers[name]] = x

      return features

      content_img_features = get_features(content_img, vgg)
      style_img_features = get_features(style_img, vgg)

      target_content = content_img_features['conv4_2']


      How do I reconstruct the image from the output of conv4_2?









      share









      $endgroup$




      I've been trying to implement neural style transfer as described in this paper here According to the paper,




      we can visualise the information at different processing stages in the
      CNN by reconstructing the input image from only knowing the network’s
      responses in a particular layer.




      My question is, how exactly does one go about reconstructing image from a single layer?
      I'm implementing this in pytorch. I've the output from layer conv4_2 stored in a tensor of shape [1,512,50,50] but how do I visualize this?



      Here's a part of my code, if that helps.



      vgg = models.vgg19(pretrained=True).features

      for param in vgg.parameters():
      param.requires_grad_(False)

      device = torch.device("cpu")

      vgg.to(device)

      content_img = Image.open("image3.jpg").convert('RGB')
      style_img = Image.open("image5.jpg").convert('RGB')
      content_img = transformation(content_img).to(device)
      style_img = transformation(style_img).to(device)

      def get_features(image, model):

      layers = {'0': 'conv1_1', '5': 'conv2_1', '10': 'conv3_1',
      '19': 'conv4_1', '21': 'conv4_2', '28': 'conv5_1'}
      x = image
      features = {}

      for name, layer in model._modules.items():
      x = layer(x)

      if name in layers:
      features[layers[name]] = x

      return features

      content_img_features = get_features(content_img, vgg)
      style_img_features = get_features(style_img, vgg)

      target_content = content_img_features['conv4_2']


      How do I reconstruct the image from the output of conv4_2?







      neural-network cnn convolution pytorch neural-style-transfer





      share












      share










      share



      share










      asked 2 mins ago









      Judy T RajJudy T Raj

      1161




      1161






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "557"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f47992%2freconstructing-input-image-from-layers-of-a-cnn%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Data Science Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f47992%2freconstructing-input-image-from-layers-of-a-cnn%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Aikido

          The minimum number of groups for any class cannot be less than 2 error

          SMOTE: ValueError: Expected n_neighbors <= n_samples, but n_samples = 1, n_neighbors = 6