lookbzz commited on
Commit
b11387a
·
verified ·
1 Parent(s): daee86b

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. README.md +0 -10
  2. onboard_run_axmodel.py +68 -0
README.md CHANGED
@@ -20,16 +20,6 @@ For those who are interested in model conversion, you can try to export onnx or
20
 
21
  [satrn.axera](https://github.com/AXERA-TECH/satrn.axera)
22
 
23
- ## Installation
24
-
25
- ```
26
- conda create -n open-mmlab python=3.8 pytorch=1.10 cudatoolkit=11.3 torchvision -c pytorch -y
27
- conda activate open-mmlab
28
- pip3 install openmim
29
- git clone https://github.com/open-mmlab/mmocr.git
30
- cd mmocr
31
- mim install -e .
32
- ```
33
 
34
  ## Support Platform
35
 
 
20
 
21
  [satrn.axera](https://github.com/AXERA-TECH/satrn.axera)
22
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  ## Support Platform
25
 
onboard_run_axmodel.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import axengine as axe
3
+ import numpy as np
4
+ import math
5
+
6
+ def _get_source_mask(src_seq, valid_ratios) -> torch.Tensor:
7
+ """Generate mask for source sequence.
8
+
9
+ Args:
10
+ src_seq (torch.Tensor): Image sequence. Shape :math:`(N, T, C)`.
11
+ valid_ratios (list[float]): The valid ratio of input image. For
12
+ example, if the width of the original image is w1 and the width
13
+ after padding is w2, then valid_ratio = w1/w2. Source mask is
14
+ used to cover the area of the padding region.
15
+
16
+ Returns:
17
+ Tensor or None: Source mask. Shape :math:`(N, T)`. The region of
18
+ padding area are False, and the rest are True.
19
+ """
20
+
21
+ N, T, _ = src_seq.size()
22
+ mask = None
23
+ if len(valid_ratios) > 0:
24
+ mask = src_seq.new_zeros((N, T), device=src_seq.device)
25
+ for i, valid_ratio in enumerate(valid_ratios):
26
+ valid_width = min(T, math.ceil(T * valid_ratio))
27
+ mask[i, :valid_width] = 1
28
+
29
+ return mask
30
+
31
+ onnx_bb_encoder = axe.InferenceSession("backbone_encoder.axmodel")
32
+ onnx_decoder = axe.InferenceSession("decoder.axmodel")
33
+
34
+ input_image = torch.tensor(np.load('input_tensor/input_image.npy'))
35
+ out_enc = onnx_bb_encoder.run(["output"], {"input": np.array(input_image.cpu())})[0]
36
+ out_enc = torch.tensor(out_enc)
37
+ data_samples = None
38
+
39
+ N = out_enc.size(0)
40
+
41
+ init_target_seq = torch.tensor(np.load('input_tensor/init_target_seq.npy')).to(torch.int32)
42
+ outputs = []
43
+ max_seq_len = 25
44
+ for step in range(0, max_seq_len):
45
+ valid_ratios = [1.0 for _ in range(out_enc.size(0))]
46
+ if data_samples is not None:
47
+ valid_ratios = []
48
+ for data_sample in data_samples:
49
+ valid_ratios.append(data_sample.get('valid_ratio'))
50
+
51
+ src_mask = _get_source_mask(out_enc, valid_ratios)
52
+ # step_result = model_decoder(init_target_seq,out_enc,src_mask,step)
53
+ step_result = onnx_decoder.run(["output"],{'init_target_seq':np.array(init_target_seq),
54
+ 'out_enc':np.array(out_enc),
55
+ 'src_mask':np.array(src_mask),
56
+ 'step':np.array([step]).astype(np.int32)})[0][0]
57
+ step_result = torch.tensor(step_result)
58
+ outputs.append(step_result)
59
+ _, step_max_index = torch.max(step_result, dim=-1)
60
+ init_target_seq[:, step + 1] = step_max_index
61
+ outputs = torch.stack(outputs, dim=1)
62
+ np.save('output_tensor/outputs.npy',outputs)
63
+
64
+
65
+
66
+
67
+
68
+