I’m a newbie myself. I love PyTorch’s architecture, but I’m tempted to go back to TensorFlow because the documentation and examples are few and fragmentary. For more fragments below, first we create a mean and standard deviation of correct dimenstions
resnet_mean = torch.from_numpy(
np.array([0.485, 0.456, 0.406], dtype=np.float32))
resnet_std = torch.from_numpy(
np.array([0.229, 0.224, 0.225], dtype=np.float32))
# Oh, for below, we need to match the dimensions. If you aren't unsqueezing, you're not pytorching
mean = resnet_mean.unsqueeze(0).unsqueeze(-1) #add a dimenstion for batch and (width*height)
std = resnet_std.unsqueeze(0).unsqueeze(-1) #add a dimenstion for batch and (width*height)
OK, now it gets slightly more complex. Images in pytorch tensors are #channels x height x width but mostly they come in collections or “batches” which means batch x channels x height x width. Let’s assume, via your wrestling with your datalaoder that you have the later. Then, lets reshap that tensor (OK, some programmer wants to call reshape that we all know from numpy as “view” oi vey): Assume we have a tensor of batch x chan x height x width and again, this would presumably be in some data loader normalization function
… tensor enters normalization function …
h, w = tensor.shape[2:]
norm_tensor = tensor.view(tensor.shape[0], tensor.shape[1], -1) #batch x channel x (height*width)
norm_tensor = norm_tensor - mean # Make image mean zero
norm_tensor = norm_tensor / std # Make std = 1
norm_tensor = norm_tensor.view(tensor.shape[0], tensor.shape[1], h, w) #back to batch x chan x w x h
All set, the tensors are now normalized
Now, joining after Arul’s code … presumably this would be the data loader loop that wraps the incoming tensors normalized as above:
output = resnet(norm_tensor)
OK, you have output features from your headless resnet. I think what you really wanted is not the features, but some other trainable head you put on top of the headless resnet … currently grinding through that. gist is to create a model, “foo” then.
Right after Arul’s code, do
foo_resnet = nn.Sequential(resnet(), foo())
And of course, my line above this then becomes:
output = foo_resnet(norm_tensor) #your mileage may vary unchecked code