Ejemplo n.º 1
0
def transfer_eval(runid, module_prep_model, task1, task2, weightsf, c):
    if 'model1' in c:
        # Support for transfer across models (e.g. from rnn to attn1511)
        # XXX: No separate model1_conf
        model1_module = importlib.import_module('.'+c['model1'], 'models')
        module_prep_model1 = model1_module.prep_model
    else:
        module_prep_model1 = module_prep_model

    # We construct both original and new model, then copy over
    # the weights from the original model
    print('Model')
    model1 = task1.build_model(module_prep_model1, do_compile=False)
    model = task2.build_model(module_prep_model)
    print('Model (weights)')
    model1.load_weights(weightsf)
    for n in model1.nodes.keys():
        if n in model.nodes:
            model.nodes[n].set_weights(model1.nodes[n].get_weights())
        else:
            print('- skipping (not in target) ' + n)
    print('Pre-training Transfer Evaluation')
    task2.eval(model)

    train_model(runid, model, task2, c)

    print('Predict&Eval (best val epoch)')
    res = task2.eval(model)
    return model, res
Ejemplo n.º 2
0
def main():
    """
    """
    placeholders = ['input', 'label']
    train_ops = ['train']
    log_ops = ['accuracy']
    files = get_data(config.DATA_DIRECTORY)
    queue_graph = create_image_queue_graph(files, config.PIXEL_DEPTH,
                                           config.HEIGHT, config.WIDTH,
                                           config.CHANNELS,
                                           config.BATCH_SIZE, config.CAPACITY)
    model_graph = create_model_graph(config.HEIGHT, config.WIDTH,
                                     config.CHANNELS, config.NUM_LABELS)
    train_model(queue_graph, model_graph, placeholders, train_ops, log_ops)
Ejemplo n.º 3
0
def transfer_eval(runid, module_prep_model, task1, task2, weightsf, c):
    # We construct both original and new model, then copy over
    # the weights from the original model
    print('Model')
    model1 = task1.build_model(module_prep_model, do_compile=False)
    model = task2.build_model(module_prep_model)
    print('Model (weights)')
    model1.load_weights(weightsf)
    for n in model1.nodes.keys():
        if n in model.nodes:
            model.nodes[n].set_weights(model1.nodes[n].get_weights())
        else:
            print('- skipping (not in target) ' + n)
    print('Pre-training Transfer Evaluation')
    task2.eval(model)

    train_model(runid, model, task2, c)

    print('Predict&Eval (best val epoch)')
    res = task2.eval(model)
    return model, res
Ejemplo n.º 4
0
def main(file_path, prep_config, aggregated=False, stats=False, stats_config=None, configs=True,
    output_file=None):
    ''' main function!
    Inputs:
    file_path - string - path to text files or compiled files
    prep_config - config - config used to aggregate files
    aggregated - boolean - t/f the specified file path is pre aggregated
    stats - boolean - t/f to calculate stats for data
    configs - bool/config - either config to use or generate configs
    '''

    if aggregated:
        data = load(file_path)
    else:
        data = prep(file_path, prep_config)

    if stats:
        data_stats = calc_stats(data, stats_config)
    else:
        data_stats = 0

    if configs == True:
        config_list = generate_configs()
    else:
        config_list = configs

    inferences = []
    trained_models = []

    for config in config_list:
        model = model_selection(config, data)
        trained_models.append(train_model(config, model, data))
        inferences.append(calc_inferences(config, trained_model, data))

    if output_file != None:
        helper.write_results(output_file, data_stats, trained_models, inferences)
        
    return (data_stats, trained_models, inferences)
Ejemplo n.º 5
0
                        type=float,
                        dest='GRAD_CLIP',
                        required=False)
    args = parser.parse_args()
    for k in vars(args):
        if getattr(args, k):
            setattr(config, k, getattr(args, k))

    # print({item:getattr(config, item) for item in dir(config) if not item.startswith("__")})
    print("Loading data....")
    data, labid_to_id, padding_idx = data_reader.load_data(config.DATA_DIR +
                                                           'data.csv')
    target_size = len(labid_to_id)

    print(f'Using Device: {config.DEVICE}')

    print(f'Starting training of {config.MODEL}...')

    if not os.path.exists(config.MODEL_SAVE_DIR):
        os.makedirs(config.MODEL_SAVE_DIR)

    if config.MODEL_NAME_SUFFIX != '':
        savename = config.MODEL + '_' + config.MODEL_NAME_SUFFIX
    else:
        savename = config.MODEL
    train_model(data,
                get_model(config.MODEL),
                target_size,
                padding_idx,
                n_fold=config.N_FOLDS,
                savename=savename)
def main(args):
    bert_tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
    bert_model = BertModel.from_pretrained('bert-base-uncased')
    dataset_descriptors = get_dataset_paths(args.dataset_json)
    dataset_types = [k['name'] for k in dataset_descriptors]

    # Init Bert layer and Conv
    model, conv_model, sent_embedder = init_common(args, bert_model)

    task_classifier = TaskClassifierGCDC(conv_model.get_n_blocks() *
                                         args.n_filters)
    ep_maker = EpisodeMaker(
        bert_tokenizer,
        args.max_len,
        args.max_sent,
        model.cnn.get_max_kernel(),
        args.device,
        datasets=dataset_descriptors,
        sent_embedder=None if args.finetune else sent_embedder)

    print(ep_maker.datasets['gcdc'])

    task_classifier = task_classifier.to(args.device)
    model = model.to(args.device)
    optim = torch.optim.Adam(list(model.parameters()) +
                             list(task_classifier.parameters()),
                             lr=args.lr)

    import random
    logging.info('Multitask training starting.')
    time_log = datetime.now().strftime('%y%m%d-%H%M%S')
    # writer = SummaryWriter(f'runs/multitaskep_{time_log}')
    for batch_nr in range(args.n_epochs):
        optim.zero_grad()
        dataset_type = 'gcdc'
        one_batch_dataset = ep_maker.get_episode(
            dataset_type=dataset_type,
            n_train=args.train_size_support)['support_set']

        binary, loss = loss_task_factory(dataset_type)

        train_acc, train_loss = train_model(model,
                                            task_classifier,
                                            one_batch_dataset,
                                            loss,
                                            optim,
                                            binary,
                                            disp_tqdm=False)
        # writer.add_scalar(f'Train/{dataset_type}/multi/accuracy', train_acc, batch_nr)
        # writer.add_scalar(f'Train/{dataset_type}/multi/loss', train_loss, batch_nr)

        logging.info("dataset_type %s, acc %.4f, loss %.4f", dataset_type,
                     train_acc, train_loss)
        logging.debug(
            "max of gradients of task_classifier: %f",
            max(p.grad.max() for p in task_classifier.parameters()
                ))  # we take the max because the mean wouldn't be informative
        logging.debug(
            "avg of gradients of model: %f",
            max(p.grad.max() for p in model.parameters()
                if p.grad is not None))

    for i in range(4):
        binary, loss = loss_task_factory('gcdc')
        test_set = ep_maker.datasets['gcdc'][i]['test']
        test_set.batch_size = 1
        test_set.shuffle()
        test_set = BertPreprocessor(test_set,
                                    sent_embedder,
                                    conv_model.get_max_kernel(),
                                    device=args.device,
                                    batch_size=8)
        acc, loss, _ = eval_model(model,
                                  task_classifier,
                                  test_set,
                                  loss,
                                  binary,
                                  disp_tqdm=False)
        logging.info("%s: accuracy %.4f", test_set.file, acc)
