Reconstructing input image from layers of a CNN
$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?
neural-network cnn convolution pytorch neural-style-transfer
$endgroup$
add a comment |
$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?
neural-network cnn convolution pytorch neural-style-transfer
$endgroup$
add a comment |
$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?
neural-network cnn convolution pytorch neural-style-transfer
$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
neural-network cnn convolution pytorch neural-style-transfer
asked 2 mins ago
Judy T RajJudy T Raj
1161
1161
add a comment |
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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