i just wanna training arcface in pytorch batch size is 20, and image size is 112.
why F.linear() is not working?
this is error message:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (20x30 and 512x30)
class ArcMarginProduct(nn.Module):
def __init__(self, in_features, out_features, s=30.0, m=0.50, easy_margin=False):
super(ArcMarginProduct, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.s = s
self.m = m
self.weight = Parameter(torch.FloatTensor(out_features, in_features))
nn.init.xavier_uniform_(self.weight)
self.easy_margin = easy_margin
self.cos_m = math.cos(m)
self.sin_m = math.sin(m)
self.th = math.cos(math.pi - m)
self.mm = math.sin(math.pi - m) * m
def forward(self, input, label):
# --------------------------- cos(theta) & phi(theta) ---------------------------
cosine = F.linear(F.normalize(input), F.normalize(self.weight))
sine = torch.sqrt((1.0 - torch.pow(cosine, 2)).clamp(0, 1))
phi = cosine * self.cos_m - sine * self.sin_m
if self.easy_margin:
phi = torch.where(cosine > 0, phi, cosine)
else:
phi = torch.where(cosine > self.th, phi, cosine - self.mm)
# --------------------------- convert label to one-hot ---------------------------
# one_hot = torch.zeros(cosine.size(), requires_grad=True, device='cuda')
one_hot = torch.zeros(cosine.size(), device=device)
one_hot.scatter_(1, label.view(-1, 1).long(), 1)
# -------------torch.where(out_i = {x_i if condition_i else y_i) -------------
output = (one_hot * phi) + ((1.0 - one_hot) * cosine) # you can use torch.where if your torch.__version__ is 0.4
output *= self.s
# print(output)
return output
this is my training code
model = resnet18
optimizer = optim.Adam(model.parameters(), lr=1e-3 ) # optimizer
criterion = torch.nn.CrossEntropyLoss() # loss func
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size = 5, gamma = 0.9)
metric_fc = ArcMarginProduct(512, len(col_list), s=30, m=0.50, easy_margin=False).to(device)
start = time.time()
for epoch in range(num_epochs):
e_time = time.time()
for i, (images, label) in enumerate(trainloader):
model.train()
images = images.to(device)
label = label.to(device)
optimizer.zero_grad()
feature = model(images)
outputs = metric_fc(feature, label)
loss = criterion(outputs, label)
loss.backward()
optimizer.step()
if (i+1) % num_epochs == 0:
print(f'Epoch: {epoch} - Batch: {i*batch_size} - Loss: {loss:.6f} - Time:{time.time() - e_time}')
print("time :", time.time() - start)
and, Please let me know if you have a simple example to test arcface training with data.