加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
train.py 9.02 KB
一键复制 编辑 原始数据 按行查看 历史
Bubbliiiing 提交于 2020-11-25 13:11 . Add files via upload
from nets.frcnn import FasterRCNN
from nets.frcnn_training import Generator
from torch.autograd import Variable
from trainer import FasterRCNNTrainer
import time
import numpy as np
import torch
import torch.optim as optim
import torch.backends.cudnn as cudnn
from tqdm import tqdm
from torch.utils.data import DataLoader
from utils.dataloader import FRCNNDataset, frcnn_dataset_collate
def get_lr(optimizer):
for param_group in optimizer.param_groups:
return param_group['lr']
def fit_ont_epoch(net,epoch,epoch_size,epoch_size_val,gen,genval,Epoch,cuda):
total_loss = 0
rpn_loc_loss = 0
rpn_cls_loss = 0
roi_loc_loss = 0
roi_cls_loss = 0
val_toal_loss = 0
with tqdm(total=epoch_size,desc=f'Epoch {epoch + 1}/{Epoch}',postfix=dict,mininterval=0.3) as pbar:
for iteration, batch in enumerate(gen):
if iteration >= epoch_size:
break
imgs,boxes,labels = batch[0], batch[1], batch[2]
with torch.no_grad():
if cuda:
imgs = Variable(torch.from_numpy(imgs).type(torch.FloatTensor)).cuda()
boxes = [Variable(torch.from_numpy(box).type(torch.FloatTensor)).cuda() for box in boxes]
labels = [Variable(torch.from_numpy(label).type(torch.FloatTensor)).cuda() for label in labels]
else:
imgs = Variable(torch.from_numpy(imgs).type(torch.FloatTensor))
boxes = [Variable(torch.from_numpy(box).type(torch.FloatTensor)) for box in boxes]
labels = [Variable(torch.from_numpy(label).type(torch.FloatTensor)) for label in labels]
losses = train_util.train_step(imgs, boxes, labels, 1)
rpn_loc, rpn_cls, roi_loc, roi_cls, total = losses
total_loss += total
rpn_loc_loss += rpn_loc
rpn_cls_loss += rpn_cls
roi_loc_loss += roi_loc
roi_cls_loss += roi_cls
pbar.set_postfix(**{'total' : total_loss.item() / (iteration + 1),
'rpn_loc' : rpn_loc_loss.item() / (iteration + 1),
'rpn_cls' : rpn_cls_loss.item() / (iteration + 1),
'roi_loc' : roi_loc_loss.item() / (iteration + 1),
'roi_cls' : roi_cls_loss.item() / (iteration + 1),
'lr' : get_lr(optimizer)})
pbar.update(1)
print('Start Validation')
with tqdm(total=epoch_size_val, desc=f'Epoch {epoch + 1}/{Epoch}',postfix=dict,mininterval=0.3) as pbar:
for iteration, batch in enumerate(genval):
if iteration >= epoch_size_val:
break
imgs,boxes,labels = batch[0], batch[1], batch[2]
with torch.no_grad():
if cuda:
imgs = Variable(torch.from_numpy(imgs).type(torch.FloatTensor)).cuda()
boxes = [Variable(torch.from_numpy(box).type(torch.FloatTensor)).cuda() for box in boxes]
labels = [Variable(torch.from_numpy(label).type(torch.FloatTensor)).cuda() for label in labels]
else:
imgs = Variable(torch.from_numpy(imgs).type(torch.FloatTensor))
boxes = [Variable(torch.from_numpy(box).type(torch.FloatTensor)) for box in boxes]
labels = [Variable(torch.from_numpy(label).type(torch.FloatTensor)) for label in labels]
train_util.optimizer.zero_grad()
losses = train_util.forward(imgs, boxes, labels, 1)
_,_,_,_, val_total = losses
val_toal_loss += val_total
pbar.set_postfix(**{'total_loss': val_toal_loss.item() / (iteration + 1)})
pbar.update(1)
print('Finish Validation')
print('Epoch:'+ str(epoch+1) + '/' + str(Epoch))
print('Total Loss: %.4f || Val Loss: %.4f ' % (total_loss/(epoch_size+1),val_toal_loss/(epoch_size_val+1)))
print('Saving state, iter:', str(epoch+1))
torch.save(model.state_dict(), 'logs/Epoch%d-Total_Loss%.4f-Val_Loss%.4f.pth'%((epoch+1),total_loss/(epoch_size+1),val_toal_loss/(epoch_size_val+1)))
if __name__ == "__main__":
# 参数初始化
annotation_path = '2007_train.txt'
NUM_CLASSES = 20
IMAGE_SHAPE = [600,600,3]
BACKBONE = "resnet50"
model = FasterRCNN(NUM_CLASSES,backbone=BACKBONE)
#-------------------------------#
# Dataloder的使用
#-------------------------------#
Use_Data_Loader = True
Cuda = True
model_path = r'model_data/voc_weights_resnet.pth'
print('Loading weights into state dict...')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model_dict = model.state_dict()
pretrained_dict = torch.load(model_path, map_location=device)
pretrained_dict = {k: v for k, v in pretrained_dict.items() if np.shape(model_dict[k]) == np.shape(v)}
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
print('Finished!')
net = model.train()
if Cuda:
net = torch.nn.DataParallel(model)
cudnn.benchmark = True
net = net.cuda()
# 0.1用于验证,0.9用于训练
val_split = 0.1
with open(annotation_path) as f:
lines = f.readlines()
np.random.seed(10101)
np.random.shuffle(lines)
np.random.seed(None)
num_val = int(len(lines)*val_split)
num_train = len(lines) - num_val
if True:
lr = 1e-4
Init_Epoch = 0
Freeze_Epoch = 50
optimizer = optim.Adam(net.parameters(),lr,weight_decay=5e-4)
lr_scheduler = optim.lr_scheduler.StepLR(optimizer,step_size=1,gamma=0.95)
if Use_Data_Loader:
train_dataset = FRCNNDataset(lines[:num_train],(IMAGE_SHAPE[0],IMAGE_SHAPE[1]))
val_dataset = FRCNNDataset(lines[num_train:],(IMAGE_SHAPE[0],IMAGE_SHAPE[1]))
gen = DataLoader(train_dataset, shuffle=True, batch_size=1, num_workers=4, pin_memory=True,
drop_last=True, collate_fn=frcnn_dataset_collate)
gen_val = DataLoader(val_dataset, shuffle=True, batch_size=1, num_workers=4, pin_memory=True,
drop_last=True, collate_fn=frcnn_dataset_collate)
else:
gen = Generator(lines[:num_train],(IMAGE_SHAPE[0],IMAGE_SHAPE[1])).generate()
gen_val = Generator(lines[num_train:],(IMAGE_SHAPE[0],IMAGE_SHAPE[1])).generate()
epoch_size = num_train
epoch_size_val = num_val
# ------------------------------------#
# 冻结一定部分训练
# ------------------------------------#
for param in model.extractor.parameters():
param.requires_grad = False
# ------------------------------------#
# 由于batch==1所以冻结bn层
# ------------------------------------#
model.freeze_bn()
train_util = FasterRCNNTrainer(model,optimizer)
for epoch in range(Init_Epoch,Freeze_Epoch):
fit_ont_epoch(net,epoch,epoch_size,epoch_size_val,gen,gen_val,Freeze_Epoch,Cuda)
lr_scheduler.step()
if True:
lr = 1e-5
Freeze_Epoch = 50
Unfreeze_Epoch = 100
optimizer = optim.Adam(net.parameters(),lr,weight_decay=5e-4)
lr_scheduler = optim.lr_scheduler.StepLR(optimizer,step_size=1,gamma=0.95)
if Use_Data_Loader:
train_dataset = FRCNNDataset(lines[:num_train],(IMAGE_SHAPE[0],IMAGE_SHAPE[1]))
val_dataset = FRCNNDataset(lines[num_train:],(IMAGE_SHAPE[0],IMAGE_SHAPE[1]))
gen = DataLoader(train_dataset, shuffle=True, batch_size=1, num_workers=4, pin_memory=True,
drop_last=True, collate_fn=frcnn_dataset_collate)
gen_val = DataLoader(val_dataset, shuffle=True, batch_size=1, num_workers=4, pin_memory=True,
drop_last=True, collate_fn=frcnn_dataset_collate)
else:
gen = Generator(lines[:num_train],(IMAGE_SHAPE[0],IMAGE_SHAPE[1])).generate()
gen_val = Generator(lines[num_train:],(IMAGE_SHAPE[0],IMAGE_SHAPE[1])).generate()
epoch_size = num_train
epoch_size_val = num_val
#------------------------------------#
# 解冻后训练
#------------------------------------#
for param in model.extractor.parameters():
param.requires_grad = True
# ------------------------------------#
# 由于batch==1所以冻结bn层
# ------------------------------------#
model.freeze_bn()
train_util = FasterRCNNTrainer(model,optimizer)
for epoch in range(Freeze_Epoch,Unfreeze_Epoch):
fit_ont_epoch(net,epoch,epoch_size,epoch_size_val,gen,gen_val,Unfreeze_Epoch,Cuda)
lr_scheduler.step()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化