Exemplo n.º 1
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
Exemplo n.º 2
0
elif opt_name == 'own':
    optimizer = net_model.get_optim()
    closure_bool = optimizer.closure_bool
else:
    print("ERROR:")
    print("There is no optimizer named {}".format(opt_name))

### restoraion
if config['restore']:
    restore_file = os.path.join(config['restore_path'], config['restore_file'])
    if os.path.isfile(restore_file):
        print("=> loading checkpoint '{}'".format(restore_file))

        checkpoint = torch.load(restore_file)
        start_epoch = checkpoint['epoch']
        net_model.load_state_dict(checkpoint['state_dict'])
        optimizer.load_state_dict(checkpoint['optimizer'])

        print("=> loaded checkpoint '{}' (epoch {})".format(
            start_epoch, checkpoint['epoch']))
    else:
        print("ERROR:")
        print("=> no checkpoint found at '{}'".format(restore_file))
        exit()
else:
    if not os.path.isdir(config['restore_path']):
        os.mkdir(config['restore_path'])

inputs = net_model.get_inputs()
outputs = net_model.get_outputs()
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
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()
Exemplo n.º 6
0
                    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/"


def compute_psnr(ref_im, res_im):
    ref_img = scipy.misc.fromimage(ref_im).astype(float) / 255.0

    res_img = scipy.misc.fromimage(res_im).astype(float) / 255.0
Exemplo n.º 7
0
        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 != '':
        print("load from ", opt.pretrained_model_path)
        pretrain_log = torch.load(opt.pretrained_model_path,
                                  map_location=opt.device)
        model.load_state_dict(pretrain_log['state_dict'])
        print("pre-tran states are loaded")

    model.to(opt.device)

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

    print("trian")
    for epoch in tqdm(range(1, opt.epochs + 1)):

        train_epoch(epoch, train_loader, model, criterion, optimizer, opt,
                    writer_train)

        val_loss, val_acc_top_1, val_acc_top_5 = val_epoch(
            epoch, validation_loader, model, criterion, opt)
Exemplo n.º 8
0
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
# idx = torch.randint(0, 100, ())