Cyril666 commited on
Commit
7998bc9
·
1 Parent(s): feae110

First model version

Browse files
app.py CHANGED
@@ -1,18 +1,29 @@
1
  import os
2
  os.system('pip install --upgrade --no-cache-dir gdown')
3
  os.system('gdown -O ./output/ctw/model_ctw.pth 1Ajslu_9WisuZ2nJGzE6qbD87aK6_ozzA')
 
 
4
  os.system('pip install "git+https://github.com/philferriere/cocoapi.git#egg=pycocotools&subdirectory=PythonAPI"')
5
  os.system('python setup.py build develop --user')
6
- #os.system('pip install git+https://github.com/Cyril-Sterling/ContourNet')
7
 
8
  import cv2
9
  import pandas as pd
10
  import gradio as gr
 
11
  from det_demo import DetDemo
12
  from maskrcnn_benchmark.config import cfg
13
 
 
 
 
14
 
15
  def infer(filepath):
 
 
 
 
 
 
16
  cfg.merge_from_file('./configs/det/r50_baseline.yaml')
17
  # manual override some options
18
  cfg.merge_from_list(["MODEL.DEVICE", "cpu"])
@@ -23,9 +34,15 @@ def infer(filepath):
23
  confidence_threshold=0.7,
24
  output_polygon=True
25
  )
 
26
  image = cv2.imread(filepath)
 
 
 
 
27
  result_polygons, result_masks, result_boxes = det_demo.run_on_opencv_image(image)
28
  visual_image = det_demo.visualization(image.copy(), result_polygons, result_masks, result_boxes)
 
29
  cv2.imwrite('result.jpg', visual_image)
30
  return 'result.jpg'#, pd.DataFrame(result_words)
31
 
 
1
  import os
2
  os.system('pip install --upgrade --no-cache-dir gdown')
3
  os.system('gdown -O ./output/ctw/model_ctw.pth 1Ajslu_9WisuZ2nJGzE6qbD87aK6_ozzA')
4
+ os.system('gdown -O ./workdir.zip 1mYM_26qHUom_5NU7iutHneB_KHlLjL5y')
5
+ os.system('unzip workdir.zip')
6
  os.system('pip install "git+https://github.com/philferriere/cocoapi.git#egg=pycocotools&subdirectory=PythonAPI"')
7
  os.system('python setup.py build develop --user')
 
8
 
9
  import cv2
10
  import pandas as pd
11
  import gradio as gr
12
+
13
  from det_demo import DetDemo
14
  from maskrcnn_benchmark.config import cfg
15
 
16
+ from demo import get_model, preprocess, postprocess, load
17
+ from utils import Config, Logger, CharsetMapper
18
+
19
 
20
  def infer(filepath):
21
+ config = Config('configs/rec/train_abinet.yaml')
22
+ config.model_vision_checkpoint = None
23
+ model = get_model(config)
24
+ model = load(model, 'workdir/train-abinet/best-train-abinet.pth')
25
+ charset = CharsetMapper(filename=config.dataset_charset_path, max_length=config.dataset_max_length + 1)
26
+
27
  cfg.merge_from_file('./configs/det/r50_baseline.yaml')
28
  # manual override some options
29
  cfg.merge_from_list(["MODEL.DEVICE", "cpu"])
 
34
  confidence_threshold=0.7,
35
  output_polygon=True
36
  )
37
+
38
  image = cv2.imread(filepath)
39
+ patchs = [image[box[1]:box[3], box[0]:box[2], :] for box in result_boxes]
40
+ patchs = [preprocess(patch, config.dataset_image_width, config.dataset_image_height) for patch in patchs]
41
+ patchs = torch.stack(patchs, dim=0)
42
+
43
  result_polygons, result_masks, result_boxes = det_demo.run_on_opencv_image(image)
44
  visual_image = det_demo.visualization(image.copy(), result_polygons, result_masks, result_boxes)
45
+
46
  cv2.imwrite('result.jpg', visual_image)
47
  return 'result.jpg'#, pd.DataFrame(result_words)
48
 
