model.train(False) for params in model.parameters(): params.requires_grad = False model = model.to(DEVICE) normalize = transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] ) preprocess = transforms.Compose([ transforms.Scale(256), transforms.CenterCrop(224), transforms.ToTensor(), normalize]) in_path = "/content/drive/My Drive/Lorenzo/ego-rnn-two-in-one/get_modulation/frames" out_path = "/content/drive/My Drive/Lorenzo/ego-rnn-two-in-one/get_modulation/out2" inputFlow = [] inputFrame = [] frames = [] for i in range(1, len(os.listdir(in_path+"/rgb"))+1): print(i) # FLOW X fl_name = in_path + '/X/flow_x_' + str(int(round(i))).zfill(5) + '.png' # zfill used to add leading zeros img = Image.open(fl_name) # load single optical x frame
def main(): torch.manual_seed(args.seed) os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_devices use_gpu = torch.cuda.is_available() if not args.evaluate: sys.stdout = Logger(osp.join(args.save_dir, 'log_train.txt'), mode='a') else: sys.stdout = Logger(osp.join(args.save_dir, 'log_test.txt'), mode='a') print("==========\nArgs:{}\n==========".format(args)) if use_gpu: print("Currently using GPU {}".format(args.gpu_devices)) cudnn.benchmark = True torch.cuda.manual_seed_all(args.seed) else: print("Currently using CPU (GPU is highly recommended)") print("Initializing dataset {}".format(args.dataset)) dataset = data_manager.init_imgreid_dataset(name=args.dataset, dataset_dir=args.root, fore_dir=args.fore_dir) transform_train = ST.Compose([ ST.Scale((args.height, args.width), interpolation=3), ST.RandomHorizontalFlip(), ST.ToTensor(), ST.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ST.RandomErasing(0.5) ]) transform_test = ST.Compose([ ST.Scale((args.height, args.width), interpolation=3), ST.ToTensor(), ST.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) pin_memory = True if use_gpu else False trainloader = DataLoader( ImageDataset_hardSplit_seg(dataset.train, transform=transform_train), sampler=RandomIdentitySampler(dataset.train, num_instances=args.num_instances), batch_size=args.train_batch, num_workers=args.workers, pin_memory=pin_memory, drop_last=True, ) queryloader = DataLoader( ImageDataset(dataset.query, transform=transform_test), batch_size=args.test_batch, shuffle=False, num_workers=args.workers, pin_memory=pin_memory, drop_last=False, ) galleryloader = DataLoader( ImageDataset(dataset.gallery, transform=transform_test), batch_size=args.test_batch, shuffle=False, num_workers=args.workers, pin_memory=pin_memory, drop_last=False, ) print("Initializing model: {}".format(args.arch)) model = models.init_model(name=args.arch, num_classes=dataset.num_train_pids) print(model) criterion_xent = CrossEntropyLabelSmooth(use_gpu=use_gpu) criterion_htri = TripletLoss() criterion_mask = MaskLoss() criterion_split = HardSplitLoss() criterion_cluster = ClusterLoss() optimizer = init_optim(args.optim, model.parameters(), args.lr, args.weight_decay) scheduler = lr_scheduler.MultiStepLR(optimizer, milestones=args.stepsize, gamma=args.gamma) if args.resume: if check_isfile(args.resume): checkpoint = torch.load(args.resume) model.load_state_dict(checkpoint['state_dict']) args.start_epoch = checkpoint['epoch'] rank1 = checkpoint['rank1'] print("Loaded checkpoint from '{}'".format(args.resume)) print("- start_epoch: {}\n- rank1: {}".format( args.start_epoch, rank1)) if use_gpu: model = nn.DataParallel(model).cuda() if args.evaluate: print("Evaluate only") test(model, queryloader, galleryloader, use_gpu) return start_time = time.time() train_time = 0 best_rank1 = -np.inf best_epoch = 0 print("==> Start training") for epoch in range(args.start_epoch, args.max_epoch): start_train_time = time.time() train(epoch, model, criterion_xent, criterion_htri, criterion_mask, criterion_split, criterion_cluster, optimizer, trainloader, use_gpu) train_time += round(time.time() - start_train_time) scheduler.step() if (epoch + 1) > args.start_eval and ( epoch + 1) % args.eval_step == 0 or epoch == 0: print("==> Test") rank1 = test(model, queryloader, galleryloader, use_gpu) is_best = rank1 > best_rank1 if is_best: best_rank1 = rank1 best_epoch = epoch + 1 if use_gpu: state_dict = model.module.state_dict() else: state_dict = model.state_dict() save_checkpoint( { 'state_dict': state_dict, 'rank1': rank1, 'epoch': epoch, }, is_best, osp.join(args.save_dir, 'checkpoint_ep' + str(epoch + 1) + '.pth.tar')) print("==> Best Rank-1 {:.1%}, achieved at epoch {}".format( best_rank1, best_epoch)) elapsed = round(time.time() - start_time) elapsed = str(datetime.timedelta(seconds=elapsed)) train_time = str(datetime.timedelta(seconds=train_time)) print( "Finished. Total elapsed time (h:m:s): {}. Training time (h:m:s): {}.". format(elapsed, train_time)) print("==========\nArgs:{}\n==========".format(args))
import lrp from tqdm import tqdm import os import random # In[2]: norm_value = 1 # ds_mean = [114.7748 / norm_value, 107.7354 / norm_value, 99.4750 / norm_value] ds_mean = [90.0, 98.0, 102.0] ds_std = [38.7568578 / norm_value, 37.88248729 / norm_value,40.02898126 / norm_value] st = spatial_transforms.Compose([ spatial_transforms.Scale(112), spatial_transforms.CornerCrop(112, 'c'), spatial_transforms.ToTensor(1), spatial_transforms.Normalize(ds_mean, [1,1,1]) #Hara et. al. normalised by mean only # spatial_transforms.Normalize(ds_mean, ds_std) ]) tt = temporal_transforms.LoopPadding(16) ds = ucf101.UCF101( root_path="/media/datasets/Video/UCF-101/jpg", annotation_path="/media/datasets/Video/UCF-101/ucf101_01.json", subset="validation", n_samples_for_each_video=0, spatial_transform=st, temporal_transform=tt ) # In[3]:
crop_method = spatial_transforms.MultiScaleCornerCrop( args.scales, args.sample_size, crop_positions=['c']) before_crop_duration = int(args.sample_duration * args.downsample) if args.mode == 'train': temporal_transform = TemporalSequentialCrop(before_crop_duration, args.downsample) if args.view == 'front_depth' or args.view == 'front_IR': spatial_transform = spatial_transforms.Compose([ crop_method, spatial_transforms.RandomRotate(), spatial_transforms.SaltImage(), spatial_transforms.Dropout(), spatial_transforms.ToTensor(args.norm_value), spatial_transforms.Normalize([0], [1]) ]) elif args.view == 'top_depth' or args.view == 'top_IR': spatial_transform = spatial_transforms.Compose([ spatial_transforms.RandomHorizontalFlip(), spatial_transforms.Scale(args.sample_size), spatial_transforms.CenterCrop(args.sample_size), spatial_transforms.RandomRotate(), spatial_transforms.SaltImage(), spatial_transforms.Dropout(), spatial_transforms.ToTensor(args.norm_value), spatial_transforms.Normalize([0], [1]) ]) print(
normal_vec_front_d = torch.from_numpy(normal_vec_front_d) normal_vec_front_ir = torch.from_numpy(normal_vec_front_ir) normal_vec_top_d = torch.from_numpy(normal_vec_top_d) normal_vec_top_ir = torch.from_numpy(normal_vec_top_ir) if use_cuda: normal_vec_front_d = normal_vec_front_d.cuda() normal_vec_front_ir = normal_vec_front_ir.cuda() normal_vec_top_d = normal_vec_top_d.cuda() normal_vec_top_ir = normal_vec_top_ir.cuda() val_spatial_transform = spatial_transforms.Compose([ spatial_transforms.Scale(sample_size), spatial_transforms.CenterCrop(sample_size), spatial_transforms.ToTensor(255), spatial_transforms.Normalize([0], [1]), ]) print( "===========================================Loading Test Data==========================================" ) test_data_front_d = DAD_Test( root_path=root_path, subset='validation', view='front_depth', sample_duration=sample_duration, type=None, spatial_transform=val_spatial_transform, )