def main(args):
    device = torch.device('cuda' if args.use_cuda else 'cpu')
    args.sample_rate = {
        '8k': 8000,
        '16k': 16000,
        '24k': 24000,
        '48k': 48000,
    }[args.sample_rate]
    model = Model(
        rnn_layers=args.rnn_layers,
        rnn_units=args.rnn_units,
        win_len=args.win_len,
        win_inc=args.win_inc,
        fft_len=args.fft_len,
        win_type=args.win_type,
        mode=args.target_mode,
    )
    if not args.log_dir:
        writer = SummaryWriter(os.path.join(args.exp_dir, 'tensorboard'))
    else:
        writer = SummaryWriter(args.log_dir)
    model.to(device)
    if not args.decode:
        train(model, FLAGS, device, writer)
    reload_for_eval(model, FLAGS.exp_dir, FLAGS.use_cuda)
    decode(model, args, device)
Пример #2
0
class ModelLoader:
    def __init__(self, path: str):
        self.path = path
        self.net = Net()

    def load_model(self):
        self.net.load_state_dict(
            torch.load(self.path, map_location=torch.device('cpu')))
        self.net.eval()
        return self.net
Пример #3
0
def run_model(trainX, trainY, testX, testY, part):
    train_loader = DataLoader(TensorDataset(trainX, trainY),
                              batch_size=128,
                              shuffle=True)
    test_loader = DataLoader(TensorDataset(testX, testY))
    model = None
    if part:
        model = Net(part=True, embed_dim=EMBED_DIM, num_splits=NUM_PARTITIONS)
    else:
        model = Net(part=False, embed_dim=EMBED_DIM, num_splits=NUM_PARTITIONS)
    optimizer = optim.SGD(model.parameters(), lr=.01)  #Add momentum?
    for epoch in range(50):
        train(model, train_loader, optimizer, epoch)
    test(model, test_loader)
Пример #4
0
def Train(cfg):
    os.environ['CUDA_DEVICES_ORDER'] = 'PCI_BUS_ID'
    os.environ['CUDA_VISIBLE_DEVICES'] = ','.join(
        [str(item) for item in cfg.TASK.GPUS])
    device = torch.device('cuda' if len(cfg.TASK.GPUS) > 0 else 'cpu')

    # init logger
    output_dir = os.path.join(cfg.TASK.OUTPUT_ROOT_DIR, cfg.TASK.NAME)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    logger = setup_logger(cfg.TASK.NAME, output_dir, distributed_rank=0)

    # data loader
    train_loader = MakeTrainLoader(
        os.path.join(cfg.DATA.ROOT_DIR, cfg.DATA.TRAIN_PATH),
        cfg.DATA.TRAIN_BATCH)
    num_images = len(train_loader.dataset)
    print('total train data: ', num_images)

    # model
    model = Net().to(device)
    # loss
    loss_funcs = nn.CrossEntropyLoss().to(device)
    # optimizer
    params = [p for n, p in model.named_parameters()]
    param_groups = [{'params': params, 'lr': 0.1}]
    # optimizer = optim.SGD(param_groups, momentum=0.9, weight_decay=5e-4)
    optimizer = optim.Adam(param_groups)
    # lr scheduler
    lr_scheduler = BuildLRScheduler(optimizer, [50, 100, 200], 0.1)

    # start train
    for epoch_idx in range(0, cfg.SOLVER.EPOCHS):
        logger.info('train epoch: {0}'.format(epoch_idx))
        TrainEpoch(epoch_idx, train_loader, model, loss_funcs, optimizer,
                   lr_scheduler, device, logger)