configs/ic/r50_baseline.yaml DELETED
@@ -1,75 +0,0 @@
1
- OUTPUT_DIR: "./output/ic15"
2
- MODEL:
3
- META_ARCHITECTURE: "GeneralizedRCNN"
4
- WEIGHT: catalog://ImageNetPretrained/MSRA/R-50
5
- BACKBONE:
6
- CONV_BODY: "R-50-FPN"
7
- RESNETS:
8
- BACKBONE_OUT_CHANNELS: 256
9
- RPN:
10
- USE_FPN: True
11
- ANCHOR_STRIDE: (4, 8, 16, 32, 64)
12
- ASPECT_RATIOS: (0.25, 0.5, 1.0, 2.0, 4.0)
13
- ROI_HEADS:
14
- USE_FPN: True
15
- SCORE_THRESH: 0.52 # ic15
16
- NMS: 0.89
17
- ROI_BOX_HEAD:
18
- DEFORMABLE_POOLING: False
19
- POOLER_RESOLUTION: 7
20
- POOLER_SCALES: (0.25, 0.125, 0.0625, 0.03125)
21
- POOLER_SAMPLING_RATIO: 2
22
- FEATURE_EXTRACTOR: "FPN2MLPFeatureExtractor"
23
- PREDICTOR: "FPNPredictor"
24
- NUM_CLASSES: 2
25
- CLASS_WEIGHT: 1.0
26
- ## Boundary
27
- BOUNDARY_ON: True
28
- ROI_BOUNDARY_HEAD:
29
- DEFORMABLE_POOLING: False
30
- FEATURE_EXTRACTOR: "BoundaryRCNNFPNFeatureExtractor"
31
- POOLER_RESOLUTION: 14
32
- POOLER_SCALES: (0.25, 0.125, 0.0625, 0.03125)
33
- POOLER_SAMPLING_RATIO: 2
34
- PREDICTOR: "BoundaryRCNNC4Predictor"
35
- RESOLUTION: 48
36
- SHARE_BOX_FEATURE_EXTRACTOR: False
37
- BO_WEIGHT: 0.1
38
- Loss_balance: 1.0
39
-
40
- PROCESS:
41
- PNMS: True
42
- NMS_THRESH: 0.25
43
- DATASETS:
44
- TRAIN: ("ic15_train",)
45
- TEST: ("ic15_test",)
46
- Test_Visual: True
47
- DATALOADER:
48
- SIZE_DIVISIBILITY: 32
49
- SOLVER:
50
- BASE_LR: 0.00025
51
- BIAS_LR_FACTOR: 2
52
- WEIGHT_DECAY: 0.0001
53
- # STEPS: (120000, 160000)
54
- STEPS: (5000, 10000) # fine-tune
55
- # MAX_ITER: 180000
56
- MAX_ITER: 190500 # fine-tune
57
- IMS_PER_BATCH: 1
58
- CHECKPOINT_PERIOD: 5000
59
- INPUT:
60
-
61
- MIN_SIZE_TRAIN: (400,600,720,1000,1200)
62
- MAX_SIZE_TRAIN: 2000
63
- MIN_SIZE_TEST: 1200
64
- MAX_SIZE_TEST: 2000
65
-
66
- CROP_PROB_TRAIN: 1.0
67
- ROTATE_PROB_TRAIN: 0.3 # fine-tune
68
- # ROTATE_PROB_TRAIN: 1.0
69
- # ROTATE_DEGREE: (0,30,60,90,210,150,180,210,240,270,300,330,360)
70
- ROTATE_DEGREE: (10,) # fine-tune
71
-
72
- TEST:
73
- IMS_PER_BATCH: 1
74
-
75
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
configs/rec/template.yaml ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ global:
2
+ name: exp
3
+ phase: train
4
+ stage: pretrain-vision
5
+ workdir: /tmp/workdir
6
+ seed: ~
7
+
8
+ dataset:
9
+ train: {
10
+ roots: ['data/training/MJ/MJ_train/',
11
+ 'data/training/MJ/MJ_test/',
12
+ 'data/training/MJ/MJ_valid/',
13
+ 'data/training/ST'],
14
+ batch_size: 128
15
+ }
16
+ test: {
17
+ roots: ['data/evaluation/IIIT5k_3000',
18
+ 'data/evaluation/SVT',
19
+ 'data/evaluation/SVTP',
20
+ 'data/evaluation/IC13_857',
21
+ 'data/evaluation/IC15_1811',
22
+ 'data/evaluation/CUTE80'],
23
+ batch_size: 128
24
+ }
25
+ charset_path: data/charset_36.txt
26
+ num_workers: 4
27
+ max_length: 25 # 30
28
+ image_height: 32
29
+ image_width: 128
30
+ case_sensitive: False
31
+ eval_case_sensitive: False
32
+ data_aug: True
33
+ multiscales: False
34
+ pin_memory: True
35
+ smooth_label: False
36
+ smooth_factor: 0.1
37
+ one_hot_y: True
38
+ use_sm: False
39
+
40
+ training:
41
+ epochs: 6
42
+ show_iters: 50
43
+ eval_iters: 3000
44
+ save_iters: 20000
45
+ start_iters: 0
46
+ stats_iters: 100000
47
+
48
+ optimizer:
49
+ type: Adadelta # Adadelta, Adam
50
+ true_wd: False
51
+ wd: 0. # 0.001
52
+ bn_wd: False
53
+ args: {
54
+ # betas: !!python/tuple [0.9, 0.99], # betas=(0.9,0.99) for AdamW
55
+ # betas: !!python/tuple [0.9, 0.999], # for default Adam
56
+ }
57
+ clip_grad: 20
58
+ lr: [1.0, 1.0, 1.0] # lr: [0.005, 0.005, 0.005]
59
+ scheduler: {
60
+ periods: [3, 2, 1],
61
+ gamma: 0.1,
62
+ }
63
+
64
+ model:
65
+ name: 'modules.model_abinet.ABINetModel'
66
+ checkpoint: ~
67
+ strict: True
configs/rec/train_abinet.yaml ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ global:
2
+ name: train-abinet
3
+ phase: train
4
+ stage: train-super
5
+ workdir: workdir
6
+ seed: ~
7
+
8
+ dataset:
9
+ train: {
10
+ roots: ['data/training/MJ/MJ_train/',
11
+ 'data/training/MJ/MJ_test/',
12
+ 'data/training/MJ/MJ_valid/',
13
+ 'data/training/ST'],
14
+ batch_size: 384
15
+ }
16
+ test: {
17
+ roots: ['data/evaluation/IIIT5k_3000',
18
+ 'data/evaluation/SVT',
19
+ 'data/evaluation/SVTP',
20
+ 'data/evaluation/IC13_857',
21
+ 'data/evaluation/IC15_1811',
22
+ 'data/evaluation/CUTE80'],
23
+ batch_size: 384
24
+ }
25
+ data_aug: True
26
+ multiscales: False
27
+ num_workers: 14
28
+
29
+ training:
30
+ epochs: 10
31
+ show_iters: 50
32
+ eval_iters: 3000
33
+ save_iters: 3000
34
+
35
+ optimizer:
36
+ type: Adam
37
+ true_wd: False
38
+ wd: 0.0
39
+ bn_wd: False
40
+ clip_grad: 20
41
+ lr: 0.0001
42
+ args: {
43
+ betas: !!python/tuple [0.9, 0.999], # for default Adam
44
+ }
45
+ scheduler: {
46
+ periods: [6, 4],
47
+ gamma: 0.1,
48
+ }
49
+
50
+ model:
51
+ name: 'modules.model_abinet_iter.ABINetIterModel'
52
+ iter_size: 3
53
+ ensemble: ''
54
+ use_vision: False
55
+ vision: {
56
+ checkpoint: workdir/pretrain-vision-model/best-pretrain-vision-model.pth,
57
+ loss_weight: 1.,
58
+ attention: 'position',
59
+ backbone: 'transformer',
60
+ backbone_ln: 3,
61
+ }
62
+ language: {
63
+ checkpoint: workdir/pretrain-language-model/pretrain-language-model.pth,
64
+ num_layers: 4,
65
+ loss_weight: 1.,
66
+ detach: True,
67
+ use_self_attn: False
68
+ }
69
+ alignment: {
70
+ loss_weight: 1.,
71
+ }