def prepare_dataloader(df, fold, train_batch, valid_batch,img_sz,num_workers, data_root): trainset = df[df.fold != fold].reset_index(drop=True) validset = df[df.fold == fold].reset_index(drop=True) train_dataset = ICDARDataset( trainset, image_root=data_root, transforms=get_train_transforms(img_sz)) valid_dataset = ICDARDataset( validset, image_root=data_root, transforms=get_valid_transforms(img_sz)) train_loader = DataLoader( train_dataset, batch_size=train_batch, pin_memory=False, drop_last=False, shuffle=True, num_workers=num_workers) val_loader = DataLoader( valid_dataset, batch_size=train_batch, num_workers=num_workers, shuffle=False, pin_memory=False) return train_loader, val_loader
test_img_num = 25 # Load model checkpoint that is to be evaluated # checkpoint = torch.load(checkpoint) if torch.cuda.is_available() == True: checkpoint = torch.load(checkpoint, map_location=torch.device('cuda')) else: checkpoint = torch.load(checkpoint, map_location=torch.device('cpu')) model = checkpoint['model'] model = model.to(device) # Switch to eval mode model.eval() # Load test data test_dataset = ICDARDataset(data_folder, split='test') test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False, collate_fn=test_dataset.collate_fn, num_workers=workers, pin_memory=True) def evaluate(test_loader, model, test_img_num): """ Evaluate. :param test_loader: DataLoader for test data :param model: model """
def main(): """ Training and validation. """ global epochs_since_improvement, start_epoch, label_map, best_loss, epoch, checkpoint # Initialize model or load checkpoint if checkpoint is None: model = SSD300(n_classes=n_classes) # Initialize the optimizer, with twice the default learning rate for biases, as in the original Caffe repo biases = list() not_biases = list() for param_name, param in model.named_parameters(): if param.requires_grad: if param_name.endswith('.bias'): biases.append(param) else: not_biases.append(param) # optimizer = torch.optim.Adam(model.parameters(), lr=lr, betas=(0.9, 0.99)) optimizer = torch.optim.SGD(params=[{ 'params': biases, 'lr': 2 * lr }, { 'params': not_biases }], lr=lr, momentum=momentum, weight_decay=weight_decay) # Move to default device model = model.to(device) criterion = MultiBoxLoss(priors_cxcy=model.priors_cxcy).to(device) # Custom dataloaders train_dataset = ICDARDataset(data_folder, split='train') val_dataset = ICDARDataset(data_folder, split='test') train_loader = torch.utils.data.DataLoader( train_dataset, batch_size=batch_size, shuffle=True, collate_fn=train_dataset.collate_fn, num_workers=workers, pin_memory=True) # note that we're passing the collate function here val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=batch_size, shuffle=True, collate_fn=val_dataset.collate_fn, num_workers=workers, pin_memory=True) # Epochs for epoch in range(start_epoch, epochs): # One epoch's training train_loss = train(train_loader=train_loader, model=model, criterion=criterion, optimizer=optimizer, epoch=epoch) # One epoch's validation val_loss = validate(val_loader=val_loader, model=model, criterion=criterion) # Did validation loss improve? is_best = val_loss < best_loss best_loss = min(val_loss, best_loss) if not is_best: epochs_since_improvement += 1 print("\nEpochs since last improvement: %d\n" % (epochs_since_improvement, )) else: epochs_since_improvement = 0 # Save checkpoint save_checkpoint(epoch, epochs_since_improvement, model, optimizer, val_loss, best_loss, is_best) with open('log.txt', 'a+') as f: f.write('epoch:' + str(epoch) + ' train loss:' + str(train_loss) + ' val loss:' + str(val_loss) + '\n')
def main(): """ Training and validation. """ global epochs_since_improvement, start_epoch, label_map, best_F1, epoch, checkpoint # Initialize model or load checkpoint if checkpoint is None: model = LSTMClassifier() # Initialize the optimizer, with twice the default learning rate for biases, as in the original Caffe repo biases = list() not_biases = list() for param_name, param in model.named_parameters(): if param.requires_grad: if param_name.endswith('.bias'): biases.append(param) else: not_biases.append(param) optimizer = torch.optim.Adam(model.parameters(), lr=lr, betas=(0.9, 0.99)) # optimizer = torch.optim.SGD(params=[{'params': biases, 'lr': 2 * lr}, {'params': not_biases}], #lr=lr, momentum=momentum, weight_decay=weight_decay) else: checkpoint = torch.load(checkpoint) start_epoch = checkpoint['epoch'] epochs_since_improvement = checkpoint['epochs_since_improvement'] best_F1 = checkpoint['best_F1'] print('\nLoaded checkpoint from epoch %d. Best F1 so far is %.3f.\n' % (start_epoch, best_F1)) model = checkpoint['model'] optimizer = checkpoint['optimizer'] # Move to default device model = model.to(device) print(model) # criterion = torch.nn.CrossEntropyLoss() criterion = FocalLoss() # Custom dataloaders train_dataset = ICDARDataset(data_folder, split='train') val_dataset = ICDARDataset(data_folder, split='test') train_loader = torch.utils.data.DataLoader( train_dataset, batch_size=batch_size, shuffle=True, collate_fn=train_dataset.collate_fn, num_workers=workers, pin_memory=True) val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=batch_size, shuffle=True, collate_fn=val_dataset.collate_fn, num_workers=workers, pin_memory=True) # Epochs for epoch in range(start_epoch, epochs): # One epoch's training train_loss = train(train_loader=train_loader, model=model, criterion=criterion, optimizer=optimizer, epoch=epoch) # One epoch's validation val_loss, accuracy, F1 = validate(val_loader=val_loader, model=model, criterion=criterion) # Did validation loss improve? # is_best = train_loss < best_loss # best_loss = min(train_loss, best_loss) # Did validation loss improve? is_best = F1 > best_F1 best_F1 = max(F1, best_F1) if not is_best: epochs_since_improvement += 1 print("\nEpochs since last improvement: %d\n" % (epochs_since_improvement, )) else: epochs_since_improvement = 0 # Save checkpoint save_checkpoint(epoch, epochs_since_improvement, model, optimizer, val_loss, best_F1, is_best) with open('log.txt', 'a+') as f: f.write('epoch:' + str(epoch) + ' train loss:' + str(train_loss) + ' val loss:' + str(val_loss) + 'accuracy:' + str(accuracy) + '\n')
def run_infer(opt): device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') n_classes = 6 weight_path = opt.weight_path tst_preds = [] TEST_DIR = opt.test_dir test = pd.DataFrame() # test['image_id'] = sorted(list(os.listdir(TEST_DIR))) content = pd.read_csv('/content/test_aug.csv') test_image = [] for i in content["image_id"]: test_image.append(i) test['image_id'] = sorted(test_image) if "vit" in opt.model_arch: testset = ICDARDataset(test, TEST_DIR, transforms=get_inference_transforms(384)) else: testset = ICDARDataset(test, TEST_DIR, transforms=get_inference_transforms( opt.img_size)) tst_loader = DataLoader(testset, batch_size=opt.valid_bs, num_workers=opt.num_workers, shuffle=False, pin_memory=False) print("[INFO] Found {} folds in weight path".format( len(os.listdir(weight_path)))) print(os.listdir(weight_path)) print("[INFO] Start inference ...") # for fold in os.listdir(weight_path): # print(fold) # model = Classifier(opt.model_arch, n_classes).to(device) # model_path = os.path.join(weight_path, fold, "best.pt") # model.load_state_dict(torch.load(model_path)['model']) # with torch.no_grad(): # for _ in range(opt.tta): # tst_preds += [inference_one_epoch(model, tst_loader, device)] # del model model = Classifier(opt.model_arch, n_classes).to(device) model_path = '/content/drive/MyDrive/Emotion_Speech_Recognition/Models/Classify_image/resnet/best.pt' model.load_state_dict(torch.load(model_path)['model']) with torch.no_grad(): for _ in range(opt.tta): tst_preds += [inference_one_epoch(model, tst_loader, device)] del model avg_tst_preds = np.mean(tst_preds, axis=0) if not (os.path.isdir(opt.work_dir)): os.mkdir(opt.work_dir) # np.save(os.path.join(opt.work_dir, "{}.npy".format(opt.model_arch)), avg_tst_preds) test['predict'] = np.argmax(avg_tst_preds, axis=1) gt = [] for i in content["image_id"]: gt.append(i.split('/')[0]) test['gt'] = gt test.to_csv('submission.csv', index=False) torch.cuda.empty_cache()
def main(): """ Training and validation. """ global epochs_since_improvement, start_epoch, label_map, best_loss, epoch, checkpoint # Initialize model or load checkpoint if checkpoint is None: model = SSD300(n_classes=n_classes) # Initialize the optimizer, with twice the default learning rate for biases, as in the original Caffe repo biases = list() not_biases = list() for param_name, param in model.named_parameters(): if param.requires_grad: if param_name.endswith('.bias'): biases.append(param) else: not_biases.append(param) # optimizer = torch.optim.Adam(model.parameters(), lr=lr, betas=(0.9, 0.99)) optimizer = torch.optim.SGD(params=[{ 'params': biases, 'lr': 2 * lr }, { 'params': not_biases }], lr=lr, momentum=momentum, weight_decay=weight_decay) else: checkpoint = torch.load(checkpoint) start_epoch = checkpoint['epoch'] + 1 epochs_since_improvement = checkpoint['epochs_since_improvement'] best_loss = checkpoint['best_loss'] + 1 print( '\nLoaded checkpoint from epoch %d. Best loss so far is %.3f.\n' % (start_epoch, best_loss)) model = checkpoint['model'] optimizer = checkpoint['optimizer'] # Move to default device model = model.to(device) criterion = MultiBoxLoss(priors_cxcy=model.priors_cxcy).to(device) # Custom dataloaders train_dataset = ICDARDataset(data_folder, split='train') val_dataset = ICDARDataset(data_folder, split='test') train_loader = torch.utils.data.DataLoader( train_dataset, batch_size=batch_size, shuffle=True, collate_fn=train_dataset.collate_fn, num_workers=workers, pin_memory=True) # note that we're passing the collate function here val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=batch_size, shuffle=True, collate_fn=val_dataset.collate_fn, num_workers=workers, pin_memory=True) # Epochs for epoch in range(start_epoch, epochs): # Paper describes decaying the learning rate at the 80000th, 100000th, 120000th 'iteration', i.e. model update or batch # The paper uses a batch size of 32, which means there were about 517 iterations in an epoch # Therefore, to find the epochs to decay at, you could do, # if epoch in {80000 // 517, 100000 // 517, 120000 // 517}: # adjust_learning_rate(optimizer, 0.1) # In practice, I just decayed the learning rate when loss stopped improving for long periods, # and I would resume from the last best checkpoint with the new learning rate, # since there's no point in resuming at the most recent and significantly worse checkpoint. # So, when you're ready to decay the learning rate, just set checkpoint = 'BEST_checkpoint_ssd300.pth.tar' above # and have adjust_learning_rate(optimizer, 0.1) BEFORE this 'for' loop # One epoch's training train_loss = train(train_loader=train_loader, model=model, criterion=criterion, optimizer=optimizer, epoch=epoch) # One epoch's validation val_loss = validate(val_loader=val_loader, model=model, criterion=criterion) # Did validation loss improve? # is_best = train_loss < best_loss # best_loss = min(train_loss, best_loss) # Did validation loss improve? is_best = val_loss < best_loss best_loss = min(val_loss, best_loss) if not is_best: epochs_since_improvement += 1 print("\nEpochs since last improvement: %d\n" % (epochs_since_improvement, )) else: epochs_since_improvement = 0 # Save checkpoint save_checkpoint(epoch, epochs_since_improvement, model, optimizer, val_loss, best_loss, is_best) with open('log.txt', 'a+') as f: f.write('epoch:' + str(epoch) + ' train loss:' + str(train_loss) + ' val loss:' + str(val_loss) + '\n')