Ejemplo n.º 7
0
def main():
    paths = sys.argv[1:]
    """
    ['./Data/Train/Under_90_min_tuning/data.zip', './Data/Validation/Validation_10_percent/data.zip',
     './tuning_results.txt', './hyperparameter.txt']
    """

    with zipfile.ZipFile(paths[0], "r") as zip_ref:
        if not os.path.exists("Tmp"):
            os.mkdir("Tmp")
        if not os.path.exists(os.path.join("Tmp", "train")):
            os.mkdir(os.path.join("Tmp", "train"))
        zip_ref.extractall(os.path.join("Tmp", "train"))  # remove it at last

    create_json(os.path.join("Tmp", "train"),
                os.path.join('Tmp', 'train_corpus.json'))

    with zipfile.ZipFile(paths[1], "r") as zip_ref:
        if not os.path.exists("Tmp"):
            os.mkdir("Tmp")
        if not os.path.exists(os.path.join("Tmp", "validation")):
            os.mkdir(os.path.join("Tmp", "validation"))
        zip_ref.extractall(os.path.join("Tmp",
                                        "validation"))  # remove it at last

    create_json(os.path.join("Tmp", "validation"),
                os.path.join('Tmp', 'valid_corpus.json'))

    number_of_validation_examples = sum(
        1 for line in open(os.path.join('Tmp', 'valid_corpus.json')))
    print("number of validation examples: " +
          str(number_of_validation_examples))

    with open(paths[2], 'w') as f:  # create file for writing
        pass

    filters = [150, 200, 250]
    kernel_size = [7, 11, 15]
    units = [150, 200, 250]

    # load the train and test data
    data_gen = AudioGenerator(spectrogram=False)

    data_gen.load_train_data(desc_file=os.path.join(
        'Tmp',
        'train_corpus.json'))  # necessary to calculate mean and variance

    data_gen.load_validation_data(
        desc_file=os.path.join('Tmp', 'valid_corpus.json'))

    print("validation examples in datagen: " + str(len(data_gen.valid_texts)))

    for f in filters:
        for k in kernel_size:
            for u in units:
                model_end = final_model(
                    input_dim=13,
                    filters=f,  # loaded from hyperparameter
                    kernel_size=k,  # loaded from hyperparameter
                    conv_stride=2,
                    conv_border_mode='valid',
                    units=u,  # loaded from hyperparameter
                    activation='relu',
                    cell=GRU,
                    dropout_rate=1,
                    number_of_layers=2)

                train_model(
                    input_to_softmax=model_end,
                    #pickle_path=os.path.join('Tmp','model_end.pickle'),
                    train_json=os.path.join('Tmp', 'train_corpus.json'),
                    valid_json=os.path.join('Tmp', 'valid_corpus.json'),
                    save_model_path=os.path.join('Tmp', 'model.h5'),
                    epochs=10,  # changed
                    spectrogram=False)

                wer_sum = 0

                for i in range(number_of_validation_examples):
                    actual, predicted = get_predictions(
                        data_gen, i, 'validation', model_end,
                        os.path.join('Tmp', 'model.h5'))

                    wer_sum += wer(actual.split(), predicted.split())

                wer_sum /= number_of_validation_examples

                with open(paths[2], 'a') as file:  # open file for writing
                    file.write(
                        str(f) + " " + str(k) + " " + str(u) + " " +
                        str(wer_sum) + "\n")

    # now take the lowest wer and have the hyperparameters to paths[3]

    lines = []
    with open(paths[2], 'r') as f:
        lines = f.readlines()

    min_wer = sys.maxsize
    min_filter = None
    min_kernel_size = None
    min_units = None

    for l in lines:
        s = l.split()
        if float(s[3]) < min_wer:
            min_wer = float(s[3])
            min_filter = int(s[0])
            min_kernel_size = int(s[1])
            min_units = int(s[2])

    with open(paths[3], 'w') as f:
        f.write(str(min_filter) + "\n")
        f.write(str(min_kernel_size) + "\n")
        f.write(str(min_units) + "\n")

    shutil.rmtree("Tmp")
Ejemplo n.º 8
0
        params = {
            'path': args.features_dir,
            'model_path': args.model_dir,
            'batch_size': args.batch_size,
            'epochs': args.epochs,
            'learning_rate': args.learning_rate,
            'n_cnn_filters': [int(x) for x in args.n_filters.split('-')],
            'n_cnn_kernels': [int(x) for x in args.n_kernels.split('-')],
            'n_fc_units': [int(x) for x in args.n_fc_units.split('-')],
            'n_classes': args.n_classes,
            'train_val_ratio': args.train_val_ratio,
            'baseline_val_loss': args.baseline_val_loss,
        }

        train_model(params)

        print('model training complete')

    elif mode == 'prediction':

        params = {
            'test_data': args.test_speech_dir,
            'model_path': os.path.join(args.model_dir, 'vad_model.pt'),
            'smoothing': args.smoothing,
            'visualize': args.visualize,
            'parallel': args.parallel,
            'fig_path': args.fig_path
        }

        prediction(params)
Ejemplo n.º 9
0
    # Intersection over Union (Jaccard) used for scoring
    score_func = db_eval_iou
    
    # get the DAVIS 2016 data loaders
    loaders = {k: DataLoader(DAVIS(p["/"],p[k], s)) for k in ['train','val']}

    # get model and load pre-trained weights
    model = load_model( STM(new_arch=new_arch), p["weights"])

    # set trainable parameters
    select_params( model, contains=weight_hint)

    # loss function
    criterion = CrossEntropyLoss()
    
    # optimizier
    optimizer = Adam(model.parameters(), lr=learning_rate)

    # create logger
    log = Tracker()
    log.create_dir()
    log.set_save_string('state_dict_M_{}_E_{}_J_{:5f}_T_{}.pth' )
    
    # train model and validate after each epoch
    train_model(loaders, model, criterion, optimizer, log, score_func,
                batch_size=batch_size, mode=mode,num_epochs=num_epochs)

    # plot log file for statistics
    log.plot()
    
