def feature_extract(args): os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # close the warning os.environ["CUDA_VISIBLE_DEVICES"] = args.gpus cudnn.benchmark = True # == dataset config== num_class, data_length, image_tmpl = data_config(args) train_transforms, test_transforms, _ = augmentation_config(args) train_data_loader, val_data_loader, _, _, _, _ = data_loader_init( args, data_length, image_tmpl, train_transforms, test_transforms, _) # == model config== model = model_config(args, num_class) tc = TC(args) features = single_extract(tc, val_data_loader, model) return features
def main(): os.environ["CUDA_VISIBLE_DEVICES"] = args.gpus # == dataset config== num_class, data_length, image_tmpl = data_config(args) train_transforms, test_transforms, eval_transforms = augmentation_config(args) _, _, eval_data_loader, _, _, _ = data_loader_init(args, data_length, image_tmpl, train_transforms, test_transforms, eval_transforms) model = model_config(args, num_class) output = [] total_num = len(eval_data_loader) for i, (data, label, index) in enumerate(eval_data_loader): proc_start_time = time.time() rst = eval_video(model, (i, data, label)) output.append(rst) cnt_time = time.time() - proc_start_time if i % 10 == 0: print('video {} done, total {}/{}, average {} sec/video'.format(i, i + 1, total_num, float(cnt_time) / (i + 1))) date = datetime.datetime.today().strftime('%m-%d-%H%M') # =====output: every video's num and every video's label # =====x[0]:softmax value x[1]:label if not os.path.isdir("../experiments/evaluation/{}/{}".format(args.dataset,date)): os.mkdir("../experiments/evaluation/{}/{}".format(args.dataset,date)) video_pred = [np.argmax(x[0]) for x in output] np.save("../experiments/evaluation/{}/{}/video_pred.npy".format(args.dataset,date), video_pred) video_labels = [x[1] for x in output] np.save("../experiments/evaluation/{}/{}/video_labels.npy".format(args.dataset, date), video_labels) cf = confusion_matrix(video_labels, video_pred).astype(float) np.save("../experiments/evaluation/{}/{}/confusion.npy".format(args.dataset, date), cf) cf_name = "../experiments/evaluation/{}/{}/confusion.npy".format(args.dataset, date) cls_cnt = cf.sum(axis=1) cls_hit = np.diag(cf) cls_acc = cls_hit / cls_cnt print(cls_acc) print('Accuracy {:.02f}%'.format(np.mean(cls_acc) * 100)) name_list = [x.strip().split()[0] for x in open(args.val_list)] order_dict = {e: i for i, e in enumerate(sorted(name_list))} reorder_output = [None] * len(output) reorder_label = [None] * len(output) for i in range(len(output)): idx = order_dict[name_list[i]] reorder_output[idx] = output[i] reorder_label[idx] = video_labels[i] np.savez('../experiments/evaluation/' + args.dataset + '/' + date + "/" + args.mode + 'res', scores=reorder_output, labels=reorder_label) return cf_name
def main(): # = os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # close the warning os.environ["CUDA_VISIBLE_DEVICES"] = args.gpus torch.manual_seed(1) cudnn.benchmark = True # == dataset config== num_class, data_length, image_tmpl = data_config(args) train_transforms, test_transforms, eval_transforms = augmentation_config( args) train_data_loader, val_data_loader, _, _, _, _ = data_loader_init( args, data_length, image_tmpl, train_transforms, test_transforms, eval_transforms) # == model config== model = model_config(args, num_class) pos_aug = GenPositive() # == train and eval== test(train_data_loader, model, pos_aug) return 1
def train_and_eval(args): # = global lowest_val_loss, best_prec1 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # close the warning os.environ["CUDA_VISIBLE_DEVICES"] = args.gpus torch.manual_seed(1) cudnn.benchmark = True timer = Timer() recorder = Record(args) # == dataset config== num_class, data_length, image_tmpl = data_config(args) train_transforms, test_transforms = augmentation_config(args) train_data_loader, val_data_loader = data_loader_init( args, data_length, image_tmpl, train_transforms, test_transforms) # == model config== models = [] optimizers = [] for i in range(args.mutual_num): model = model_config(args, num_class) models.append(model) recorder.record_message('a', '=' * 100) recorder.record_message('a', str(model.module)) recorder.record_message('a', '=' * 100) # == optim config== for i in range(args.mutual_num): train_criterion, val_criterion, optimizer = optim_init(args, model) optimizers.append(optimizer) # == data augmentation(self-supervised) config== tc = TC(args) # == train and eval== for epoch in range(args.start_epoch, args.epochs): timer.tic() for i in range(args.mutual_num): adjust_learning_rate(optimizers[i], args.lr, epoch, args.lr_steps) if args.eval_indict == 'acc': train_prec1, train_loss = train(args, tc, train_data_loader, models, train_criterion, optimizers, epoch, recorder) # train_prec1, train_loss = random.random() * 100, random.random() recorder.record_train(train_loss / 5.0, train_prec1 / 100.0) else: train_loss = train(args, tc, train_data_loader, models, train_criterion, optimizers, epoch, recorder) # train_prec1, train_loss = random.random() * 100, random.random() recorder.record_train(train_loss) if (epoch + 1) % args.eval_freq == 0: if args.eval_indict == 'acc': val_prec1, val_loss = validate(args, tc, val_data_loader, models, val_criterion, recorder) # val_prec1, val_loss = random.random() * 100, random.random() recorder.record_val(val_loss / 5.0, val_prec1 / 100.0) is_best = val_prec1 > best_prec1 best_prec1 = max(val_prec1, best_prec1) checkpoint = { 'epoch': epoch + 1, 'arch': "i3d", 'state_dict': model.state_dict(), 'best_prec1': best_prec1 } else: val_loss = validate(args, tc, val_data_loader, models, val_criterion, recorder) # val_loss = random.random() # val_prec1, val_loss = random.random() * 100, random.random() recorder.record_val(val_loss) is_best = val_loss < lowest_val_loss lowest_val_loss = min(val_loss, lowest_val_loss) checkpoint = { 'epoch': epoch + 1, 'arch': "i3d", 'state_dict': model.state_dict(), 'lowest_val': lowest_val_loss } recorder.save_model(checkpoint, is_best) timer.toc() left_time = timer.average_time * (args.epochs - epoch) if args.eval_indict == 'acc': message = "best_prec1 is: {} left time is : {}".format( best_prec1, timer.format(left_time)) else: message = "lowest_val_loss is: {} left time is : {}".format( lowest_val_loss, timer.format(left_time)) print(message) recorder.record_message('a', message) # return recorder.best_name return recorder.filename