shreyasvaidya commited on
Commit
5eadefe
·
verified ·
1 Parent(s): 82ca572

Upload folder using huggingface_hub

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +2 -0
  2. .gitignore +176 -0
  3. CHANGELOG.md +19 -0
  4. Dockerfile +45 -0
  5. IndicPhotoOCR/__init__.py +0 -0
  6. IndicPhotoOCR/detection/__init__.py +0 -0
  7. IndicPhotoOCR/detection/east_config.py +39 -0
  8. IndicPhotoOCR/detection/east_detector.py +87 -0
  9. IndicPhotoOCR/detection/east_locality_aware_nms.py +75 -0
  10. IndicPhotoOCR/detection/east_model.py +242 -0
  11. IndicPhotoOCR/detection/east_preprossing.py +681 -0
  12. IndicPhotoOCR/detection/east_utils.py +283 -0
  13. IndicPhotoOCR/detection/textbpn/__init__.py +0 -0
  14. IndicPhotoOCR/detection/textbpn/cfglib/config.py +90 -0
  15. IndicPhotoOCR/detection/textbpn/cfglib/option.py +123 -0
  16. IndicPhotoOCR/detection/textbpn/models/TextBPN_resnet50_300.pth +3 -0
  17. IndicPhotoOCR/detection/textbpn/network/Reg_loss.py +196 -0
  18. IndicPhotoOCR/detection/textbpn/network/Seg_loss.py +107 -0
  19. IndicPhotoOCR/detection/textbpn/network/__init__.py +1 -0
  20. IndicPhotoOCR/detection/textbpn/network/backbone/__init__.py +1 -0
  21. IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/Makefile +6 -0
  22. IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/Makefile.sh +6 -0
  23. IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/__init__.py +13 -0
  24. IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/functions/__init__.py +0 -0
  25. IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/functions/deform_conv.py +181 -0
  26. IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/functions/deform_pool.py +69 -0
  27. IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/modules/__init__.py +0 -0
  28. IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/modules/deform_conv.py +157 -0
  29. IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/modules/deform_pool.py +172 -0
  30. IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/setup.py +19 -0
  31. IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/src/deform_conv_cuda.cpp +695 -0
  32. IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/src/deform_conv_cuda_kernel.cu +866 -0
  33. IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/src/deform_pool_cuda.cpp +87 -0
  34. IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/src/deform_pool_cuda_kernel.cu +364 -0
  35. IndicPhotoOCR/detection/textbpn/network/backbone/resnet.py +336 -0
  36. IndicPhotoOCR/detection/textbpn/network/backbone/vgg.py +60 -0
  37. IndicPhotoOCR/detection/textbpn/network/layers/Adaptive_Deformation.py +88 -0
  38. IndicPhotoOCR/detection/textbpn/network/layers/CircConv.py +91 -0
  39. IndicPhotoOCR/detection/textbpn/network/layers/GCN.py +77 -0
  40. IndicPhotoOCR/detection/textbpn/network/layers/GraphConv.py +45 -0
  41. IndicPhotoOCR/detection/textbpn/network/layers/RNN.py +35 -0
  42. IndicPhotoOCR/detection/textbpn/network/layers/Transformer.py +140 -0
  43. IndicPhotoOCR/detection/textbpn/network/layers/Transformer_old.py +171 -0
  44. IndicPhotoOCR/detection/textbpn/network/layers/__init__.py +0 -0
  45. IndicPhotoOCR/detection/textbpn/network/layers/gcn_utils.py +150 -0
  46. IndicPhotoOCR/detection/textbpn/network/layers/model_block.py +149 -0
  47. IndicPhotoOCR/detection/textbpn/network/layers/position_encoding.py +89 -0
  48. IndicPhotoOCR/detection/textbpn/network/layers/resnet.py +73 -0
  49. IndicPhotoOCR/detection/textbpn/network/layers/resnet_dcn.py +59 -0
  50. IndicPhotoOCR/detection/textbpn/network/layers/vgg.py +62 -0
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.jpg filter=lfs diff=lfs merge=lfs -text
37
+ *.png filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Output directories
2
+ outputs/
3
+ multirun/
4
+ ray_results/
5
+
6
+ # Byte-compiled / optimized / DLL files
7
+ __pycache__/
8
+ *.py[cod]
9
+ *$py.class
10
+
11
+ # C extensions
12
+ *.so
13
+
14
+ # Distribution / packaging
15
+ # requirements/core.*.txt
16
+ .Python
17
+ build/
18
+ develop-eggs/
19
+ dist/
20
+ downloads/
21
+ eggs/
22
+ .eggs/
23
+ lib/
24
+ lib64/
25
+ parts/
26
+ sdist/
27
+ var/
28
+ wheels/
29
+ share/python-wheels/
30
+ *.egg-info/
31
+ .installed.cfg
32
+ *.egg
33
+ MANIFEST
34
+
35
+ # PyInstaller
36
+ # Usually these files are written by a python script from a template
37
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
38
+ *.manifest
39
+ *.spec
40
+
41
+ # Installer logs
42
+ pip-log.txt
43
+ pip-delete-this-directory.txt
44
+
45
+ # Unit test / coverage reports
46
+ htmlcov/
47
+ .tox/
48
+ .nox/
49
+ .coverage
50
+ .coverage.*
51
+ .cache
52
+ nosetests.xml
53
+ coverage.xml
54
+ *.cover
55
+ *.py,cover
56
+ .hypothesis/
57
+ .pytest_cache/
58
+ cover/
59
+
60
+ # Translations
61
+ *.mo
62
+ *.pot
63
+
64
+ # Django stuff:
65
+ *.log
66
+ local_settings.py
67
+ db.sqlite3
68
+ db.sqlite3-journal
69
+
70
+ # Flask stuff:
71
+ instance/
72
+ .webassets-cache
73
+
74
+ # Scrapy stuff:
75
+ .scrapy
76
+
77
+ # Sphinx documentation
78
+ docs/_build/
79
+
80
+ # PyBuilder
81
+ .pybuilder/
82
+ target/
83
+
84
+ # Jupyter Notebook
85
+ .ipynb_checkpoints
86
+
87
+ # IPython
88
+ profile_default/
89
+ ipython_config.py
90
+
91
+ # pyenv
92
+ # For a library or package, you might want to ignore these files since the code is
93
+ # intended to run in multiple environments; otherwise, check them in:
94
+ # .python-version
95
+
96
+ # pipenv
97
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
98
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
99
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
100
+ # install all needed dependencies.
101
+ #Pipfile.lock
102
+
103
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
104
+ __pypackages__/
105
+
106
+ # Celery stuff
107
+ celerybeat-schedule
108
+ celerybeat.pid
109
+
110
+ # SageMath parsed files
111
+ *.sage.py
112
+
113
+ # Environments
114
+ .env
115
+ .venv
116
+ env/
117
+ venv/
118
+ ENV/
119
+ env.bak/
120
+ venv.bak/
121
+ .python-version
122
+
123
+ # Spyder project settings
124
+ .spyderproject
125
+ .spyproject
126
+
127
+ # Rope project settings
128
+ .ropeproject
129
+
130
+ # mkdocs documentation
131
+ /site
132
+
133
+ # mypy
134
+ .mypy_cache/
135
+ .dmypy.json
136
+ dmypy.json
137
+
138
+ # Pyre type checker
139
+ .pyre/
140
+
141
+ # pytype static type analyzer
142
+ .pytype/
143
+
144
+ # Cython debug symbols
145
+ cython_debug/
146
+
147
+ # IDE
148
+ .idea/
149
+
150
+ ########## CUSTOM FOLDER ##############
151
+ README_original.md
152
+
153
+ results/
154
+ images
155
+ bharatSTR/East/tmp
156
+ bharatSTR/models
157
+ bharatSTR/images
158
+ __pycache__/
159
+ bharatSTR/
160
+
161
+ IndicPhotoOCR/detection/East
162
+ IndicPhotoOCR/recognition/models
163
+
164
+ IndicPhotoOCR/script_identification/images
165
+ IndicPhotoOCR/script_identification/models
166
+
167
+
168
+ build/
169
+ dist/
170
+ test.png
171
+ static/pics/IndicPhotoOCR.gif
172
+ input_image.jpg
173
+ output_image.png
174
+ flagged
175
+
176
+
CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Changelog
2
+
3
+ ## [1.1.0] - 2024-11-24
4
+ ### Added
5
+ - Updated package naming convention
6
+
7
+ ## [1.0.3] - 2024-11-06
8
+ ### Added
9
+ - Python package requirements sorted with setup.py
10
+
11
+ ## [1.0.2] - 2024-11-01
12
+ ### Added
13
+ - Added language support for 10 additional models in the recognition module.
14
+
15
+ ## [1.0.1] - 2024-10-28
16
+ ### Added
17
+ - Added support for detecting polygonal bounding boxes in `visualize_detection`.
18
+ - Introduced the `show` argument in `visualize_detection` to control image display.
19
+
Dockerfile ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use NVIDIA PyTorch as the base image
2
+ FROM nvcr.io/nvidia/pytorch:23.12-py3
3
+
4
+ # Install additional dependencies
5
+ RUN apt-get update && apt-get install -y ffmpeg libsm6 libxext6
6
+
7
+ # Set environment variables for Miniconda and Conda environment
8
+ ENV CONDA_DIR /opt/conda
9
+ ENV PATH $CONDA_DIR/bin:$PATH
10
+
11
+ # Install Miniconda
12
+ RUN apt-get update && apt-get install -y wget && \
13
+ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
14
+ bash Miniconda3-latest-Linux-x86_64.sh -b -p $CONDA_DIR && \
15
+ rm Miniconda3-latest-Linux-x86_64.sh
16
+
17
+ # Create a new Conda environment named "bocr" with Python 3.9
18
+ RUN conda create -n bocr python=3.9 -y
19
+
20
+ # Initialize conda
21
+ RUN conda init
22
+
23
+ # Reload the env configs
24
+ RUN source ~/.bashrc
25
+
26
+ # Make RUN commands use the bocr environment
27
+ SHELL ["conda", "run", "-n", "bocr", "/bin/bash", "-c"]
28
+
29
+ # # Set default shell to bash
30
+ # SHELL ["/bin/bash", "-c"]
31
+
32
+ # # Clone BharatOCR repository
33
+ # RUN git clone https://github.com/Bhashini-IITJ/BharatOCR.git && \
34
+ # git switch photoOCR && \
35
+ # cd IndicPhotoOCR && \
36
+ # python setup.py sdist bdist_wheel && \
37
+ # pip install ./dist/IndicPhotoOCR-1.1.0-py3-none-any.whl[cu118] --extra-index-url https://download.pytorch.org/whl/cu118
38
+
39
+ # # # Set default command to run BharatOCR
40
+ # CMD ["conda", "run", "-n", "bocr", "python", "-m", "IndicPhotoOCR.ocr"]
41
+
42
+
43
+ # cd IndicPhotoOCR
44
+ # sudo docker build -t indicphotoocr:latest .
45
+ # sudo docker run --gpus all --rm -it indicphotoocr:latest
IndicPhotoOCR/__init__.py ADDED
File without changes
IndicPhotoOCR/detection/__init__.py ADDED
File without changes
IndicPhotoOCR/detection/east_config.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # data-config
3
+ import numpy as np
4
+
5
+ train_data_path = './dataset/train/'
6
+ train_batch_size_per_gpu = 14 # 14
7
+ num_workers = 24 # 24
8
+ gpu_ids = [0] # [0,1,2,3]
9
+ gpu = 1 # 4
10
+ input_size = 512 # 预处理后归一化后图像尺寸
11
+ background_ratio = 3. / 8 # 纯背景样本比例
12
+ random_scale = np.array([0.5, 1, 2.0, 3.0]) # 提取多尺度图片信息
13
+ geometry = 'RBOX' # 选择使用几何特征图类型
14
+ max_image_large_side = 1280
15
+ max_text_size = 800
16
+ min_text_size = 10
17
+ min_crop_side_ratio = 0.1
18
+ means=[100, 100, 100]
19
+ pretrained = True # 是否加载基础网络的预训练模型
20
+ pretrained_basemodel_path = 'IndicPhotoOCR/detection/East/tmp/backbone_net/mobilenet_v2.pth.tar'
21
+ pre_lr = 1e-4 # 基础网络的初始学习率
22
+ lr = 1e-3 # 后面网络的初始学习率
23
+ decay_steps = 50 # decayed_learning_rate = learning_rate * decay_rate ^ (global_epoch / decay_steps)
24
+ decay_rate = 0.97
25
+ init_type = 'xavier' # 网络参数初始化方式
26
+ resume = True # 整体网络是否恢复原来保存的模型
27
+ checkpoint = 'IndicPhotoOCR/detection/East/tmp/epoch_990_checkpoint.pth.tar' # 指定具体路径及文件名
28
+ max_epochs = 1000 # 最大迭代epochs数
29
+ l2_weight_decay = 1e-6 # l2正则化惩罚项权重
30
+ print_freq = 10 # 每10个batch输出损失结果
31
+ save_eval_iteration = 50 # 每10个epoch保存一次模型,并做一次评价
32
+ save_model_path = './tmp/' # 模型保存路径
33
+ test_img_path = './dataset/full_set' # demo测试样本路径'./demo/test_img/',数据集测试为'./dataset/test/'
34
+ res_img_path = 'results' # demo结果存放路径'./demo/result_img/',数据集测试为 './dataset/test_result/'
35
+ write_images = True # 是否输出图像结果
36
+ score_map_thresh = 0.8 # 置信度阈值
37
+ box_thresh = 0.1 # 文本框中置信度平均值的阈值
38
+ nms_thres = 0.2 # 局部非极大抑制IOU阈值
39
+ compute_hmean_path = './dataset/test_compute_hmean/'
IndicPhotoOCR/detection/east_detector.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import cv2
4
+ import numpy as np
5
+ import time
6
+ import warnings
7
+
8
+
9
+ import IndicPhotoOCR.detection.east_config as cfg
10
+ from IndicPhotoOCR.detection.east_utils import ModelManager
11
+ from IndicPhotoOCR.detection.east_model import East
12
+ import IndicPhotoOCR.detection.east_utils as utils
13
+
14
+ # Suppress warnings
15
+ warnings.filterwarnings("ignore")
16
+
17
+ class EASTdetector:
18
+ def __init__(self, model_name= "east", model_path=None):
19
+ self.model_path = model_path
20
+ # self.model_manager = ModelManager()
21
+ # self.model_manager.ensure_model(model_name)
22
+ # self.ensure_model(self.model_name)
23
+ # self.root_model_dir = "BharatSTR/bharatOCR/detection/East/tmp"
24
+
25
+ def detect(self, image_path, model_checkpoint, device):
26
+ # Load image
27
+ im = cv2.imread(image_path)
28
+ # im = cv2.imread(image_path)[:, :, ::-1]
29
+
30
+ # Initialize the EAST model and load checkpoint
31
+ model = East()
32
+ model = torch.nn.DataParallel(model, device_ids=cfg.gpu_ids)
33
+
34
+ # Load the model checkpoint with weights_only=True
35
+ checkpoint = torch.load(model_checkpoint, map_location=torch.device(device), weights_only=True)
36
+ model.load_state_dict(checkpoint['state_dict'])
37
+ model.eval()
38
+
39
+ # Resize image and convert to tensor format
40
+ im_resized, (ratio_h, ratio_w) = utils.resize_image(im)
41
+ im_resized = im_resized.astype(np.float32).transpose(2, 0, 1)
42
+ im_tensor = torch.from_numpy(im_resized).unsqueeze(0).cpu()
43
+
44
+ # Inference
45
+ timer = {'net': 0, 'restore': 0, 'nms': 0}
46
+ start = time.time()
47
+ score, geometry = model(im_tensor)
48
+ timer['net'] = time.time() - start
49
+
50
+ # Process output
51
+ score = score.permute(0, 2, 3, 1).data.cpu().numpy()
52
+ geometry = geometry.permute(0, 2, 3, 1).data.cpu().numpy()
53
+
54
+ # Detect boxes
55
+ boxes, timer = utils.detect(
56
+ score_map=score, geo_map=geometry, timer=timer,
57
+ score_map_thresh=cfg.score_map_thresh, box_thresh=cfg.box_thresh,
58
+ nms_thres=cfg.box_thresh
59
+ )
60
+ bbox_result_dict = {'detections': []}
61
+
62
+ # Parse detected boxes and adjust coordinates
63
+ if boxes is not None:
64
+ boxes = boxes[:, :8].reshape((-1, 4, 2))
65
+ boxes[:, :, 0] /= ratio_w
66
+ boxes[:, :, 1] /= ratio_h
67
+ for box in boxes:
68
+ box = utils.sort_poly(box.astype(np.int32))
69
+ if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm(box[3] - box[0]) < 5:
70
+ continue
71
+ bbox_result_dict['detections'].append([
72
+ [int(coord[0]), int(coord[1])] for coord in box
73
+ ])
74
+
75
+ return bbox_result_dict
76
+
77
+ # if __name__ == "__main__":
78
+ # import argparse
79
+ # parser = argparse.ArgumentParser(description='Text detection using EAST model')
80
+ # parser.add_argument('--image_path', type=str, required=True, help='Path to the input image')
81
+ # parser.add_argument('--device', type=str, default='cpu', help='Device to run the model on, e.g., "cpu" or "cuda"')
82
+ # parser.add_argument('--model_checkpoint', type=str, required=True, help='Path to the model checkpoint file')
83
+ # args = parser.parse_args()
84
+
85
+ # # Run prediction and get results as dictionary
86
+ # detection_result = predict(args.image_path, args.device, args.model_checkpoint)
87
+ # print(detection_result)
IndicPhotoOCR/detection/east_locality_aware_nms.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import numpy as np
3
+ from shapely.geometry import Polygon
4
+
5
+
6
+ def intersection(g, p):
7
+ g = Polygon(g[:8].reshape((4, 2)))
8
+ p = Polygon(p[:8].reshape((4, 2)))
9
+ if not g.is_valid or not p.is_valid:
10
+ return 0
11
+ inter = Polygon(g).intersection(Polygon(p)).area
12
+ union = g.area + p.area - inter
13
+ if union == 0:
14
+ return 0
15
+ else:
16
+ return inter/union
17
+
18
+
19
+ def weighted_merge(g, p):
20
+ # g[0]=min(g[0],p[0])
21
+ # g[1] = min(g[1], p[1])
22
+ # g[4] = max(g[4], p[4])
23
+ # g[5]= max(g[5],p[5])
24
+ #
25
+ # g[2] = max(g[2], p[2])
26
+ # g[3] = min(g[3], p[3])
27
+ # g[6] = min(g[6], p[6])
28
+ # g[7] = max(g[7], p[7])
29
+
30
+ g[:8] = (g[8] * g[:8] + p[8] * p[:8])/(g[8] + p[8])
31
+ g[8] = (g[8] + p[8])
32
+ return g
33
+
34
+
35
+ def standard_nms(S, thres):
36
+ order = np.argsort(S[:, 8])[::-1]
37
+ keep = []
38
+ while order.size > 0:
39
+ i = order[0]
40
+ keep.append(i)
41
+ ovr = np.array([intersection(S[i], S[t]) for t in order[1:]])
42
+
43
+ inds = np.where(ovr <= thres)[0]
44
+ order = order[inds+1]
45
+
46
+ return S[keep]
47
+
48
+
49
+ def nms_locality(polys, thres=0.3):
50
+ '''
51
+ locality aware nms of EAST
52
+ :param polys: a N*9 numpy array. first 8 coordinates, then prob
53
+ :return: boxes after nms
54
+ '''
55
+ S = []
56
+ p = None
57
+ for g in polys:
58
+ if p is not None and intersection(g, p) > thres:
59
+ p = weighted_merge(g, p)
60
+ else:
61
+ if p is not None:
62
+ S.append(p)
63
+ p = g
64
+ if p is not None:
65
+ S.append(p)
66
+
67
+ if len(S) == 0:
68
+ return np.array([])
69
+ return standard_nms(np.array(S), thres)
70
+
71
+
72
+ if __name__ == '__main__':
73
+ # 343,350,448,135,474,143,369,359
74
+ print(Polygon(np.array([[343, 350], [448, 135],
75
+ [474, 143], [369, 359]])).area)
IndicPhotoOCR/detection/east_model.py ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch.nn as nn
3
+ import math
4
+ import torch
5
+
6
+
7
+ from IndicPhotoOCR.detection import east_config as cfg
8
+ from IndicPhotoOCR.detection import east_utils as utils
9
+
10
+
11
+ def conv_bn(inp, oup, stride):
12
+ return nn.Sequential(
13
+ nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
14
+ nn.BatchNorm2d(oup),
15
+ nn.ReLU6(inplace=True)
16
+ )
17
+
18
+
19
+ class InvertedResidual(nn.Module):
20
+ def __init__(self, inp, oup, stride, expand_ratio):
21
+ super(InvertedResidual, self).__init__()
22
+ self.stride = stride
23
+ assert stride in [1, 2]
24
+
25
+ hidden_dim = round(inp * expand_ratio)
26
+ self.use_res_connect = self.stride == 1 and inp == oup
27
+
28
+ if expand_ratio == 1:
29
+ self.conv = nn.Sequential(
30
+ # dw
31
+ nn.Conv2d(hidden_dim, hidden_dim, 3, stride, 1, groups=hidden_dim, bias=False),
32
+ nn.BatchNorm2d(hidden_dim),
33
+ nn.ReLU6(inplace=True),
34
+ # pw-linear
35
+ nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
36
+ nn.BatchNorm2d(oup),
37
+ )
38
+ else:
39
+ self.conv = nn.Sequential(
40
+ # pw
41
+ nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False),
42
+ nn.BatchNorm2d(hidden_dim),
43
+ nn.ReLU6(inplace=True),
44
+ # dw
45
+ nn.Conv2d(hidden_dim, hidden_dim, 3, stride, 1, groups=hidden_dim, bias=False),
46
+ nn.BatchNorm2d(hidden_dim),
47
+ nn.ReLU6(inplace=True),
48
+ # pw-linear
49
+ nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
50
+ nn.BatchNorm2d(oup),
51
+ )
52
+
53
+ def forward(self, x):
54
+ if self.use_res_connect:
55
+ return x + self.conv(x)
56
+ else:
57
+ return self.conv(x)
58
+
59
+
60
+ class MobileNetV2(nn.Module):
61
+ def __init__(self, width_mult=1.):
62
+ super(MobileNetV2, self).__init__()
63
+ block = InvertedResidual
64
+ input_channel = 32
65
+ last_channel = 1280
66
+ interverted_residual_setting = [
67
+ # t, c, n, s
68
+ [1, 16, 1, 1],
69
+ [6, 24, 2, 2],
70
+ [6, 32, 3, 2],
71
+ [6, 64, 4, 2],
72
+ [6, 96, 3, 1],
73
+ [6, 160, 3, 2],
74
+ # [6, 320, 1, 1],
75
+ ]
76
+
77
+ # building first layer
78
+ # assert input_size % 32 == 0
79
+ input_channel = int(input_channel * width_mult)
80
+ self.last_channel = int(last_channel * width_mult) if width_mult > 1.0 else last_channel
81
+ self.features = [conv_bn(3, input_channel, 2)]
82
+ # building inverted residual blocks
83
+ for t, c, n, s in interverted_residual_setting:
84
+ output_channel = int(c * width_mult)
85
+ for i in range(n):
86
+ if i == 0:
87
+ self.features.append(block(input_channel, output_channel, s, expand_ratio=t))
88
+ else:
89
+ self.features.append(block(input_channel, output_channel, 1, expand_ratio=t))
90
+ input_channel = output_channel
91
+
92
+ # make it nn.Sequential
93
+ self.features = nn.Sequential(*self.features)
94
+
95
+ self._initialize_weights()
96
+
97
+ def forward(self, x):
98
+ x = self.features(x)
99
+ # x = x.mean(3).mean(2)
100
+ # x = self.classifier(x)
101
+ return x
102
+
103
+ def _initialize_weights(self):
104
+ for m in self.modules():
105
+ if isinstance(m, nn.Conv2d):
106
+ n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
107
+ m.weight.data.normal_(0, math.sqrt(2. / n))
108
+ if m.bias is not None:
109
+ m.bias.data.zero_()
110
+ elif isinstance(m, nn.BatchNorm2d):
111
+ m.weight.data.fill_(1)
112
+ m.bias.data.zero_()
113
+ elif isinstance(m, nn.Linear):
114
+ n = m.weight.size(1)
115
+ m.weight.data.normal_(0, 0.01)
116
+ m.bias.data.zero_()
117
+
118
+
119
+ def mobilenet(pretrained=True, **kwargs):
120
+ """
121
+ Constructs a ResNet-50 model.
122
+ Args:
123
+ pretrained (bool): If True, returns a model pre-trained on ImageNet
124
+ """
125
+ model = MobileNetV2()
126
+ if pretrained:
127
+ model_dict = model.state_dict()
128
+ pretrained_dict = torch.load(cfg.pretrained_basemodel_path,map_location=torch.device('cpu'), weights_only=True)
129
+ pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
130
+ model_dict.update(pretrained_dict)
131
+ model.load_state_dict(model_dict)
132
+ # state_dict = torch.load(cfg.pretrained_basemodel_path) # add map_location='cpu' if no gpu
133
+ # model.load_state_dict(state_dict)
134
+
135
+ return model
136
+
137
+
138
+ class East(nn.Module):
139
+ def __init__(self):
140
+ super(East, self).__init__()
141
+ self.mobilenet = mobilenet(True)
142
+ # self.si for stage i
143
+ self.s1 = nn.Sequential(*list(self.mobilenet.children())[0][0:4])
144
+ self.s2 = nn.Sequential(*list(self.mobilenet.children())[0][4:7])
145
+ self.s3 = nn.Sequential(*list(self.mobilenet.children())[0][7:14])
146
+ self.s4 = nn.Sequential(*list(self.mobilenet.children())[0][14:17])
147
+
148
+ self.conv1 = nn.Conv2d(160+96, 128, 1)
149
+ self.bn1 = nn.BatchNorm2d(128)
150
+ self.relu1 = nn.ReLU()
151
+
152
+ self.conv2 = nn.Conv2d(128, 128, 3, padding=1)
153
+ self.bn2 = nn.BatchNorm2d(128)
154
+ self.relu2 = nn.ReLU()
155
+
156
+ self.conv3 = nn.Conv2d(128+32, 64, 1)
157
+ self.bn3 = nn.BatchNorm2d(64)
158
+ self.relu3 = nn.ReLU()
159
+
160
+ self.conv4 = nn.Conv2d(64, 64, 3, padding=1)
161
+ self.bn4 = nn.BatchNorm2d(64)
162
+ self.relu4 = nn.ReLU()
163
+
164
+ self.conv5 = nn.Conv2d(64+24, 64, 1)
165
+ self.bn5 = nn.BatchNorm2d(64)
166
+ self.relu5 = nn.ReLU()
167
+
168
+ self.conv6 = nn.Conv2d(64, 32, 3, padding=1)
169
+ self.bn6 = nn.BatchNorm2d(32)
170
+ self.relu6 = nn.ReLU()
171
+
172
+ self.conv7 = nn.Conv2d(32, 32, 3, padding=1)
173
+ self.bn7 = nn.BatchNorm2d(32)
174
+ self.relu7 = nn.ReLU()
175
+
176
+ self.conv8 = nn.Conv2d(32, 1, 1)
177
+ self.sigmoid1 = nn.Sigmoid()
178
+ self.conv9 = nn.Conv2d(32, 4, 1)
179
+ self.sigmoid2 = nn.Sigmoid()
180
+ self.conv10 = nn.Conv2d(32, 1, 1)
181
+ self.sigmoid3 = nn.Sigmoid()
182
+ self.unpool1 = nn.Upsample(scale_factor=2, mode='bilinear')
183
+ self.unpool2 = nn.Upsample(scale_factor=2, mode='bilinear')
184
+ self.unpool3 = nn.Upsample(scale_factor=2, mode='bilinear')
185
+
186
+ # utils.init_weights([self.conv1,self.conv2,self.conv3,self.conv4,
187
+ # self.conv5,self.conv6,self.conv7,self.conv8,
188
+ # self.conv9,self.conv10,self.bn1,self.bn2,
189
+ # self.bn3,self.bn4,self.bn5,self.bn6,self.bn7])
190
+
191
+ def forward(self, images):
192
+ images = utils.mean_image_subtraction(images)
193
+
194
+ f0 = self.s1(images)
195
+ f1 = self.s2(f0)
196
+ f2 = self.s3(f1)
197
+ f3 = self.s4(f2)
198
+
199
+ # _, f = self.mobilenet(images)
200
+ h = f3 # bs 2048 w/32 h/32
201
+ g = (self.unpool1(h)) # bs 2048 w/16 h/16
202
+ c = self.conv1(torch.cat((g, f2), 1))
203
+ c = self.bn1(c)
204
+ c = self.relu1(c)
205
+
206
+ h = self.conv2(c) # bs 128 w/16 h/16
207
+ h = self.bn2(h)
208
+ h = self.relu2(h)
209
+ g = self.unpool2(h) # bs 128 w/8 h/8
210
+ c = self.conv3(torch.cat((g, f1), 1))
211
+ c = self.bn3(c)
212
+ c = self.relu3(c)
213
+
214
+ h = self.conv4(c) # bs 64 w/8 h/8
215
+ h = self.bn4(h)
216
+ h = self.relu4(h)
217
+ g = self.unpool3(h) # bs 64 w/4 h/4
218
+ c = self.conv5(torch.cat((g, f0), 1))
219
+ c = self.bn5(c)
220
+ c = self.relu5(c)
221
+
222
+ h = self.conv6(c) # bs 32 w/4 h/4
223
+ h = self.bn6(h)
224
+ h = self.relu6(h)
225
+ g = self.conv7(h) # bs 32 w/4 h/4
226
+ g = self.bn7(g)
227
+ g = self.relu7(g)
228
+
229
+ F_score = self.conv8(g) # bs 1 w/4 h/4
230
+ F_score = self.sigmoid1(F_score)
231
+ geo_map = self.conv9(g)
232
+ geo_map = self.sigmoid2(geo_map) * 512
233
+ angle_map = self.conv10(g)
234
+ angle_map = self.sigmoid3(angle_map)
235
+ angle_map = (angle_map - 0.5) * math.pi / 2
236
+
237
+ F_geometry = torch.cat((geo_map, angle_map), 1) # bs 5 w/4 h/4
238
+
239
+ return F_score, F_geometry
240
+
241
+
242
+ model=East()
IndicPhotoOCR/detection/east_preprossing.py ADDED
@@ -0,0 +1,681 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # coding:utf-8
3
+ import glob
4
+ import csv
5
+ import cv2
6
+ import os
7
+ import numpy as np
8
+ from shapely.geometry import Polygon
9
+
10
+
11
+ from IndicPhotoOCR.detection import east_config as cfg
12
+ from IndicPhotoOCR.detection import east_utils
13
+
14
+
15
+ def get_images(img_root):
16
+ files = []
17
+ for ext in ['jpg']:
18
+ files.extend(glob.glob(
19
+ os.path.join(img_root, '*.{}'.format(ext))))
20
+ # print(glob.glob(
21
+ # os.path.join(FLAGS.training_data_path, '*.{}'.format(ext))))
22
+ return files
23
+
24
+
25
+ def load_annoataion(p):
26
+ '''
27
+ load annotation from the text file
28
+ :param p:
29
+ :return:
30
+ '''
31
+ text_polys = []
32
+ text_tags = []
33
+ if not os.path.exists(p):
34
+ return np.array(text_polys, dtype=np.float32)
35
+ with open(p, 'r', encoding='UTF-8') as f:
36
+ reader = csv.reader(f)
37
+ for line in reader:
38
+ label = line[-1]
39
+ # strip BOM. \ufeff for python3, \xef\xbb\bf for python2
40
+ line = [i.strip('\ufeff').strip('\xef\xbb\xbf') for i in line]
41
+
42
+ x1, y1, x2, y2, x3, y3, x4, y4 = list(map(float, line[:8]))
43
+ text_polys.append([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])
44
+ # print(text_polys)
45
+ if label == '*' or label == '###':
46
+ text_tags.append(True)
47
+ else:
48
+ text_tags.append(False)
49
+ return np.array(text_polys, dtype=np.float32), np.array(text_tags, dtype=np.bool)
50
+
51
+
52
+ def polygon_area(poly):
53
+ '''
54
+ compute area of a polygon
55
+ :param poly:
56
+ :return:
57
+ '''
58
+ edge = [
59
+ (poly[1][0] - poly[0][0]) * (poly[1][1] + poly[0][1]),
60
+ (poly[2][0] - poly[1][0]) * (poly[2][1] + poly[1][1]),
61
+ (poly[3][0] - poly[2][0]) * (poly[3][1] + poly[2][1]),
62
+ (poly[0][0] - poly[3][0]) * (poly[0][1] + poly[3][1])
63
+ ]
64
+ return np.sum(edge) / 2.
65
+
66
+
67
+ def check_and_validate_polys(polys, tags, xxx_todo_changeme):
68
+ '''
69
+ check so that the text poly is in the same direction,
70
+ and also filter some invalid polygons
71
+ :param polys:
72
+ :param tags:
73
+ :return:
74
+ '''
75
+ (h, w) = xxx_todo_changeme
76
+ if polys.shape[0] == 0:
77
+ return polys
78
+ polys[:, :, 0] = np.clip(polys[:, :, 0], 0, w - 1)
79
+ polys[:, :, 1] = np.clip(polys[:, :, 1], 0, h - 1)
80
+
81
+ validated_polys = []
82
+ validated_tags = []
83
+
84
+ # 判断四边形的点时针方向,以及是否是有效四边形
85
+ for poly, tag in zip(polys, tags):
86
+ p_area = polygon_area(poly)
87
+ if abs(p_area) < 1:
88
+ # print poly
89
+ print('invalid poly')
90
+ continue
91
+ if p_area > 0:
92
+ print('poly in wrong direction')
93
+ poly = poly[(0, 3, 2, 1), :]
94
+ validated_polys.append(poly)
95
+ validated_tags.append(tag)
96
+ return np.array(validated_polys), np.array(validated_tags)
97
+
98
+
99
+ def crop_area(im, polys, tags, crop_background=False, max_tries=100):
100
+ '''
101
+ make random crop from the input image
102
+ :param im:
103
+ :param polys:
104
+ :param tags:
105
+ :param crop_background:
106
+ :param max_tries:
107
+ :return:
108
+ '''
109
+ h, w, _ = im.shape
110
+ pad_h = h // 10
111
+ pad_w = w // 10
112
+ h_array = np.zeros((h + pad_h * 2), dtype=np.int32)
113
+ w_array = np.zeros((w + pad_w * 2), dtype=np.int32)
114
+ for poly in polys:
115
+ poly = np.round(poly, decimals=0).astype(np.int32)
116
+ minx = np.min(poly[:, 0])
117
+ maxx = np.max(poly[:, 0])
118
+ w_array[minx + pad_w:maxx + pad_w] = 1
119
+ miny = np.min(poly[:, 1])
120
+ maxy = np.max(poly[:, 1])
121
+ h_array[miny + pad_h:maxy + pad_h] = 1
122
+ # ensure the cropped area not across a text,保证裁剪区域不能与文本交叉
123
+ h_axis = np.where(h_array == 0)[0]
124
+ w_axis = np.where(w_array == 0)[0]
125
+ if len(h_axis) == 0 or len(w_axis) == 0:
126
+ return im, polys, tags
127
+ for i in range(max_tries): # 试验50次
128
+ xx = np.random.choice(w_axis, size=2)
129
+ xmin = np.min(xx) - pad_w
130
+ xmax = np.max(xx) - pad_w
131
+ xmin = np.clip(xmin, 0, w - 1)
132
+ xmax = np.clip(xmax, 0, w - 1)
133
+ yy = np.random.choice(h_axis, size=2)
134
+ ymin = np.min(yy) - pad_h
135
+ ymax = np.max(yy) - pad_h
136
+ ymin = np.clip(ymin, 0, h - 1)
137
+ ymax = np.clip(ymax, 0, h - 1)
138
+ if xmax - xmin < cfg.min_crop_side_ratio * w or ymax - ymin < cfg.min_crop_side_ratio * h:
139
+ # area too small
140
+ continue
141
+ if polys.shape[0] != 0:
142
+ poly_axis_in_area = (polys[:, :, 0] >= xmin) & (polys[:, :, 0] <= xmax) \
143
+ & (polys[:, :, 1] >= ymin) & (polys[:, :, 1] <= ymax)
144
+ selected_polys = np.where(np.sum(poly_axis_in_area, axis=1) == 4)[0]
145
+ else:
146
+ selected_polys = []
147
+ if len(selected_polys) == 0:
148
+ # no text in this area
149
+ if crop_background:
150
+ return im[ymin:ymax + 1, xmin:xmax + 1, :], polys[selected_polys], tags[selected_polys]
151
+ else:
152
+ continue
153
+ im = im[ymin:ymax + 1, xmin:xmax + 1, :]
154
+ polys = polys[selected_polys]
155
+ tags = tags[selected_polys]
156
+ polys[:, :, 0] -= xmin
157
+ polys[:, :, 1] -= ymin
158
+ return im, polys, tags
159
+
160
+ return im, polys, tags
161
+
162
+
163
+ def shrink_poly(poly, r):
164
+ '''
165
+ fit a poly inside the origin poly, maybe bugs here...
166
+ used for generate the score map
167
+ :param poly: the text poly
168
+ :param r: r in the paper
169
+ :return: the shrinked poly
170
+ '''
171
+ # shrink ratio
172
+ R = 0.3
173
+ # find the longer pair
174
+ if np.linalg.norm(poly[0] - poly[1]) + np.linalg.norm(poly[2] - poly[3]) > \
175
+ np.linalg.norm(poly[0] - poly[3]) + np.linalg.norm(poly[1] - poly[2]):
176
+ # first move (p0, p1), (p2, p3), then (p0, p3), (p1, p2)
177
+ ## p0, p1
178
+ theta = np.arctan2((poly[1][1] - poly[0][1]), (poly[1][0] - poly[0][0]))
179
+ poly[0][0] += R * r[0] * np.cos(theta)
180
+ poly[0][1] += R * r[0] * np.sin(theta)
181
+ poly[1][0] -= R * r[1] * np.cos(theta)
182
+ poly[1][1] -= R * r[1] * np.sin(theta)
183
+ ## p2, p3
184
+ theta = np.arctan2((poly[2][1] - poly[3][1]), (poly[2][0] - poly[3][0]))
185
+ poly[3][0] += R * r[3] * np.cos(theta)
186
+ poly[3][1] += R * r[3] * np.sin(theta)
187
+ poly[2][0] -= R * r[2] * np.cos(theta)
188
+ poly[2][1] -= R * r[2] * np.sin(theta)
189
+ ## p0, p3
190
+ theta = np.arctan2((poly[3][0] - poly[0][0]), (poly[3][1] - poly[0][1]))
191
+ poly[0][0] += R * r[0] * np.sin(theta)
192
+ poly[0][1] += R * r[0] * np.cos(theta)
193
+ poly[3][0] -= R * r[3] * np.sin(theta)
194
+ poly[3][1] -= R * r[3] * np.cos(theta)
195
+ ## p1, p2
196
+ theta = np.arctan2((poly[2][0] - poly[1][0]), (poly[2][1] - poly[1][1]))
197
+ poly[1][0] += R * r[1] * np.sin(theta)
198
+ poly[1][1] += R * r[1] * np.cos(theta)
199
+ poly[2][0] -= R * r[2] * np.sin(theta)
200
+ poly[2][1] -= R * r[2] * np.cos(theta)
201
+ else:
202
+ ## p0, p3
203
+ # print poly
204
+ theta = np.arctan2((poly[3][0] - poly[0][0]), (poly[3][1] - poly[0][1]))
205
+ poly[0][0] += R * r[0] * np.sin(theta)
206
+ poly[0][1] += R * r[0] * np.cos(theta)
207
+ poly[3][0] -= R * r[3] * np.sin(theta)
208
+ poly[3][1] -= R * r[3] * np.cos(theta)
209
+ ## p1, p2
210
+ theta = np.arctan2((poly[2][0] - poly[1][0]), (poly[2][1] - poly[1][1]))
211
+ poly[1][0] += R * r[1] * np.sin(theta)
212
+ poly[1][1] += R * r[1] * np.cos(theta)
213
+ poly[2][0] -= R * r[2] * np.sin(theta)
214
+ poly[2][1] -= R * r[2] * np.cos(theta)
215
+ ## p0, p1
216
+ theta = np.arctan2((poly[1][1] - poly[0][1]), (poly[1][0] - poly[0][0]))
217
+ poly[0][0] += R * r[0] * np.cos(theta)
218
+ poly[0][1] += R * r[0] * np.sin(theta)
219
+ poly[1][0] -= R * r[1] * np.cos(theta)
220
+ poly[1][1] -= R * r[1] * np.sin(theta)
221
+ ## p2, p3
222
+ theta = np.arctan2((poly[2][1] - poly[3][1]), (poly[2][0] - poly[3][0]))
223
+ poly[3][0] += R * r[3] * np.cos(theta)
224
+ poly[3][1] += R * r[3] * np.sin(theta)
225
+ poly[2][0] -= R * r[2] * np.cos(theta)
226
+ poly[2][1] -= R * r[2] * np.sin(theta)
227
+ return poly
228
+
229
+
230
+ # def point_dist_to_line(p1, p2, p3):
231
+ # # compute the distance from p3 to p1-p2
232
+ # return np.linalg.norm(np.cross(p2 - p1, p1 - p3)) / np.linalg.norm(p2 - p1)
233
+
234
+
235
+ # 点p3到直线p12的距离
236
+ def point_dist_to_line(p1, p2, p3):
237
+ # compute the distance from p3 to p1-p2
238
+ # return np.linalg.norm(np.cross(p2 - p1, p1 - p3)) / np.linalg.norm(p2 - p1)
239
+ a = np.linalg.norm(p1 - p2)
240
+ b = np.linalg.norm(p2 - p3)
241
+ c = np.linalg.norm(p3 - p1)
242
+ s = (a + b + c) / 2.0
243
+ area = np.abs((s * (s - a) * (s - b) * (s - c))) ** 0.5
244
+ if a < 1.0:
245
+ return (b + c) / 2.0
246
+ return 2 * area / a
247
+
248
+
249
+ def fit_line(p1, p2):
250
+ # fit a line ax+by+c = 0
251
+ if p1[0] == p1[1]:
252
+ return [1., 0., -p1[0]]
253
+ else:
254
+ [k, b] = np.polyfit(p1, p2, deg=1)
255
+ return [k, -1., b]
256
+
257
+
258
+ def line_cross_point(line1, line2):
259
+ # line1 0= ax+by+c, compute the cross point of line1 and line2
260
+ if line1[0] != 0 and line1[0] == line2[0]:
261
+ print('Cross point does not exist')
262
+ return None
263
+ if line1[0] == 0 and line2[0] == 0:
264
+ print('Cross point does not exist')
265
+ return None
266
+ if line1[1] == 0:
267
+ x = -line1[2]
268
+ y = line2[0] * x + line2[2]
269
+ elif line2[1] == 0:
270
+ x = -line2[2]
271
+ y = line1[0] * x + line1[2]
272
+ else:
273
+ k1, _, b1 = line1
274
+ k2, _, b2 = line2
275
+ x = -(b1 - b2) / (k1 - k2)
276
+ y = k1 * x + b1
277
+ return np.array([x, y], dtype=np.float32)
278
+
279
+
280
+ def line_verticle(line, point):
281
+ # get the verticle line from line across point
282
+ if line[1] == 0:
283
+ verticle = [0, -1, point[1]]
284
+ else:
285
+ if line[0] == 0:
286
+ verticle = [1, 0, -point[0]]
287
+ else:
288
+ verticle = [-1. / line[0], -1, point[1] - (-1 / line[0] * point[0])]
289
+ return verticle
290
+
291
+
292
+ def rectangle_from_parallelogram(poly):
293
+ '''
294
+ fit a rectangle from a parallelogram
295
+ :param poly:
296
+ :return:
297
+ '''
298
+ p0, p1, p2, p3 = poly
299
+ angle_p0 = np.arccos(np.dot(p1 - p0, p3 - p0) / (np.linalg.norm(p0 - p1) * np.linalg.norm(p3 - p0)))
300
+ if angle_p0 < 0.5 * np.pi:
301
+ if np.linalg.norm(p0 - p1) > np.linalg.norm(p0 - p3):
302
+ # p0 and p2
303
+ ## p0
304
+ p2p3 = fit_line([p2[0], p3[0]], [p2[1], p3[1]])
305
+ p2p3_verticle = line_verticle(p2p3, p0)
306
+
307
+ new_p3 = line_cross_point(p2p3, p2p3_verticle)
308
+ ## p2
309
+ p0p1 = fit_line([p0[0], p1[0]], [p0[1], p1[1]])
310
+ p0p1_verticle = line_verticle(p0p1, p2)
311
+
312
+ new_p1 = line_cross_point(p0p1, p0p1_verticle)
313
+ return np.array([p0, new_p1, p2, new_p3], dtype=np.float32)
314
+ else:
315
+ p1p2 = fit_line([p1[0], p2[0]], [p1[1], p2[1]])
316
+ p1p2_verticle = line_verticle(p1p2, p0)
317
+
318
+ new_p1 = line_cross_point(p1p2, p1p2_verticle)
319
+ p0p3 = fit_line([p0[0], p3[0]], [p0[1], p3[1]])
320
+ p0p3_verticle = line_verticle(p0p3, p2)
321
+
322
+ new_p3 = line_cross_point(p0p3, p0p3_verticle)
323
+ return np.array([p0, new_p1, p2, new_p3], dtype=np.float32)
324
+ else:
325
+ if np.linalg.norm(p0 - p1) > np.linalg.norm(p0 - p3):
326
+ # p1 and p3
327
+ ## p1
328
+ p2p3 = fit_line([p2[0], p3[0]], [p2[1], p3[1]])
329
+ p2p3_verticle = line_verticle(p2p3, p1)
330
+
331
+ new_p2 = line_cross_point(p2p3, p2p3_verticle)
332
+ ## p3
333
+ p0p1 = fit_line([p0[0], p1[0]], [p0[1], p1[1]])
334
+ p0p1_verticle = line_verticle(p0p1, p3)
335
+
336
+ new_p0 = line_cross_point(p0p1, p0p1_verticle)
337
+ return np.array([new_p0, p1, new_p2, p3], dtype=np.float32)
338
+ else:
339
+ p0p3 = fit_line([p0[0], p3[0]], [p0[1], p3[1]])
340
+ p0p3_verticle = line_verticle(p0p3, p1)
341
+
342
+ new_p0 = line_cross_point(p0p3, p0p3_verticle)
343
+ p1p2 = fit_line([p1[0], p2[0]], [p1[1], p2[1]])
344
+ p1p2_verticle = line_verticle(p1p2, p3)
345
+
346
+ new_p2 = line_cross_point(p1p2, p1p2_verticle)
347
+ return np.array([new_p0, p1, new_p2, p3], dtype=np.float32)
348
+
349
+
350
+ def sort_rectangle(poly):
351
+ # sort the four coordinates of the polygon, points in poly should be sorted clockwise
352
+ # First find the lowest point
353
+ p_lowest = np.argmax(poly[:, 1])
354
+ if np.count_nonzero(poly[:, 1] == poly[p_lowest, 1]) == 2:
355
+ # 底边平行于X轴, 那么p0为左上角 - if the bottom line is parallel to x-axis, then p0 must be the upper-left corner
356
+ p0_index = np.argmin(np.sum(poly, axis=1))
357
+ p1_index = (p0_index + 1) % 4
358
+ p2_index = (p0_index + 2) % 4
359
+ p3_index = (p0_index + 3) % 4
360
+ return poly[[p0_index, p1_index, p2_index, p3_index]], 0.
361
+ else:
362
+ # 找到最低点右边的点 - find the point that sits right to the lowest point
363
+ p_lowest_right = (p_lowest - 1) % 4
364
+ p_lowest_left = (p_lowest + 1) % 4
365
+ angle = np.arctan(
366
+ -(poly[p_lowest][1] - poly[p_lowest_right][1]) / (poly[p_lowest][0] - poly[p_lowest_right][0]))
367
+ # assert angle > 0
368
+ if angle <= 0:
369
+ print(angle, poly[p_lowest], poly[p_lowest_right])
370
+ if angle / np.pi * 180 > 45:
371
+ # 这个点为p2 - this point is p2
372
+ p2_index = p_lowest
373
+ p1_index = (p2_index - 1) % 4
374
+ p0_index = (p2_index - 2) % 4
375
+ p3_index = (p2_index + 1) % 4
376
+ return poly[[p0_index, p1_index, p2_index, p3_index]], -(np.pi / 2 - angle)
377
+ else:
378
+ # 这个点为p3 - this point is p3
379
+ p3_index = p_lowest
380
+ p0_index = (p3_index + 1) % 4
381
+ p1_index = (p3_index + 2) % 4
382
+ p2_index = (p3_index + 3) % 4
383
+ return poly[[p0_index, p1_index, p2_index, p3_index]], angle
384
+
385
+
386
+ def restore_rectangle_rbox(origin, geometry):
387
+ d = geometry[:, :4]
388
+ angle = geometry[:, 4]
389
+ # for angle > 0
390
+ origin_0 = origin[angle >= 0]
391
+ d_0 = d[angle >= 0]
392
+ angle_0 = angle[angle >= 0]
393
+ if origin_0.shape[0] > 0:
394
+ p = np.array([np.zeros(d_0.shape[0]), -d_0[:, 0] - d_0[:, 2],
395
+ d_0[:, 1] + d_0[:, 3], -d_0[:, 0] - d_0[:, 2],
396
+ d_0[:, 1] + d_0[:, 3], np.zeros(d_0.shape[0]),
397
+ np.zeros(d_0.shape[0]), np.zeros(d_0.shape[0]),
398
+ d_0[:, 3], -d_0[:, 2]])
399
+ p = p.transpose((1, 0)).reshape((-1, 5, 2)) # N*5*2
400
+
401
+ rotate_matrix_x = np.array([np.cos(angle_0), np.sin(angle_0)]).transpose((1, 0))
402
+ rotate_matrix_x = np.repeat(rotate_matrix_x, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1)) # N*5*2
403
+
404
+ rotate_matrix_y = np.array([-np.sin(angle_0), np.cos(angle_0)]).transpose((1, 0))
405
+ rotate_matrix_y = np.repeat(rotate_matrix_y, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1))
406
+
407
+ p_rotate_x = np.sum(rotate_matrix_x * p, axis=2)[:, :, np.newaxis] # N*5*1
408
+ p_rotate_y = np.sum(rotate_matrix_y * p, axis=2)[:, :, np.newaxis] # N*5*1
409
+
410
+ p_rotate = np.concatenate([p_rotate_x, p_rotate_y], axis=2) # N*5*2
411
+
412
+ p3_in_origin = origin_0 - p_rotate[:, 4, :]
413
+ new_p0 = p_rotate[:, 0, :] + p3_in_origin # N*2
414
+ new_p1 = p_rotate[:, 1, :] + p3_in_origin
415
+ new_p2 = p_rotate[:, 2, :] + p3_in_origin
416
+ new_p3 = p_rotate[:, 3, :] + p3_in_origin
417
+
418
+ new_p_0 = np.concatenate([new_p0[:, np.newaxis, :], new_p1[:, np.newaxis, :],
419
+ new_p2[:, np.newaxis, :], new_p3[:, np.newaxis, :]], axis=1) # N*4*2
420
+ else:
421
+ new_p_0 = np.zeros((0, 4, 2))
422
+ # for angle < 0
423
+ origin_1 = origin[angle < 0]
424
+ d_1 = d[angle < 0]
425
+ angle_1 = angle[angle < 0]
426
+ if origin_1.shape[0] > 0:
427
+ p = np.array([-d_1[:, 1] - d_1[:, 3], -d_1[:, 0] - d_1[:, 2],
428
+ np.zeros(d_1.shape[0]), -d_1[:, 0] - d_1[:, 2],
429
+ np.zeros(d_1.shape[0]), np.zeros(d_1.shape[0]),
430
+ -d_1[:, 1] - d_1[:, 3], np.zeros(d_1.shape[0]),
431
+ -d_1[:, 1], -d_1[:, 2]])
432
+ p = p.transpose((1, 0)).reshape((-1, 5, 2)) # N*5*2
433
+
434
+ rotate_matrix_x = np.array([np.cos(-angle_1), -np.sin(-angle_1)]).transpose((1, 0))
435
+ rotate_matrix_x = np.repeat(rotate_matrix_x, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1)) # N*5*2
436
+
437
+ rotate_matrix_y = np.array([np.sin(-angle_1), np.cos(-angle_1)]).transpose((1, 0))
438
+ rotate_matrix_y = np.repeat(rotate_matrix_y, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1))
439
+
440
+ p_rotate_x = np.sum(rotate_matrix_x * p, axis=2)[:, :, np.newaxis] # N*5*1
441
+ p_rotate_y = np.sum(rotate_matrix_y * p, axis=2)[:, :, np.newaxis] # N*5*1
442
+
443
+ p_rotate = np.concatenate([p_rotate_x, p_rotate_y], axis=2) # N*5*2
444
+
445
+ p3_in_origin = origin_1 - p_rotate[:, 4, :]
446
+ new_p0 = p_rotate[:, 0, :] + p3_in_origin # N*2
447
+ new_p1 = p_rotate[:, 1, :] + p3_in_origin
448
+ new_p2 = p_rotate[:, 2, :] + p3_in_origin
449
+ new_p3 = p_rotate[:, 3, :] + p3_in_origin
450
+
451
+ new_p_1 = np.concatenate([new_p0[:, np.newaxis, :], new_p1[:, np.newaxis, :],
452
+ new_p2[:, np.newaxis, :], new_p3[:, np.newaxis, :]], axis=1) # N*4*2
453
+ else:
454
+ new_p_1 = np.zeros((0, 4, 2))
455
+ return np.concatenate([new_p_0, new_p_1])
456
+
457
+
458
+ def restore_rectangle(origin, geometry):
459
+ return restore_rectangle_rbox(origin, geometry)
460
+
461
+
462
+ def generate_rbox(im_size, polys, tags):
463
+ h, w = im_size
464
+ poly_mask = np.zeros((h, w), dtype=np.uint8)
465
+ score_map = np.zeros((h, w), dtype=np.uint8)
466
+ geo_map = np.zeros((h, w, 5), dtype=np.float32)
467
+ # mask used during traning, to ignore some hard areas,用于忽略那些过小的文本
468
+ training_mask = np.ones((h, w), dtype=np.uint8)
469
+ for poly_idx, poly_tag in enumerate(zip(polys, tags)):
470
+ poly = poly_tag[0]
471
+ tag = poly_tag[1]
472
+
473
+ # 对每个顶点,找到经过他的两条边中较短的那条
474
+ r = [None, None, None, None]
475
+ for i in range(4):
476
+ r[i] = min(np.linalg.norm(poly[i] - poly[(i + 1) % 4]),
477
+ np.linalg.norm(poly[i] - poly[(i - 1) % 4]))
478
+ # score map
479
+ # 放缩边框为之前的0.3倍,并对边框对应score图中的位置进行填充
480
+ shrinked_poly = shrink_poly(poly.copy(), r).astype(np.int32)[np.newaxis, :, :]
481
+ cv2.fillPoly(score_map, shrinked_poly, 1)
482
+ cv2.fillPoly(poly_mask, shrinked_poly, poly_idx + 1)
483
+ # if the poly is too small, then ignore it during training
484
+ # 如果文本框标签太小或者txt中没具体标记是什么内容,即*或者###,则加掩模,训练时忽略该部分
485
+ poly_h = min(np.linalg.norm(poly[0] - poly[3]), np.linalg.norm(poly[1] - poly[2]))
486
+ poly_w = min(np.linalg.norm(poly[0] - poly[1]), np.linalg.norm(poly[2] - poly[3]))
487
+ if min(poly_h, poly_w) < cfg.min_text_size:
488
+ cv2.fillPoly(training_mask, poly.astype(np.int32)[np.newaxis, :, :], 0)
489
+ if tag:
490
+ cv2.fillPoly(training_mask, poly.astype(np.int32)[np.newaxis, :, :], 0)
491
+
492
+ # 当前新加入的文本框区域像素点
493
+ xy_in_poly = np.argwhere(poly_mask == (poly_idx + 1))
494
+ # if geometry == 'RBOX':
495
+ # 对任意两个顶点的组合生成一个平行四边形 - generate a parallelogram for any combination of two vertices
496
+ fitted_parallelograms = []
497
+ for i in range(4):
498
+ # 选中p0和p1的连线边,生成两个平行四边形
499
+ p0 = poly[i]
500
+ p1 = poly[(i + 1) % 4]
501
+ p2 = poly[(i + 2) % 4]
502
+ p3 = poly[(i + 3) % 4]
503
+ # 拟合ax+by+c=0
504
+ edge = fit_line([p0[0], p1[0]], [p0[1], p1[1]])
505
+ backward_edge = fit_line([p0[0], p3[0]], [p0[1], p3[1]])
506
+ forward_edge = fit_line([p1[0], p2[0]], [p1[1], p2[1]])
507
+ # 通过另外两个点距离edge的距离,来决定edge对应的平行线应该过p2还是p3
508
+ if point_dist_to_line(p0, p1, p2) > point_dist_to_line(p0, p1, p3):
509
+ # 平行线经过p2 - parallel lines through p2
510
+ if edge[1] == 0:
511
+ edge_opposite = [1, 0, -p2[0]]
512
+ else:
513
+ edge_opposite = [edge[0], -1, p2[1] - edge[0] * p2[0]]
514
+ else:
515
+ # 经过p3 - after p3
516
+ if edge[1] == 0:
517
+ edge_opposite = [1, 0, -p3[0]]
518
+ else:
519
+ edge_opposite = [edge[0], -1, p3[1] - edge[0] * p3[0]]
520
+ # move forward edge
521
+ new_p0 = p0
522
+ new_p1 = p1
523
+ new_p2 = p2
524
+ new_p3 = p3
525
+ new_p2 = line_cross_point(forward_edge, edge_opposite)
526
+ if point_dist_to_line(p1, new_p2, p0) > point_dist_to_line(p1, new_p2, p3):
527
+ # across p0
528
+ if forward_edge[1] == 0:
529
+ forward_opposite = [1, 0, -p0[0]]
530
+ else:
531
+ forward_opposite = [forward_edge[0], -1, p0[1] - forward_edge[0] * p0[0]]
532
+ else:
533
+ # across p3
534
+ if forward_edge[1] == 0:
535
+ forward_opposite = [1, 0, -p3[0]]
536
+ else:
537
+ forward_opposite = [forward_edge[0], -1, p3[1] - forward_edge[0] * p3[0]]
538
+ new_p0 = line_cross_point(forward_opposite, edge)
539
+ new_p3 = line_cross_point(forward_opposite, edge_opposite)
540
+ fitted_parallelograms.append([new_p0, new_p1, new_p2, new_p3, new_p0])
541
+ # or move backward edge
542
+ new_p0 = p0
543
+ new_p1 = p1
544
+ new_p2 = p2
545
+ new_p3 = p3
546
+ new_p3 = line_cross_point(backward_edge, edge_opposite)
547
+ if point_dist_to_line(p0, p3, p1) > point_dist_to_line(p0, p3, p2):
548
+ # across p1
549
+ if backward_edge[1] == 0:
550
+ backward_opposite = [1, 0, -p1[0]]
551
+ else:
552
+ backward_opposite = [backward_edge[0], -1, p1[1] - backward_edge[0] * p1[0]]
553
+ else:
554
+ # across p2
555
+ if backward_edge[1] == 0:
556
+ backward_opposite = [1, 0, -p2[0]]
557
+ else:
558
+ backward_opposite = [backward_edge[0], -1, p2[1] - backward_edge[0] * p2[0]]
559
+ new_p1 = line_cross_point(backward_opposite, edge)
560
+ new_p2 = line_cross_point(backward_opposite, edge_opposite)
561
+ fitted_parallelograms.append([new_p0, new_p1, new_p2, new_p3, new_p0])
562
+
563
+ # 选定面积最小的平行四边形
564
+ areas = [Polygon(t).area for t in fitted_parallelograms]
565
+ parallelogram = np.array(fitted_parallelograms[np.argmin(areas)][:-1], dtype=np.float32)
566
+ # sort thie polygon
567
+ parallelogram_coord_sum = np.sum(parallelogram, axis=1)
568
+ min_coord_idx = np.argmin(parallelogram_coord_sum)
569
+ parallelogram = parallelogram[
570
+ [min_coord_idx, (min_coord_idx + 1) % 4, (min_coord_idx + 2) % 4, (min_coord_idx + 3) % 4]]
571
+
572
+ # 得到外包矩形即旋转角
573
+ rectange = rectangle_from_parallelogram(parallelogram)
574
+ rectange, rotate_angle = sort_rectangle(rectange)
575
+
576
+ p0_rect, p1_rect, p2_rect, p3_rect = rectange
577
+ # 对当前新加入的文本框区域像素点,根据其到矩形四边的距离修改geo_map
578
+ for y, x in xy_in_poly:
579
+ point = np.array([x, y], dtype=np.float32)
580
+ # top
581
+ geo_map[y, x, 0] = point_dist_to_line(p0_rect, p1_rect, point)
582
+ # right
583
+ geo_map[y, x, 1] = point_dist_to_line(p1_rect, p2_rect, point)
584
+ # down
585
+ geo_map[y, x, 2] = point_dist_to_line(p2_rect, p3_rect, point)
586
+ # left
587
+ geo_map[y, x, 3] = point_dist_to_line(p3_rect, p0_rect, point)
588
+ # angle
589
+ geo_map[y, x, 4] = rotate_angle
590
+ return score_map, geo_map, training_mask
591
+
592
+
593
+ def generator(index,
594
+ input_size=512,
595
+ background_ratio=3. / 8, # 纯背景样本比例
596
+ random_scale=np.array([0.5, 1, 2.0, 3.0]), # 提取多尺度图片信息
597
+ image_list=None):
598
+ try:
599
+ im_fn = image_list[index]
600
+ im = cv2.imread(im_fn)
601
+ if im is None:
602
+ print("can't find image")
603
+ return None, None, None, None, None
604
+ h, w, _ = im.shape
605
+ # 所以要把gt去掉
606
+ txt_fn = im_fn.replace(os.path.basename(im_fn).split('.')[1], 'txt')
607
+ if not os.path.exists(txt_fn):
608
+ print('text file {} does not exists'.format(txt_fn))
609
+ return None, None, None, None, None
610
+ # 加载标注框信息
611
+ text_polys, text_tags = load_annoataion(txt_fn)
612
+
613
+ text_polys, text_tags = check_and_validate_polys(text_polys, text_tags, (h, w))
614
+
615
+ # random scale this image,随机选择一种尺度
616
+ rd_scale = np.random.choice(random_scale)
617
+ im = cv2.resize(im, dsize=None, fx=rd_scale, fy=rd_scale)
618
+ text_polys *= rd_scale
619
+
620
+ # random crop a area from image,3/8���选中的概率,裁剪纯背景的图片
621
+ if np.random.rand() < background_ratio:
622
+ # crop background
623
+ im, text_polys, text_tags = crop_area(im, text_polys, text_tags, crop_background=True)
624
+ if text_polys.shape[0] > 0:
625
+ # print("cannot find background")
626
+ return None, None, None, None, None
627
+ # pad and resize image
628
+ new_h, new_w, _ = im.shape
629
+ max_h_w_i = np.max([new_h, new_w, input_size])
630
+ im_padded = np.zeros((max_h_w_i, max_h_w_i, 3), dtype=np.uint8)
631
+ im_padded[:new_h, :new_w, :] = im.copy()
632
+ # 将裁剪后图片扩充成512*512的图片
633
+ im = cv2.resize(im_padded, dsize=(input_size, input_size))
634
+ score_map = np.zeros((input_size, input_size), dtype=np.uint8)
635
+ geo_map_channels = 5 if cfg.geometry == 'RBOX' else 8
636
+ geo_map = np.zeros((input_size, input_size, geo_map_channels), dtype=np.float32)
637
+ training_mask = np.ones((input_size, input_size), dtype=np.uint8)
638
+ else:
639
+ # 5 / 8的选中的概率,裁剪含文本信息的图片
640
+ im, text_polys, text_tags = crop_area(im, text_polys, text_tags, crop_background=False)
641
+ if text_polys.shape[0] == 0:
642
+ # print("cannot find txt ground")
643
+ return None, None, None, None, None
644
+ h, w, _ = im.shape
645
+ # pad the image to the training input size or the longer side of image
646
+ new_h, new_w, _ = im.shape
647
+ max_h_w_i = np.max([new_h, new_w, input_size])
648
+ im_padded = np.zeros((max_h_w_i, max_h_w_i, 3), dtype=np.uint8)
649
+ im_padded[:new_h, :new_w, :] = im.copy()
650
+ im = im_padded
651
+ # resize the image to input size
652
+ # 填充,resize图像至设定尺寸
653
+ new_h, new_w, _ = im.shape
654
+ resize_h = input_size
655
+ resize_w = input_size
656
+ im = cv2.resize(im, dsize=(resize_w, resize_h))
657
+ # 将文本框坐标标签等比例修改
658
+ resize_ratio_3_x = resize_w / float(new_w)
659
+ resize_ratio_3_y = resize_h / float(new_h)
660
+ text_polys[:, :, 0] *= resize_ratio_3_x
661
+ text_polys[:, :, 1] *= resize_ratio_3_y
662
+ new_h, new_w, _ = im.shape
663
+ score_map, geo_map, training_mask = generate_rbox((new_h, new_w), text_polys, text_tags)
664
+
665
+ # 将一个样本的样本内容和标签信息append
666
+ images = im[:,:,::-1].astype(np.float32)
667
+ # 文件名加入列表
668
+ image_fns = im_fn
669
+ # 512*512取提取四分之一行列
670
+ score_maps = score_map[::4, ::4, np.newaxis].astype(np.float32)
671
+ geo_maps = geo_map[::4, ::4, :].astype(np.float32)
672
+ training_masks = training_mask[::4, ::4, np.newaxis].astype(np.float32)
673
+ # 符合一个样本之后输出
674
+ return images, image_fns, score_maps, geo_maps, training_masks
675
+
676
+ except Exception as e:
677
+ import traceback
678
+ traceback.print_exc()
679
+
680
+ # print("Exception is exist!")
681
+ return None, None, None, None, None
IndicPhotoOCR/detection/east_utils.py ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import os
4
+ from torch.nn import init
5
+ import cv2
6
+ import numpy as np
7
+ import time
8
+ import requests
9
+
10
+ from IndicPhotoOCR.detection import east_config as cfg
11
+ from IndicPhotoOCR.detection import east_preprossing as preprossing
12
+ from IndicPhotoOCR.detection import east_locality_aware_nms as locality_aware_nms
13
+
14
+
15
+
16
+ # Example usage:
17
+ model_info = {
18
+ "east": {
19
+ "paths": [ cfg.checkpoint, cfg.pretrained_basemodel_path],
20
+ "urls" : ["https://github.com/anikde/STocr/releases/download/e0.1.0/epoch_990_checkpoint.pth.tar", "https://github.com/anikde/STocr/releases/download/e0.1.0/mobilenet_v2.pth.tar"]
21
+ },
22
+ }
23
+
24
+ class ModelManager:
25
+ def __init__(self):
26
+ # self.root_model_dir = "bharatOCR/detection/"
27
+ pass
28
+
29
+ def download_model(self, url, path):
30
+ response = requests.get(url, stream=True)
31
+ if response.status_code == 200:
32
+ with open(path, 'wb') as f:
33
+ for chunk in response.iter_content(chunk_size=8192):
34
+ if chunk: # Filter out keep-alive chunks
35
+ f.write(chunk)
36
+ print(f"Downloaded: {path}")
37
+ else:
38
+ print(f"Failed to download from {url}")
39
+
40
+ def ensure_model(self, model_name):
41
+ model_paths = model_info[model_name]["paths"] # Changed to handle multiple paths
42
+ urls = model_info[model_name]["urls"] # Changed to handle multiple URLs
43
+
44
+
45
+ for model_path, url in zip(model_paths, urls):
46
+ # full_model_path = os.path.join(self.root_model_dir, model_path)
47
+
48
+ # Ensure the model path directory exists
49
+ os.makedirs(os.path.dirname(os.path.join(*cfg.pretrained_basemodel_path.split("/"))), exist_ok=True)
50
+
51
+ if not os.path.exists(model_path):
52
+ print(f"Model not found locally. Downloading {model_name} from {url}...")
53
+ self.download_model(url, model_path)
54
+ else:
55
+ print(f"Model already exists at {model_path}. No need to download.")
56
+
57
+
58
+
59
+ # # Initialize ModelManager and ensure Hindi models are downloaded
60
+ model_manager = ModelManager()
61
+ model_manager.ensure_model("east")
62
+
63
+
64
+
65
+ def init_weights(m_list, init_type=cfg.init_type, gain=0.02):
66
+ print("EAST <==> Prepare <==> Init Network'{}' <==> Begin".format(cfg.init_type))
67
+ # this will apply to each layer
68
+ for m in m_list:
69
+ classname = m.__class__.__name__
70
+ if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
71
+ if init_type == 'normal':
72
+ init.normal_(m.weight.data, 0.0, gain)
73
+ elif init_type == 'xavier':
74
+ init.xavier_normal_(m.weight.data, gain=gain)
75
+ elif init_type == 'kaiming':
76
+ init.kaiming_normal_(m.weight.data, a=0, mode='fan_in') # good for relu
77
+ elif init_type == 'orthogonal':
78
+ init.orthogonal_(m.weight.data, gain=gain)
79
+ else:
80
+ raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
81
+
82
+ if hasattr(m, 'bias') and m.bias is not None:
83
+ init.constant_(m.bias.data, 0.0)
84
+ elif classname.find('BatchNorm2d') != -1:
85
+ init.normal_(m.weight.data, 1.0, gain)
86
+ init.constant_(m.bias.data, 0.0)
87
+
88
+ print("EAST <==> Prepare <==> Init Network'{}' <==> Done".format(cfg.init_type))
89
+
90
+
91
+ def Loading_checkpoint(model, optimizer, scheduler, filename='checkpoint.pth.tar'):
92
+ """[summary]
93
+ [description]
94
+ Arguments:
95
+ state {[type]} -- [description] a dict describe some params
96
+ Keyword Arguments:
97
+ filename {str} -- [description] (default: {'checkpoint.pth.tar'})
98
+ """
99
+ weightpath = os.path.abspath(cfg.checkpoint)
100
+ print("EAST <==> Prepare <==> Loading checkpoint '{}' <==> Begin".format(weightpath))
101
+ checkpoint = torch.load(weightpath)
102
+ start_epoch = checkpoint['epoch'] + 1
103
+ model.load_state_dict(checkpoint['state_dict'])
104
+ optimizer.load_state_dict(checkpoint['optimizer'])
105
+ scheduler.load_state_dict(checkpoint['scheduler'])
106
+ print("EAST <==> Prepare <==> Loading checkpoint '{}' <==> Done".format(weightpath))
107
+
108
+ return start_epoch
109
+
110
+
111
+ def save_checkpoint(epoch, model, optimizer, scheduler, filename='checkpoint.pth.tar'):
112
+ """[summary]
113
+ [description]
114
+ Arguments:
115
+ state {[type]} -- [description] a dict describe some params
116
+ Keyword Arguments:
117
+ filename {str} -- [description] (default: {'checkpoint.pth.tar'})
118
+ """
119
+ print('EAST <==> Save weight - epoch {} <==> Begin'.format(epoch))
120
+ state = {
121
+ 'epoch': epoch,
122
+ 'state_dict': model.state_dict(),
123
+ 'optimizer': optimizer.state_dict(),
124
+ 'scheduler': scheduler.state_dict()
125
+ }
126
+ weight_dir = cfg.save_model_path
127
+ if not os.path.exists(weight_dir):
128
+ os.mkdir(weight_dir)
129
+ filename = 'epoch_' + str(epoch) + '_checkpoint.pth.tar'
130
+ file_path = os.path.join(weight_dir, filename)
131
+ torch.save(state, file_path)
132
+ print('EAST <==> Save weight - epoch {} <==> Done'.format(epoch))
133
+
134
+
135
+ class Regularization(torch.nn.Module):
136
+ def __init__(self, model, weight_decay, p=2):
137
+ super(Regularization, self).__init__()
138
+ if weight_decay < 0:
139
+ print("param weight_decay can not <0")
140
+ exit(0)
141
+ self.model = model
142
+ self.weight_decay = weight_decay
143
+ self.p = p
144
+ self.weight_list = self.get_weight(model)
145
+ # self.weight_info(self.weight_list)
146
+
147
+ def to(self, device):
148
+ self.device = device
149
+ super().to(device)
150
+ return self
151
+
152
+ def forward(self, model):
153
+ self.weight_list = self.get_weight(model) # 获得最新的权重
154
+ reg_loss = self.regularization_loss(self.weight_list, self.weight_decay, p=self.p)
155
+ return reg_loss
156
+
157
+ def get_weight(self, model):
158
+ weight_list = []
159
+ for name, param in model.named_parameters():
160
+ if 'weight' in name:
161
+ weight = (name, param)
162
+ weight_list.append(weight)
163
+ return weight_list
164
+
165
+ def regularization_loss(self, weight_list, weight_decay, p=2):
166
+ reg_loss = 0
167
+ for name, w in weight_list:
168
+ l2_reg = torch.norm(w, p=p)
169
+ reg_loss = reg_loss + l2_reg
170
+
171
+ reg_loss = weight_decay * reg_loss
172
+ return reg_loss
173
+
174
+ def weight_info(self, weight_list):
175
+ print("---------------regularization weight---------------")
176
+ for name, w in weight_list:
177
+ print(name)
178
+ print("---------------------------------------------------")
179
+
180
+
181
+ def resize_image(im, max_side_len=2400):
182
+ '''
183
+ resize image to a size multiple of 32 which is required by the network
184
+ :param im: the resized image
185
+ :param max_side_len: limit of max image size to avoid out of memory in gpu
186
+ :return: the resized image and the resize ratio
187
+ '''
188
+ h, w, _ = im.shape
189
+
190
+ resize_w = w
191
+ resize_h = h
192
+
193
+ # limit the max side
194
+ """
195
+ if max(resize_h, resize_w) > max_side_len:
196
+ ratio = float(max_side_len) / resize_h if resize_h > resize_w else float(max_side_len) / resize_w
197
+ else:
198
+ ratio = 1.
199
+
200
+ resize_h = int(resize_h * ratio)
201
+ resize_w = int(resize_w * ratio)
202
+ """
203
+
204
+ resize_h = resize_h if resize_h % 32 == 0 else (resize_h // 32 - 1) * 32
205
+ resize_w = resize_w if resize_w % 32 == 0 else (resize_w // 32 - 1) * 32
206
+ #resize_h, resize_w = 512, 512
207
+ im = cv2.resize(im, (int(resize_w), int(resize_h)))
208
+
209
+ ratio_h = resize_h / float(h)
210
+ ratio_w = resize_w / float(w)
211
+
212
+ return im, (ratio_h, ratio_w)
213
+
214
+
215
+ def detect(score_map, geo_map, timer, score_map_thresh=0.8, box_thresh=0.1, nms_thres=0.2):
216
+ '''
217
+ restore text boxes from score map and geo map
218
+ :param score_map:
219
+ :param geo_map:
220
+ :param timer:
221
+ :param score_map_thresh: threshhold for score map
222
+ :param box_thresh: threshhold for boxes
223
+ :param nms_thres: threshold for nms
224
+ :return:
225
+ '''
226
+
227
+ # score_map 和 geo_map 的维数进行调整
228
+ if len(score_map.shape) == 4:
229
+ score_map = score_map[0, :, :, 0]
230
+ geo_map = geo_map[0, :, :, :]
231
+ # filter the score map
232
+ xy_text = np.argwhere(score_map > score_map_thresh)
233
+ # sort the text boxes via the y axis
234
+ xy_text = xy_text[np.argsort(xy_text[:, 0])]
235
+ # restore
236
+ start = time.time()
237
+ text_box_restored = preprossing.restore_rectangle(xy_text[:, ::-1] * 4,
238
+ geo_map[xy_text[:, 0], xy_text[:, 1], :]) # N*4*2
239
+ # print('{} text boxes before nms'.format(text_box_restored.shape[0]))
240
+ boxes = np.zeros((text_box_restored.shape[0], 9), dtype=np.float32)
241
+ boxes[:, :8] = text_box_restored.reshape((-1, 8))
242
+ boxes[:, 8] = score_map[xy_text[:, 0], xy_text[:, 1]]
243
+ timer['restore'] = time.time() - start
244
+ # nms part
245
+ start = time.time()
246
+ boxes = locality_aware_nms.nms_locality(boxes.astype(np.float64), nms_thres)
247
+ timer['nms'] = time.time() - start
248
+ # print(timer['nms'])
249
+ if boxes.shape[0] == 0:
250
+ return None, timer
251
+
252
+ # here we filter some low score boxes by the average score map, this is different from the orginal paper
253
+ for i, box in enumerate(boxes):
254
+ mask = np.zeros_like(score_map, dtype=np.uint8)
255
+ cv2.fillPoly(mask, box[:8].reshape((-1, 4, 2)).astype(np.int32) // 4, 1)
256
+ boxes[i, 8] = cv2.mean(score_map, mask)[0]
257
+ boxes = boxes[boxes[:, 8] > box_thresh]
258
+ return boxes, timer
259
+
260
+
261
+ def sort_poly(p):
262
+ min_axis = np.argmin(np.sum(p, axis=1))
263
+ p = p[[min_axis, (min_axis + 1) % 4, (min_axis + 2) % 4, (min_axis + 3) % 4]]
264
+ if abs(p[0, 0] - p[1, 0]) > abs(p[0, 1] - p[1, 1]):
265
+ return p
266
+ else:
267
+ return p[[0, 3, 2, 1]]
268
+
269
+
270
+ def mean_image_subtraction(images, means=cfg.means):
271
+ '''
272
+ image normalization
273
+ :param images: bs * w * h * channel
274
+ :param means:
275
+ :return:
276
+ '''
277
+ num_channels = images.data.shape[1]
278
+ if len(means) != num_channels:
279
+ raise ValueError('len(means) must match the number of channels')
280
+ for i in range(num_channels):
281
+ images.data[:, i, :, :] -= means[i]
282
+
283
+ return images
IndicPhotoOCR/detection/textbpn/__init__.py ADDED
File without changes
IndicPhotoOCR/detection/textbpn/cfglib/config.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from easydict import EasyDict
2
+ import torch
3
+ import os
4
+
5
+ config = EasyDict()
6
+
7
+
8
+ # Normalize image
9
+ config.means = (0.485, 0.456, 0.406)
10
+ config.stds = (0.229, 0.224, 0.225)
11
+
12
+ config.gpu = "1"
13
+
14
+ # Experiment name #
15
+ config.exp_name = "Synthtext"
16
+
17
+ # dataloader jobs number
18
+ config.num_workers = 24
19
+
20
+ # batch_size
21
+ config.batch_size = 12
22
+
23
+ # training epoch number
24
+ config.max_epoch = 200
25
+
26
+ config.start_epoch = 0
27
+
28
+ # learning rate
29
+ config.lr = 1e-4
30
+
31
+ # using GPU
32
+ config.cuda = False
33
+
34
+ config.output_dir = 'output'
35
+
36
+ config.input_size = 640
37
+
38
+ # max polygon per image
39
+ # synText, total-text:64; CTW1500: 64; icdar: 64; MLT: 32; TD500: 64.
40
+ config.max_annotation = 64
41
+
42
+ # adj num for graph
43
+ config.adj_num = 4
44
+
45
+ # control points number
46
+ config.num_points = 20
47
+
48
+ # use hard examples (annotated as '#')
49
+ config.use_hard = True
50
+
51
+ # Load data into memory at one time
52
+ config.load_memory = False
53
+
54
+ # prediction on 1/scale feature map
55
+ config.scale = 1
56
+
57
+ # # clip gradient of loss
58
+ config.grad_clip = 25
59
+
60
+ # demo tcl threshold
61
+ config.dis_threshold = 0.4
62
+
63
+ config.cls_threshold = 0.8
64
+
65
+ # Contour approximation factor
66
+ config.approx_factor = 0.004
67
+
68
+
69
+ def update_config(config, extra_config):
70
+ for k, v in vars(extra_config).items():
71
+ config[k] = v
72
+ # print(config.gpu)
73
+ # config.device = torch.device('cuda') if config.cuda else torch.device('cpu')
74
+ config.device = torch.device('cpu')
75
+
76
+
77
+ def print_config(config):
78
+ print('==========Options============')
79
+ for k, v in config.items():
80
+ print('{}: {}'.format(k, v))
81
+ print('=============End=============')
82
+
83
+
84
+
85
+ ################### MY Settings ##################
86
+ config.resume=True
87
+
88
+ config.device="cpu"
89
+
90
+ # config.test_size = [224, 224]
IndicPhotoOCR/detection/textbpn/cfglib/option.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import torch
3
+ import os
4
+ import torch.backends.cudnn as cudnn
5
+
6
+ from datetime import datetime
7
+
8
+
9
+ def str2bool(v):
10
+ return v.lower() in ("yes", "true", "t", "1")
11
+
12
+
13
+ def arg2str(args):
14
+ args_dict = vars(args)
15
+ option_str = datetime.now().strftime('%b%d_%H-%M-%S') + '\n'
16
+
17
+ for k, v in sorted(args_dict.items()):
18
+ option_str += ('{}: {}\n'.format(str(k), str(v)))
19
+
20
+ return option_str
21
+
22
+
23
+ class BaseOptions(object):
24
+
25
+ def __init__(self):
26
+
27
+ self.parser = argparse.ArgumentParser()
28
+
29
+ # basic opts
30
+ self.parser.add_argument('--exp_name', default="TD500", type=str,
31
+ choices=['Synthtext', 'Totaltext', 'Ctw1500','Icdar2015',
32
+ "MLT2017", 'TD500', "MLT2019", "ArT", "ALL"], help='Experiment name')
33
+ self.parser.add_argument("--gpu", default="1", help="set gpu id", type=str)
34
+ self.parser.add_argument('--resume', default=None, type=str, help='Path to target resume checkpoint')
35
+ self.parser.add_argument('--num_workers', default=24, type=int, help='Number of workers used in dataloading')
36
+ self.parser.add_argument('--cuda', default=True, type=str2bool, help='Use cuda to train model')
37
+ self.parser.add_argument('--mgpu', action='store_true', help='Use multi-gpu to train model')
38
+ self.parser.add_argument('--save_dir', default='./model/', help='Path to save checkpoint models')
39
+ self.parser.add_argument('--vis_dir', default='./vis/', help='Path to save visualization images')
40
+ self.parser.add_argument('--log_dir', default='./logs/', help='Path to tensorboard log')
41
+ self.parser.add_argument('--loss', default='CrossEntropyLoss', type=str, help='Training Loss')
42
+ # self.parser.add_argument('--input_channel', default=1, type=int, help='number of input channels' )
43
+ self.parser.add_argument('--pretrain', default=False, type=str2bool, help='Pretrained AutoEncoder model')
44
+ self.parser.add_argument('--verbose', '-v', default=True, type=str2bool, help='Whether to output debug info')
45
+ self.parser.add_argument('--viz', action='store_true', help='Whether to output debug info')
46
+ # self.parser.add_argument('--viz', default=True, type=str2bool, help='Whether to output debug info')
47
+
48
+ # train opts
49
+ self.parser.add_argument('--max_epoch', default=250, type=int, help='Max epochs')
50
+ self.parser.add_argument('--lr', '--learning-rate', default=1e-3, type=float, help='initial learning rate')
51
+ self.parser.add_argument('--lr_adjust', default='fix',
52
+ choices=['fix', 'poly'], type=str, help='Learning Rate Adjust Strategy')
53
+ self.parser.add_argument('--stepvalues', default=[], nargs='+', type=int, help='# of iter to change lr')
54
+ self.parser.add_argument('--weight_decay', '--wd', default=0., type=float, help='Weight decay for SGD')
55
+ self.parser.add_argument('--gamma', default=0.1, type=float, help='Gamma update for SGD lr')
56
+ self.parser.add_argument('--momentum', default=0.9, type=float, help='momentum')
57
+ self.parser.add_argument('--batch_size', default=6, type=int, help='Batch size for training')
58
+ self.parser.add_argument('--optim', default='Adam', type=str, choices=['SGD', 'Adam'], help='Optimizer')
59
+ self.parser.add_argument('--save_freq', default=5, type=int, help='save weights every # epoch')
60
+ self.parser.add_argument('--display_freq', default=10, type=int, help='display training metrics every # iter')
61
+ self.parser.add_argument('--viz_freq', default=50, type=int, help='visualize training process every # iter')
62
+ self.parser.add_argument('--log_freq', default=10000, type=int, help='log to tensorboard every # iterations')
63
+ self.parser.add_argument('--val_freq', default=1000, type=int, help='do validation every # iterations')
64
+
65
+ # backbone
66
+ self.parser.add_argument('--scale', default=1, type=int, help='prediction on 1/scale feature map')
67
+ self.parser.add_argument('--net', default='resnet50', type=str,
68
+ choices=['vgg', 'resnet50', 'resnet18',
69
+ "deformable_resnet18", "deformable_resnet50"],
70
+ help='Network architecture')
71
+ # data args
72
+ self.parser.add_argument('--load_memory', default=False, type=str2bool, help='Load data into memory')
73
+ self.parser.add_argument('--rescale', type=float, default=255.0, help='rescale factor')
74
+ self.parser.add_argument('--input_size', default=640, type=int, help='model input size')
75
+ self.parser.add_argument('--test_size', default=[640, 960], type=int, nargs='+', help='test size')
76
+
77
+ # eval args00
78
+ self.parser.add_argument('--checkepoch', default=1070, type=int, help='Load checkpoint number')
79
+ self.parser.add_argument('--start_epoch', default=0, type=int, help='start epoch number')
80
+ self.parser.add_argument('--cls_threshold', default=0.875, type=float, help='threshold of pse')
81
+ self.parser.add_argument('--dis_threshold', default=0.35, type=float, help='filter the socre < score_i')
82
+
83
+ # demo args
84
+ self.parser.add_argument('--img_root', default=None, type=str, help='Path to deploy images')
85
+
86
+ def parse(self, fixed=None):
87
+
88
+ if fixed is not None:
89
+ args = self.parser.parse_args(fixed)
90
+ else:
91
+ args = self.parser.parse_args()
92
+
93
+ return args
94
+
95
+ def initialize(self, fixed=None):
96
+
97
+ # Parse options
98
+ self.args = self.parse(fixed)
99
+ os.environ['CUDA_VISIBLE_DEVICES'] = self.args.gpu
100
+
101
+ # Setting default torch Tensor type
102
+ if self.args.cuda and torch.cuda.is_available():
103
+ torch.set_default_tensor_type('torch.cuda.FloatTensor')
104
+ cudnn.benchmark = True
105
+ else:
106
+ torch.set_default_tensor_type('torch.FloatTensor')
107
+
108
+ # Create weights saving directory
109
+ if not os.path.exists(self.args.save_dir):
110
+ os.mkdir(self.args.save_dir)
111
+
112
+ # Create weights saving directory of target model
113
+ model_save_path = os.path.join(self.args.save_dir, self.args.exp_name)
114
+
115
+ if not os.path.exists(model_save_path):
116
+ os.mkdir(model_save_path)
117
+
118
+ return self.args
119
+
120
+ def update(self, args, extra_options):
121
+
122
+ for k, v in extra_options.items():
123
+ setattr(args, k, v)
IndicPhotoOCR/detection/textbpn/models/TextBPN_resnet50_300.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b735b9c93c8758972d3b8cfd3ef8e1c09afa8cd9106f4cb11406300b141b1d78
3
+ size 145703602
IndicPhotoOCR/detection/textbpn/network/Reg_loss.py ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ # @Time : 10/1/21
3
+ # @Author : GXYM
4
+ import torch
5
+ from torch import nn
6
+ import numpy as np
7
+ import torch.nn.functional as F
8
+
9
+
10
+ class PolyMatchingLoss(nn.Module):
11
+ def __init__(self, pnum, device, loss_type="L1"):
12
+ super(PolyMatchingLoss, self).__init__()
13
+
14
+ self.pnum = pnum
15
+ self.device = device
16
+ self.loss_type = loss_type
17
+ self.smooth_L1 = F.smooth_l1_loss
18
+ self.L2_loss = torch.nn.MSELoss(reduce=False, size_average=False)
19
+
20
+ batch_size = 1
21
+ pidxall = np.zeros(shape=(batch_size, pnum, pnum), dtype=np.int32)
22
+ for b in range(batch_size):
23
+ for i in range(pnum):
24
+ pidx = (np.arange(pnum) + i) % pnum
25
+ pidxall[b, i] = pidx
26
+
27
+ pidxall = torch.from_numpy(np.reshape(pidxall, newshape=(batch_size, -1))).to(device)
28
+ self.feature_id = pidxall.unsqueeze_(2).long().expand(pidxall.size(0), pidxall.size(1), 2).detach()
29
+ print(self.feature_id.shape)
30
+
31
+ def match_loss(self, pred, gt):
32
+ batch_size = pred.shape[0]
33
+ feature_id = self.feature_id.expand(batch_size, self.feature_id.size(1), 2)
34
+
35
+ gt_expand = torch.gather(gt, 1, feature_id).view(batch_size, self.pnum, self.pnum, 2)
36
+ pred_expand = pred.unsqueeze(1)
37
+
38
+ if self.loss_type == "L2":
39
+ dis = self.L2_loss(pred_expand, gt_expand)
40
+ dis = dis.sum(3).sqrt().mean(2)
41
+ elif self.loss_type == "L1":
42
+ dis = self.smooth_L1(pred_expand, gt_expand, reduction='none')
43
+ dis = dis.sum(3).mean(2)
44
+
45
+ min_dis, min_id = torch.min(dis, dim=1, keepdim=True)
46
+
47
+ return min_dis
48
+
49
+ def forward(self, pred_list, gt):
50
+ loss = torch.tensor(0.)
51
+ for pred in pred_list:
52
+ loss += torch.mean(self.match_loss(pred, gt))
53
+
54
+ return loss / torch.tensor(len(pred_list))
55
+
56
+ # los = []
57
+ # for pred in pred_list:
58
+ # los.append(self.match_loss(pred, gt))
59
+ #
60
+ # los_b = torch.tensor(0.)
61
+ # loss_c = torch.tensor(0.)
62
+ # for i, _ in enumerate(los):
63
+ # los_b += torch.mean(los[i])
64
+ # loss_c += (torch.mean(torch.clamp(los[i] - los[i - 1], min=0.0)) if i > 0 else torch.tensor(0.))
65
+ # loss = los_b / torch.tensor(len(los)) + 0.5*loss_c / torch.tensor(len(los)-1)
66
+ #
67
+ # return loss
68
+
69
+
70
+ class AttentionLoss(nn.Module):
71
+ def __init__(self, beta=4, gamma=0.5):
72
+ super(AttentionLoss, self).__init__()
73
+
74
+ self.beta = beta
75
+ self.gamma = gamma
76
+
77
+ def forward(self, pred, gt):
78
+ num_pos = torch.sum(gt)
79
+ num_neg = torch.sum(1 - gt)
80
+ alpha = num_neg / (num_pos + num_neg)
81
+ edge_beta = torch.pow(self.beta, torch.pow(1 - pred, self.gamma))
82
+ bg_beta = torch.pow(self.beta, torch.pow(pred, self.gamma))
83
+
84
+ loss = 0
85
+ loss = loss - alpha * edge_beta * torch.log(pred) * gt
86
+ loss = loss - (1 - alpha) * bg_beta * torch.log(1 - pred) * (1 - gt)
87
+ return torch.mean(loss)
88
+
89
+
90
+ class GeoCrossEntropyLoss(nn.Module):
91
+ def __init__(self):
92
+ super(GeoCrossEntropyLoss, self).__init__()
93
+
94
+ def forward(self, output, target, poly):
95
+ output = torch.nn.functional.softmax(output, dim=1)
96
+ output = torch.log(torch.clamp(output, min=1e-4))
97
+ poly = poly.view(poly.size(0), 4, poly.size(1) // 4, 2)
98
+ target = target[..., None, None].expand(poly.size(0), poly.size(1), 1, poly.size(3))
99
+ target_poly = torch.gather(poly, 2, target)
100
+ sigma = (poly[:, :, 0] - poly[:, :, 1]).pow(2).sum(2, keepdim=True)
101
+ kernel = torch.exp(-(poly - target_poly).pow(2).sum(3) / (sigma / 3))
102
+ loss = -(output * kernel.transpose(2, 1)).sum(1).mean()
103
+ return loss
104
+
105
+
106
+ class AELoss(nn.Module):
107
+ def __init__(self):
108
+ super(AELoss, self).__init__()
109
+
110
+ def forward(self, ae, ind, ind_mask):
111
+ """
112
+ ae: [b, 1, h, w]
113
+ ind: [b, max_objs, max_parts]
114
+ ind_mask: [b, max_objs, max_parts]
115
+ obj_mask: [b, max_objs]
116
+ """
117
+ # first index
118
+ b, _, h, w = ae.shape
119
+ b, max_objs, max_parts = ind.shape
120
+ obj_mask = torch.sum(ind_mask, dim=2) != 0
121
+
122
+ ae = ae.view(b, h * w, 1)
123
+ seed_ind = ind.view(b, max_objs * max_parts, 1)
124
+ tag = ae.gather(1, seed_ind).view(b, max_objs, max_parts)
125
+
126
+ # compute the mean
127
+ tag_mean = tag * ind_mask
128
+ tag_mean = tag_mean.sum(2) / (ind_mask.sum(2) + 1e-4)
129
+
130
+ # pull ae of the same object to their mean
131
+ pull_dist = (tag - tag_mean.unsqueeze(2)).pow(2) * ind_mask
132
+ obj_num = obj_mask.sum(dim=1).float()
133
+ pull = (pull_dist.sum(dim=(1, 2)) / (obj_num + 1e-4)).sum()
134
+ pull /= b
135
+
136
+ # push away the mean of different objects
137
+ push_dist = torch.abs(tag_mean.unsqueeze(1) - tag_mean.unsqueeze(2))
138
+ push_dist = 1 - push_dist
139
+ push_dist = nn.functional.relu(push_dist, inplace=True)
140
+ obj_mask = (obj_mask.unsqueeze(1) + obj_mask.unsqueeze(2)) == 2
141
+ push_dist = push_dist * obj_mask.float()
142
+ push = ((push_dist.sum(dim=(1, 2)) - obj_num) / (obj_num * (obj_num - 1) + 1e-4)).sum()
143
+ push /= b
144
+ return pull, push
145
+
146
+
147
+ def smooth_l1_loss(inputs, target, sigma=9.0):
148
+ try:
149
+ diff = torch.abs(inputs - target)
150
+ less_one = (diff < 1.0 / sigma).float()
151
+ loss = less_one * 0.5 * diff ** 2 * sigma \
152
+ + torch.abs(torch.tensor(1.0) - less_one) * (diff - 0.5 / sigma)
153
+ loss = torch.mean(loss) if loss.numel() > 0 else torch.tensor(0.0)
154
+ except Exception as e:
155
+ print('RPN_REGR_Loss Exception:', e)
156
+ loss = torch.tensor(0.0)
157
+
158
+ return loss
159
+
160
+
161
+ def _neg_loss(pred, gt):
162
+ ''' Modified focal loss. Exactly the same as CornerNet.
163
+ Runs faster and costs a little bit more memory
164
+ Arguments:
165
+ pred (batch x c x h x w)
166
+ gt_regr (batch x c x h x w)
167
+ '''
168
+ pos_inds = gt.eq(1).float()
169
+ neg_inds = gt.lt(1).float()
170
+
171
+ neg_weights = torch.pow(1 - gt, 4)
172
+
173
+ loss = 0
174
+
175
+ pos_loss = torch.log(pred) * torch.pow(1 - pred, 2) * pos_inds
176
+ neg_loss = torch.log(1 - pred) * torch.pow(pred, 2) * neg_weights * neg_inds
177
+
178
+ num_pos = pos_inds.float().sum()
179
+ pos_loss = pos_loss.sum()
180
+ neg_loss = neg_loss.sum()
181
+
182
+ if num_pos == 0:
183
+ loss = loss - neg_loss
184
+ else:
185
+ loss = loss - (pos_loss + neg_loss) / num_pos
186
+ return loss
187
+
188
+
189
+ class FocalLoss(nn.Module):
190
+ '''nn.Module warpper for focal loss'''
191
+ def __init__(self):
192
+ super(FocalLoss, self).__init__()
193
+ self.neg_loss = _neg_loss
194
+
195
+ def forward(self, out, target):
196
+ return self.neg_loss(out, target)
IndicPhotoOCR/detection/textbpn/network/Seg_loss.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ # @Time : 10/1/21
3
+ # @Author : GXYM
4
+ import torch
5
+ from torch import nn
6
+ import numpy as np
7
+
8
+
9
+ class SegmentLoss(nn.Module):
10
+ def __init__(self, Lambda, ratio=3, reduction='mean'):
11
+ """Implement PSE Loss.
12
+ """
13
+ super(SegmentLoss, self).__init__()
14
+ assert reduction in ['mean', 'sum'], " reduction must in ['mean','sum']"
15
+ self.Lambda = Lambda
16
+ self.ratio = ratio
17
+ self.reduction = reduction
18
+
19
+ def forward(self, outputs, labels, training_masks, th=0.5):
20
+ texts = outputs[:, -1, :, :]
21
+ kernels = outputs[:, :-1, :, :]
22
+ gt_texts = labels[:, -1, :, :]
23
+ gt_kernels = labels[:, :-1, :, :]
24
+
25
+ selected_masks = self.ohem_batch(texts, gt_texts, training_masks)
26
+ selected_masks = selected_masks.to(outputs.device)
27
+
28
+ loss_text = self.dice_loss(texts, gt_texts, selected_masks)
29
+
30
+ loss_kernels = []
31
+ # mask0 = torch.sigmoid(texts).data.cpu().numpy()
32
+ mask0 = texts.data.cpu().numpy()
33
+ mask1 = training_masks.data.cpu().numpy()
34
+ selected_masks = ((mask0 > th) & (mask1 > th)).astype('float32')
35
+ selected_masks = torch.from_numpy(selected_masks).float()
36
+ selected_masks = selected_masks.to(outputs.device)
37
+ kernels_num = gt_kernels.size()[1]
38
+ for i in range(kernels_num):
39
+ kernel_i = kernels[:, i, :, :]
40
+ gt_kernel_i = gt_kernels[:, i, :, :]
41
+ loss_kernel_i = self.dice_loss(kernel_i, gt_kernel_i, selected_masks)
42
+ loss_kernels.append(loss_kernel_i)
43
+ loss_kernels = torch.stack(loss_kernels).mean(0)
44
+ if self.reduction == 'mean':
45
+ loss_text = loss_text.mean()
46
+ loss_kernels = loss_kernels.mean()
47
+ elif self.reduction == 'sum':
48
+ loss_text = loss_text.sum()
49
+ loss_kernels = loss_kernels.sum()
50
+
51
+ loss = self.Lambda *loss_text + (1-self.Lambda)*loss_kernels
52
+ return loss_text, loss_kernels, loss
53
+
54
+ def dice_loss(self, input, target, mask):
55
+ # input = torch.sigmoid(input)
56
+
57
+ input = input.contiguous().view(input.size()[0], -1)
58
+ target = target.contiguous().view(target.size()[0], -1)
59
+ mask = mask.contiguous().view(mask.size()[0], -1)
60
+
61
+ input = input * mask
62
+ target = (target.float()) * mask
63
+
64
+ a = torch.sum(input * target, 1)
65
+ b = torch.sum(input * input, 1) + 0.001
66
+ c = torch.sum(target * target, 1) + 0.001
67
+ d = (2 * a) / (b + c)
68
+ return 1 - d
69
+
70
+ def ohem_single(self, score, gt_text, training_mask, th=0.5):
71
+ pos_num = (int)(np.sum(gt_text > th)) - (int)(np.sum((gt_text > th) & (training_mask <= th)))
72
+
73
+ if pos_num == 0:
74
+ # selected_mask = gt_text.copy() * 0 # may be not good
75
+ selected_mask = training_mask
76
+ selected_mask = selected_mask.reshape(1, selected_mask.shape[0], selected_mask.shape[1]).astype('float32')
77
+ return selected_mask
78
+
79
+ neg_num = (int)(np.sum(gt_text <= th))
80
+ neg_num = (int)(min(pos_num * 3, neg_num))
81
+
82
+ if neg_num == 0:
83
+ selected_mask = training_mask
84
+ selected_mask = selected_mask.reshape(1, selected_mask.shape[0], selected_mask.shape[1]).astype('float32')
85
+ return selected_mask
86
+
87
+ neg_score = score[gt_text <= th]
88
+ # 将负样本得分从高到低排序
89
+ neg_score_sorted = np.sort(-neg_score)
90
+ threshold = -neg_score_sorted[neg_num - 1]
91
+ # 选出 得分高的 负样本 和正样本 的 mask
92
+ selected_mask = ((score >= threshold) | (gt_text > th)) & (training_mask > th)
93
+ selected_mask = selected_mask.reshape(1, selected_mask.shape[0], selected_mask.shape[1]).astype('float32')
94
+ return selected_mask
95
+
96
+ def ohem_batch(self, scores, gt_texts, training_masks):
97
+ scores = scores.data.cpu().numpy()
98
+ gt_texts = gt_texts.data.cpu().numpy()
99
+ training_masks = training_masks.data.cpu().numpy()
100
+ selected_masks = []
101
+ for i in range(scores.shape[0]):
102
+ selected_masks.append(self.ohem_single(scores[i, :, :], gt_texts[i, :, :], training_masks[i, :, :]))
103
+
104
+ selected_masks = np.concatenate(selected_masks, 0)
105
+ selected_masks = torch.from_numpy(selected_masks).float()
106
+
107
+ return selected_masks
IndicPhotoOCR/detection/textbpn/network/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from . import *
IndicPhotoOCR/detection/textbpn/network/backbone/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from .resnet import resnet18, resnet34, resnet50, resnet101, deformable_resnet50, deformable_resnet18
IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/Makefile ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ rm *.so
3
+ python setup.py build_ext --inplace
4
+ rm -rf ./build
5
+
6
+
IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/Makefile.sh ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ rm *.so
3
+ python setup.py build_ext --inplace
4
+ rm -rf ./build
5
+
6
+
IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/__init__.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .functions.deform_conv import deform_conv, modulated_deform_conv
2
+ from .functions.deform_pool import deform_roi_pooling
3
+ from .modules.deform_conv import (DeformConv, ModulatedDeformConv,
4
+ DeformConvPack, ModulatedDeformConvPack)
5
+ from .modules.deform_pool import (DeformRoIPooling, DeformRoIPoolingPack,
6
+ ModulatedDeformRoIPoolingPack)
7
+
8
+ __all__ = [
9
+ 'DeformConv', 'DeformConvPack', 'ModulatedDeformConv',
10
+ 'ModulatedDeformConvPack', 'DeformRoIPooling', 'DeformRoIPoolingPack',
11
+ 'ModulatedDeformRoIPoolingPack', 'deform_conv', 'modulated_deform_conv',
12
+ 'deform_roi_pooling'
13
+ ]
IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/functions/__init__.py ADDED
File without changes
IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/functions/deform_conv.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch.autograd import Function
3
+ from torch.nn.modules.utils import _pair
4
+
5
+ from .. import deform_conv_cuda
6
+
7
+
8
+ class DeformConvFunction(Function):
9
+
10
+ @staticmethod
11
+ def forward(ctx,
12
+ input,
13
+ offset,
14
+ weight,
15
+ stride=1,
16
+ padding=0,
17
+ dilation=1,
18
+ groups=1,
19
+ deformable_groups=1,
20
+ im2col_step=64):
21
+ if input is not None and input.dim() != 4:
22
+ raise ValueError(
23
+ "Expected 4D tensor as input, got {}D tensor instead.".format(
24
+ input.dim()))
25
+ ctx.stride = _pair(stride)
26
+ ctx.padding = _pair(padding)
27
+ ctx.dilation = _pair(dilation)
28
+ ctx.groups = groups
29
+ ctx.deformable_groups = deformable_groups
30
+ ctx.im2col_step = im2col_step
31
+
32
+ ctx.save_for_backward(input, offset, weight)
33
+
34
+ output = input.new_empty(
35
+ DeformConvFunction._output_size(input, weight, ctx.padding,
36
+ ctx.dilation, ctx.stride))
37
+
38
+ ctx.bufs_ = [input.new_empty(0), input.new_empty(0)] # columns, ones
39
+
40
+ if not input.is_cuda:
41
+ raise NotImplementedError
42
+ else:
43
+ cur_im2col_step = min(ctx.im2col_step, input.shape[0])
44
+ assert (input.shape[0] %
45
+ cur_im2col_step) == 0, 'im2col step must divide batchsize'
46
+ deform_conv_cuda.deform_conv_forward_cuda(
47
+ input, weight, offset, output, ctx.bufs_[0], ctx.bufs_[1],
48
+ weight.size(3), weight.size(2), ctx.stride[1], ctx.stride[0],
49
+ ctx.padding[1], ctx.padding[0], ctx.dilation[1],
50
+ ctx.dilation[0], ctx.groups, ctx.deformable_groups,
51
+ cur_im2col_step)
52
+ return output
53
+
54
+ @staticmethod
55
+ def backward(ctx, grad_output):
56
+ input, offset, weight = ctx.saved_tensors
57
+
58
+ grad_input = grad_offset = grad_weight = None
59
+
60
+ if not grad_output.is_cuda:
61
+ raise NotImplementedError
62
+ else:
63
+ cur_im2col_step = min(ctx.im2col_step, input.shape[0])
64
+ assert (input.shape[0] %
65
+ cur_im2col_step) == 0, 'im2col step must divide batchsize'
66
+
67
+ if ctx.needs_input_grad[0] or ctx.needs_input_grad[1]:
68
+ grad_input = torch.zeros_like(input)
69
+ grad_offset = torch.zeros_like(offset)
70
+ deform_conv_cuda.deform_conv_backward_input_cuda(
71
+ input, offset, grad_output, grad_input,
72
+ grad_offset, weight, ctx.bufs_[0], weight.size(3),
73
+ weight.size(2), ctx.stride[1], ctx.stride[0],
74
+ ctx.padding[1], ctx.padding[0], ctx.dilation[1],
75
+ ctx.dilation[0], ctx.groups, ctx.deformable_groups,
76
+ cur_im2col_step)
77
+
78
+ if ctx.needs_input_grad[2]:
79
+ grad_weight = torch.zeros_like(weight)
80
+ deform_conv_cuda.deform_conv_backward_parameters_cuda(
81
+ input, offset, grad_output,
82
+ grad_weight, ctx.bufs_[0], ctx.bufs_[1], weight.size(3),
83
+ weight.size(2), ctx.stride[1], ctx.stride[0],
84
+ ctx.padding[1], ctx.padding[0], ctx.dilation[1],
85
+ ctx.dilation[0], ctx.groups, ctx.deformable_groups, 1,
86
+ cur_im2col_step)
87
+
88
+ return (grad_input, grad_offset, grad_weight, None, None, None, None,
89
+ None)
90
+
91
+ @staticmethod
92
+ def _output_size(input, weight, padding, dilation, stride):
93
+ channels = weight.size(0)
94
+ output_size = (input.size(0), channels)
95
+ for d in range(input.dim() - 2):
96
+ in_size = input.size(d + 2)
97
+ pad = padding[d]
98
+ kernel = dilation[d] * (weight.size(d + 2) - 1) + 1
99
+ stride_ = stride[d]
100
+ output_size += ((in_size + (2 * pad) - kernel) // stride_ + 1, )
101
+ if not all(map(lambda s: s > 0, output_size)):
102
+ raise ValueError(
103
+ "convolution input is too small (output would be {})".format(
104
+ 'x'.join(map(str, output_size))))
105
+ return output_size
106
+
107
+
108
+ class ModulatedDeformConvFunction(Function):
109
+
110
+ @staticmethod
111
+ def forward(ctx,
112
+ input,
113
+ offset,
114
+ mask,
115
+ weight,
116
+ bias=None,
117
+ stride=1,
118
+ padding=0,
119
+ dilation=1,
120
+ groups=1,
121
+ deformable_groups=1):
122
+ ctx.stride = stride
123
+ ctx.padding = padding
124
+ ctx.dilation = dilation
125
+ ctx.groups = groups
126
+ ctx.deformable_groups = deformable_groups
127
+ ctx.with_bias = bias is not None
128
+ if not ctx.with_bias:
129
+ bias = input.new_empty(1) # fake tensor
130
+ if not input.is_cuda:
131
+ raise NotImplementedError
132
+ if weight.requires_grad or mask.requires_grad or offset.requires_grad \
133
+ or input.requires_grad:
134
+ ctx.save_for_backward(input, offset, mask, weight, bias)
135
+ output = input.new_empty(
136
+ ModulatedDeformConvFunction._infer_shape(ctx, input, weight))
137
+ ctx._bufs = [input.new_empty(0), input.new_empty(0)]
138
+ deform_conv_cuda.modulated_deform_conv_cuda_forward(
139
+ input, weight, bias, ctx._bufs[0], offset, mask, output,
140
+ ctx._bufs[1], weight.shape[2], weight.shape[3], ctx.stride,
141
+ ctx.stride, ctx.padding, ctx.padding, ctx.dilation, ctx.dilation,
142
+ ctx.groups, ctx.deformable_groups, ctx.with_bias)
143
+ return output
144
+
145
+ @staticmethod
146
+ def backward(ctx, grad_output):
147
+ if not grad_output.is_cuda:
148
+ raise NotImplementedError
149
+ input, offset, mask, weight, bias = ctx.saved_tensors
150
+ grad_input = torch.zeros_like(input)
151
+ grad_offset = torch.zeros_like(offset)
152
+ grad_mask = torch.zeros_like(mask)
153
+ grad_weight = torch.zeros_like(weight)
154
+ grad_bias = torch.zeros_like(bias)
155
+ deform_conv_cuda.modulated_deform_conv_cuda_backward(
156
+ input, weight, bias, ctx._bufs[0], offset, mask, ctx._bufs[1],
157
+ grad_input, grad_weight, grad_bias, grad_offset, grad_mask,
158
+ grad_output, weight.shape[2], weight.shape[3], ctx.stride,
159
+ ctx.stride, ctx.padding, ctx.padding, ctx.dilation, ctx.dilation,
160
+ ctx.groups, ctx.deformable_groups, ctx.with_bias)
161
+ if not ctx.with_bias:
162
+ grad_bias = None
163
+
164
+ return (grad_input, grad_offset, grad_mask, grad_weight, grad_bias,
165
+ None, None, None, None, None)
166
+
167
+ @staticmethod
168
+ def _infer_shape(ctx, input, weight):
169
+ n = input.size(0)
170
+ channels_out = weight.size(0)
171
+ height, width = input.shape[2:4]
172
+ kernel_h, kernel_w = weight.shape[2:4]
173
+ height_out = (height + 2 * ctx.padding -
174
+ (ctx.dilation * (kernel_h - 1) + 1)) // ctx.stride + 1
175
+ width_out = (width + 2 * ctx.padding -
176
+ (ctx.dilation * (kernel_w - 1) + 1)) // ctx.stride + 1
177
+ return n, channels_out, height_out, width_out
178
+
179
+
180
+ deform_conv = DeformConvFunction.apply
181
+ modulated_deform_conv = ModulatedDeformConvFunction.apply
IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/functions/deform_pool.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch.autograd import Function
3
+
4
+ from .. import deform_pool_cuda
5
+
6
+
7
+ class DeformRoIPoolingFunction(Function):
8
+
9
+ @staticmethod
10
+ def forward(ctx,
11
+ data,
12
+ rois,
13
+ offset,
14
+ spatial_scale,
15
+ out_size,
16
+ out_channels,
17
+ no_trans,
18
+ group_size=1,
19
+ part_size=None,
20
+ sample_per_part=4,
21
+ trans_std=.0):
22
+ ctx.spatial_scale = spatial_scale
23
+ ctx.out_size = out_size
24
+ ctx.out_channels = out_channels
25
+ ctx.no_trans = no_trans
26
+ ctx.group_size = group_size
27
+ ctx.part_size = out_size if part_size is None else part_size
28
+ ctx.sample_per_part = sample_per_part
29
+ ctx.trans_std = trans_std
30
+
31
+ assert 0.0 <= ctx.trans_std <= 1.0
32
+ if not data.is_cuda:
33
+ raise NotImplementedError
34
+
35
+ n = rois.shape[0]
36
+ output = data.new_empty(n, out_channels, out_size, out_size)
37
+ output_count = data.new_empty(n, out_channels, out_size, out_size)
38
+ deform_pool_cuda.deform_psroi_pooling_cuda_forward(
39
+ data, rois, offset, output, output_count, ctx.no_trans,
40
+ ctx.spatial_scale, ctx.out_channels, ctx.group_size, ctx.out_size,
41
+ ctx.part_size, ctx.sample_per_part, ctx.trans_std)
42
+
43
+ if data.requires_grad or rois.requires_grad or offset.requires_grad:
44
+ ctx.save_for_backward(data, rois, offset)
45
+ ctx.output_count = output_count
46
+
47
+ return output
48
+
49
+ @staticmethod
50
+ def backward(ctx, grad_output):
51
+ if not grad_output.is_cuda:
52
+ raise NotImplementedError
53
+
54
+ data, rois, offset = ctx.saved_tensors
55
+ output_count = ctx.output_count
56
+ grad_input = torch.zeros_like(data)
57
+ grad_rois = None
58
+ grad_offset = torch.zeros_like(offset)
59
+
60
+ deform_pool_cuda.deform_psroi_pooling_cuda_backward(
61
+ grad_output, data, rois, offset, output_count, grad_input,
62
+ grad_offset, ctx.no_trans, ctx.spatial_scale, ctx.out_channels,
63
+ ctx.group_size, ctx.out_size, ctx.part_size, ctx.sample_per_part,
64
+ ctx.trans_std)
65
+ return (grad_input, grad_rois, grad_offset, None, None, None, None,
66
+ None, None, None, None)
67
+
68
+
69
+ deform_roi_pooling = DeformRoIPoolingFunction.apply
IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/modules/__init__.py ADDED
File without changes
IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/modules/deform_conv.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ import torch
4
+ import torch.nn as nn
5
+ from torch.nn.modules.utils import _pair
6
+
7
+ from ..functions.deform_conv import deform_conv, modulated_deform_conv
8
+
9
+
10
+ class DeformConv(nn.Module):
11
+
12
+ def __init__(self,
13
+ in_channels,
14
+ out_channels,
15
+ kernel_size,
16
+ stride=1,
17
+ padding=0,
18
+ dilation=1,
19
+ groups=1,
20
+ deformable_groups=1,
21
+ bias=False):
22
+ super(DeformConv, self).__init__()
23
+
24
+ assert not bias
25
+ assert in_channels % groups == 0, \
26
+ 'in_channels {} cannot be divisible by groups {}'.format(
27
+ in_channels, groups)
28
+ assert out_channels % groups == 0, \
29
+ 'out_channels {} cannot be divisible by groups {}'.format(
30
+ out_channels, groups)
31
+
32
+ self.in_channels = in_channels
33
+ self.out_channels = out_channels
34
+ self.kernel_size = _pair(kernel_size)
35
+ self.stride = _pair(stride)
36
+ self.padding = _pair(padding)
37
+ self.dilation = _pair(dilation)
38
+ self.groups = groups
39
+ self.deformable_groups = deformable_groups
40
+
41
+ self.weight = nn.Parameter(
42
+ torch.Tensor(out_channels, in_channels // self.groups,
43
+ *self.kernel_size))
44
+
45
+ self.reset_parameters()
46
+
47
+ def reset_parameters(self):
48
+ n = self.in_channels
49
+ for k in self.kernel_size:
50
+ n *= k
51
+ stdv = 1. / math.sqrt(n)
52
+ self.weight.data.uniform_(-stdv, stdv)
53
+
54
+ def forward(self, x, offset):
55
+ return deform_conv(x, offset, self.weight, self.stride, self.padding,
56
+ self.dilation, self.groups, self.deformable_groups)
57
+
58
+
59
+ class DeformConvPack(DeformConv):
60
+
61
+ def __init__(self, *args, **kwargs):
62
+ super(DeformConvPack, self).__init__(*args, **kwargs)
63
+
64
+ self.conv_offset = nn.Conv2d(
65
+ self.in_channels,
66
+ self.deformable_groups * 2 * self.kernel_size[0] *
67
+ self.kernel_size[1],
68
+ kernel_size=self.kernel_size,
69
+ stride=_pair(self.stride),
70
+ padding=_pair(self.padding),
71
+ bias=True)
72
+ self.init_offset()
73
+
74
+ def init_offset(self):
75
+ self.conv_offset.weight.data.zero_()
76
+ self.conv_offset.bias.data.zero_()
77
+
78
+ def forward(self, x):
79
+ offset = self.conv_offset(x)
80
+ return deform_conv(x, offset, self.weight, self.stride, self.padding,
81
+ self.dilation, self.groups, self.deformable_groups)
82
+
83
+
84
+ class ModulatedDeformConv(nn.Module):
85
+
86
+ def __init__(self,
87
+ in_channels,
88
+ out_channels,
89
+ kernel_size,
90
+ stride=1,
91
+ padding=0,
92
+ dilation=1,
93
+ groups=1,
94
+ deformable_groups=1,
95
+ bias=True):
96
+ super(ModulatedDeformConv, self).__init__()
97
+ self.in_channels = in_channels
98
+ self.out_channels = out_channels
99
+ self.kernel_size = _pair(kernel_size)
100
+ self.stride = stride
101
+ self.padding = padding
102
+ self.dilation = dilation
103
+ self.groups = groups
104
+ self.deformable_groups = deformable_groups
105
+ self.with_bias = bias
106
+
107
+ self.weight = nn.Parameter(
108
+ torch.Tensor(out_channels, in_channels // groups,
109
+ *self.kernel_size))
110
+ if bias:
111
+ self.bias = nn.Parameter(torch.Tensor(out_channels))
112
+ else:
113
+ self.register_parameter('bias', None)
114
+ self.reset_parameters()
115
+
116
+ def reset_parameters(self):
117
+ n = self.in_channels
118
+ for k in self.kernel_size:
119
+ n *= k
120
+ stdv = 1. / math.sqrt(n)
121
+ self.weight.data.uniform_(-stdv, stdv)
122
+ if self.bias is not None:
123
+ self.bias.data.zero_()
124
+
125
+ def forward(self, x, offset, mask):
126
+ return modulated_deform_conv(x, offset, mask, self.weight, self.bias,
127
+ self.stride, self.padding, self.dilation,
128
+ self.groups, self.deformable_groups)
129
+
130
+
131
+ class ModulatedDeformConvPack(ModulatedDeformConv):
132
+
133
+ def __init__(self, *args, **kwargs):
134
+ super(ModulatedDeformConvPack, self).__init__(*args, **kwargs)
135
+
136
+ self.conv_offset_mask = nn.Conv2d(
137
+ self.in_channels,
138
+ self.deformable_groups * 3 * self.kernel_size[0] *
139
+ self.kernel_size[1],
140
+ kernel_size=self.kernel_size,
141
+ stride=_pair(self.stride),
142
+ padding=_pair(self.padding),
143
+ bias=True)
144
+ self.init_offset()
145
+
146
+ def init_offset(self):
147
+ self.conv_offset_mask.weight.data.zero_()
148
+ self.conv_offset_mask.bias.data.zero_()
149
+
150
+ def forward(self, x):
151
+ out = self.conv_offset_mask(x)
152
+ o1, o2, mask = torch.chunk(out, 3, dim=1)
153
+ offset = torch.cat((o1, o2), dim=1)
154
+ mask = torch.sigmoid(mask)
155
+ return modulated_deform_conv(x, offset, mask, self.weight, self.bias,
156
+ self.stride, self.padding, self.dilation,
157
+ self.groups, self.deformable_groups)
IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/modules/deform_pool.py ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from torch import nn
2
+
3
+ from ..functions.deform_pool import deform_roi_pooling
4
+
5
+
6
+ class DeformRoIPooling(nn.Module):
7
+
8
+ def __init__(self,
9
+ spatial_scale,
10
+ out_size,
11
+ out_channels,
12
+ no_trans,
13
+ group_size=1,
14
+ part_size=None,
15
+ sample_per_part=4,
16
+ trans_std=.0):
17
+ super(DeformRoIPooling, self).__init__()
18
+ self.spatial_scale = spatial_scale
19
+ self.out_size = out_size
20
+ self.out_channels = out_channels
21
+ self.no_trans = no_trans
22
+ self.group_size = group_size
23
+ self.part_size = out_size if part_size is None else part_size
24
+ self.sample_per_part = sample_per_part
25
+ self.trans_std = trans_std
26
+
27
+ def forward(self, data, rois, offset):
28
+ if self.no_trans:
29
+ offset = data.new_empty(0)
30
+ return deform_roi_pooling(
31
+ data, rois, offset, self.spatial_scale, self.out_size,
32
+ self.out_channels, self.no_trans, self.group_size, self.part_size,
33
+ self.sample_per_part, self.trans_std)
34
+
35
+
36
+ class DeformRoIPoolingPack(DeformRoIPooling):
37
+
38
+ def __init__(self,
39
+ spatial_scale,
40
+ out_size,
41
+ out_channels,
42
+ no_trans,
43
+ group_size=1,
44
+ part_size=None,
45
+ sample_per_part=4,
46
+ trans_std=.0,
47
+ num_offset_fcs=3,
48
+ deform_fc_channels=1024):
49
+ super(DeformRoIPoolingPack,
50
+ self).__init__(spatial_scale, out_size, out_channels, no_trans,
51
+ group_size, part_size, sample_per_part, trans_std)
52
+
53
+ self.num_offset_fcs = num_offset_fcs
54
+ self.deform_fc_channels = deform_fc_channels
55
+
56
+ if not no_trans:
57
+ seq = []
58
+ ic = self.out_size * self.out_size * self.out_channels
59
+ for i in range(self.num_offset_fcs):
60
+ if i < self.num_offset_fcs - 1:
61
+ oc = self.deform_fc_channels
62
+ else:
63
+ oc = self.out_size * self.out_size * 2
64
+ seq.append(nn.Linear(ic, oc))
65
+ ic = oc
66
+ if i < self.num_offset_fcs - 1:
67
+ seq.append(nn.ReLU(inplace=True))
68
+ self.offset_fc = nn.Sequential(*seq)
69
+ self.offset_fc[-1].weight.data.zero_()
70
+ self.offset_fc[-1].bias.data.zero_()
71
+
72
+ def forward(self, data, rois):
73
+ assert data.size(1) == self.out_channels
74
+ if self.no_trans:
75
+ offset = data.new_empty(0)
76
+ return deform_roi_pooling(
77
+ data, rois, offset, self.spatial_scale, self.out_size,
78
+ self.out_channels, self.no_trans, self.group_size,
79
+ self.part_size, self.sample_per_part, self.trans_std)
80
+ else:
81
+ n = rois.shape[0]
82
+ offset = data.new_empty(0)
83
+ x = deform_roi_pooling(data, rois, offset, self.spatial_scale,
84
+ self.out_size, self.out_channels, True,
85
+ self.group_size, self.part_size,
86
+ self.sample_per_part, self.trans_std)
87
+ offset = self.offset_fc(x.view(n, -1))
88
+ offset = offset.view(n, 2, self.out_size, self.out_size)
89
+ return deform_roi_pooling(
90
+ data, rois, offset, self.spatial_scale, self.out_size,
91
+ self.out_channels, self.no_trans, self.group_size,
92
+ self.part_size, self.sample_per_part, self.trans_std)
93
+
94
+
95
+ class ModulatedDeformRoIPoolingPack(DeformRoIPooling):
96
+
97
+ def __init__(self,
98
+ spatial_scale,
99
+ out_size,
100
+ out_channels,
101
+ no_trans,
102
+ group_size=1,
103
+ part_size=None,
104
+ sample_per_part=4,
105
+ trans_std=.0,
106
+ num_offset_fcs=3,
107
+ num_mask_fcs=2,
108
+ deform_fc_channels=1024):
109
+ super(ModulatedDeformRoIPoolingPack, self).__init__(
110
+ spatial_scale, out_size, out_channels, no_trans, group_size,
111
+ part_size, sample_per_part, trans_std)
112
+
113
+ self.num_offset_fcs = num_offset_fcs
114
+ self.num_mask_fcs = num_mask_fcs
115
+ self.deform_fc_channels = deform_fc_channels
116
+
117
+ if not no_trans:
118
+ offset_fc_seq = []
119
+ ic = self.out_size * self.out_size * self.out_channels
120
+ for i in range(self.num_offset_fcs):
121
+ if i < self.num_offset_fcs - 1:
122
+ oc = self.deform_fc_channels
123
+ else:
124
+ oc = self.out_size * self.out_size * 2
125
+ offset_fc_seq.append(nn.Linear(ic, oc))
126
+ ic = oc
127
+ if i < self.num_offset_fcs - 1:
128
+ offset_fc_seq.append(nn.ReLU(inplace=True))
129
+ self.offset_fc = nn.Sequential(*offset_fc_seq)
130
+ self.offset_fc[-1].weight.data.zero_()
131
+ self.offset_fc[-1].bias.data.zero_()
132
+
133
+ mask_fc_seq = []
134
+ ic = self.out_size * self.out_size * self.out_channels
135
+ for i in range(self.num_mask_fcs):
136
+ if i < self.num_mask_fcs - 1:
137
+ oc = self.deform_fc_channels
138
+ else:
139
+ oc = self.out_size * self.out_size
140
+ mask_fc_seq.append(nn.Linear(ic, oc))
141
+ ic = oc
142
+ if i < self.num_mask_fcs - 1:
143
+ mask_fc_seq.append(nn.ReLU(inplace=True))
144
+ else:
145
+ mask_fc_seq.append(nn.Sigmoid())
146
+ self.mask_fc = nn.Sequential(*mask_fc_seq)
147
+ self.mask_fc[-2].weight.data.zero_()
148
+ self.mask_fc[-2].bias.data.zero_()
149
+
150
+ def forward(self, data, rois):
151
+ assert data.size(1) == self.out_channels
152
+ if self.no_trans:
153
+ offset = data.new_empty(0)
154
+ return deform_roi_pooling(
155
+ data, rois, offset, self.spatial_scale, self.out_size,
156
+ self.out_channels, self.no_trans, self.group_size,
157
+ self.part_size, self.sample_per_part, self.trans_std)
158
+ else:
159
+ n = rois.shape[0]
160
+ offset = data.new_empty(0)
161
+ x = deform_roi_pooling(data, rois, offset, self.spatial_scale,
162
+ self.out_size, self.out_channels, True,
163
+ self.group_size, self.part_size,
164
+ self.sample_per_part, self.trans_std)
165
+ offset = self.offset_fc(x.view(n, -1))
166
+ offset = offset.view(n, 2, self.out_size, self.out_size)
167
+ mask = self.mask_fc(x.view(n, -1))
168
+ mask = mask.view(n, 1, self.out_size, self.out_size)
169
+ return deform_roi_pooling(
170
+ data, rois, offset, self.spatial_scale, self.out_size,
171
+ self.out_channels, self.no_trans, self.group_size,
172
+ self.part_size, self.sample_per_part, self.trans_std) * mask
IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/setup.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ PATH ="{}:{}".format(os.environ['PATH'], "/opt/cuda/bin")
3
+ # os.environ['CUDA_VISIBLE_DEVICES'] = "1"
4
+ os.environ['PATH'] = PATH
5
+ from setuptools import setup
6
+ from torch.utils.cpp_extension import BuildExtension, CUDAExtension
7
+
8
+ setup(
9
+ name='deform_conv',
10
+ ext_modules=[
11
+ CUDAExtension('deform_conv_cuda', [
12
+ 'src/deform_conv_cuda.cpp',
13
+ 'src/deform_conv_cuda_kernel.cu',
14
+ ]),
15
+ CUDAExtension('deform_pool_cuda', [
16
+ 'src/deform_pool_cuda.cpp', 'src/deform_pool_cuda_kernel.cu'
17
+ ]),
18
+ ],
19
+ cmdclass={'build_ext': BuildExtension})
IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/src/deform_conv_cuda.cpp ADDED
@@ -0,0 +1,695 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // modify from
2
+ // https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/blob/mmdetection/mmdet/ops/dcn/src/deform_conv_cuda.c
3
+
4
+ #include <torch/extension.h>
5
+
6
+ #include <cmath>
7
+ #include <vector>
8
+
9
+ void deformable_im2col(const at::Tensor data_im, const at::Tensor data_offset,
10
+ const int channels, const int height, const int width,
11
+ const int ksize_h, const int ksize_w, const int pad_h,
12
+ const int pad_w, const int stride_h, const int stride_w,
13
+ const int dilation_h, const int dilation_w,
14
+ const int parallel_imgs, const int deformable_group,
15
+ at::Tensor data_col);
16
+
17
+ void deformable_col2im(const at::Tensor data_col, const at::Tensor data_offset,
18
+ const int channels, const int height, const int width,
19
+ const int ksize_h, const int ksize_w, const int pad_h,
20
+ const int pad_w, const int stride_h, const int stride_w,
21
+ const int dilation_h, const int dilation_w,
22
+ const int parallel_imgs, const int deformable_group,
23
+ at::Tensor grad_im);
24
+
25
+ void deformable_col2im_coord(
26
+ const at::Tensor data_col, const at::Tensor data_im,
27
+ const at::Tensor data_offset, const int channels, const int height,
28
+ const int width, const int ksize_h, const int ksize_w, const int pad_h,
29
+ const int pad_w, const int stride_h, const int stride_w,
30
+ const int dilation_h, const int dilation_w, const int parallel_imgs,
31
+ const int deformable_group, at::Tensor grad_offset);
32
+
33
+ void modulated_deformable_im2col_cuda(
34
+ const at::Tensor data_im, const at::Tensor data_offset,
35
+ const at::Tensor data_mask, const int batch_size, const int channels,
36
+ const int height_im, const int width_im, const int height_col,
37
+ const int width_col, const int kernel_h, const int kenerl_w,
38
+ const int pad_h, const int pad_w, const int stride_h, const int stride_w,
39
+ const int dilation_h, const int dilation_w, const int deformable_group,
40
+ at::Tensor data_col);
41
+
42
+ void modulated_deformable_col2im_cuda(
43
+ const at::Tensor data_col, const at::Tensor data_offset,
44
+ const at::Tensor data_mask, const int batch_size, const int channels,
45
+ const int height_im, const int width_im, const int height_col,
46
+ const int width_col, const int kernel_h, const int kenerl_w,
47
+ const int pad_h, const int pad_w, const int stride_h, const int stride_w,
48
+ const int dilation_h, const int dilation_w, const int deformable_group,
49
+ at::Tensor grad_im);
50
+
51
+ void modulated_deformable_col2im_coord_cuda(
52
+ const at::Tensor data_col, const at::Tensor data_im,
53
+ const at::Tensor data_offset, const at::Tensor data_mask,
54
+ const int batch_size, const int channels, const int height_im,
55
+ const int width_im, const int height_col, const int width_col,
56
+ const int kernel_h, const int kenerl_w, const int pad_h, const int pad_w,
57
+ const int stride_h, const int stride_w, const int dilation_h,
58
+ const int dilation_w, const int deformable_group, at::Tensor grad_offset,
59
+ at::Tensor grad_mask);
60
+
61
+ void shape_check(at::Tensor input, at::Tensor offset, at::Tensor *gradOutput,
62
+ at::Tensor weight, int kH, int kW, int dH, int dW, int padH,
63
+ int padW, int dilationH, int dilationW, int group,
64
+ int deformable_group) {
65
+ TORCH_CHECK(weight.ndimension() == 4,
66
+ "4D weight tensor (nOutputPlane,nInputPlane,kH,kW) expected, "
67
+ "but got: %s",
68
+ weight.ndimension());
69
+
70
+ TORCH_CHECK(weight.is_contiguous(), "weight tensor has to be contiguous");
71
+
72
+ TORCH_CHECK(kW > 0 && kH > 0,
73
+ "kernel size should be greater than zero, but got kH: %d kW: %d", kH,
74
+ kW);
75
+
76
+ TORCH_CHECK((weight.size(2) == kH && weight.size(3) == kW),
77
+ "kernel size should be consistent with weight, ",
78
+ "but got kH: %d kW: %d weight.size(2): %d, weight.size(3): %d", kH,
79
+ kW, weight.size(2), weight.size(3));
80
+
81
+ TORCH_CHECK(dW > 0 && dH > 0,
82
+ "stride should be greater than zero, but got dH: %d dW: %d", dH, dW);
83
+
84
+ TORCH_CHECK(
85
+ dilationW > 0 && dilationH > 0,
86
+ "dilation should be greater than 0, but got dilationH: %d dilationW: %d",
87
+ dilationH, dilationW);
88
+
89
+ int ndim = input.ndimension();
90
+ int dimf = 0;
91
+ int dimh = 1;
92
+ int dimw = 2;
93
+
94
+ if (ndim == 4) {
95
+ dimf++;
96
+ dimh++;
97
+ dimw++;
98
+ }
99
+
100
+ TORCH_CHECK(ndim == 3 || ndim == 4, "3D or 4D input tensor expected but got: %s",
101
+ ndim);
102
+
103
+ long nInputPlane = weight.size(1) * group;
104
+ long inputHeight = input.size(dimh);
105
+ long inputWidth = input.size(dimw);
106
+ long nOutputPlane = weight.size(0);
107
+ long outputHeight =
108
+ (inputHeight + 2 * padH - (dilationH * (kH - 1) + 1)) / dH + 1;
109
+ long outputWidth =
110
+ (inputWidth + 2 * padW - (dilationW * (kW - 1) + 1)) / dW + 1;
111
+
112
+ TORCH_CHECK(nInputPlane % deformable_group == 0,
113
+ "input channels must divide deformable group size");
114
+
115
+ if (outputWidth < 1 || outputHeight < 1)
116
+ AT_ERROR(
117
+ "Given input size: (%ld x %ld x %ld). "
118
+ "Calculated output size: (%ld x %ld x %ld). Output size is too small",
119
+ nInputPlane, inputHeight, inputWidth, nOutputPlane, outputHeight,
120
+ outputWidth);
121
+
122
+ TORCH_CHECK(input.size(1) == nInputPlane,
123
+ "invalid number of input planes, expected: %d, but got: %d",
124
+ nInputPlane, input.size(1));
125
+
126
+ TORCH_CHECK((inputHeight >= kH && inputWidth >= kW),
127
+ "input image is smaller than kernel");
128
+
129
+ TORCH_CHECK((offset.size(2) == outputHeight && offset.size(3) == outputWidth),
130
+ "invalid spatial size of offset, expected height: %d width: %d, but "
131
+ "got height: %d width: %d",
132
+ outputHeight, outputWidth, offset.size(2), offset.size(3));
133
+
134
+ TORCH_CHECK((offset.size(1) == deformable_group * 2 * kH * kW),
135
+ "invalid number of channels of offset");
136
+
137
+ if (gradOutput != NULL) {
138
+ TORCH_CHECK(gradOutput->size(dimf) == nOutputPlane,
139
+ "invalid number of gradOutput planes, expected: %d, but got: %d",
140
+ nOutputPlane, gradOutput->size(dimf));
141
+
142
+ TORCH_CHECK((gradOutput->size(dimh) == outputHeight &&
143
+ gradOutput->size(dimw) == outputWidth),
144
+ "invalid size of gradOutput, expected height: %d width: %d , but "
145
+ "got height: %d width: %d",
146
+ outputHeight, outputWidth, gradOutput->size(dimh),
147
+ gradOutput->size(dimw));
148
+ }
149
+ }
150
+
151
+ int deform_conv_forward_cuda(at::Tensor input, at::Tensor weight,
152
+ at::Tensor offset, at::Tensor output,
153
+ at::Tensor columns, at::Tensor ones, int kW,
154
+ int kH, int dW, int dH, int padW, int padH,
155
+ int dilationW, int dilationH, int group,
156
+ int deformable_group, int im2col_step) {
157
+ // todo: resize columns to include im2col: done
158
+ // todo: add im2col_step as input
159
+ // todo: add new output buffer and transpose it to output (or directly
160
+ // transpose output) todo: possibly change data indexing because of
161
+ // parallel_imgs
162
+
163
+ shape_check(input, offset, NULL, weight, kH, kW, dH, dW, padH, padW,
164
+ dilationH, dilationW, group, deformable_group);
165
+
166
+ input = input.contiguous();
167
+ offset = offset.contiguous();
168
+ weight = weight.contiguous();
169
+
170
+ int batch = 1;
171
+ if (input.ndimension() == 3) {
172
+ // Force batch
173
+ batch = 0;
174
+ input.unsqueeze_(0);
175
+ offset.unsqueeze_(0);
176
+ }
177
+
178
+ // todo: assert batchsize dividable by im2col_step
179
+
180
+ long batchSize = input.size(0);
181
+ long nInputPlane = input.size(1);
182
+ long inputHeight = input.size(2);
183
+ long inputWidth = input.size(3);
184
+
185
+ long nOutputPlane = weight.size(0);
186
+
187
+ long outputWidth =
188
+ (inputWidth + 2 * padW - (dilationW * (kW - 1) + 1)) / dW + 1;
189
+ long outputHeight =
190
+ (inputHeight + 2 * padH - (dilationH * (kH - 1) + 1)) / dH + 1;
191
+
192
+ TORCH_CHECK((offset.size(0) == batchSize), "invalid batch size of offset");
193
+
194
+ output = output.view({batchSize / im2col_step, im2col_step, nOutputPlane,
195
+ outputHeight, outputWidth});
196
+ columns = at::zeros(
197
+ {nInputPlane * kW * kH, im2col_step * outputHeight * outputWidth},
198
+ input.options());
199
+
200
+ if (ones.ndimension() != 2 ||
201
+ ones.size(0) * ones.size(1) < outputHeight * outputWidth) {
202
+ ones = at::ones({outputHeight, outputWidth}, input.options());
203
+ }
204
+
205
+ input = input.view({batchSize / im2col_step, im2col_step, nInputPlane,
206
+ inputHeight, inputWidth});
207
+ offset =
208
+ offset.view({batchSize / im2col_step, im2col_step,
209
+ deformable_group * 2 * kH * kW, outputHeight, outputWidth});
210
+
211
+ at::Tensor output_buffer =
212
+ at::zeros({batchSize / im2col_step, nOutputPlane,
213
+ im2col_step * outputHeight, outputWidth},
214
+ output.options());
215
+
216
+ output_buffer = output_buffer.view(
217
+ {output_buffer.size(0), group, output_buffer.size(1) / group,
218
+ output_buffer.size(2), output_buffer.size(3)});
219
+
220
+ for (int elt = 0; elt < batchSize / im2col_step; elt++) {
221
+ deformable_im2col(input[elt], offset[elt], nInputPlane, inputHeight,
222
+ inputWidth, kH, kW, padH, padW, dH, dW, dilationH,
223
+ dilationW, im2col_step, deformable_group, columns);
224
+
225
+ columns = columns.view({group, columns.size(0) / group, columns.size(1)});
226
+ weight = weight.view({group, weight.size(0) / group, weight.size(1),
227
+ weight.size(2), weight.size(3)});
228
+
229
+ for (int g = 0; g < group; g++) {
230
+ output_buffer[elt][g] = output_buffer[elt][g]
231
+ .flatten(1)
232
+ .addmm_(weight[g].flatten(1), columns[g])
233
+ .view_as(output_buffer[elt][g]);
234
+ }
235
+ }
236
+
237
+ output_buffer = output_buffer.view(
238
+ {output_buffer.size(0), output_buffer.size(1) * output_buffer.size(2),
239
+ output_buffer.size(3), output_buffer.size(4)});
240
+
241
+ output_buffer = output_buffer.view({batchSize / im2col_step, nOutputPlane,
242
+ im2col_step, outputHeight, outputWidth});
243
+ output_buffer.transpose_(1, 2);
244
+ output.copy_(output_buffer);
245
+ output = output.view({batchSize, nOutputPlane, outputHeight, outputWidth});
246
+
247
+ input = input.view({batchSize, nInputPlane, inputHeight, inputWidth});
248
+ offset = offset.view(
249
+ {batchSize, deformable_group * 2 * kH * kW, outputHeight, outputWidth});
250
+
251
+ if (batch == 0) {
252
+ output = output.view({nOutputPlane, outputHeight, outputWidth});
253
+ input = input.view({nInputPlane, inputHeight, inputWidth});
254
+ offset = offset.view({offset.size(1), offset.size(2), offset.size(3)});
255
+ }
256
+
257
+ return 1;
258
+ }
259
+
260
+ int deform_conv_backward_input_cuda(at::Tensor input, at::Tensor offset,
261
+ at::Tensor gradOutput, at::Tensor gradInput,
262
+ at::Tensor gradOffset, at::Tensor weight,
263
+ at::Tensor columns, int kW, int kH, int dW,
264
+ int dH, int padW, int padH, int dilationW,
265
+ int dilationH, int group,
266
+ int deformable_group, int im2col_step) {
267
+ shape_check(input, offset, &gradOutput, weight, kH, kW, dH, dW, padH, padW,
268
+ dilationH, dilationW, group, deformable_group);
269
+
270
+ input = input.contiguous();
271
+ offset = offset.contiguous();
272
+ gradOutput = gradOutput.contiguous();
273
+ weight = weight.contiguous();
274
+
275
+ int batch = 1;
276
+
277
+ if (input.ndimension() == 3) {
278
+ // Force batch
279
+ batch = 0;
280
+ input = input.view({1, input.size(0), input.size(1), input.size(2)});
281
+ offset = offset.view({1, offset.size(0), offset.size(1), offset.size(2)});
282
+ gradOutput = gradOutput.view(
283
+ {1, gradOutput.size(0), gradOutput.size(1), gradOutput.size(2)});
284
+ }
285
+
286
+ long batchSize = input.size(0);
287
+ long nInputPlane = input.size(1);
288
+ long inputHeight = input.size(2);
289
+ long inputWidth = input.size(3);
290
+
291
+ long nOutputPlane = weight.size(0);
292
+
293
+ long outputWidth =
294
+ (inputWidth + 2 * padW - (dilationW * (kW - 1) + 1)) / dW + 1;
295
+ long outputHeight =
296
+ (inputHeight + 2 * padH - (dilationH * (kH - 1) + 1)) / dH + 1;
297
+
298
+ TORCH_CHECK((offset.size(0) == batchSize), 3, "invalid batch size of offset");
299
+ gradInput = gradInput.view({batchSize, nInputPlane, inputHeight, inputWidth});
300
+ columns = at::zeros(
301
+ {nInputPlane * kW * kH, im2col_step * outputHeight * outputWidth},
302
+ input.options());
303
+
304
+ // change order of grad output
305
+ gradOutput = gradOutput.view({batchSize / im2col_step, im2col_step,
306
+ nOutputPlane, outputHeight, outputWidth});
307
+ gradOutput.transpose_(1, 2);
308
+
309
+ gradInput = gradInput.view({batchSize / im2col_step, im2col_step, nInputPlane,
310
+ inputHeight, inputWidth});
311
+ input = input.view({batchSize / im2col_step, im2col_step, nInputPlane,
312
+ inputHeight, inputWidth});
313
+ gradOffset = gradOffset.view({batchSize / im2col_step, im2col_step,
314
+ deformable_group * 2 * kH * kW, outputHeight,
315
+ outputWidth});
316
+ offset =
317
+ offset.view({batchSize / im2col_step, im2col_step,
318
+ deformable_group * 2 * kH * kW, outputHeight, outputWidth});
319
+
320
+ for (int elt = 0; elt < batchSize / im2col_step; elt++) {
321
+ // divide into groups
322
+ columns = columns.view({group, columns.size(0) / group, columns.size(1)});
323
+ weight = weight.view({group, weight.size(0) / group, weight.size(1),
324
+ weight.size(2), weight.size(3)});
325
+ gradOutput = gradOutput.view(
326
+ {gradOutput.size(0), group, gradOutput.size(1) / group,
327
+ gradOutput.size(2), gradOutput.size(3), gradOutput.size(4)});
328
+
329
+ for (int g = 0; g < group; g++) {
330
+ columns[g] = columns[g].addmm_(weight[g].flatten(1).transpose(0, 1),
331
+ gradOutput[elt][g].flatten(1), 0.0f, 1.0f);
332
+ }
333
+
334
+ columns =
335
+ columns.view({columns.size(0) * columns.size(1), columns.size(2)});
336
+ gradOutput = gradOutput.view(
337
+ {gradOutput.size(0), gradOutput.size(1) * gradOutput.size(2),
338
+ gradOutput.size(3), gradOutput.size(4), gradOutput.size(5)});
339
+
340
+ deformable_col2im_coord(columns, input[elt], offset[elt], nInputPlane,
341
+ inputHeight, inputWidth, kH, kW, padH, padW, dH, dW,
342
+ dilationH, dilationW, im2col_step, deformable_group,
343
+ gradOffset[elt]);
344
+
345
+ deformable_col2im(columns, offset[elt], nInputPlane, inputHeight,
346
+ inputWidth, kH, kW, padH, padW, dH, dW, dilationH,
347
+ dilationW, im2col_step, deformable_group, gradInput[elt]);
348
+ }
349
+
350
+ gradOutput.transpose_(1, 2);
351
+ gradOutput =
352
+ gradOutput.view({batchSize, nOutputPlane, outputHeight, outputWidth});
353
+
354
+ gradInput = gradInput.view({batchSize, nInputPlane, inputHeight, inputWidth});
355
+ input = input.view({batchSize, nInputPlane, inputHeight, inputWidth});
356
+ gradOffset = gradOffset.view(
357
+ {batchSize, deformable_group * 2 * kH * kW, outputHeight, outputWidth});
358
+ offset = offset.view(
359
+ {batchSize, deformable_group * 2 * kH * kW, outputHeight, outputWidth});
360
+
361
+ if (batch == 0) {
362
+ gradOutput = gradOutput.view({nOutputPlane, outputHeight, outputWidth});
363
+ input = input.view({nInputPlane, inputHeight, inputWidth});
364
+ gradInput = gradInput.view({nInputPlane, inputHeight, inputWidth});
365
+ offset = offset.view({offset.size(1), offset.size(2), offset.size(3)});
366
+ gradOffset =
367
+ gradOffset.view({offset.size(1), offset.size(2), offset.size(3)});
368
+ }
369
+
370
+ return 1;
371
+ }
372
+
373
+ int deform_conv_backward_parameters_cuda(
374
+ at::Tensor input, at::Tensor offset, at::Tensor gradOutput,
375
+ at::Tensor gradWeight, // at::Tensor gradBias,
376
+ at::Tensor columns, at::Tensor ones, int kW, int kH, int dW, int dH,
377
+ int padW, int padH, int dilationW, int dilationH, int group,
378
+ int deformable_group, float scale, int im2col_step) {
379
+ // todo: transpose and reshape outGrad
380
+ // todo: reshape columns
381
+ // todo: add im2col_step as input
382
+
383
+ shape_check(input, offset, &gradOutput, gradWeight, kH, kW, dH, dW, padH,
384
+ padW, dilationH, dilationW, group, deformable_group);
385
+
386
+ input = input.contiguous();
387
+ offset = offset.contiguous();
388
+ gradOutput = gradOutput.contiguous();
389
+
390
+ int batch = 1;
391
+
392
+ if (input.ndimension() == 3) {
393
+ // Force batch
394
+ batch = 0;
395
+ input = input.view(
396
+ at::IntList({1, input.size(0), input.size(1), input.size(2)}));
397
+ gradOutput = gradOutput.view(
398
+ {1, gradOutput.size(0), gradOutput.size(1), gradOutput.size(2)});
399
+ }
400
+
401
+ long batchSize = input.size(0);
402
+ long nInputPlane = input.size(1);
403
+ long inputHeight = input.size(2);
404
+ long inputWidth = input.size(3);
405
+
406
+ long nOutputPlane = gradWeight.size(0);
407
+
408
+ long outputWidth =
409
+ (inputWidth + 2 * padW - (dilationW * (kW - 1) + 1)) / dW + 1;
410
+ long outputHeight =
411
+ (inputHeight + 2 * padH - (dilationH * (kH - 1) + 1)) / dH + 1;
412
+
413
+ TORCH_CHECK((offset.size(0) == batchSize), "invalid batch size of offset");
414
+
415
+ columns = at::zeros(
416
+ {nInputPlane * kW * kH, im2col_step * outputHeight * outputWidth},
417
+ input.options());
418
+
419
+ gradOutput = gradOutput.view({batchSize / im2col_step, im2col_step,
420
+ nOutputPlane, outputHeight, outputWidth});
421
+ gradOutput.transpose_(1, 2);
422
+
423
+ at::Tensor gradOutputBuffer = at::zeros_like(gradOutput);
424
+ gradOutputBuffer =
425
+ gradOutputBuffer.view({batchSize / im2col_step, nOutputPlane, im2col_step,
426
+ outputHeight, outputWidth});
427
+ gradOutputBuffer.copy_(gradOutput);
428
+ gradOutputBuffer =
429
+ gradOutputBuffer.view({batchSize / im2col_step, nOutputPlane,
430
+ im2col_step * outputHeight, outputWidth});
431
+
432
+ gradOutput.transpose_(1, 2);
433
+ gradOutput =
434
+ gradOutput.view({batchSize, nOutputPlane, outputHeight, outputWidth});
435
+
436
+ input = input.view({batchSize / im2col_step, im2col_step, nInputPlane,
437
+ inputHeight, inputWidth});
438
+ offset =
439
+ offset.view({batchSize / im2col_step, im2col_step,
440
+ deformable_group * 2 * kH * kW, outputHeight, outputWidth});
441
+
442
+ for (int elt = 0; elt < batchSize / im2col_step; elt++) {
443
+ deformable_im2col(input[elt], offset[elt], nInputPlane, inputHeight,
444
+ inputWidth, kH, kW, padH, padW, dH, dW, dilationH,
445
+ dilationW, im2col_step, deformable_group, columns);
446
+
447
+ // divide into group
448
+ gradOutputBuffer = gradOutputBuffer.view(
449
+ {gradOutputBuffer.size(0), group, gradOutputBuffer.size(1) / group,
450
+ gradOutputBuffer.size(2), gradOutputBuffer.size(3)});
451
+ columns = columns.view({group, columns.size(0) / group, columns.size(1)});
452
+ gradWeight =
453
+ gradWeight.view({group, gradWeight.size(0) / group, gradWeight.size(1),
454
+ gradWeight.size(2), gradWeight.size(3)});
455
+
456
+ for (int g = 0; g < group; g++) {
457
+ gradWeight[g] = gradWeight[g]
458
+ .flatten(1)
459
+ .addmm_(gradOutputBuffer[elt][g].flatten(1),
460
+ columns[g].transpose(1, 0), 1.0, scale)
461
+ .view_as(gradWeight[g]);
462
+ }
463
+ gradOutputBuffer = gradOutputBuffer.view(
464
+ {gradOutputBuffer.size(0),
465
+ gradOutputBuffer.size(1) * gradOutputBuffer.size(2),
466
+ gradOutputBuffer.size(3), gradOutputBuffer.size(4)});
467
+ columns =
468
+ columns.view({columns.size(0) * columns.size(1), columns.size(2)});
469
+ gradWeight = gradWeight.view({gradWeight.size(0) * gradWeight.size(1),
470
+ gradWeight.size(2), gradWeight.size(3),
471
+ gradWeight.size(4)});
472
+ }
473
+
474
+ input = input.view({batchSize, nInputPlane, inputHeight, inputWidth});
475
+ offset = offset.view(
476
+ {batchSize, deformable_group * 2 * kH * kW, outputHeight, outputWidth});
477
+
478
+ if (batch == 0) {
479
+ gradOutput = gradOutput.view({nOutputPlane, outputHeight, outputWidth});
480
+ input = input.view({nInputPlane, inputHeight, inputWidth});
481
+ }
482
+
483
+ return 1;
484
+ }
485
+
486
+ void modulated_deform_conv_cuda_forward(
487
+ at::Tensor input, at::Tensor weight, at::Tensor bias, at::Tensor ones,
488
+ at::Tensor offset, at::Tensor mask, at::Tensor output, at::Tensor columns,
489
+ int kernel_h, int kernel_w, const int stride_h, const int stride_w,
490
+ const int pad_h, const int pad_w, const int dilation_h,
491
+ const int dilation_w, const int group, const int deformable_group,
492
+ const bool with_bias) {
493
+ TORCH_CHECK(input.is_contiguous(), "input tensor has to be contiguous");
494
+ TORCH_CHECK(weight.is_contiguous(), "weight tensor has to be contiguous");
495
+
496
+ const int batch = input.size(0);
497
+ const int channels = input.size(1);
498
+ const int height = input.size(2);
499
+ const int width = input.size(3);
500
+
501
+ const int channels_out = weight.size(0);
502
+ const int channels_kernel = weight.size(1);
503
+ const int kernel_h_ = weight.size(2);
504
+ const int kernel_w_ = weight.size(3);
505
+
506
+ if (kernel_h_ != kernel_h || kernel_w_ != kernel_w)
507
+ AT_ERROR("Input shape and kernel shape wont match: (%d x %d vs %d x %d).",
508
+ kernel_h_, kernel_w, kernel_h_, kernel_w_);
509
+ if (channels != channels_kernel * group)
510
+ AT_ERROR("Input shape and kernel channels wont match: (%d vs %d).",
511
+ channels, channels_kernel * group);
512
+
513
+ const int height_out =
514
+ (height + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
515
+ const int width_out =
516
+ (width + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
517
+
518
+ if (ones.ndimension() != 2 ||
519
+ ones.size(0) * ones.size(1) < height_out * width_out) {
520
+ // Resize plane and fill with ones...
521
+ ones = at::ones({height_out, width_out}, input.options());
522
+ }
523
+
524
+ // resize output
525
+ output = output.view({batch, channels_out, height_out, width_out}).zero_();
526
+ // resize temporary columns
527
+ columns =
528
+ at::zeros({channels * kernel_h * kernel_w, 1 * height_out * width_out},
529
+ input.options());
530
+
531
+ output = output.view({output.size(0), group, output.size(1) / group,
532
+ output.size(2), output.size(3)});
533
+
534
+ for (int b = 0; b < batch; b++) {
535
+ modulated_deformable_im2col_cuda(
536
+ input[b], offset[b], mask[b], 1, channels, height, width, height_out,
537
+ width_out, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w,
538
+ dilation_h, dilation_w, deformable_group, columns);
539
+
540
+ // divide into group
541
+ weight = weight.view({group, weight.size(0) / group, weight.size(1),
542
+ weight.size(2), weight.size(3)});
543
+ columns = columns.view({group, columns.size(0) / group, columns.size(1)});
544
+
545
+ for (int g = 0; g < group; g++) {
546
+ output[b][g] = output[b][g]
547
+ .flatten(1)
548
+ .addmm_(weight[g].flatten(1), columns[g])
549
+ .view_as(output[b][g]);
550
+ }
551
+
552
+ weight = weight.view({weight.size(0) * weight.size(1), weight.size(2),
553
+ weight.size(3), weight.size(4)});
554
+ columns =
555
+ columns.view({columns.size(0) * columns.size(1), columns.size(2)});
556
+ }
557
+
558
+ output = output.view({output.size(0), output.size(1) * output.size(2),
559
+ output.size(3), output.size(4)});
560
+
561
+ if (with_bias) {
562
+ output += bias.view({1, bias.size(0), 1, 1});
563
+ }
564
+ }
565
+
566
+ void modulated_deform_conv_cuda_backward(
567
+ at::Tensor input, at::Tensor weight, at::Tensor bias, at::Tensor ones,
568
+ at::Tensor offset, at::Tensor mask, at::Tensor columns,
569
+ at::Tensor grad_input, at::Tensor grad_weight, at::Tensor grad_bias,
570
+ at::Tensor grad_offset, at::Tensor grad_mask, at::Tensor grad_output,
571
+ int kernel_h, int kernel_w, int stride_h, int stride_w, int pad_h,
572
+ int pad_w, int dilation_h, int dilation_w, int group, int deformable_group,
573
+ const bool with_bias) {
574
+ TORCH_CHECK(input.is_contiguous(), "input tensor has to be contiguous");
575
+ TORCH_CHECK(weight.is_contiguous(), "weight tensor has to be contiguous");
576
+
577
+ const int batch = input.size(0);
578
+ const int channels = input.size(1);
579
+ const int height = input.size(2);
580
+ const int width = input.size(3);
581
+
582
+ const int channels_kernel = weight.size(1);
583
+ const int kernel_h_ = weight.size(2);
584
+ const int kernel_w_ = weight.size(3);
585
+ if (kernel_h_ != kernel_h || kernel_w_ != kernel_w)
586
+ AT_ERROR("Input shape and kernel shape wont match: (%d x %d vs %d x %d).",
587
+ kernel_h_, kernel_w, kernel_h_, kernel_w_);
588
+ if (channels != channels_kernel * group)
589
+ AT_ERROR("Input shape and kernel channels wont match: (%d vs %d).",
590
+ channels, channels_kernel * group);
591
+
592
+ const int height_out =
593
+ (height + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
594
+ const int width_out =
595
+ (width + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
596
+
597
+ if (ones.ndimension() != 2 ||
598
+ ones.size(0) * ones.size(1) < height_out * width_out) {
599
+ // Resize plane and fill with ones...
600
+ ones = at::ones({height_out, width_out}, input.options());
601
+ }
602
+
603
+ grad_input = grad_input.view({batch, channels, height, width});
604
+ columns = at::zeros({channels * kernel_h * kernel_w, height_out * width_out},
605
+ input.options());
606
+
607
+ grad_output =
608
+ grad_output.view({grad_output.size(0), group, grad_output.size(1) / group,
609
+ grad_output.size(2), grad_output.size(3)});
610
+
611
+ for (int b = 0; b < batch; b++) {
612
+ // divide int group
613
+ columns = columns.view({group, columns.size(0) / group, columns.size(1)});
614
+ weight = weight.view({group, weight.size(0) / group, weight.size(1),
615
+ weight.size(2), weight.size(3)});
616
+
617
+ for (int g = 0; g < group; g++) {
618
+ columns[g].addmm_(weight[g].flatten(1).transpose(0, 1),
619
+ grad_output[b][g].flatten(1), 0.0f, 1.0f);
620
+ }
621
+
622
+ columns =
623
+ columns.view({columns.size(0) * columns.size(1), columns.size(2)});
624
+ weight = weight.view({weight.size(0) * weight.size(1), weight.size(2),
625
+ weight.size(3), weight.size(4)});
626
+
627
+ // gradient w.r.t. input coordinate data
628
+ modulated_deformable_col2im_coord_cuda(
629
+ columns, input[b], offset[b], mask[b], 1, channels, height, width,
630
+ height_out, width_out, kernel_h, kernel_w, pad_h, pad_w, stride_h,
631
+ stride_w, dilation_h, dilation_w, deformable_group, grad_offset[b],
632
+ grad_mask[b]);
633
+ // gradient w.r.t. input data
634
+ modulated_deformable_col2im_cuda(
635
+ columns, offset[b], mask[b], 1, channels, height, width, height_out,
636
+ width_out, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w,
637
+ dilation_h, dilation_w, deformable_group, grad_input[b]);
638
+
639
+ // gradient w.r.t. weight, dWeight should accumulate across the batch and
640
+ // group
641
+ modulated_deformable_im2col_cuda(
642
+ input[b], offset[b], mask[b], 1, channels, height, width, height_out,
643
+ width_out, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w,
644
+ dilation_h, dilation_w, deformable_group, columns);
645
+
646
+ columns = columns.view({group, columns.size(0) / group, columns.size(1)});
647
+ grad_weight = grad_weight.view({group, grad_weight.size(0) / group,
648
+ grad_weight.size(1), grad_weight.size(2),
649
+ grad_weight.size(3)});
650
+ if (with_bias)
651
+ grad_bias = grad_bias.view({group, grad_bias.size(0) / group});
652
+
653
+ for (int g = 0; g < group; g++) {
654
+ grad_weight[g] =
655
+ grad_weight[g]
656
+ .flatten(1)
657
+ .addmm_(grad_output[b][g].flatten(1), columns[g].transpose(0, 1))
658
+ .view_as(grad_weight[g]);
659
+ if (with_bias) {
660
+ grad_bias[g] =
661
+ grad_bias[g]
662
+ .view({-1, 1})
663
+ .addmm_(grad_output[b][g].flatten(1), ones.view({-1, 1}))
664
+ .view(-1);
665
+ }
666
+ }
667
+
668
+ columns =
669
+ columns.view({columns.size(0) * columns.size(1), columns.size(2)});
670
+ grad_weight = grad_weight.view({grad_weight.size(0) * grad_weight.size(1),
671
+ grad_weight.size(2), grad_weight.size(3),
672
+ grad_weight.size(4)});
673
+ if (with_bias)
674
+ grad_bias = grad_bias.view({grad_bias.size(0) * grad_bias.size(1)});
675
+ }
676
+ grad_output = grad_output.view({grad_output.size(0) * grad_output.size(1),
677
+ grad_output.size(2), grad_output.size(3),
678
+ grad_output.size(4)});
679
+ }
680
+
681
+ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
682
+ m.def("deform_conv_forward_cuda", &deform_conv_forward_cuda,
683
+ "deform forward (CUDA)");
684
+ m.def("deform_conv_backward_input_cuda", &deform_conv_backward_input_cuda,
685
+ "deform_conv_backward_input (CUDA)");
686
+ m.def("deform_conv_backward_parameters_cuda",
687
+ &deform_conv_backward_parameters_cuda,
688
+ "deform_conv_backward_parameters (CUDA)");
689
+ m.def("modulated_deform_conv_cuda_forward",
690
+ &modulated_deform_conv_cuda_forward,
691
+ "modulated deform conv forward (CUDA)");
692
+ m.def("modulated_deform_conv_cuda_backward",
693
+ &modulated_deform_conv_cuda_backward,
694
+ "modulated deform conv backward (CUDA)");
695
+ }
IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/src/deform_conv_cuda_kernel.cu ADDED
@@ -0,0 +1,866 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*!
2
+ ******************* BEGIN Caffe Copyright Notice and Disclaimer ****************
3
+ *
4
+ * COPYRIGHT
5
+ *
6
+ * All contributions by the University of California:
7
+ * Copyright (c) 2014-2017 The Regents of the University of California (Regents)
8
+ * All rights reserved.
9
+ *
10
+ * All other contributions:
11
+ * Copyright (c) 2014-2017, the respective contributors
12
+ * All rights reserved.
13
+ *
14
+ * Caffe uses a shared copyright model: each contributor holds copyright over
15
+ * their contributions to Caffe. The project versioning records all such
16
+ * contribution and copyright details. If a contributor wants to further mark
17
+ * their specific copyright on a particular contribution, they should indicate
18
+ * their copyright solely in the commit message of the change when it is
19
+ * committed.
20
+ *
21
+ * LICENSE
22
+ *
23
+ * Redistribution and use in source and binary forms, with or without
24
+ * modification, are permitted provided that the following conditions are met:
25
+ *
26
+ * 1. Redistributions of source code must retain the above copyright notice, this
27
+ * list of conditions and the following disclaimer.
28
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
29
+ * this list of conditions and the following disclaimer in the documentation
30
+ * and/or other materials provided with the distribution.
31
+ *
32
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
33
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
36
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42
+ *
43
+ * CONTRIBUTION AGREEMENT
44
+ *
45
+ * By contributing to the BVLC/caffe repository through pull-request, comment,
46
+ * or otherwise, the contributor releases their content to the
47
+ * license and copyright terms herein.
48
+ *
49
+ ***************** END Caffe Copyright Notice and Disclaimer ********************
50
+ *
51
+ * Copyright (c) 2018 Microsoft
52
+ * Licensed under The MIT License [see LICENSE for details]
53
+ * \file modulated_deformable_im2col.cuh
54
+ * \brief Function definitions of converting an image to
55
+ * column matrix based on kernel, padding, dilation, and offset.
56
+ * These functions are mainly used in deformable convolution operators.
57
+ * \ref: https://arxiv.org/abs/1703.06211
58
+ * \author Yuwen Xiong, Haozhi Qi, Jifeng Dai, Xizhou Zhu, Han Hu, Dazhi Cheng
59
+ */
60
+
61
+ // modify from https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/blob/mmdetection/mmdet/ops/dcn/src/deform_conv_cuda_kernel.cu
62
+
63
+ #include <ATen/ATen.h>
64
+ #include <THC/THCAtomics.cuh>
65
+ #include <stdio.h>
66
+ #include <math.h>
67
+ #include <float.h>
68
+
69
+ using namespace at;
70
+
71
+ #define CUDA_KERNEL_LOOP(i, n) \
72
+ for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < (n); \
73
+ i += blockDim.x * gridDim.x)
74
+
75
+ const int CUDA_NUM_THREADS = 1024;
76
+ const int kMaxGridNum = 65535;
77
+
78
+ inline int GET_BLOCKS(const int N)
79
+ {
80
+ return std::min(kMaxGridNum, (N + CUDA_NUM_THREADS - 1) / CUDA_NUM_THREADS);
81
+ }
82
+
83
+ template <typename scalar_t>
84
+ __device__ scalar_t deformable_im2col_bilinear(const scalar_t *bottom_data, const int data_width,
85
+ const int height, const int width, scalar_t h, scalar_t w)
86
+ {
87
+
88
+ int h_low = floor(h);
89
+ int w_low = floor(w);
90
+ int h_high = h_low + 1;
91
+ int w_high = w_low + 1;
92
+
93
+ scalar_t lh = h - h_low;
94
+ scalar_t lw = w - w_low;
95
+ scalar_t hh = 1 - lh, hw = 1 - lw;
96
+
97
+ scalar_t v1 = 0;
98
+ if (h_low >= 0 && w_low >= 0)
99
+ v1 = bottom_data[h_low * data_width + w_low];
100
+ scalar_t v2 = 0;
101
+ if (h_low >= 0 && w_high <= width - 1)
102
+ v2 = bottom_data[h_low * data_width + w_high];
103
+ scalar_t v3 = 0;
104
+ if (h_high <= height - 1 && w_low >= 0)
105
+ v3 = bottom_data[h_high * data_width + w_low];
106
+ scalar_t v4 = 0;
107
+ if (h_high <= height - 1 && w_high <= width - 1)
108
+ v4 = bottom_data[h_high * data_width + w_high];
109
+
110
+ scalar_t w1 = hh * hw, w2 = hh * lw, w3 = lh * hw, w4 = lh * lw;
111
+
112
+ scalar_t val = (w1 * v1 + w2 * v2 + w3 * v3 + w4 * v4);
113
+ return val;
114
+ }
115
+
116
+ template <typename scalar_t>
117
+ __device__ scalar_t get_gradient_weight(scalar_t argmax_h, scalar_t argmax_w,
118
+ const int h, const int w, const int height, const int width)
119
+ {
120
+
121
+ if (argmax_h <= -1 || argmax_h >= height || argmax_w <= -1 || argmax_w >= width)
122
+ {
123
+ //empty
124
+ return 0;
125
+ }
126
+
127
+ int argmax_h_low = floor(argmax_h);
128
+ int argmax_w_low = floor(argmax_w);
129
+ int argmax_h_high = argmax_h_low + 1;
130
+ int argmax_w_high = argmax_w_low + 1;
131
+
132
+ scalar_t weight = 0;
133
+ if (h == argmax_h_low && w == argmax_w_low)
134
+ weight = (h + 1 - argmax_h) * (w + 1 - argmax_w);
135
+ if (h == argmax_h_low && w == argmax_w_high)
136
+ weight = (h + 1 - argmax_h) * (argmax_w + 1 - w);
137
+ if (h == argmax_h_high && w == argmax_w_low)
138
+ weight = (argmax_h + 1 - h) * (w + 1 - argmax_w);
139
+ if (h == argmax_h_high && w == argmax_w_high)
140
+ weight = (argmax_h + 1 - h) * (argmax_w + 1 - w);
141
+ return weight;
142
+ }
143
+
144
+ template <typename scalar_t>
145
+ __device__ scalar_t get_coordinate_weight(scalar_t argmax_h, scalar_t argmax_w,
146
+ const int height, const int width, const scalar_t *im_data,
147
+ const int data_width, const int bp_dir)
148
+ {
149
+
150
+ if (argmax_h <= -1 || argmax_h >= height || argmax_w <= -1 || argmax_w >= width)
151
+ {
152
+ //empty
153
+ return 0;
154
+ }
155
+
156
+ int argmax_h_low = floor(argmax_h);
157
+ int argmax_w_low = floor(argmax_w);
158
+ int argmax_h_high = argmax_h_low + 1;
159
+ int argmax_w_high = argmax_w_low + 1;
160
+
161
+ scalar_t weight = 0;
162
+
163
+ if (bp_dir == 0)
164
+ {
165
+ if (argmax_h_low >= 0 && argmax_w_low >= 0)
166
+ weight += -1 * (argmax_w_low + 1 - argmax_w) * im_data[argmax_h_low * data_width + argmax_w_low];
167
+ if (argmax_h_low >= 0 && argmax_w_high <= width - 1)
168
+ weight += -1 * (argmax_w - argmax_w_low) * im_data[argmax_h_low * data_width + argmax_w_high];
169
+ if (argmax_h_high <= height - 1 && argmax_w_low >= 0)
170
+ weight += (argmax_w_low + 1 - argmax_w) * im_data[argmax_h_high * data_width + argmax_w_low];
171
+ if (argmax_h_high <= height - 1 && argmax_w_high <= width - 1)
172
+ weight += (argmax_w - argmax_w_low) * im_data[argmax_h_high * data_width + argmax_w_high];
173
+ }
174
+ else if (bp_dir == 1)
175
+ {
176
+ if (argmax_h_low >= 0 && argmax_w_low >= 0)
177
+ weight += -1 * (argmax_h_low + 1 - argmax_h) * im_data[argmax_h_low * data_width + argmax_w_low];
178
+ if (argmax_h_low >= 0 && argmax_w_high <= width - 1)
179
+ weight += (argmax_h_low + 1 - argmax_h) * im_data[argmax_h_low * data_width + argmax_w_high];
180
+ if (argmax_h_high <= height - 1 && argmax_w_low >= 0)
181
+ weight += -1 * (argmax_h - argmax_h_low) * im_data[argmax_h_high * data_width + argmax_w_low];
182
+ if (argmax_h_high <= height - 1 && argmax_w_high <= width - 1)
183
+ weight += (argmax_h - argmax_h_low) * im_data[argmax_h_high * data_width + argmax_w_high];
184
+ }
185
+
186
+ return weight;
187
+ }
188
+
189
+ template <typename scalar_t>
190
+ __global__ void deformable_im2col_gpu_kernel(const int n, const scalar_t *data_im, const scalar_t *data_offset,
191
+ const int height, const int width, const int kernel_h, const int kernel_w,
192
+ const int pad_h, const int pad_w, const int stride_h, const int stride_w,
193
+ const int dilation_h, const int dilation_w, const int channel_per_deformable_group,
194
+ const int batch_size, const int num_channels, const int deformable_group,
195
+ const int height_col, const int width_col,
196
+ scalar_t *data_col)
197
+ {
198
+ CUDA_KERNEL_LOOP(index, n)
199
+ {
200
+ // index index of output matrix
201
+ const int w_col = index % width_col;
202
+ const int h_col = (index / width_col) % height_col;
203
+ const int b_col = (index / width_col / height_col) % batch_size;
204
+ const int c_im = (index / width_col / height_col) / batch_size;
205
+ const int c_col = c_im * kernel_h * kernel_w;
206
+
207
+ // compute deformable group index
208
+ const int deformable_group_index = c_im / channel_per_deformable_group;
209
+
210
+ const int h_in = h_col * stride_h - pad_h;
211
+ const int w_in = w_col * stride_w - pad_w;
212
+ scalar_t *data_col_ptr = data_col + ((c_col * batch_size + b_col) * height_col + h_col) * width_col + w_col;
213
+ //const scalar_t* data_im_ptr = data_im + ((b_col * num_channels + c_im) * height + h_in) * width + w_in;
214
+ const scalar_t *data_im_ptr = data_im + (b_col * num_channels + c_im) * height * width;
215
+ const scalar_t *data_offset_ptr = data_offset + (b_col * deformable_group + deformable_group_index) * 2 * kernel_h * kernel_w * height_col * width_col;
216
+
217
+ for (int i = 0; i < kernel_h; ++i)
218
+ {
219
+ for (int j = 0; j < kernel_w; ++j)
220
+ {
221
+ const int data_offset_h_ptr = ((2 * (i * kernel_w + j)) * height_col + h_col) * width_col + w_col;
222
+ const int data_offset_w_ptr = ((2 * (i * kernel_w + j) + 1) * height_col + h_col) * width_col + w_col;
223
+ const scalar_t offset_h = data_offset_ptr[data_offset_h_ptr];
224
+ const scalar_t offset_w = data_offset_ptr[data_offset_w_ptr];
225
+ scalar_t val = static_cast<scalar_t>(0);
226
+ const scalar_t h_im = h_in + i * dilation_h + offset_h;
227
+ const scalar_t w_im = w_in + j * dilation_w + offset_w;
228
+ if (h_im > -1 && w_im > -1 && h_im < height && w_im < width)
229
+ {
230
+ //const scalar_t map_h = i * dilation_h + offset_h;
231
+ //const scalar_t map_w = j * dilation_w + offset_w;
232
+ //const int cur_height = height - h_in;
233
+ //const int cur_width = width - w_in;
234
+ //val = deformable_im2col_bilinear(data_im_ptr, width, cur_height, cur_width, map_h, map_w);
235
+ val = deformable_im2col_bilinear(data_im_ptr, width, height, width, h_im, w_im);
236
+ }
237
+ *data_col_ptr = val;
238
+ data_col_ptr += batch_size * height_col * width_col;
239
+ }
240
+ }
241
+ }
242
+ }
243
+
244
+ void deformable_im2col(
245
+ const at::Tensor data_im, const at::Tensor data_offset, const int channels,
246
+ const int height, const int width, const int ksize_h, const int ksize_w,
247
+ const int pad_h, const int pad_w, const int stride_h, const int stride_w,
248
+ const int dilation_h, const int dilation_w, const int parallel_imgs,
249
+ const int deformable_group, at::Tensor data_col)
250
+ {
251
+ // num_axes should be smaller than block size
252
+ // todo: check parallel_imgs is correctly passed in
253
+ int height_col = (height + 2 * pad_h - (dilation_h * (ksize_h - 1) + 1)) / stride_h + 1;
254
+ int width_col = (width + 2 * pad_w - (dilation_w * (ksize_w - 1) + 1)) / stride_w + 1;
255
+ int num_kernels = channels * height_col * width_col * parallel_imgs;
256
+ int channel_per_deformable_group = channels / deformable_group;
257
+
258
+ AT_DISPATCH_FLOATING_TYPES_AND_HALF(
259
+ data_im.type(), "deformable_im2col_gpu", ([&] {
260
+ const scalar_t *data_im_ = data_im.data<scalar_t>();
261
+ const scalar_t *data_offset_ = data_offset.data<scalar_t>();
262
+ scalar_t *data_col_ = data_col.data<scalar_t>();
263
+
264
+ deformable_im2col_gpu_kernel<<<GET_BLOCKS(num_kernels), CUDA_NUM_THREADS>>>(
265
+ num_kernels, data_im_, data_offset_, height, width, ksize_h, ksize_w,
266
+ pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w,
267
+ channel_per_deformable_group, parallel_imgs, channels, deformable_group,
268
+ height_col, width_col, data_col_);
269
+ }));
270
+
271
+ cudaError_t err = cudaGetLastError();
272
+ if (err != cudaSuccess)
273
+ {
274
+ printf("error in deformable_im2col: %s\n", cudaGetErrorString(err));
275
+ }
276
+ }
277
+
278
+ template <typename scalar_t>
279
+ __global__ void deformable_col2im_gpu_kernel(
280
+ const int n, const scalar_t *data_col, const scalar_t *data_offset,
281
+ const int channels, const int height, const int width,
282
+ const int kernel_h, const int kernel_w,
283
+ const int pad_h, const int pad_w,
284
+ const int stride_h, const int stride_w,
285
+ const int dilation_h, const int dilation_w,
286
+ const int channel_per_deformable_group,
287
+ const int batch_size, const int deformable_group,
288
+ const int height_col, const int width_col,
289
+ scalar_t *grad_im)
290
+ {
291
+ CUDA_KERNEL_LOOP(index, n)
292
+ {
293
+ const int j = (index / width_col / height_col / batch_size) % kernel_w;
294
+ const int i = (index / width_col / height_col / batch_size / kernel_w) % kernel_h;
295
+ const int c = index / width_col / height_col / batch_size / kernel_w / kernel_h;
296
+ // compute the start and end of the output
297
+
298
+ const int deformable_group_index = c / channel_per_deformable_group;
299
+
300
+ int w_out = index % width_col;
301
+ int h_out = (index / width_col) % height_col;
302
+ int b = (index / width_col / height_col) % batch_size;
303
+ int w_in = w_out * stride_w - pad_w;
304
+ int h_in = h_out * stride_h - pad_h;
305
+
306
+ const scalar_t *data_offset_ptr = data_offset + (b * deformable_group + deformable_group_index) *
307
+ 2 * kernel_h * kernel_w * height_col * width_col;
308
+ const int data_offset_h_ptr = ((2 * (i * kernel_w + j)) * height_col + h_out) * width_col + w_out;
309
+ const int data_offset_w_ptr = ((2 * (i * kernel_w + j) + 1) * height_col + h_out) * width_col + w_out;
310
+ const scalar_t offset_h = data_offset_ptr[data_offset_h_ptr];
311
+ const scalar_t offset_w = data_offset_ptr[data_offset_w_ptr];
312
+ const scalar_t cur_inv_h_data = h_in + i * dilation_h + offset_h;
313
+ const scalar_t cur_inv_w_data = w_in + j * dilation_w + offset_w;
314
+
315
+ const scalar_t cur_top_grad = data_col[index];
316
+ const int cur_h = (int)cur_inv_h_data;
317
+ const int cur_w = (int)cur_inv_w_data;
318
+ for (int dy = -2; dy <= 2; dy++)
319
+ {
320
+ for (int dx = -2; dx <= 2; dx++)
321
+ {
322
+ if (cur_h + dy >= 0 && cur_h + dy < height &&
323
+ cur_w + dx >= 0 && cur_w + dx < width &&
324
+ abs(cur_inv_h_data - (cur_h + dy)) < 1 &&
325
+ abs(cur_inv_w_data - (cur_w + dx)) < 1)
326
+ {
327
+ int cur_bottom_grad_pos = ((b * channels + c) * height + cur_h + dy) * width + cur_w + dx;
328
+ scalar_t weight = get_gradient_weight(cur_inv_h_data, cur_inv_w_data, cur_h + dy, cur_w + dx, height, width);
329
+ atomicAdd(grad_im + cur_bottom_grad_pos, weight * cur_top_grad);
330
+ }
331
+ }
332
+ }
333
+ }
334
+ }
335
+
336
+ void deformable_col2im(
337
+ const at::Tensor data_col, const at::Tensor data_offset, const int channels,
338
+ const int height, const int width, const int ksize_h,
339
+ const int ksize_w, const int pad_h, const int pad_w,
340
+ const int stride_h, const int stride_w,
341
+ const int dilation_h, const int dilation_w,
342
+ const int parallel_imgs, const int deformable_group,
343
+ at::Tensor grad_im)
344
+ {
345
+
346
+ // todo: make sure parallel_imgs is passed in correctly
347
+ int height_col = (height + 2 * pad_h - (dilation_h * (ksize_h - 1) + 1)) / stride_h + 1;
348
+ int width_col = (width + 2 * pad_w - (dilation_w * (ksize_w - 1) + 1)) / stride_w + 1;
349
+ int num_kernels = channels * ksize_h * ksize_w * height_col * width_col * parallel_imgs;
350
+ int channel_per_deformable_group = channels / deformable_group;
351
+
352
+ AT_DISPATCH_FLOATING_TYPES_AND_HALF(
353
+ data_col.type(), "deformable_col2im_gpu", ([&] {
354
+ const scalar_t *data_col_ = data_col.data<scalar_t>();
355
+ const scalar_t *data_offset_ = data_offset.data<scalar_t>();
356
+ scalar_t *grad_im_ = grad_im.data<scalar_t>();
357
+
358
+ deformable_col2im_gpu_kernel<<<GET_BLOCKS(num_kernels), CUDA_NUM_THREADS>>>(
359
+ num_kernels, data_col_, data_offset_, channels, height, width, ksize_h,
360
+ ksize_w, pad_h, pad_w, stride_h, stride_w,
361
+ dilation_h, dilation_w, channel_per_deformable_group,
362
+ parallel_imgs, deformable_group, height_col, width_col, grad_im_);
363
+ }));
364
+
365
+ cudaError_t err = cudaGetLastError();
366
+ if (err != cudaSuccess)
367
+ {
368
+ printf("error in deformable_col2im: %s\n", cudaGetErrorString(err));
369
+ }
370
+ }
371
+
372
+ template <typename scalar_t>
373
+ __global__ void deformable_col2im_coord_gpu_kernel(const int n, const scalar_t *data_col,
374
+ const scalar_t *data_im, const scalar_t *data_offset,
375
+ const int channels, const int height, const int width,
376
+ const int kernel_h, const int kernel_w,
377
+ const int pad_h, const int pad_w,
378
+ const int stride_h, const int stride_w,
379
+ const int dilation_h, const int dilation_w,
380
+ const int channel_per_deformable_group,
381
+ const int batch_size, const int offset_channels, const int deformable_group,
382
+ const int height_col, const int width_col, scalar_t *grad_offset)
383
+ {
384
+ CUDA_KERNEL_LOOP(index, n)
385
+ {
386
+ scalar_t val = 0;
387
+ int w = index % width_col;
388
+ int h = (index / width_col) % height_col;
389
+ int c = (index / width_col / height_col) % offset_channels;
390
+ int b = (index / width_col / height_col) / offset_channels;
391
+ // compute the start and end of the output
392
+
393
+ const int deformable_group_index = c / (2 * kernel_h * kernel_w);
394
+ const int col_step = kernel_h * kernel_w;
395
+ int cnt = 0;
396
+ const scalar_t *data_col_ptr = data_col + deformable_group_index * channel_per_deformable_group *
397
+ batch_size * width_col * height_col;
398
+ const scalar_t *data_im_ptr = data_im + (b * deformable_group + deformable_group_index) *
399
+ channel_per_deformable_group / kernel_h / kernel_w * height * width;
400
+ const scalar_t *data_offset_ptr = data_offset + (b * deformable_group + deformable_group_index) * 2 *
401
+ kernel_h * kernel_w * height_col * width_col;
402
+
403
+ const int offset_c = c - deformable_group_index * 2 * kernel_h * kernel_w;
404
+
405
+ for (int col_c = (offset_c / 2); col_c < channel_per_deformable_group; col_c += col_step)
406
+ {
407
+ const int col_pos = (((col_c * batch_size + b) * height_col) + h) * width_col + w;
408
+ const int bp_dir = offset_c % 2;
409
+
410
+ int j = (col_pos / width_col / height_col / batch_size) % kernel_w;
411
+ int i = (col_pos / width_col / height_col / batch_size / kernel_w) % kernel_h;
412
+ int w_out = col_pos % width_col;
413
+ int h_out = (col_pos / width_col) % height_col;
414
+ int w_in = w_out * stride_w - pad_w;
415
+ int h_in = h_out * stride_h - pad_h;
416
+ const int data_offset_h_ptr = (((2 * (i * kernel_w + j)) * height_col + h_out) * width_col + w_out);
417
+ const int data_offset_w_ptr = (((2 * (i * kernel_w + j) + 1) * height_col + h_out) * width_col + w_out);
418
+ const scalar_t offset_h = data_offset_ptr[data_offset_h_ptr];
419
+ const scalar_t offset_w = data_offset_ptr[data_offset_w_ptr];
420
+ scalar_t inv_h = h_in + i * dilation_h + offset_h;
421
+ scalar_t inv_w = w_in + j * dilation_w + offset_w;
422
+ if (inv_h <= -1 || inv_w <= -1 || inv_h >= height || inv_w >= width)
423
+ {
424
+ inv_h = inv_w = -2;
425
+ }
426
+ const scalar_t weight = get_coordinate_weight(
427
+ inv_h, inv_w,
428
+ height, width, data_im_ptr + cnt * height * width, width, bp_dir);
429
+ val += weight * data_col_ptr[col_pos];
430
+ cnt += 1;
431
+ }
432
+
433
+ grad_offset[index] = val;
434
+ }
435
+ }
436
+
437
+ void deformable_col2im_coord(
438
+ const at::Tensor data_col, const at::Tensor data_im, const at::Tensor data_offset,
439
+ const int channels, const int height, const int width, const int ksize_h,
440
+ const int ksize_w, const int pad_h, const int pad_w, const int stride_h,
441
+ const int stride_w, const int dilation_h, const int dilation_w,
442
+ const int parallel_imgs, const int deformable_group, at::Tensor grad_offset)
443
+ {
444
+
445
+ int height_col = (height + 2 * pad_h - (dilation_h * (ksize_h - 1) + 1)) / stride_h + 1;
446
+ int width_col = (width + 2 * pad_w - (dilation_w * (ksize_w - 1) + 1)) / stride_w + 1;
447
+ int num_kernels = height_col * width_col * 2 * ksize_h * ksize_w * deformable_group * parallel_imgs;
448
+ int channel_per_deformable_group = channels * ksize_h * ksize_w / deformable_group;
449
+
450
+ AT_DISPATCH_FLOATING_TYPES_AND_HALF(
451
+ data_col.type(), "deformable_col2im_coord_gpu", ([&] {
452
+ const scalar_t *data_col_ = data_col.data<scalar_t>();
453
+ const scalar_t *data_im_ = data_im.data<scalar_t>();
454
+ const scalar_t *data_offset_ = data_offset.data<scalar_t>();
455
+ scalar_t *grad_offset_ = grad_offset.data<scalar_t>();
456
+
457
+ deformable_col2im_coord_gpu_kernel<<<GET_BLOCKS(num_kernels), CUDA_NUM_THREADS>>>(
458
+ num_kernels, data_col_, data_im_, data_offset_, channels, height, width,
459
+ ksize_h, ksize_w, pad_h, pad_w, stride_h, stride_w,
460
+ dilation_h, dilation_w, channel_per_deformable_group,
461
+ parallel_imgs, 2 * ksize_h * ksize_w * deformable_group, deformable_group,
462
+ height_col, width_col, grad_offset_);
463
+ }));
464
+ }
465
+
466
+ template <typename scalar_t>
467
+ __device__ scalar_t dmcn_im2col_bilinear(const scalar_t *bottom_data, const int data_width,
468
+ const int height, const int width, scalar_t h, scalar_t w)
469
+ {
470
+ int h_low = floor(h);
471
+ int w_low = floor(w);
472
+ int h_high = h_low + 1;
473
+ int w_high = w_low + 1;
474
+
475
+ scalar_t lh = h - h_low;
476
+ scalar_t lw = w - w_low;
477
+ scalar_t hh = 1 - lh, hw = 1 - lw;
478
+
479
+ scalar_t v1 = 0;
480
+ if (h_low >= 0 && w_low >= 0)
481
+ v1 = bottom_data[h_low * data_width + w_low];
482
+ scalar_t v2 = 0;
483
+ if (h_low >= 0 && w_high <= width - 1)
484
+ v2 = bottom_data[h_low * data_width + w_high];
485
+ scalar_t v3 = 0;
486
+ if (h_high <= height - 1 && w_low >= 0)
487
+ v3 = bottom_data[h_high * data_width + w_low];
488
+ scalar_t v4 = 0;
489
+ if (h_high <= height - 1 && w_high <= width - 1)
490
+ v4 = bottom_data[h_high * data_width + w_high];
491
+
492
+ scalar_t w1 = hh * hw, w2 = hh * lw, w3 = lh * hw, w4 = lh * lw;
493
+
494
+ scalar_t val = (w1 * v1 + w2 * v2 + w3 * v3 + w4 * v4);
495
+ return val;
496
+ }
497
+
498
+ template <typename scalar_t>
499
+ __device__ scalar_t dmcn_get_gradient_weight(scalar_t argmax_h, scalar_t argmax_w,
500
+ const int h, const int w, const int height, const int width)
501
+ {
502
+ if (argmax_h <= -1 || argmax_h >= height || argmax_w <= -1 || argmax_w >= width)
503
+ {
504
+ //empty
505
+ return 0;
506
+ }
507
+
508
+ int argmax_h_low = floor(argmax_h);
509
+ int argmax_w_low = floor(argmax_w);
510
+ int argmax_h_high = argmax_h_low + 1;
511
+ int argmax_w_high = argmax_w_low + 1;
512
+
513
+ scalar_t weight = 0;
514
+ if (h == argmax_h_low && w == argmax_w_low)
515
+ weight = (h + 1 - argmax_h) * (w + 1 - argmax_w);
516
+ if (h == argmax_h_low && w == argmax_w_high)
517
+ weight = (h + 1 - argmax_h) * (argmax_w + 1 - w);
518
+ if (h == argmax_h_high && w == argmax_w_low)
519
+ weight = (argmax_h + 1 - h) * (w + 1 - argmax_w);
520
+ if (h == argmax_h_high && w == argmax_w_high)
521
+ weight = (argmax_h + 1 - h) * (argmax_w + 1 - w);
522
+ return weight;
523
+ }
524
+
525
+ template <typename scalar_t>
526
+ __device__ scalar_t dmcn_get_coordinate_weight(scalar_t argmax_h, scalar_t argmax_w,
527
+ const int height, const int width, const scalar_t *im_data,
528
+ const int data_width, const int bp_dir)
529
+ {
530
+ if (argmax_h <= -1 || argmax_h >= height || argmax_w <= -1 || argmax_w >= width)
531
+ {
532
+ //empty
533
+ return 0;
534
+ }
535
+
536
+ int argmax_h_low = floor(argmax_h);
537
+ int argmax_w_low = floor(argmax_w);
538
+ int argmax_h_high = argmax_h_low + 1;
539
+ int argmax_w_high = argmax_w_low + 1;
540
+
541
+ scalar_t weight = 0;
542
+
543
+ if (bp_dir == 0)
544
+ {
545
+ if (argmax_h_low >= 0 && argmax_w_low >= 0)
546
+ weight += -1 * (argmax_w_low + 1 - argmax_w) * im_data[argmax_h_low * data_width + argmax_w_low];
547
+ if (argmax_h_low >= 0 && argmax_w_high <= width - 1)
548
+ weight += -1 * (argmax_w - argmax_w_low) * im_data[argmax_h_low * data_width + argmax_w_high];
549
+ if (argmax_h_high <= height - 1 && argmax_w_low >= 0)
550
+ weight += (argmax_w_low + 1 - argmax_w) * im_data[argmax_h_high * data_width + argmax_w_low];
551
+ if (argmax_h_high <= height - 1 && argmax_w_high <= width - 1)
552
+ weight += (argmax_w - argmax_w_low) * im_data[argmax_h_high * data_width + argmax_w_high];
553
+ }
554
+ else if (bp_dir == 1)
555
+ {
556
+ if (argmax_h_low >= 0 && argmax_w_low >= 0)
557
+ weight += -1 * (argmax_h_low + 1 - argmax_h) * im_data[argmax_h_low * data_width + argmax_w_low];
558
+ if (argmax_h_low >= 0 && argmax_w_high <= width - 1)
559
+ weight += (argmax_h_low + 1 - argmax_h) * im_data[argmax_h_low * data_width + argmax_w_high];
560
+ if (argmax_h_high <= height - 1 && argmax_w_low >= 0)
561
+ weight += -1 * (argmax_h - argmax_h_low) * im_data[argmax_h_high * data_width + argmax_w_low];
562
+ if (argmax_h_high <= height - 1 && argmax_w_high <= width - 1)
563
+ weight += (argmax_h - argmax_h_low) * im_data[argmax_h_high * data_width + argmax_w_high];
564
+ }
565
+
566
+ return weight;
567
+ }
568
+
569
+ template <typename scalar_t>
570
+ __global__ void modulated_deformable_im2col_gpu_kernel(const int n,
571
+ const scalar_t *data_im, const scalar_t *data_offset, const scalar_t *data_mask,
572
+ const int height, const int width, const int kernel_h, const int kernel_w,
573
+ const int pad_h, const int pad_w,
574
+ const int stride_h, const int stride_w,
575
+ const int dilation_h, const int dilation_w,
576
+ const int channel_per_deformable_group,
577
+ const int batch_size, const int num_channels, const int deformable_group,
578
+ const int height_col, const int width_col,
579
+ scalar_t *data_col)
580
+ {
581
+ CUDA_KERNEL_LOOP(index, n)
582
+ {
583
+ // index index of output matrix
584
+ const int w_col = index % width_col;
585
+ const int h_col = (index / width_col) % height_col;
586
+ const int b_col = (index / width_col / height_col) % batch_size;
587
+ const int c_im = (index / width_col / height_col) / batch_size;
588
+ const int c_col = c_im * kernel_h * kernel_w;
589
+
590
+ // compute deformable group index
591
+ const int deformable_group_index = c_im / channel_per_deformable_group;
592
+
593
+ const int h_in = h_col * stride_h - pad_h;
594
+ const int w_in = w_col * stride_w - pad_w;
595
+
596
+ scalar_t *data_col_ptr = data_col + ((c_col * batch_size + b_col) * height_col + h_col) * width_col + w_col;
597
+ //const float* data_im_ptr = data_im + ((b_col * num_channels + c_im) * height + h_in) * width + w_in;
598
+ const scalar_t *data_im_ptr = data_im + (b_col * num_channels + c_im) * height * width;
599
+ const scalar_t *data_offset_ptr = data_offset + (b_col * deformable_group + deformable_group_index) * 2 * kernel_h * kernel_w * height_col * width_col;
600
+
601
+ const scalar_t *data_mask_ptr = data_mask + (b_col * deformable_group + deformable_group_index) * kernel_h * kernel_w * height_col * width_col;
602
+
603
+ for (int i = 0; i < kernel_h; ++i)
604
+ {
605
+ for (int j = 0; j < kernel_w; ++j)
606
+ {
607
+ const int data_offset_h_ptr = ((2 * (i * kernel_w + j)) * height_col + h_col) * width_col + w_col;
608
+ const int data_offset_w_ptr = ((2 * (i * kernel_w + j) + 1) * height_col + h_col) * width_col + w_col;
609
+ const int data_mask_hw_ptr = ((i * kernel_w + j) * height_col + h_col) * width_col + w_col;
610
+ const scalar_t offset_h = data_offset_ptr[data_offset_h_ptr];
611
+ const scalar_t offset_w = data_offset_ptr[data_offset_w_ptr];
612
+ const scalar_t mask = data_mask_ptr[data_mask_hw_ptr];
613
+ scalar_t val = static_cast<scalar_t>(0);
614
+ const scalar_t h_im = h_in + i * dilation_h + offset_h;
615
+ const scalar_t w_im = w_in + j * dilation_w + offset_w;
616
+ //if (h_im >= 0 && w_im >= 0 && h_im < height && w_im < width) {
617
+ if (h_im > -1 && w_im > -1 && h_im < height && w_im < width)
618
+ {
619
+ //const float map_h = i * dilation_h + offset_h;
620
+ //const float map_w = j * dilation_w + offset_w;
621
+ //const int cur_height = height - h_in;
622
+ //const int cur_width = width - w_in;
623
+ //val = dmcn_im2col_bilinear(data_im_ptr, width, cur_height, cur_width, map_h, map_w);
624
+ val = dmcn_im2col_bilinear(data_im_ptr, width, height, width, h_im, w_im);
625
+ }
626
+ *data_col_ptr = val * mask;
627
+ data_col_ptr += batch_size * height_col * width_col;
628
+ //data_col_ptr += height_col * width_col;
629
+ }
630
+ }
631
+ }
632
+ }
633
+
634
+ template <typename scalar_t>
635
+ __global__ void modulated_deformable_col2im_gpu_kernel(const int n,
636
+ const scalar_t *data_col, const scalar_t *data_offset, const scalar_t *data_mask,
637
+ const int channels, const int height, const int width,
638
+ const int kernel_h, const int kernel_w,
639
+ const int pad_h, const int pad_w,
640
+ const int stride_h, const int stride_w,
641
+ const int dilation_h, const int dilation_w,
642
+ const int channel_per_deformable_group,
643
+ const int batch_size, const int deformable_group,
644
+ const int height_col, const int width_col,
645
+ scalar_t *grad_im)
646
+ {
647
+ CUDA_KERNEL_LOOP(index, n)
648
+ {
649
+ const int j = (index / width_col / height_col / batch_size) % kernel_w;
650
+ const int i = (index / width_col / height_col / batch_size / kernel_w) % kernel_h;
651
+ const int c = index / width_col / height_col / batch_size / kernel_w / kernel_h;
652
+ // compute the start and end of the output
653
+
654
+ const int deformable_group_index = c / channel_per_deformable_group;
655
+
656
+ int w_out = index % width_col;
657
+ int h_out = (index / width_col) % height_col;
658
+ int b = (index / width_col / height_col) % batch_size;
659
+ int w_in = w_out * stride_w - pad_w;
660
+ int h_in = h_out * stride_h - pad_h;
661
+
662
+ const scalar_t *data_offset_ptr = data_offset + (b * deformable_group + deformable_group_index) * 2 * kernel_h * kernel_w * height_col * width_col;
663
+ const scalar_t *data_mask_ptr = data_mask + (b * deformable_group + deformable_group_index) * kernel_h * kernel_w * height_col * width_col;
664
+ const int data_offset_h_ptr = ((2 * (i * kernel_w + j)) * height_col + h_out) * width_col + w_out;
665
+ const int data_offset_w_ptr = ((2 * (i * kernel_w + j) + 1) * height_col + h_out) * width_col + w_out;
666
+ const int data_mask_hw_ptr = ((i * kernel_w + j) * height_col + h_out) * width_col + w_out;
667
+ const scalar_t offset_h = data_offset_ptr[data_offset_h_ptr];
668
+ const scalar_t offset_w = data_offset_ptr[data_offset_w_ptr];
669
+ const scalar_t mask = data_mask_ptr[data_mask_hw_ptr];
670
+ const scalar_t cur_inv_h_data = h_in + i * dilation_h + offset_h;
671
+ const scalar_t cur_inv_w_data = w_in + j * dilation_w + offset_w;
672
+
673
+ const scalar_t cur_top_grad = data_col[index] * mask;
674
+ const int cur_h = (int)cur_inv_h_data;
675
+ const int cur_w = (int)cur_inv_w_data;
676
+ for (int dy = -2; dy <= 2; dy++)
677
+ {
678
+ for (int dx = -2; dx <= 2; dx++)
679
+ {
680
+ if (cur_h + dy >= 0 && cur_h + dy < height &&
681
+ cur_w + dx >= 0 && cur_w + dx < width &&
682
+ abs(cur_inv_h_data - (cur_h + dy)) < 1 &&
683
+ abs(cur_inv_w_data - (cur_w + dx)) < 1)
684
+ {
685
+ int cur_bottom_grad_pos = ((b * channels + c) * height + cur_h + dy) * width + cur_w + dx;
686
+ scalar_t weight = dmcn_get_gradient_weight(cur_inv_h_data, cur_inv_w_data, cur_h + dy, cur_w + dx, height, width);
687
+ atomicAdd(grad_im + cur_bottom_grad_pos, weight * cur_top_grad);
688
+ }
689
+ }
690
+ }
691
+ }
692
+ }
693
+
694
+ template <typename scalar_t>
695
+ __global__ void modulated_deformable_col2im_coord_gpu_kernel(const int n,
696
+ const scalar_t *data_col, const scalar_t *data_im,
697
+ const scalar_t *data_offset, const scalar_t *data_mask,
698
+ const int channels, const int height, const int width,
699
+ const int kernel_h, const int kernel_w,
700
+ const int pad_h, const int pad_w,
701
+ const int stride_h, const int stride_w,
702
+ const int dilation_h, const int dilation_w,
703
+ const int channel_per_deformable_group,
704
+ const int batch_size, const int offset_channels, const int deformable_group,
705
+ const int height_col, const int width_col,
706
+ scalar_t *grad_offset, scalar_t *grad_mask)
707
+ {
708
+ CUDA_KERNEL_LOOP(index, n)
709
+ {
710
+ scalar_t val = 0, mval = 0;
711
+ int w = index % width_col;
712
+ int h = (index / width_col) % height_col;
713
+ int c = (index / width_col / height_col) % offset_channels;
714
+ int b = (index / width_col / height_col) / offset_channels;
715
+ // compute the start and end of the output
716
+
717
+ const int deformable_group_index = c / (2 * kernel_h * kernel_w);
718
+ const int col_step = kernel_h * kernel_w;
719
+ int cnt = 0;
720
+ const scalar_t *data_col_ptr = data_col + deformable_group_index * channel_per_deformable_group * batch_size * width_col * height_col;
721
+ const scalar_t *data_im_ptr = data_im + (b * deformable_group + deformable_group_index) * channel_per_deformable_group / kernel_h / kernel_w * height * width;
722
+ const scalar_t *data_offset_ptr = data_offset + (b * deformable_group + deformable_group_index) * 2 * kernel_h * kernel_w * height_col * width_col;
723
+ const scalar_t *data_mask_ptr = data_mask + (b * deformable_group + deformable_group_index) * kernel_h * kernel_w * height_col * width_col;
724
+
725
+ const int offset_c = c - deformable_group_index * 2 * kernel_h * kernel_w;
726
+
727
+ for (int col_c = (offset_c / 2); col_c < channel_per_deformable_group; col_c += col_step)
728
+ {
729
+ const int col_pos = (((col_c * batch_size + b) * height_col) + h) * width_col + w;
730
+ const int bp_dir = offset_c % 2;
731
+
732
+ int j = (col_pos / width_col / height_col / batch_size) % kernel_w;
733
+ int i = (col_pos / width_col / height_col / batch_size / kernel_w) % kernel_h;
734
+ int w_out = col_pos % width_col;
735
+ int h_out = (col_pos / width_col) % height_col;
736
+ int w_in = w_out * stride_w - pad_w;
737
+ int h_in = h_out * stride_h - pad_h;
738
+ const int data_offset_h_ptr = (((2 * (i * kernel_w + j)) * height_col + h_out) * width_col + w_out);
739
+ const int data_offset_w_ptr = (((2 * (i * kernel_w + j) + 1) * height_col + h_out) * width_col + w_out);
740
+ const int data_mask_hw_ptr = (((i * kernel_w + j) * height_col + h_out) * width_col + w_out);
741
+ const scalar_t offset_h = data_offset_ptr[data_offset_h_ptr];
742
+ const scalar_t offset_w = data_offset_ptr[data_offset_w_ptr];
743
+ const scalar_t mask = data_mask_ptr[data_mask_hw_ptr];
744
+ scalar_t inv_h = h_in + i * dilation_h + offset_h;
745
+ scalar_t inv_w = w_in + j * dilation_w + offset_w;
746
+ if (inv_h <= -1 || inv_w <= -1 || inv_h >= height || inv_w >= width)
747
+ {
748
+ inv_h = inv_w = -2;
749
+ }
750
+ else
751
+ {
752
+ mval += data_col_ptr[col_pos] * dmcn_im2col_bilinear(data_im_ptr + cnt * height * width, width, height, width, inv_h, inv_w);
753
+ }
754
+ const scalar_t weight = dmcn_get_coordinate_weight(
755
+ inv_h, inv_w,
756
+ height, width, data_im_ptr + cnt * height * width, width, bp_dir);
757
+ val += weight * data_col_ptr[col_pos] * mask;
758
+ cnt += 1;
759
+ }
760
+ // KERNEL_ASSIGN(grad_offset[index], offset_req, val);
761
+ grad_offset[index] = val;
762
+ if (offset_c % 2 == 0)
763
+ // KERNEL_ASSIGN(grad_mask[(((b * deformable_group + deformable_group_index) * kernel_h * kernel_w + offset_c / 2) * height_col + h) * width_col + w], mask_req, mval);
764
+ grad_mask[(((b * deformable_group + deformable_group_index) * kernel_h * kernel_w + offset_c / 2) * height_col + h) * width_col + w] = mval;
765
+ }
766
+ }
767
+
768
+ void modulated_deformable_im2col_cuda(
769
+ const at::Tensor data_im, const at::Tensor data_offset, const at::Tensor data_mask,
770
+ const int batch_size, const int channels, const int height_im, const int width_im,
771
+ const int height_col, const int width_col, const int kernel_h, const int kenerl_w,
772
+ const int pad_h, const int pad_w, const int stride_h, const int stride_w,
773
+ const int dilation_h, const int dilation_w,
774
+ const int deformable_group, at::Tensor data_col)
775
+ {
776
+ // num_axes should be smaller than block size
777
+ const int channel_per_deformable_group = channels / deformable_group;
778
+ const int num_kernels = channels * batch_size * height_col * width_col;
779
+
780
+ AT_DISPATCH_FLOATING_TYPES_AND_HALF(
781
+ data_im.type(), "modulated_deformable_im2col_gpu", ([&] {
782
+ const scalar_t *data_im_ = data_im.data<scalar_t>();
783
+ const scalar_t *data_offset_ = data_offset.data<scalar_t>();
784
+ const scalar_t *data_mask_ = data_mask.data<scalar_t>();
785
+ scalar_t *data_col_ = data_col.data<scalar_t>();
786
+
787
+ modulated_deformable_im2col_gpu_kernel<<<GET_BLOCKS(num_kernels), CUDA_NUM_THREADS>>>(
788
+ num_kernels, data_im_, data_offset_, data_mask_, height_im, width_im, kernel_h, kenerl_w,
789
+ pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, channel_per_deformable_group,
790
+ batch_size, channels, deformable_group, height_col, width_col, data_col_);
791
+ }));
792
+
793
+ cudaError_t err = cudaGetLastError();
794
+ if (err != cudaSuccess)
795
+ {
796
+ // printf("error in modulated_deformable_im2col_cuda: %s\n", cudaGetErrorString(err));
797
+ }
798
+ }
799
+
800
+ void modulated_deformable_col2im_cuda(
801
+ const at::Tensor data_col, const at::Tensor data_offset, const at::Tensor data_mask,
802
+ const int batch_size, const int channels, const int height_im, const int width_im,
803
+ const int height_col, const int width_col, const int kernel_h, const int kernel_w,
804
+ const int pad_h, const int pad_w, const int stride_h, const int stride_w,
805
+ const int dilation_h, const int dilation_w,
806
+ const int deformable_group, at::Tensor grad_im)
807
+ {
808
+
809
+ const int channel_per_deformable_group = channels / deformable_group;
810
+ const int num_kernels = channels * kernel_h * kernel_w * batch_size * height_col * width_col;
811
+
812
+ AT_DISPATCH_FLOATING_TYPES_AND_HALF(
813
+ data_col.type(), "modulated_deformable_col2im_gpu", ([&] {
814
+ const scalar_t *data_col_ = data_col.data<scalar_t>();
815
+ const scalar_t *data_offset_ = data_offset.data<scalar_t>();
816
+ const scalar_t *data_mask_ = data_mask.data<scalar_t>();
817
+ scalar_t *grad_im_ = grad_im.data<scalar_t>();
818
+
819
+ modulated_deformable_col2im_gpu_kernel<<<GET_BLOCKS(num_kernels), CUDA_NUM_THREADS>>>(
820
+ num_kernels, data_col_, data_offset_, data_mask_, channels, height_im, width_im,
821
+ kernel_h, kernel_w, pad_h, pad_h, stride_h, stride_w,
822
+ dilation_h, dilation_w, channel_per_deformable_group,
823
+ batch_size, deformable_group, height_col, width_col, grad_im_);
824
+ }));
825
+
826
+ cudaError_t err = cudaGetLastError();
827
+ if (err != cudaSuccess)
828
+ {
829
+ printf("error in modulated_deformable_col2im_cuda: %s\n", cudaGetErrorString(err));
830
+ }
831
+ }
832
+
833
+ void modulated_deformable_col2im_coord_cuda(
834
+ const at::Tensor data_col, const at::Tensor data_im, const at::Tensor data_offset, const at::Tensor data_mask,
835
+ const int batch_size, const int channels, const int height_im, const int width_im,
836
+ const int height_col, const int width_col, const int kernel_h, const int kernel_w,
837
+ const int pad_h, const int pad_w, const int stride_h, const int stride_w,
838
+ const int dilation_h, const int dilation_w,
839
+ const int deformable_group,
840
+ at::Tensor grad_offset, at::Tensor grad_mask)
841
+ {
842
+ const int num_kernels = batch_size * height_col * width_col * 2 * kernel_h * kernel_w * deformable_group;
843
+ const int channel_per_deformable_group = channels * kernel_h * kernel_w / deformable_group;
844
+
845
+ AT_DISPATCH_FLOATING_TYPES_AND_HALF(
846
+ data_col.type(), "modulated_deformable_col2im_coord_gpu", ([&] {
847
+ const scalar_t *data_col_ = data_col.data<scalar_t>();
848
+ const scalar_t *data_im_ = data_im.data<scalar_t>();
849
+ const scalar_t *data_offset_ = data_offset.data<scalar_t>();
850
+ const scalar_t *data_mask_ = data_mask.data<scalar_t>();
851
+ scalar_t *grad_offset_ = grad_offset.data<scalar_t>();
852
+ scalar_t *grad_mask_ = grad_mask.data<scalar_t>();
853
+
854
+ modulated_deformable_col2im_coord_gpu_kernel<<<GET_BLOCKS(num_kernels), CUDA_NUM_THREADS>>>(
855
+ num_kernels, data_col_, data_im_, data_offset_, data_mask_, channels, height_im, width_im,
856
+ kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w,
857
+ dilation_h, dilation_w, channel_per_deformable_group,
858
+ batch_size, 2 * kernel_h * kernel_w * deformable_group, deformable_group, height_col, width_col,
859
+ grad_offset_, grad_mask_);
860
+ }));
861
+ cudaError_t err = cudaGetLastError();
862
+ if (err != cudaSuccess)
863
+ {
864
+ printf("error in modulated_deformable_col2im_coord_cuda: %s\n", cudaGetErrorString(err));
865
+ }
866
+ }
IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/src/deform_pool_cuda.cpp ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // modify from
2
+ // https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/blob/mmdetection/mmdet/ops/dcn/src/modulated_dcn_cuda.c
3
+
4
+ // based on
5
+ // author: Charles Shang
6
+ // https://github.com/torch/cunn/blob/master/lib/THCUNN/generic/SpatialConvolutionMM.cu
7
+
8
+ #include <torch/extension.h>
9
+
10
+ #include <cmath>
11
+ #include <vector>
12
+
13
+ void DeformablePSROIPoolForward(
14
+ const at::Tensor data, const at::Tensor bbox, const at::Tensor trans,
15
+ at::Tensor out, at::Tensor top_count, const int batch, const int channels,
16
+ const int height, const int width, const int num_bbox,
17
+ const int channels_trans, const int no_trans, const float spatial_scale,
18
+ const int output_dim, const int group_size, const int pooled_size,
19
+ const int part_size, const int sample_per_part, const float trans_std);
20
+
21
+ void DeformablePSROIPoolBackwardAcc(
22
+ const at::Tensor out_grad, const at::Tensor data, const at::Tensor bbox,
23
+ const at::Tensor trans, const at::Tensor top_count, at::Tensor in_grad,
24
+ at::Tensor trans_grad, const int batch, const int channels,
25
+ const int height, const int width, const int num_bbox,
26
+ const int channels_trans, const int no_trans, const float spatial_scale,
27
+ const int output_dim, const int group_size, const int pooled_size,
28
+ const int part_size, const int sample_per_part, const float trans_std);
29
+
30
+ void deform_psroi_pooling_cuda_forward(
31
+ at::Tensor input, at::Tensor bbox, at::Tensor trans, at::Tensor out,
32
+ at::Tensor top_count, const int no_trans, const float spatial_scale,
33
+ const int output_dim, const int group_size, const int pooled_size,
34
+ const int part_size, const int sample_per_part, const float trans_std) {
35
+ TORCH_CHECK(input.is_contiguous(), "input tensor has to be contiguous");
36
+
37
+ const int batch = input.size(0);
38
+ const int channels = input.size(1);
39
+ const int height = input.size(2);
40
+ const int width = input.size(3);
41
+ const int channels_trans = no_trans ? 2 : trans.size(1);
42
+
43
+ const int num_bbox = bbox.size(0);
44
+ if (num_bbox != out.size(0))
45
+ AT_ERROR("Output shape and bbox number wont match: (%d vs %d).",
46
+ out.size(0), num_bbox);
47
+
48
+ DeformablePSROIPoolForward(
49
+ input, bbox, trans, out, top_count, batch, channels, height, width,
50
+ num_bbox, channels_trans, no_trans, spatial_scale, output_dim, group_size,
51
+ pooled_size, part_size, sample_per_part, trans_std);
52
+ }
53
+
54
+ void deform_psroi_pooling_cuda_backward(
55
+ at::Tensor out_grad, at::Tensor input, at::Tensor bbox, at::Tensor trans,
56
+ at::Tensor top_count, at::Tensor input_grad, at::Tensor trans_grad,
57
+ const int no_trans, const float spatial_scale, const int output_dim,
58
+ const int group_size, const int pooled_size, const int part_size,
59
+ const int sample_per_part, const float trans_std) {
60
+ TORCH_CHECK(out_grad.is_contiguous(), "out_grad tensor has to be contiguous");
61
+ TORCH_CHECK(input.is_contiguous(), "input tensor has to be contiguous");
62
+
63
+ const int batch = input.size(0);
64
+ const int channels = input.size(1);
65
+ const int height = input.size(2);
66
+ const int width = input.size(3);
67
+ const int channels_trans = no_trans ? 2 : trans.size(1);
68
+
69
+ const int num_bbox = bbox.size(0);
70
+ if (num_bbox != out_grad.size(0))
71
+ AT_ERROR("Output shape and bbox number wont match: (%d vs %d).",
72
+ out_grad.size(0), num_bbox);
73
+
74
+ DeformablePSROIPoolBackwardAcc(
75
+ out_grad, input, bbox, trans, top_count, input_grad, trans_grad, batch,
76
+ channels, height, width, num_bbox, channels_trans, no_trans,
77
+ spatial_scale, output_dim, group_size, pooled_size, part_size,
78
+ sample_per_part, trans_std);
79
+ }
80
+
81
+ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
82
+ m.def("deform_psroi_pooling_cuda_forward", &deform_psroi_pooling_cuda_forward,
83
+ "deform psroi pooling forward(CUDA)");
84
+ m.def("deform_psroi_pooling_cuda_backward",
85
+ &deform_psroi_pooling_cuda_backward,
86
+ "deform psroi pooling backward(CUDA)");
87
+ }
IndicPhotoOCR/detection/textbpn/network/backbone/assets/dcn/src/deform_pool_cuda_kernel.cu ADDED
@@ -0,0 +1,364 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*!
2
+ * Copyright (c) 2017 Microsoft
3
+ * Licensed under The MIT License [see LICENSE for details]
4
+ * \file deformable_psroi_pooling.cu
5
+ * \brief
6
+ * \author Yi Li, Guodong Zhang, Jifeng Dai
7
+ */
8
+ /***************** Adapted by Charles Shang *********************/
9
+ // modify from https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/blob/mmdetection/mmdet/ops/dcn/src/cuda/deform_psroi_pooling_cuda.cu
10
+
11
+ #include <ATen/ATen.h>
12
+ #include <THC/THCAtomics.cuh>
13
+ #include <stdio.h>
14
+ #include <math.h>
15
+ #include <algorithm>
16
+
17
+ using namespace at;
18
+
19
+ #define CUDA_KERNEL_LOOP(i, n) \
20
+ for (int i = blockIdx.x * blockDim.x + threadIdx.x; \
21
+ i < (n); \
22
+ i += blockDim.x * gridDim.x)
23
+
24
+ const int CUDA_NUM_THREADS = 1024;
25
+ inline int GET_BLOCKS(const int N)
26
+ {
27
+ return (N + CUDA_NUM_THREADS - 1) / CUDA_NUM_THREADS;
28
+ }
29
+
30
+ template <typename scalar_t>
31
+ __device__ scalar_t bilinear_interp(
32
+ const scalar_t *data,
33
+ const scalar_t x,
34
+ const scalar_t y,
35
+ const int width,
36
+ const int height)
37
+ {
38
+ int x1 = floor(x);
39
+ int x2 = ceil(x);
40
+ int y1 = floor(y);
41
+ int y2 = ceil(y);
42
+ scalar_t dist_x = (scalar_t)(x - x1);
43
+ scalar_t dist_y = (scalar_t)(y - y1);
44
+ scalar_t value11 = data[y1 * width + x1];
45
+ scalar_t value12 = data[y2 * width + x1];
46
+ scalar_t value21 = data[y1 * width + x2];
47
+ scalar_t value22 = data[y2 * width + x2];
48
+ scalar_t value = (1 - dist_x) * (1 - dist_y) * value11 + (1 - dist_x) * dist_y * value12 + dist_x * (1 - dist_y) * value21 + dist_x * dist_y * value22;
49
+ return value;
50
+ }
51
+
52
+ template <typename scalar_t>
53
+ __global__ void DeformablePSROIPoolForwardKernel(
54
+ const int count,
55
+ const scalar_t *bottom_data,
56
+ const scalar_t spatial_scale,
57
+ const int channels,
58
+ const int height, const int width,
59
+ const int pooled_height, const int pooled_width,
60
+ const scalar_t *bottom_rois, const scalar_t *bottom_trans,
61
+ const int no_trans,
62
+ const scalar_t trans_std,
63
+ const int sample_per_part,
64
+ const int output_dim,
65
+ const int group_size,
66
+ const int part_size,
67
+ const int num_classes,
68
+ const int channels_each_class,
69
+ scalar_t *top_data,
70
+ scalar_t *top_count)
71
+ {
72
+ CUDA_KERNEL_LOOP(index, count)
73
+ {
74
+ // The output is in order (n, ctop, ph, pw)
75
+ int pw = index % pooled_width;
76
+ int ph = (index / pooled_width) % pooled_height;
77
+ int ctop = (index / pooled_width / pooled_height) % output_dim;
78
+ int n = index / pooled_width / pooled_height / output_dim;
79
+
80
+ // [start, end) interval for spatial sampling
81
+ const scalar_t *offset_bottom_rois = bottom_rois + n * 5;
82
+ int roi_batch_ind = offset_bottom_rois[0];
83
+ scalar_t roi_start_w = (scalar_t)(round(offset_bottom_rois[1])) * spatial_scale - 0.5;
84
+ scalar_t roi_start_h = (scalar_t)(round(offset_bottom_rois[2])) * spatial_scale - 0.5;
85
+ scalar_t roi_end_w = (scalar_t)(round(offset_bottom_rois[3]) + 1.) * spatial_scale - 0.5;
86
+ scalar_t roi_end_h = (scalar_t)(round(offset_bottom_rois[4]) + 1.) * spatial_scale - 0.5;
87
+
88
+ // Force too small ROIs to be 1x1
89
+ scalar_t roi_width = max(roi_end_w - roi_start_w, 0.1); //avoid 0
90
+ scalar_t roi_height = max(roi_end_h - roi_start_h, 0.1);
91
+
92
+ // Compute w and h at bottom
93
+ scalar_t bin_size_h = roi_height / (scalar_t)(pooled_height);
94
+ scalar_t bin_size_w = roi_width / (scalar_t)(pooled_width);
95
+
96
+ scalar_t sub_bin_size_h = bin_size_h / (scalar_t)(sample_per_part);
97
+ scalar_t sub_bin_size_w = bin_size_w / (scalar_t)(sample_per_part);
98
+
99
+ int part_h = floor((scalar_t)(ph) / pooled_height * part_size);
100
+ int part_w = floor((scalar_t)(pw) / pooled_width * part_size);
101
+ int class_id = ctop / channels_each_class;
102
+ scalar_t trans_x = no_trans ? (scalar_t)(0) : bottom_trans[(((n * num_classes + class_id) * 2) * part_size + part_h) * part_size + part_w] * (scalar_t)trans_std;
103
+ scalar_t trans_y = no_trans ? (scalar_t)(0) : bottom_trans[(((n * num_classes + class_id) * 2 + 1) * part_size + part_h) * part_size + part_w] * (scalar_t)trans_std;
104
+
105
+ scalar_t wstart = (scalar_t)(pw)*bin_size_w + roi_start_w;
106
+ wstart += trans_x * roi_width;
107
+ scalar_t hstart = (scalar_t)(ph)*bin_size_h + roi_start_h;
108
+ hstart += trans_y * roi_height;
109
+
110
+ scalar_t sum = 0;
111
+ int count = 0;
112
+ int gw = floor((scalar_t)(pw)*group_size / pooled_width);
113
+ int gh = floor((scalar_t)(ph)*group_size / pooled_height);
114
+ gw = min(max(gw, 0), group_size - 1);
115
+ gh = min(max(gh, 0), group_size - 1);
116
+
117
+ const scalar_t *offset_bottom_data = bottom_data + (roi_batch_ind * channels) * height * width;
118
+ for (int ih = 0; ih < sample_per_part; ih++)
119
+ {
120
+ for (int iw = 0; iw < sample_per_part; iw++)
121
+ {
122
+ scalar_t w = wstart + iw * sub_bin_size_w;
123
+ scalar_t h = hstart + ih * sub_bin_size_h;
124
+ // bilinear interpolation
125
+ if (w < -0.5 || w > width - 0.5 || h < -0.5 || h > height - 0.5)
126
+ {
127
+ continue;
128
+ }
129
+ w = min(max(w, 0.), width - 1.);
130
+ h = min(max(h, 0.), height - 1.);
131
+ int c = (ctop * group_size + gh) * group_size + gw;
132
+ scalar_t val = bilinear_interp(offset_bottom_data + c * height * width, w, h, width, height);
133
+ sum += val;
134
+ count++;
135
+ }
136
+ }
137
+ top_data[index] = count == 0 ? (scalar_t)(0) : sum / count;
138
+ top_count[index] = count;
139
+ }
140
+ }
141
+
142
+ template <typename scalar_t>
143
+ __global__ void DeformablePSROIPoolBackwardAccKernel(
144
+ const int count,
145
+ const scalar_t *top_diff,
146
+ const scalar_t *top_count,
147
+ const int num_rois,
148
+ const scalar_t spatial_scale,
149
+ const int channels,
150
+ const int height, const int width,
151
+ const int pooled_height, const int pooled_width,
152
+ const int output_dim,
153
+ scalar_t *bottom_data_diff, scalar_t *bottom_trans_diff,
154
+ const scalar_t *bottom_data,
155
+ const scalar_t *bottom_rois,
156
+ const scalar_t *bottom_trans,
157
+ const int no_trans,
158
+ const scalar_t trans_std,
159
+ const int sample_per_part,
160
+ const int group_size,
161
+ const int part_size,
162
+ const int num_classes,
163
+ const int channels_each_class)
164
+ {
165
+ CUDA_KERNEL_LOOP(index, count)
166
+ {
167
+ // The output is in order (n, ctop, ph, pw)
168
+ int pw = index % pooled_width;
169
+ int ph = (index / pooled_width) % pooled_height;
170
+ int ctop = (index / pooled_width / pooled_height) % output_dim;
171
+ int n = index / pooled_width / pooled_height / output_dim;
172
+
173
+ // [start, end) interval for spatial sampling
174
+ const scalar_t *offset_bottom_rois = bottom_rois + n * 5;
175
+ int roi_batch_ind = offset_bottom_rois[0];
176
+ scalar_t roi_start_w = (scalar_t)(round(offset_bottom_rois[1])) * spatial_scale - 0.5;
177
+ scalar_t roi_start_h = (scalar_t)(round(offset_bottom_rois[2])) * spatial_scale - 0.5;
178
+ scalar_t roi_end_w = (scalar_t)(round(offset_bottom_rois[3]) + 1.) * spatial_scale - 0.5;
179
+ scalar_t roi_end_h = (scalar_t)(round(offset_bottom_rois[4]) + 1.) * spatial_scale - 0.5;
180
+
181
+ // Force too small ROIs to be 1x1
182
+ scalar_t roi_width = max(roi_end_w - roi_start_w, 0.1); //avoid 0
183
+ scalar_t roi_height = max(roi_end_h - roi_start_h, 0.1);
184
+
185
+ // Compute w and h at bottom
186
+ scalar_t bin_size_h = roi_height / (scalar_t)(pooled_height);
187
+ scalar_t bin_size_w = roi_width / (scalar_t)(pooled_width);
188
+
189
+ scalar_t sub_bin_size_h = bin_size_h / (scalar_t)(sample_per_part);
190
+ scalar_t sub_bin_size_w = bin_size_w / (scalar_t)(sample_per_part);
191
+
192
+ int part_h = floor((scalar_t)(ph) / pooled_height * part_size);
193
+ int part_w = floor((scalar_t)(pw) / pooled_width * part_size);
194
+ int class_id = ctop / channels_each_class;
195
+ scalar_t trans_x = no_trans ? (scalar_t)(0) : bottom_trans[(((n * num_classes + class_id) * 2) * part_size + part_h) * part_size + part_w] * (scalar_t)trans_std;
196
+ scalar_t trans_y = no_trans ? (scalar_t)(0) : bottom_trans[(((n * num_classes + class_id) * 2 + 1) * part_size + part_h) * part_size + part_w] * (scalar_t)trans_std;
197
+
198
+ scalar_t wstart = (scalar_t)(pw)*bin_size_w + roi_start_w;
199
+ wstart += trans_x * roi_width;
200
+ scalar_t hstart = (scalar_t)(ph)*bin_size_h + roi_start_h;
201
+ hstart += trans_y * roi_height;
202
+
203
+ if (top_count[index] <= 0)
204
+ {
205
+ continue;
206
+ }
207
+ scalar_t diff_val = top_diff[index] / top_count[index];
208
+ const scalar_t *offset_bottom_data = bottom_data + roi_batch_ind * channels * height * width;
209
+ scalar_t *offset_bottom_data_diff = bottom_data_diff + roi_batch_ind * channels * height * width;
210
+ int gw = floor((scalar_t)(pw)*group_size / pooled_width);
211
+ int gh = floor((scalar_t)(ph)*group_size / pooled_height);
212
+ gw = min(max(gw, 0), group_size - 1);
213
+ gh = min(max(gh, 0), group_size - 1);
214
+
215
+ for (int ih = 0; ih < sample_per_part; ih++)
216
+ {
217
+ for (int iw = 0; iw < sample_per_part; iw++)
218
+ {
219
+ scalar_t w = wstart + iw * sub_bin_size_w;
220
+ scalar_t h = hstart + ih * sub_bin_size_h;
221
+ // bilinear interpolation
222
+ if (w < -0.5 || w > width - 0.5 || h < -0.5 || h > height - 0.5)
223
+ {
224
+ continue;
225
+ }
226
+ w = min(max(w, 0.), width - 1.);
227
+ h = min(max(h, 0.), height - 1.);
228
+ int c = (ctop * group_size + gh) * group_size + gw;
229
+ // backward on feature
230
+ int x0 = floor(w);
231
+ int x1 = ceil(w);
232
+ int y0 = floor(h);
233
+ int y1 = ceil(h);
234
+ scalar_t dist_x = w - x0, dist_y = h - y0;
235
+ scalar_t q00 = (1 - dist_x) * (1 - dist_y);
236
+ scalar_t q01 = (1 - dist_x) * dist_y;
237
+ scalar_t q10 = dist_x * (1 - dist_y);
238
+ scalar_t q11 = dist_x * dist_y;
239
+ int bottom_index_base = c * height * width;
240
+ atomicAdd(offset_bottom_data_diff + bottom_index_base + y0 * width + x0, q00 * diff_val);
241
+ atomicAdd(offset_bottom_data_diff + bottom_index_base + y1 * width + x0, q01 * diff_val);
242
+ atomicAdd(offset_bottom_data_diff + bottom_index_base + y0 * width + x1, q10 * diff_val);
243
+ atomicAdd(offset_bottom_data_diff + bottom_index_base + y1 * width + x1, q11 * diff_val);
244
+
245
+ if (no_trans)
246
+ {
247
+ continue;
248
+ }
249
+ scalar_t U00 = offset_bottom_data[bottom_index_base + y0 * width + x0];
250
+ scalar_t U01 = offset_bottom_data[bottom_index_base + y1 * width + x0];
251
+ scalar_t U10 = offset_bottom_data[bottom_index_base + y0 * width + x1];
252
+ scalar_t U11 = offset_bottom_data[bottom_index_base + y1 * width + x1];
253
+ scalar_t diff_x = (U11 * dist_y + U10 * (1 - dist_y) - U01 * dist_y - U00 * (1 - dist_y)) * trans_std * diff_val;
254
+ diff_x *= roi_width;
255
+ scalar_t diff_y = (U11 * dist_x + U01 * (1 - dist_x) - U10 * dist_x - U00 * (1 - dist_x)) * trans_std * diff_val;
256
+ diff_y *= roi_height;
257
+
258
+ atomicAdd(bottom_trans_diff + (((n * num_classes + class_id) * 2) * part_size + part_h) * part_size + part_w, diff_x);
259
+ atomicAdd(bottom_trans_diff + (((n * num_classes + class_id) * 2 + 1) * part_size + part_h) * part_size + part_w, diff_y);
260
+ }
261
+ }
262
+ }
263
+ }
264
+
265
+ void DeformablePSROIPoolForward(const at::Tensor data,
266
+ const at::Tensor bbox,
267
+ const at::Tensor trans,
268
+ at::Tensor out,
269
+ at::Tensor top_count,
270
+ const int batch,
271
+ const int channels,
272
+ const int height,
273
+ const int width,
274
+ const int num_bbox,
275
+ const int channels_trans,
276
+ const int no_trans,
277
+ const float spatial_scale,
278
+ const int output_dim,
279
+ const int group_size,
280
+ const int pooled_size,
281
+ const int part_size,
282
+ const int sample_per_part,
283
+ const float trans_std)
284
+ {
285
+ const int pooled_height = pooled_size;
286
+ const int pooled_width = pooled_size;
287
+ const int count = num_bbox * output_dim * pooled_height * pooled_width;
288
+ const int num_classes = no_trans ? 1 : channels_trans / 2;
289
+ const int channels_each_class = no_trans ? output_dim : output_dim / num_classes;
290
+
291
+ AT_DISPATCH_FLOATING_TYPES_AND_HALF(
292
+ data.type(), "deformable_psroi_pool_forward", ([&] {
293
+ const scalar_t *bottom_data = data.data<scalar_t>();
294
+ const scalar_t *bottom_rois = bbox.data<scalar_t>();
295
+ const scalar_t *bottom_trans = no_trans ? NULL : trans.data<scalar_t>();
296
+ scalar_t *top_data = out.data<scalar_t>();
297
+ scalar_t *top_count_data = top_count.data<scalar_t>();
298
+
299
+ DeformablePSROIPoolForwardKernel<<<GET_BLOCKS(count), CUDA_NUM_THREADS>>>(
300
+ count, bottom_data, (scalar_t)spatial_scale, channels, height, width, pooled_height, pooled_width,
301
+ bottom_rois, bottom_trans, no_trans, (scalar_t)trans_std, sample_per_part, output_dim,
302
+ group_size, part_size, num_classes, channels_each_class, top_data, top_count_data);
303
+ }));
304
+
305
+ cudaError_t err = cudaGetLastError();
306
+ if (err != cudaSuccess)
307
+ {
308
+ printf("error in DeformablePSROIPoolForward: %s\n", cudaGetErrorString(err));
309
+ }
310
+ }
311
+
312
+ void DeformablePSROIPoolBackwardAcc(const at::Tensor out_grad,
313
+ const at::Tensor data,
314
+ const at::Tensor bbox,
315
+ const at::Tensor trans,
316
+ const at::Tensor top_count,
317
+ at::Tensor in_grad,
318
+ at::Tensor trans_grad,
319
+ const int batch,
320
+ const int channels,
321
+ const int height,
322
+ const int width,
323
+ const int num_bbox,
324
+ const int channels_trans,
325
+ const int no_trans,
326
+ const float spatial_scale,
327
+ const int output_dim,
328
+ const int group_size,
329
+ const int pooled_size,
330
+ const int part_size,
331
+ const int sample_per_part,
332
+ const float trans_std)
333
+ {
334
+ // LOG(INFO) << "DeformablePSROIPoolBackward";
335
+ const int num_rois = num_bbox;
336
+ const int pooled_height = pooled_size;
337
+ const int pooled_width = pooled_size;
338
+ const int count = num_bbox * output_dim * pooled_height * pooled_width;
339
+ const int num_classes = no_trans ? 1 : channels_trans / 2;
340
+ const int channels_each_class = no_trans ? output_dim : output_dim / num_classes;
341
+
342
+ AT_DISPATCH_FLOATING_TYPES_AND_HALF(
343
+ out_grad.type(), "deformable_psroi_pool_backward_acc", ([&] {
344
+ const scalar_t *top_diff = out_grad.data<scalar_t>();
345
+ const scalar_t *bottom_data = data.data<scalar_t>();
346
+ const scalar_t *bottom_rois = bbox.data<scalar_t>();
347
+ const scalar_t *bottom_trans = no_trans ? NULL : trans.data<scalar_t>();
348
+ scalar_t *bottom_data_diff = in_grad.data<scalar_t>();
349
+ scalar_t *bottom_trans_diff = no_trans ? NULL : trans_grad.data<scalar_t>();
350
+ const scalar_t *top_count_data = top_count.data<scalar_t>();
351
+
352
+ DeformablePSROIPoolBackwardAccKernel<<<GET_BLOCKS(count), CUDA_NUM_THREADS>>>(
353
+ count, top_diff, top_count_data, num_rois, (scalar_t)spatial_scale, channels, height, width,
354
+ pooled_height, pooled_width, output_dim, bottom_data_diff, bottom_trans_diff,
355
+ bottom_data, bottom_rois, bottom_trans, no_trans, (scalar_t)trans_std, sample_per_part,
356
+ group_size, part_size, num_classes, channels_each_class);
357
+ }));
358
+
359
+ cudaError_t err = cudaGetLastError();
360
+ if (err != cudaSuccess)
361
+ {
362
+ printf("error in DeformablePSROIPoolForward: %s\n", cudaGetErrorString(err));
363
+ }
364
+ }
IndicPhotoOCR/detection/textbpn/network/backbone/resnet.py ADDED
@@ -0,0 +1,336 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch.nn as nn
2
+ import math
3
+ import torch.utils.model_zoo as model_zoo
4
+ BatchNorm2d = nn.BatchNorm2d
5
+
6
+ __all__ = ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101',
7
+ 'resnet152']
8
+
9
+
10
+ model_urls = {
11
+ 'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
12
+ 'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
13
+ 'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
14
+ 'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
15
+ 'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
16
+ }
17
+
18
+
19
+ def constant_init(module, constant, bias=0):
20
+ nn.init.constant_(module.weight, constant)
21
+ if hasattr(module, 'bias'):
22
+ nn.init.constant_(module.bias, bias)
23
+
24
+
25
+ def conv3x3(in_planes, out_planes, stride=1):
26
+ """3x3 convolution with padding"""
27
+ return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
28
+ padding=1, bias=False)
29
+
30
+
31
+ class BasicBlock(nn.Module):
32
+ expansion = 1
33
+
34
+ def __init__(self, inplanes, planes, stride=1, downsample=None, dcn=None):
35
+ super(BasicBlock, self).__init__()
36
+ self.with_dcn = dcn is not None
37
+ self.conv1 = conv3x3(inplanes, planes, stride)
38
+ self.bn1 = BatchNorm2d(planes)
39
+ self.relu = nn.ReLU(inplace=True)
40
+ self.with_modulated_dcn = False
41
+ if self.with_dcn:
42
+ fallback_on_stride = dcn.get('fallback_on_stride', False)
43
+ self.with_modulated_dcn = dcn.get('modulated', False)
44
+ # self.conv2 = conv3x3(planes, planes)
45
+ if not self.with_dcn or fallback_on_stride:
46
+ self.conv2 = nn.Conv2d(planes, planes, kernel_size=3,
47
+ padding=1, bias=False)
48
+ else:
49
+ deformable_groups = dcn.get('deformable_groups', 1)
50
+ if not self.with_modulated_dcn:
51
+ from network.backbone.assets.dcn import DeformConv
52
+ conv_op = DeformConv
53
+ offset_channels = 18
54
+ else:
55
+ from network.backbone.assets.dcn import ModulatedDeformConv
56
+ conv_op = ModulatedDeformConv
57
+ offset_channels = 27
58
+ self.conv2_offset = nn.Conv2d(
59
+ planes,
60
+ deformable_groups * offset_channels,
61
+ kernel_size=3,
62
+ padding=1)
63
+ self.conv2 = conv_op(
64
+ planes,
65
+ planes,
66
+ kernel_size=3,
67
+ padding=1,
68
+ deformable_groups=deformable_groups,
69
+ bias=False)
70
+ self.bn2 = BatchNorm2d(planes)
71
+ self.downsample = downsample
72
+ self.stride = stride
73
+
74
+ def forward(self, x):
75
+ residual = x
76
+
77
+ out = self.conv1(x)
78
+ out = self.bn1(out)
79
+ out = self.relu(out)
80
+
81
+ # out = self.conv2(out)
82
+ if not self.with_dcn:
83
+ out = self.conv2(out)
84
+ elif self.with_modulated_dcn:
85
+ offset_mask = self.conv2_offset(out)
86
+ offset = offset_mask[:, :18, :, :]
87
+ mask = offset_mask[:, -9:, :, :].sigmoid()
88
+ out = self.conv2(out, offset, mask)
89
+ else:
90
+ offset = self.conv2_offset(out)
91
+ out = self.conv2(out, offset)
92
+ out = self.bn2(out)
93
+
94
+ if self.downsample is not None:
95
+ residual = self.downsample(x)
96
+
97
+ out += residual
98
+ out = self.relu(out)
99
+
100
+ return out
101
+
102
+
103
+ class Bottleneck(nn.Module):
104
+ expansion = 4
105
+
106
+ def __init__(self, inplanes, planes, stride=1, downsample=None, dcn=None):
107
+ super(Bottleneck, self).__init__()
108
+ self.with_dcn = dcn is not None
109
+ self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
110
+ self.bn1 = BatchNorm2d(planes)
111
+ fallback_on_stride = False
112
+ self.with_modulated_dcn = False
113
+ if self.with_dcn:
114
+ fallback_on_stride = dcn.get('fallback_on_stride', False)
115
+ self.with_modulated_dcn = dcn.get('modulated', False)
116
+ if not self.with_dcn or fallback_on_stride:
117
+ self.conv2 = nn.Conv2d(planes, planes, kernel_size=3,
118
+ stride=stride, padding=1, bias=False)
119
+ else:
120
+ deformable_groups = dcn.get('deformable_groups', 1)
121
+ if not self.with_modulated_dcn:
122
+ from network.backbone.assets.dcn import DeformConv
123
+ conv_op = DeformConv
124
+ offset_channels = 18
125
+ else:
126
+ from network.backbone.assets.dcn import ModulatedDeformConv
127
+ conv_op = ModulatedDeformConv
128
+ offset_channels = 27
129
+ self.conv2_offset = nn.Conv2d(
130
+ planes, deformable_groups * offset_channels,
131
+ kernel_size=3,
132
+ padding=1)
133
+ self.conv2 = conv_op(
134
+ planes, planes, kernel_size=3, padding=1, stride=stride,
135
+ deformable_groups=deformable_groups, bias=False)
136
+ self.bn2 = BatchNorm2d(planes)
137
+ self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
138
+ self.bn3 = BatchNorm2d(planes * 4)
139
+ self.relu = nn.ReLU(inplace=True)
140
+ self.downsample = downsample
141
+ self.stride = stride
142
+ self.dcn = dcn
143
+ self.with_dcn = dcn is not None
144
+
145
+ def forward(self, x):
146
+ residual = x
147
+
148
+ out = self.conv1(x)
149
+ out = self.bn1(out)
150
+ out = self.relu(out)
151
+
152
+ # out = self.conv2(out)
153
+ if not self.with_dcn:
154
+ out = self.conv2(out)
155
+ elif self.with_modulated_dcn:
156
+ offset_mask = self.conv2_offset(out)
157
+ offset = offset_mask[:, :18, :, :]
158
+ mask = offset_mask[:, -9:, :, :].sigmoid()
159
+ out = self.conv2(out, offset, mask)
160
+ else:
161
+ offset = self.conv2_offset(out)
162
+ out = self.conv2(out, offset)
163
+ out = self.bn2(out)
164
+ out = self.relu(out)
165
+
166
+ out = self.conv3(out)
167
+ out = self.bn3(out)
168
+
169
+ if self.downsample is not None:
170
+ residual = self.downsample(x)
171
+
172
+ out += residual
173
+ out = self.relu(out)
174
+
175
+ return out
176
+
177
+
178
+ class ResNet(nn.Module):
179
+ def __init__(self, block, layers, num_classes=1000,
180
+ dcn=None, stage_with_dcn=(False, False, False, False)):
181
+ self.dcn = dcn
182
+ self.stage_with_dcn = stage_with_dcn
183
+ self.inplanes = 64
184
+ super(ResNet, self).__init__()
185
+ self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
186
+ bias=False)
187
+ self.bn1 = BatchNorm2d(64)
188
+ self.relu = nn.ReLU(inplace=True)
189
+ self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
190
+ self.layer1 = self._make_layer(block, 64, layers[0])
191
+ self.layer2 = self._make_layer(
192
+ block, 128, layers[1], stride=2, dcn=dcn)
193
+ self.layer3 = self._make_layer(
194
+ block, 256, layers[2], stride=2, dcn=dcn)
195
+ self.layer4 = self._make_layer(
196
+ block, 512, layers[3], stride=2, dcn=dcn)
197
+ self.avgpool = nn.AvgPool2d(7, stride=1)
198
+ self.fc = nn.Linear(512 * block.expansion, num_classes)
199
+
200
+ self.smooth = nn.Conv2d(2048, 256, kernel_size=1, stride=1, padding=1)
201
+
202
+ for m in self.modules():
203
+ if isinstance(m, nn.Conv2d):
204
+ n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
205
+ m.weight.data.normal_(0, math.sqrt(2. / n))
206
+ elif isinstance(m, BatchNorm2d):
207
+ m.weight.data.fill_(1)
208
+ m.bias.data.zero_()
209
+ if self.dcn is not None:
210
+ for m in self.modules():
211
+ if isinstance(m, Bottleneck) or isinstance(m, BasicBlock):
212
+ if hasattr(m, 'conv2_offset'):
213
+ constant_init(m.conv2_offset, 0)
214
+
215
+ def _make_layer(self, block, planes, blocks, stride=1, dcn=None):
216
+ downsample = None
217
+ if stride != 1 or self.inplanes != planes * block.expansion:
218
+ downsample = nn.Sequential(
219
+ nn.Conv2d(self.inplanes, planes * block.expansion,
220
+ kernel_size=1, stride=stride, bias=False),
221
+ BatchNorm2d(planes * block.expansion),
222
+ )
223
+
224
+ layers = []
225
+ layers.append(block(self.inplanes, planes,
226
+ stride, downsample, dcn=dcn))
227
+ self.inplanes = planes * block.expansion
228
+ for i in range(1, blocks):
229
+ layers.append(block(self.inplanes, planes, dcn=dcn))
230
+
231
+ return nn.Sequential(*layers)
232
+
233
+ def forward(self, x):
234
+ x = self.conv1(x)
235
+ x = self.bn1(x)
236
+ x = self.relu(x)
237
+ x1 = self.maxpool(x)
238
+
239
+ x2 = self.layer1(x1)
240
+ x3 = self.layer2(x2)
241
+ x4 = self.layer3(x3)
242
+ x5 = self.layer4(x4)
243
+
244
+ return x1, x2, x3, x4, x5
245
+
246
+
247
+ def resnet18(pretrained=True, **kwargs):
248
+ """Constructs a ResNet-18 model.
249
+ Args:
250
+ pretrained (bool): If True, returns a model pre-trained on ImageNet
251
+ """
252
+ model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
253
+ if pretrained:
254
+ model.load_state_dict(model_zoo.load_url(
255
+ model_urls['resnet18']), strict=False)
256
+ return model
257
+
258
+ def deformable_resnet18(pretrained=True, **kwargs):
259
+ """Constructs a ResNet-18 model.
260
+ Args:
261
+ pretrained (bool): If True, returns a model pre-trained on ImageNet
262
+ """
263
+ model = ResNet(BasicBlock, [2, 2, 2, 2],
264
+ dcn=dict(modulated=True,
265
+ deformable_groups=1,
266
+ fallback_on_stride=False),
267
+ stage_with_dcn=[False, True, True, True], **kwargs)
268
+ if pretrained:
269
+ model.load_state_dict(model_zoo.load_url(
270
+ model_urls['resnet18']), strict=False)
271
+ return model
272
+
273
+
274
+ def resnet34(pretrained=True, **kwargs):
275
+ """Constructs a ResNet-34 model.
276
+ Args:
277
+ pretrained (bool): If True, returns a model pre-trained on ImageNet
278
+ """
279
+ model = ResNet(BasicBlock, [3, 4, 6, 3], **kwargs)
280
+ if pretrained:
281
+ model.load_state_dict(model_zoo.load_url(
282
+ model_urls['resnet34']), strict=False)
283
+ return model
284
+
285
+
286
+ def resnet50(pretrained=True, **kwargs):
287
+ """Constructs a ResNet-50 model.
288
+ Args:
289
+ pretrained (bool): If True, returns a model pre-trained on ImageNet
290
+ """
291
+ model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
292
+ if pretrained:
293
+ model.load_state_dict(model_zoo.load_url(
294
+ model_urls['resnet50']), strict=False)
295
+ return model
296
+
297
+
298
+ def deformable_resnet50(pretrained=True, **kwargs):
299
+ """Constructs a ResNet-50 model with deformable conv.
300
+ Args:
301
+ pretrained (bool): If True, returns a model pre-trained on ImageNet
302
+ """
303
+ model = ResNet(Bottleneck, [3, 4, 6, 3],
304
+ dcn=dict(modulated=True,
305
+ deformable_groups=1,
306
+ fallback_on_stride=False),
307
+ stage_with_dcn=[False, True, True, True],
308
+ **kwargs)
309
+ if pretrained:
310
+ model.load_state_dict(model_zoo.load_url(
311
+ model_urls['resnet50']), strict=False)
312
+ return model
313
+
314
+
315
+ def resnet101(pretrained=True, **kwargs):
316
+ """Constructs a ResNet-101 model.
317
+ Args:
318
+ pretrained (bool): If True, returns a model pre-trained on ImageNet
319
+ """
320
+ model = ResNet(Bottleneck, [3, 4, 23, 3], **kwargs)
321
+ if pretrained:
322
+ model.load_state_dict(model_zoo.load_url(
323
+ model_urls['resnet101']), strict=False)
324
+ return model
325
+
326
+
327
+ def resnet152(pretrained=True, **kwargs):
328
+ """Constructs a ResNet-152 model.
329
+ Args:
330
+ pretrained (bool): If True, returns a model pre-trained on ImageNet
331
+ """
332
+ model = ResNet(Bottleneck, [3, 8, 36, 3], **kwargs)
333
+ if pretrained:
334
+ model.load_state_dict(model_zoo.load_url(
335
+ model_urls['resnet152']), strict=False)
336
+ return model
IndicPhotoOCR/detection/textbpn/network/backbone/vgg.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch.nn as nn
2
+ import torch.utils.model_zoo as model_zoo
3
+ import torchvision.models as models
4
+
5
+ model_urls = {
6
+ 'vgg11': 'https://download.pytorch.org/models/vgg11-bbd30ac9.pth',
7
+ 'vgg16': 'https://download.pytorch.org/models/vgg16-397923af.pth',
8
+ 'vgg19': 'https://download.pytorch.org/models/vgg19-dcbb9e9d.pth',
9
+ 'vgg11_bn': 'https://download.pytorch.org/models/vgg11_bn-6002323d.pth',
10
+ 'vgg16_bn': 'https://download.pytorch.org/models/vgg16_bn-6c64b313.pth',
11
+ 'vgg19_bn': 'https://download.pytorch.org/models/vgg19_bn-c79401a0.pth',
12
+ }
13
+
14
+
15
+ class VggNet(nn.Module):
16
+ def __init__(self, name="vgg16", pretrain=True):
17
+ super().__init__()
18
+ if name == "vgg16":
19
+ base_net = models.vgg16(pretrained=False)
20
+ elif name == "vgg16_bn":
21
+ base_net = models.vgg16_bn(pretrained=False)
22
+ else:
23
+ print(" base model is not support !")
24
+ if pretrain:
25
+ print("load the {} weight from ./cache".format(name))
26
+ base_net.load_state_dict(model_zoo.load_url(model_urls[name], model_dir="./cache"))
27
+
28
+ if name == "vgg16":
29
+ self.stage1 = nn.Sequential(*[base_net.features[layer] for layer in range(0, 5)])
30
+ self.stage2 = nn.Sequential(*[base_net.features[layer] for layer in range(5, 10)])
31
+ self.stage3 = nn.Sequential(*[base_net.features[layer] for layer in range(10, 17)])
32
+ self.stage4 = nn.Sequential(*[base_net.features[layer] for layer in range(17, 24)])
33
+ self.stage5 = nn.Sequential(*[base_net.features[layer] for layer in range(24, 31)])
34
+ elif name == "vgg16_bn":
35
+ self.stage1 = nn.Sequential(*[base_net.features[layer] for layer in range(0, 7)])
36
+ self.stage2 = nn.Sequential(*[base_net.features[layer] for layer in range(7, 14)])
37
+ self.stage3 = nn.Sequential(*[base_net.features[layer] for layer in range(14, 24)])
38
+ self.stage4 = nn.Sequential(*[base_net.features[layer] for layer in range(24, 34)])
39
+ self.stage5 = nn.Sequential(*[base_net.features[layer] for layer in range(34, 44)])
40
+
41
+ def forward(self, x):
42
+ C1 = self.stage1(x)
43
+ C2 = self.stage2(C1)
44
+ C3 = self.stage3(C2)
45
+ C4 = self.stage4(C3)
46
+ C5 = self.stage5(C4)
47
+
48
+ return C1, C2, C3, C4, C5
49
+
50
+
51
+ if __name__ == '__main__':
52
+ import torch
53
+ input = torch.randn((4, 3, 512, 512))
54
+ net = VggNet()
55
+ C1, C2, C3, C4, C5 = net(input)
56
+ print(C1.size())
57
+ print(C2.size())
58
+ print(C3.size())
59
+ print(C4.size())
60
+ print(C5.size())
IndicPhotoOCR/detection/textbpn/network/layers/Adaptive_Deformation.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ###################################################################
2
+ # File Name: AdaptiveDeformation.py
3
+ # Author: S.X.Zhang
4
+ ###################################################################
5
+
6
+ from __future__ import print_function
7
+ from __future__ import division
8
+ from __future__ import absolute_import
9
+
10
+ import torch
11
+ import torch.nn as nn
12
+ import torch.nn.functional as F
13
+ from torch.nn import init
14
+
15
+
16
+ class MeanAggregator(nn.Module):
17
+ def __init__(self):
18
+ super(MeanAggregator, self).__init__()
19
+
20
+ def forward(self, features, A):
21
+ x = torch.bmm(A, features)
22
+ return x
23
+
24
+
25
+ class GraphConv(nn.Module):
26
+ def __init__(self, in_dim, out_dim, agg):
27
+ super(GraphConv, self).__init__()
28
+ self.in_dim = in_dim
29
+ self.out_dim = out_dim
30
+ self.weight = nn.Parameter(torch.FloatTensor(in_dim * 2, out_dim))
31
+ self.bias = nn.Parameter(torch.FloatTensor(out_dim))
32
+ init.xavier_uniform_(self.weight)
33
+ init.constant_(self.bias, 0)
34
+ self.agg = agg()
35
+
36
+ def forward(self, features, A):
37
+ b, n, d = features.shape
38
+ assert (d == self.in_dim)
39
+ agg_feats = self.agg(features, A)
40
+ cat_feats = torch.cat([features, agg_feats], dim=2)
41
+ out = torch.einsum('bnd,df->bnf', (cat_feats, self.weight))
42
+ out = F.relu(out + self.bias)
43
+ return out
44
+
45
+
46
+ class AdaptiveDeformation(nn.Module):
47
+ def __init__(self, input, state_dim):
48
+ super(AdaptiveDeformation, self).__init__()
49
+ self.bn0 = nn.BatchNorm1d(input, affine=False)
50
+ self.conv1 = nn.Conv1d(input, state_dim, 1)
51
+ self.rnn = nn.LSTM(input, state_dim, 1, bidirectional=True)
52
+ self.gconv1 = GraphConv(input, 256, MeanAggregator)
53
+ self.gconv2 = GraphConv(256, 1024, MeanAggregator)
54
+ self.gconv3 = GraphConv(1024, 512, MeanAggregator)
55
+ self.gconv4 = GraphConv(512, state_dim, MeanAggregator)
56
+
57
+ self.prediction = nn.Sequential(
58
+ nn.Conv1d(4*state_dim, 128, 1),
59
+ nn.ReLU(inplace=True),
60
+ nn.Dropout(0.1),
61
+ nn.Conv1d(128, 64, 1),
62
+ nn.ReLU(inplace=True),
63
+ nn.Dropout(0.1),
64
+ nn.Conv1d(64, 2, 1))
65
+
66
+ def forward(self, x, A):
67
+ x = self.bn0(x)
68
+
69
+ # # rnn block
70
+ yl = x.permute(2, 0, 1)
71
+ yl, _ = self.rnn(yl)
72
+ yl = yl.permute(1, 2, 0)
73
+
74
+ # # gcn block
75
+ yg = x.permute(0, 2, 1)
76
+ b, n, c = yg.shape
77
+ A = A.expand(b, n, n)
78
+ yg = self.gconv1(yg, A)
79
+ yg = self.gconv2(yg, A)
80
+ yg = self.gconv3(yg, A)
81
+ yg = self.gconv4(yg, A)
82
+ yg = yg.permute(0, 2, 1)
83
+
84
+ # res block
85
+ x = torch.cat([yl, yg, self.conv1(x)], dim=1)
86
+ pred = self.prediction(x)
87
+
88
+ return pred
IndicPhotoOCR/detection/textbpn/network/layers/CircConv.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch.nn as nn
2
+ import torch
3
+
4
+
5
+ class CircConv(nn.Module):
6
+ def __init__(self, state_dim, out_state_dim=None, n_adj=4):
7
+ super(CircConv, self).__init__()
8
+
9
+ self.n_adj = n_adj
10
+ out_state_dim = state_dim if out_state_dim is None else out_state_dim
11
+ self.fc = nn.Conv1d(state_dim, out_state_dim, kernel_size=self.n_adj*2+1)
12
+
13
+ def forward(self, input, adj):
14
+ input = torch.cat([input[..., -self.n_adj:], input, input[..., :self.n_adj]], dim=2)
15
+ return self.fc(input)
16
+
17
+
18
+ class DilatedCircConv(nn.Module):
19
+ def __init__(self, state_dim, out_state_dim=None, n_adj=4, dilation=1):
20
+ super(DilatedCircConv, self).__init__()
21
+
22
+ self.n_adj = n_adj
23
+ self.dilation = dilation
24
+ out_state_dim = state_dim if out_state_dim is None else out_state_dim
25
+ self.fc = nn.Conv1d(state_dim, out_state_dim, kernel_size=self.n_adj*2+1, dilation=self.dilation)
26
+
27
+ def forward(self, input, adj):
28
+ if self.n_adj != 0:
29
+ input = torch.cat([input[..., -self.n_adj*self.dilation:], input, input[..., :self.n_adj*self.dilation]], dim=2)
30
+ return self.fc(input)
31
+
32
+
33
+ _conv_factory = {
34
+ 'grid': CircConv,
35
+ 'dgrid': DilatedCircConv
36
+ }
37
+
38
+
39
+ class BasicBlock(nn.Module):
40
+ def __init__(self, state_dim, out_state_dim, conv_type, n_adj=4, dilation=1):
41
+ super(BasicBlock, self).__init__()
42
+
43
+ self.conv = _conv_factory[conv_type](state_dim, out_state_dim, n_adj, dilation)
44
+ self.relu = nn.ReLU(inplace=True)
45
+ self.norm = nn.BatchNorm1d(out_state_dim)
46
+
47
+ def forward(self, x, adj=None):
48
+ x = self.conv(x, adj)
49
+ x = self.relu(x)
50
+ x = self.norm(x)
51
+ return x
52
+
53
+
54
+ class DeepSnake(nn.Module):
55
+ def __init__(self, state_dim, feature_dim, conv_type='dgrid'):
56
+ super(DeepSnake, self).__init__()
57
+
58
+ self.head = BasicBlock(feature_dim, state_dim, conv_type)
59
+
60
+ self.res_layer_num = 7
61
+ dilation = [1, 1, 1, 2, 2, 4, 4]
62
+ for i in range(self.res_layer_num):
63
+ conv = BasicBlock(state_dim, state_dim, conv_type, n_adj=4, dilation=dilation[i])
64
+ self.__setattr__('res'+str(i), conv)
65
+
66
+ fusion_state_dim = 256
67
+ self.fusion = nn.Conv1d(state_dim * (self.res_layer_num + 1), fusion_state_dim, 1)
68
+ self.prediction = nn.Sequential(
69
+ nn.Conv1d(state_dim * (self.res_layer_num + 1) + fusion_state_dim, 256, 1),
70
+ nn.ReLU(inplace=True),
71
+ nn.Conv1d(256, 64, 1),
72
+ nn.ReLU(inplace=True),
73
+ nn.Conv1d(64, 2, 1)
74
+ )
75
+
76
+ def forward(self, x, adj):
77
+ states = []
78
+
79
+ x = self.head(x, adj)
80
+ states.append(x)
81
+ for i in range(self.res_layer_num):
82
+ x = self.__getattr__('res'+str(i))(x, adj) + x
83
+ states.append(x)
84
+
85
+ state = torch.cat(states, dim=1)
86
+ global_state = torch.max(self.fusion(state), dim=2, keepdim=True)[0]
87
+ global_state = global_state.expand(global_state.size(0), global_state.size(1), state.size(2))
88
+ state = torch.cat([global_state, state], dim=1)
89
+ x = self.prediction(state)
90
+
91
+ return x
IndicPhotoOCR/detection/textbpn/network/layers/GCN.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ###################################################################
2
+ # File Name: GCN.py
3
+ # Author: S.X.Zhang
4
+ ###################################################################
5
+
6
+ from __future__ import print_function
7
+ from __future__ import division
8
+ from __future__ import absolute_import
9
+
10
+ import torch
11
+ import torch.nn as nn
12
+ import torch.nn.functional as F
13
+ from torch.nn import init
14
+
15
+
16
+ class MeanAggregator(nn.Module):
17
+ def __init__(self):
18
+ super(MeanAggregator, self).__init__()
19
+
20
+ def forward(self, features, A):
21
+ x = torch.bmm(A, features)
22
+ return x
23
+
24
+
25
+ class GraphConv(nn.Module):
26
+ def __init__(self, in_dim, out_dim, agg):
27
+ super(GraphConv, self).__init__()
28
+ self.in_dim = in_dim
29
+ self.out_dim = out_dim
30
+ self.weight = nn.Parameter(torch.FloatTensor(in_dim * 2, out_dim))
31
+ self.bias = nn.Parameter(torch.FloatTensor(out_dim))
32
+ init.xavier_uniform_(self.weight)
33
+ init.constant_(self.bias, 0)
34
+ self.agg = agg()
35
+
36
+ def forward(self, features, A):
37
+ b, n, d = features.shape
38
+ assert (d == self.in_dim)
39
+ agg_feats = self.agg(features, A)
40
+ cat_feats = torch.cat([features, agg_feats], dim=2)
41
+ out = torch.einsum('bnd,df->bnf', (cat_feats, self.weight))
42
+ out = F.relu(out + self.bias)
43
+ return out
44
+
45
+
46
+ class GCN(nn.Module):
47
+ def __init__(self, in_dim, out_dim):
48
+ super(GCN, self).__init__()
49
+ self.bn0 = nn.BatchNorm1d(in_dim, affine=False)
50
+
51
+ self.conv1 = GraphConv(in_dim, 256, MeanAggregator)
52
+ self.conv2 = GraphConv(256, 1024, MeanAggregator)
53
+ self.conv3 = GraphConv(1024, 512, MeanAggregator)
54
+ self.conv4 = GraphConv(512, out_dim, MeanAggregator)
55
+
56
+ self.prediction = nn.Sequential(
57
+ nn.Conv1d(out_dim, 128, 1),
58
+ nn.ReLU(inplace=True),
59
+ nn.Conv1d(128, 64, 1),
60
+ nn.ReLU(inplace=True),
61
+ nn.Conv1d(64, 2, 1))
62
+
63
+ def forward(self, x, A):
64
+ x = self.bn0(x)
65
+ x = x.permute(0, 2, 1)
66
+ b, n, c = x.shape
67
+ A = A.expand(b, n, n)
68
+
69
+ x = self.conv1(x, A)
70
+ x = self.conv2(x, A)
71
+ x = self.conv3(x, A)
72
+ x = self.conv4(x, A)
73
+
74
+ x = x.permute(0, 2, 1)
75
+ pred = self.prediction(x)
76
+
77
+ return pred
IndicPhotoOCR/detection/textbpn/network/layers/GraphConv.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ import torch
4
+ from torch.nn.parameter import Parameter
5
+ from torch.nn.modules.module import Module
6
+ from torch.nn import init
7
+
8
+
9
+ class GraphConvolution(Module):
10
+ """
11
+ Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
12
+ """
13
+
14
+ def __init__(self, in_features, out_features, bias=True):
15
+ super(GraphConvolution, self).__init__()
16
+ self.in_features = in_features
17
+ self.out_features = out_features
18
+ self.weight = Parameter(torch.FloatTensor(in_features, out_features))
19
+ init.xavier_uniform_(self.weight)
20
+ if bias:
21
+ self.bias = Parameter(torch.FloatTensor(out_features))
22
+ init.constant_(self.bias, 0)
23
+ else:
24
+ self.register_parameter('bias', None)
25
+
26
+ self.reset_parameters()
27
+
28
+ def reset_parameters(self):
29
+ stdv = 1. / math.sqrt(self.weight.size(1))
30
+ self.weight.data.uniform_(-stdv, stdv)
31
+ if self.bias is not None:
32
+ self.bias.data.uniform_(-stdv, stdv)
33
+
34
+ def forward(self, input, adj):
35
+ support = torch.mm(input, self.weight)
36
+ output = torch.spmm(adj, support)
37
+ if self.bias is not None:
38
+ return output + self.bias
39
+ else:
40
+ return output
41
+
42
+ def __repr__(self):
43
+ return self.__class__.__name__ + ' (' \
44
+ + str(self.in_features) + ' -> ' \
45
+ + str(self.out_features) + ')'
IndicPhotoOCR/detection/textbpn/network/layers/RNN.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ###################################################################
2
+ # File Name: RNN.py
3
+ # Author: S.X.Zhang
4
+ ###################################################################
5
+
6
+ from __future__ import print_function
7
+ from __future__ import division
8
+ from __future__ import absolute_import
9
+
10
+ import torch
11
+ import torch.nn as nn
12
+ import torch.nn.functional as F
13
+ from torch.nn import init
14
+
15
+
16
+ class RNN(nn.Module):
17
+ def __init__(self, input, state_dim):
18
+ super(RNN, self).__init__()
19
+ self.bn0 = nn.BatchNorm1d(input, affine=False)
20
+ self.rnn = nn.LSTM(input, state_dim, 1, dropout=0.1, bidirectional=True)
21
+ self.prediction = nn.Sequential(
22
+ nn.Conv1d(state_dim*2, 128, 1),
23
+ nn.ReLU(inplace=True),
24
+ nn.Conv1d(128, 64, 1),
25
+ nn.ReLU(inplace=True),
26
+ nn.Conv1d(64, 2, 1))
27
+
28
+ def forward(self, x, adj):
29
+ x = self.bn0(x)
30
+ x = x.permute(2, 0, 1)
31
+ x, _ = self.rnn(x)
32
+ x = x.permute(1, 2, 0)
33
+ pred = self.prediction(x)
34
+
35
+ return pred
IndicPhotoOCR/detection/textbpn/network/layers/Transformer.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ###################################################################
2
+ # File Name: GCN.py
3
+ # Author: S.X.Zhang
4
+ ###################################################################
5
+ import torch
6
+ from torch import nn, Tensor
7
+ import numpy as np
8
+ from IndicPhotoOCR.detection.textbpn.cfglib.config import config as cfg
9
+
10
+
11
+ class Positional_encoding(nn.Module):
12
+ def __init__(self, PE_size, n_position=256):
13
+ super(Positional_encoding, self).__init__()
14
+ self.PE_size = PE_size
15
+ self.n_position = n_position
16
+ self.register_buffer('pos_table', self.get_encoding_table(n_position, PE_size))
17
+
18
+ def get_encoding_table(self, n_position, PE_size):
19
+ position_table = np.array(
20
+ [[pos / np.power(10000, 2. * i / self.PE_size) for i in range(self.PE_size)] for pos in range(n_position)])
21
+ position_table[:, 0::2] = np.sin(position_table[:, 0::2])
22
+ position_table[:, 1::2] = np.cos(position_table[:, 1::2])
23
+ return torch.FloatTensor(position_table).unsqueeze(0)
24
+
25
+ def forward(self, inputs):
26
+ return inputs + self.pos_table[:, :inputs.size(1), :].clone().detach()
27
+
28
+
29
+ class MultiHeadAttention(nn.Module):
30
+ def __init__(self, num_heads, embed_dim, dropout=0.1, if_resi=True):
31
+ super(MultiHeadAttention, self).__init__()
32
+ self.layer_norm = nn.LayerNorm(embed_dim)
33
+ self.MultiheadAttention = nn.MultiheadAttention(embed_dim, num_heads, dropout=dropout)
34
+ self.Q_proj = nn.Sequential(nn.Linear(embed_dim, embed_dim), nn.ReLU())
35
+ self.K_proj = nn.Sequential(nn.Linear(embed_dim, embed_dim), nn.ReLU())
36
+ self.V_proj = nn.Sequential(nn.Linear(embed_dim, embed_dim), nn.ReLU())
37
+ self.if_resi = if_resi
38
+
39
+ def forward(self, inputs):
40
+ query = self.layer_norm(inputs)
41
+ q = self.Q_proj(query)
42
+ k = self.K_proj(query)
43
+ v = self.V_proj(query)
44
+ attn_output, attn_output_weights = self.MultiheadAttention(q, k, v)
45
+ if self.if_resi:
46
+ attn_output += inputs
47
+ else:
48
+ attn_output = attn_output
49
+
50
+ return attn_output
51
+
52
+
53
+ class FeedForward(nn.Module):
54
+ def __init__(self, in_channel, FFN_channel, if_resi=True):
55
+ super(FeedForward, self).__init__()
56
+ """
57
+ 1024 2048
58
+ """
59
+ output_channel = (FFN_channel, in_channel)
60
+ self.fc1 = nn.Sequential(nn.Linear(in_channel, output_channel[0]), nn.ReLU())
61
+ self.fc2 = nn.Linear(output_channel[0], output_channel[1])
62
+ self.layer_norm = nn.LayerNorm(in_channel)
63
+ self.if_resi = if_resi
64
+
65
+ def forward(self, inputs):
66
+ outputs = self.layer_norm(inputs)
67
+ outputs = self.fc1(outputs)
68
+ outputs = self.fc2(outputs)
69
+ if self.if_resi:
70
+ outputs += inputs
71
+ else:
72
+ outputs = outputs
73
+ return outputs
74
+
75
+
76
+ class TransformerLayer(nn.Module):
77
+ def __init__(self, out_dim, in_dim, num_heads, attention_size,
78
+ dim_feedforward=1024, drop_rate=0.1, if_resi=True, block_nums=3):
79
+ super(TransformerLayer, self).__init__()
80
+ self.block_nums = block_nums
81
+ self.if_resi = if_resi
82
+ self.linear = nn.Linear(in_dim, attention_size)
83
+ for i in range(self.block_nums):
84
+ self.__setattr__('MHA_self_%d' % i, MultiHeadAttention(num_heads, attention_size,
85
+ dropout=drop_rate, if_resi=if_resi))
86
+ self.__setattr__('FFN_%d' % i, FeedForward(out_dim, dim_feedforward, if_resi=if_resi))
87
+
88
+ def forward(self, query):
89
+ inputs = self.linear(query)
90
+ # outputs = inputs
91
+ for i in range(self.block_nums):
92
+ outputs = self.__getattr__('MHA_self_%d' % i)(inputs)
93
+ outputs = self.__getattr__('FFN_%d' % i)(outputs)
94
+ if self.if_resi:
95
+ inputs = inputs+outputs
96
+ else:
97
+ inputs = outputs
98
+ # outputs = inputs
99
+ return inputs
100
+
101
+
102
+ class Transformer(nn.Module):
103
+
104
+ def __init__(self, in_dim, out_dim, num_heads=8,
105
+ dim_feedforward=1024, drop_rate=0.1, if_resi=False, block_nums=3):
106
+ super().__init__()
107
+
108
+ self.bn0 = nn.BatchNorm1d(in_dim, affine=False)
109
+ self.conv1 = nn.Conv1d(in_dim, out_dim, 1, dilation=1)
110
+
111
+ # self.pos_embedding = Positional_encoding(in_dim)
112
+ self.transformer = TransformerLayer(out_dim, in_dim, num_heads, attention_size=out_dim,
113
+ dim_feedforward=dim_feedforward, drop_rate=drop_rate,
114
+ if_resi=if_resi, block_nums=block_nums)
115
+
116
+ self.prediction = nn.Sequential(
117
+ nn.Conv1d(2*out_dim, 128, 1),
118
+ nn.ReLU(inplace=True),
119
+ nn.Dropout(0.1),
120
+ nn.Conv1d(128, 64, 1),
121
+ nn.ReLU(inplace=True),
122
+ # nn.Dropout(0.1),
123
+ nn.Conv1d(64, 2, 1))
124
+
125
+ def forward(self, x, adj):
126
+ x = self.bn0(x)
127
+
128
+ x1 = x.permute(0, 2, 1)
129
+ # x1 = self.pos_embedding(x1)
130
+ x1 = self.transformer(x1)
131
+ x1 = x1.permute(0, 2, 1)
132
+
133
+ x = torch.cat([x1, self.conv1(x)], dim=1)
134
+ # x = x1+self.conv1(x)
135
+ pred = self.prediction(x)
136
+
137
+ return pred
138
+
139
+
140
+
IndicPhotoOCR/detection/textbpn/network/layers/Transformer_old.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ###################################################################
2
+ # File Name: GCN.py
3
+ # Author: S.X.Zhang
4
+ ###################################################################
5
+
6
+ import torch
7
+ import torch.nn.functional as F
8
+ from torch import nn, Tensor
9
+ from torch.autograd import Variable
10
+ import numpy as np
11
+ from cfglib.config import config as cfg
12
+
13
+
14
+ class Positional_encoding(nn.Module):
15
+ def __init__(self, PE_size, n_position=200):
16
+ super(Positional_encoding, self).__init__()
17
+ self.PE_size = PE_size
18
+ self.n_position = n_position
19
+ self.register_buffer('pos_table', self.get_encoding_table(n_position, PE_size))
20
+
21
+ def get_encoding_table(self, n_position, PE_size):
22
+ position_table = np.array(
23
+ [[pos / np.power(10000, 2. * i / self.PE_size) for i in range(self.PE_size)] for pos in range(n_position)])
24
+ position_table[:, 0::2] = np.sin(position_table[:, 0::2])
25
+ position_table[:, 1::2] = np.cos(position_table[:, 1::2])
26
+ return torch.FloatTensor(position_table).unsqueeze(0)
27
+
28
+ def forward(self, inputs):
29
+ return inputs + self.pos_table[:, :inputs.size(1), :].clone().detach()
30
+
31
+
32
+ class MultiHeadAttention(nn.Module):
33
+ def __init__(self, num_heads, embedding_size, attention_size,
34
+ drop_rate, future_blind=True, query_mask=False, if_resi=True):
35
+ super(MultiHeadAttention, self).__init__()
36
+ self.num_heads = num_heads
37
+ self.embedding_size = embedding_size
38
+ self.attention_size = attention_size
39
+ self.drop_rate = drop_rate
40
+ self.future_blind = future_blind
41
+
42
+ self.Q_proj = nn.Sequential(nn.Linear(self.embedding_size, self.attention_size), nn.ReLU())
43
+ self.K_proj = nn.Sequential(nn.Linear(self.embedding_size, self.attention_size), nn.ReLU())
44
+ self.V_proj = nn.Sequential(nn.Linear(self.embedding_size, self.attention_size), nn.ReLU())
45
+
46
+ self.drop_out = nn.Dropout(p=self.drop_rate)
47
+ self.layer_norm = nn.LayerNorm(self.attention_size)
48
+ self.if_resi = if_resi
49
+
50
+ def forward(self, query, key, value):
51
+ q = self.Q_proj(query)
52
+ k = self.K_proj(key)
53
+ v = self.V_proj(value)
54
+
55
+ q_ = torch.cat(torch.chunk(q, self.num_heads, dim=2), dim=0)
56
+ k_ = torch.cat(torch.chunk(k, self.num_heads, dim=2), dim=0)
57
+ v_ = torch.cat(torch.chunk(v, self.num_heads, dim=2), dim=0)
58
+
59
+ outputs = torch.bmm(q_, k_.permute(0, 2, 1))
60
+ outputs = outputs / (k_.size()[-1] ** 0.5)
61
+
62
+ # key mask
63
+
64
+ # future mask
65
+ if self.future_blind:
66
+ diag_vals = torch.ones_like(outputs[0, :, :]).to(cfg.device)
67
+ tril = torch.tril(diag_vals, diagonal=0)
68
+ masks = Variable(torch.unsqueeze(tril, 0).repeat(outputs.size()[0], 1, 1)) # (h*N,T_q,T_k)
69
+ padding = Variable(torch.ones_like(masks).to(cfg.device) * (-2 ** 32 + 1))
70
+ condition = masks.eq(0)
71
+ outputs = torch.where(condition, padding, outputs)
72
+
73
+ outputs = F.softmax(outputs, dim=-1)
74
+ # if self.future_blind==True:a
75
+ # print(outputs[0])
76
+ outputs = self.drop_out(outputs)
77
+
78
+ outputs = torch.bmm(outputs, v_)
79
+ outputs = torch.cat(torch.chunk(outputs, self.num_heads, dim=0), dim=2) # N,T_q,C
80
+
81
+ if self.if_resi:
82
+ # outputs += query
83
+ outputs += q
84
+ else:
85
+ outputs = outputs
86
+ outputs = self.layer_norm(outputs)
87
+
88
+ return outputs
89
+
90
+
91
+ class FeedForward(nn.Module):
92
+ def __init__(self, in_channel, FFN_channel, if_resi=True):
93
+ super(FeedForward, self).__init__()
94
+ """
95
+ 1024 2048
96
+ """
97
+ output_channel = (FFN_channel, in_channel)
98
+ self.fc1 = nn.Sequential(nn.Linear(in_channel, output_channel[0]), nn.ReLU())
99
+ self.fc2 = nn.Linear(output_channel[0], output_channel[1])
100
+ self.layer_norm = nn.LayerNorm(in_channel)
101
+ self.if_resi = if_resi
102
+
103
+ def forward(self, inputs):
104
+ outputs = self.fc1(inputs)
105
+ outputs = self.fc2(outputs)
106
+ if self.if_resi:
107
+ outputs += inputs
108
+ else:
109
+ outputs = outputs
110
+ outputs = self.layer_norm(outputs)
111
+ return outputs
112
+
113
+
114
+ class TransformerLayer(nn.Module):
115
+ def __init__(self, out_dim, num_heads, embedding_size, attention_size,
116
+ dim_feedforward=1024, drop_rate=0.1, if_resi=True, block_nums=3):
117
+ super(TransformerLayer, self).__init__()
118
+ self.block_nums = block_nums
119
+ self.if_resi = if_resi
120
+ for i in range(self.block_nums):
121
+ self.__setattr__('MHA_self_%d' % i, MultiHeadAttention(num_heads, embedding_size, attention_size,
122
+ drop_rate, future_blind=False, if_resi=if_resi))
123
+ self.__setattr__('FFN_%d' % i, FeedForward(out_dim, dim_feedforward, if_resi=if_resi))
124
+
125
+ def forward(self, query):
126
+ outputs = None
127
+ for i in range(self.block_nums):
128
+ outputs = self.__getattr__('MHA_self_%d' % i)(query, query, query)
129
+ outputs = self.__getattr__('FFN_%d' % i)(outputs)
130
+ return outputs
131
+
132
+
133
+ class Transformer(nn.Module):
134
+
135
+ def __init__(self, in_dim, out_dim, num_heads=8,
136
+ dim_feedforward=1024, drop_rate=0.1, if_resi=False, block_nums=3):
137
+ super().__init__()
138
+
139
+ self.bn0 = nn.BatchNorm1d(in_dim, affine=False)
140
+ self.conv1 = nn.Conv1d(in_dim, out_dim, 1, dilation=1)
141
+
142
+ embed_dim = in_dim
143
+ # self.pos_embedding = Positional_encoding(embed_dim)
144
+ self.transformer = TransformerLayer(out_dim, num_heads, embedding_size=embed_dim,
145
+ attention_size=out_dim, dim_feedforward=dim_feedforward,
146
+ drop_rate=drop_rate, if_resi=if_resi, block_nums=block_nums)
147
+
148
+ self.prediction = nn.Sequential(
149
+ nn.Conv1d(out_dim*2, 128, 1),
150
+ nn.ReLU(inplace=True),
151
+ nn.Dropout(0.1),
152
+ nn.Conv1d(128, 64, 1),
153
+ nn.ReLU(inplace=True),
154
+ # nn.Dropout(0.1),
155
+ nn.Conv1d(64, 2, 1))
156
+
157
+ def forward(self, x, adj):
158
+ x = self.bn0(x)
159
+
160
+ x1 = x.permute(0, 2, 1)
161
+ x1 = self.transformer(x1)
162
+ x1 = x1.permute(0, 2, 1)
163
+
164
+ x = torch.cat([x1, self.conv1(x)], dim=1)
165
+ # x = x1+self.conv1(x)
166
+ pred = self.prediction(x)
167
+
168
+ return pred
169
+
170
+
171
+
IndicPhotoOCR/detection/textbpn/network/layers/__init__.py ADDED
File without changes
IndicPhotoOCR/detection/textbpn/network/layers/gcn_utils.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ __author__ = "S.X.Zhang"
3
+ import torch
4
+ import numpy as np
5
+ import cv2
6
+ import torch.nn as nn
7
+ from torch.autograd import Variable
8
+
9
+
10
+ def normalize_adj(A, type="AD"):
11
+ if type == "DAD":
12
+ A = A + np.eye(A.shape[0]) # A=A+I
13
+ d = np.sum(A, axis=0)
14
+ d_inv = np.power(d, -0.5).flatten()
15
+ d_inv[np.isinf(d_inv)] = 0.0
16
+ d_inv = np.diag(d_inv)
17
+ G = A.dot(d_inv).transpose().dot(d_inv) # L = D^-1/2 A D^-1/2
18
+ G = torch.from_numpy(G)
19
+ elif type == "AD":
20
+ A = A + np.eye(A.shape[0]) # A=A+I
21
+ A = torch.from_numpy(A)
22
+ D = A.sum(1, keepdim=True)
23
+ G = A.div(D) # L= A/D
24
+ else:
25
+ A = A + np.eye(A.shape[0]) # A=A+I
26
+ D = A.sum(1, keepdim=True)
27
+ D = np.diag(D)
28
+ G = torch.from_numpy(D - A) # L = D-A
29
+ return G
30
+
31
+
32
+ def np_to_variable(x, is_cuda=True, dtype=torch.FloatTensor):
33
+ v = Variable(torch.from_numpy(x).type(dtype))
34
+ if is_cuda:
35
+ v = v.cuda()
36
+ return v
37
+
38
+
39
+ def set_trainable(model, requires_grad):
40
+ for param in model.parameters():
41
+ param.requires_grad = requires_grad
42
+
43
+
44
+ def weights_normal_init(model, dev=0.01):
45
+ if isinstance(model, list):
46
+ for m in model:
47
+ weights_normal_init(m, dev)
48
+ else:
49
+ for m in model.modules():
50
+ if isinstance(m, nn.Conv2d):
51
+ m.weight.data.normal_(0.0, dev)
52
+ elif isinstance(m, nn.Linear):
53
+ m.weight.data.normal_(0.0, dev)
54
+
55
+
56
+ def clip_gradient(model, clip_norm):
57
+ """Computes a gradient clipping coefficient based on gradient norm."""
58
+ totalnorm = 0
59
+ for p in model.parameters():
60
+ if p.requires_grad:
61
+ modulenorm = p.grad.data.norm()
62
+ totalnorm += modulenorm ** 2
63
+ totalnorm = np.sqrt(totalnorm)
64
+
65
+ norm = clip_norm / max(totalnorm, clip_norm)
66
+ for p in model.parameters():
67
+ if p.requires_grad:
68
+ p.grad.mul_(norm)
69
+
70
+
71
+ def EuclideanDistances(A, B):
72
+ BT = B.transpose()
73
+ vecProd = np.dot(A,BT)
74
+ SqA = A**2
75
+ sumSqA = np.matrix(np.sum(SqA, axis=1))
76
+ sumSqAEx = np.tile(sumSqA.transpose(), (1, vecProd.shape[1]))
77
+
78
+ SqB = B**2
79
+ sumSqB = np.sum(SqB, axis=1)
80
+ sumSqBEx = np.tile(sumSqB, (vecProd.shape[0], 1))
81
+ SqED = sumSqBEx + sumSqAEx - 2*vecProd
82
+ SqED[SqED<0]=0.0
83
+ ED = np.sqrt(SqED)
84
+ return ED
85
+
86
+
87
+ def get_center_feature(cnn_feature, img_poly, ind, h, w):
88
+ batch_size = cnn_feature.size(0)
89
+ for i in range(batch_size):
90
+ poly = img_poly[ind == i].cpu().numpy()
91
+ mask = np.zeros((h, w), dtype=np.uint8)
92
+ cv2.fillPoly(mask, poly.astype(np.int32), color=(1,))
93
+ return None
94
+
95
+
96
+ def get_node_feature(cnn_feature, img_poly, ind, h, w):
97
+ img_poly = img_poly.clone().float()
98
+ img_poly[..., 0] = img_poly[..., 0] / (w / 2.) - 1
99
+ img_poly[..., 1] = img_poly[..., 1] / (h / 2.) - 1
100
+
101
+ batch_size = cnn_feature.size(0)
102
+ gcn_feature = torch.zeros([img_poly.size(0), cnn_feature.size(1), img_poly.size(1)]).to(img_poly.device)
103
+ for i in range(batch_size):
104
+ poly = img_poly[ind == i].unsqueeze(0)
105
+ gcn_feature[ind == i] = torch.nn.functional.grid_sample(cnn_feature[i:i + 1], poly)[0].permute(1, 0, 2)
106
+ return gcn_feature
107
+
108
+
109
+ def get_adj_mat(n_adj, n_nodes):
110
+ a = np.zeros([n_nodes, n_nodes], dtype=np.float)
111
+
112
+ for i in range(n_nodes):
113
+ for j in range(-n_adj // 2, n_adj // 2 + 1):
114
+ if j != 0:
115
+ a[i][(i + j) % n_nodes] = 1
116
+ a[(i + j) % n_nodes][i] = 1
117
+ return a
118
+
119
+
120
+ def get_adj_ind(n_adj, n_nodes, device):
121
+ ind = torch.tensor([i for i in range(-n_adj // 2, n_adj // 2 + 1) if i != 0]).long()
122
+ ind = (torch.arange(n_nodes)[:, None] + ind[None]) % n_nodes
123
+ return ind.to(device)
124
+
125
+
126
+ def coord_embedding(b, w, h, device):
127
+ x_range = torch.linspace(0, 1, w, device=device)
128
+ y_range = torch.linspace(0, 1, h, device=device)
129
+ y, x = torch.meshgrid(y_range, x_range)
130
+ y = y.expand([b, 1, -1, -1])
131
+ x = x.expand([b, 1, -1, -1])
132
+ coord_map = torch.cat([x, y], 1)
133
+
134
+ return coord_map
135
+
136
+
137
+ def img_poly_to_can_poly(img_poly):
138
+ if len(img_poly) == 0:
139
+ return torch.zeros_like(img_poly)
140
+ x_min = torch.min(img_poly[..., 0], dim=-1)[0]
141
+ y_min = torch.min(img_poly[..., 1], dim=-1)[0]
142
+ can_poly = img_poly.clone()
143
+ can_poly[..., 0] = can_poly[..., 0] - x_min[..., None]
144
+ can_poly[..., 1] = can_poly[..., 1] - y_min[..., None]
145
+ # x_max = torch.max(img_poly[..., 0], dim=-1)[0]
146
+ # y_max = torch.max(img_poly[..., 1], dim=-1)[0]
147
+ # h, w = y_max - y_min + 1, x_max - x_min + 1
148
+ # long_side = torch.max(h, w)
149
+ # can_poly = can_poly / long_side[..., None, None]
150
+ return can_poly
IndicPhotoOCR/detection/textbpn/network/layers/model_block.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ __author__ = "S.X.Zhang"
3
+ import torch
4
+ import torch.nn as nn
5
+ import torch.nn.functional as F
6
+ from IndicPhotoOCR.detection.textbpn.network.layers.vgg import VggNet
7
+ from IndicPhotoOCR.detection.textbpn.network.layers.resnet import ResNet
8
+ from IndicPhotoOCR.detection.textbpn.network.layers.resnet_dcn import ResNet_DCN
9
+ from IndicPhotoOCR.detection.textbpn.cfglib.config import config as cfg
10
+
11
+
12
+ class UpBlok(nn.Module):
13
+
14
+ def __init__(self, in_channels, out_channels):
15
+ super().__init__()
16
+ self.conv1x1 = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0)
17
+ self.conv3x3 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1)
18
+ self.deconv = nn.ConvTranspose2d(out_channels, out_channels, kernel_size=4, stride=2, padding=1)
19
+
20
+ def forward(self, upsampled, shortcut):
21
+ x = torch.cat([upsampled, shortcut], dim=1)
22
+ x = self.conv1x1(x)
23
+ x = F.relu(x)
24
+ x = self.conv3x3(x)
25
+ x = F.relu(x)
26
+ x = self.deconv(x)
27
+ return x
28
+
29
+
30
+ class MergeBlok(nn.Module):
31
+ def __init__(self, in_channels, out_channels):
32
+ super().__init__()
33
+ self.conv1x1 = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0)
34
+ self.conv3x3 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1)
35
+
36
+ def forward(self, upsampled, shortcut):
37
+ x = torch.cat([upsampled, shortcut], dim=1)
38
+ x = self.conv1x1(x)
39
+ x = F.relu(x)
40
+ x = self.conv3x3(x)
41
+ return x
42
+
43
+
44
+ class FPN(nn.Module):
45
+
46
+ def __init__(self, backbone='resnet50', is_training=True):
47
+ super().__init__()
48
+ self.is_training = is_training
49
+ self.backbone_name = backbone
50
+
51
+ if backbone in ['vgg_bn', 'vgg']:
52
+ self.backbone = VggNet(name=backbone, pretrain=is_training)
53
+ self.deconv5 = nn.ConvTranspose2d(512, 256, kernel_size=4, stride=2, padding=1)
54
+ self.merge4 = UpBlok(512 + 256, 128)
55
+ self.merge3 = UpBlok(256 + 128, 64)
56
+ if cfg.scale == 1:
57
+ self.merge2 = UpBlok(128 + 64, 32) # FPN 1/2
58
+ self.merge1 = UpBlok(64 + 32, 32) # FPN 1/1
59
+ elif cfg.scale == 2:
60
+ self.merge2 = UpBlok(128 + 64, 32) # FPN 1/2
61
+ self.merge1 = MergeBlok(64 + 32, 32) # FPN 1/2
62
+ elif cfg.scale == 4:
63
+ self.merge2 = MergeBlok(128 + 64, 32) # FPN 1/4
64
+
65
+ elif backbone in ['resnet50']:
66
+ self.backbone = ResNet(name=backbone, pretrain=is_training)
67
+ self.deconv5 = nn.ConvTranspose2d(2048, 256, kernel_size=4, stride=2, padding=1)
68
+ self.merge4 = UpBlok(1024 + 256, 128)
69
+ self.merge3 = UpBlok(512 + 128, 64)
70
+ if cfg.scale == 1:
71
+ self.merge2 = UpBlok(256 + 64, 32) # FPN 1/2
72
+ self.merge1 = UpBlok(64 + 32, 32) # FPN 1/1
73
+ elif cfg.scale == 2:
74
+ self.merge2 = UpBlok(256 + 64, 32) # FPN 1/2
75
+ self.merge1 = MergeBlok(64 + 32, 32) # FPN 1/2
76
+ elif cfg.scale == 4:
77
+ self.merge2 = MergeBlok(256 + 64, 32) # FPN 1/4
78
+ self.merge1 = MergeBlok(64 + 32, 32) # FPN 1/4
79
+
80
+ elif backbone in ['resnet18']:
81
+ self.backbone = ResNet(name=backbone, pretrain=is_training)
82
+ self.deconv5 = nn.ConvTranspose2d(512, 256, kernel_size=4, stride=2, padding=1)
83
+ self.merge4 = UpBlok(256 + 256, 128)
84
+ self.merge3 = UpBlok(128 + 128, 64)
85
+ if cfg.scale == 1:
86
+ self.merge2 = UpBlok(64 + 64, 32) # FPN 1/2
87
+ self.merge1 = UpBlok(64 + 32, 32) # FPN 1/1
88
+ elif cfg.scale == 2:
89
+ self.merge2 = UpBlok(64 + 64, 32) # FPN 1/2
90
+ self.merge1 = MergeBlok(64 + 32, 32) # FPN 1/2
91
+ elif cfg.scale == 4:
92
+ self.merge2 = MergeBlok(64 + 64, 32) # FPN 1/4
93
+ self.merge1 = MergeBlok(64 + 32, 32) # FPN 1/4
94
+
95
+ elif backbone in ["deformable_resnet18"]:
96
+ self.backbone = ResNet_DCN(name=backbone, pretrain=is_training)
97
+ self.deconv5 = nn.ConvTranspose2d(512, 256, kernel_size=4, stride=2, padding=1)
98
+ self.merge4 = UpBlok(256 + 256, 128)
99
+ self.merge3 = UpBlok(128 + 128, 64)
100
+ if cfg.scale == 1:
101
+ self.merge2 = UpBlok(64 + 64, 32) # FPN 1/2
102
+ self.merge1 = UpBlok(64 + 32, 32) # FPN 1/1
103
+ elif cfg.scale == 2:
104
+ self.merge2 = UpBlok(64 + 64, 32) # FPN 1/2
105
+ self.merge1 = MergeBlok(64 + 32, 32) # FPN 1/2
106
+ elif cfg.scale == 4:
107
+ self.merge2 = MergeBlok(64 + 64, 32) # FPN 1/4
108
+ self.merge1 = MergeBlok(64 + 32, 32) # FPN 1/4
109
+
110
+ elif backbone in ["deformable_resnet50"]:
111
+ self.backbone = ResNet_DCN(name=backbone, pretrain=is_training)
112
+ self.deconv5 = nn.ConvTranspose2d(2048, 256, kernel_size=4, stride=2, padding=1)
113
+ self.merge4 = UpBlok(1024 + 256, 128)
114
+ self.merge3 = UpBlok(512 + 128, 64)
115
+ if cfg.scale == 1:
116
+ self.merge2 = UpBlok(256 + 64, 32) # FPN 1/2
117
+ self.merge1 = UpBlok(64 + 32, 32) # FPN 1/1
118
+ elif cfg.scale == 2:
119
+ self.merge2 = UpBlok(256 + 64, 32) # FPN 1/2
120
+ self.merge1 = MergeBlok(64 + 32, 32) # FPN 1/2
121
+ elif cfg.scale == 4:
122
+ self.merge2 = MergeBlok(256 + 64, 32) # FPN 1/4
123
+ self.merge1 = MergeBlok(64 + 32, 32) # FPN 1/4
124
+ else:
125
+ print("backbone is not support !")
126
+
127
+ def forward(self, x):
128
+ C1, C2, C3, C4, C5 = self.backbone(x)
129
+ #print(C5.size())
130
+ #print(C4.size())
131
+ #print(C3.size())
132
+ #print(C2.size())
133
+ #print(C1.size())
134
+ up5 = self.deconv5(C5)
135
+ up5 = F.relu(up5)
136
+
137
+ up4 = self.merge4(C4, up5)
138
+ up4 = F.relu(up4)
139
+
140
+ up3 = self.merge3(C3, up4)
141
+ up3 = F.relu(up3)
142
+
143
+ up2 = self.merge2(C2, up3)
144
+ up2 = F.relu(up2)
145
+
146
+ up1 = self.merge1(C1, up2)
147
+ up1 = F.relu(up1)
148
+
149
+ return up1, up2, up3, up4, up5
IndicPhotoOCR/detection/textbpn/network/layers/position_encoding.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
2
+ """
3
+ Various positional encodings for the transformer.
4
+ """
5
+ import math
6
+ import torch
7
+ from torch import nn
8
+
9
+ from util.misc import NestedTensor
10
+
11
+
12
+ class PositionEmbeddingSine(nn.Module):
13
+ """
14
+ This is a more standard version of the position embedding, very similar to the one
15
+ used by the Attention is all you need paper, generalized to work on images.
16
+ """
17
+ def __init__(self, num_pos_feats=64, temperature=10000, normalize=False, scale=None):
18
+ super().__init__()
19
+ self.num_pos_feats = num_pos_feats
20
+ self.temperature = temperature
21
+ self.normalize = normalize
22
+ if scale is not None and normalize is False:
23
+ raise ValueError("normalize should be True if scale is passed")
24
+ if scale is None:
25
+ scale = 2 * math.pi
26
+ self.scale = scale
27
+
28
+ def forward(self, tensor_list: NestedTensor):
29
+ x = tensor_list.tensors
30
+ mask = tensor_list.mask
31
+ assert mask is not None
32
+ not_mask = ~mask
33
+ y_embed = not_mask.cumsum(1, dtype=torch.float32)
34
+ x_embed = not_mask.cumsum(2, dtype=torch.float32)
35
+ if self.normalize:
36
+ eps = 1e-6
37
+ y_embed = y_embed / (y_embed[:, -1:, :] + eps) * self.scale
38
+ x_embed = x_embed / (x_embed[:, :, -1:] + eps) * self.scale
39
+
40
+ dim_t = torch.arange(self.num_pos_feats, dtype=torch.float32, device=x.device)
41
+ dim_t = self.temperature ** (2 * (dim_t // 2) / self.num_pos_feats)
42
+
43
+ pos_x = x_embed[:, :, :, None] / dim_t
44
+ pos_y = y_embed[:, :, :, None] / dim_t
45
+ pos_x = torch.stack((pos_x[:, :, :, 0::2].sin(), pos_x[:, :, :, 1::2].cos()), dim=4).flatten(3)
46
+ pos_y = torch.stack((pos_y[:, :, :, 0::2].sin(), pos_y[:, :, :, 1::2].cos()), dim=4).flatten(3)
47
+ pos = torch.cat((pos_y, pos_x), dim=3).permute(0, 3, 1, 2)
48
+ return pos
49
+
50
+
51
+ class PositionEmbeddingLearned(nn.Module):
52
+ """
53
+ Absolute pos embedding, learned.
54
+ """
55
+ def __init__(self, num_pos_feats=256):
56
+ super().__init__()
57
+ self.row_embed = nn.Embedding(50, num_pos_feats)
58
+ self.col_embed = nn.Embedding(50, num_pos_feats)
59
+ self.reset_parameters()
60
+
61
+ def reset_parameters(self):
62
+ nn.init.uniform_(self.row_embed.weight)
63
+ nn.init.uniform_(self.col_embed.weight)
64
+
65
+ def forward(self, tensor_list: NestedTensor):
66
+ x = tensor_list.tensors
67
+ h, w = x.shape[-2:]
68
+ i = torch.arange(w, device=x.device)
69
+ j = torch.arange(h, device=x.device)
70
+ x_emb = self.col_embed(i)
71
+ y_emb = self.row_embed(j)
72
+ pos = torch.cat([
73
+ x_emb.unsqueeze(0).repeat(h, 1, 1),
74
+ y_emb.unsqueeze(1).repeat(1, w, 1),
75
+ ], dim=-1).permute(2, 0, 1).unsqueeze(0).repeat(x.shape[0], 1, 1, 1)
76
+ return pos
77
+
78
+
79
+ def build_position_encoding(args):
80
+ N_steps = args.hidden_dim // 2
81
+ if args.position_embedding in ('v2', 'sine'):
82
+ # TODO find a better way of exposing other arguments
83
+ position_embedding = PositionEmbeddingSine(N_steps, normalize=True)
84
+ elif args.position_embedding in ('v3', 'learned'):
85
+ position_embedding = PositionEmbeddingLearned(N_steps)
86
+ else:
87
+ raise ValueError(f"not supported {args.position_embedding}")
88
+
89
+ return position_embedding
IndicPhotoOCR/detection/textbpn/network/layers/resnet.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from torchvision.models import resnet
4
+ import torch.utils.model_zoo as model_zoo
5
+ from IndicPhotoOCR.detection.textbpn.cfglib.config import config as cfg
6
+
7
+ model_urls = {
8
+ 'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
9
+ 'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
10
+ 'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
11
+ 'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
12
+ 'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
13
+
14
+ }
15
+
16
+
17
+ class ResNet(nn.Module):
18
+ def __init__(self, name="resnet50", pretrain=True):
19
+ super().__init__()
20
+
21
+ if name == "resnet50":
22
+ base_net = resnet.resnet50(pretrained=False)
23
+ elif name == "resnet101":
24
+ base_net = resnet.resnet101(pretrained=False)
25
+ elif name == "resnet18":
26
+ base_net = resnet.resnet18(pretrained=False)
27
+ elif name == "resnet34":
28
+ base_net = resnet.resnet34(pretrained=False)
29
+
30
+ else:
31
+ print(" base model is not support !")
32
+
33
+ if pretrain:
34
+ print("load the {} weight from ./cache".format(name))
35
+ base_net.load_state_dict(model_zoo.load_url(model_urls[name], model_dir="./cache",
36
+ map_location=torch.device(cfg.device)), strict=False)
37
+ # print(base_net)
38
+ self.stage1 = nn.Sequential(
39
+ base_net.conv1,
40
+ base_net.bn1,
41
+ base_net.relu,
42
+ base_net.maxpool
43
+ )
44
+ self.stage2 = base_net.layer1
45
+ self.stage3 = base_net.layer2
46
+ self.stage4 = base_net.layer3
47
+ self.stage5 = base_net.layer4
48
+ self.up2 = nn.ConvTranspose2d(64, 64, kernel_size=4, stride=2, padding=1)
49
+
50
+ def forward(self, x):
51
+ C1 = self.stage1(x)
52
+ C2 = self.stage2(C1)
53
+ C3 = self.stage3(C2)
54
+ C4 = self.stage4(C3)
55
+ C5 = self.stage5(C4)
56
+
57
+ if cfg.scale == 2 or cfg.scale == 1:
58
+ # up2 --> 1/2
59
+ C1 = self.up2(C1)
60
+
61
+ return C1, C2, C3, C4, C5
62
+
63
+
64
+ if __name__ == '__main__':
65
+ import torch
66
+ input = torch.randn((4, 3, 512, 512))
67
+ net = ResNet()
68
+ C1, C2, C3, C4, C5 = net(input)
69
+ print(C1.size())
70
+ print(C2.size())
71
+ print(C3.size())
72
+ print(C4.size())
73
+ print(C5.size())
IndicPhotoOCR/detection/textbpn/network/layers/resnet_dcn.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from IndicPhotoOCR.detection.textbpn.network.backbone.resnet import deformable_resnet18,deformable_resnet50
4
+ import torch.utils.model_zoo as model_zoo
5
+ from IndicPhotoOCR.detection.textbpn.cfglib.config import config as cfg
6
+
7
+ model_urls = {
8
+ 'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
9
+ 'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
10
+ 'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
11
+ 'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
12
+ 'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
13
+
14
+ }
15
+
16
+
17
+ class ResNet_DCN(nn.Module):
18
+ def __init__(self, name="deformable_resnet18", pretrain=False):
19
+ super().__init__()
20
+
21
+ if name == "deformable_resnet18":
22
+ self.base_net = deformable_resnet18(pretrained=False)
23
+ if pretrain:
24
+ print("load the {} weight from ./cache".format(name))
25
+ self.base_net.load_state_dict(
26
+ model_zoo.load_url(model_urls["resnet18"], model_dir="./cache",
27
+ map_location=torch.device(cfg.device)), strict=False)
28
+
29
+ elif name == "deformable_resnet50":
30
+ self.base_net = deformable_resnet50(pretrained=False)
31
+ if pretrain:
32
+ print("load the {} weight from ./cache".format(name))
33
+ self.base_net.load_state_dict(
34
+ model_zoo.load_url(model_urls["resnet50"], model_dir="./cache",
35
+ map_location=torch.device(cfg.device)), strict=False)
36
+ else:
37
+ print(" base model is not support !")
38
+
39
+ # print(base_net)
40
+ self.up2 = nn.ConvTranspose2d(64, 64, kernel_size=4, stride=2, padding=1)
41
+
42
+ def forward(self, x):
43
+ C1, C2, C3, C4, C5 = self.base_net(x)
44
+ # up2 --> 1/2
45
+ C1 = self.up2(C1)
46
+
47
+ return C1, C2, C3, C4, C5
48
+
49
+
50
+ if __name__ == '__main__':
51
+ import torch
52
+ input = torch.randn((4, 3, 512, 512))
53
+ net = ResNet_DCN()
54
+ C1, C2, C3, C4, C5 = net(input)
55
+ print(C1.size())
56
+ print(C2.size())
57
+ print(C3.size())
58
+ print(C4.size())
59
+ print(C5.size())
IndicPhotoOCR/detection/textbpn/network/layers/vgg.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch.nn as nn
2
+ import torch.utils.model_zoo as model_zoo
3
+ import torchvision.models as models
4
+ from IndicPhotoOCR.detection.textbpn.cfglib.config import config as cfg
5
+
6
+ model_urls = {
7
+ 'vgg11': 'https://download.pytorch.org/models/vgg11-bbd30ac9.pth',
8
+ 'vgg16': 'https://download.pytorch.org/models/vgg16-397923af.pth',
9
+ 'vgg19': 'https://download.pytorch.org/models/vgg19-dcbb9e9d.pth',
10
+ 'vgg11_bn': 'https://download.pytorch.org/models/vgg11_bn-6002323d.pth',
11
+ 'vgg16_bn': 'https://download.pytorch.org/models/vgg16_bn-6c64b313.pth',
12
+ 'vgg19_bn': 'https://download.pytorch.org/models/vgg19_bn-c79401a0.pth',
13
+ }
14
+
15
+
16
+ class VggNet(nn.Module):
17
+ def __init__(self, name="vgg16", pretrain=True):
18
+ super().__init__()
19
+ if name == "vgg16":
20
+ base_net = models.vgg16(pretrained=False)
21
+ elif name == "vgg16_bn":
22
+ base_net = models.vgg16_bn(pretrained=False)
23
+ else:
24
+ print(" base model is not support !")
25
+ if pretrain:
26
+ print("load the {} weight from ./cache".format(name))
27
+ base_net.load_state_dict(model_zoo.load_url(model_urls[name],
28
+ model_dir="./cache",map_location=torch.device(cfg.device)))
29
+
30
+ if name == "vgg16":
31
+ self.stage1 = nn.Sequential(*[base_net.features[layer] for layer in range(0, 5)])
32
+ self.stage2 = nn.Sequential(*[base_net.features[layer] for layer in range(5, 10)])
33
+ self.stage3 = nn.Sequential(*[base_net.features[layer] for layer in range(10, 17)])
34
+ self.stage4 = nn.Sequential(*[base_net.features[layer] for layer in range(17, 24)])
35
+ self.stage5 = nn.Sequential(*[base_net.features[layer] for layer in range(24, 31)])
36
+ elif name == "vgg16_bn":
37
+ self.stage1 = nn.Sequential(*[base_net.features[layer] for layer in range(0, 7)])
38
+ self.stage2 = nn.Sequential(*[base_net.features[layer] for layer in range(7, 14)])
39
+ self.stage3 = nn.Sequential(*[base_net.features[layer] for layer in range(14, 24)])
40
+ self.stage4 = nn.Sequential(*[base_net.features[layer] for layer in range(24, 34)])
41
+ self.stage5 = nn.Sequential(*[base_net.features[layer] for layer in range(34, 44)])
42
+
43
+ def forward(self, x):
44
+ C1 = self.stage1(x)
45
+ C2 = self.stage2(C1)
46
+ C3 = self.stage3(C2)
47
+ C4 = self.stage4(C3)
48
+ C5 = self.stage5(C4)
49
+
50
+ return C1, C2, C3, C4, C5
51
+
52
+
53
+ if __name__ == '__main__':
54
+ import torch
55
+ input = torch.randn((4, 3, 512, 512))
56
+ net = VggNet()
57
+ C1, C2, C3, C4, C5 = net(input)
58
+ print(C1.size())
59
+ print(C2.size())
60
+ print(C3.size())
61
+ print(C4.size())
62
+ print(C5.size())