Ejemplo n.º 10
0
                
                if check_name("INIT", basename):
                    print("initializing with vgg weights")
                    model.load_state_dict(init_weights(modelzoo.vgg11(pretrained=True), model))
                
                model = model.to(device)
                
                if adam or finetune:
                    print("Using adam")
                    opt = optim.Adam(model.parameters())
                else:
                    opt = optim.SGD(model.parameters(), lr=LR, momentum=0.9)
                scheduler = lr_scheduler.StepLR(opt, step_size=BATCH_SIZE//2, gamma=0.1)

                train_model(model, loss, metric, opt, scheduler, dataloaders[o], device, dataset_sizes[o], 
                            inepoch_plot=False, loss_name="Batch DICE Loss", metric_name="Batch DICE", model_name=basename + "-" + o, num_epochs=NEPOCHS, 
                            save=True, savefolder=model_folder)

                print("Testing train result...")
                test_model(model, loss, metric, dataloaders[o], dataset_sizes[o], device,
                        loss_name="Batch DICE Loss", metric_name="Batch DICE")
        # Only test and show results
        else: 
            print("Loading saved result and testing full volume with {}...".format(basename))
            plt.title("Loss in all orientations")
            models = get_models(bias, e2d, res, small, bn, dunet, model_folder=model_folder)
            for i, o in enumerate(orientations):
                path = os.path.join(model_folder, basename + "-" + o + ".pkl")
                loaded_results = TrainResults.load(path)
                
                loaded_results.plot(show=False, loss_only=True, o=o)
Ejemplo n.º 11
0
for epoch in range(num_epochs):
    log.log_time('Epoch {}/{}'.format(epoch, num_epochs - 1))
    
    # Iterate over videos.
    for video_step, video_loader in enumerate(train_loader):
        video_loss = []

        # Iterate over frames.
        for _, sample in  enumerate(video_loader):
            n_samples += 1                                                
            
            # Send data to device
            y, x = sample['y'].to(device), sample['x'].to(device)
    
            # Train model with sample
            loss = train_model(model, {'x':x, 'y':y}, criterion, optimizer)
	        video_loss.append(loss) 
    
        # Logs per video
		log.log_time('Video: {}\tTotal Loss: {:.6f}\tAvg Loss: {:.6f}'
            .format(n_samples, np.sum(video_loss), np.average(video_loss)))
        
        # Test model
        # NOTE: len(train_loader) must be >> len(test_loader)

        if video_step % TEST_INTERVAL == 0:
            test_loss = []
            # Iterate over videos.
            for video_step, video_loader in enumerate(test_loader):
                # Iterate over frames.
                for _, sample in  enumerate(video_loader):
Ejemplo n.º 12
0
    dataloaders = data.DataLoader(DataLoader(args),
                                  batch_size=args.batch_size,
                                  shuffle=True,
                                  num_workers=args.workers,
                                  pin_memory=True)

    device = torch.device("cuda:0")

    print("constructing model ....")
    model = VSR(args.nframes)

    model = nn.DataParallel(model.to(device), gpuids)

    if args.resume:
        ckpt = torch.load(args.model_path)
        new_ckpt = {}
        for key in ckpt:
            if not key.startswith('module'):
                new_key = 'module.' + key
            else:
                new_key = key
            new_ckpt[new_key] = ckpt[key]
        model.load_state_dict(new_ckpt)
    print("model constructed")

    summary_writer = SummaryWriter(args.log_dir)

    optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
    scheduler = ExponentialLR(optimizer, gamma=args.gamma)
    train_model(model, optimizer, scheduler, dataloaders, summary_writer,
                device, args)
Ejemplo n.º 13
0
                              params=model_params,
                              regularizer=regularizer)

    logger.info("Loading the pretrained model from %s",
                os.path.join(args.pretrained_dir, args.pretrained_model_name))
    try:
        pretrained_model_state_path = os.path.join(args.pretrained_dir,
                                                   args.pretrained_model_name)
        pretrained_model_state = torch.load(pretrained_model_state_path)
        model.load_state_dict(state_dict=pretrained_model_state)
    except:
        raise ConfigurationError(
            "It appears that the configuration of the pretrained model and "
            "the model to fine-tune are not compatible. "
            "Please check the compatibility of the encoders and taggers in the "
            "config files.")

    ### Create multi-task trainer ###
    multi_task_trainer_params = params.pop("multi_task_trainer")
    trainer = MultiTaskTrainer.from_params(model=model,
                                           task_list=task_list,
                                           serialization_dir=serialization_dir,
                                           params=multi_task_trainer_params)

    ### Launch training ###
    metrics = train_model(multi_task_trainer=trainer, recover=False)
    if metrics is not None:
        logging.info(
            "Fine-tuning is finished ! Let's have a drink. It's on the house !"
        )
