Last active
May 15, 2017 14:23
-
-
Save sheikirfanbasha/74024c2d4506f95a70405ac76d284060 to your computer and use it in GitHub Desktop.
Pre processing the image as per VGG-16 Model requirement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vgg_mean = {103.939, 116.779, 123.68} | |
function check_input(img) | |
assert(img:dim() == 4, 'img must be N x C x H x W') | |
assert(img:size(2) == 3, 'img must have three channels') | |
end | |
function preprocess(img) | |
check_input(img) | |
local mean = img.new(vgg_mean):view(1, 3, 1, 1):expandAs(img) | |
local perm = torch.LongTensor{3, 2, 1} | |
return img:index(2, perm):mul(255):add(-1, mean) | |
end | |
input_img = image.load("hen.jpg", 3, 'float') | |
input_img = image.scale(input_img, 224) | |
H, W = input_img:size(2), input_img:size(3) | |
input_img = input_img:view(1, 3, H, W) | |
print(#input_img) | |
input = preprocess(input_img) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment