def CrossEntropyLoss(self, logit, target): n, c, h, w = logit.size() criterion = nn.CrossEntropyLoss(weight=self.weight, ignore_index=self.ignore_index, size_average=self.size_average) if self.cuda and len(self.args.gpu_ids) > 1: criterion = DataParallelCriterion(criterion) if self.cuda and len(self.args.gpu_ids) < 2: criterion = criterion.cuda() loss = criterion(logit, target.long()) if self.batch_average: loss /= n return loss
if config.model_type=='LSTM': model = LSTMLM(input_size=len(vocab), embedding_size=config.embedding_size, hidden_size=config.hidden_size, output_size=len(vocab), n_layers=config.n_layers, dropout_p=config.dropout_p) elif config.model_type=='BiLSTM': model = BiLSTMLM(input_size=len(vocab), embedding_size=config.embedding_size, hidden_size=config.hidden_size, output_size=len(vocab), n_layers=config.n_layers, dropout_p=config.dropout_p) loss_fn = nn.NLLLoss(ignore_index=vocab.stoi[vocab.pad_token]) optimizer = optim.Adam(model.parameters()) if config.cuda: if config.multi_gpu: from parallel import DataParallelModel, DataParallelCriterion model = DataParallelModel(model).cuda() loss_fn = DataParallelCriterion(loss_fn).cuda() else: model = model.cuda() loss_fn = loss_fn.cuda() print('=========MODEL=========\n',model) # Train for epoch in range(1, config.epochs+1): train()
def main_tr(args, crossVal): dataLoad = ld.LoadData(args.data_dir, args.classes) data = dataLoad.processData(crossVal, args.data_name) # load the model model = net.MiniSeg(args.classes, aux=True) if not osp.isdir(osp.join(args.savedir + '_mod' + str(args.max_epochs))): os.mkdir(args.savedir + '_mod' + str(args.max_epochs)) if not osp.isdir( osp.join(args.savedir + '_mod' + str(args.max_epochs), args.data_name)): os.mkdir( osp.join(args.savedir + '_mod' + str(args.max_epochs), args.data_name)) saveDir = args.savedir + '_mod' + str( args.max_epochs) + '/' + args.data_name + '/' + args.model_name # create the directory if not exist if not osp.exists(saveDir): os.mkdir(saveDir) if args.gpu and torch.cuda.device_count() > 1: #model = torch.nn.DataParallel(model) model = DataParallelModel(model) if args.gpu: model = model.cuda() total_paramters = sum([np.prod(p.size()) for p in model.parameters()]) print('Total network parameters: ' + str(total_paramters)) # define optimization criteria weight = torch.from_numpy( data['classWeights']) # convert the numpy array to torch if args.gpu: weight = weight.cuda() criteria = CrossEntropyLoss2d(weight, args.ignore_label) #weight if args.gpu and torch.cuda.device_count() > 1: criteria = DataParallelCriterion(criteria) if args.gpu: criteria = criteria.cuda() # compose the data with transforms trainDataset_main = myTransforms.Compose([ myTransforms.Normalize(mean=data['mean'], std=data['std']), myTransforms.Scale(args.width, args.height), myTransforms.RandomCropResize(int(32. / 1024. * args.width)), myTransforms.RandomFlip(), myTransforms.ToTensor() ]) trainDataset_scale1 = myTransforms.Compose([ myTransforms.Normalize(mean=data['mean'], std=data['std']), myTransforms.Scale(int(args.width * 1.5), int(args.height * 1.5)), myTransforms.RandomCropResize(int(100. / 1024. * args.width)), myTransforms.RandomFlip(), myTransforms.ToTensor() ]) trainDataset_scale2 = myTransforms.Compose([ myTransforms.Normalize(mean=data['mean'], std=data['std']), myTransforms.Scale(int(args.width * 1.25), int(args.height * 1.25)), myTransforms.RandomCropResize(int(100. / 1024. * args.width)), myTransforms.RandomFlip(), myTransforms.ToTensor() ]) trainDataset_scale3 = myTransforms.Compose([ myTransforms.Normalize(mean=data['mean'], std=data['std']), myTransforms.Scale(int(args.width * 0.75), int(args.height * 0.75)), myTransforms.RandomCropResize(int(32. / 1024. * args.width)), myTransforms.RandomFlip(), myTransforms.ToTensor() ]) valDataset = myTransforms.Compose([ myTransforms.Normalize(mean=data['mean'], std=data['std']), myTransforms.Scale(args.width, args.height), myTransforms.ToTensor() ]) # since we training from scratch, we create data loaders at different scales # so that we can generate more augmented data and prevent the network from overfitting trainLoader = torch.utils.data.DataLoader(myDataLoader.Dataset( data['trainIm'], data['trainAnnot'], transform=trainDataset_main), batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers, pin_memory=True, drop_last=True) trainLoader_scale1 = torch.utils.data.DataLoader( myDataLoader.Dataset(data['trainIm'], data['trainAnnot'], transform=trainDataset_scale1), batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers, pin_memory=True, drop_last=True) trainLoader_scale2 = torch.utils.data.DataLoader( myDataLoader.Dataset(data['trainIm'], data['trainAnnot'], transform=trainDataset_scale2), batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers, pin_memory=True, drop_last=True) trainLoader_scale3 = torch.utils.data.DataLoader( myDataLoader.Dataset(data['trainIm'], data['trainAnnot'], transform=trainDataset_scale3), batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers, pin_memory=True, drop_last=True) valLoader = torch.utils.data.DataLoader(myDataLoader.Dataset( data['valIm'], data['valAnnot'], transform=valDataset), batch_size=args.batch_size, shuffle=False, num_workers=args.num_workers, pin_memory=True) max_batches = len(trainLoader) + len(trainLoader_scale1) + len( trainLoader_scale2) + len(trainLoader_scale3) if args.gpu: cudnn.benchmark = True start_epoch = 0 if args.pretrained is not None: state_dict = torch.load(args.pretrained) new_keys = [] new_values = [] for idx, key in enumerate(state_dict.keys()): if 'pred' not in key: new_keys.append(key) new_values.append(list(state_dict.values())[idx]) new_dict = OrderedDict(list(zip(new_keys, new_values))) model.load_state_dict(new_dict, strict=False) print('pretrained model loaded') if args.resume is not None: if osp.isfile(args.resume): print("=> loading checkpoint '{}'".format(args.resume)) checkpoint = torch.load(args.resume) start_epoch = checkpoint['epoch'] args.lr = checkpoint['lr'] model.load_state_dict(checkpoint['state_dict']) print("=> loaded checkpoint '{}' (epoch {})".format( args.resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(args.resume)) log_file = osp.join(saveDir, 'trainValLog_' + args.model_name + '.txt') if osp.isfile(log_file): logger = open(log_file, 'a') else: logger = open(log_file, 'w') logger.write("Parameters: %s" % (str(total_paramters))) logger.write("\n%s\t%s\t\t%s\t%s\t%s\t%s\tlr" % ('CrossVal', 'Epoch', 'Loss(Tr)', 'Loss(val)', 'mIOU (tr)', 'mIOU (val)')) logger.flush() optimizer = torch.optim.Adam(model.parameters(), args.lr, (0.9, 0.999), eps=1e-08, weight_decay=1e-4) maxmIOU = 0 maxEpoch = 0 print(args.model_name + '-CrossVal: ' + str(crossVal + 1)) for epoch in range(start_epoch, args.max_epochs): # train for one epoch cur_iter = 0 train(args, trainLoader_scale1, model, criteria, optimizer, epoch, max_batches, cur_iter) cur_iter += len(trainLoader_scale1) train(args, trainLoader_scale2, model, criteria, optimizer, epoch, max_batches, cur_iter) cur_iter += len(trainLoader_scale2) train(args, trainLoader_scale3, model, criteria, optimizer, epoch, max_batches, cur_iter) cur_iter += len(trainLoader_scale3) lossTr, overall_acc_tr, per_class_acc_tr, per_class_iu_tr, mIOU_tr, lr = \ train(args, trainLoader, model, criteria, optimizer, epoch, max_batches, cur_iter) # evaluate on validation set lossVal, overall_acc_val, per_class_acc_val, per_class_iu_val, mIOU_val = \ val(args, valLoader, model, criteria) torch.save( { 'epoch': epoch + 1, 'arch': str(model), 'state_dict': model.state_dict(), 'optimizer': optimizer.state_dict(), 'lossTr': lossTr, 'lossVal': lossVal, 'iouTr': mIOU_tr, 'iouVal': mIOU_val, 'lr': lr }, osp.join( saveDir, 'checkpoint_' + args.model_name + '_crossVal' + str(crossVal + 1) + '.pth.tar')) # save the model also model_file_name = osp.join( saveDir, 'model_' + args.model_name + '_crossVal' + str(crossVal + 1) + '_' + str(epoch + 1) + '.pth') torch.save(model.state_dict(), model_file_name) logger.write( "\n%d\t\t%d\t\t%.4f\t\t%.4f\t\t%.4f\t\t%.4f\t\t%.7f" % (crossVal + 1, epoch + 1, lossTr, lossVal, mIOU_tr, mIOU_val, lr)) logger.flush() print("\nEpoch No. %d:\tTrain Loss = %.4f\tVal Loss = %.4f\t mIOU(tr) = %.4f\t mIOU(val) = %.4f\n" \ % (epoch + 1, lossTr, lossVal, mIOU_tr, mIOU_val)) if mIOU_val >= maxmIOU: maxmIOU = mIOU_val maxEpoch = epoch + 1 torch.cuda.empty_cache() logger.flush() logger.close() return maxEpoch, maxmIOU