未验证 提交 8dc42c73 编写于 作者: S SunGaofeng 提交者: GitHub

fix fluid.io.save/load to fluid.save/load (#4337)

* fix fluid.io.save/load into fluid.save/load

* make the filename of saved params consistent with that in readme
上级 783b34c4
......@@ -163,38 +163,38 @@ run.sh
| 模型 | Batch Size | 环境配置 | cuDNN版本 | GAP | 下载链接 |
| :-------: | :---: | :---------: | :-----: | :----: | :----------: |
| Attention Cluster | 2048 | 8卡P40 | 7.1 | 0.84 | [model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionCluster_final.pdparams) |
| Attention LSTM | 1024 | 8卡P40 | 7.1 | 0.86 | [model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionLSTM_final.pdparams) |
| NeXtVLAD | 160 | 4卡P40 | 7.1 | 0.87 | [model](https://paddlemodels.bj.bcebos.com/video_classification/NEXTVLAD_final.pdparams) |
| Attention Cluster | 2048 | 8卡P40 | 7.1 | 0.84 | [model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionCluster.pdparams) |
| Attention LSTM | 1024 | 8卡P40 | 7.1 | 0.86 | [model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionLSTM.pdparams) |
| NeXtVLAD | 160 | 4卡P40 | 7.1 | 0.87 | [model](https://paddlemodels.bj.bcebos.com/video_classification/NEXTVLAD.pdparams) |
- 基于Kinetics数据集模型:
| 模型 | Batch Size | 环境配置 | cuDNN版本 | Top-1 | 下载链接 |
| :-------: | :---: | :---------: | :----: | :----: | :----------: |
| StNet | 128 | 8卡P40 | 7.1 | 0.69 | [model](https://paddlemodels.bj.bcebos.com/video_classification/STNET_final.pdparams) |
| TSN | 256 | 8卡P40 | 7.1 | 0.67 | [model](https://paddlemodels.bj.bcebos.com/video_classification/TSN_final.pdparams) |
| TSM | 128 | 8卡P40 | 7.1 | 0.70 | [model](https://paddlemodels.bj.bcebos.com/video_classification/TSM_final.pdparams) |
| Non-local | 64 | 8卡P40 | 7.1 | 0.74 | [model](https://paddlemodels.bj.bcebos.com/video_classification/NONLOCAL_final.pdparams) |
| StNet | 128 | 8卡P40 | 7.1 | 0.69 | [model](https://paddlemodels.bj.bcebos.com/video_classification/STNET.pdparams) |
| TSN | 256 | 8卡P40 | 7.1 | 0.67 | [model](https://paddlemodels.bj.bcebos.com/video_classification/TSN.pdparams) |
| TSM | 128 | 8卡P40 | 7.1 | 0.70 | [model](https://paddlemodels.bj.bcebos.com/video_classification/TSM.pdparams) |
| Non-local | 64 | 8卡P40 | 7.1 | 0.74 | [model](https://paddlemodels.bj.bcebos.com/video_classification/NONLOCAL.pdparams) |
- 基于ActivityNet的动作定位模型:
| 模型 | Batch Size | 环境配置 | cuDNN版本 | 精度 | 下载链接 |
| :-------: | :---: | :---------: | :----: | :----: | :----------: |
| C-TCN | 16 | 8卡P40 | 7.1 | 0.31 (MAP) | [model](https://paddlemodels.bj.bcebos.com/video_detection/CTCN_final.pdparams) |
| BSN | 16 | 1卡K40 | 7.0 | 66.64% (AUC) | [model-tem](https://paddlemodels.bj.bcebos.com/video_detection/BsnTem_final.pdparams), [model-pem](https://paddlemodels.bj.bcebos.com/video_detection/BsnPem_final.pdparams) |
| BMN | 16 | 4卡K40 | 7.0 | 67.19% (AUC) | [model](https://paddlemodels.bj.bcebos.com/video_detection/BMN_final.pdparams) |
| C-TCN | 16 | 8卡P40 | 7.1 | 0.31 (MAP) | [model](https://paddlemodels.bj.bcebos.com/video_detection/CTCN.pdparams) |
| BSN | 16 | 1卡K40 | 7.0 | 66.64% (AUC) | [model-tem](https://paddlemodels.bj.bcebos.com/video_detection/BsnTem.pdparams), [model-pem](https://paddlemodels.bj.bcebos.com/video_detection/BsnPem.pdparams) |
| BMN | 16 | 4卡K40 | 7.0 | 67.19% (AUC) | [model](https://paddlemodels.bj.bcebos.com/video_detection/BMN.pdparams) |
- 基于ActivityNet Captions的视频描述模型:
| 模型 | Batch Size | 环境配置 | cuDNN版本 | METEOR | 下载链接 |
| :-------: | :---: | :---------: | :----: | :----: | :----------: |
| ETS | 256 | 4卡P40 | 7.0 | 9.8 | [model](https://paddlemodels.bj.bcebos.com/video_caption/ETS_final.pdparams) |
| ETS | 256 | 4卡P40 | 7.0 | 9.8 | [model](https://paddlemodels.bj.bcebos.com/video_caption/ETS.pdparams) |
- 基于TACoS的视频查找模型:
| 模型 | Batch Size | 环境配置 | cuDNN版本 | R1@IOU5 | R5@IOU5 | 下载链接 |
| :-------: | :---: | :---------: | :----: | :----: | :----: | :----------: |
| TALL | 56 | 1卡P40 | 7.2 | 0.13 | 0.24 | [model](https://paddlemodels.bj.bcebos.com/video_grounding/TALL_final.pdparams) |
| TALL | 56 | 1卡P40 | 7.2 | 0.13 | 0.24 | [model](https://paddlemodels.bj.bcebos.com/video_grounding/TALL.pdparams) |
......
......@@ -43,7 +43,7 @@ Attention Cluster模型使用2nd-Youtube-8M数据集, 数据下载及准备请
bash run.sh train AttentionCluster ./configs/attention_cluster.yaml
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionCluster_final.pdparams)通过`--resume`指定权重存放路径进行finetune等开发,或者在run.sh脚本中修改resume为解压之后的权重文件存放路径。
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionCluster.pdparams)通过`--resume`指定权重存放路径进行finetune等开发,或者在run.sh脚本中修改resume为解压之后的权重文件存放路径。
**数据读取器说明:** 模型读取Youtube-8M数据集中已抽取好的`rgb``audio`数据,对于每个视频的数据,均匀采样100帧,该值由配置文件中的`seg_num`参数指定。
......@@ -69,7 +69,7 @@ Attention Cluster模型使用2nd-Youtube-8M数据集, 数据下载及准备请
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要评估的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionCluster_final.pdparams)进行评估
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionCluster.pdparams)进行评估
- 评估结果以log的形式直接打印输出GAP、Hit@1等精度指标
......@@ -110,7 +110,7 @@ Attention Cluster模型使用2nd-Youtube-8M数据集, 数据下载及准备请
- 使用`run.sh`进行评估时,请修改脚本中的`weights`参数指定需要用到的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionCluster_final.pdparams)进行推断
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionCluster.pdparams)进行推断
- 模型推断结果以log的形式直接打印输出,可以看到每个测试样本的分类预测概率。
......
......@@ -138,6 +138,6 @@ class AttentionCluster(ModelBase):
def weights_info(self):
return (
"AttentionCluster_final.pdparams",
"https://paddlemodels.bj.bcebos.com/video_classification/AttentionCluster_final.pdparams"
"AttentionCluster.pdparams",
"https://paddlemodels.bj.bcebos.com/video_classification/AttentionCluster.pdparams"
)
......@@ -40,7 +40,7 @@ AttentionLSTM模型使用2nd-Youtube-8M数据集,关于数据部分请参考[
- AttentionLSTM模型使用8卡Nvidia Tesla P40来训练的,总的batch size数是1024。
### 使用预训练模型做finetune
请先将提供的[model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionLSTM_final.pdparams)下载到本地,并在上述脚本文件中添加`--resume`为所保存的预训练模型存放路径。
请先将提供的[model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionLSTM.pdparams)下载到本地,并在上述脚本文件中添加`--resume`为所保存的预训练模型存放路径。
## 模型评估
可通过如下两种方式进行模型评估:
......@@ -56,7 +56,7 @@ AttentionLSTM模型使用2nd-Youtube-8M数据集,关于数据部分请参考[
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要评估的权重。
- 若未指定`weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionLSTM_final.pdparams)进行评估
- 若未指定`weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionLSTM.pdparams)进行评估
- 评估结果以log的形式直接打印输出GAP、Hit@1等精度指标
......@@ -99,7 +99,7 @@ AttentionLSTM模型使用2nd-Youtube-8M数据集,关于数据部分请参考[
- 使用`run.sh`进行评估时,请修改脚本中的`weights`参数指定需要用到的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionLSTM_final.pdparams)进行推断
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/AttentionLSTM.pdparams)进行推断
- 模型推断结果以log的形式直接打印输出,可以看到每个测试样本的分类预测概率。
......
......@@ -151,6 +151,6 @@ class AttentionLSTM(ModelBase):
def weights_info(self):
return (
'AttentionLSTM_final.pdparams',
'https://paddlemodels.bj.bcebos.com/video_classification/AttentionLSTM_final.pdparams'
'AttentionLSTM.pdparams',
'https://paddlemodels.bj.bcebos.com/video_classification/AttentionLSTM.pdparams'
)
......@@ -69,7 +69,7 @@ BMN的训练数据采用ActivityNet1.3提供的数据集,数据下载及准备
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要评估的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/BMN_final.pdparams)进行评估。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/BMN.pdparams)进行评估。
- 上述程序会将运行结果保存在data/output/EVAL\BMN\_results文件夹下,测试结果保存在data/evaluate\_results/bmn\_results\_validation.json文件中。使用ActivityNet官方提供的测试脚本,即可计算AR@AN和AUC。具体计算过程请参考[指标计算](../../metrics/bmn_metrics/README.md)
......@@ -101,7 +101,7 @@ BMN的训练数据采用ActivityNet1.3提供的数据集,数据下载及准备
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要用到的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/BMN_final.pdparams)进行推断。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/BMN.pdparams)进行推断。
- 上述程序会将运行结果保存在data/output/INFER/BMN\_results文件夹下,测试结果保存在data/predict\_results/bmn\_results\_test.json文件中。
......
......@@ -192,6 +192,5 @@ class BMN(ModelBase):
def weights_info(self):
return (
'BMN_final.pdparams',
'https://paddlemodels.bj.bcebos.com/video_detection/BMN_final.pdparams'
)
'BMN.pdparams',
'https://paddlemodels.bj.bcebos.com/video_detection/BMN.pdparams')
......@@ -367,5 +367,4 @@ class BMN_NET(object):
tem_loss = self.tem_loss_func(pred_start, pred_end, gt_start, gt_end)
loss = tem_loss + 10 * pem_reg_loss + pem_cls_loss
loss.persistable = True
return loss, tem_loss, pem_reg_loss, pem_cls_loss
......@@ -101,7 +101,7 @@ TEM模块可通过如下两种方式进行模型评估:
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要评估的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/BsnTem_final.pdparams)进行评估
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/BsnTem.pdparams)进行评估
- 上述程序会将运行结果保存在data/output/EVAL/TEM\_results文件夹下,同时调用PGM模块生成proposal和BSP特征,分别保存在data/output/EVAL/PGM\_proposals和data/output/EVAL/PGM\_feature路径下。
......@@ -119,7 +119,7 @@ TEM评估模块完成后,PEM模块可通过如下两种方式进行模型评
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要评估的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/BsnPem_final.pdparams)进行评估。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/BsnPem.pdparams)进行评估。
- 上述程序会将运行结果保存在data/output/EVAL/PEM\_results文件夹下,测试结果保存在data/evaluate\_results/bsn\_results\_validation.json文件中。使用ActivityNet官方提供的测试脚本,即可计算AR@AN、AUC。具体计算过程请参考[指标计算](../../metrics/bmn_metrics/README.md)
......@@ -149,7 +149,7 @@ TEM模块可通过如下两种方式启动模型推断:
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要用到的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/BsnTem_final.pdparams)进行推断。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/BsnTem.pdparams)进行推断。
- 上述程序会将运行结果保存在data/output/INFER的子目录TEM\_results、PGM\_proposals、PGM\_feature中。
......@@ -170,7 +170,7 @@ PEM模块可通过如下两种方式启动模型推断:
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要用到的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/BsnPem_final.pdparams)进行推断。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/BsnPem.pdparams)进行推断。
- 上述程序会将运行结果保存在data/output/INFER/PEM\_results文件夹下,测试结果保存在data/predict\_results/bsn\_results\_test.json文件中。
......
......@@ -181,8 +181,8 @@ class BsnTem(ModelBase):
def weights_info(self):
return (
'BsnTem_final.pdparams',
'https://paddlemodels.bj.bcebos.com/video_detection/BsnTem_final.pdparams'
'BsnTem.pdparams',
'https://paddlemodels.bj.bcebos.com/video_detection/BsnTem.pdparams'
)
......@@ -330,6 +330,6 @@ class BsnPem(ModelBase):
def weights_info(self):
return (
'BsnPem_final.pdparams',
'https://paddlemodels.bj.bcebos.com/video_detection/BsnPem_final.pdparams'
'BsnPem.pdparams',
'https://paddlemodels.bj.bcebos.com/video_detection/BsnPem.pdparams'
)
......@@ -41,7 +41,7 @@ C-TCN的训练数据采用ActivityNet1.3提供的数据集,数据下载及准
- 从头开始训练,使用上述启动命令行或者脚本程序即可启动训练,不需要用到预训练模型
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/CTCN_final.pdparams)通过`--resume`指定权重存放路径进行finetune等开发
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/CTCN.pdparams)通过`--resume`指定权重存放路径进行finetune等开发
**训练策略:**
......@@ -64,7 +64,7 @@ C-TCN的训练数据采用ActivityNet1.3提供的数据集,数据下载及准
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要评估的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/CTCN_final.pdparams)进行评估
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/CTCN.pdparams)进行评估
- 运行上述程序会将测试结果保存在json文件中,默认存储在data/evaluate\_results目录下,程序根据所使用的超参数自动生成文件名,例如:CTCN\_test\_res\_decode\_0.001\_0.8\_0.9\_0.004.json。使用ActivityNet官方提供的测试脚本,即可计算MAP。具体计算过程请参考[指标计算](../../metrics/detections/README.md)
......@@ -96,7 +96,7 @@ C-TCN的训练数据采用ActivityNet1.3提供的数据集,数据下载及准
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要用到的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/CTCN_final.pdparams)进行推断
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_detection/CTCN.pdparams)进行推断
- 模型推断结果存储于json文件中,默认存储在`data/dataset/inference_results`目录下,程序根据所使用的超参数自动生成文件名,例如:CTCN\_infer\_res\_decode\_0.001\_0.8\_0.9\_0.004.json。同时也会以log的形式打印输出,显示每个视频的预测片段起止时间和类别
......
......@@ -182,6 +182,5 @@ class CTCN(ModelBase):
def weights_info(self):
return (
'CTCN_final.pdparams',
'https://paddlemodels.bj.bcebos.com/video_detection/CTCN_final.pdparams'
)
'CTCN.pdparams',
'https://paddlemodels.bj.bcebos.com/video_detection/CTCN.pdparams')
......@@ -42,7 +42,7 @@ ETS的训练数据采用ActivityNet Captions提供的数据集,数据下载及
- 从头开始训练,使用上述启动命令行或者脚本程序即可启动训练,不需要用到预训练模型
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_caption/ETS_final.pdparams)通过`--resume`指定权重存放路径进行finetune等开发
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_caption/ETS.pdparams)通过`--resume`指定权重存放路径进行finetune等开发
**训练策略:**
......@@ -65,7 +65,7 @@ ETS的训练数据采用ActivityNet Captions提供的数据集,数据下载及
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要评估的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_caption/ETS_final.pdparams)进行评估
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_caption/ETS.pdparams)进行评估
- 运行上述程序会将测试结果保存在json文件中,默认存储在data/evaluate\_results目录下。使用ActivityNet Captions官方提供的测试脚本,即可计算METEOR。具体计算过程请参考[指标计算](../../metrics/ets_metrics/README.md)
......@@ -96,7 +96,7 @@ ETS的训练数据采用ActivityNet Captions提供的数据集,数据下载及
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要用到的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_caption/ETS_final.pdparams)进行推断
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_caption/ETS.pdparams)进行推断
- 模型推断结果存储于json文件中,默认存储在`data/dataset/predict_results`目录下
......
......@@ -189,7 +189,5 @@ class ETS(ModelBase):
return (None, None)
def weights_info(self):
return (
'ETS_final.pdparams',
'https://paddlemodels.bj.bcebos.com/video_caption/ETS_final.pdparams'
)
return ('ETS.pdparams',
'https://paddlemodels.bj.bcebos.com/video_caption/ETS.pdparams')
......@@ -28,6 +28,10 @@ WEIGHT_DIR = os.path.join(os.path.expanduser('~'), '.paddle', 'weights')
logger = logging.getLogger(__name__)
def is_parameter(var):
return isinstance(var, fluid.framework.Parameter)
class NotImplementError(Exception):
"Error: model function not implement"
......@@ -146,10 +150,12 @@ class ModelBase(object):
def load_pretrain_params(self, exe, pretrain, prog, place):
logger.info("Load pretrain weights from {}".format(pretrain))
fluid.io.load_params(exe, pretrain, main_program=prog)
state_dict = fluid.load_program_state(pretrain)
fluid.set_program_state(prog, state_dict)
def load_test_weights(self, exe, weights, prog, place):
fluid.io.load_params(exe, '', main_program=prog, filename=weights)
params_list = list(filter(is_parameter, prog.list_vars()))
fluid.load(prog, weights, executor=exe, var_list=params_list)
def get_config_from_sec(self, sec, item, default=None):
if sec.upper() not in self.cfg:
......
......@@ -41,7 +41,7 @@ NeXtVLAD模型使用2nd-Youtube-8M数据集, 数据下载及准备请参考[数
### 使用预训练模型做finetune
请先将提供的预训练模型[model](https://paddlemodels.bj.bcebos.com/video_classification/NEXTVLAD_final.pdparams)下载到本地,并在上述脚本文件中添加--resume为所保存的模型参数存放路径。
请先将提供的预训练模型[model](https://paddlemodels.bj.bcebos.com/video_classification/NEXTVLAD.pdparams)下载到本地,并在上述脚本文件中添加--resume为所保存的模型参数存放路径。
使用4卡Nvidia Tesla P40,总的batch size数是160。
......@@ -65,7 +65,7 @@ NeXtVLAD模型使用2nd-Youtube-8M数据集, 数据下载及准备请参考[数
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要评估的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/NEXTVLAD_final.pdparams)进行评估
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/NEXTVLAD.pdparams)进行评估
- 评估结果以log的形式直接打印输出GAP、Hit@1等精度指标
......@@ -110,7 +110,7 @@ NeXtVLAD模型使用2nd-Youtube-8M数据集, 数据下载及准备请参考[数
- 使用`run.sh`进行评估时,请修改脚本中的`weights`参数指定需要用到的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/NEXTVLAD_final.pdparams)进行推断
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/NEXTVLAD.pdparams)进行推断
- 模型推断结果以log的形式直接打印输出,可以看到每个测试样本的分类预测概率。
......
......@@ -183,8 +183,8 @@ class NEXTVLAD(ModelBase):
def weights_info(self):
return (
'NEXTVLAD_final.pdparams',
'https://paddlemodels.bj.bcebos.com/video_classification/NEXTVLAD_final.pdparams'
'NEXTVLAD.pdparams',
'https://paddlemodels.bj.bcebos.com/video_classification/NEXTVLAD.pdparams'
)
......
......@@ -102,7 +102,7 @@ Non-local模型的训练数据采用由DeepMind公布的Kinetics-400动作识别
- 从头开始训练,需要加载在ImageNet上训练的ResNet50权重作为初始化参数(该模型参数转自Caffe2)。请下载此[模型参数](https://paddlemodels.bj.bcebos.com/video_classification/Nonlocal_ResNet50_pretrained.tar.gz)并解压,将上面启动命令行或者run.sh脚本中的`pretrain`参数设置为解压之后的模型参数存放路径。如果没有手动下载并设置`pretrain`参数,则程序会自动下载并将参数保存在~/.paddle/weights/Nonlocal\_ResNet50\_pretrained目录下面
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/NONLOCAL_final.pdparams)通过`--resume`指定权重存放路径进行finetune等开发
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/NONLOCAL.pdparams)通过`--resume`指定权重存放路径进行finetune等开发
**数据读取器说明:** 模型读取Kinetics-400数据集中的`mp4`数据,根据视频长度和采样频率随机选取起始帧的位置,每个视频抽取`video_length`帧图像,对每帧图像做随机增强,短边缩放至[256, 320]之间的某个随机数,长边根据长宽比计算出来,然后再截取出224x224的区域作为训练数据输入网络。
......@@ -129,7 +129,7 @@ Non-local模型的训练数据采用由DeepMind公布的Kinetics-400动作识别
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要评估的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/NONLOCAL_final.pdparams)进行评估
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/NONLOCAL.pdparams)进行评估
- 评估结果以log的形式直接打印输出TOP1\_ACC、TOP5\_ACC等精度指标
......@@ -167,7 +167,7 @@ Non-local模型的训练数据采用由DeepMind公布的Kinetics-400动作识别
- 如果video\_path为'', 则忽略掉此参数。如果video\_path != '',则程序会对video\_path指定的视频文件进行预测,而忽略掉filelist的值,预测结果为此视频的分类概率。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/NONLOCAL_final.pdparams)进行推断
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/NONLOCAL.pdparams)进行推断
- 模型推断结果以log的形式直接打印输出,可以看到测试样本的分类预测概率。
......
......@@ -137,8 +137,8 @@ class NonLocal(ModelBase):
def weights_info(self):
return (
'NONLOCAL_final.pdparams',
'https://paddlemodels.bj.bcebos.com/video_classification/NONLOCAL_final.pdparams'
'NONLOCAL.pdparams',
'https://paddlemodels.bj.bcebos.com/video_classification/NONLOCAL.pdparams'
)
def load_pretrain_params(self, exe, pretrain, prog, place):
......
......@@ -29,24 +29,24 @@ def load_pretrain_params_from_file(exe, prog, pretrained_file, place):
However, the conv_weights of Nonlocal model is not the same as that in ResNet50/101 because the
input shape is [N, C, T, H, W] and the convolution kernels' shape is [Cout, Cin, Kt, Kh, Kw]. It is
different from the convolution kernels of ResNet whose shape is typically [Cout, Cin, Kh, Kw].
When loading conv_weights from the pretrained file, shape mismatch error will be raised due to the check
in fluid.io. This check on params' shape is newly added in fluid.version==1.6.0. So it is recommendated to
treat conv_weights specifically.
So it is recommendated to treat conv_weights specifically.
The process is as following:
1, check the params that will be loaded, those with the same name in the target program and pretrained_file.
These params will be called common params in this function.
2, Create presistable variables in the new_scope with the name of each common params. If it is the weights of
convolution, the created varibale's shape will be set to 2D-convolution-kernel type.
3, load params from the pretrained_file into those persistable variables created in the new_scope
4, get the value of common params in the new_scope and transform it if it belongs to conv weights.
5, set the value to params in the target program
2, load params from the pretrained_file into a state dict, named file_state_dict in this method.
3, get the value of common params in the file_state_dict, treat the convolution parameters specifically.
4, set the value to params in the target program
"""
logger.info('load pretrained params from {}'.format(pretrained_file))
if os.path.isdir(pretrained_file):
# get params' list in prog
param_list = filter(is_parameter, prog.list_vars())
param_name_list = [p.name for p in param_list]
param_name_list = []
param_shape_dict = {}
for p in param_list:
param_name_list.append(p.name)
param_shape_dict[p.name] = p.shape
# get all params' names in pretrained_file
param_name_from_file = os.listdir(pretrained_file)
......@@ -54,77 +54,23 @@ def load_pretrain_params_from_file(exe, prog, pretrained_file, place):
# only those common params will be loaded from pretrained_file into prog
common_names = get_common_names(param_name_list, param_name_from_file)
# get global scope and block for prog
global_scope = fluid.global_scope()
global_block = prog.global_block()
# save details of common params
common_var_map = {}
for name in common_names:
var = global_block.var(name)
var_type = var.type
var_dtype = var.dtype
var_shape = var.shape
if len(var_shape) == 5:
# When param is conv_weights, its shape is [Cout, Cin, Kt, Kh, Kw].
# The corresponding params in ResNet50/101 is [Cout, Cin, Kh, Kw]
var_shape2d = (var_shape[0], var_shape[1], var_shape[3],
var_shape[4])
else:
var_shape2d = var_shape[:]
common_var_map[name] = [var_type, var_dtype, var_shape, var_shape2d]
# create new_scope and new_prog to create vars
cpu_place = fluid.CPUPlace()
exe_cpu = fluid.Executor(cpu_place)
new_scope = fluid.Scope()
new_prog = fluid.Program()
new_start_prog = fluid.Program()
new_block = new_prog.global_block()
# create vars in new_scope
created_vars = []
with fluid.scope_guard(new_scope):
with fluid.program_guard(new_prog, new_start_prog):
for name in common_names:
var_type, var_dtype, var_shape, var_shape2d = common_var_map[
name]
new_var = new_block.create_var(
name=name,
type=var_type,
shape=var_shape2d,
dtype=var_dtype,
persistable=True)
created_vars.append(new_var)
# load pretrained_file into the persistable vars created in new_scope
with fluid.scope_guard(new_scope):
fluid.io.load_vars(
exe_cpu,
pretrained_file,
main_program=new_prog,
vars=created_vars)
logger.info('-------- loading params -----------')
file_state_dict = fluid.load_program_state(pretrained_file)
pretrain_state_dict = {}
for name in common_names:
# get the tensor of vars in new_scope
new_tensor = new_scope.var(name).get_tensor()
new_value = np.array(new_tensor)
prog_tensor = global_scope.var(name).get_tensor()
var_type, var_dtype, var_shape, var_shape2d = common_var_map[name]
# set the value of loaded vars to those with the same name in the target program
if len(var_shape) == 5:
common_array = file_state_dict[name]
param_shape = param_shape_dict[name]
if len(param_shape) == 5:
# transform the loaded conv weights into the format of [Cout, Cin, Kt, Kh, Kw]
num_inflate = var_shape[2]
stacked_array = np.stack(
[new_value] * num_inflate, axis=2) / float(num_inflate)
prog_tensor.set(stacked_array.astype('float32'), place)
num_inflate = param_shape[2]
pretrain_state_dict[name] = np.stack(
[common_array] * num_inflate, axis=2) / float(num_inflate)
logger.info("load inflated({}) param {}".format(num_inflate,
name))
else:
prog_tensor.set(new_value, place)
pretrain_state_dict[name] = common_array
logger.info("load param {}".format(name))
fluid.set_program_state(prog, pretrain_state_dict)
else:
raise TypeError(
"pretrained file {} is not in a directory, not suitable to load params".
......@@ -165,78 +111,23 @@ def load_weights_params_from_file(exe, prog, weights, place):
This check on params' shape is newly added in fluid.version==1.6.0. So it is recommendated to
treat pred_w specifically.
The process is as following:
1, get the details of param_list in the target program (prog)
2, create persistable vars in new_scope with the same name as those in param_list with
the details stored in step 1. If the name is 'pred_w', the var shape should be [Cin, Cout].
3, get the value of vars in the new_scope.
If var.name is 'pred_w', transform it from fc-weights type to be consistent with convolution.
4, set the value to params in prog
1, load the parmas from weights file into a state_dict
2, specifically treat the paramter named "pred_w" from the foramt of fc into convolution
3, set the state_dict to prog
"""
logger.info('Load test weights from {}'.format(weights))
# get the param_list in prog
prog_vars = filter(is_parameter, prog.list_vars())
# save the details of params in prog
var_map = {}
for var in prog_vars:
var_name = var.name
var_type = var.type
var_dtype = var.dtype
var_shape = var.shape
# For pred_w, get the fc-weights type shape
if var_name == "pred_w":
assert len(
var_shape
) == 5, "pred_weights.shape shoud be [Cout, Cin, 1, 1, 1] when test"
var_shape = (var_shape[1], var_shape[0])
var_map[var_name] = [var_type, var_dtype, var_shape]
# create new_scope and new_prog
cpu_place = fluid.CPUPlace()
exe_cpu = fluid.Executor(cpu_place)
new_scope = fluid.Scope()
new_prog = fluid.Program()
new_start_prog = fluid.Program()
new_block = new_prog.global_block()
created_vars = []
# create persistable variables in new_scope
with fluid.scope_guard(new_scope):
with fluid.program_guard(new_prog, new_start_prog):
for var_name in var_map.keys():
var_type, var_dtype, var_shape = var_map[var_name]
new_var = new_block.create_var(
name=var_name,
type=var_type,
shape=var_shape,
dtype=var_dtype,
persistable=True)
created_vars.append(new_var)
# load params from file into the above vars created in new_scope
with fluid.scope_guard(new_scope):
fluid.io.load_vars(
exe_cpu,
'',
main_program=new_prog,
vars=created_vars,
filename=weights)
# get the global scope of prog
global_scope = fluid.global_scope()
# set value of vars in new_scope to the params of prog with the same name
# and specially treat on "pred_w"
for var_name in var_map.keys():
global_tensor = global_scope.var(var_name).get_tensor()
new_tensor = new_scope.var(var_name).get_tensor()
new_value = np.array(new_tensor)
if var_name != "pred_w":
global_tensor.set(new_value, place)
else:
pred_array = np.transpose(new_value, (1, 0))
pred_array = np.reshape(
pred_array,
[pred_array.shape[0], pred_array.shape[1], 1, 1, 1])
global_tensor.set(pred_array.astype('float32'), place)
prog_vars = list(filter(is_parameter, prog.list_vars()))
if weights[-9:] == '.pdparams':
weights = weights[:-9]
state_dict = fluid.load_program_state(weights, var_list=prog_vars)
pred_array = state_dict["pred_w"]
pred_array = np.transpose(pred_array, (1, 0))
pred_array = np.reshape(
pred_array, [pred_array.shape[0], pred_array.shape[1], 1, 1, 1])
state_dict["pred_w"] = pred_array
fluid.set_program_state(prog, state_dict)
......@@ -44,7 +44,7 @@ StNet的训练数据采用由DeepMind公布的Kinetics-400动作识别数据集
- 从头开始训练,需要加载在ImageNet上训练的ResNet50权重作为初始化参数,请下载此[模型参数](https://paddlemodels.bj.bcebos.com/video_classification/ResNet50_pretrained.tar.gz)并解压,将上面启动命令行或者run.sh脚本中的`pretrain`参数设置为解压之后的模型参数存放路径。如果没有手动下载并设置`pretrain`参数,则程序会自动下载并将参数保存在~/.paddle/weights/ResNet50\_pretrained目录下面
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/STNET_final.pdparams)通过`--resume`指定权重存放路径进行finetune等开发
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/STNET.pdparams)通过`--resume`指定权重存放路径进行finetune等开发
**数据读取器说明:** 模型读取Kinetics-400数据集中的`mp4`数据,每条数据抽取`seg_num`段,每段抽取`seg_len`帧图像,对每帧图像做随机增强后,缩放至`target_size`
......@@ -70,7 +70,7 @@ StNet的训练数据采用由DeepMind公布的Kinetics-400动作识别数据集
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要评估的权重。
- 若未指定`weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/STNET_final.pdparams)进行评估
- 若未指定`weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/STNET.pdparams)进行评估
- 评估结果以log的形式直接打印输出TOP1\_ACC、TOP5\_ACC等精度指标
......@@ -110,7 +110,7 @@ StNet的训练数据采用由DeepMind公布的Kinetics-400动作识别数据集
- 如果video\_path为'', 则忽略掉此参数。如果video\_path != '',则程序会对video\_path指定的视频文件进行预测,而忽略掉filelist的值,预测结果为此视频的分类概率。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/STNET_final.pdparams)进行推断
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/STNET.pdparams)进行推断
- 模型推断结果以log的形式直接打印输出,可以看到测试样本的分类预测概率。
......
......@@ -143,81 +143,40 @@ class STNET(ModelBase):
def weights_info(self):
return (
'STNET_final.pdparams',
'https://paddlemodels.bj.bcebos.com/video_classification/STNET_final.pdparams'
'STNET.pdparams',
'https://paddlemodels.bj.bcebos.com/video_classification/STNET.pdparams'
)
def load_pretrain_params(self, exe, pretrain, prog, place):
"""
The pretrained params are ResNet50 pretrained on ImageNet.
However, conv1_weights of StNet is not the same as that in ResNet50 because the input are super-image
concatanated by a series of images. When loading conv1_weights from the pretrained file, shape
mismatch error will be raised due to the check in fluid.io. This check on params' shape is newly
added in fluid.version==1.6.0. So it is recommendated to treat conv1_weights specifically.
However, conv1_weights' shape of StNet is not the same as that in ResNet50 because the input are super-image
concatanated by a series of images. So it is recommendated to treat conv1_weights specifically.
The process is as following:
1, load params except conv1_weights from pretrain
2, create var named 'conv1_weights' in new_scope, and load the value from the pretrain file
3, get the value of conv1_weights in the new_scope and transform it
4, set the transformed value to conv1_weights in prog
1, load params from pretrain
2, get the value of conv1_weights in the state_dict and transform it
3, set the transformed value to conv1_weights in prog
"""
def is_parameter(var):
if isinstance(var, fluid.framework.Parameter):
return isinstance(var, fluid.framework.Parameter) and (not ("fc_0" in var.name)) \
and (not ("batch_norm" in var.name)) and (not ("xception" in var.name)) \
and (not ("conv3d" in var.name)) and (not ("conv1_weights") in var.name)
logger.info(
"Load pretrain weights from {}, exclude conv1, fc, batch_norm, xception, conv3d layers.".
"Load pretrain weights from {}, exclude fc, batch_norm, xception, conv3d layers.".
format(pretrain))
# loaded params from pretrained file exclued conv1, fc, batch_norm, xception, conv3d
prog_vars = filter(is_parameter, prog.list_vars())
fluid.io.load_vars(exe, pretrain, vars=prog_vars, main_program=prog)
# get global scope and conv1_weights' details
global_scope = fluid.global_scope()
global_block = prog.global_block()
conv1_weights_name = "conv1_weights"
var_conv1_weights = global_block.var(conv1_weights_name)
tensor_conv1_weights = global_scope.var(conv1_weights_name).get_tensor()
var_type = var_conv1_weights.type
var_dtype = var_conv1_weights.dtype
var_shape = var_conv1_weights.shape
assert var_shape[
1] == 3 * self.seglen, "conv1_weights.shape[1] shoud be 3 x seglen({})".format(
self.seglen)
# transform shape to be consistent with conv1_weights of ResNet50
var_shape = (var_shape[0], 3, var_shape[2], var_shape[3])
# create new_scope and new_prog to create var with transformed shape
cpu_place = fluid.CPUPlace()
exe_cpu = fluid.Executor(cpu_place)
new_scope = fluid.Scope()
new_prog = fluid.Program()
new_start_prog = fluid.Program()
new_block = new_prog.global_block()
with fluid.scope_guard(new_scope):
with fluid.program_guard(new_prog, new_start_prog):
new_var = new_block.create_var(
name=conv1_weights_name,
type=var_type,
shape=var_shape,
dtype=var_dtype,
persistable=True)
# load conv1_weights from pretrain file into the var created in new_scope
with fluid.scope_guard(new_scope):
fluid.io.load_vars(
exe_cpu, pretrain, main_program=new_prog, vars=[new_var])
# get the valued of loaded conv1_weights, and transform it
new_tensor = new_scope.var(conv1_weights_name).get_tensor()
new_value = np.array(new_tensor)
param_numpy = np.mean(new_value, axis=1, keepdims=True) / self.seglen
param_numpy = np.repeat(param_numpy, 3 * self.seglen, axis=1)
# set the value of conv1_weights in the original program
tensor_conv1_weights.set(param_numpy.astype(np.float32), place)
# All the expected pretrained params are set to prog now
state_dict = fluid.load_program_state(pretrain)
dict_keys = list(state_dict.keys())
for name in dict_keys:
if ("batch_norm" in name) or ("fc_0" in name) or ("batch_norm" in name) \
or ("xception" in name) or ("conv3d" in name):
del state_dict[name]
logger.info(
'Delete {} from pretrained parameters. Do not load it'.
format(name))
conv1_weights = state_dict["conv1_weights"]
conv1_weights = np.mean(
conv1_weights, axis=1, keepdims=True) / self.seglen
conv1_weights = np.repeat(conv1_weights, 3 * self.seglen, axis=1)
logger.info(
'conv1_weights is transformed from [Cout, 3, Kh, Kw] into [Cout, 3*seglen, Kh, Kw]'
)
state_dict["conv1_weights"] = conv1_weights
fluid.set_program_state(prog, state_dict)
......@@ -42,7 +42,7 @@ TALL的训练数据采用TACoS数据集,数据下载及准备请参考[数据
- 从头开始训练,使用上述启动命令行或者脚本程序即可启动训练,不需要用到预训练模型
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_grounding/TALL_final.pdparams)通过`--resume`指定权重存放路径进行finetune等开发
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_grounding/TALL.pdparams)通过`--resume`指定权重存放路径进行finetune等开发
- 模型未设置验证集,故将valid\_interval设为10000,在训练过程中不进行验证。
......@@ -66,7 +66,7 @@ TALL的训练数据采用TACoS数据集,数据下载及准备请参考[数据
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要评估的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_grounding/TALL_final.pdparams)进行评估
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_grounding/TALL.pdparams)进行评估
- 运行上述程序会将测试结果打印出来,同时保存在json文件中,默认存储在data/evaluate\_results目录下。
......@@ -97,7 +97,7 @@ TALL的训练数据采用TACoS数据集,数据下载及准备请参考[数据
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要用到的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_grounding/TALL_final.pdparams)进行推断
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_grounding/TALL.pdparams)进行推断
- 模型推断结果存储于json文件中,默认存储在`data/dataset/predict_results`目录下。
......
......@@ -163,6 +163,5 @@ class TALL(ModelBase):
def weights_info(self):
return (
'TALL_final.pdparams',
'https://paddlemodels.bj.bcebos.com/video_grounding/TALL_final.pdparams'
)
'TALL.pdparams',
'https://paddlemodels.bj.bcebos.com/video_grounding/TALL.pdparams')
......@@ -51,7 +51,7 @@ TSM的训练数据采用由DeepMind公布的Kinetics-400动作识别数据集。
- 从头开始训练,需要加载在ImageNet上训练的ResNet50权重作为初始化参数,请下载此[模型参数](https://paddlemodels.bj.bcebos.com/video_classification/ResNet50_pretrained.tar.gz)并解压,将上面启动命令行或者run.sh脚本中的`pretrain`参数设置为解压之后的模型参数存放路径。如果没有手动下载并设置`pretrain`参数,则程序会自动下载并将参数保存在~/.paddle/weights/ResNet50\_pretrained目录下面
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/TSM_final.pdparams)通过`--resume`指定权重存放路径进行finetune等开发
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/TSM.pdparams)通过`--resume`指定权重存放路径进行finetune等开发
**数据读取器说明:** 模型读取Kinetics-400数据集中的`mp4`数据,每条数据抽取`seg_num`段,每段抽取1帧图像,对每帧图像做随机增强后,缩放至`target_size`
......@@ -74,7 +74,7 @@ TSM的训练数据采用由DeepMind公布的Kinetics-400动作识别数据集。
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要评估的权重。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/TSM_final.pdparams)进行评估
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/TSM.pdparams)进行评估
- 评估结果以log的形式直接打印输出TOP1\_ACC、TOP5\_ACC等精度指标
......@@ -104,7 +104,7 @@ TSM的训练数据采用由DeepMind公布的Kinetics-400动作识别数据集。
- 如果video\_path为'', 则忽略掉此参数。如果video\_path != '',则程序会对video\_path指定的视频文件进行预测,而忽略掉filelist的值,预测结果为此视频的分类概率。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/TSM_final.pdparams)进行推断
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/TSM.pdparams)进行推断
- 模型推断结果以log的形式直接打印输出,可以看到测试样本的分类预测概率。
......
......@@ -137,8 +137,8 @@ class TSM(ModelBase):
def weights_info(self):
return (
'TSM_final.pdparams',
'https://paddlemodels.bj.bcebos.com/video_classification/TSM_final.pdparams'
'TSM.pdparams',
'https://paddlemodels.bj.bcebos.com/video_classification/TSM.pdparams'
)
def load_pretrain_params(self, exe, pretrain, prog, place):
......@@ -148,5 +148,12 @@ class TSM(ModelBase):
logger.info("Load pretrain weights from {}, exclude fc layer.".format(
pretrain))
vars = filter(is_parameter, prog.list_vars())
fluid.io.load_vars(exe, pretrain, vars=vars, main_program=prog)
state_dict = fluid.load_program_state(pretrain)
dict_keys = list(state_dict.keys())
for name in dict_keys:
if "fc_0" in name:
del state_dict[name]
print('Delete {} from pretrained parameters. Do not load it'.
format(name))
fluid.set_program_state(prog, state_dict)
......@@ -43,7 +43,7 @@ TSN的训练数据采用由DeepMind公布的Kinetics-400动作识别数据集。
- 从头开始训练,需要加载在ImageNet上训练的ResNet50权重作为初始化参数,请下载此[模型参数](https://paddlemodels.bj.bcebos.com/video_classification/ResNet50_pretrained.tar.gz)并解压,将上面启动命令行或者run.sh脚本中的`pretrain`参数设置为解压之后的模型参数
存放路径。如果没有手动下载并设置`pretrain`参数,则程序会自动下载并将参数保存在~/.paddle/weights/ResNet50\_pretrained目录下面
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/TSN_final.pdparams)通过`--resume`指定权重存
- 可下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/TSN.pdparams)通过`--resume`指定权重存
放路径进行finetune等开发
**数据读取器说明:** 模型读取Kinetics-400数据集中的`mp4`数据,每条数据抽取`seg_num`段,每段抽取1帧图像,对每帧图像做随机增强后,缩放至`target_size`
......@@ -68,7 +68,7 @@ TSN的训练数据采用由DeepMind公布的Kinetics-400动作识别数据集。
- 使用`run.sh`进行评估时,需要修改脚本中的`weights`参数指定需要评估的权重
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/TSN_final.pdparams)进行评估
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/TSN.pdparams)进行评估
- 评估结果以log的形式直接打印输出TOP1\_ACC、TOP5\_ACC等精度指标
......@@ -100,7 +100,7 @@ TSN的训练数据采用由DeepMind公布的Kinetics-400动作识别数据集。
- 如果video\_path为'', 则忽略掉此参数。如果video\_path != '',则程序会对video\_path指定的视频文件进行预测,而忽略掉filelist的值,预测结果为此视频的分类概率。
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/TSN_final.pdparams)进行推断
- 若未指定`--weights`参数,脚本会下载已发布模型[model](https://paddlemodels.bj.bcebos.com/video_classification/TSN.pdparams)进行推断
- 模型推断结果以log的形式直接打印输出,可以看到测试样本的分类预测概率。
......
......@@ -147,8 +147,8 @@ class TSN(ModelBase):
def weights_info(self):
return (
'TSN_final.pdparams',
'https://paddlemodels.bj.bcebos.com/video_classification/TSN_final.pdparams'
'TSN.pdparams',
'https://paddlemodels.bj.bcebos.com/video_classification/TSN.pdparams'
)
def load_pretrain_params(self, exe, pretrain, prog, place):
......@@ -158,5 +158,12 @@ class TSN(ModelBase):
logger.info("Load pretrain weights from {}, exclude fc layer.".format(
pretrain))
vars = filter(is_parameter, prog.list_vars())
fluid.io.load_vars(exe, pretrain, vars=vars, main_program=prog)
state_dict = fluid.load_program_state(pretrain)
dict_keys = list(state_dict.keys())
for name in dict_keys:
if "fc_0" in name:
del state_dict[name]
print('Delete {} from pretrained parameters. Do not load it'.
format(name))
fluid.set_program_state(prog, state_dict)
......@@ -142,8 +142,6 @@ def train(args):
train_feeds = train_model.feeds()
train_fetch_list = train_model.fetches()
train_loss = train_fetch_list[0]
for item in train_fetch_list:
item.persistable = True
optimizer = train_model.optimizer()
optimizer.minimize(train_loss)
train_dataloader = train_model.dataloader()
......@@ -156,8 +154,6 @@ def train(args):
valid_feeds = valid_model.feeds()
valid_fetch_list = valid_model.fetches()
valid_dataloader = valid_model.dataloader()
for item in valid_fetch_list:
item.persistable = True
place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)
......@@ -165,11 +161,9 @@ def train(args):
if args.resume:
# if resume weights is given, load resume weights directly
assert os.path.exists(args.resume), \
"Given resume weight dir {} not exist.".format(args.resume)
fluid.io.load_persistables(
exe, '', main_program=train_prog, filename=args.resume)
assert os.path.exists(args.resume + '.pdparams'), \
"Given resume weight dir {}.pdparams not exist.".format(args.resume)
fluid.load(train_prog, model_path=args.resume, executor=exe)
else:
# if not in resume mode, load pretrain weights
if args.pretrain:
......@@ -234,7 +228,7 @@ def train(args):
train_with_dataloader(
exe,
train_prog,
compiled_train_prog, #train_exe,
compiled_train_prog,
train_dataloader,
train_fetch_list,
train_metrics,
......@@ -244,7 +238,7 @@ def train(args):
save_dir=args.save_dir,
save_model_name=args.model_name,
fix_random_seed=args.fix_random_seed,
compiled_test_prog=compiled_valid_prog, #test_exe=valid_exe,
compiled_test_prog=compiled_valid_prog,
test_dataloader=valid_dataloader,
test_fetch_list=valid_fetch_list,
test_metrics=valid_metrics,
......
......@@ -100,7 +100,7 @@ def train_with_dataloader(exe, train_prog, compiled_train_prog, train_dataloader
train_metrics.calculate_and_log_out(train_outs, \
info = '[TRAIN] Epoch {}, iter {} '.format(epoch, train_iter))
train_iter += 1
# NOTE: profiler tools, used for benchmark
if is_profiler and epoch == 0 and train_iter == log_interval:
profiler.start_profiler("All")
......@@ -115,40 +115,15 @@ def train_with_dataloader(exe, train_prog, compiled_train_prog, train_dataloader
logger.info('[TRAIN] Epoch {} training finished, average time: {}'.
format(epoch, np.mean(epoch_periods[1:])))
save_model(
exe,
train_prog,
save_dir,
save_model_name,
"_epoch{}".format(epoch),
save_type='.pdckpt')
save_model(
exe,
train_prog,
save_dir,
save_model_name,
"_epoch{}".format(epoch),
save_type='.pdparams')
save_model(exe, train_prog, save_dir, save_model_name,
"_epoch{}".format(epoch))
if compiled_test_prog and valid_interval > 0 and (
epoch + 1) % valid_interval == 0:
test_with_dataloader(exe, compiled_test_prog, test_dataloader,
test_fetch_list, test_metrics, log_interval,
save_model_name)
save_model(
exe,
train_prog,
save_dir,
save_model_name,
'_final',
save_type='.pdckpt')
save_model(
exe,
train_prog,
save_dir,
save_model_name,
'_final',
save_type='.pdparams')
save_model(exe, train_prog, save_dir, save_model_name)
#when fix_random seed for debug
if fix_random_seed:
cards = os.environ.get('CUDA_VISIBLE_DEVICES')
......@@ -158,28 +133,12 @@ def train_with_dataloader(exe, train_prog, compiled_train_prog, train_dataloader
np.mean(epoch_periods)))
def save_model(exe,
program,
save_dir,
model_name,
postfix=None,
save_type='.pdckpt'):
"""
save_type: '.pdckpt' or '.pdparams', '.pdckpt' for all persistable variables,
'.pdparams' for parameters only
"""
def save_model(exe, program, save_dir, model_name, postfix=''):
"""save paramters and optimizer related varaibles"""
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
saved_model_name = model_name + postfix + save_type
if save_type == '.pdckpt':
fluid.io.save_persistables(
exe, save_dir, main_program=program, filename=saved_model_name)
elif save_type == '.pdparams':
fluid.io.save_params(
exe, save_dir, main_program=program, filename=saved_model_name)
else:
raise NotImplementedError(
'save_type {} not implemented, it should be either {} or {}'
.format(save_type, '.pdckpt', '.pdparams'))
saved_model_name = model_name + postfix
fluid.save(program, os.path.join(save_dir, saved_model_name))
return
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册