def main():
    model = Net()
    if torch.cuda.is_available():
        model.cuda()
    else:
        pass
    model.apply(weights_init)

    if args.resume:
        if isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            model.load_state_dict(checkpoint['state_dict'])
            print("=> loaded checkpoint '{}'"
                  .format(args.resume))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    # 数据处理
    # 直接在train里面处理
    # dataParser = DataParser(batch_size)
    loss_function = nn.L1Loss()
    optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=0.9, weight_decay=5e-4)
    # train_scheduler = optim.lr_scheduler.MultiStepLR(optimizer,milestones=settings.MILESTONES,gamma=0.2)#learning rate decay
    scheduler = lr_scheduler.StepLR(optimizer, step_size=args.stepsize, gamma=args.gamma)

    log = Logger(join(TMP_DIR, '%s-%d-log.txt' % ('Adam', args.lr)))
    sys.stdout = log
    train_loss = []
    train_loss_detail = []

    for epoch in range(args.start_epoch, args.maxepoch):
        if epoch == 0:
            print("Performing initial testing...")
            # 暂时空着

        tr_avg_loss, tr_detail_loss = train(model = model,optimizer = optimizer,epoch= epoch,save_dir=join(TMP_DIR, 'epoch-%d-training-record' % epoch))
        test()

        log.flush()
        # Save checkpoint
        save_file = os.path.join(TMP_DIR, 'checkpoint_epoch{}.pth'.format(epoch))
        save_checkpoint({'epoch': epoch, 'state_dict': model.state_dict(), 'optimizer': optimizer.state_dict()})

        scheduler.step()  # 自动调整学习率
        train_loss.append(tr_avg_loss)
        train_loss_detail += tr_detail_loss
Пример #6
0
        #shape->[batch_size,10]
        v_norm = torch.sqrt(torch.sum(v**2, dim=2, keepdim=True)).squeeze()

        #shape->[batch_size,]
        _, predicted = v_norm.data.max(dim=1)
        if conf.cuda:
            predicted = predicted.cpu()
        predicted = predicted.numpy()

        predicted_true_num += torch.sum(predicted == labels)
        total_num += labels.shape[0]
    test_acc = predicted_true_num / total_num
    print "total number is {}".format(total_num)
    print "accuracy of test is {}".format(test_acc)


if __name__ == "__main__":
    net = Net()
    if conf.cuda:
        net.cuda()
    net.double()
    if conf.istraining:
        train(net)
        torch.save(net.state_dict(), conf.model_name)
    else:
        net.load_state_dict(
            torch.load(conf.model_name,
                       map_location=lambda storage, loc: storage))
        test(net)
    #print "learning rate is {}".format(conf.lr)
Пример #7
0
trainloader = ChestXrayDataSet(data_dir=DATA_DIR,
                                    image_list_file=TRAIN_IMAGE_LIST
                                    )
                                    # transform=transforms.Compose([
                                    #     transforms.Resize(256),
                                    #     transforms.TenCrop(224),
                                    #     # transforms.Lambda
                                    #     # (lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])),
                                    #     # transforms.Lambda
                                    #     # (lambda crops: torch.stack([normalize(crop) for crop in crops]))
                                    #     #
                                    #     ]))

# net = DenseNet(growthRate=12, depth=10, reduction=0.5, bottleneck=True, nClasses=14)
# net = LinkNet34(num_classes=14)
net = Net(14)
lr = 0.1
optimizer = optim.SGD(net.parameters(),
                      lr=lr,
                      momentum=0.9,
                      weight_decay=0.0005)

criterion = nn.MSELoss()
net.cuda()
gpu = True

for epoch in range(2):  # loop over the dataset multiple times
    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        # get the inputs
        inputs, labels = data
Пример #8
0

if __name__ == "__main__":
    parse = argparse.ArgumentParser()
    parse.add_argument("--train-csv-path", type=str, default="data/train.csv", help="Training csv path for label file")
    parse.add_argument("--valid-csv-path", type=str, default="data/valid.csv", help="Validation csv path for label file")
    parse.add_argument("--test-csv-path", type=str, default="data/test.csv", help="Testing csv path for label file")
    parse.add_argument("--batch-size", type=int, default=64, help="Batch size of images")
    parse.add_argument("--lr", type=float, default=0.005, help="Learning rate")
    parse.add_argument("--momentum", type=float, default=0.9, help="Momentum")
    parse.add_argument("--gamma", type=float, default=0.8, metavar="M",
                        help="Learning rate step gamma (default: 0.7)")
    opt = parse.parse_args()

    train_loader, valid_loader, test_loader = load_dataset(train_csv_path=opt.train_csv_path,
                                                            valid_csv_path=opt.valid_csv_path,
                                                            test_csv_path=opt.test_csv_path,
                                                            bs=opt.batch_size, workers=2, transform=True)
    
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    # Initialize model
    model = Net()
    model.to(device)

    # Define hyperparameter, optimizer, loss, scheduler
    optimizer = optim.SGD(model.parameters(), lr=opt.lr, momentum=opt.momentum)
    loss_fn = nn.CrossEntropyLoss()
    # scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=opt.gamma)

    train(model, device, train_loader, valid_loader, loss_fn, optimizer, epoch=50)