Ejemplo n.º 14
0
    ]
    test_data = [
        np.expand_dims(test_data[:, :, :, i], axis=3)
        for i in range(data_chn_num)
    ]
    ''' Train Networks'''
    train_config = TrainConfig(args)

    # U-Net (only FLAIR)
    train_dat = [train_data[0], train_trgt]
    test_dat = [test_data[0], test_trgt]
    train_model(train_config,
                START_TIME,
                net_depth=args.depth,
                SALIENCY=False,
                DILATION=False,
                restore_dir=None,
                net_type='FLAIR',
                train_dat=train_dat,
                test_dat=test_dat)

    # U-Net (only IAM)
    train_dat = [train_data[1], train_trgt]
    test_dat = [test_data[1], test_trgt]
    train_model(train_config,
                START_TIME,
                net_depth=args.depth,
                SALIENCY=False,
                DILATION=False,
                restore_dir=None,
                net_type='IAM',
Ejemplo n.º 15
0
if (vars.mode == 'test'):  #test
    #model.load_state_dict(torch.load("D:\\Projects\\Python\\Zeitoon Detection\"))
    model.load_state_dict(torch.load(vars.test_model))
    model = model.to(vars.device)

    test_model(model, vars.criterion, 'test')
else:
    optimizer = optim.SGD(model.parameters(),
                          lr=0.001 if vars.pre_trained else 0.06,
                          momentum=0.9)
    #optimizer = optim.Adam(model.parameters(), lr=0.05)
    # Decay LR by a factor of 0.6 every 6 epochs
    exp_lr_scheduler = lr_scheduler.StepLR(
        optimizer, step_size=10 if vars.pre_trained else 6, gamma=0.6)
    model = model.to(vars.device)
    model = train_model(model, vars.criterion, optimizer, exp_lr_scheduler,
                        vars.num_epochs)
    visualize_model(model)

# ######################################################################
# # ConvNet as fixed feature extractor
# # Here, we need to freeze all the network except the final layer. We need
# # to set ``requires_grad == False`` to freeze the parameters so that the
# # gradients are not computed in ``backward()``.
# # You can read more about this in the documentation
# # `here <https://pytorch.org/docs/notes/autograd.html#excluding-subgraphs-from-backward>`__.
# #

# model_conv = torchvision.models.resnet18(pretrained=True)
# for param in model_conv.parameters():
#     param.requires_grad = False
Ejemplo n.º 16
0
    # try:
    #     s = sys.argv[1]
    # except IndexError:
    #     s = ""
    # create_interactions(s)


    epochs = 1

    for i in range(0, 10):
        # Generate new data
        create_interactions(str(i))
        # Load
        model = load_model(name="model")
        model.compile(loss='mse',
           optimizer=RMSprop())
        # Train
        train_filename = "/ssd/train_extra.csv{}".format(i)
        model, losses = train_model(model, epochs, train_filename, nb_epoch=2)
        # Test
        print("MSE", losses[-1])
        test_filename = "/ssd/test_extra.csv{}".format(i)
        m = test_model(model, test_filename)
        # Save model
        save_model(model, name="model")
        # if m > 0.93:
        #     break



Ejemplo n.º 17
0
if args.phase == 'train':
    dataloader = data.DataLoader(DataLoader(args),
                                 batch_size=args.batch_size,
                                 shuffle=True,
                                 num_workers=args.workers,
                                 pin_memory=True)

    device = torch.device("cuda:0")

    print("constructing model ....")
    model1 = EDVR(128, args.nframes, 8, 5, 40)
    model1 = nn.DataParallel(model1.to(device), gpuids)

    model2 = D()
    model2 = nn.DataParallel(model2.to(device), gpuids)

    if args.resume:
        model1 = load_state_dict(model1, args.model_path)
        model2 = load_state_dict(model2, args.dis_model_path)
    print("model constructed")

    optimizer1 = torch.optim.Adam(model1.parameters(), lr=args.lr)
    scheduler1 = ExponentialLR(optimizer1, gamma=args.gamma)

    optimizer2 = torch.optim.Adam(model2.parameters(), lr=args.lr)
    scheduler2 = ExponentialLR(optimizer2, gamma=args.gamma)

    summery_writer = SummaryWriter(args.log_dir)

    train_model(model1, model2, optimizer1, optimizer2, scheduler1, scheduler2,
                dataloader, summery_writer, device, args)
Ejemplo n.º 18
0
def train_or_eval(do_train=True,
                  weights_type_bits=None,
                  hidden_sizes=None,
                  use_pca=None,
                  use_old_pca=None,
                  pca_d=None):
    weights_type, bits = ask_weight_info() if weights_type_bits is None \
                         else weights_type_bits
    hidden_sizes = ask_layer_info() if hidden_sizes is None else hidden_sizes
    use_pca = ask_use_pca() if use_pca is None else use_pca

    # TODO: need to ask for the number of hidden layers and their
    # sizes. When compiling / evaluating, we should be able to get
    # that information by looking at the weights, but that might mess
    # with our current method of determining whether the weights are
    # quantized or not. An easy fix is probably to actually check for
    # shift/scale values (maybe by looking at the shape of where they
    # should be) instead of only looking at the length of the weights
    # tuple.

    if use_pca:
        # Check if any PCA data exists.
        if Path('tf/emnist/test_pca.pkl').is_file():
            data = load_pickled('tf/emnist/test_pca.pkl')
            if use_old_pca:
                print('Using existing PCA data.')
            else:
                if use_old_pca is None:
                    questions = [
                        inquirer.Confirm(
                            'old_pca',
                            message=
                            'Use existing PCA data (%d principal components)?'
                            % data.images.shape[1])
                    ]
                    if inquirer.prompt(questions)['old_pca']:
                        print('Using existing PCA data.')
                    else:
                        gen_pca(pca_d)
                else:
                    if use_old_pca: print('Using existing PCA data.')
                    else: gen_pca(pca_d)
        else:
            if use_old_pca:
                print('train_or_eval error: use_old_pca is set but there \
is no existing PCA data')
                exit(-1)
            else:
                gen_pca(pca_d)

    # Write PCA flag to disk so we can easily check later whether PCA
    # is being used or not (so we use the appropriate number of
    # batches during evaluation).
    save_pickled('pca.pkl', use_pca)

    # Choose between float and quantized models.
    model = float_model if weights_type == 'float' else quantized_model

    # Load parameters and data for the chosen dataset.
    _, load_data \
        = choose_dataset('emnist' + ('_pca' if use_pca else ''))
    print('Loading data...')
    train_data, validation_data, test_data = load_data('tf/')

    # Choose which set to use (train, test, etc.).
    # We pass 3 for combined train+validation if PCA isn't being used,
    # otherwise we pass 2 to use only training data.
    images, labels = choose_images_labels(train_data, validation_data,
                                          test_data, 2 if use_pca else 3)
    example_shape = images.shape[1:]
    input_size = reduce(mul, example_shape, 1)

    with tf.Graph().as_default():

        # Build all of the ops.
        print("Initializing...")

        # Choose between a few different values for learning rate and
        # stuff depending on the options chosen by the user.
        learning_rate, max_steps, decay_step, decay_factor = choose_hypers(
            weights_type, bits)
        batch_size = 100

        x, y, weights, logits, loss_op, pred_op, train_op = build_ops(
            100, bits, learning_rate, decay_step, decay_factor, model,
            input_size, hidden_sizes)

        # Create session and initialize variables.
        sess = init_session()

        # Go.
        if do_train:
            print('Training model...')
            seq = train_model(sess,
                              model,
                              x,
                              y,
                              train_op,
                              loss_op,
                              pred_op,
                              weights,
                              images,
                              labels,
                              batch_size,
                              max_steps,
                              'tf/models/default/',
                              bits,
                              1.0,
                              log=False)
            i = 0
            for _, acc in seq:
                i += 1
                render_progress_bar(i, max_steps,
                                    (colored('Accuracy', 'yellow') + ': %0.02f%%') \
                                    % (acc * 100.0))
            clear_bottom_bar()
            print('Done training model.')
            print('Selecting weights with best accuracy (%.02f%%).' %
                  (acc * 100.0))
            print(str(acc))
        else:
            print('Evaluating model...')
            model.load_weights(sess,
                               'tf/models/default/',
                               num_bits=bits,
                               pca_d=pca_d)
            acc = evaluate(sess, x, y, pred_op, images, labels, 100, log=False)
            print('acc: %.02f' % acc)
Ejemplo n.º 19
0
def train_and_evaluate(num_epochs, model, optimizer, loss_fn, train_dataloader,
                       val_dataloader, early_stopping_criteria, directory,
                       use_bert, use_mongo):
    """Train on training set and evaluate on evaluation set
    Args:
        num_epochs: Number of epochs to run the training and evaluation
        model: Model
        optimizer: Optimizer
        loss_fn: Loss function
        dataloader: Dataloader for the training set
        val_dataloader: Dataloader for the validation set
        scheduler: Scheduler
        directory: Directory path name to story the logging files

    Returns train and evaluation metrics with epoch, loss, accuracy, recall, precision and f1-score
    """

    train_metrics = pd.DataFrame(
        columns=['epoch', 'loss', 'accuracy', 'recall', 'precision', 'f1'])
    val_metrics = pd.DataFrame(
        columns=['epoch', 'loss', 'accuracy', 'recall', 'precision', 'f1'])

    best_val_loss = float("inf")

    early_stop_step = 0

    for epoch in trange(num_epochs, desc="Epoch"):

        ### TRAINING ###
        train_results = train_model(model, optimizer, loss_fn,
                                    train_dataloader, device, use_bert)
        train_metrics.loc[len(train_metrics)] = {
            'epoch': epoch,
            'loss': train_results['loss'],
            'accuracy': train_results['accuracy'],
            'recall': train_results['recall'],
            'precision': train_results['precision'],
            'f1': train_results['f1']
        }
        if use_mongo: log_scalars(train_results, "Train")

        ### EVALUATION ###
        val_results = evaluate_model(model, optimizer, loss_fn, val_dataloader,
                                     device, use_bert)
        val_metrics.loc[len(val_metrics)] = {
            'epoch': epoch,
            'loss': val_results['loss'],
            'accuracy': val_results['accuracy'],
            'recall': val_results['recall'],
            'precision': val_results['precision'],
            'f1': val_results['f1']
        }
        if use_mongo: log_scalars(val_results, "Validation")

        #Save best and latest state
        best_model = val_results['loss'] < best_val_loss
        #last_model = epoch == num_epochs-1

        if best_model:
            save_checkpoint(
                {
                    'epoch': epoch + 1,
                    'state_dict': model.state_dict(),
                    'optim_dict': optimizer.state_dict()
                },
                directory=directory,
                checkpoint='best_model.pth.tar')

        #Early stopping
        if val_results['loss'] >= best_val_loss:
            early_stop_step += 1
            print("Early stop step:", early_stop_step)
        else:
            best_val_loss = val_results['loss']
            early_stop_step = 0

        stop_early = early_stop_step >= early_stopping_criteria

        if stop_early:
            print("Stopping early at epoch {}".format(epoch))

            return train_metrics, val_metrics

        print('\n')
        print('Train Loss: {} | Train Acc: {}'.format(
            train_results['loss'], train_results['accuracy']))
        print('Valid Loss: {} | Valid Acc: {}'.format(val_results['loss'],
                                                      val_results['accuracy']))
        print('Train recall: {} | Train precision: {} | Train f1: {}'.format(
            train_results['recall'], train_results['precision'],
            train_results['f1']))
        print('Valid recall: {} | Valid precision: {} | Valid f1 {}'.format(
            val_results['recall'], val_results['precision'],
            val_results['f1']))

    return train_metrics, val_metrics
Ejemplo n.º 20
0
    if not Path(file + '.json').is_file():
        print("File not found")
        load = 0
    else:
        model = load_model(file)

if load == 0:

    print("Creating neural network...")

    file = input("Name: ")
    epochs = int(input("Epcohs: "))
    batch = int(input("Batch Size: "))

    model = train_model(X_train, y_train, batch, epochs, file)

more_train = int(input("More training (0/1): "))

if more_train == 1:

    more_epochs = int(input("How much epochs: "))
    batch = int(input("Batch Size: "))
    model = train_model(X_train, y_train, batch, more_epochs, file)

predict = int(input("Predict y_test (0/1): "))

y_pred = model.predict(X_test)
y_pred_bool = (y_pred > 0.5)

if predict == 1:
Ejemplo n.º 21
0
from train import train_model
from data_loader import load
from examples.NIPS.MNIST.mnist import test_MNIST, MNIST_Net, neural_predicate
from model import Model
from optimizer import Optimizer
from network import Network
import torch

queries = load('train_data.txt')
test_queries = load('test_data.txt')

with open('addition.pl') as f:
    problog_string = f.read()

network = MNIST_Net()
net = Network(network, 'mnist_net', neural_predicate)
net.optimizer = torch.optim.Adam(network.parameters(), lr=0.001)
model = Model(problog_string, [net], caching=False)
optimizer = Optimizer(model, 2)

train_model(model,
            queries,
            1,
            optimizer,
            test_iter=1000,
            test=lambda x: x.accuracy(test_queries, test=True),
            snapshot_iter=10000)
Ejemplo n.º 22
0
if use_cuda:
    print('Using GPU')
    model.cuda()
else:
    print('Using CPU')

optimizer = optim.SGD(model.parameters(),
                      lr=config["lr"],
                      momentum=config["momentum"])
criterion = nn.CrossEntropyLoss()

# Run the functions and save the best model in the function model_ft.
model_ft, losses_train, accuracy_train, losses_val, accuracy_val = train_model(
    model,
    criterion,
    optimizer,
    dataloders,
    dataset_sizes,
    use_cuda,
    num_epochs=config["epochs"])

plt.figure(1)
plt.subplot(221)
plt.plot(losses_train)
plt.ylabel('Losses Train')
plt.subplot(222)
plt.plot(accuracy_train)
plt.ylabel('Accuracy Train')
plt.subplot(223)
plt.plot(losses_val)
plt.ylabel('Losses Eval')
plt.subplot(224)
Ejemplo n.º 23
0
Archivo: run.py Proyecto: floregol/gcn
print_partition_index(val_mask, "Val", y_val)
print_partition_index(test_mask, "Test", y_test)

# Some preprocessing
features = preprocess_features(features)
known_percent = show_data_stats(adj, features, y_train, y_val, y_test,
                                train_mask, val_mask, test_mask,
                                SHOW_TEST_VAL_DATASET_STATS)

model_func, support, sub_sampled_support, num_supports = get_model_and_support(
    'k-nn', adj, initial_train_mask, train_mask, val_mask, test_mask,
    WITH_TEST)
test_acc, list_node_correctly_classified = train_model(model_func,
                                                       num_supports,
                                                       support,
                                                       features,
                                                       y_train,
                                                       y_val,
                                                       y_test,
                                                       train_mask,
                                                       val_mask,
                                                       test_mask,
                                                       sub_sampled_support,
                                                       VERBOSE_TRAINING,
                                                       seed=seed,
                                                       list_adj=list_adj)
print(str(test_acc) + "%")
correct_paths_to_known, incorrect_paths_to_known = get_classification_stats(
    list_node_correctly_classified, get_list_from_mask(test_mask),
    paths_to_known_list)
print_classification_stats(correct_paths_to_known, incorrect_paths_to_known)
Ejemplo n.º 24
0
smoothing_parameter = 0.13
pos_prior = 0.78

response_list = []

context_list = []

label_response_dict = {}

word_frequency = {}
tokenizer = TweetTokenizer()

classified = []

sarcasm_tokens, non_sarcasm_tokens, total_sarcasm_tokens, total_non_sarcasm_tokens = train_model(
)

with open("data/test.jsonl", encoding="utf-8") as json_file:

    data = json.loads("[" + json_file.read().replace("}\n{", "},\n{") + "]")

    for p in data:
        response_list.append(p["response"].replace("@USER", ""))
        context_list.append(p["context"])

for line in response_list:
    words = tokenizer.tokenize(line.lower())

    sarcasm_prob = 0
    non_sarcasm_prob = 0
Ejemplo n.º 25
0
def main(current_path, args):

    print(args)
    # Give a folder path as an argument with '--log_dir' to save
    # TensorBoard summaries. Default is a log folder in current directory.
    # Create the directory for TensorBoard variables if there is not.
    if not os.path.exists(args.log_dir):
        os.makedirs(args.log_dir)
    filename = args.file

    # preprocessing raw data and building vocabulary
    vocabulary = read_data(filename)
    print('Data size', len(vocabulary))

    # Step 2: Build the dictionary and replace rare words with UNK token

    # Filling 4 global variables:
    # data - list of codes (integers from 0 to vocabulary_size-1).
    # This is the original text but words are replaced by their codes
    # count - map of words(strings) to count of occurrences
    # dictionary - map of words(strings) to their codes(integers)
    # reverse_dictionary - maps codes(integers) to words(strings)
    data, count, dictionary, reverse_dictionary = build_dataset(vocabulary, 0)
    del vocabulary  # Hint to reduce memory.

    vocabulary_size = len(dictionary)

    num_sampled = args.neg_samples  # Number of negative examples to sample.
    train_args = {
        'batch_size': args.train_bs,
        'embedding_size': args.emb_size,  # Dimension of the embedding vector.
        'skip_window':
        args.skip_window,  # How many words to consider left and right.
        'num_skips': args.
        num_skips,  # How many times to reuse an input to generate a label.
        'num_steps': args.epochs,  # Number of epochs
        'log_dir': args.log_dir
    }

    model = Word2Vec(args.train_bs, args.emb_size, vocabulary_size,
                     num_sampled)
    graph = model._graph
    embeddings = None
    with tf.Session(graph=graph) as session:
        # Open a writer to write summaries.
        writer = tf.summary.FileWriter(args.log_dir, session.graph)
        embeddings = train_model(session, model, reverse_dictionary, writer,
                                 data, **train_args)
        #similarity = model.similarity.eval()
        np.savetxt(args.emb_file, embeddings)

        # Save the model for checkpoints.
        model.savemodel.save(session, os.path.join(args.log_dir, 'model.ckpt'))

        # Create a configuration for visualizing embeddings with the labels in TensorBoard.
        config = projector.ProjectorConfig()
        embedding_conf = config.embeddings.add()
        embedding_conf.tensor_name = model.embeddings.name
        embedding_conf.metadata_path = os.path.join(args.log_dir,
                                                    'metadata.tsv')
        projector.visualize_embeddings(writer, config)

    # Closing writer
    writer.close()

    print("Plotting items on TSNE plot...")
    runTSNE(embeddings, reverse_dictionary, args.tsne_img_file)
Ejemplo n.º 26
0
        print('\n')
    with open('data2.bin', 'wb') as fp:
        pickle.dump(data, fp)


def createWord2Vec(modelFile, dataset):
    data = pickle.load(open(dataset, 'rb'))

    # train model
    model = Word2Vec(data, size=300, window=3, min_count=5, sg=1, workers=8)

    # save model
    model.save(modelFile)


def process_word2Vec(modelFile, words):
    model = Word2Vec.load(modelFile)
    for word in words:
        print(word)
        print(model.most_similar(word))


if __name__ == '__main__':
    process_embedding()
    tokenizer, max_len = train_model()

    # uncomment and change csvpath to fill a file with prediction from our model
    # since train_model save our best model in './model7.h5'we use this path to get it

    # fill_csv(csvpath, './model7.h5', tokenizer, max_len)
Ejemplo n.º 27
0
                          transform=composed_for_valset,
                          train=False)

