# PyTorch Template Project
PyTorch deep learning project made easy.
<!-- @import "[TOC]" {cmd="toc" depthFrom=1 depthTo=6 orderedList=false} -->
<!-- code_chunk_output -->
* [PyTorch Template Project](#pytorch-template-project)
* [Requirements](#requirements)
* [Features](#features)
* [Folder Structure](#folder-structure)
* [Usage](#usage)
* [Config file format](#config-file-format)
* [Using config files](#using-config-files)
* [Resuming from checkpoints](#resuming-from-checkpoints)
* [Using Multiple GPU](#using-multiple-gpu)
* [Customization](#customization)
* [Custom CLI options](#custom-cli-options)
* [Data Loader](#data-loader)
* [Trainer](#trainer)
* [Model](#model)
* [Loss](#loss)
* [metrics](#metrics)
* [Additional logging](#additional-logging)
* [Validation data](#validation-data)
* [Checkpoints](#checkpoints)
* [Tensorboard Visualization](#tensorboard-visualization)
* [Contribution](#contribution)
* [TODOs](#todos)
* [License](#license)
* [Acknowledgements](#acknowledgements)
<!-- /code_chunk_output -->
## modification notes
* put the data in the same dir to source code.
```
images_test_A images_train_5s pytorch-template
```
* modify config.json to change parameters. Some parameters are not used
* steps:
* train with validation set: in config.json, stage is "train", command: python train.py -c config.json
* train with all train data: keep the parameters the same, change "stage" in config.json to "all", run the same command:
* test: command python test.py -c config.json -r /path/to/you best model parameters
* prediction result can be find in saved/model/G2/[timestamps]/prediction.json
## Requirements
* Python >= 3.5 (3.6 recommended)
* PyTorch >= 0.4 (1.2 recommended)
* tqdm (Optional for `test.py`)
* tensorboard >= 1.14 (see [Tensorboard Visualization](#tensorboard-visualization))
## Features
* Clear folder structure which is suitable for many deep learning projects.
* `.json` config file support for convenient parameter tuning.
* Customizable command line options for more convenient parameter tuning.
* Checkpoint saving and resuming.
* Abstract base classes for faster development:
* `BaseTrainer` handles checkpoint saving/resuming, training process logging, and more.
* `BaseDataLoader` handles batch generation, data shuffling, and validation data splitting.
* `BaseModel` provides basic model summary.
## Folder Structure
```
folder structure/
│
├── train.py - main script to start training
├── test.py - evaluation of trained model
│
├── config.json - holds configuration for training
├── parse_config.py - class to handle config file and cli options
│
├── new_project.py - initialize new project with template files
│
├── base/ - abstract base classes
│ ├── base_data_loader.py
│ ├── base_model.py
│ └── base_trainer.py
│
├── data_loader/ - anything about data loading goes here
│ ├── data_loaders.py
│ └── g2dataset.py/ - customized class for G2 dataset and transforms
│
├── model/ - models, losses, and metrics
│ ├── loss.py
│ ├── metric.py - accuracy and score
│ ├── model.py
│ ├── resnet.py - models used for G2
│ ├── scheduler.py - learning rate scheduler
│ └── utils.py
│
├── saved/
│ ├── models/ - trained models are saved here
│ └── log/ - default logdir for tensorboard and logging output
│
├── trainer/ - trainers
│ └── trainer.py
│
├── logger/ - module for tensorboard visualization and logging
│ ├── visualization.py
│ ├── logger.py
│ └── logger_config.json
│
└── utils/ - small utility functions
├── util.py
└── ...
```
## Usage
The code in this repo is an MNIST example of the template.
Try `python train.py -c config.json` to run code.
### Config file format
Config files are in `.json` format:
```javascript
{
"name": "Mnist_LeNet", // training session name
"n_gpu": 1, // number of GPUs to use for training.
"arch": {
"type": "MnistModel", // name of model architecture to train
"args": {
}
},
"data_loader": {
"type": "MnistDataLoader", // selecting data loader
"args":{
"data_dir": "data/", // dataset path
"batch_size": 64, // batch size
"shuffle": true, // shuffle training data before splitting
"validation_split": 0.1 // size of validation dataset. float(portion) or int(number of samples)
"num_workers": 2, // number of cpu processes to be used for data loading
}
},
"optimizer": {
"type": "Adam",
"args":{
"lr": 0.001, // learning rate
"weight_decay": 0, // (optional) weight decay
"amsgrad": true
}
},
"loss": "nll_loss", // loss
"metrics": [
"accuracy", "top_k_acc" // list of metrics to evaluate
],
"lr_scheduler": {
"type": "StepLR", // learning rate scheduler
"args":{
"step_size": 50,
"gamma": 0.1
}
},
"trainer": {
"epochs": 100, // number of training epochs
"save_dir": "saved/", // checkpoints are saved in save_dir/models/name
"save_freq": 1, // save checkpoints every save_freq epochs
"verbosity": 2, // 0: quiet, 1: per epoch, 2: full
"monitor": "min val_loss" // mode and metric for model performance monitoring. set 'off' to disable.
"early_stop": 10 // number of epochs to wait before early stop. set 0 to disable.
"tensorboard": true, // enable tensorboard visualization
}
}
```
Add addional configurations if you need.
### Using config files
Modify the configurations in `.json` config files, then run:
```
python train.py --config config.json
```
### Resuming from checkpoints
You can resume from a previously saved checkpoint by:
```
python train.py --resume path/to/checkpoint
```
### Using Multiple GPU
You can enable multi-GPU training by setting `n_gpu` argument of the config file to larger number.
If configured to use smaller number of gpu than available, first n devices will be used by default.
Specify indices of available GPUs by cuda environmental variable.
```
python train.py --device 2,3 -c config.json
```
This is equivalent to
```
CUDA_VISIBLE_DEVICES=2,3 python train.py -c config.py
```
## Customization
### Project initialization
Use the `new_project.py` script to make your new project directory with template files.
`python new_project.py ../NewProject` then a new project folder named 'NewProject' will be made.
This script will filter out unneccessary files like cache, git files or readme file.
### Custom CLI options
Changing values of config file is a clean, safe and easy way of tuning hyperparameters. However, sometimes
it is better to have command line options if some values need to be changed too often or quickly.
This template uses the configurations stored in the json file by default, but by registering custom options as follows
you can change some of them using CLI flags.
```python
# simple class-like object having 3 attributes, `flags`, `type`, `target`.
CustomArgs = collections.namedtuple('CustomArgs', 'flags type target')
options = [
CustomArgs(['--lr', '--learning_rate'], type=float, target=('optimizer', 'args', 'lr')),
CustomArgs(['--bs', '--batch_size'], type=int, target=('data_loader', 'args', 'batch_size'))
# options added here can be modified by command line flags.
]
```
`target` argument should be s
没有合适的资源?快使用搜索试试~ 我知道了~
G2比赛代码.zip
共119个文件
pyc:45个
py:34个
json:31个
0 下载量 145 浏览量
2023-08-24
16:52:23
上传
评论
收藏 457KB ZIP 举报
温馨提示
G2比赛代码.zip
资源推荐
资源详情
资源评论
收起资源包目录
G2比赛代码.zip (119个子文件)
.gitignore 47B
G2codev2.iml 474B
prediction_kfold801B.json 181KB
prediction_kfold802B2.json 181KB
prediction_kfold802B.json 181KB
prediction_kfoldB731.json 181KB
prediction_kfold806B.json 179KB
prediction_kfold805B.json 179KB
prediction_kfold801A.json 45KB
prediction_kfoldA731.json 45KB
prediction_kfold802A2.json 45KB
prediction_kfold802A.json 45KB
prediction_kfold806A.json 45KB
prediction_kfold805A.json 45KB
pseudo802_2B.json 15KB
pseudo802B.json 15KB
pseudo801B.json 12KB
pseudo805B.json 11KB
pseudo807B.json 11KB
pseudo806B.json 11KB
pseudoB.json 11KB
pseudo802_2A.json 3KB
pseudo802A.json 3KB
pseudo.json 3KB
pseudo801A.json 3KB
pseudoA.json 3KB
pseudo805A.json 2KB
pseudo806A.json 2KB
pseudo807A.json 2KB
config.json 1KB
logger_config.json 869B
zmx.json 60B
config.json 43B
LICENSE 1KB
README.md 14KB
resnet.py 15KB
test.py 7KB
base_trainer.py 7KB
parse_config.py 6KB
parse_config.py 6KB
trainer.py 6KB
train.py 5KB
g2dataset.py 5KB
scheduler.py 4KB
visualization.py 3KB
util.py 2KB
1.py 2KB
base_data_loader.py 2KB
test1.py 1KB
manuscript1.py 1KB
loss.py 1KB
metric.py 933B
manuscript2.py 857B
model.py 828B
new_project.py 810B
pseudo.py 778B
logger.py 750B
logger.py 750B
test3.py 738B
data_loaders.py 658B
base_model.py 648B
tow.py 474B
test2.py 453B
utils.py 154B
__init__.py 86B
__init__.py 50B
__init__.py 23B
__init__.py 20B
__init__.py 0B
resnet.cpython-36.pyc 12KB
resnet.cpython-38.pyc 12KB
parse_config.cpython-38.pyc 6KB
parse_config.cpython-36.pyc 6KB
parse_config.cpython-38.pyc 6KB
base_trainer.cpython-38.pyc 5KB
trainer.cpython-38.pyc 4KB
g2dataset.cpython-38.pyc 4KB
base_trainer.cpython-36.pyc 4KB
trainer.cpython-36.pyc 4KB
g2dataset.cpython-36.pyc 3KB
scheduler.cpython-38.pyc 3KB
scheduler.cpython-36.pyc 3KB
util.cpython-38.pyc 3KB
util.cpython-36.pyc 3KB
visualization.cpython-38.pyc 3KB
visualization.cpython-36.pyc 3KB
loss.cpython-38.pyc 2KB
base_data_loader.cpython-38.pyc 2KB
base_data_loader.cpython-36.pyc 2KB
base_model.cpython-38.pyc 1KB
model.cpython-38.pyc 1KB
base_model.cpython-36.pyc 1KB
metric.cpython-38.pyc 1KB
model.cpython-36.pyc 1KB
metric.cpython-36.pyc 1KB
data_loaders.cpython-38.pyc 996B
data_loaders.cpython-36.pyc 993B
logger.cpython-38.pyc 822B
logger.cpython-36.pyc 817B
tow.cpython-38.pyc 799B
共 119 条
- 1
- 2
资源评论
白话机器学习
- 粉丝: 9738
- 资源: 7681
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功