def test(args): if args.gpu: ctx = [mx.gpu(0)] else: ctx = [mx.cpu(0)] if args.dataset == "Sony": out_channels = 12 scale = 2 else: out_channels = 27 scale = 3 # load data test_transform = utils.Compose([utils.ToTensor()]) test_dataset = data.MyDataset(args.dataset, "test", transform=test_transform) test_loader = gluon.data.DataLoader(test_dataset, batch_size=1, last_batch='discard') unet = net.UNet(out_channels, scale) unet.load_params(args.model, ctx=ctx) batches = 0 avg_psnr = 0. for img, gt in test_loader: batches += 1 imgs = gluon.utils.split_and_load(img[0], ctx) label = gluon.utils.split_and_load(gt[0], ctx) outputs = [] for x in imgs: outputs.append(unet(x)) metric.update(label, outputs) avg_psnr += 10 * math.log10(1 / metric.get()[1]) metric.reset() avg_psnr /= batches print('Test avg psnr: {:.3f}'.format(avg_psnr))
import torch.nn as nn import bilinear_model from torch.autograd import Variable import data from collections import OrderedDict import os import torch.backends.cudnn as cudnn import math os.environ["CUDA_VISIBLE_DEVICES"] = "1" trainset = data.MyDataset('./CUB200/train_images_shuffle.txt', transform=transforms.Compose([ transforms.Resize(448), transforms.RandomHorizontalFlip(), transforms.CenterCrop(448), transforms.ToTensor(), transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)) ])) trainloader = torch.utils.data.DataLoader(trainset, batch_size=16, shuffle=True, num_workers=4) testset = data.MyDataset('./CUB200/test_images_shuffle.txt', transform=transforms.Compose([ transforms.Resize(448), transforms.CenterCrop(448), transforms.ToTensor(), transforms.Normalize(mean=(0.485, 0.456, 0.406),
import model import accuracy parser = argparse.ArgumentParser(description='Compare to Tesseract OCR') parser.add_argument('-m', '--model', metavar='m', type=str, default='model.pth') parser.add_argument('input_dir', type=str, help='Input directory') args = parser.parse_args() device = torch.device('cpu') dataset = data.MyDataset(args.input_dir) net = model.MyNet().to(device) net.load_state_dict(torch.load(args.model, map_location='cpu')) net.eval() torch.no_grad() time_sum_tess = 0 time_sum_mynet = 0 accuracy_sum_tess = 0.0 accuracy_sum_mynet = 0.0 accuracy_worst_tess = float('inf') accuracy_worst_mynet = float('inf')
import torchvision.transforms as transforms import torch.nn as nn from torch.autograd import Variable import data from collections import OrderedDict import os import torch.backends.cudnn as cudnn import math from PIL import Image import han_model os.environ["CUDA_VISIBLE_DEVICES"] = "0" trainset = data.MyDataset('D:/Zhen/LMX/LMX/data/train_images_shuffle.txt', transform=transforms.Compose([ transforms.Resize((512, 512), Image.BILINEAR), transforms.RandomHorizontalFlip(), transforms.RandomCrop(448), transforms.ToTensor(), transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)) ])) trainloader = torch.utils.data.DataLoader(trainset, batch_size=16, shuffle=True, num_workers=4) testset = data.MyDataset('D:/Zhen/LMX/LMX/data/test_images_shuffle.txt', transform=transforms.Compose([ transforms.Resize((512, 512), Image.BILINEAR), transforms.CenterCrop(448), transforms.ToTensor(), transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)) ])) testloader = torch.utils.data.DataLoader(testset, batch_size=8, shuffle=False, num_workers=4) cudnn.benchmark = True
def train(args): np.random.seed(args.seed) if args.gpu: ctx = [mx.gpu(0)] else: ctx = [mx.cpu(0)] if args.dataset == "Sony": out_channels = 12 scale = 2 else: out_channels = 27 scale = 3 # load data train_transform = utils.Compose([ utils.RandomCrop(args.patch_size, scale), utils.RandomFlipLeftRight(), utils.RandomFlipTopBottom(), utils.RandomTranspose(), utils.ToTensor(), ]) train_dataset = data.MyDataset(args.dataset, "train", transform=train_transform) val_transform = utils.Compose([utils.ToTensor()]) val_dataset = data.MyDataset(args.dataset, "val", transform=val_transform) train_loader = gluon.data.DataLoader(train_dataset, shuffle=True, batch_size=args.batch_size, last_batch='rollover') val_loader = gluon.data.DataLoader(val_dataset, batch_size=1, last_batch='discard') unet = net.UNet(out_channels, scale) unet.initialize(init=initializer.Xavier(), ctx=ctx) # optimizer and loss trainer = gluon.Trainer(unet.collect_params(), 'adam', {'learning_rate': args.lr}) l1_loss = gluon.loss.L1Loss() print "Start training now.." for i in range(args.epochs): total_loss = 0 count = 0 profiler.set_state('run') for batch_id, (img, gt) in enumerate(train_loader): batch_size = img.shape[0] count += batch_size img_list = gluon.utils.split_and_load(img[0], ctx) gt_list = gluon.utils.split_and_load(gt[0], ctx) with autograd.record(): preds = [unet(x) for x in img_list] losses = [] for ii in range(len(preds)): loss = l1_loss(gt_list[ii], preds[ii]) losses.append(loss) for loss in losses: loss.backward() total_loss += sum([l.sum().asscalar() for l in losses]) avg_loss = total_loss / count trainer.step(batch_size) metric.update(gt_list, preds) F.waitall() profiler.set_state('stop') print profiler.dumps() break gt_save = gt_list[0] output_save = preds[0] if (batch_id + 1) % 100 == 0: message = "Epoch {}: [{}/{}]: l1_loss: {:.4f}".format( i + 1, count, len(train_dataset), avg_loss) print message temp = F.concat(gt_save, output_save, dim=3) temp = temp.asnumpy().reshape(temp.shape[2], temp.shape[3], 3) scipy.misc.toimage(temp * 255, high=255, low=0, cmin=0, cmax=255, mode='RGB').save(args.save_model_dir + '%04d_%05d_00_train.jpg' % (i + 1, count)) # evaluate batches = 0 avg_psnr = 0. for img, gt in val_loader: batches += 1 imgs = gluon.utils.split_and_load(img[0], ctx) label = gluon.utils.split_and_load(gt[0], ctx) outputs = [] for x in imgs: outputs.append(unet(x)) metric.update(label, outputs) avg_psnr += 10 * math.log10(1 / metric.get()[1]) metric.reset() avg_psnr /= batches print('Epoch {}: validation avg psnr: {:.3f}'.format(i + 1, avg_psnr)) # save model if (i + 1) % args.save_freq == 0: save_model_filename = "Epoch_" + str(i + 1) + ".params" save_model_path = os.path.join(args.save_model_dir, save_model_filename) unet.save_params(save_model_path) print("\nCheckpoint, trained model saved at", save_model_path) # save model save_model_filename = "Final_Epoch_" + str(i + 1) + ".params" save_model_path = os.path.join(args.save_model_dir, save_model_filename) unet.save_params(save_model_path) print("\nCheckpoint, trained model saved at", save_model_path)
import torchvision.transforms as transforms import torch.nn as nn from torch.autograd import Variable import data from collections import OrderedDict import os import torch.backends.cudnn as cudnn import math from PIL import Image import hbp_model os.environ["CUDA_VISIBLE_DEVICES"] = "1" trainset = data.MyDataset('/data/guijun/caffe-20160312/examples/compact_bilinear/train_images_shuffle.txt', transform=transforms.Compose([ transforms.Resize((600, 600), Image.BILINEAR), transforms.RandomHorizontalFlip(), transforms.RandomCrop(448), transforms.ToTensor(), transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)) ])) trainloader = torch.utils.data.DataLoader(trainset, batch_size=16, shuffle=True, num_workers=4) testset = data.MyDataset('/data/guijun/caffe-20160312/examples/compact_bilinear/test_images_shuffle.txt', transform=transforms.Compose([ transforms.Resize((600, 600), Image.BILINEAR), transforms.CenterCrop(448), transforms.ToTensor(), transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)) ])) testloader = torch.utils.data.DataLoader(testset, batch_size=8, shuffle=False, num_workers=4) cudnn.benchmark = True
import numpy as np import models import data import utils parser = argparse.ArgumentParser("Test multimodal VAE.") parser.add_argument("-opts", help="option file", type=str, required=True) parser.add_argument("-banned", help="banned modalities", nargs="+", type=int, required=True) parser.add_argument("-prefix", help="output prefix", type=str, required=True) args = parser.parse_args() opts = yaml.safe_load(open(args.opts, "r")) print(yaml.dump(opts)) testset = data.MyDataset("data", modality=["endpoint", "joint"], action=["grasp", "move"], mode="test") model = models.MultiVAE( in_blocks=opts["in_blocks"], in_shared=opts["in_shared"], out_shared=opts["out_shared"], out_blocks=opts["out_blocks"], init=opts["init_method"]) model.to(opts["device"]) model.load(opts["save"], "multivae_last") model.cpu().eval() print(model) out_folder = os.path.join(opts["save"], "outs") yje = torch.zeros(7) zje = torch.zeros(7)
from matplotlib.backends.backend_pdf import PdfPages import models import data import utils parser = argparse.ArgumentParser("Test multimodal VAE.") parser.add_argument("-opts", help="option file", type=str, required=True) parser.add_argument("-banned", help="banned modalities", nargs="+", type=int, required=True) parser.add_argument("-prefix", help="output prefix", type=str, required=True) args = parser.parse_args() opts = yaml.safe_load(open(args.opts, "r")) print(yaml.dump(opts)) trainset = data.MyDataset(opts["data"], modality=opts["modality"], action=opts["action"], mode="train") testset = data.MyDataset(opts["data"], modality=opts["modality"], action=opts["action"], mode="test") model = models.MultiVAE( in_blocks=opts["in_blocks"], in_shared=opts["in_shared"], out_shared=opts["out_shared"], out_blocks=opts["out_blocks"], init=opts["init_method"]) model.to(opts["device"]) model.load(opts["save"], "multivae_best") model.cpu().eval() print(model) out_folder = os.path.join(opts["save"], "outs") if not os.path.exists(out_folder):
opts = yaml.safe_load(open(args.opts, "r")) if not os.path.exists(opts["save"]): os.makedirs(opts["save"]) opts["time"] = time.asctime(time.localtime(time.time())) dev = opts["device"] file = open(os.path.join(opts["save"], "opts.yaml"), "w") yaml.dump(opts, file) file.close() print(yaml.dump(opts)) logdir = os.path.join(opts["save"], "log") writer = SummaryWriter(logdir) idx = torch.randperm(opts["traj_count"])[:opts["traj_count"]].tolist() trainset = data.MyDataset(opts["data"], modality=opts["modality"], action=opts["action"], mode="train", traj_list=idx) valset = data.MyDataset(opts["data"], modality=opts["modality"], action=opts["action"], mode="val") trainloader = torch.utils.data.DataLoader(trainset, batch_size=opts["batch_size"], shuffle=True) valloader = torch.utils.data.DataLoader(valset, batch_size=10000, shuffle=False) val_sample = iter(valloader).next() x_val = trainset.normalize(val_sample) x_val = [x.to(dev) for x in x_val] model = models.MultiVAE( in_blocks=opts["in_blocks"], in_shared=opts["in_shared"], out_shared=opts["out_shared"], out_blocks=opts["out_blocks"], init=opts["init_method"]) model.to(dev)
transform = torchvision.transforms.Compose([ torchvision.transforms.Resize(config.image_size), # RandomCrop(224), torchvision.transforms.RandomHorizontalFlip(), torchvision.transforms.ToTensor() ]) test_transform = torchvision.transforms.Compose([ torchvision.transforms.Resize(config.image_size), # CenterCrop(224), torchvision.transforms.ToTensor() ]) print('Loading dataset ...') src_dataset = data.MyDataset(path=src_domain_dir, labels='all', transform=transform) label2index = src_dataset.label2index tgt_dataset = data.MyDataset(path=tgt_domain_dir, labels=data.shared_classes, transform=transform, label2index=label2index) src_test_dataset = data.MyDataset(path=src_domain_dir, labels='all', transform=test_transform, label2index=label2index) tgt_test_dataset = data.MyDataset(path=tgt_domain_dir, labels=data.shared_classes, transform=test_transform, label2index=label2index)