def get_training_object(self): model = get_torchvision_model(self.net_type, self.pretrained, self.classes, self.loss) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = model.to(device) if self.weight_path is not None: state_dict = torch.load(self.weight_path) # state_dict = state_dict["state"] model.load_state_dict(state_dict) #### optimizer optimizer = torch.optim.Adam(model.parameters(), lr=self.lr, betas=(0.9, 0.999)) scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau( optimizer, mode="max", factor=self.factor, patience=self.patience) # model = nn.DataParallel(model) # scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=10) # loss_criteria =torch.nn.BCELoss() if self.loss.startswith("focal"): loss_criteria = FocalLoss(gamma=self.gamma) else: loss_criteria = nn.BCEWithLogitsLoss() return model, loss_criteria, optimizer, scheduler
def forward_pass(_run, pretrained_model_id, test_batch_size, data_files, model_params, eer_criteria, class_labels): """ forward pass dev and eval data to trained model """ use_cuda = torch.cuda.is_available() # use cpu device = torch.device("cuda" if use_cuda else "cpu") tmp = torch.tensor([2]).to(device) # model is RNN if model_params['MODEL_SELECT'] == 4: use_rnn = True else: use_rnn = False # model is trained with focal loss objective if model_params['FOCAL_GAMMA']: print('training with focal loss') focal_obj = FocalLoss(gamma=model_params['FOCAL_GAMMA']) else: focal_obj = None kwargs = {'num_workers': 2, 'pin_memory': True} if use_cuda else {} # create model # cnx model = E2E(**model_params).to(device) num_params = sum(p.numel() for p in model.parameters() if p.requires_grad) print('===> Model total parameter: {}'.format(num_params)) if pretrained_model_id: pretrain_pth = 'snapshots/' + str(pretrained_model_id) + '/model_best.pth.tar' #pretrain_pth = '../pretrained/pa/senet34_py3' if os.path.isfile(pretrain_pth): print("===> loading checkpoint '{}'".format(pretrain_pth)) # python 2 checkpoint = torch.load(pretrain_pth, map_location=lambda storage, loc: storage) # load for cpu model.load_state_dict(checkpoint['state_dict'], strict=False) # python 3 # checkpoint = torch.load(pretrain_pth) # model.load_state_dict(checkpoint, strict=False) print("===> loaded checkpoint '{}'" .format(pretrain_pth)) else: print("===> no checkpoint found at '{}'".format(pretrain_pth)) exit() # Data loading code (class analysis for multi-class classification only) val_data = SpoofDatsetSystemID(data_files['dev_scp'], data_files['dev_utt2index'], binary_class=False) # val_data = SpoofDatsetEval(data_files['dev_scp']) # eval_data = SpoofDatsetEval(data_files['eval_scp']) val_loader = torch.utils.data.DataLoader(val_data, batch_size=test_batch_size, shuffle=False, **kwargs) # eval_loader = torch.utils.data.DataLoader( # eval_data, batch_size=test_batch_size, shuffle=False, **kwargs) # forward pass for dev print("===> forward pass for dev set") score_file_pth = os.path.join(data_files['scoring_dir'], str(pretrained_model_id) + '-dev_scores.txt') print("===> dev scoring file saved at: '{}'".format(score_file_pth)) prediction(val_loader, model, device, score_file_pth, data_files['dev_utt2systemID'], use_rnn, focal_obj)
def __init__(self, num_anchors=9, num_classes=20, compound_coef=0): super(EfficientDet, self).__init__() self.compound_coef = compound_coef self.num_channels = [64, 88, 112, 160, 224, 288, 384, 384][self.compound_coef] self.conv3 = nn.Conv2d(48, self.num_channels, kernel_size=1, stride=1, padding=0)#40 self.conv4 = nn.Conv2d(96, self.num_channels, kernel_size=1, stride=1, padding=0)#80 self.conv5 = nn.Conv2d(232, self.num_channels, kernel_size=1, stride=1, padding=0)#192 self.conv6 = nn.Conv2d(232, self.num_channels, kernel_size=3, stride=2, padding=1)#192 self.conv7 = nn.Sequential(nn.ReLU(), nn.Conv2d(self.num_channels, self.num_channels, kernel_size=3, stride=2, padding=1)) self.bifpn = nn.Sequential(*[BiFPN(self.num_channels) for _ in range(min(2 + self.compound_coef, 8))]) self.num_classes = num_classes self.regressor = Regressor(in_channels=self.num_channels, num_anchors=num_anchors, num_layers=3 + self.compound_coef // 3) self.classifier = Classifier(in_channels=self.num_channels, num_anchors=num_anchors, num_classes=num_classes, num_layers=3 + self.compound_coef // 3) self.anchors = Anchors() self.regressBoxes = BBoxTransform() self.clipBoxes = ClipBoxes() self.focalLoss = FocalLoss() for m in self.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() prior = 0.01 self.classifier.header.weight.data.fill_(0) self.classifier.header.bias.data.fill_(-math.log((1.0 - prior) / prior)) self.regressor.header.weight.data.fill_(0) self.regressor.header.bias.data.fill_(0) self.backbone_net = EfficientNet()
def __init__(self, config): super(EfficientDet, self).__init__() self.is_training = config.is_training self.nms_threshold = config.nms_threshold model_conf = EFFICIENTDET[config.network] self.num_channels = model_conf['W_bifpn'] input_channels = model_conf['EfficientNet_output'] self.convs = [] self.conv3 = nn.Conv2d(input_channels[0], self.num_channels, kernel_size=1, stride=1, padding=0) self.conv4 = nn.Conv2d(input_channels[1], self.num_channels, kernel_size=1, stride=1, padding=0) self.conv5 = nn.Conv2d(input_channels[2], self.num_channels, kernel_size=1, stride=1, padding=0) self.conv6 = nn.Conv2d(input_channels[3], self.num_channels, kernel_size=1, stride=1, padding=0) self.conv7 = nn.Conv2d(input_channels[4], self.num_channels, kernel_size=1, stride=1, padding=0) self.convs.append(self.conv3) self.convs.append(self.conv4) self.convs.append(self.conv5) self.convs.append(self.conv6) self.convs.append(self.conv7) self.bifpn = nn.Sequential( *[BiFPN(self.num_channels) for _ in range(model_conf['D_bifpn'])]) self.num_classes = config.num_classes self.anchors = Anchors() self.regressor = Regressor(in_channels=self.num_channels, num_anchors=self.anchors.num_anchors, num_layers=model_conf['D_class']) self.classifier = Classifier(in_channels=self.num_channels, num_anchors=self.anchors.num_anchors, num_classes=self.num_classes, num_layers=model_conf['D_class']) self.regressBoxes = BBoxTransform() self.clipBoxes = ClipBoxes() self.focalLoss = FocalLoss() for m in self.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() prior = 0.01 self.classifier.header.weight.data.fill_(0) self.classifier.header.bias.data.fill_(-math.log((1.0 - prior) / prior)) self.regressor.header.weight.data.fill_(0) self.regressor.header.bias.data.fill_(0) if config.resume: self.backbone_net = EfficientNet.from_name( model_conf['EfficientNet']) else: self.backbone_net = EfficientNet.from_pretrained( model_conf['EfficientNet'])
with torch.no_grad(): valid_loss, valid_loss_min = Validate(model, validloader, criterion, valid_loss_min, device, model_sv_pth) stats.append([train_loss, valid_loss]) stat = pd.DataFrame(stats, columns=['train_loss', 'valid_loss']) print('Finished Training') if plot: plotCurves(stat) if __name__ == "__main__": #Define transforms for the training data and validation data train_transforms = transforms.Compose([transforms.Resize(input_size, 0)]) valid_transforms = transforms.Compose([transforms.Resize(input_size, 0)]) #pass transform here-in train_data = CamVid_Dataset(img_pth = path + 'train/', mask_pth = path + 'train_labels/', transform = train_transforms) valid_data = CamVid_Dataset(img_pth = path + 'val/', mask_pth = path + 'val_labels/', transform = valid_transforms) #data loaders trainloader = torch.utils.data.DataLoader(train_data, batch_size=batch, shuffle=True) validloader = torch.utils.data.DataLoader(valid_data, batch_size=batch, shuffle=True) model = UNet(3, 32, True).to(device) criterion = FocalLoss() optimizer = optim.SGD(model.parameters(), lr=lr, momentum=0.99) train(model, trainloader, validloader, criterion, optimizer, epochs, device, load_model_pth, model_sv_pth, plot=True, visualize=True, load_model=False)
def work(_run, pretrained, batch_size, test_batch_size, epochs, start_epoch, log_interval, n_warmup_steps, data_files, model_params, eer_criteria, leave_one_out): global best_acc1 global best_eer use_cuda = torch.cuda.is_available() device = torch.device("cuda" if use_cuda else "cpu") tmp = torch.tensor([2]).to(device) # model is trained for binary classification (for datalaoder) if model_params['NUM_SPOOF_CLASS'] == 2: binary_class = True else: binary_class = False # model is RNN if model_params['MODEL_SELECT'] == 4: use_rnn = True else: use_rnn = False if model_params['FOCAL_GAMMA']: print('training with focal loss') focal_obj = FocalLoss(gamma=model_params['FOCAL_GAMMA']) else: focal_obj = None kwargs = {'num_workers': 2, 'pin_memory': True} if use_cuda else {} # create model # cnx model = E2E(**model_params).to(device) model_params = sum(p.numel() for p in model.parameters() if p.requires_grad) print('===> Model total parameter: {}'.format(model_params)) # Wrap model for multi-GPUs, if necessary if torch.cuda.is_available() and torch.cuda.device_count() > 1: print('multi-gpu') model = nn.DataParallel(model).cuda() # define loss function (criterion) and optimizer # cnx optimizer = ScheduledOptim( torch.optim.Adam( filter(lambda p: p.requires_grad, model.parameters()), betas=(0.9, 0.98), eps=1e-09, weight_decay=1e-4, amsgrad=True), n_warmup_steps) # optionally resume from a checkpoint if pretrained: if os.path.isfile(pretrained): print("===> loading checkpoint '{}'".format(pretrained)) checkpoint = torch.load(pretrained) start_epoch = checkpoint['epoch'] if eer_criteria and 'best_eer' in checkpoint: best_eer = checkpoint['best_eer'] else: best_acc1 = checkpoint['best_acc1'] print("===> Best accuracy: %f" % best_acc1) model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) print("===> loaded checkpoint '{}' (epoch {})" .format(pretrained, checkpoint['epoch'])) else: print("===> no checkpoint found at '{}'".format(pretrained)) cudnn.benchmark = True # It enables benchmark mode in cudnn. # Data loading code train_data = SpoofDatsetSystemID(data_files['train_scp'], data_files['train_utt2index'], rnd_nslides=True, binary_class=binary_class, leave_one_out=leave_one_out) val_data = SpoofDatsetSystemID(data_files['dev_scp'], data_files['dev_utt2index'], binary_class=binary_class, leave_one_out=leave_one_out) train_loader = torch.utils.data.DataLoader( train_data, batch_size=batch_size, shuffle=True, **kwargs) val_loader = torch.utils.data.DataLoader( val_data, batch_size=test_batch_size, shuffle=False, **kwargs) if leave_one_out: eval_data = SpoofDatsetSystemID(data_files['dev_scp'], data_files['dev_utt2index'], binary_class, False) eval_loader = torch.utils.data.DataLoader( eval_data, batch_size=test_batch_size, shuffle=False, **kwargs) else: eval_loader = val_loader best_epoch = 0 early_stopping, max_patience = 0, 5 # for early stopping for epoch in range(start_epoch, start_epoch + epochs): # train for one epoch train(train_loader, model, optimizer, epoch, device, log_interval, use_rnn, focal_obj) # evaluate on validation set acc1 = validate(val_loader, data_files['dev_utt2systemID'], model, device, log_interval, use_rnn, eer_criteria, focal_obj) # remember best acc@1/eer and save checkpoint if eer_criteria: is_best = acc1 < best_eer best_eer = min(acc1, best_eer) else: is_best = acc1 > best_acc1 best_acc1 = max(acc1, best_acc1) # adjust learning rate + early stopping if is_best: early_stopping = 0 best_epoch = epoch + 1 else: early_stopping += 1 if epoch - best_epoch > 2: optimizer.increase_delta() best_epoch = epoch + 1 if early_stopping == max_patience: break # save model if not is_best: continue if eer_criteria: save_checkpoint({ 'epoch': epoch, 'state_dict': model.state_dict(), 'best_eer': best_eer, 'optimizer': optimizer.state_dict(), }, is_best, "snapshots/" + str(_run._id), str(epoch) + ('_%.3f' % acc1) + ".pth.tar") else: save_checkpoint({ 'epoch': epoch, 'state_dict': model.state_dict(), 'best_acc1': best_acc1, 'optimizer': optimizer.state_dict(), }, is_best, "snapshots/" + str(_run._id), str(epoch) + ('_%.3f' % acc1) + ".pth.tar") # load best model best_model_pth = os.path.join('snapshots', str(_run._id), 'model_best.pth.tar') score_file_pth = os.path.join(data_files['scoring_dir'], str(_run._id) + '-scores.txt') print("===> loading best model for scoring: '{}'".format(best_model_pth)) checkpoint = torch.load(best_model_pth) model.load_state_dict(checkpoint['state_dict']) # compute EER print("===> scoring file saved at: '{}'".format(score_file_pth)) prediction(eval_loader, model, device, score_file_pth, data_files['dev_utt2systemID'], use_rnn, focal_obj)