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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
| import time import copy import os import random import numpy as np import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader import torchvision.transforms as transforms from torchvision.models import alexnet from visdom import Visdom
from utils.data.custom_classifier_dataset import CustomClassifierDataset from utils.data.custom_hard_negative_mining_dataset import CustomHardNegativeMiningDataset from utils.data.custom_batch_sampler import CustomBatchSampler from utils.util import check_dir from utils.util import save_model
batch_positive = 32 batch_negative = 96 batch_total = 128
def load_data(data_root_dir): transform = transforms.Compose([ transforms.ToPILImage(), transforms.Resize((227, 227)), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ])
data_loaders = {} data_sizes = {} remain_negative_list = list() for name in ['train', 'val']: data_dir = os.path.join(data_root_dir, name)
data_set = CustomClassifierDataset(data_dir, transform=transform) if name is 'train': """ 使用hard negative mining方式 初始正负样本比例为1:1。由于正样本数远小于负样本,所以以正样本数为基准,在负样本集中随机提取同样数目负样本作为初始负样本集 """
positive_list = data_set.get_positives() negative_list = data_set.get_negatives() init_negative_idxs = random.sample(range(len(negative_list)), len(positive_list)) init_negative_list = [negative_list[idx] for idx in range(len(negative_list)) if idx in init_negative_idxs] remain_negative_list = [negative_list[idx] for idx in range(len(negative_list)) if idx not in init_negative_idxs]
data_set.set_negative_list(init_negative_list) data_loaders['remain'] = remain_negative_list
sampler = CustomBatchSampler(data_set.get_positive_num(), data_set.get_negative_num(), batch_positive, batch_negative)
data_loader = DataLoader(data_set, batch_size=batch_total, sampler=sampler, num_workers=8, drop_last=True) data_loaders[name] = data_loader data_sizes[name] = len(sampler) return data_loaders, data_sizes
def hinge_loss(outputs, labels): """ 折页损失计算 :param outputs: 大小为(N, num_classes) :param labels: 大小为(N) :return: 损失值 """ num_labels = len(labels) corrects = outputs[range(num_labels), labels].unsqueeze(0).T
margin = 1.0 margins = outputs - corrects + margin loss = torch.sum(torch.max(margins, 1)[0]) / len(labels)
return loss
def add_hard_negatives(hard_negative_list, negative_list, add_negative_list): for item in hard_negative_list: if len(add_negative_list) == 0: negative_list.append(item) add_negative_list.append(list(item['rect'])) if list(item['rect']) not in add_negative_list: negative_list.append(item) add_negative_list.append(list(item['rect']))
def get_hard_negatives(preds, cache_dicts): fp_mask = preds == 1 tn_mask = preds == 0
fp_rects = cache_dicts['rect'][fp_mask].numpy() fp_image_ids = cache_dicts['image_id'][fp_mask].numpy()
tn_rects = cache_dicts['rect'][tn_mask].numpy() tn_image_ids = cache_dicts['image_id'][tn_mask].numpy()
hard_negative_list = [{'rect': fp_rects[idx], 'image_id': fp_image_ids[idx]} for idx in range(len(fp_rects))] easy_negatie_list = [{'rect': tn_rects[idx], 'image_id': tn_image_ids[idx]} for idx in range(len(tn_rects))]
return hard_negative_list, easy_negatie_list
def train_model(data_loaders, model, criterion, optimizer, lr_scheduler, num_epochs=25, device=None): since = time.time()
best_model_weights = copy.deepcopy(model.state_dict()) best_acc = 0.0 viz = Visdom(env='loss and val svm') viz.line(Y=np.column_stack((0., 0.)), X=np.column_stack((0., 0.)), win="{} loss/acc".format('train'), opts=dict(title='{} loss&acc'.format('train'), xlabel='epoch', ylabel='loss/acc', legend=["loss", "acc"])) viz.line(Y=np.column_stack((0., 0.)), X=np.column_stack((0., 0.)), win="{} loss/acc".format('val'), opts=dict(title='{} loss&acc'.format('val'), xlabel='epoch', ylabel='loss/acc', legend=["loss", "acc"]))
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch, num_epochs )) print('-' * 10)
for phase in ['train', 'val']:
if phase == 'train': model.train() else: model.eval()
running_loss = 0.0 running_corrects = 0 batch_i = 0
data_set = data_loaders[phase].dataset print('{} - positive_num: {} - negative_num: {} - data size: {}'.format( phase, data_set.get_positive_num(), data_set.get_negative_num(), data_sizes[phase]))
for inputs, labels, cache_dicts in data_loaders[phase]: inputs = inputs.to(device) labels = labels.to(device)
optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'): outputs = model(inputs) _, preds = torch.max(outputs, 1) loss = criterion(outputs, labels)
if phase == 'train': loss.backward() optimizer.step()
running_loss += loss.item() * inputs.size(0) running_corrects += torch.sum(preds == labels.data) batch_i += 1 print("batch", batch_i, "running_loss_adds=", running_loss)
if phase == 'train': lr_scheduler.step()
epoch_loss = running_loss / data_sizes[phase] epoch_acc = running_corrects.double() / data_sizes[phase]
print('{} Loss: {:.4f} Acc: {:.4f}'.format( phase, epoch_loss, epoch_acc))
viz.line(Y=np.column_stack(([epoch_loss], [epoch_acc])), X=np.column_stack(([epoch + 1], [epoch + 1])), win="{} loss/acc".format(phase), opts=dict(title='{} loss&acc'.format(phase), xlabel='epoch', ylabel='loss/acc', legend=["loss", "acc"]), update="append")
if phase == 'val' and epoch_acc > best_acc: best_acc = epoch_acc best_model_weights = copy.deepcopy(model.state_dict())
train_dataset = data_loaders['train'].dataset remain_negative_list = data_loaders['remain'] jpeg_images = train_dataset.get_jpeg_images() transform = train_dataset.get_transform()
with torch.set_grad_enabled(False): remain_dataset = CustomHardNegativeMiningDataset(remain_negative_list, jpeg_images, transform=transform) remain_data_loader = DataLoader(remain_dataset, batch_size=batch_total, num_workers=8, drop_last=True)
negative_list = train_dataset.get_negatives() add_negative_list = data_loaders.get('add_negative', [])
running_corrects = 0 for inputs, labels, cache_dicts in remain_data_loader: inputs = inputs.to(device) labels = labels.to(device)
optimizer.zero_grad()
outputs = model(inputs) _, preds = torch.max(outputs, 1)
running_corrects += torch.sum(preds == labels.data)
hard_negative_list, easy_neagtive_list = get_hard_negatives(preds.cpu().numpy(), cache_dicts) add_hard_negatives(hard_negative_list, negative_list, add_negative_list)
remain_acc = running_corrects.double() / len(remain_negative_list) print('remiam negative size: {}, acc: {:.4f}'.format(len(remain_negative_list), remain_acc))
train_dataset.set_negative_list(negative_list) tmp_sampler = CustomBatchSampler(train_dataset.get_positive_num(), train_dataset.get_negative_num(), batch_positive, batch_negative) data_loaders['train'] = DataLoader(train_dataset, batch_size=batch_total, sampler=tmp_sampler, num_workers=8, drop_last=True) data_loaders['add_negative'] = add_negative_list
data_sizes['train'] = len(tmp_sampler)
save_model(model, 'models/linear_svm_alexnet_car_%d.pth' % epoch)
time_elapsed = time.time() - since print('Training complete in {:.0f}m {:.0f}s'.format( time_elapsed // 60, time_elapsed % 60)) print('Best val Acc: {:4f}'.format(best_acc))
model.load_state_dict(best_model_weights) return model
if __name__ == '__main__': device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') data_loaders, data_sizes = load_data('./data/classifier_car') model_path = './models/alexnet_car.pth' model = alexnet() num_classes = 2 num_features = model.classifier[6].in_features model.classifier[6] = nn.Linear(num_features, num_classes) model.load_state_dict(torch.load(model_path)) model.eval() for param in model.parameters(): param.requires_grad = False model.classifier[6] = nn.Linear(num_features, num_classes) model = model.to(device) criterion = hinge_loss optimizer = optim.SGD(model.parameters(), lr=1e-4, momentum=0.9) lr_schduler = optim.lr_scheduler.StepLR(optimizer, step_size=4, gamma=0.1) best_model = train_model(data_loaders, model, criterion, optimizer, lr_schduler, num_epochs=25, device=device) save_model(best_model, 'models/best_linear_svm_alexnet_car.pth') print('done')
|