def run(params): model = None if 'load_model_from_dir' in params.__dict__ and params.load_model_from_dir: print('Loading the model from an existing dir!') model_params = pickle.load( open(os.path.join(params.dir_name, 'config.pkl'), 'rb')) if 'lookup_table' in params.__dict__: model_params.lookup_table = params.lookup_table if 'sentiment_dic' in params.__dict__: model_params.sentiment_dic = params.sentiment_dic model = models.setup(model_params) model.load_state_dict( torch.load(os.path.join(params.dir_name, 'model'))) model = model.to(params.device) else: model = models.setup(params).to(params.device) if not ('fine_tune' in params.__dict__ and params.fine_tune == False): print('Training the model!') train(params, model) model = torch.load(params.best_model_file) os.remove(params.best_model_file) performance_dict = test(model, params) performance_str = print_performance(performance_dict, params) save_model(model, params, performance_str) return performance_dict
def train(): # Turn on training mode which enables dropout. model.train() total_loss = 0. start_time = time.time() ntokens = len(corpus.dictionary) if args.model != 'Transformer': hidden = model.init_hidden(args.batch_size) for batch, i in enumerate(range(0, train_data.size(0) - 1, args.bptt)): data, targets = get_batch(train_data, i) # Starting each batch, we detach the hidden state from how it was previously produced. # If we didn't, the model would try backpropagating all the way to start of the dataset. model.zero_grad() if args.model == 'Transformer': output = model(data) else: hidden = repackage_hidden(hidden) output, hidden = model(data, hidden) loss = criterion(output.view(-1, ntokens), targets) loss.backward() # `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs. torch.nn.utils.clip_grad_norm_(model.parameters(), args.clip) for p in model.parameters(): p.data.add_(-lr, p.grad.data) total_loss += loss.item() if batch % args.log_interval == 0 and batch > 0: cur_loss = total_loss / args.log_interval elapsed = time.time() - start_time print( '| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.2f} | ms/batch {:5.2f} | ' 'loss {:5.2f} | ppl {:8.2f}'.format( epoch, batch, len(train_data) // args.bptt, lr, elapsed * 1000 / args.log_interval, cur_loss, math.exp(cur_loss))) total_loss = 0 start_time = time.time()
import sys from utils.model import train DATASET_NAME = sys.argv[1] if __name__ == '__main__': train(DATASET_NAME)
def main(image_folder, model_folder, building_folder, figure_folder, info_csv, stats_json_file, train_results, batch_size, ideal_batch_size, epochs, device_name): # Configure folders image_folder = Path(image_folder) model_folder = Path(model_folder) building_folder = Path(building_folder) stats_json_file = Path(stats_json_file) figure_folder = Path(figure_folder) # Seed program torch.manual_seed(0) # retrieve information on buildings df = pd.read_csv(info_csv) # Get information on a TIF n_ch, h, w = get_tif_dims(list(image_folder.iterdir())[0]) # Get means and stds if stats_json_file.exists(): mean_channels, std_channels = load_stats(stats_json_file) else: stats = compute_mean_std(image_folder, n_ch) mean_channels = stats['mean'] std_channels = stats["std"] with open(stats_json_file, 'w') as file: json.dump(stats, file) # New dims for image pad_h, pad_w = 8 - h % 8, 8 - w % 8 # Prepare padding if pad_h % 2 == 0: top_p = bottom_p = pad_h // 2 else: top_p, bottom_p = pad_h // 2, pad_h - (pad_h // 2) if pad_w % 2 == 0: left_p = right_p = pad_w // 2 else: left_p, right_p = pad_w // 2, pad_w - (pad_w // 2) img_pad = (left_p, top_p, right_p, bottom_p) # Compose transforms transforms = SemSegCompose(img_pad, mean_channels, std_channels, 360) # Prepare loading function # load_tif_with_mask = partial( # load_tif, # df=df, # mean_vec=mean_channels, # std_vec=std_channels, # building_folder=building_folder, # padding=(pad_h, pad_w)) # Make dataset ds = MulPanSharpenDataset(image_folder, building_folder, df, transforms=transforms) # ds = datasets.DatasetFolder(root=image_folder, # loader=load_tif_with_mask, # extensions=('.tif',)) logger.info(f"N° of images: {len(ds)}") logger.info(f"Type of img: {ds.label}") train_ds, val_ds = split_dataset(ds, train_size=0.8) logger.info(f"Train set size: {len(train_ds)}") logger.info(f"Val set size: {len(val_ds)}") train_dl = DataLoader(train_ds, batch_size=batch_size, shuffle=True) val_dl = DataLoader(val_ds, batch_size=batch_size, shuffle=True) logger.info(f"N° of iterations per batch (train): {len(train_dl)}") logger.info(f"N° of iterations per batch (val): {len(val_dl)}") # get model logger.info("Getting U-Net model") model = torch.hub.load('mateuszbuda/brain-segmentation-pytorch', 'unet', in_channels=n_ch, out_channels=2, init_features=32, pretrained=False) device = torch.device(device_name) logger.info(f"Mounting model on {device}") model = model.to(device) # Define criterion logger.info("Defining cross-entropy loss with 11/89 ratio") criterion = nn.CrossEntropyLoss(weight=torch.tensor([.11, .89])) criterion = criterion.to(device) # Define optimizer logger.info("Defining Adam optimizer") optimizer = optim.Adam(model.parameters(), lr=1e-3) results = train(model, train_dl, val_dl, criterion, optimizer, epochs, device, 2, model_folder, ideal_batch_size // batch_size) # Saves model metrics as pickle file with open(train_results, "wb") as f: pickle.dump(results, f) # Saves model torch.save(model.state_dict(), model_folder / 'unet_model') logger.info(f"Saved model at {model_folder / 'unet_model'}") logging.info(f"Metrics evaluation. Check {figure_folder} for results.") # Plot metrics plot_loss(results, figure_folder) logger.info("Loss curve created.") plot_last_cm(results, figure_folder) logger.info("Last confusion matrix created") plot_correct_preds(results, figure_folder) logger.info("Evolution of correct predictions created;") plot_accuracy(results, figure_folder) logger.info("Accuracy plot created.") plot_iou(results, figure_folder) logger.info("IoU evolution plot created.") for i in range(5): generate_masks(ds, model, figure_folder, i, 5) logger.info("Created model results.")
# importing libraries from sklearn.metrics import accuracy_score from utils import read_dataset from utils import model # reading data X_train, X_test, y_train, y_test = read_dataset.read_irisdata("../dataset/Iris.csv") # number of K k_value = 2 epoch = 5 ''' method : train() arguments : number of clusters, features, labels, number of epochs returns : k number of centroids ''' centroids = model.train(k_value, X_train, y_train, epoch) ''' method : predict() arguments : number of clusters, k centroids, testing set returns : predicted class label ''' class_label = model.predict(k_value, centroids, X_test) # accuracy score accuracy = accuracy_score(y_test, class_label) print('Test Accuracy: {}\n\n'.format(accuracy))
def run_nn(in_data_dir, index_file, out_path, run_dir, save_path_name='save', resume=None, batch_size=256, epochs=50, start_epoch=0, lr=1e-4, wd=1e-3, workers=12, no_cuda=True, seed=1): # parser = argparse.ArgumentParser() # parser.add_argument('path', default='/home/dell/eeg-data-sample/', type=str, help='path to the main dir of the experiment') # parser.add_argument('--save', default='', type=str, metavar='PATH', help='save path under the main exp path') # parser.add_argument('--resume', default='', type=str, metavar='PATH', help='path to a checkpoint (default: none)') # parser.add_argument('--batch-size', type=int, default=256, help='input batch size for training (default: 512)') # parser.add_argument('--epochs', type=int, default=50, help='number of epochs to train (default: 100)') # parser.add_argument('--start-epoch', default=0, type=int, help='manual epoch number (useful on restarts)') # parser.add_argument('--lr', type=float, default=1e-4, help='learning rate (default: 1e-4)') # parser.add_argument('--wd', default=1e-3, type=float, help='weight decay (default: 1e-3)') # parser.add_argument('--workers', type=int, default=12, help='number of data loading workers (default: 12)') # parser.add_argument('--no-cuda', action='store_true', default=False, help='disables CUDA training') # parser.add_argument('--seed', type=int, default=1, help='random seed (default: 1)') # args = parser.parse_args() # if args.save == '': # args.save = os.path.join(os.path.dirname(args.path), # '1DenseNet_sample_butter') # else: # args.save = os.path.join(os.path.dirname(args.path), args.save) save_path = os.path.join('./Data/', run_dir, save_path_name) torch.manual_seed(seed) use_cuda = not no_cuda and torch.cuda.is_available() cudnn.benchmark = True if use_cuda else False print("===> Loading Datasets...") print("\t===> Loading data index info") with open(os.path.join('./Data/', index_file), "rb") as f: data_splits = pickle.load(f) print(len(data_splits['train'])) label_h5_path = os.path.join('./Data/', "labels.h5") print(data_splits['train'][0]) print("\t===> Construct train set") train_set = MontagePickleDataset(in_data_dir, list_index=data_splits['train'], label_h5_path=label_h5_path, transform=None) train_loader = DataLoader(dataset=train_set, batch_size=batch_size, shuffle=True, num_workers=workers) print("\t===> Construct validation set") val_set = MontagePickleDataset(in_data_dir, list_index=data_splits['validation'], label_h5_path=label_h5_path, transform=None) val_loader = DataLoader(dataset=val_set, batch_size=batch_size, shuffle=False, num_workers=workers) print("===> Dataset loaded!") # class_weights, pseudo_counts = compute_class_weight(data_splits['train'], alpha=0.2) # pickle.dump(class_weights, open("/home/dell/eeg-data-sample/class_weights_random_sample_without_zero.pkl", "wb"), # pickle.HIGHEST_PROTOCOL) with open(os.path.join('./Data/', "class_weights.pkl"), "rb") as f: class_weights = pickle.load(f) class_weights = torch.from_numpy(class_weights).float() print("===> Class Weights: {}".format(class_weights)) # Create model print('===> Building a Model') model = DenseNetClassifier() if use_cuda: model.cuda() print('===> Model built!') print('\t===> contains {} trainable params'.format(count_parameters(model))) # print('\t===> DenseNet Final Features: {}'.format(model.features.num_features)) criterion = WeightedKLDivWithLogitsLoss(class_weights) if use_cuda: criterion.cuda() optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=wd) os.makedirs(save_path, exist_ok=True) best_val_epoch = 0 best_val_loss = sys.float_info.max best_val_acc = 0.0 train_losses, train_accuracies = [], [] val_losses, val_accuracies = [], [] # optionally resume from a checkpoint if resume: if os.path.isfile(resume): print("===> Loading Checkpoint to Resume '{}'".format(resume)) checkpoint = torch.load(resume) start_epoch = checkpoint['epoch'] + 1 best_val_epoch = checkpoint['best_epoch'] best_val_loss = checkpoint['best_valid_loss'] best_val_acc = checkpoint['best_valid_accuracy'] model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) # scheduler.load_state_dict(checkpoint['scheduler']) print("\t===> Loaded Checkpoint '{}' (epoch {})".format(resume, checkpoint['epoch'])) else: raise FileNotFoundError("\t====> no checkpoint found at '{}'".format(resume)) start_time = timer() writer = SummaryWriter() for epoch in tqdm(range(start_epoch, epochs), desc="Epochs"): # Training train_loss, train_acc = train(model, train_loader, criterion, optimizer, use_cuda) train_losses.append(train_loss) train_accuracies.append(train_acc) val_loss, val_acc = evaluate(model, val_loader, criterion, use_cuda) val_losses.append(val_loss) val_accuracies.append(val_acc) print("train", train_loss, train_acc) print("val", val_loss, val_acc) # print("criterion", criterion) writer.add_scalar('train_loss', train_loss, epoch) writer.add_scalar('train_acc', train_acc, epoch) writer.add_scalar('validation_loss', val_loss, epoch) writer.add_scalar('validation_acc', val_acc, epoch) # writer.add_scalar('learning_rate', lr, epoch) is_best = val_loss < best_val_loss # is_best = val_acc > best_val_acc if is_best: best_val_epoch = epoch best_val_loss = val_loss best_val_acc = val_acc print("***IS BEST***") with open(os.path.join(save_path, 'train_result.txt'), 'w') as f: f.write('Best Validation Epoch: {}\n'.format(epoch)) f.write('Train Loss: {}\n'.format(train_loss)) f.write('Train Accuracy: {}\n'.format(train_acc)) f.write('Validation Loss: {}\n'.format(val_loss)) f.write('Validation Accuracy: {}\n'.format(val_acc)) torch.save(model, os.path.join(save_path, 'entire_model.pth')) save_checkpoint({ 'epoch': epoch, 'arch': str(model.__class__.__name__), 'state_dict': model.state_dict(), 'best_epoch': best_val_epoch, 'best_valid_loss': best_val_loss, 'best_valid_accuracy': best_val_acc, 'optimizer': optimizer.state_dict(), # 'scheduler': scheduler.state_dict(), }, is_best, filename=os.path.join(save_path, 'checkpoint.pth.tar')) loss_curve, acc_curve = plot_learning_curves(train_losses, val_losses, train_accuracies, val_accuracies) loss_curve.savefig(os.path.join(save_path, "loss.eps"), format='eps', bbox_inches='tight') acc_curve.savefig(os.path.join(save_path, "acc.eps"), format='eps', bbox_inches='tight') plt.close(loss_curve) plt.close(acc_curve) end_time = timer() print("\nDone! - Elapsed Time: {} minutes".format((end_time - start_time) / 60)) writer.export_scalars_to_json("./all_scalars.json") writer.close()