Пример #9
0
from collections import OrderedDict
from torch.autograd import Variable
import torch

parser = argparse.ArgumentParser()

parser.add_argument('num_batches', type=int, nargs=1, help='number of batches')
parser.add_argument('config', type=str, nargs=1, help='dataset configuration')

args = parser.parse_args()

num_batches = args.num_batches[0]
config = json.load(open(args.config[0]))

### model and reader
net_model = Net(config)
net_reader = NetReader(config)
net_model.train(mode=False)

data_loader = net_reader.getDataLoader()
inputs = net_model.get_inputs()
outputs = net_model.get_outputs()
criterion = net_model.get_criterion(config)

### trainable and restorable

restore_var = OrderedDict(net_model.get_restorable())
loss = 0

### optimizer detection
Пример #10
0
parser.add_argument("config", type=str, nargs=1, help='dataset configuration')
parser.add_argument("--shape", nargs=1, help="In shape", type=str)
parser.add_argument("--layer", nargs=1, help="Layer shape", type=str)

args = parser.parse_args()

if args.layer:
    shape = eval(''.join(args.shape[0]))
    layer_name = args.layer[0]
    print("In shape: {}\nOut shape: {}".format(
        shape, ShapeOutput(tuple(shape), layer_name)))
    exit()

print("Model: ")
config = json.load(open(args.config[0]))
net_model = Net(config)
inputs = net_model.get_inputs()
outputs = net_model.get_outputs()
for key, var in inputs.items():
    print("shape of {} is {}".format(key, var.data.numpy().shape))

for key, var in outputs.items():
    print("shape of {} is {}".format(key, var.data.numpy().shape))

print("Reader: ")

config = json.load(open(args.config[0]))
net_reader = NetReader(config)
obj0 = net_reader.dataset[0]
for key, var in obj0.items():
    if isinstance(var, int):
Пример #11
0
from flask import Flask, jsonify, request
from flask_restful import Api, Resource
import numpy as np
import traceback
import json
from model.model import Net
import torch
import cv2

app = Flask(__name__)
api = Api(app)

model = Net()
model.load_state_dict(torch.load('mnist_cnn.pt'))
model.eval()


# convert request_input dict to input accepted by model.
def parse_input(request_input):
    """parse input to make it ready for model input.

    Arguments:
        request_input::file- input received from API call.

    Returns:
        model_ready_input::torch.Tensor- data ready to be fed into model.
    """
    img = request_input.read()
    img = np.frombuffer(img, np.uint8)
    img = cv2.imdecode(img, cv2.IMREAD_COLOR)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Пример #12
0
                                    transform=transforms.Compose([
                                        transforms.Scale(32),
                                        RandomRotate((-180, 180)),
                                        transforms.ToTensor(),
                                        transforms.Normalize((0.1307, ),
                                                             (0.3081, ))
                                    ]))

train_loader = torch.utils.data.DataLoader(mnist_train_dataset,
                                           batch_size=args.batch_size,
                                           shuffle=True)
test_loader = torch.utils.data.DataLoader(mnist_test_dataset,
                                          batch_size=args.batch_size,
                                          shuffle=False)

model = Net(args.use_arf, args.orientation)
print(model)
if args.cuda:
    model.cuda()

optimizer = optim.Adadelta(model.parameters())
best_test_acc = 0.


def train(epoch):
    model.train()
    for batch_idx, (data, target) in enumerate(tqdm(train_loader)):
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        optimizer.zero_grad()
        # print(data.shape)
Пример #13
0
                                               num_workers=16,
                                               pin_memory=True)

    validation_dataset = Dataset(validation_data_path, opt.annotation_path,
                                 opt.labels_path)
    validation_loader = torch.utils.data.DataLoader(validation_dataset,
                                                    batch_size=opt.batch_size,
                                                    shuffle=False,
                                                    num_workers=16)

    writer_train = SummaryWriter(
        log_dir=os.path.join(opt.log_learning_path, 'train'))
    writer_val = SummaryWriter(
        log_dir=os.path.join(opt.log_learning_path, 'val'))

    model = Net()

    device = opt.device
    if opt.is_parallel:
        opt.device = torch.device(
            "cuda:0" if torch.cuda.is_available() else "cpu")
        if torch.cuda.device_count() > 1:
            model = nn.DataParallel(model)
            print("multi GPUs: ", torch.cuda.device_count())
    elif device != "":
        opt.device = "cuda:" + str(device)
    else:
        opt.device = torch.device(
            "cuda:0" if torch.cuda.is_available() else "cpu")

    if opt.pretrained_model_path != '':