datasets = {'train': train_dataset, 'val': val_dataset}

# pdb.set_trace()

dataloaders = {
    x: torch.utils.data.DataLoader(datasets[x],
                                   batch_size=batch_size,
                                   shuffle=True,
                                   num_workers=4)
    for x in ['train', 'val']
}

dataset_sizes = {x: len(datasets[x]) for x in ['train', 'val']}
class_names = datasets['train'].classes

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# pdb.set_trace()
track_sample = siss[91]
train_model(net.to(device),
            optimizer,
            scheduler,
            dataloaders,
            dataset_sizes,
            track_sample,
            batch_size,
            num_epochs=epochs)
Ejemplo n.º 28
0
from build_model import build_model
from train import train_model
from utils import parse_args
from dataset import get_streams

if __name__ == "__main__":
    args = parse_args()

    cost = build_model(args)
    train_stream, valid_stream = get_streams(args)

    train_model(cost, train_stream, valid_stream, args)
Ejemplo n.º 29
0
def main():
    print("Running train_aml.py")

    parser = argparse.ArgumentParser("train")
    parser.add_argument(
        "--model_name",
        type=str,
        help="Name of the Model",
        default="COVID19Articles_model.pkl",
    )

    parser.add_argument("--step_output",
                        type=str,
                        help=("output for passing data to next step"))

    parser.add_argument("--dataset_version",
                        type=str,
                        help=("dataset version"))

    parser.add_argument("--data_file_path",
                        type=str,
                        help=("data file path, if specified,\
               a new version of the dataset will be registered"))

    parser.add_argument(
        "--caller_run_id",
        type=str,
        help=("caller run id, for example ADF pipeline run id"))

    parser.add_argument("--dataset_name",
                        type=str,
                        help=("Dataset name. Dataset must be passed by name\
              to always get the desired dataset version\
              rather than the one used while the pipeline creation"))

    args = parser.parse_args()

    print("Argument [model_name]: %s" % args.model_name)
    print("Argument [step_output]: %s" % args.step_output)
    print("Argument [dataset_version]: %s" % args.dataset_version)
    print("Argument [data_file_path]: %s" % args.data_file_path)
    print("Argument [caller_run_id]: %s" % args.caller_run_id)
    print("Argument [dataset_name]: %s" % args.dataset_name)

    model_name = args.model_name
    step_output_path = args.step_output
    dataset_version = args.dataset_version
    data_file_path = args.data_file_path
    dataset_name = args.dataset_name

    run = Run.get_context()

    print("Getting training parameters")

    # Load the training parameters from the parameters file
    with open("parameters.json") as f:
        pars = json.load(f)
    try:
        train_args = pars["training"]
    except KeyError:
        print("Could not load training values from file")
        train_args = {}

    # Log the training parameters
    print(f"Parameters: {train_args}")
    for (k, v) in train_args.items():
        run.log(k, v)
        run.parent.log(k, v)

    # Get the dataset
    if (dataset_name):
        if (data_file_path == 'none'):
            dataset = Dataset.get_by_name(run.experiment.workspace,
                                          dataset_name,
                                          dataset_version)  # NOQA: E402, E501
        else:
            dataset = register_dataset(run.experiment.workspace, dataset_name,
                                       os.environ.get("DATASTORE_NAME"),
                                       data_file_path)
    else:
        e = ("No dataset provided")
        print(e)
        raise Exception(e)

    # Link dataset to the step run so it is trackable in the UI
    run.input_datasets['training_data'] = dataset
    run.parent.tag("dataset_id", value=dataset.id)

    # Split the data into test/train
    df = dataset.to_pandas_dataframe()
    data = split_data(df)

    # Train the model
    model = train_model(data, train_args)

    # Evaluate and log the metrics returned from the train function
    metrics = get_model_metrics(model, data)
    for (k, v) in metrics.items():
        run.log(k, v)
        run.parent.log(k, v)

    # Pass model file to next step
    os.makedirs(step_output_path, exist_ok=True)
    model_output_path = os.path.join(step_output_path, model_name)
    joblib.dump(value=model, filename=model_output_path)

    # Also upload model file to run outputs for history
    os.makedirs('outputs', exist_ok=True)
    output_path = os.path.join('outputs', model_name)
    joblib.dump(value=model, filename=output_path)

    run.tag("run_type", value="train")
    print(f"tags now present for run: {run.tags}")

    run.complete()
