Obtain features from ArcFace

Pymal: <p>I’m trying to extract features using pytorch implementation of <a href="https://github.com/TreB1eN/InsightFace_Pytorch" rel="nofollow noopener">ArcFace</a></p>
<p>Here is my code:</p>
<pre><code class="lang-auto">import numpy as np
from tqdm import tqdm

import torch
import torch.nn as nn
import torchvision.transforms as transforms
from torchvision import datasets

from model import Backbone

transform = transforms.Compose([
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
    ])

data_path = "../data/CASIA/"
batch_size = 16
num_workers = 16

data = datasets.ImageFolder(data_path, transform=transform)
loader = torch.utils.data.DataLoader(data, 
                                     batch_size=batch_size, 
                                     num_workers=num_workers,
                                     shuffle=True,
                                     pin_memory=True)

model = Backbone(50, 0.6, 'ir_se')
ckpt = torch.load("../pretrained/model_ir_se50.pth")
model.load_state_dict(ckpt)
model.cuda()
model.eval()

features = []
def hook(module, input, output):
    N, C, H, W = output.shape
    output = output.reshape(N, C, -1)
    features.append(output.mean(dim=2).cpu().detach().numpy())

handle = model._modules['body'][23].res_layer[5].fc2.register_forward_hook(hook)
for i_batch, inputs in tqdm(enumerate(loader), total=len(loader)):
    _ = model(inputs[0].cuda())

features = np.concatenate(features)
handle.remove()
</code></pre>
<p>where <code>Backbone</code> model is imported from <a href="https://github.com/TreB1eN/InsightFace_Pytorch/blob/350ff7aa9c9db8d369d1932e14d2a4d11a3e9553/model.py#L109" rel="nofollow noopener">here</a>, the pre-trained model is downloaded from <a href="https://onedrive.live.com/?authkey=%21AOw5TZL8cWlj10I&amp;cid=CEC0E1F8F0542A13&amp;id=CEC0E1F8F0542A13%21835&amp;parId=root&amp;action=locate" rel="nofollow noopener">here</a>, and I’m working with CASIA dataset.</p>
<p>However I get the following error:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "preprocess.py", line 86, in &lt;module&gt;
    feat, labels = extract_feature(args.data_path + args.data_str, args.batch_size, args.workers)
  File "preprocess.py", line 67, in extract_feature
    _ = model(inputs[0].cuda())
  File "/home/saed/.local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/saed/Dropbox/Projects/X_Domain_Clustering/src/arcface.py", line 138, in forward
    x = self.output_layer(x)
  File "/home/saed/.local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/saed/.local/lib/python3.7/site-packages/torch/nn/modules/container.py", line 100, in forward
    input = module(input)
  File "/home/saed/.local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/saed/.local/lib/python3.7/site-packages/torch/nn/modules/linear.py", line 87, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/saed/.local/lib/python3.7/site-packages/torch/nn/functional.py", line 1370, in linear
    ret = torch.addmm(bias, input, weight.t())
RuntimeError: size mismatch, m1: [16 x 131072], m2: [25088 x 512] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:290

</code></pre>
<p>Now I’m wondering if my approach is correct, and if so, how should I resolve this issue?</p>

ptrblck: <p>This error is raised in the <code>output_layer</code>, as the incoming number of features is not matching the defined <code>in_features</code> in this layer.</p>
<p>How did you define the <code>in_features</code>?<br>
If the model was working before, did you change the spatial size of your input?<br>
Also, is your batch size 16? If not, a <code>view</code> or <code>reshape</code> operation might have been called in a wrong way inside your model.</p>

Pymal: <p>Thanks for the reply.<br>
I just managed to fix the error by adding the following line in transform:</p>
<pre><code class="lang-python">transforms.Resize(size=(112, 112))
</code></pre>
<p>Now my code works. I just wonder if the whole approach makes sense in extracting features using a pre-trained model. The use of <code>register_forward_hook</code> and creating handle, for instance.</p>

Azizbek_Urinov: <p>Hello, my name is Azi. from Uzbekistan.My brain is stucked since last week. Please can you explain me what is ArcFace really is? is it loss function?</p>
<p>My real question is : I am writing a code it get ArcFace weights and why I have to use other model’s weights?</p>
<p>Please help me… (<a href="mailto:uacoding01@gmail.com">uacoding01@gmail.com</a>)</p>