def test(args): # parse config config = parse_config(args.config) test_config = merge_configs(config, 'test', vars(args)) print_configs(test_config, 'Test') place = fluid.CUDAPlace(0) with fluid.dygraph.guard(place): video_model = TSM_ResNet("TSM", test_config) model_dict, _ = fluid.load_dygraph(args.weights) video_model.set_dict(model_dict) test_reader = KineticsReader(mode='test', cfg=test_config) test_reader = test_reader.create_reader() video_model.eval() total_loss = 0.0 total_acc1 = 0.0 total_acc5 = 0.0 total_sample = 0 for batch_id, data in enumerate(test_reader()): x_data = np.array([item[0] for item in data]) y_data = np.array([item[1] for item in data]).reshape([-1, 1]) imgs = to_variable(x_data) labels = to_variable(y_data) labels.stop_gradient = True outputs = video_model(imgs) loss = fluid.layers.cross_entropy(input=outputs, label=labels, ignore_index=-1) avg_loss = fluid.layers.mean(loss) acc_top1 = fluid.layers.accuracy(input=outputs, label=labels, k=1) acc_top5 = fluid.layers.accuracy(input=outputs, label=labels, k=5) total_loss += avg_loss.numpy() total_acc1 += acc_top1.numpy() total_acc5 += acc_top5.numpy() total_sample += 1 print('TEST iter {}, loss = {}, acc1 {}, acc5 {}'.format( batch_id, avg_loss.numpy(), acc_top1.numpy(), acc_top5.numpy())) print('Finish loss {}, acc1 {}, acc5 {}'.format( total_loss / total_sample, total_acc1 / total_sample, total_acc5 / total_sample))
def validate_model(): # parse config args = parse_args() config = parse_config(args.config) val_config = merge_configs(config, 'test', vars(args)) val_reader = KineticsReader(args.model_name.upper(), 'test', val_config).create_reader() val_model = ECO.GoogLeNet(val_config['MODEL']['num_classes'], val_config['MODEL']['seg_num'], val_config['MODEL']['seglen'], 'RGB') model, _ = fluid.dygraph.load_dygraph(args.save_dir + '/ucf_model') val_model.load_dict(model) val_model.eval() acc_list = [] for batch_id, data in enumerate(val_reader()): dy_x_data = np.array([x[0] for x in data]).astype('float32') y_data = np.array([[x[1]] for x in data]).astype('int64') img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True out, acc = val_model(img, label) if out is not None: acc_list.append(acc.numpy()[0]) val_model.train() return np.mean(acc_list)
def train(args): # parse config place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace() with fluid.dygraph.guard(place): config = parse_config(args.config) train_config = merge_configs(config, 'train', vars(args)) print_configs(train_config, 'Train') train_model = TSN1.TSNResNet('TSN', train_config['MODEL']['num_layers'], train_config['MODEL']['num_classes'], train_config['MODEL']['seg_num'], 0.00002) opt = fluid.optimizer.Momentum(0.001, 0.9, parameter_list=train_model.parameters()) if args.pretrain: # 加载上一次训练的模型,继续训练 model, _ = fluid.dygraph.load_dygraph(args.save_dir + '/tsn_model') train_model.load_dict(model) # build model if not os.path.exists(args.save_dir): os.makedirs(args.save_dir) # get reader train_config.TRAIN.batch_size = train_config.TRAIN.batch_size train_reader = KineticsReader(args.model_name.upper(), 'train', train_config).create_reader() epochs = args.epoch or train_model.epoch_num() for i in range(epochs): for batch_id, data in enumerate(train_reader()): dy_x_data = np.array([x[0] for x in data]).astype('float32') y_data = np.array([[x[1]] for x in data]).astype('int64') img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True out, acc = train_model(img, label) loss = fluid.layers.cross_entropy(out, label) avg_loss = fluid.layers.mean(loss) avg_loss.backward() opt.minimize(avg_loss) train_model.clear_gradients() if batch_id % 1 == 0: logger.info("Loss at epoch {} step {}: {}, acc: {}".format( i, batch_id, avg_loss.numpy(), acc.numpy())) print("Loss at epoch {} step {}: {}, acc: {}".format( i, batch_id, avg_loss.numpy(), acc.numpy())) fluid.dygraph.save_dygraph(train_model.state_dict(), args.save_dir + '/tsn_model') logger.info("Final loss: {}".format(avg_loss.numpy())) print("Final loss: {}".format(avg_loss.numpy()))
def eval2(weights, batch_size): # parse config use_gpu = True model_name = 'tsn' config = 'configs/tsn.txt' use_gpu = use_gpu weights = weights batch_size = batch_size log_interval = 1 infer_topk = 1 save_dir = './output' config = parse_config(config) val_config = config #merge_configs(config, 'valid', vars(args)) print_configs(val_config, "Valid") with fluid.dygraph.guard(): #val_model = TSN18.TSNResNet('TSN18', val_config['MODEL']['num_layers'], #val_config['MODEL']['num_classes'], #val_config['MODEL']['seg_num'], 0.00002) val_model = baseline_2d_resnets_pp.ResNet50Flow( 'ResNet50Flow', val_config['MODEL']['num_layers'], val_config['MODEL']['num_classes'], val_config['MODEL']['seg_num']) label_dic = np.load('label_dir.npy', allow_pickle=True).item() label_dic = {v: k for k, v in label_dic.items()} # get infer reader val_reader = KineticsReader(model_name.upper(), 'valid', val_config).create_reader() # if no weight files specified, exit() if weights: weights = weights else: print("model path must be specified") exit() para_state_dict, _ = fluid.load_dygraph(weights) val_model.load_dict(para_state_dict) val_model.eval() acc_list = [] for batch_id, data in enumerate(val_reader()): dy_x_data = np.array([x[0] for x in data]).astype('float32') y_data = np.array([[x[1]] for x in data]).astype('int64') img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True out, acc = val_model(img, label) acc_list.append(acc.numpy()[0]) testacc = np.mean(acc_list) print("验证集准确率为:{}".format(np.mean(acc_list))) return testacc
def main(): args = parser.parse_args() if args.dataset == 'ucf101': args.num_class = 101 elif args.dataset == 'hmdb51': args.num_class = 51 elif args.dataset == 'kinetics': args.num_class = 400 else: raise ValueError('Unknown dataset ' + args.dataset) place = fluid.CUDAPlace(0) with fluid.dygraph.guard(place): model = TSN(args.num_class, args.num_segments, args.modality, args.arch, dropout=0) args.short_size = model.scale_size args.target_size = model.crop_size args.input_mean = model.input_mean args.input_std = model.input_std * 3 state_dict = fluid.dygraph.load_dygraph(args.load_path)[0] model.set_dict(state_dict) test_reader = KineticsReader('test', args, args.test_list).create_reader() log = open(args.log_path, 'w') model.eval() avg_acc = AverageMeter() for batch_id, data in enumerate(test_reader()): dy_x_data = np.array([x[0] for x in data]).astype('float32') y_data = np.array([[x[1]] for x in data]).astype('int64') img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True out, acc = model(img, label) avg_acc.update(acc.numpy()[0], label.shape[0]) if (batch_id + 1) % args.print_freq == 0: output = 'Test batch_id:{} | acc {} | avg acc:{}'.format( batch_id + 1, acc.numpy()[0], avg_acc.avg) print(output) log.write(output + '\n') log.flush() output = 'Test Avg acc:{}'.format(avg_acc.avg) print(output) log.write(output + '\n') log.flush() log.close()
def eval(args): # parse config config = parse_config(args.config) val_config = merge_configs(config, 'valid', vars(args)) train_config = merge_configs(config, 'train', vars(args)) print_configs(val_config, "Valid") place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace() with fluid.dygraph.guard(place): val_model = ECO.ECO(num_classes=train_config['MODEL']['num_classes'], num_segments=train_config['MODEL']['seg_num']) label_dic = np.load('label_dir.npy', allow_pickle=True).item() label_dic = {v: k for k, v in label_dic.items()} # get infer reader # val_reader = KineticsReader(args.model_name.upper(), 'valid', val_config).create_reader() val_reader = KineticsReader('ECO', 'valid', val_config).create_reader() # if no weight files specified, exit() if args.weights: weights = args.weights else: print("model path must be specified") exit() para_state_dict, _ = fluid.load_dygraph(weights) val_model.load_dict(para_state_dict) val_model.eval() acc_list = [] false_class = [] for batch_id, data in enumerate(val_reader()): dy_x_data = np.array([x[0] for x in data]).astype('float32') y_data = np.array([[x[1]] for x in data]).astype('int64') img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True out, acc = val_model(img, label) if acc.numpy()[0] != 1: false_class.append(label.numpy()[0][0]) acc_list.append(acc.numpy()[0]) print(batch_id, 'acc:', np.mean(acc_list)) if len(false_class)==0: continue print(np.sort(np.array(false_class))) bin = np.bincount(np.array(false_class)) most_false = np.argmax(bin) print('false class:', bin) print('most false class num:', most_false) print("validate set acc:{}".format(np.mean(acc_list)))
def val(epoch, model, cfg, args): reader = KineticsReader(mode="valid", cfg=cfg) reader = reader.create_reader() total_loss = 0.0 total_acc1 = 0.0 total_acc5 = 0.0 total_sample = 0 for batch_id, data in enumerate(reader()): x_data = np.array([item[0] for item in data]) y_data = np.array([item[1] for item in data]).reshape([-1, 1]) imgs = to_variable(x_data) labels = to_variable(y_data) labels.stop_gradient = True outputs = model(imgs) loss = fluid.layers.cross_entropy(input=outputs, label=labels, ignore_index=-1) avg_loss = fluid.layers.mean(loss) acc_top1 = fluid.layers.accuracy(input=outputs, label=labels, k=1) acc_top5 = fluid.layers.accuracy(input=outputs, label=labels, k=5) total_loss += avg_loss.numpy()[0] total_acc1 += acc_top1.numpy()[0] total_acc5 += acc_top5.numpy()[0] total_sample += 1 print('TEST Epoch {}, iter {}, loss = {}, acc1 {}, acc5 {}'.format( epoch, batch_id, avg_loss.numpy()[0], acc_top1.numpy()[0], acc_top5.numpy()[0])) print('Finish loss {} , acc1 {} , acc5 {}'.format( total_loss / total_sample, total_acc1 / total_sample, total_acc5 / total_sample))
def eval(args): # parse config config = parse_config(args.config) val_config = merge_configs(config, 'valid', vars(args)) print_configs(val_config, "Valid") with fluid.dygraph.guard(): # val_model = TSN1.TSNResNet('TSN', val_config['MODEL']['num_layers'], val_model = TSNResNet('TSN', val_config['MODEL']['num_layers'], val_config['MODEL']['num_classes'], seg_num=val_config['MODEL'] ['seg_num']) # 这行加了个seg_num = 这个参考infer.py文件中的修改 label_dic = np.load('label_dir.npy', allow_pickle=True).item() label_dic = {v: k for k, v in label_dic.items()} # get infer reader val_reader = KineticsReader(args.model_name.upper(), 'valid', val_config).create_reader() # if no weight files specified, exit() if args.weights: weights = args.weights else: print("model path must be specified") exit() para_state_dict, _ = fluid.load_dygraph(weights) val_model.load_dict(para_state_dict) val_model.eval() acc_list = [] for batch_id, data in enumerate(val_reader()): dy_x_data = np.array([x[0] for x in data]).astype('float32') y_data = np.array([[x[1]] for x in data]).astype('int64') img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True out, acc = val_model(img, label) acc_list.append(acc.numpy()[0]) print("验证集准确率为:{}".format(np.mean(acc_list)))
def infer(args): # parse config config = parse_config(args.config) infer_config = merge_configs(config, 'infer', vars(args)) print_configs(infer_config, "Infer") with fluid.dygraph.guard(): # infer_model = TSN1.TSNResNet('TSN', infer_config['MODEL']['num_layers'], # infer_config['MODEL']['num_classes'], # infer_config['MODEL']['seg_num'], 0.00002) # 上面注释是原代码,下面是修改后的 infer_model = TSNResNet('TSN', infer_config['MODEL']['num_layers'], infer_config['MODEL']['num_classes'], seg_num=infer_config['MODEL']['seg_num']) label_dic = np.load('label_dir.npy', allow_pickle=True).item() label_dic = {v: k for k, v in label_dic.items()} # get infer reader infer_reader = KineticsReader(args.model_name.upper(), 'infer', infer_config).create_reader() # if no weight files specified, exit() if args.weights: weights = args.weights else: print("model path must be specified") exit() para_state_dict, _ = fluid.load_dygraph(weights) infer_model.load_dict(para_state_dict) infer_model.eval() for batch_id, data in enumerate(infer_reader()): dy_x_data = np.array([x[0] for x in data]).astype('float32') y_data = [x[1] for x in data] img = fluid.dygraph.to_variable(dy_x_data) out = infer_model(img).numpy()[0] label_id = np.where(out == np.max(out)) print("实际标签{}, 预测结果{}".format(y_data, label_dic[label_id[0][0]]))
def test(args): config = parse_config(args.config) test_config = merge_configs(config, 'test', vars(args)) # print_configs(test_config, "test") with fluid.dygraph.guard(): test_model = ECO.GoogLeNet(test_config['MODEL']['num_classes'], test_config['MODEL']['seg_num'], test_config['MODEL']['seglen'], 'RGB') # get test reader test_reader = KineticsReader(args.model_name.upper(), 'test', test_config).create_reader() # if no weight files specified, exit() if args.weights: weights = args.weights else: print("model path must be specified") exit() para_state_dict, _ = fluid.load_dygraph(weights) test_model.load_dict(para_state_dict) test_model.eval() acc_list = [] for batch_id, data in enumerate(test_reader()): dy_x_data = np.array([x[0] for x in data]).astype('float32') y_data = np.array([[x[1]] for x in data]).astype('int64') img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True out, acc = test_model(img, label) acc_list.append(acc.numpy()[0]) print("The accuracy for test dataset is:{}".format(np.mean(acc_list)))
def train(args): all_train_rewards = [] all_test_rewards = [] prev_result = 0 # parse config place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace() with fluid.dygraph.guard(place): config = parse_config(args.config) train_config = merge_configs(config, 'train', vars(args)) print_configs(train_config, 'Train') train_model = ECO.GoogLeNet(train_config['MODEL']['num_classes'], train_config['MODEL']['seg_num'], train_config['MODEL']['seglen'], 'RGB') opt = fluid.optimizer.Momentum( 0.001, 0.9, parameter_list=train_model.parameters(), use_nesterov=True, regularization=fluid.regularizer.L2Decay( regularization_coeff=0.0005)) if args.pretrain: model, _ = fluid.dygraph.load_dygraph('trained_model/best_model') train_model.load_dict(model) # build model if not os.path.exists(args.save_dir): os.makedirs(args.save_dir) # get reader train_reader = KineticsReader(args.model_name.upper(), 'train', train_config).create_reader() epochs = args.epoch or train_model.epoch_num() train_model.train() for i in range(epochs): for batch_id, data in enumerate(train_reader()): dy_x_data = np.array([x[0] for x in data]).astype('float32') y_data = np.array([[x[1]] for x in data]).astype('int64') img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True out, acc = train_model(img, label) if out is not None: loss = fluid.layers.cross_entropy(out, label) avg_loss = fluid.layers.mean(loss) avg_loss.backward() opt.minimize(avg_loss) train_model.clear_gradients() if batch_id % 200 == 0: print("Loss at epoch {} step {}: {}, acc: {}".format( i, batch_id, avg_loss.numpy(), acc.numpy())) fluid.dygraph.save_dygraph( train_model.state_dict(), args.save_dir + '/ucf_model') result = validate_model() all_test_rewards.append(result) if result > prev_result: prev_result = result print('The best result is ' + str(result)) fluid.save_dygraph(train_model.state_dict(), 'trained_model/best_model') np.savez('result_data/ucf_data.npz', all_train_rewards=all_train_rewards, all_test_rewards=all_test_rewards) all_train_rewards.append(acc.numpy()) logger.info("Final loss: {}".format(avg_loss.numpy())) print("Final loss: {}".format(avg_loss.numpy())) np.savez('result_data/ucf_data.npz', all_train_rewards=all_train_rewards, all_test_rewards=all_test_rewards)
def train(args): # parse config place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace() with fluid.dygraph.guard(place): config = parse_config(args.config) train_config = merge_configs(config, 'train', vars(args)) val_config = merge_configs(config, 'test', vars(args)) if not os.path.exists(args.save_dir): os.mkdir(args.save_dir) # 根据自己定义的网络,声明train_model train_model = ResNet3D.generate_model(50) if args.resume == True: # 加载上一次训练的模型,继续训练 model, _ = fluid.dygraph.load_dygraph(args.save_dir + '/tsn_model') train_model.load_dict(model) print('Resume from ' + args.save_dir + '/tsn_model') elif args.pretrain: pretrain_weights = fluid.io.load_program_state(args.pretrain) inner_state_dict = train_model.state_dict() print('Resume from' + args.pretrain) for name, para in inner_state_dict.items(): if ((name in pretrain_weights) and (not ('fc' in para.name))): para.set_value(pretrain_weights[name]) else: print('del ' + para.name) opt = fluid.optimizer.Momentum(train_config.TRAIN.learning_rate, train_config.TRAIN.learning_rate_decay, parameter_list=train_model.parameters()) # build model if not os.path.exists(args.save_dir): os.makedirs(args.save_dir) # get reader train_config.TRAIN.batch_size = train_config.TRAIN.batch_size train_reader = KineticsReader(args.model_name.upper(), 'train', train_config).create_reader() val_reader = KineticsReader(args.model_name.upper(), 'valid', val_config).create_reader() epochs = args.epoch or train_model.epoch_num() for i in range(epochs): for batch_id, data in enumerate(train_reader( )): # data (list) (batch)[seg_num,3 * seglen,size,size] dy_x_data = np.array([x[0] for x in data]).astype( 'float32') # [batch, seg_num, 3 * seglen, size, size] y_data = np.array([[x[1]] for x in data ]).astype('int64') # [batch, 1] img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True out, acc = train_model(img, label) loss = fluid.layers.cross_entropy(out, label) avg_loss = fluid.layers.mean(loss) avg_loss.backward() opt.minimize(avg_loss) train_model.clear_gradients() logger.info("Loss at epoch {} step {}: {}, acc: {}".format( i, batch_id, avg_loss.numpy(), acc.numpy())) print("Loss at epoch {} step {}: {}, acc: {}".format( i, batch_id, avg_loss.numpy(), acc.numpy())) acc_list = [] for batch_id, data in enumerate(val_reader()): dy_x_data = np.array([x[0] for x in data]).astype('float32') y_data = np.array([[x[1]] for x in data]).astype('int64') img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True out, acc = train_model(img, label) acc_list.append(acc.numpy()[0]) logger.info("Val at epoch {}: acc: {}".format( i, np.mean(acc_list))) print("Val at epoch {}: acc: {}".format(i, np.mean(acc_list)) + '\n') if i % 10 == 0: fluid.dygraph.save_dygraph( train_model.state_dict(), args.save_dir + '/tsn_model_' + str(i)) fluid.dygraph.save_dygraph(train_model.state_dict(), args.save_dir + '/tsn_model') logger.info("Final loss: {}".format(avg_loss.numpy())) print("Final loss: {}".format(avg_loss.numpy()))
def main(): place = fluid.CUDAPlace(0) with fluid.dygraph.guard(place): global args args = parser.parse_args() if args.dataset == 'ucf101': args.num_class = 101 elif args.dataset == 'hmdb51': args.num_class = 51 elif args.dataset == 'kinetics': args.num_class = 400 else: raise ValueError('Unknown dataset '+args.dataset) model = TSN(args.num_class, args.num_segments, args.modality, args.arch, dropout=args.dropout) args.short_size = model.scale_size args.target_size = model.crop_size args.input_mean = model.input_mean args.input_std = model.input_std * 3 if args.pretrained_parts == 'finetune': print('***Finetune model with {}***'.format(args.pretrained_model)) state_dict = fluid.dygraph.load_dygraph(args.pretrained_model)[0] model_dict = model.state_dict() print('extra keys: {}'.format(set(list(state_dict.keys())) - set(list(model_dict.keys())))) print('missing keys: {}'.format(set(list(model_dict.keys())) - set(list(state_dict.keys())))) for k, v in state_dict.items(): if 'fc' not in k: model_dict.update({k:v}) model.set_dict(model_dict) optimizer = fluid.optimizer.Momentum(args.lr, args.momentum, model.parameters(), regularization=fluid.regularizer.L2Decay(args.weight_decay), grad_clip=fluid.clip.GradientClipByGlobalNorm(args.clip_gradient)) train_reader = KineticsReader('train', args, args.train_list).create_reader() val_reader = KineticsReader('val', args, args.val_list).create_reader() saturate_cnt = 0 best_prec1 = 0 log = open(os.path.join(args.log_path, args.save_name+'_train.csv'), 'w') for epoch in range(args.epochs): if saturate_cnt == args.num_saturate: print('learning rate decay by 0.1.') log.write('learning rate decay by 0.1. \n') log.flush() adjust_learing_rate(optimizer) saturate_cnt = 0 train(train_reader, model, optimizer, epoch, log) if (epoch + 1) % args.eval_freq == 0 or epoch == args.epochs - 1: prec1 = validate(val_reader, model, epoch, log) is_best = prec1 > best_prec1 if is_best: saturate_cnt = 0 else: saturate_cnt = saturate_cnt + 1 output = "- Validation Prec@1 saturates for {} epochs. Best acc{}".format(saturate_cnt, best_prec1) print(output) log.write(output + '\n') log.flush() best_prec1 = max(prec1, best_prec1) if is_best: fluid.dygraph.save_dygraph(model.state_dict(), os.path.join(args.save_dir, args.save_name)) log.close()
def train(args, distributed): #===================== GPU CONF =====================# if distributed: # if run on parallel mode place = fluid.CUDAPlace(fluid.dygraph.parallel.Env().dev_id) else: # if run on single GPU mode, and select gpu number. args.use_gpu = True place = fluid.CUDAPlace(args.gpu_num) if args.use_gpu else fluid.CPUPlace() # ===================== Dygraph Mode =====================# with fluid.dygraph.guard(place): # leverage from TSN training script config = parse_config(args.config) train_config = merge_configs(config, 'train', vars(args)) val_config = merge_configs(config, 'valid', vars(args)) print_configs(train_config, 'Train') # ===================== Init ECO =====================# train_model = ECO.ECO(num_classes=train_config['MODEL']['num_classes'], num_segments=train_config['MODEL']['seg_num']) if distributed: strategy = fluid.dygraph.parallel.prepare_context() train_model = fluid.dygraph.parallel.DataParallel(train_model, strategy) # trick 1: use clip gradient method to avoid gradient explosion if args.gd is not None: clip = fluid.clip.GradientClipByGlobalNorm(clip_norm=args.gd) print('clip:', clip) # ===================== Init Optimizer =====================# # optimizer config: use momentum, nesterov, weight decay, lr decay learning_rate = 0.001 opt = fluid.optimizer.Momentum(learning_rate, 0.9, parameter_list=train_model.parameters(), use_nesterov=True, regularization=fluid.regularizer.L2Decay(regularization_coeff=5e-4), grad_clip=clip) # trick 2: Freezing BatchNorm2D except the first one. # trick 3: make all weight layer lr mult as 1, bias lr mult as 2. get_optim_policies(opt) print('get_optim_policies:--batch_norm_0.w_0', opt._parameter_list[2].optimize_attr,opt._parameter_list[2].stop_gradient) print('get_optim_policies:--batch_norm_0.b_0', opt._parameter_list[3].optimize_attr,opt._parameter_list[2].stop_gradient) # ===================== Use Pretrained Model =====================# # use pretrained model: ECO_Full_rgb_model_Kinetics.pth 2.tar(download from MZO git) # then transform it from torch to paddle weight except fc layer. if args.pretrain: model, _ = fluid.dygraph.load_dygraph(args.save_dir + '/ECO_FULL_RGB_seg16') # also tried using pretrained model on torch, 32F-92.9%,16F-91.8% precision trained on torch # model, _ = fluid.dygraph.load_dygraph(args.save_dir + '/eco_91.81_model_best') train_model.load_dict(model) # build model if not os.path.exists(args.save_dir): os.makedirs(args.save_dir) # ===================== Init Data Reader =====================# # leverage from TSN training script train_config.TRAIN.batch_size = train_config.TRAIN.batch_size train_reader = KineticsReader('ECO', 'train', train_config).create_reader() print('train_reader', train_reader) val_reader = KineticsReader('ECO', 'valid', val_config).create_reader() if distributed: train_reader = fluid.contrib.reader.distributed_batch_reader(train_reader) # ===================== Init Trick Params =====================# epochs = args.epoch or train_model.epoch_num() loss_summ = 0 saturate_cnt = 0 exp_num = 0 best_prec1 = 0 for i in range(epochs): train_model.train() # trick 4: Saturate lr decay: different from lr piecewise decay or others # calculate prec every epoch, if prec1 does not rise for 5 times(named model saturated), then use decay lr. if saturate_cnt == args.num_saturate: exp_num = exp_num + 1 saturate_cnt = 0 decay = 0.1 ** (exp_num) learning_rate = learning_rate * decay opt = fluid.optimizer.Momentum(learning_rate, 0.9, parameter_list=train_model.parameters(), use_nesterov=True, regularization=fluid.regularizer.L2Decay(regularization_coeff=5e-4), grad_clip=clip) print('get_optim_policies:--batch_norm_0.w_0', opt._parameter_list[2].optimize_attr, opt._parameter_list[2].stop_gradient) print('get_optim_policies:--batch_norm_0.b_0', opt._parameter_list[3].optimize_attr, opt._parameter_list[2].stop_gradient) print("- Learning rate decreases by a factor of '{}'".format(10 ** (exp_num))) for batch_id, data in enumerate(train_reader()): lr = opt.current_step_lr() print('lr:', lr) # check lr every batch ids dy_x_data = np.array([x[0] for x in data]).astype('float32') y_data = np.array([[x[1]] for x in data]).astype('int64') img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True out, acc = train_model(img, label) loss = fluid.layers.cross_entropy(out, label) avg_loss = fluid.layers.mean(loss) loss_summ += avg_loss if distributed: avg_loss = train_model.scale_loss(avg_loss) avg_loss.backward() if distributed: train_model.apply_collective_grads() if (batch_id + 1) % 4 == 0: # trick 5: scale down gradients when iter size is functioning every 4 batches opt.minimize(loss_summ) opt.clear_gradients() loss_summ = 0 if batch_id % 1 == 0: logger.info( "Loss at epoch {} step {}: {}, acc: {}".format(i, batch_id, avg_loss.numpy(), acc.numpy())) print("Loss at epoch {} step {}: {}, acc: {}".format(i, batch_id, avg_loss.numpy(), acc.numpy())) if (i + 1) % args.eval_freq == 0 or i == args.epochs - 1: train_model.eval() acc_list = [] false_class = [] for batch_id, data in enumerate(val_reader()): dy_x_data = np.array([x[0] for x in data]).astype('float32') y_data = np.array([[x[1]] for x in data]).astype('int64') img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True out, acc = train_model(img, label) if acc.numpy()[0] != 1: false_class.append(label.numpy()[0][0]) acc_list.append(acc.numpy()[0]) print(batch_id, 'acc:', np.mean(acc_list)) if len(false_class) == 0: continue print("validate set acc:{}".format(np.mean(acc_list))) prec1 = np.mean(acc_list) # remember best prec@1 and save checkpoint is_best = prec1 > best_prec1 if is_best: saturate_cnt = 0 fluid.dygraph.save_dygraph(train_model.state_dict(), args.save_dir + '/ECO_FULL_1/' + str(i) + '_best_' + str(prec1)) else: saturate_cnt = saturate_cnt + 1 print("- Validation Prec@1 saturates for {} epochs.".format(saturate_cnt), best_prec1) best_prec1 = max(prec1, best_prec1) logger.info("Final loss: {}".format(avg_loss.numpy())) print("Final loss: {}".format(avg_loss.numpy()))
def train(args): # parse config 参数配置 place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace() # 是否使用 GPU with fluid.dygraph.guard(place): config = parse_config(args.config) train_config = merge_configs(config, 'train', vars(args)) # vars 函数,返回参数表达式的值 print_configs( train_config, 'Train configs : ' ) train_model = ResNet3D.ResNet3D('resnet',train_config['MODEL']['num_layers'], train_config['MODEL']['num_classes'], train_config['MODEL']['seg_num'], 0.00002) #根据自己定义的网络,声明train_model # parameter_list 指明在训练的时候,哪些参数( 在此是 train_model.parameters() )会被优化 opt = fluid.optimizer.Momentum(0.001, 0.9, parameter_list=train_model.parameters()) if args.pretrain: # 加载上一次训练的模型,继续训练 model, _ = fluid.dygraph.load_dygraph(args.save_dir + '/resnet_model') train_model.load_dict(model) # 创建一个保存模型的路径 if not os.path.exists(args.save_dir): os.makedirs(args.save_dir) # get reader train_config.TRAIN.batch_size = train_config.TRAIN.batch_size # 两边完全一样啊??? # KineticsReader().create_reader() 函数返回值是 batch_size 组 <img, label> 数据 train_reader = KineticsReader(args.model_name.upper(), 'train', train_config).create_reader() epochs = args.epoch or train_model.epoch_num() for i in range(epochs): for batch_id, data in enumerate(train_reader()): dy_x_data = np.array([x[0] for x in data]).astype('float32') y_data = np.array([[x[1]] for x in data]).astype('int64') ## 获取的img 是一个5维数据:batchbatch_size,提取多少片段(seg_num*seg_len),通道数,长,宽 img = fluid.dygraph.to_variable(dy_x_data) label = fluid.dygraph.to_variable(y_data) label.stop_gradient = True out, acc = train_model(img, label) loss = fluid.layers.cross_entropy(out, label) avg_loss = fluid.layers.mean(loss) avg_loss.backward() opt.minimize(avg_loss) train_model.clear_gradients() # 隔多少次训练,进行一次输出提示 if batch_id % 1 == 0: logger.info("Loss at epoch {} step {}: {}, acc: {}".format(i, batch_id, avg_loss.numpy(), acc.numpy())) print("Loss at epoch {} step {}: {}, acc: {}".format(i, batch_id, avg_loss.numpy(), acc.numpy())) fluid.dygraph.save_dygraph(train_model.state_dict(), args.save_dir + '/resnet_model') logger.info("Final loss: {}".format(avg_loss.numpy())) print("Final loss: {}".format(avg_loss.numpy()))
input = fluid.data(name='data', shape=[-1, 3, 224, 224], dtype='float32') label = fluid.data(name='label', shape=[-1, 1], dtype='int64') print(f'label shape:{label.shape}') model = ECOfull(input, num_segments=args.num_segments) net_out = model() cost = fluid.layers.softmax_with_cross_entropy(net_out, label) avg_cost = fluid.layers.mean(cost) acc = fluid.layers.accuracy(net_out, label) # test program eval_program = main_program.clone(for_test=True) # optimizer fluid.optimizer.SGD(args.lr).minimize(avg_cost) # 验证集 val_reader = KineticsReader('eco', 'valid', cfg).create_reader() feeder = fluid.DataFeeder([input, label], place) # 初始化参数 exe = fluid.Executor(place=place) exe.run(start_program) val_exe = fluid.Executor(place=place) fluid.io.load(main_program, 'models/eco_full_best', val_exe) val_avg_loss = [] val_acc = [] fetch_list = [avg_cost.name, acc.name] for data in val_reader(): avg_cost_, acc_ = exe.run(eval_program, feed=feeder.feed(data), fetch_list=fetch_list) val_avg_loss += [avg_cost_] val_acc += [acc_]
log_vars = dict() for loss_name, loss_value in losses.items(): log_vars[loss_name] = fluid.layers.mean(loss_value) loss = sum(_value for _key, _value in log_vars.items() if 'loss' in _key) log_vars['loss'] = loss return loss, log_vars args = parse_args() config = parse_config(args.config) train_config = merge_configs(config, 'train', vars(args)) val_config = merge_configs(config, 'valid', vars(args)) train_reader = KineticsReader(args.model_name.upper(), 'train', train_config).create_reader() val_reader = KineticsReader(args.model_name.upper(), 'valid', val_config).create_reader() label = fluid.layers.data(name='label', shape=[1], dtype='int64') data_shape = [1, 3, 32, 224, 224] img = fluid.layers.data(name='images', shape=data_shape, dtype='float32') network = TSN.TSN3D( backbone=train_config['MODEL']['backbone'], necks=train_config['MODEL']['necks'], spatial_temporal_module=train_config['MODEL']['spatial_temporal_module'], segmental_consensus=train_config['MODEL']['segmental_consensus'], cls_head=train_config['MODEL']['cls_head']) losses, acc = network.net(img, label, is_training_val=True) loss, log_vars = parse_losses(losses)
def main(): global args, best_acc args = parser.parse_args() writer = LogWriter(args.log) # writer = None cfg = parse_config('config.txt') print_configs(cfg, 'TRAIN') main_program = fluid.default_main_program() start_program = fluid.default_startup_program() place = fluid.CUDAPlace(0) if args.use_cuda else fluid.CPUPlace() with fluid.program_guard(main_program, start_program): # data placeholder input = fluid.data(name='data', shape=[-1, 3, 224, 224], dtype='float32') label = fluid.data(name='label', shape=[-1, 1], dtype='int64') print(f'label shape:{label.shape}') model = ECOfull(input, num_segments=args.num_segments) net_out = model() cost = fluid.layers.softmax_with_cross_entropy(net_out, label) avg_cost = fluid.layers.mean(cost) acc = fluid.layers.accuracy(net_out, label) # test program eval_program = main_program.clone(for_test=True) # optimizer fluid.optimizer.SGD(args.lr).minimize(avg_cost) #print(main_program.all_parameters()) reader = KineticsReader('eco', 'train', cfg).create_reader() feeder = fluid.DataFeeder([input, label], place) # 验证集 val_reader = KineticsReader('eco', 'valid', cfg).create_reader() # 初始化参数 exe = fluid.Executor(place=place) exe.run(start_program) train_exe = fluid.Executor(place=place) if 0: # fluid.io.load(train_exe, 'models/', filename='eco_full.pdparams') fluid.io.load(main_program, 'models/eco_full_best', train_exe) # # pre-trained else: f = open('program_state_dict.pkl', 'rb') state_dict = pickle.load(f) f.close() fluid.io.set_program_state(main_program, state_dict) step = 0 best_acc = read_best_acc() for i in range(args.epochs): for index, data in enumerate(reader()): avg_cost_, acc_ = train_exe.run( main_program, feed=feeder.feed(data), fetch_list=[avg_cost.name, acc.name]) if (index + 1) % args.print_freq == 0: if not writer is None: writer.add_scalar(tag='train/loss', step=step, value=avg_cost_[0]) writer.add_scalar(tag='train/acc', step=step, value=acc_[0]) print( f'epoch:{i+1} step:{index + 1} avg loss:{avg_cost_[0]} acc:{acc_[0]}' ) step += 1 if (i + 1) % args.eval_freq == 0: fetch_list = [avg_cost.name, acc.name] validate(val_reader, feeder, place, eval_program, fetch_list, epoch=i, writer=writer)
def train(args): config = parse_config(args.config) train_config = merge_configs(config, 'train', vars(args)) valid_config = merge_configs(config, 'valid', vars(args)) print_configs(train_config, 'Train') use_data_parallel = False trainer_count = fluid.dygraph.parallel.Env().nranks place = fluid.CUDAPlace(fluid.dygraph.parallel.Env().dev_id) \ if use_data_parallel else fluid.CUDAPlace(0) with fluid.dygraph.guard(place): if use_data_parallel: strategy = fluid.dygraph.parallel.prepare_context() video_model = TSM_ResNet("TSM", train_config) optimizer = create_optimizer(train_config.TRAIN, video_model.parameters()) if use_data_parallel: video_model = fluid.dygraph.parallel.DataParallel( video_model, strategy) bs_denominator = 1 if args.use_gpu: # check number of GPUs gpus = os.getenv("CUDA_VISIBLE_DEVICES", "") if gpus == "": pass else: gpus = gpus.split(",") num_gpus = len(gpus) assert num_gpus == train_config.TRAIN.num_gpus, \ "num_gpus({}) set by CUDA_VISIBLE_DEVICES" \ "shoud be the same as that" \ "set in {}({})".format( num_gpus, args.config, train_config.TRAIN.num_gpus) bs_denominator = train_config.TRAIN.num_gpus train_config.TRAIN.batch_size = int(train_config.TRAIN.batch_size / bs_denominator) train_reader = KineticsReader(mode="train", cfg=train_config) train_reader = train_reader.create_reader() if use_data_parallel: train_reader = fluid.contrib.reader.distributed_batch_reader( train_reader) for epoch in range(train_config.TRAIN.epoch): video_model.train() total_loss = 0.0 total_acc1 = 0.0 total_acc5 = 0.0 total_sample = 0 for batch_id, data in enumerate(train_reader()): x_data = np.array([item[0] for item in data]) y_data = np.array([item[1] for item in data]).reshape([-1, 1]) imgs = to_variable(x_data) labels = to_variable(y_data) labels.stop_gradient = True outputs = video_model(imgs) loss = fluid.layers.cross_entropy(input=outputs, label=labels, ignore_index=-1) avg_loss = fluid.layers.mean(loss) acc_top1 = fluid.layers.accuracy(input=outputs, label=labels, k=1) acc_top5 = fluid.layers.accuracy(input=outputs, label=labels, k=5) if use_data_parallel: avg_loss = video_model.scale_loss(avg_loss) avg_loss.backward() video_model.apply_collective_grads() else: avg_loss.backward() optimizer.minimize(avg_loss) video_model.clear_gradients() total_loss += avg_loss.numpy()[0] total_acc1 += acc_top1.numpy()[0] total_acc5 += acc_top5.numpy()[0] total_sample += 1 print('TRAIN Epoch {}, iter {}, loss = {}, acc1 {}, acc5 {}'. format(epoch, batch_id, avg_loss.numpy()[0], acc_top1.numpy()[0], acc_top5.numpy()[0])) print( 'TRAIN End, Epoch {}, avg_loss= {}, avg_acc1= {}, avg_acc5= {}' .format(epoch, total_loss / total_sample, total_acc1 / total_sample, total_acc5 / total_sample)) video_model.eval() val(epoch, video_model, valid_config, args) if fluid.dygraph.parallel.Env().local_rank == 0: fluid.dygraph.save_dygraph(video_model.state_dict(), "final") logger.info('[TRAIN] training finished')