def main():
    # checkpoint dir
    ckpt_dir = parser.ckpt_dir
    mkdir(parser.result_dir)

    # dataset preparation
    test_data = create_dataset(dataset_name=dataset_name, set='val')
    test_data_loader = data.DataLoader(test_data,
                                       batch_size=1,
                                       shuffle=False,
                                       num_workers=parser.num_workers,
                                       pin_memory=True)

    # create model and optimizer
    model = create_model(num_classes=parser.num_classes, name='DeepLab')

    if parser.restore:
        print("loading checkpoint...")
        checkpoint = torch.load(os.path.join(ckpt_dir, ckpt_name))
        model.load_state_dict(checkpoint['model'])

    print("start evaluating...")
    print("pytorch version: " + TORCH_VERSION + ", cuda version: " +
          TORCH_CUDA_VERSION + ", cudnn version: " + CUDNN_VERSION)
    print("available graphical device: " + DEVICE_NAME)
    os.system("nvidia-smi")

    mIoU = evaluation(model, test_data_loader)

    print("finished evaluating, the mIoU on the evaluation set is: " +
          str(mIoU))
Esempio n. 2
0
 def load_trained_model(self, train_data):
     p = join(self.logdir, 'trained_model*')
     files = glob.glob(p)
     best_trained_model_path = max(files, key=getctime)
     trained_model = create_model(train_data)
     trained_model.load_state_dict(
         torch.load(best_trained_model_path, map_location=FLAGS.device))
     trained_model.to(FLAGS.device)
     return trained_model
Esempio n. 3
0
def train(train_data, val_data, saver):
    train_data.init_node_feats(FLAGS.device)
    val_data.init_node_feats(FLAGS.device)
    model = create_model(train_data)
    model = model.to(FLAGS.device)
    pytorch_total_params = sum(p.numel() for p in model.parameters())
    print("Number params: ", pytorch_total_params)
    moving_avg = MovingAverage(FLAGS.validation_window_size,
                               FLAGS.validation_metric != 'loss')
    pyg_graph = train_data.get_pyg_graph(FLAGS.device)
    optimizer = torch.optim.Adam(
        model.parameters(),
        lr=FLAGS.lr,
    )

    epoch_losses = []
    losses = []
    for epoch in range(FLAGS.num_epochs):
        t = time.time()
        model.train()
        model.zero_grad()
        loss, preds_train = model(pyg_graph, train_data)
        loss.backward()
        optimizer.step()
        loss = loss.item()
        with torch.no_grad():
            val_loss, preds_val = model(pyg_graph, val_data)
            val_loss = val_loss.item()
            eval_res_val = eval(preds_val, val_data, FLAGS.use_wikidata)
            epoch_losses.append(val_loss)
            losses.append(loss)

            if FLAGS.show_eval:
                print("Epoch: {:04d}, Train Loss: {:.5f}, Time: {:.5f}".format(
                    epoch, loss,
                    time.time() - t))
                print("Val Loss: {:.5f}".format(val_loss))
                print("Val Results: ...")
                pprint(eval_res_val)

            eval_res_val["loss"] = val_loss

            if len(moving_avg.results) == 0 or moving_avg.best_result(
                    eval_res_val[FLAGS.validation_metric]):
                saver.save_trained_model(model, epoch + 1)

            # Accuracy is used for the moving average
            moving_avg.add_to_moving_avg(eval_res_val[FLAGS.validation_metric])
            if moving_avg.stop():
                break

    if FLAGS.plot:
        visualize_loss(epoch_losses, losses)

    best_model = saver.load_trained_model(train_data)
    return best_model, model
Esempio n. 4
0
def main():
    # parameter initialization
    total_epoches = parser.max_epoch
    cur_epoch = 1
    # data_dir = parser.data_dir
    save_dir = parser.ckpt_dir

    # dataset preparation
    train_data = create_dataset(dataset_name=dataset_name, set='train')
    val_data = create_dataset(dataset_name=dataset_name, set='val')
    train_dataloader = data.DataLoader(train_data, batch_size=parser.batch_size, shuffle=True,
                                       num_workers=parser.num_workers, pin_memory=True)
    val_dataloader = data.DataLoader(val_data, batch_size=1, shuffle=False,
                                     num_workers=parser.num_workers, pin_memory=True)

    # create model and optimizer
    model = create_model(num_classes=parser.num_classes, name='DeepLab')
    optimizer = create_optimizer(model.get_optim_params(parser), lr=parser.learning_rate,
                                 momentum=parser.momentum, weight_decay=parser.weight_decay, name="SGD")
    # model = torch.nn.DataParallel(model)
    # patch_replication_callback(model)
    best_mIoU = 0
    best_epoch = 1

    if parser.restore:
        print("loading checkpoint...")
        checkpoint = torch.load(save_dir)
        cur_epoch = checkpoint['epoch']
        model.load_state_dict(checkpoint['model'])
        optimizer.load_state_dict(checkpoint['optimizer'])
        best_mIoU = checkpoint['best_mIoU']

    print("start training...")
    print("pytorch version: " + TORCH_VERSION + ", cuda version: " + TORCH_CUDA_VERSION +
          ", cudnn version: " + CUDNN_VERSION)
    print("available graphical device: " + DEVICE_NAME)
    os.system("nvidia-smi")

    for epoch in range(cur_epoch, total_epoches + 1):
        print("current epoch:" + str(epoch))
        train(model, optimizer, epoch, train_dataloader)
        print("now start evaluating...")
        cur_mIoU = validation(model, val_dataloader)
        if cur_mIoU > best_mIoU:
            best_epoch = epoch
            best_mIoU = cur_mIoU
            state_dict = {
                'epoch': epoch,
                'model': model.state_dict(),
                'optimizer': optimizer.state_dict(),
                'best_mIoU': best_mIoU
            }
            prefix = "Semantic_Segmentation_" + dataset_name
            sava_checkpoint(state_dict, save_dir, prefix=prefix)

    print("finished training, the best mIoU is: " + str(best_mIoU) + " in epoch " + str(best_epoch))