Ejemplo n.º 30
0
Archivo: main.py Proyecto: yyht/PRS
def main():
    args = parser.parse_args()
    logger = setup_logger()

    ## Use below for slurm setting.
    # slurm_job_id = os.getenv('SLURM_JOB_ID', 'nojobid')
    # slurm_proc_id = os.getenv('SLURM_PROC_ID', None)

    # unique_identifier = str(slurm_job_id)
    # if slurm_proc_id is not None:
    #     unique_identifier = unique_identifier + "_" + str(slurm_proc_id)
    unique_identifier = ''

    # Load config
    config_path = args.config
    episode_path = args.episode

    if args.resume_ckpt and not args.config:
        base_dir = os.path.dirname(os.path.dirname(args.resume_ckpt))
        config_path = os.path.join(base_dir, 'config.yaml')
        episode_path = os.path.join(base_dir, 'episode.yaml')
    config = yaml.load(open(config_path), Loader=yaml.FullLoader)
    episode = yaml.load(open(episode_path), Loader=yaml.FullLoader)
    config['data_schedule'] = episode

    # Override options
    for option in args.override.split('|'):
        if not option:
            continue
        address, value = option.split('=')
        keys = address.split('.')
        here = config
        for key in keys[:-1]:
            if key not in here:
                raise ValueError('{} is not defined in config file. '
                                 'Failed to override.'.format(address))
            here = here[key]
        if keys[-1] not in here:
            raise ValueError('{} is not defined in config file. '
                             'Failed to override.'.format(address))
        here[keys[-1]] = yaml.load(value, Loader=yaml.FullLoader)


    # Set log directory
    config['log_dir'] = os.path.join(args.log_dir, unique_identifier)
    if not args.resume_ckpt and os.path.exists(config['log_dir']):
        logger.warning('%s already exists' % config['log_dir'])
        input('Press enter to continue')

    # print the configuration
    print(colorful.bold_white("configuration:").styled_string)
    pprint(config)
    print(colorful.bold_white("configuration end").styled_string)

    if args.resume_ckpt and not args.log_dir:
        config['log_dir'] = os.path.dirname(
            os.path.dirname(args.resume_ckpt)
        )

    # Save config
    os.makedirs(config['log_dir'], mode=0o755, exist_ok=True)
    if not args.resume_ckpt or args.config:
        config_save_path = os.path.join(config['log_dir'], 'config.yaml')
        episode_save_path = os.path.join(config['log_dir'], 'episode.yaml')
        yaml.dump(config, open(config_save_path, 'w'))
        yaml.dump(episode, open(episode_save_path, 'w'))
        print(colorful.bold_yellow('config & episode saved to {}'.format(config['log_dir'])).styled_string)

    # Build components
    data_scheduler = DataScheduler(config)

    writer = SummaryWriter(config['log_dir'])
    model = MODEL[config['model_name']](config, writer)

    if args.resume_ckpt:
        model.load_state_dict(torch.load(args.resume_ckpt))
    model.to(config['device'])
    train_model(config, model, data_scheduler, writer)

    print(colorful.bold_white("\nThank you and Good Job Computer").styled_string)