Пример #14
0
def save_checkpoint(state, path, epoch=0, step=0):
    filename = os.path.join(path,
                            'checkpoint-{}-{}.ckpt.tar'.format(epoch, step))
    torch.save(state, filename)


config = json.load(open(args.config[0]))
lr = float(config["learning_rate"])
N = int(config["num_epoch"])
auto_save = int(config["auto_save"])
start_epoch = 0
opt_name = config["opt"]

### model and reader
net_model = Net(config)
net_reader = NetReader(config)
net_model.train(mode=True)

criterion = net_model.get_criterion(config)
data_loader = net_reader.getDataLoader()

### trainable and restorable
trainable_var = OrderedDict(net_model.get_trainable())
untrainable_var = OrderedDict(net_model.named_parameters())

for key, val in trainable_var.items():
    del untrainable_var[key]

restore_var = OrderedDict(net_model.get_restorable())
loss = 0
Пример #15
0
def train(args, pt_dir, train_loader, test_loader, writer, logger, hp, hp_str):
    model = Net(hp).cuda()

    if hp.train.optimizer == 'adam':
        optimizer = torch.optim.Adam(model.parameters(),
                                     lr=hp.train.adam.initlr)
    else:
        raise Exception("%s optimizer not supported" % hp.train.optimizer)

    git_hash = get_commit_hash()

    init_epoch = -1
    step = 0

    if args.checkpoint_path is not None:
        logger.info("Resuming from checkpoint: %s" % args.checkpoint_path)
        checkpoint = torch.load(args.checkpoint_path)
        model.load_state_dict(checkpoint['model'])
        optimizer.load_state_dict(checkpoint['optimizer'])
        step = checkpoint['step']
        init_epoch = checkpoint['epoch']
        git_hash = checkpoint['git_hash']
        if hp_str != checkpoint['hp_str']:
            logger.warning(
                "New hparams is different from checkpoint. Will use new.")
    else:
        logger.info("Starting new training run.")

    try:
        for epoch in itertools.count(init_epoch + 1):
            model.train()
            loader = tqdm.tqdm(train_loader, desc='Train data loader')
            for spec, target in loader:
                spec = spec.cuda()
                target = target.cuda()
                output = model(spec)
                loss = model.get_loss(output, target)

                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                step += 1

                loss = loss.item()
                if loss > 1e8 or math.isnan(loss):
                    logger.error("Loss exploded to %.02f at step %d!" %
                                 (loss, step))
                    raise Exception("Loss exploded")

                if step % hp.log.summary_interval == 0:
                    writer.train_logging(loss, step)
                    loader.set_description('Loss %.02f at step %d' %
                                           (loss, step))
            if epoch % hp.log.chkpt_interval == 0:
                save_path = os.path.join(
                    pt_dir, '%s_%s_%05d.pt' % (args.name, git_hash, epoch))
                torch.save(
                    {
                        'model': model.state_dict(),
                        'optimizer': optimizer.state_dict(),
                        'step': step,
                        'epoch': epoch,
                        'hp_str': hp_str,
                        'git_hash': git_hash,
                    }, save_path)
                logger.info("Saved checkpoint to: %s" % save_path)

            testing_model(model, test_loader, writer, step, hp)

    except Exception as e:
        logger.info("Exiting due to exception: %s" % e)
        traceback.print_exc()
Пример #16
0
 def __init__(self, path: str):
     self.path = path
     self.net = Net()
Пример #17
0
from model.model import Net

parser = argparse.ArgumentParser(description="PyTorch DeepDehazing")
parser.add_argument("--checkpoint",
                    type=str,
                    default="",
                    help="path to load model checkpoint")
parser.add_argument("--test",
                    type=str,
                    default="../ITS_data/test/data",
                    help="path to load test images")

opt = parser.parse_args()

# for i in range(669,670):
net = Net()
print(opt)