Esempio n. 5
0
def main():
    # dataset preparation
    source_data, target_data, val_data = create_dataset(mode='G2C')
    source_dataloader = Data.DataLoader(source_data,
                                        batch_size=parser.batch_size,
                                        shuffle=True,
                                        num_workers=parser.num_workers,
                                        pin_memory=True)
    target_dataloader = Data.DataLoader(target_data,
                                        batch_size=parser.batch_size,
                                        shuffle=True,
                                        num_workers=parser.num_workers,
                                        pin_memory=True)
    val_dataloader = Data.DataLoader(val_data,
                                     batch_size=parser.batch_size,
                                     shuffle=False,
                                     num_workers=parser.num_workers,
                                     pin_memory=True)
    source_dataloader_iter = enumerate(source_dataloader)
    target_dataloader_iter = enumerate(target_dataloader)

    save_dir = parser.ckpt_dir

    # create model and optimizer
    model = create_model(num_classes=parser.num_classes, name='DeepLab')
    D1 = Discriminator(num_classes=parser.num_classes)
    D2 = Discriminator(num_classes=parser.num_classes)

    optimizer_G = create_optimizer(model.get_optim_params(parser),
                                   lr=parser.learning_rate,
                                   momentum=parser.momentum,
                                   weight_decay=parser.weight_decay,
                                   name="SGD")
    optimizer_D1 = create_optimizer(D1.parameters(),
                                    lr=LEARNING_RATE_D,
                                    name="Adam",
                                    betas=BETAS)
    optimizer_D2 = create_optimizer(D2.parameters(),
                                    lr=LEARNING_RATE_D,
                                    name="Adam",
                                    betas=BETAS)

    optimizer_G.zero_grad()
    optimizer_D1.zero_grad()
    optimizer_D2.zero_grad()

    start_iter = 1
    last_mIoU = 0

    if parser.restore:
        print("loading checkpoint...")
        checkpoint = torch.load(save_dir)
        start_iter = checkpoint['iter']
        model.load_state_dict(checkpoint['model'])
        optimizer_G.load_state_dict(checkpoint['optimizer']['G'])
        optimizer_D1.load_state_dict(checkpoint['optimizer']['D1'])
        optimizer_D2.load_state_dict(checkpoint['optimizer']['D2'])
        last_mIoU = checkpoint['best_mIoU']

    print("start training...")
    print("pytorch version: " + TORCH_VERSION + ", cuda version: " +
          TORCH_CUDA_VERSION + ", cudnn version: " + CUDNN_VERSION)
    print("available graphical device: " + DEVICE_NAME)
    os.system("nvidia-smi")

    discriminator = {'D1': D1, 'D2': D2}
    optimizer = {'G': optimizer_G, 'D1': optimizer_D1, 'D2': optimizer_D2}

    best_mIoU, best_iter = train(model, discriminator, optimizer,
                                 source_dataloader_iter,
                                 target_dataloader_iter, val_dataloader,
                                 start_iter, last_mIoU)

    print("finished training, the best mIoU is: " + str(best_mIoU) +
          " in iteration " + str(best_iter))
Esempio n. 6
0
best_f1 = 0

train_dataset_pack, test_dataset_pack = data_2tensor(vocab_path, train_path,
                                                     test_path, label_path,
                                                     'cuda:0')

np.random.shuffle(train_dataset_pack)
length = len(train_dataset_pack)
train_length = int(length * 0.8)
val_length = length - train_length
print('{}维度,开始{},训练集{}条,验证集{}条================================'.format(
    dim, asp, train_length, length - train_length))
train_dataset_iterator = Iterator(train_dataset_pack[:train_length])
val_dataset_pack = train_dataset_pack[train_length:]
word2id, id2word, embedding_matrix, model, model_path = create_model(
    model_name, device, att, vocab_path, embedding_path, data_source, asp, dim,
    word_embedding_type, word_embedding_corpus)

model = model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
for epoch in range(epochs):
    loss_total = 0
    batch_count = 0
    print(
        'epoch:{}==================================================={}模型,device:{},batch_size:{},learning_rate:{}'
        .format(epoch, model_name, device, batch_size, learning_rate))
    for train_batch in train_dataset_iterator.next(batch_size, shuffle=False):
        pack = train_batch
        x, l, y = [i for i in zip(*pack)]
        x = torch.stack(x)
        l = torch.stack(l)
import context

import tensorflow as tf
from model import model_factory
from config import cfg

devices = tf.config.experimental.list_physical_devices('GPU')
for device in devices:
    tf.config.experimental.set_memory_growth(device, True)

model = model_factory.create_model(cfg)
model.summary()

x = tf.random.normal((1, 320, 320, 3))
outputs = model(x)
for o in outputs:
    print(o.shape)