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) train_model = ResNet3D.ResNet3D('res3d', 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 + '/res3d_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) # print(img.shape) ##################测试 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 % 12 == 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 + '/res3d_model') logger.info("Final loss: {}".format(avg_loss.numpy())) print("Final loss: {}".format(avg_loss.numpy()))
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 = ResNet3D.Resnet('ResNet3D',[2,2,2,2], ResNet3D.get_inplanes()) 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 = ResNet3D.Resnet('ResNet3D', [2, 2, 2, 2], ResNet3D.get_inplanes()) 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]]))
pin_memory=par.pin_mem) print("Number of samples in test dataset: %d\n" % len(test_df.index)) #create model if par.model == "deepvo": model = DeepVO(par.img_h, par.img_w, par.batch_norm) elif par.model == "resnet3d": if par.resnet_depth == 18: num_blocks = [2, 2, 2, 2] elif par.resnet_depth == 34: num_blocks = [3, 4, 6, 3] else: raise NotImplementedError("Invalid choice of Resnet depth!") model = ResNet3D(num_blocks) else: raise NotImplementedError("Invalid model selected!") use_cuda = torch.cuda.is_available() if use_cuda: print("Moving model to GPU...\n") model = model.cuda() if not par.load_weights or not os.path.isfile(par.load_model_path): raise ValueError("Pretrained weights not provided!") else: model.load_state_dict(torch.load(par.load_model_path)) print("Begin evaluating model...\n")
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()))
import torch import paddle.fluid as fluid from collections import OrderedDict from model import ResNet3D torch_weight = torch.load('r3d50_KM_200ep.pth', map_location=torch.device('cpu')) # 全连接1039 weight = [] for torch_key in torch_weight['state_dict'].keys(): weight.append( [torch_key, torch_weight['state_dict'][torch_key].detach().numpy()]) with fluid.dygraph.guard(): # 加载网络结构 paddle_model = ResNet3D.generate_model(50) # 读取paddle网络结构的参数列表 paddle_weight = paddle_model.state_dict() # 将paddle权重的参数列表打印出来 # for paddle_key in paddle_weight: # print(paddle_key) # 进行模型参数转换 new_weight_dict = OrderedDict() i = 0 for paddle_key in paddle_weight.keys(): print(paddle_key) if 'num_batches_tracked' in weight[i][0]: i += 1
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()))