def __init__(self, root_path, video_path, annotation_path, result_path, model_path, modality): print('***checkpoint***') opt = parse_opts_offline([ '--root_path', root_path, '--video_path', video_path, '--annotation_path', annotation_path, '--result_path', result_path, '--resume_path', model_path, '--dataset', 'ems', '--sample_duration', '32', '--model', 'resnext', '--model_depth', '101', '--resnet_shortcut', 'B', '--batch_size', '1', '--n_finetune_classes', '4', '--n_threads', '1', '--checkpoint', '1', '--modality', modality, '--n_val_samples', '1', '--test_subset', 'test' ]) 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) opt.std = get_std(opt.norm_value) print(opt) #%% warnings.filterwarnings('ignore') torch.manual_seed(opt.manual_seed) model, parameters = generate_model(opt) pytorch_total_params = sum(p.numel() for p in model.parameters() if p.requires_grad) print("Total number of trainable parameters: ", pytorch_total_params) self.opt = opt self.model = model self.parameters = parameters
def main(): gpu_num = "1" os.environ["CUDA_VISIBLE_DEVICES"] = gpu_num import gc gc.disable() fix_gpu_memory() global opt opt = parse_opts_offline() if opt.cmd == "train": train_LSTM(opt.training_path, opt.validation_path, opt.model_path, opt.weight_path) if opt.cmd == "test": test_LSTM(opt.testing_path, opt.oldmodel_path, opt.testing_res, opt.testing_res_CDF) if opt.cmd == "transfer": train(opt.training_path, opt.validation_path, opt.oldmodel_weight_path, opt.model_path, opt.weight_path)
def calculate_accuracy(outputs, targets, topk=(1, )): maxk = max(topk) batch_size = targets.size(0) _, pred = outputs.topk(maxk, 1, True, True) pred = pred.t() correct = pred.eq(targets.view(1, -1).expand_as(pred)) ret = [] for k in topk: correct_k = correct[:k].float().sum().item() ret.append(correct_k / batch_size) return ret opt = parse_opts_offline() 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) opt.std = get_std(opt.norm_value)