print(opt.checkpoint)
net.load_state_dict(torch.load(opt.checkpoint)['state_dict'])
net.eval()
net = nn.DataParallel(net, device_ids=[0]).cuda()

images = utils.load_all_image(opt.test)

ssims = []
psnrs = []
psnrs2 = []
str_label = "../ITS_data/test/label/"

Пример #18
0
def main():

    args = args_parser()

    logs = LogSaver(args)

    acc_test, loss_test, acc_train, loss_train = [], [], [], []

    # ToDo change this
    classes = [i for i in range(args.num_classes)]

    distrib = Distribute(args.num_workers, len(classes))

    train_data_distribution = copy.deepcopy(
        distrib.get_distribution(args.dstr_Train,
                                 args.n_labels_per_agent_Train,
                                 args.sub_labels_Train))

    test_data_distribution = copy.deepcopy(
        distrib.get_distribution(args.dstr_Test, args.n_labels_per_agent_Test,
                                 args.sub_labels_Test))

    print(train_data_distribution, "\n\n TEST DISTRIBUTION",
          test_data_distribution)

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

    fed_trainloaders, fed_testloaders, workers = get_dataloaders(
        train_data_distribution, test_data_distribution, args.dataset,
        args.train_bs, args.test_bs, args.num_workers)
    print("TRAINLOADERs ARE CREATED")

    batches = extract_batches_per_worker(fed_trainloaders)
    batches_test = extract_batches_per_worker(fed_testloaders)

    net = Net(10)

    # copy weights
    # w_glob = net.state_dict()

    net.to(device)
    loss_func = nn.CrossEntropyLoss()

    for rnd in range(args.rounds):

        w_local = {}
        n = []

        # For now all of the updates are calculated sequentially
        for worker in workers:

            trainloader = batches[worker]

            # Batch size is needed to calculate accuracy
            w, loss, acc = train(worker,
                                 net,
                                 trainloader,
                                 loss_func,
                                 args.local_ep,
                                 args.train_bs,
                                 device=device)
            # ToDo w -> w.state_dict()
            w_local[worker] = w  #.state_dict()
            n.append(len(trainloader))
            loss_train.append(copy.deepcopy(loss))
            acc_train.append(copy.deepcopy(acc))

        net = federated_avg(w_local)

        # w_glob = FedAvg(w_local, n)
        # Analog to model distribution
        # net.load_state_dict(w_glob)

        # Perform tests after global update
        for worker in workers:
            testloader = batches_test[worker]
            loss, acc = test(worker,
                             net,
                             testloader,
                             loss_func,
                             args.test_bs,
                             device=device)

            print(worker.id, "loss", loss, "acc", acc)
            acc_test.append(copy.deepcopy(acc))
            loss_test.append(copy.deepcopy(loss))

        print("Round", rnd)
        #print(acc_train[-1], loss_train[-1], acc_test[-1], loss_test[-1])
        logs.add_row(acc_train, loss_train, acc_test, loss_test)

    print("End of training")

    print(acc_train, "\n\n", type(acc_train))

    logs.plot(loss_train, loss_test, np.array(acc_train), np.array(acc_test))
    print("Plots are created\n", acc_train, "\n\n", loss_train)
    logs.save_model(net)
Пример #19
0
  torch.cuda.manual_seed(args.seed)

kwargs = {'num_works': 1, 'pin_memory': True} if args.cuda else {}
mnist_train_dataset = datasets.MNIST('data', train=True, download=True, transform=transforms.Compose([
  transforms.Scale(32), RandomRotate((-180, 180)), transforms.ToTensor(),
  transforms.Normalize((0.1307,), (0.3081,))]))

mnist_test_dataset = datasets.MNIST('data', train=False, transform=transforms.Compose([
  transforms.Scale(32), RandomRotate((-180, 180)), transforms.ToTensor(),
  transforms.Normalize((0.1307,), (0.3081,))]))

test_loader = torch.utils.data.DataLoader(mnist_test_dataset,
                                          batch_size=1,
                                          shuffle=False)

model = Net(use_arf=args.use_arf).cuda()
model.load_state_dict(torch.load('model/model_state.pth'))
print('Model loaded!')
# print(mnist_test_dataset[0])
# a, _ = mnist_test_dataset[0]
# a.unsqueeze_(0)
# output = model(a)


def normalize_output(img):
    img = img - img.min()
    img = img / img.max()
    return img


# Plot some images