# Swin-Transformer-Serve
Deploy Pre-trained [Swin-Transformer](https://github.com/microsoft/Swin-Transformer/) classifier trained on ImageNet 1K using [TorchServe](https://github.com/pytorch/serve)
### Create and activate virtual env
```bash
virtualenv env --python=python3
source env/bin/activate
```
### Install dependencies
```bash
cd project
# clone TorchServe
git clone https://github.com/pytorch/serve.git
cd serve
# Refer to readme in serve repo for CUDA enabled local installtion
# The instruction below is for CPU
python ./ts_scripts/install_dependencies.py
pip install torchserve torch-model-archiver
```
### Pretrained Weights and Config
#### Download weights.
| name | pretrain | resolution | acc@1 | acc@5 | 1K model |
|--------|--------------|------------|-------|-------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Swin-T | ImageNet-1K | 224x224 | 81.2 | 95.5 | [github](https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_tiny_patch4_window7_224.pth)/[baidu](https://pan.baidu.com/s/156nWJy4Q28rDlrX-rRbI3w) |
| Swin-S | ImageNet-1K | 224x224 | 83.2 | 96.2 | [github](https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_small_patch4_window7_224.pth)/[baidu](https://pan.baidu.com/s/1KFjpj3Efey3LmtE1QqPeQg) |
| Swin-B | ImageNet-1K | 224x224 | 83.5 | 96.5 | [github](https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_base_patch4_window7_224.pth)/[baidu](https://pan.baidu.com/s/16bqCTEc70nC_isSsgBSaqQ) |
| Swin-B | ImageNet-1K | 384x384 | 84.5 | 97.0 | [github](https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_base_patch4_window12_384.pth)/[baidu](https://pan.baidu.com/s/1xT1cu740-ejW7htUdVLnmw) |
| Swin-B | ImageNet-22K | 224x224 | 85.2 | 97.5 | [github](https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_base_patch4_window7_224_22kto1k.pth)/[baidu](https://pan.baidu.com/s/1n_wNkcbRxVXit8r_KrfAVg) |
| Swin-B | ImageNet-22K | 384x384 | 86.4 | 98.0 | [github](https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_base_patch4_window12_384_22kto1k.pth)/[baidu](https://pan.baidu.com/s/1caKTSdoLJYoi4WBcnmWuWg) |
| Swin-L | ImageNet-22K | 224x224 | 86.3 | 97.9 | [github](https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_large_patch4_window7_224_22kto1k.pth)/[baidu](https://pan.baidu.com/s/1NkQApMWUhxBGjk1ne6VqBQ) |
| Swin-L | ImageNet-22K | 384x384 | 87.3 | 98.2 | [github](https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_large_patch4_window12_384_22kto1k.pth)/[baidu](https://pan.baidu.com/s/1X0FLHQyPOC6Kmv2CmgxJvA) |
For demo we will use Tiny model.
```bash
wget https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_tiny_patch4_window7_224.pth -O weights/swin_tiny_patch4_window7_224.pth
```
Copy the corresonding model config file from to `swin_config.yaml`
```bash
cp configs/swin_tiny_patch4_window7_224.yaml swin_config.yaml
```
### TorchServe
Create MAR file using [torch-model-archiver](https://github.com/pytorch/serve/tree/master/model-archiver).
```bash
torch-model-archiver --model-name swin -v 1.0 --model-file swin_transformer.py --serialized-file weights/swin_tiny_patch4_window7_224.pth --handler swin_handler.py --extra-files index_to_name.json,swin_config.yaml --requirements-file requirements.txt
# move the swin.mar to model_store folder
mv swin.mar model_store
```
Start the `torchserve` using below cmd
in the config.properties we have set the configurations.[Refer](https://github.com/pytorch/serve/blob/master/docs/configuration.md)
```bash
torchserve --start --model-store model_store --models swin=swin.mar
```
test prediction using cURL
```bash
curl http://127.0.0.1:8080/predictions/swin -T kitten_small.jpg
```
```bash
{
"tabby": 0.44951513409614563,
"tiger_cat": 0.1962115466594696,
"lynx": 0.16013166308403015,
"Egyptian_cat": 0.08244507014751434,
"tiger": 0.015334611758589745
}
```
As we set in the configuration file only one worker will be created per model.
To increase workers and batch inference can done using [Management API](https://github.com/pytorch/serve/blob/master/docs/management_api.md)
#### Increase worker
```bash
curl -v -X PUT "http://localhost:8081/models/swin?min_worker=3"
```
This will increase minimum workers to 3.
#### Batch Inference.
[Reference](https://github.com/pytorch/serve/blob/master/docs/batch_inference_with_ts.md)
```bash
#if the model is already running dergister it first
curl -X DELETE "http://localhost:8081/models/swin/1.0"
# The following command will register a model "swin.mar" and configure TorchServe to use a batch_size of 8 and a max batch delay of 50 milli seconds.
curl -X POST "localhost:8081/models?url=swin.mar&batch_size=8&max_batch_delay=50"
```
Stop torchserve
```bash
torchserve --stop
```
## Model deploy using docker
Update `config.properties` file with following lines
```
install_py_dep_per_model=true
default_workers_per_model=1
```
Execute the below cmds to build a `cpu` docker image
```bash
cd serve/docker
sudo ./build_image.sh
```
`gpu` docker image
```bash
sudo ./build_image.sh -g -cv cu102
```
On successful docker build
##### CPU
```bash
cd Swin-Transformer-Serve
sudo docker run --rm -it -p 8080:8080 -p 8081:8081 -p 8082:8082 -p 7070:7070 -p 7071:7071 -v $(pwd)/model_store:/home/model-server/model-store pytorch/torchserve:latest-cpu
```
##### GPU
```bash
cd Swin-Transformer-Serve
sudo docker run --rm -it --gpus all -p 8080:8080 -p 8081:8081 -p 8082:8082 -p 7070:7070 -p 7071:7071 -v $(pwd)/model_store:/home/model-server/model-store pytorch/torchserve:latest-gpu
```
#### Register model
```bash
curl -X POST "localhost:8081/models?url=swin.mar&batch_size=1&max_batch_delay=50"
```
### Creating mar file for torchscript mode model
#### Generate serialized-file using TorchScript
```python
import torch
from swin_handler import get_config
from swin_transformer import SwinTransformer
config = get_config("swin_config.yaml")
model = SwinTransformer(img_size=config.DATA.IMG_SIZE,
patch_size=config.MODEL.SWIN.PATCH_SIZE,
in_chans=config.MODEL.SWIN.IN_CHANS,
num_classes=config.MODEL.NUM_CLASSES,
embed_dim=config.MODEL.SWIN.EMBED_DIM,
depths=config.MODEL.SWIN.DEPTHS,
num_heads=config.MODEL.SWIN.NUM_HEADS,
window_size=config.MODEL.SWIN.WINDOW_SIZE,
mlp_ratio=config.MODEL.SWIN.MLP_RATIO,
qkv_bias=config.MODEL.SWIN.QKV_BIAS,
qk_scale=config.MODEL.SWIN.QK_SCALE,
drop_rate=config.MODEL.DROP_RATE,
drop_path_rate=config.MODEL.DROP_PATH_RATE,
ape=config.MODEL.SWIN.APE,
patch_norm=config.MODEL.SWIN.PATCH_NORM,
use_checkpoint=config.TRAIN.USE_CHECKPOINT)
model.load_state_dict(torch.load("weights/swin_tiny_patch4_window7_224.pth",map_location="cpu")["model"])
model.eval()
example_input = torch.rand(1, 3, config.DATA.IMG_SIZE, config.DATA.IMG_SIZE)
traced_script_module = torch.jit.trace(model, example_input)
traced_script_module.save("swin.pt")
```
#### TorchServe
```bash
torch-model-archiver --model-name swin -v 1.0 --serialized-file weights/swin_tiny_patch4_window7_224.pth --handler swin_handler.py --extra-files index_to_name.json,swin_conf

__AtYou__
- 粉丝: 3515
- 资源: 2177
最新资源
- matlab实现四旋翼无人机自抗扰姿态容错控制-飞行器控制-四旋翼无人机-自抗扰控制-UAV-扰动识别-matlab
- 基于主从博弈的综合能源微网与共享储能优化运行研究:迭代算法探索与利益相关者的博弈策略,基于主从博弈的综合能源微网与共享储能优化运行研究:电价策略与用户行为互动,matlab 代码基于主从博弈的共享储能
- 14章RAG全栈技术从基础到精通 ,打造高精准AI应用
- MATLAB平台下的多目标粒子群算法在冷热电联供综合能源系统优化中的实践,基于多目标粒子群算法的冷热电联供综合能源系统优化调度模型,MATLAB代码:基于多目标粒子群算法冷热电联供综合能源系统运行优化
- 三相桥式全控整流及其有源逆变与Simulink仿真波形分析(含不同触发角和负载变化图),三相桥式全控整流及其有源逆变与Simulink仿真探究:触发角与负载变化下的波形图分析,三相桥式全控整流及其有源
- 中国智能制造装备产业发展机遇蓝皮书 2024.pdf
- 【毕业设计】Python的Django-html网易新闻+评论的舆情热点分析平台源码(完整前后端+mysql+说明文档+LW+PPT).zip
- 西门子1200 PLC与昆仑通态上位软件结合的新能源物料输送系统程序案例解析,V16+博图版本,RS485通讯控制托利多称重仪表模拟量读取技术实现,西门子S1200博图物料输送系统:新能源物料输送PL
- 【毕业设计】Python的Django-html图像的信息隐藏技术研究源码(完整前后端+mysql+说明文档+LW+PPT).zip
- 【毕业设计】Python的Django-html深度学习的车牌识别系统源码(完整前后端+mysql+说明文档+LW+PPT).zip
- 【毕业设计】Python的Django-html深度学习屋内烟雾检测方法源码(完整前后端+mysql+说明文档+LW+PPT).zip
- 灰狼算法在微电网优化调度中的应用:考虑风光储与需求响应的多元经济优化策略,模型可塑性强且出图丰富的实践研究,灰狼算法在微电网风光储需求响应优化调度中的应用:经济性优化与模型可塑性的研究分析图集,灰狼算
- 【毕业设计】Python的Django-html人脸识别的实验室智能门禁系统源码(完整前后端+mysql+说明文档+LW+PPT).zip
- 【毕业设计】Python的Django-html深度学习的身份证识别考勤系统源码(完整前后端+mysql+说明文档+LW+PPT).zip
- 【毕业设计】Python的Django-html时间序列分析的大气污染预测系统源码(完整前后端+mysql+说明文档+LW+PPT).zip
- matlab实现遗传算法在物流中心配送路径规划中的应用-遗传算法-路径规划-物流配送-最佳配送方案-matlab
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