Ejemplo n.º 31
0
from train import train_model
from data_loader import load
from examples.NIPS.MNIST.mnist import test_MNIST, MNIST_Net, neural_predicate
from model import Model
from optimizer import Optimizer
from network import Network
import torch

queries = load('train_data.txt')

with open('addition.pl') as f:
    problog_string = f.read()

network = MNIST_Net()
net = Network(network, 'mnist_net', neural_predicate)
net.optimizer = torch.optim.Adam(network.parameters(), lr=0.001)
model = Model(problog_string, [net], caching=True)
optimizer = Optimizer(model, 2)

train_model(model,
            queries,
            1,
            optimizer,
            test_iter=1000,
            test=test_MNIST,
            snapshot_iter=10000)
Ejemplo n.º 32
0
parser.add_argument(
    '--format',
    '-f',
    required=True,
    choices=['10x_h5', '10x_mtx'],
    help=
    'The format of input files. It has two options, \'10x_h5\' or \'10x_mtx\'.'
)
args = parser.parse_args()

#Argv 1: Pathway of the input file
if args.task == 'train':
    train.train_model(input_file=args.input_file,
                      weight_file=args.weight_file,
                      output_path=args.output_path,
                      gene_file=args.gene_file,
                      hidden_size=args.latent_size,
                      batch_size=args.batch_size,
                      optimizer=args.algorithm,
                      format_type=args.format)
#Argv 2: Pathway of the load_weight
elif args.task == 'prediction':
    load_model.load_weight(input_file=args.input_file,
                           load_weight_file=args.load_file,
                           weight_file=args.weight_file,
                           gene_file=args.gene_file,
                           hidden_size=args.latent_size,
                           output_path=args.output_path,
                           filtered=args.filtered,
                           optimizer=args.algorithm,
                           mode=args.mode,
                           format_type=args.format)
