Extract feature from image using '.t7' learned model on PyTorch

Hi, I’m really beginner of neural network library.
I have torch7 learned model saved as model.t7 and I want to extract feature of a image image.jpg.

Following is what I did:

from torch.utils.serialization import load_lua

x = load_lua('model.t7')
print(x)

output

{'mean': [0.5657177752729754, 0.5381838567195789, 0.4972228365504561],
 'std': [0.29023818639817184, 0.2874722565279285, 0.2933830104791508],
 'model': nn.Sequential {
   [input -> (0) -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> (8) -> (9) -> (10) -> (11) -> (12) -> (13) -> (14) -> (15) -> (16) -> (17) -> (18) -> (19) -> (20) -> (21) -> (22) -> (23) -> (24) -> output]
   (0): nn.SpatialConvolution(3 -> 64, 3x3, 1, 1, 1, 1)
   (1): nn.ReLU
   (2): nn.SpatialConvolution(64 -> 64, 3x3, 1, 1, 1, 1)
   (3): nn.ReLU
   (4): nn.Dropout(0.2500)
   (5): nn.SpatialMaxPooling(4x4, 4, 4)
   (6): nn.SpatialBatchNormalization
   (7): nn.SpatialConvolution(64 -> 128, 3x3, 1, 1, 1, 1)
   (8): nn.ReLU
   (9): nn.SpatialConvolution(128 -> 128, 3x3, 1, 1, 1, 1)
   (10): nn.ReLU
   (11): nn.Dropout(0.2500)
   (12): nn.SpatialMaxPooling(4x4, 4, 4)
   (13): nn.SpatialBatchNormalization
   (14): nn.SpatialConvolution(128 -> 256, 3x3, 1, 1, 1, 1)
   (15): nn.ReLU
   (16): nn.SpatialConvolution(256 -> 256, 3x3, 1, 1, 1, 1)
   (17): nn.ReLU
   (18): nn.Dropout(0.2500)
   (19): nn.SpatialMaxPooling(4x4, 4, 4)
   (20): nn.SpatialBatchNormalization
   (21): nn.SpatialConvolution(256 -> 128, 1x1)
   (22): nn.ReLU
   (23): nn.Reshape(3072)
   (24): nn.Linear(3072 -> 128)
 }}

It seems I succesfullly loaded the learned network.
Then what do I have to do to extract 128 dimensional feature vector of image.jpg?

I’d probably use a torchvision imagenet or so example from the pytorch tutorials. You need to do the preprocessing like when using torchvision pretrained models (with x[‘mean’] and x[‘std’]). Then you feed it into x[‘model’].

You might have to transpose the axes (I don’t know) depending on the order the lua model uses (pytorch uses batch x channel x height x with.

Your image will have to have the right dimensions in order to feed into the last linear layer, the feature in Level 22 must have 3072 elements in total.

Best regards

Thomas

Thank you for the reply. As you said, I should learn how to preprocess images so that image can be input to the network.

By the way, Is it possible to use the network(loaded from load_lua()) as a usual PyTorch model?