def main(): import dataset X = dataset.ImageFolder('./images') ind = np.random.permutation(len(X))[:500] X2 = [] for i in ind: X2.append(X[i][0].numpy()) X2 = np.array(X2).transpose(1, 0, 2, 3).reshape(3, -1).T cid, cc = kmeans(X2, 4, 50, -1, 1) print(cid.shape) print(cc) out = {'cc': cc} np.savez('cc.npz', **out)
def dataloader(batch_size, data_root, shuffle=False, num_workers=8): transform_test = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ]) testset = dataset.ImageFolder(data_root, transform_test) testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers) return testloader
def get_batches(args, flows): args.warp = False dset = dataset.ImageFolder( is_train=False, root=args.output_path, mv_dir='', args=args ) batches = [] assert len(flows)==dset.__len__() for idx in range(dset.__len__()): _, ctx_frames, fn = dset.__getitem__(idx) ctx_frames = ctx_frames[None,:,:,:] # print("frames") # print(ctx_frames.size()) batches.append((flows[idx], ctx_frames, fn)) return batches
def __init__(self, cfg, logger, model_dir, tensorboard_log_dir, **kwargs): super(RETrainer, self).__init__(cfg, logger, model_dir, tensorboard_log_dir, **kwargs) self.imp_value = torch.ones(cfg['CODE_CHNS']).cuda() self.order = torch.randperm(cfg['CODE_CHNS']).cuda() _, self.reorder = torch.sort(self.order) self.log.info('initialized imp order: ') self.log.info(self.order) imp_transform = transforms.Compose([ transforms.RandomCrop((256, 256)), transforms.RandomHorizontalFlip(), transforms.RandomVerticalFlip(), transforms.ToTensor(), ]) imp_set = dataset.ImageFolder(root=cfg['DATASET']['TRAIN_SET'], transform=imp_transform) self.imp_loader = data.DataLoader(dataset=imp_set, batch_size=1, shuffle=True, num_workers=cfg['WORKERS']) self.end_batch = cfg['RE_END_BATCH']
def get_batches_from_file(args): # TODO: torch stack batch, ctx frames for better performance dset = dataset.ImageFolder( is_train=False, root=args.output_path, mv_dir=MV_PATH, args=args ) batches = [] for idx in range(dset.__len__()): batch, ctx_frames, fn = dset.__getitem__(idx) batch = batch[None, :, :, :] ctx_frames = ctx_frames[None, :, :, :] with torch.no_grad(): batch = batch.cuda() cooked_batch = util.prepare_batch(batch, args.v_compress, args.warp) _,_,_,flows = cooked_batch batches.append((flows, ctx_frames, fn)) return batches
parser.add_argument('--iterations', type=int, default=24, help='unroll iterations') parser.add_argument('--checkpoint', type=int, help='unroll iterations') args = parser.parse_args() ## load 32x32 patches from images import dataset train_transform = transforms.Compose([ transforms.RandomCrop((32, 32)), #transforms.ToTensor(), ]) train_set = dataset.ImageFolder(root=args.train, transform=train_transform) train_loader = data.DataLoader(dataset=train_set, batch_size=args.batch_size, shuffle=True, num_workers=1) print('total images: {}; total batches: {}'.format(len(train_set), len(train_loader))) ## load networks on GPU import network encoder = network.EncoderCell().cuda() binarizer = network.Binarizer().cuda() decoder = network.DecoderCell().cuda()
def __init__(self, cfg, logger, model_dir, tensorboard_log_dir, **kwargs): torch.cuda.set_device(cfg['GPU_DEVICE']) self.nums_epoch = cfg['TRAIN']['NUM_EPOCH'] self.epoch = 0 self.wr_train_idx = 1 self.wr_eval_idx = 1 self.scale = 16 self.max_psnr = 0. self.max_ssim = 0. self.its_bpp = 24. self.log = logger self.alpha = cfg['TRAIN']['ALPHA'] self.beta = cfg['TRAIN']['BETA'] self.print_freq = cfg['PRINT_FREQ'] self.quant_level = cfg['QUA_LEVELS'] self.model_dir = model_dir self.writer = SummaryWriter(tensorboard_log_dir) self.encoder = Encoder(cfg).cuda() self.decoder = Decoder(cfg).cuda() self.quantizer = nn.ModuleList() self.entropy = nn.ModuleList() self.clip_value = cfg['TRAIN']['CLIP_VALUE'] self.part = [0] [ self.part.append(int(x * cfg['CODE_CHNS']) + self.part[i]) for i, x in enumerate(cfg['ENP']['PART']) ] for i in range(len(cfg['ENP']['PART'])): self.quantizer.append( GMMQuantizer(cfg['QUA_LEVELS'][i], cfg['QUA']['STD'][i], cfg['QUA']['PI'][i]).cuda()) self.entropy.append( ContextEstimater3d(cfg['QUA_LEVELS'][i], cfg['ENP']['FEAT_NUMS'][i]).cuda()) self.code_nums = cfg['CODE_CHNS'] self.solver_enc = optim.Adam(self.encoder.parameters(), lr=cfg['TRAIN']['LR_ENC']) Q_params = list() for i in range(len(cfg['QUA_LEVELS'])): Q_params.append({ 'params': self.quantizer[i].mean, 'lr': cfg['TRAIN']['LR_QUA'] }) Q_params.append({ 'params': self.quantizer[i].log_pi, 'lr': cfg['TRAIN']['LR_QUA'] * 0.2 }) Q_params.append({ 'params': self.quantizer[i].log_std, 'lr': cfg['TRAIN']['LR_QUA'] * 0.2 }) self.solver_qua = optim.Adam(Q_params) self.solver_etp = optim.Adam(self.entropy.parameters(), lr=cfg['TRAIN']['LR_ENP']) self.solver_dec = optim.Adam(self.decoder.parameters(), lr=cfg['TRAIN']['LR_DEC']) self.scheduler_enc = LS.MultiStepLR( self.solver_enc, milestones=cfg['TRAIN']['MILESTONES'], gamma=cfg['TRAIN']['GAMMA']) self.scheduler_qua = LS.MultiStepLR( self.solver_qua, milestones=cfg['TRAIN']['MILESTONES'], gamma=cfg['TRAIN']['GAMMA']) self.scheduler_etp = LS.MultiStepLR( self.solver_etp, milestones=cfg['TRAIN']['MILESTONES'], gamma=cfg['TRAIN']['GAMMA']) self.scheduler_dec = LS.MultiStepLR( self.solver_dec, milestones=cfg['TRAIN']['MILESTONES'], gamma=cfg['TRAIN']['GAMMA']) train_transform = transforms.Compose([ transforms.RandomCrop( (cfg['DATASET']['PATCH_SIZE'], cfg['DATASET']['PATCH_SIZE'])), transforms.RandomHorizontalFlip(), transforms.RandomVerticalFlip(), transforms.ToTensor(), ]) train_set = dataset.ImageFolder(root=cfg['DATASET']['TRAIN_SET'], transform=train_transform) self.train_loader = data.DataLoader( dataset=train_set, batch_size=cfg['TRAIN']['BATCH_SIZE'], shuffle=True, num_workers=cfg['WORKERS']) test_transform = transforms.Compose([ transforms.ToTensor(), ]) test_set = dataset.ImageFolder(root=cfg['DATASET']['TEST_SET'], transform=test_transform) self.test_loader = data.DataLoader(dataset=test_set, batch_size=1, shuffle=False, num_workers=cfg['WORKERS']) self.entropy_loss = nn.CrossEntropyLoss()
def main(): global args args = parser.parse_args() train_transform = transforms.Compose([ transforms.RandomCrop((32, 32)), transforms.RandomHorizontalFlip(), transforms.ToTensor() ]) train_dataset = dataset.ImageFolder(roots=args.train_paths, transform=train_transform) train_loader = data.DataLoader(dataset = train_dataset,\ batch_size = args.batch_size,\ shuffle = True,\ num_workers = args.num_workers ) encoder = compression_net.CompressionEncoder(rnn_type=args.rnn_type).cuda() binarizer = compression_net.CompressionBinarizer( code_size=args.code_size).cuda() decoder = compression_net.CompressionDecoder( rnn_type=args.rnn_type, code_size=args.code_size).cuda() optimizer = optim.Adam([ { 'params': encoder.parameters() }, { 'params': binarizer.parameters() }, { 'params': decoder.parameters() }, ], lr=args.lr) start_epoch = args.start_epoch if args.resume: if os.path.isdir('checkpoint'): print("=> loading checkpoint '{}'".format(args.resume)) encoder.load_state_dict( torch.load('checkpoint/{}_{}_{}/encoder_{:08d}.pth'.format( args.rnn_type, args.loss_type, args.code_size, args.resume))) binarizer.load_state_dict( torch.load('checkpoint/{}_{}_{}/binarizer_{:08d}.pth'.format( args.rnn_type, args.loss_type, args.code_size, args.resume))) decoder.load_state_dict( torch.load('checkpoint/{}_{}_{}/decoder_{:08d}.pth'.format( args.rnn_type, args.loss_type, args.code_size, args.resume))) #args.start_epoch = checkpoint['epoch'] #model.load_state_dict(checkpoint['state_dict']) #optimizer.load_state_dict(checkpoint['optimizer']) print("=> loaded checkpoint '{}'".format(args.resume)) start_epoch = args.resume + 1 else: print("=> no checkpoint found at '{}'".format(args.resume)) torch.manual_seed(23) scheduler = LS.MultiStepLR(optimizer, milestones=[3, 10, 20, 50, 100], gamma=0.5) for epoch in range(start_epoch, start_epoch + args.epochs): scheduler.step() train(train_loader, encoder, binarizer, decoder, epoch, optimizer) if epoch % args.save_freq == 0 or epoch == args.epochs - 1: if not os.path.exists('checkpoint'): os.mkdir('checkpoint') if not os.path.exists('checkpoint/{}_{}_{}'.format( args.rnn_type, args.loss_type, args.code_size)): os.mkdir('checkpoint/{}_{}_{}'.format(args.rnn_type, args.loss_type, args.code_size)) torch.save( encoder.state_dict(), 'checkpoint/{}_{}_{}/encoder_{:08d}.pth'.format( args.rnn_type, args.loss_type, args.code_size, epoch)) torch.save( binarizer.state_dict(), 'checkpoint/{}_{}_{}/binarizer_{:08d}.pth'.format( args.rnn_type, args.loss_type, args.code_size, epoch)) torch.save( decoder.state_dict(), 'checkpoint/{}_{}_{}/decoder_{:08d}.pth'.format( args.rnn_type, args.loss_type, args.code_size, epoch))
def load_from_image_folder(): train_set = dataset.ImageFolder(root=args.train, transform=train_transform) train_loader = data.DataLoader(dataset=train_set, batch_size=args.batch_size, shuffle=True, num_workers=1) return train_set, train_loader
transform_train = transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)), ]) transform_test = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)), ]) #trainset = torchvision.datasets.CIFAR10(root='/home/rzding/DATA', train=True, download=True, transform=transform_train) trainset = torchvision.datasets.CIFAR10(root='/home/rzding/DATA', train=True, download=True, transform=None) trainset = dataset.ImageFolder(root=, transform=transform_train) trainloader = torch.utils.data.DataLoader(trainset, batch_size=1, shuffle=True, num_workers=2) #testset = torchvision.datasets.CIFAR10(root='/home/rzding/DATA', train=False, download=True, transform=transform_train) testset = torchvision.datasets.CIFAR10(root='/home/rzding/DATA', train=False, download=True, transform=None) testloader = torch.utils.data.DataLoader(testset, batch_size=1, shuffle=False, num_workers=2) classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') #classes = ('airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') classes_f2c = {} for idx,a_class in enumerate(classes): if a_class in ['bird', 'plane', 'car', 'ship', 'truck']: classes_f2c[idx] = 0 elif a_class in ['cat', 'deer', 'dog', 'frog', 'horse']: classes_f2c[idx] = 1
'--iterations', type=int, default=16, help='unroll iterations') parser.add_argument( '--directory', '-d', required=True, type=str, help='directory of checkpoint') parser.add_argument('--checkpoint', type=int, help='unroll iterations') parser.add_argument('--evaluatePickle', '-p', required=True, type=str, help='folder of training images') args = parser.parse_args() from torchvision import transforms eval_transform = transforms.Compose([ transforms.ToTensor() ]) # evaluatePickle = "0to20.p" evaluatePickle = args.evaluatePickle txtFile = "resultTxt/"+ evaluatePickle[:-1]+"txt" +"." +args.directory eval_set = dataset.ImageFolder(root=args.eval,file_name = evaluatePickle ,train=False) eval_loader = data.DataLoader( dataset=eval_set, batch_size=args.batch_size, shuffle=True, num_workers=0) print('total images: {}; total batches: {}'.format( len(eval_set), len(eval_loader))) hypernet = network.HyperNetwork(eval_set.vid_count).cuda() encoder = network.EncoderCell().cuda() binarizer = network.Binarizer().cuda() decoder = network.DecoderCell().cuda() hypernet.eval() encoder.eval()