-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
151 lines (120 loc) · 5.07 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''=================================================
@Project -> File :L5 -> train
@IDE :PyCharm
@Author :DIPTE
@Contect : 2563640356@qq.com
@Date :2020/8/2 2:21
@Desc :
=================================================='''
import os, sys
sys.path.append('.')
import time
import argparse
import numpy as np
import torch
import torch.nn as nn
import torchvision.transforms as tfs
from torch.utils.data import DataLoader
from torchsummary import summary
from LFW_CASIAWebFace_Dataset import CASIAWebFace, LFW,SiameseCASIAWebFace,TripletFaceDataset
from SEResNet_IR import ResNet18, ResNet34
# from trainer import Trainer
# from Contrastive_trainer import Trainer##using Contrastive
from Triplet_trainer import Trainer##using Triplet
from loss_function import NormFace, SphereFace, CosFace, ArcFace
from modified_resnet import resnet18, resnet34
from loss_function import *
# margin type
margin_type = 'NormFace'
feature_dim = 512
#criterion set
criterion='Triplet'
print(margin_type,criterion)
#Softmax、NormFace、SphereFace、CosFace、ArcFace、Contrastive、Triplet、OHEM、FocalLoss
# training config
BATCH_SIZE = 8#16#32#64#128#256
LR = 1e-3
EPOCHS = 20
STEPS = [4, 10, 18]
USE_CUDA = True
# Set training device
use_cuda = USE_CUDA and torch.cuda.is_available()
device = torch.device('cuda' if torch.cuda.is_available() and use_cuda else 'cpu')
torch.backends.cudnn.benchmark = True
print(device)
# Set dataloader
transforms = tfs.Compose([
tfs.ToPILImage(),
tfs.Resize((112, 112)),###resize
tfs.ToTensor(),
tfs.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
kwargs = {'num_workers': 0, 'pin_memory': True} if use_cuda else {}
train_data = TripletFaceDataset(root_dir='CASIA-maxpy-clean', csv_name='CASIAWebFace.csv', num_triplets=30000, training_triplets_path='datasets/training_triplets_30000.npy', transform=transforms)##using Triplet
# train_data = SiameseCASIAWebFace('CASIA-maxpy-clean', 'CASIA_anno.txt',transforms)##using Contrastive
# train_data = CASIAWebFace('CASIA-maxpy-clean', 'CASIA_anno.txt',transforms)
lfw_data = LFW('./LFW/lfw_align_112', './LFW/pairs.txt', transforms)
val_data = None
dataloaders = {'train': DataLoader(train_data, batch_size=BATCH_SIZE,shuffle=True, **kwargs),
'LFW': DataLoader(lfw_data, batch_size=BATCH_SIZE,shuffle=False, **kwargs) }
ckpt_tag = 'se_resnet18'
# Set model
model = ResNet18()
# model = resnet18(pretrained=False,loss_fn='triplet',num_classes=256 )
summary(model.cuda(), (3, 112, 112))
model = model.to(device)
# model.load_state_dict(torch.load('./checkpoints/se_resnet18_CosFace_Contrastive_best.pth',map_location='cpu'))
# print(margin_type)
margin=margin_type
# '''
# Set margin
if margin_type == 'Softmax':
margin = nn.Linear(feature_dim, train_data.num_class)
elif margin_type == 'NormFace':
margin = NormFace(feature_dim, train_data.num_class)
elif margin_type == 'SphereFace':
margin = SphereFace(feature_dim, train_data.num_class)
elif margin_type == 'CosFace':
margin = CosFace(feature_dim, train_data.num_class)
elif margin_type == 'ArcFace':
margin = ArcFace(feature_dim, train_data.num_class)
else:
raise NameError("Margin Not Supported!")
margin = margin.to(device)
# margin.load_state_dict(torch.load('./checkpoints/se_resnet18_512_CosFace_Contrastive_best.pth',map_location='cpu'))
#Set criterion
if criterion=='Softmax' or criterion=='NormFace' or criterion=='SphereFace'or criterion=='CosFace'or criterion=='ArcFace':
criterion = torch.nn.CrossEntropyLoss().to(device)
elif criterion=='OHEM':
criterion = OHEMCrossEntropyLoss(0.75).to(device)
elif criterion=='FocalLoss':
criterion = FocalCrossEntropyLoss(3, 0.5).to(device)
elif criterion=='Contrastive':
criterion = ContrastiveLoss(margin=0.8).to(device)
elif criterion=='Triplet':
criterion = None#torch.nn.CrossEntropyLoss().to(device)
# criterion = torch.nn.NLLLoss().to(device)
# if criterion=='Softmax' or criterion=='NormFace' or criterion=='SphereFace'or criterion=='CosFace'or criterion=='ArcFace':
# print('ok')
# if margin_type == 'Softmax'or margin_type == 'NormFace'or margin_type == 'SphereFace'or margin_type == 'CosFace'or margin_type == 'ArcFace':
# from trainer import Trainer
# print(margin_type,criterion)
# else:
# pass
# elif criterion=='Contrastive':
# from Contrastive_trainer import Trainer
# print(margin_type, criterion)
# elif criterion=='Triplet':
# from Triplet_trainer import Trainer
# print(margin_type, criterion)
# Set optimizer
optimizer = torch.optim.Adam([{'params': model.parameters()},
{'params': margin.parameters()}], lr=LR)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer,
milestones=STEPS, gamma=0.1)
# Set trainer
trainer =Trainer(EPOCHS, dataloaders, model, optimizer, scheduler, device,
margin, ckpt_tag,criterion)
trainer.train()