Exemplo n.º 1
0
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()))
Exemplo n.º 2
0
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.ResNet3D('resnet',
                                      val_config['MODEL']['num_layers'],
                                      val_config['MODEL']['num_classes'],
                                      val_config['MODEL']['seg_num'], 0.00002)

        label_dic = np.load('label_dir.npy', allow_pickle=True).item()

        # print('eval.py 中的 label_dic  : ', label_dic)
        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.ResNet3D('res3d',
                                        infer_config['MODEL']['num_layers'],
                                        infer_config['MODEL']['num_classes'],
                                        infer_config['MODEL']['seg_num'],
                                        0.00002)

        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]]))
Exemplo n.º 4
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")
Exemplo n.º 5
0
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()))