def get_3d_model(num_feature2d_slices=30): opt = parse_opts() if opt.root_path != '': opt.video_path = os.path.join(opt.root_path, opt.video_path) opt.annotation_path = os.path.join(opt.root_path, opt.annotation_path) opt.result_path = os.path.join(opt.root_path, opt.result_path) if opt.resume_path: opt.resume_path = os.path.join(opt.root_path, opt.resume_path) if opt.pretrain_path: opt.pretrain_path = os.path.join(opt.root_path, opt.pretrain_path) opt.scales = [opt.initial_scale] for i in range(1, opt.n_scales): opt.scales.append(opt.scales[-1] * opt.scale_step) opt.arch = '{}-{}'.format(opt.model, opt.model_depth) opt.mean = get_mean(opt.norm_value, dataset=opt.mean_dataset) opt.std = get_std(opt.norm_value) opt.sample_duration = num_feature2d_slices # print(opt) with open(os.path.join(opt.result_path, 'opts.json'), 'w') as opt_file: json.dump(vars(opt), opt_file) torch.manual_seed(opt.manual_seed) model, parameters = generate_model(opt) return model
def get_opt(): opt = parse_opts() if opt.root_path is not None: opt.train_data1 = opt.root_path +'/'+ opt.train_data1 opt.train_data2 = opt.root_path + '/' + opt.train_data2 opt.val_data = opt.root_path + '/' + opt.val_data opt.test_data = opt.root_path + '/' + opt.test_data # opt.result_path = opt.root_path + '/' + opt.result_path opt.mean, opt.std = get_mean_std(opt.value_scale) opt.arch = '{}-{}'.format(opt.model, opt.mission) opt.begin_epoch = 1 # opt.mean, opt.std = get_mean_std(opt.value_scale, dataset=opt.mean_dataset) opt.n_input_channels = 3 if opt.distributed: # opt.dist_rank = int(os.environ["OMPI_COMM_WORLD_RANK"]) # opt.dist_rank = 0 # 单个机器 if opt.dist_rank == 0: print(opt) with (opt.result_path / 'opts.json').open('w') as opt_file: json.dump(vars(opt), opt_file, default=json_serial) else: print(opt) with (opt.result_path / 'opts.json').open('w') as opt_file: json.dump(vars(opt), opt_file, default=json_serial) return opt
def main(): opt = parse_opts() assert opt.phase in ['pretraining', 'finetuning' ], "Only support pretraining and finetuning." if opt.gpu is not None: warnings.warn('You have chosen a specific GPU. This will completely ' 'disable data parallelism.') if opt.dist_url == "env://" and opt.world_size == -1: opt.world_size = int(os.environ["WORLD_SIZE"]) opt.distributed = opt.world_size > 1 or opt.multiprocessing_distributed ngpus_per_node = torch.cuda.device_count() if opt.multiprocessing_distributed: # Since we have ngpus_per_node processes per node, the total world_size # needs to be adjusted accordingly opt.world_size = ngpus_per_node * opt.world_size # Use torch.multiprocessing.spawn to launch distributed processes: the # main_worker process function if not opt.test: mp.spawn(main_worker, nprocs=ngpus_per_node, args=(ngpus_per_node, opt)) else: ctx = mp.get_context('spawn') test_results = ctx.Queue() mp.spawn(main_worker, nprocs=ngpus_per_node, args=(ngpus_per_node, opt, test_results)) else: # Simply call main_worker function main_worker(opt.gpu, ngpus_per_node, opt)
def get_opt(): opt = parse_opts() if opt.root_path is not None: opt.video_path = opt.root_path / opt.video_path opt.annotation_path = opt.root_path / opt.annotation_path opt.result_path = opt.root_path / opt.result_path if opt.resume_path is not None: opt.resume_path = opt.root_path / opt.resume_path if opt.pretrain_path is not None: opt.pretrain_path = opt.root_path / opt.pretrain_path if opt.pretrain_path is not None: opt.n_finetune_classes = opt.n_classes opt.n_classes = opt.n_pretrain_classes if opt.output_topk <= 0: opt.output_topk = opt.n_classes if opt.inference_batch_size == 0: opt.inference_batch_size = opt.batch_size opt.arch = '{}-{}'.format(opt.model, opt.model_depth) opt.begin_epoch = 1 opt.mean, opt.std = get_mean_std(opt.value_scale, dataset=opt.mean_dataset) print(opt) with (opt.result_path / 'opts.json').open('w') as opt_file: json.dump(vars(opt), opt_file, default=json_serial) return opt
def __init__(self, model_file, sample_duration, model_type, cuda_id=0): self.opt = parse_opts() self.opt.model = model_type self.opt.root_path = './C3D_ResNet/data' self.opt.resume_path = os.path.join(self.opt.root_path, model_file) self.opt.pretrain_path = os.path.join(self.opt.root_path, 'models/resnet-18-kinetics.pth') self.opt.cuda_id = cuda_id self.opt.dataset = 'ucf101' self.opt.n_classes = 400 self.opt.n_finetune_classes = 3 self.opt.ft_begin_index = 4 self.opt.model_depth = 18 self.opt.resnet_shortcut = 'A' self.opt.sample_duration = sample_duration self.opt.batch_size = 1 self.opt.n_threads = 1 self.opt.checkpoint = 5 self.opt.arch = '{}-{}'.format(self.opt.model, self.opt.model_depth) self.opt.mean = get_mean(self.opt.norm_value, dataset=self.opt.mean_dataset) self.opt.std = get_std(self.opt.norm_value) # print(self.opt) print('Loading C3D action-recognition model..') self.model, parameters = generate_model(self.opt) # print(self.model) if self.opt.no_mean_norm and not self.opt.std_norm: norm_method = Normalize([0, 0, 0], [1, 1, 1]) elif not self.opt.std_norm: norm_method = Normalize(self.opt.mean, [1, 1, 1]) else: norm_method = Normalize(self.opt.mean, self.opt.std) if self.opt.resume_path: print(' loading checkpoint {}'.format(self.opt.resume_path)) checkpoint = torch.load(self.opt.resume_path) # assert self.opt.arch == checkpoint['arch'] self.opt.begin_epoch = checkpoint['epoch'] self.model.load_state_dict(checkpoint['state_dict']) self.spatial_transform = Compose([ ScaleQC(int(self.opt.sample_size / self.opt.scale_in_test)), CornerCrop(self.opt.sample_size, self.opt.crop_position_in_test), ToTensor(self.opt.norm_value), norm_method ]) self.target_transform = ClassLabel() self.model.eval()
def main_worker(): opt = parse_opts() print(opt) seed = 1 random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) # CUDA for PyTorch device = torch.device(f"cuda:{opt.gpu}" if opt.use_cuda else "cpu") # tensorboard summary_writer = tensorboardX.SummaryWriter(log_dir='tf_logs') # defining model encoder_cnn, decoder_rnn = generate_model(opt, device) # get data loaders train_loader, val_loader = get_loaders(opt) # optimizer crnn_params = list(encoder_cnn.parameters()) + \ list(decoder_rnn.parameters()) optimizer = torch.optim.Adam(crnn_params, lr=opt.lr_rate, weight_decay=opt.weight_decay) # scheduler = lr_scheduler.ReduceLROnPlateau( # optimizer, 'min', patience=opt.lr_patience) criterion = nn.CrossEntropyLoss() # resume model if opt.resume_path: start_epoch = resume_model(opt, encoder_cnn, decoder_rnn, optimizer) else: start_epoch = 1 # start training for epoch in range(start_epoch, opt.n_epochs + 1): train_loss, train_acc = train_epoch( encoder_cnn, decoder_rnn, train_loader, criterion, optimizer, epoch, opt.log_interval, device) val_loss, val_acc = val_epoch( encoder_cnn, decoder_rnn, val_loader, criterion, device) # saving weights to checkpoint if (epoch) % opt.save_interval == 0: # scheduler.step(val_loss) # write summary summary_writer.add_scalar( 'losses/train_loss', train_loss, global_step=epoch) summary_writer.add_scalar( 'losses/val_loss', val_loss, global_step=epoch) summary_writer.add_scalar( 'acc/train_acc', train_acc * 100, global_step=epoch) summary_writer.add_scalar( 'acc/val_acc', val_acc * 100, global_step=epoch) state = {'epoch': epoch, 'encoder_state_dict': encoder_cnn.state_dict(), 'decoder_state_dict': decoder_rnn.state_dict(), 'optimizer_state_dict': optimizer.state_dict()} torch.save(state, os.path.join('snapshots', f'{opt.model}-Epoch-{epoch}-Loss-{val_loss}.pth')) print("Epoch {} model saved!\n".format(epoch))
def model_process(count, model): opt = parse_opts() if opt.root_path != '': opt.video_path = os.path.join(opt.root_path, opt.video_path) opt.annotation_path = os.path.join(opt.root_path, opt.annotation_path) opt.result_path = os.path.join(opt.root_path, opt.result_path) if opt.resume_path: opt.resume_path = os.path.join(opt.root_path, opt.resume_path) if opt.pretrain_path: opt.pretrain_path = os.path.join(opt.root_path, opt.pretrain_path) opt.scales = [opt.initial_scale] for i in range(1, opt.n_scales): opt.scales.append(opt.scales[-1] * opt.scale_step) #opt.arch = '{}-{}'.format(opt.model, opt.model_depth) opt.mean = get_mean(opt.norm_value, dataset=opt.mean_dataset) opt.std = get_std(opt.norm_value) #print(opt) #print(opt.result_path) with open(os.path.join(opt.result_path, 'opts.json'), 'w') as opt_file: json.dump(vars(opt), opt_file) torch.manual_seed(opt.manual_seed) #print(model) criterion = nn.CrossEntropyLoss() if not opt.no_cuda: criterion = criterion.cuda() if opt.no_mean_norm and not opt.std_norm: norm_method = Normalize([0, 0, 0], [1, 1, 1]) elif not opt.std_norm: norm_method = Normalize(opt.mean, [1, 1, 1]) else: norm_method = Normalize(opt.mean, opt.std) print('testing is run') if opt.test: spatial_transform = Compose([ Scale(int(opt.sample_size / opt.scale_in_test)), CornerCrop(opt.sample_size, opt.crop_position_in_test), ToTensor(opt.norm_value), norm_method ]) temporal_transform = LoopPadding(opt.sample_duration) target_transform = VideoID() test_data = get_test_set(opt, spatial_transform, temporal_transform, target_transform) test_loader = torch.utils.data.DataLoader(test_data, batch_size=opt.batch_size, shuffle=False, num_workers=opt.n_threads, pin_memory=True) tester.test(count, test_loader, model, opt, test_data.class_names)
def get_opt(): opt = parse_opts() opt.begin_epoch = 1 opt.n_input_channels = 3 print(opt) with (opt.result_path / 'opts.json').open('w') as opt_file: json.dump(vars(opt), opt_file, default=json_serial) return opt
def main(): # 19.5.17 add print(torch.__version__) # 19.5.17 for ubuntu # 19.6.14 주석처리 # torch.multiprocessing.set_start_method('spawn') opt = parse_opts() # 19.5.7 add # 19.5.16. move from other functions # 19.10.17 revise, device → opt.device opt.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # 19.10.8 move from train_dataset() opt.scales = [opt.initial_scale] for i in range(1, opt.n_scales): opt.scales.append(opt.scales[-1] * opt.scale_step) opt.mean = get_mean(opt.norm_value) print(opt) torch.manual_seed(opt.manual_seed) # 19.3.8. add # 19.5.16. move from train_dataset() print("cuda is available : ", torch.cuda.is_available(), flush=True) # 19.5.16 set default tensor type # 19.6.26 ubuntu에서 주석처리해서 > 에러해결 # torch.set_default_tensor_type('torch.cuda.FloatTensor') # else: # torch.set_default_tensor_type('torch.FloatTensor') assert opt.input_type in ['RGB', 'HSV'] if opt.phase == 'full': phase_list = ['train', 'test'] else: phase_list = [opt.phase] for phase in phase_list: opt.phase = phase model = build_final_model(opt) if phase == 'train': train_dataset(opt, model) else: if opt.misaeng: test_misaeng(opt, model) else: out_path = os.path.join(opt.result_dir, 'results.json') if not os.path.exists(out_path): res = test_dataset(opt, model) json.dump(res, open(out_path, 'w')) eval_res.eval(opt)
def get_opt(): opt = parse_opts() os.makedirs(opt.result_path, exist_ok=True) if opt.root_path is not None: opt.video_path = opt.root_path / opt.video_path opt.annotation_path = opt.root_path / opt.annotation_path opt.result_path = opt.root_path / opt.result_path if opt.resume_path is not None: opt.resume_path = opt.root_path / opt.resume_path if opt.pretrain_path is not None: opt.pretrain_path = opt.root_path / opt.pretrain_path if opt.pretrain_path is not None: opt.n_finetune_classes = opt.n_classes opt.n_classes = opt.n_pretrain_classes if opt.output_topk <= 0: opt.output_topk = opt.n_classes if opt.inference_batch_size == 0: opt.inference_batch_size = opt.batch_size opt.arch = '{}-{}'.format(opt.model, opt.model_depth) opt.begin_epoch = 1 opt.mean, opt.std = get_mean_std(opt.value_scale, dataset=opt.mean_dataset) opt.n_input_channels = 3 if opt.input_type == 'flow': opt.n_input_channels = 2 opt.mean = opt.mean[:2] opt.std = opt.std[:2] if opt.distributed: opt.dist_rank = int(os.environ["OMPI_COMM_WORLD_RANK"]) if opt.dist_rank == 0: print(opt) with (opt.result_path / 'opts.json').open('w') as opt_file: json.dump(vars(opt), opt_file, default=json_serial) else: print(opt) new_path = os.path.join(str(opt.result_path), 'opts.json') # import pdb; pdb.set_trace() # # with (opt.result_path / 'opts.json').open('w') as opt_file: # with (new_path).open('w') as opt_file: # json.dump(vars(opt), opt_file, default=json_serial) with open(new_path, 'w', encoding='utf-8') as f: json.dump(vars(opt), f, default=json_serial) return opt
def main(): opt = parse_opts() print(opt) seed = 0 random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) # CUDA for PyTorch use_cuda = torch.cuda.is_available() device = torch.device(f"cuda:{opt.gpu}" if use_cuda else "cpu") labels = {0: "Mask", 1: "UnMask"} transform = transforms.Compose([ #transforms.RandomCrop(32, padding=3), transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # define model model = ResidualNet("ImageNet", opt.depth, opt.num_classes, "CBAM") checkpoint = torch.load(opt.resume_path, map_location="cpu") model.load_state_dict(checkpoint['model_state_dict'], strict=False) model = nn.Sequential(*list(model.children())[:-1]) # print(model) # print(model) model = model.to(device) model.eval() X = [] labels = [] for img in os.listdir("/Volumes/Neuroplex/kdisc/mask-demo-data/"): img_path = f"/Volumes/Neuroplex/kdisc/mask-demo-data/{img}" print(img_path) img = cv2.imread(img_path) print(img.shape) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # img = torch.from_numpy(img) img = Image.fromarray(img) img = transform(img) img = torch.unsqueeze(img, dim=0) with torch.no_grad(): outputs = model(img) X.append(outputs.view(1, -1)) if ('un' in img_path): labels.append("UnMask") else: labels.append("Mask") return X, labels
def main(): opt = parse_opts() print('=' * 100) print(f'OPTUNA_TRIALS = {opt.optuna_trials}') print('=' * 100) if opt.optuna_trials: study = optuna.create_study() study.optimize(objective, n_trials=opt.optuna_trials) print(study.best_params) else: objective(None)
def main(): opts = parse_opts() print(opts) opts.arch = '{}-{}'.format(opts.model, opts.model_depth) torch.manual_seed(opts.manual_seed) opts.input_channels = 3 # initialize model print("Loading model {}.".format(opts.arch)) model, _ = generate_model(opts) resume_path = opts.resume_path1 # resuming model {} print("Resuming model {}.".format(resume_path)) checkpoint = torch.load(resume_path) assert opts.arch == checkpoint['arch'] model.load_state_dict(checkpoint['state_dict']) model.eval() model = model.cuda() # initial detection model device = select_device() cur_dir = os.path.dirname(os.path.realpath(__file__)) weights = os.path.join(cur_dir, 'detection/best.pt') detect_model = attempt_load(weights, map_location=device) if device.type != 'cpu': detect_model.half() print("Processing case validation data.") val_data = globals()['{}'.format(opts.dataset)](train=2, opt=opts, detect_model=detect_model) val_dataloader = DataLoader(val_data, batch_size=1, shuffle=True, num_workers=opts.n_workers, pin_memory=True, drop_last=False) print("Length of case validation dataloder = {}.".format( len(val_dataloader))) validation(opts, model, val_dataloader) print("Processing control validation data.") val_data_2 = globals()['{}'.format(opts.dataset)]( train=3, opt=opts, detect_model=detect_model) val_dataloader_2 = DataLoader(val_data_2, batch_size=1, shuffle=False, num_workers=opts.n_workers, pin_memory=True, drop_last=False) print("Length of control validation_2 data = ", len(val_data_2)) validation(opts, model, val_dataloader_2)
def main(): opt = parse_opts() #device = torch.device("cuda:%d" % 0) models_list = [] for i in range(len(models_name_list)): model = get_model(opt) print('Model Loading: ' + str(models_name_list[i])) model.load_state_dict((torch.load(models_name_list[i]))) model.cuda() model.eval() #model = torch.nn.DataParallel(model) models_list.append(model) data_root_path = r"./data/Refuge2-Validation" coarse_label_list = os.listdir(data_root_path) results = [] for i in tqdm(range(len(coarse_label_list))): file = coarse_label_list[i] img = cv2.imread(os.path.join(data_root_path, file)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = img.astype('float32') / 255.0 ratio = img.shape[1] / 512 ratio2 = img.shape[0] / 512 if ratio != ratio2: print('WARNING: the size is not equal.') img = cv2.resize(img, (512, 512)) out_pred_x_all = [] out_pred_y_all = [] for model in models_list: img_out = predict_image(model, img) l_pred_y, l_pred_x = hm2loc(img_out) out_pred_x = (l_pred_x) * ratio out_pred_y = (l_pred_y) * ratio2 out_pred_x_all.append(out_pred_x) out_pred_y_all.append(out_pred_y) results.append( [file, np.mean(out_pred_x_all), np.mean(out_pred_y_all)]) output_file = "./outputs/Localization_Results_base_ensemble_tta_all_origin_1130.csv" with open(output_file, "w+") as f: f.write("{},{},{}\n".format("ImageName", "Fovea_X", "Fovea_Y")) for result in results: f.write("{},{},{}\n".format(result[0], result[1], result[2]))
def confusion_matrics(labels, preds): opt = parse_opts() # import pdb;pdb.set_trace() fpr, tpr, thresholds = roc_curve(labels, preds) precision, recall, th = precision_recall_curve(labels, preds) try: tp = tpr[np.where(tpr > 0.85)[0][0]] spe = 1 - fpr[np.where(tpr > 0.85)[0][0]] prec = precision[np.where(recall >= 0.85)[0][-1]] except: import pdb pdb.set_trace() auc = roc_auc_score(labels, preds) return auc, prec, tp, spe
def get_opt(): opt = parse_opts() opt.begin_epoch = 1 opt.n_input_channels = 3 if opt.distributed: opt.dist_rank = 0 # 单个机器 if opt.dist_rank == 0: print(opt) with (opt.result_path / 'opts.json').open('w') as opt_file: json.dump(vars(opt), opt_file, default=json_serial) else: print(opt) with (opt.result_path / 'opts.json').open('w') as opt_file: json.dump(vars(opt), opt_file, default=json_serial) return opt
def get_config(): opt = parse_opts() # Training settings dataset_use = opt.dataset # which dataset to use datacfg = opt.data_cfg # path for dataset of training and validation, e.g: cfg/ucf24.data cfgfile = opt.cfg_file # path for cfg file, e.g: cfg/ucf24.cfg # assert dataset_use == 'ucf101-24' or dataset_use == 'jhmdb-21', 'invalid dataset' # loss parameters loss_options = parse_cfg(cfgfile)[1] region_loss = RegionLoss() anchors = loss_options['anchors'].split(',') region_loss.anchors = [float(i) for i in anchors] region_loss.num_classes = int(loss_options['classes']) region_loss.num_anchors = int(loss_options['num']) return opt, region_loss
def main(): opt = parse_opts() print(opt) seed = 0 random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) # CUDA for PyTorch use_cuda = torch.cuda.is_available() device = torch.device(f"cuda:{opt.gpu}" if use_cuda else "cpu") idx_to_class = {0: "Smoking", 1: "Phoning", 2: "Other"} transform = transforms.Compose([ #transforms.RandomCrop(32, padding=3), transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # define model model = ResidualNet("ImageNet", opt.depth, opt.num_classes, "CBAM") checkpoint = torch.load(opt.resume_path, map_location="cpu") model.load_state_dict(checkpoint['model_state_dict']) model = model.to(device) model.eval() img = cv2.imread(opt.img_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # img = torch.from_numpy(img) img = Image.fromarray(img) img = transform(img) img = torch.unsqueeze(img, dim=0) with torch.no_grad(): outputs = model(img) outputs = nn.Softmax(dim=1)(outputs) scores, indices = torch.max(outputs, 1) mask = scores > 0.5 preds = indices[mask] print(scores[mask].item()) print(preds.item()) print(idx_to_class[preds.item()])
def get_opt(): opt = parse_opts() if opt.root_path is not None: opt.video_path = opt.root_path / opt.video_path opt.annotation_path = opt.root_path / opt.annotation_path opt.result_path = opt.root_path / opt.result_path if opt.resume_path is not None: opt.resume_path = opt.root_path / opt.resume_path if opt.pretrain_path is not None: opt.pretrain_path = opt.root_path / opt.pretrain_path if opt.pretrain_path is not None: opt.n_finetune_classes = opt.n_classes opt.n_classes = opt.n_pretrain_classes if opt.output_topk <= 0: opt.output_topk = opt.n_classes if opt.inference_batch_size == 0: opt.inference_batch_size = opt.batch_size opt.arch = '{}-{}'.format(opt.model, opt.model_depth) opt.begin_epoch = 1 opt.mean, opt.std = get_mean_std(opt.value_scale, dataset=opt.mean_dataset) opt.n_input_channels = 3 if opt.input_type == 'flow': opt.n_input_channels = 2 opt.mean = opt.mean[:2] opt.std = opt.std[:2] if opt.distributed: opt.dist_rank = int(os.environ["OMPI_COMM_WORLD_RANK"]) if opt.dist_rank == 0: print(opt) with (opt.result_path / 'opts.json').open('w') as opt_file: json.dump(vars(opt), opt_file, default=json_serial) else: print(opt) with (opt.result_path / 'opts.json').open('w') as opt_file: json.dump(vars(opt), opt_file, default=json_serial) assert opt.lrf_mode in ['linear', 'exp'] return opt
def main(): opt = parse_opts() opt.inputsize = [int(item) for item in opt.input_list.split(',')] torch.cuda.set_device(opt.gpuid) device = torch.device("cuda:%d" % opt.gpuid) folder = 'fold_%d_%sresult' % (opt.fold, opt.model) + time.strftime( "%Y_%m_%d%H_%M_%S", time.localtime()) save_path = os.path.join(opt.result_path, folder) model = get_model(opt) model.to(device) trainlist, vallist = get_datalist(opt.fold) trainset = DATASET(trainlist) valset = DATASET_VAL(vallist) train_loader = torch.utils.data.DataLoader(trainset, batch_size=7, num_workers=8, shuffle=True) val_loader = torch.utils.data.DataLoader(valset, batch_size=2, num_workers=6, shuffle=False) optimizer = RAdam(model.parameters(), lr=3e-4, weight_decay=1e-5) best_meandice = 100 for epoch in range(50): adjust_learning_rate(optimizer, epoch, 3e-4, 5) train(model, optimizer, train_loader, epoch, 50, device) mean_dist = val(model, val_loader, epoch, device) if mean_dist < best_meandice: best_meandice = mean_dist check_mkdirs(save_path) if epoch > 1: print('save model...:' + os.path.join(save_path, '%.4f.pkl' % (best_meandice))) check_mkdirs(save_path) torch.save( model.state_dict(), os.path.join(save_path, '%.4f.pkl' % (best_meandice))) print('Best Mean Dice: %.4f' % best_meandice) os.rename(save_path, save_path + '_%.4f' % best_meandice)
def video_inout_test(): opt = parse_opts() with open(opt.test_list_path, 'r') as f: video_name_list = [line.strip('\n') for line in f.readlines()] length_of_test = 0.0 root_dir = os.path.join(opt.root_dir, opt.test_subdir) for video_name in video_name_list: video_dir = os.path.join(root_dir, video_name) cap = cv2.VideoCapture(video_dir) total_frame = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) length_of_test += total_frame print("test length frame : {}".format(length_of_test)) misaeng_list_path = 'E:/video/misaeng/misaeng_filename_list.txt' with open(misaeng_list_path, 'r') as f: video_name_list = [line.strip('\n') for line in f.readlines()] length_of_test = 0.0 root_dir = 'E:/video/misaeng' for video_name in video_name_list: video_dir = os.path.join(root_dir, video_name) cap = cv2.VideoCapture(video_dir) total_frame = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) length_of_test += total_frame print("misaeng length frame : {}".format(length_of_test)) video_path = os.path.join(root_dir, video_name_list[0]) print(video_path) video_cap = cv2.VideoCapture(video_path) video = list() spatial_transform = get_test_spatial_transform(opt) for i in range(8): status, frame = video_cap.read() if not status: break else: hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) frame = Image.fromarray(hsv, 'HSV') frame = spatial_transform(frame) video.append(frame) video = torch.stack(video, 0) print(torch.Tensor.size(video))
def get_opt(): opt = parse_opts() if opt.root_path is not None: # 根目录 opt.video_path = opt.root_path / opt.video_path # 视频抽帧图像的位置 opt.annotation_path = opt.root_path / opt.annotation_path # 索引位置 opt.result_path = opt.root_path / opt.result_path # 模型保存路径 if opt.resume_path is not None: opt.resume_path = opt.root_path / opt.resume_path if opt.pretrain_path is not None: opt.pretrain_path = opt.root_path / opt.pretrain_path # 加载预训练模型的位置 if opt.pretrain_path is not None: opt.n_finetune_classes = opt.n_classes # 预训练更改的最后一层全连接层的神经元数,即新的分类数 opt.n_classes = opt.n_pretrain_classes if opt.output_topk <= 0: opt.output_topk = opt.n_classes # 新的分类数 if opt.inference_batch_size == 0: opt.inference_batch_size = opt.batch_size opt.arch = '{}-{}'.format(opt.model, opt.model_depth) # 模型以及模型层数 opt.begin_epoch = 1 opt.mean, opt.std = get_mean_std(opt.value_scale, dataset=opt.mean_dataset) opt.n_input_channels = 3 if opt.input_type == 'flow': opt.n_input_channels = 2 opt.mean = opt.mean[:2] opt.std = opt.std[:2] if opt.distributed: # 分布式训练 opt.dist_rank = int(os.environ["OMPI_COMM_WORLD_RANK"]) if opt.dist_rank == 0: print(opt) with (opt.result_path / 'opts.json').open('w') as opt_file: json.dump(vars(opt), opt_file, default=json_serial) else: print(opt) with (opt.result_path / 'opts.json').open('w') as opt_file: json.dump(vars(opt), opt_file, default=json_serial) return opt
def main(): print('') print("training EM model") os.environ["CUDA_VISIBLE_DEVICES"] = "0, 1" opt = parse_opts() torch.manual_seed(opt.manual_seed) model, parameters = generate_model(opt) optimizer = torch.optim.Adam(parameters, lr=opt.learning_rate) if not os.path.exists(opt.model_weight): os.mkdir(opt.model_weight) trainSet = weaklyDataset(opt.train_path) train_loader = DataLoader(trainSet, batch_size=1, shuffle=True, num_workers=0) E_step = False def adjust_learning_rate(optimizer): for param_group in optimizer.param_groups: param_group['lr'] = opt.learning_rate*4 for epoch in range(1, 66): if epoch <= 10: stage = 1 E_step = False elif epoch > 10 and epoch <= 20: stage = 1 E_step = True elif epoch > 20 and epoch <= 30: stage = 2 E_step = False adjust_learning_rate(optimizer) else: stage = 2 E_step = not E_step train_epoch(epoch, train_loader, model, optimizer, opt, E_step, stage) torch.save(model.state_dict(), opt.model_weight+"/{}.pt".format(epoch)) return
def main(): # Parse arguments (options) opts = parse_opts() # Read labels # label_file = opts.datasets_dir + '/' + opts.datasets + '/splits/pkummd.json' label_file = opts.datasets_dir + '/' + opts.datasets + '/splits/pkummd_cross_subject_background.json' class_file = opts.datasets_dir + '/' + opts.datasets + '/splits/classInd.txt' labels = video_utils.VideoLabels(label_file, class_file, opts.datasets) txt_labels = read_txt_labels(class_file) assert _FUSION_METHOD == "exponential_smoothing", "Fusion method NotImplemented" use_siminet = (opts.sim_method == "siminet" or opts.corr_at_transition or opts.corr_per_window) if use_siminet: assert opts.siminet_path != '', "Siminet model path cannot be empty" siminet.load_siminet_model(opts.n_classes, opts.siminet_path) # _VIDEOS_LST = labels.labels_key for video in _VIDEOS_LST: ret = get_predicted_actions(opts=opts, video=video, labels=labels) if ret is not None: local_predicted_actions, remote_predicted_actions, fusion_predicted_actions, true_actions = ret predictions = list( zip(local_predicted_actions, remote_predicted_actions, fusion_predicted_actions)) assert len(predictions) == len(true_actions) video_file = opts.datasets_dir + "/" + opts.datasets + f"/videos/{video}.{_VIDEO_EXTENSION}" window_size = opts.window_size try: visualizer = Visualizer(video_file, window_size, predictions, true_actions, txt_labels, target_fps=_TARGET_FPS, use_qt=_USE_QT) visualizer.display() except Visualizer.VideoError as e: print(f"Cannot open video file: {e.filename}")
def main_worker(): seed = 1 random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) opt = parse_opts() dataset_train = VRDDataset(cfg.DATASET_DIR, 'train') dataloader = DataLoader( dataset_train, num_workers=cfg.WORKERS, collate_fn=collater, batch_size=cfg.BATCH_SIZE) faster_rcnn = FasterRCNN().to(cfg.DEVICE) optimizer = optim.Adam(faster_rcnn.parameters(), lr=cfg.LR_RATE) # resume model if opt.weight_path: resume_model(opt, faster_rcnn, optimizer) for epoch in range(1, cfg.N_EPOCHS+1): train_epoch(faster_rcnn, dataloader, optimizer, epoch)
def get_model(): # os.environ["CUDA_VISIBLE_DEVICES"] = "1" opt = parse_opts() model = resnext.resnet101( num_classes=opt.n_finetune_classes, shortcut_type=opt.resnet_shortcut, cardinality=opt.resnext_cardinality, sample_size=opt.sample_size, sample_duration=opt.sample_duration) model.cuda() # print(model.cuda()) model = nn.DataParallel(model, device_ids=None) model.load_state_dict(torch.load( './trained_models/best.pth10.tar')['state_dict']) model.cpu() # model.cuda() model.eval() rgb_mean = [0.485, 0.456, 0.406] rgb_std = [0.229, 0.224, 0.225] opt.scales = [1] return model
def extract_features_from_vid(video_path): """ This function extracts 3D CNN features for a single query video. """ opt = parse_opts() opt.input = video_path opt.model = './3d-cnn/resnet-34-kinetics.pth' opt.mode = 'feature' opt.mean = get_mean() opt.arch = '{}-{}'.format(opt.model_name, opt.model_depth) opt.sample_size = 112 opt.sample_duration = 8 opt.n_classes = 400 model = generate_model(opt) # print('loading model {}'.format(opt.model)) model_data = torch.load(opt.model) assert opt.arch == model_data['arch'] model.load_state_dict(model_data['state_dict']) model.eval() if opt.verbose: print(model) outputs = [] class_names = [] with open('./3d-cnn/class_names_list') as f: for row in f: class_names.append(row[:-1]) if os.path.exists(opt.input): subprocess.call('mkdir tmp', shell=True) subprocess.call('ffmpeg -loglevel panic -i {} tmp/image_%05d.jpg'.format(video_path),shell=True) result = classify_video('tmp', video_path, class_names, model, opt) outputs.append(result) subprocess.call('rm -rf tmp', shell=True) else: print('{} does not exist'.format(video_path)) return np.array(outputs[0]['clips'][0]['features']).astype('float32')
def check_auc(g_model_path, d_model_path, i): opt_auc = parse_opts() opt_auc.batch_shuffle = False opt_auc.drop_last = False opt_auc.data_path = './data/test/' dataloader = load_data(opt_auc) model = OGNet(opt_auc, dataloader) model.cuda() d_results, labels = model.test_patches(g_model_path, d_model_path, i) d_results = np.concatenate(d_results) labels = np.concatenate(labels) fpr1, tpr1, thresholds1 = metrics.roc_curve( labels, d_results, pos_label=1) # (y, score, positive_label) fnr1 = 1 - tpr1 eer_threshold1 = thresholds1[np.nanargmin(np.absolute((fnr1 - fpr1)))] EER1 = fpr1[np.nanargmin(np.absolute((fnr1 - fpr1)))] d_f1 = np.copy(d_results) d_f1[d_f1 >= eer_threshold1] = 1 d_f1[d_f1 < eer_threshold1] = 0 f1_score = metrics.f1_score(labels, d_f1, pos_label=0) print("AUC: {0}, EER: {1}, EER_thr: {2}, F1_score: {3}".format( metrics.auc(fpr1, tpr1), EER1, eer_threshold1, f1_score))
def main_test(): print('') print('start testing') os.environ["CUDA_VISIBLE_DEVICES"] = "0, 1" opt = parse_opts() model, parameters = generate_model(opt) model_unt, parameters_unt = generate_model_unt(opt) model.load_state_dict(torch.load(opt.model_weight + "/65.pt")) model.eval() model_unt.load_state_dict(torch.load(opt.model_unt_weight + "/45.pt")) model_unt.eval() testSet = weaklyDataset(opt.test_path) test_loader = DataLoader(testSet, batch_size=1, shuffle=False, num_workers=0) test(test_loader, model, model_unt, opt) return
def main_unt(): print('') print("training UntrimmedNet model") os.environ["CUDA_VISIBLE_DEVICES"] = "0, 1" opt = parse_opts() model_unt, parameters_unt = generate_model_unt(opt) optimizer_unt = torch.optim.Adam(parameters_unt, lr=opt.learning_rate) if not os.path.exists(opt.model_unt_weight): os.mkdir(opt.model_unt_weight) trainSet = weaklyDataset(opt.train_path) train_loader = DataLoader(trainSet, batch_size=1, shuffle=True, num_workers=0) for epoch in range(1, 46): r = train_unt_epoch(epoch, train_loader, model_unt, optimizer_unt, opt) torch.save(model_unt.state_dict(), opt.model_unt_weight + "/{}.pt".format(epoch))
from model import generate_model from mean import get_mean, get_std from spatial_transforms import ( Compose, Normalize, Scale, CenterCrop, CornerCrop, MultiScaleCornerCrop, MultiScaleRandomCrop, RandomHorizontalFlip, ToTensor) from temporal_transforms import LoopPadding, TemporalRandomCrop from target_transforms import ClassLabel, VideoID from target_transforms import Compose as TargetCompose from dataset import get_training_set, get_validation_set, get_test_set from utils import Logger from train import train_epoch from validation import val_epoch import test if __name__ == '__main__': opt = parse_opts() if opt.root_path != '': opt.video_path = os.path.join(opt.root_path, opt.video_path) opt.annotation_path = os.path.join(opt.root_path, opt.annotation_path) opt.result_path = os.path.join(opt.root_path, opt.result_path) if opt.resume_path: opt.resume_path = os.path.join(opt.root_path, opt.resume_path) if opt.pretrain_path: opt.pretrain_path = os.path.join(opt.root_path, opt.pretrain_path) opt.scales = [opt.initial_scale] for i in range(1, opt.n_scales): opt.scales.append(opt.scales[-1] * opt.scale_step) opt.arch = '{}-{}'.format(opt.model, opt.model_depth) opt.mean = get_mean(opt.norm_value, dataset=opt.mean_dataset) opt.std = get_std(opt.norm_value) print(opt)