Ejemplo n.º 33
0
def main(opt):
    setup_seed(opt.seed)
    if torch.cuda.is_available():
        device = torch.device('cuda')
        torch.cuda.set_device(opt.gpu_id)
    else:
        device = torch.device('cpu')

    log_dir = opt.log_dir + '/' + opt.network + '-' + str(opt.layers)
    utils.mkdir(log_dir)

    model = get_model(opt)
    # model = nn.DataParallel(model, device_ids=[1, 2, 3])
    # model = nn.DataParallel(model, device_ids=[0, 1, 2, 3])
    # model = nn.DataParallel(model, device_ids=[4, 5, 6, 7])
    model = nn.DataParallel(model, device_ids=[0, 1, 2, 3, 4, 5, 6, 7])
    # model = convert_model(model)
    model = model.to(device)

    summary_writer = SummaryWriter(logdir=log_dir)
    weight = None
    if opt.classes == 9:
        weight = torch.tensor([1.8, 1, 1, 1.2, 1, 1.6, 1.2, 1.4, 1],
                              device=device)
    elif opt.classes == 8:
        weight = torch.tensor([1.8, 1, 1.2, 1.6, 1, 1.2, 1.8, 1],
                              device=device)
    elif opt.classes == 2:
        weight = torch.tensor([1., 1.5], device=device)

    if opt.criterion == 'lsr':
        criterion = LabelSmoothSoftmaxCE(weight=weight,
                                         use_focal_loss=opt.use_focal,
                                         reduction='sum').cuda()
    elif opt.criterion == 'focal':
        # criterion = FocalLoss(alpha=1, gamma=2, reduction='sum')
        criterion = FocalLoss2()
    elif opt.criterion == 'ce':
        criterion = nn.CrossEntropyLoss(weight=weight, reduction='sum').cuda()
    elif opt.criterion == 'bce':
        criterion = nn.BCEWithLogitsLoss(weight=weight, reduction='sum').cuda()

    if opt.classes > 2:
        # all data
        images, labels = utils.read_data(
            os.path.join(opt.root_dir, opt.train_dir),
            os.path.join(opt.root_dir, opt.train_label), opt.train_less,
            opt.clean_data)
    elif opt.classes == 2:
        # 2 categories
        images, labels = utils.read_ice_snow_data(
            os.path.join(opt.root_dir, opt.train_dir),
            os.path.join(opt.root_dir, opt.train_label))

    # 7 categories
    # images, labels = utils.read_non_ice_snow_data(
    #         os.path.join(opt.root_dir, opt.train_dir),
    #         os.path.join(opt.root_dir, opt.train_label))

    ################ devide set #################
    if opt.fore:
        train_im, train_label = images[opt.num_val:], labels[opt.num_val:]
        val_im, val_label = images[:opt.num_val], labels[:opt.num_val]
    else:
        train_im, train_label = images[:-opt.num_val], labels[:-opt.num_val]
        val_im, val_label = images[-opt.num_val:], labels[-opt.num_val:]

    if opt.cu_mode:
        train_data_1 = train_im[:4439], train_label[:4439]
        train_data_2 = train_im[:5385], train_label[:5385]
        train_data_3 = train_im, train_label
        # train_datas = [train_data_1, train_data_2, train_data_3]
        train_datas = [train_data_2, train_data_3]
        opt.num_epochs //= len(train_datas)
    else:
        train_datas = [(train_im, train_label)]
    val_data = val_im, val_label
    #########################################

    if opt.retrain:
        state_dict = torch.load(opt.model_dir + '/' + opt.network + '-' +
                                str(opt.layers) + '-' + str(opt.crop_size) +
                                '_model.ckpt')
        model.load_state_dict(state_dict)

    ################ optimizer #################
    if opt.retrain and not opt.teacher_mode:
        if opt.network in ['effnet']:
            for param in model.module.parameters():
                param.requires_grad = False
            for param in model.module._fc.parameters():
                param.requires_grad = True
            # for param in model.module._swish.parameters():
            #     param.requires_grad = True
            for param in model.module.model._bn1.parameters():
                param.requires_grad = True

        elif opt.network in ['resnet', 'resnext', \
                             'resnext_wsl_32x8d', 'resnext_wsl_32x16d', 'resnext_wsl_32x32d', \
                             'resnext_swsl']:
            for param in model.parameters():
                param.requires_grad = False
            for param in model.module.fc.parameters():
                param.requires_grad = True
            for param in model.module.layer4[2].bn3.parameters():
                # for param in model.module.layer4[2].bn2.parameters():
                param.requires_grad = True

        elif opt.network in ['pnasnet_m', 'senet_m']:
            for param in model.module.parameters():
                param.requires_grad = False
            for param in model.module.classifier.parameters():
                param.requires_grad = True
            if opt.network == 'senet_m':
                for param in model.module.features.layer4.parameters():
                    # for param in model.module.features.layer4[2].bn3.parameters():
                    param.requires_grad = True

        elif opt.network in ['inception_v3']:
            for param in model.parameters():
                param.requires_grad = False
            for param in model.fc.parameters():
                param.requires_grad = True
            for param in model.Mixed_7c.parameters():
                param.requires_grad = True
        else:
            for param in model.module.parameters():
                param.requires_grad = False
            for param in model.module.last_linear.parameters():
                param.requires_grad = True
            if opt.network in ['se_resnext50_32x4d', 'se_resnext101_32x4d']:
                for param in model.module.layer4[2].bn3.parameters():
                    param.requires_grad = True
            elif opt.network in ['senet154']:
                for param in model.module.layer4.parameters():
                    param.requires_grad = True
            elif opt.network in ['xception']:
                for param in model.module.bn4.parameters():
                    param.requires_grad = True
            elif opt.network in ['inceptionresnetv2']:
                for param in model.module.conv2d_7b.bn.parameters():
                    param.requires_grad = True
            elif opt.network in ['inceptionv4']:
                for param in model.module.features[-1].branch3.parameters():
                    param.requires_grad = True
            elif opt.network in ['fixpnas']:
                for param in model.module.cell_11.parameters():
                    param.requires_grad = True
                for param in model.module.cell_10.parameters():
                    param.requires_grad = True
                for param in model.module.cell_9.parameters():
                    param.requires_grad = True
        params = filter(lambda p: p.requires_grad, model.parameters())
    else:
        if opt.network in ['effnet'] and not opt.retrain:
            params = utils.add_weight_decay(model.module.model, 1e-4)
            params.append({
                'params': model.module._fc.parameters(),
                'lr': opt.lr * 10
            })
        else:
            params = utils.add_weight_decay(model, 1e-4)

    ################ optimizer #################
    optimizer = get_optimizer(opt, params, weight_decay=1e-4)
    if opt.scheduler in ['step', 'multistep', 'plateau', 'exponential']:
        scheduler = get_schedule(opt, optimizer)
    ############################################

    crop_size = opt.crop_size - 128
    val_transforms = my_transform(False, crop_size)
    val_dataset = WeatherDataset(val_data[0], val_data[1], val_transforms)
    val_loader = torch.utils.data.DataLoader(val_dataset,
                                             batch_size=opt.batch_size,
                                             shuffle=False,
                                             num_workers=8,
                                             pin_memory=True)
    for train_data in train_datas:
        val_dis = np.bincount(val_label) + 1e-20
        train_dis = np.bincount(train_data[1])
        print(val_dis, opt.num_val)
        print(train_dis, len(train_data[1]))

        train_transforms = my_transform(True, crop_size, opt.cutout,
                                        opt.n_holes, opt.length, opt.auto_aug,
                                        opt.rand_aug)
        train_dataset = WeatherDataset(train_data[0], train_data[1],
                                       train_transforms)

        train_loader = torch.utils.data.DataLoader(train_dataset,
                                                   batch_size=opt.batch_size,
                                                   shuffle=True,
                                                   num_workers=8,
                                                   drop_last=True,
                                                   pin_memory=True)

        loader = {'train': train_loader, 'val': val_loader}

        ################ scheduler #################
        if opt.scheduler in ['warmup', 'cycle', 'cos', 'cosw', 'sgdr']:
            scheduler = get_schedule(opt, optimizer, len(train_loader))
        ############################################

        model, acc = train_model(loader,
                                 model,
                                 criterion,
                                 optimizer,
                                 summary_writer,
                                 scheduler=scheduler,
                                 scheduler_name=opt.scheduler,
                                 num_epochs=opt.num_epochs,
                                 device=device,
                                 is_inception=opt.is_inception,
                                 mixup=opt.mixup,
                                 cutmix=opt.cutmix,
                                 alpha=opt.alpha,
                                 val_dis=val_dis)
        utils.mkdir(opt.model_dir)
        torch.save(
            model.state_dict(), opt.model_dir + '/' + opt.network + '-' +
            str(opt.layers) + '-' + str(crop_size) + '_model.ckpt')
Ejemplo n.º 34
0
if args.dataset != "mscoco" and (not "ro" in args.dataset
                                 or "predict" in args.trg_len_option
                                 or "average" in args.trg_len_option):
    #if "predict" in args.trg_len_option or "average" in args.trg_len_option:
    #trg_len_dic = torch.load(os.path.join(data_path(args.dataset), "trg_len"))
    trg_len_dic = torch.load(
        str(args.data_prefix / "trg_len_dic" / args.dataset[-4:]))
    trg_len_dic = organise_trg_len_dic(trg_len_dic)
if args.mode == 'train':
    logger.info('starting training')

    if args.dataset != "mscoco":
        train_model(args,
                    model,
                    train_real,
                    dev_real,
                    src=SRC,
                    trg=TRG,
                    trg_len_dic=trg_len_dic)
    else:
        train_model(args,
                    model,
                    train_real,
                    dev_real,
                    src=None,
                    trg=mscoco_dataset,
                    trg_len_dic=trg_len_dic)

elif args.mode == 'test':
    logger.info(
        'starting decoding from the pre-trained model, on the test set...')
Ejemplo n.º 35
0
def launch_training():

    train.train_model()
Ejemplo n.º 36
0
sequences_per_batch = 16
sequence_length = 16
learning_rate = 1e-3
number_epochs = 32

model = Char2Char(
    dictionary_size,
    embedding_size,
    lstm_hidden_size,
    number_lstm_layers,
    dropout_probability,
    output_size,
    device,
).to(device)

print(model)

model = train_model(
    model,
    dataset,
    device,
    sequences_per_batch=sequences_per_batch,
    sequence_length=sequence_length,
    epochs=number_epochs,
    learning_rate=learning_rate,
    show_loss_plot=True,
)

output = sample(model, dataset, device, 1000, "int adxl_decode(")
print(output)