pesi
/

Luigi commited on
Commit
e4e03fd
1 Parent(s): bd45a81

Bugfix: correct batch dimension fixation then regenerate ONNX models

Browse files

Batch dimension has been fixed properly following the guide below
https://onnxruntime.ai/docs/tutorials/mobile/helpers/make-dynamic-shape-fixed.html

convert_to_fp16_all.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+
4
+ def convert_onnx_to_fp16_corrected(input_dir):
5
+ # List all files in the input directory
6
+ files = os.listdir(input_dir)
7
+
8
+ # Filter out files with the .onnx extension
9
+ onnx_files = [file for file in files if file.endswith('.onnx')]
10
+
11
+ # Iterate over each ONNX file to convert it to FP16
12
+ for onnx_file in onnx_files:
13
+ # Split the file name to insert '.fp16' before the last underscore
14
+ parts = onnx_file.rsplit('_', 1)
15
+ if len(parts) == 2:
16
+ # If there is at least one underscore, insert '.fp16' before the last part
17
+ output_model_name = f"{parts[0]}.fp16_{parts[1]}"
18
+ else:
19
+ # If there's no underscore, just replace '.onnx' with '.fp16.onnx'
20
+ output_model_name = onnx_file.replace('.onnx', '.fp16.onnx')
21
+
22
+ # Construct the command to run the conversion script
23
+ command = [
24
+ 'python3', 'convert_to_fp16.py',
25
+ '--input_model', os.path.join(input_dir, onnx_file),
26
+ '--output_model', os.path.join(input_dir, output_model_name)
27
+ ]
28
+
29
+ # Execute the command
30
+ subprocess.run(command, check=True)
31
+
32
+ if __name__ == '__main__':
33
+ # Assuming the current directory is the input directory
34
+ input_dir = os.getcwd()
35
+ convert_onnx_to_fp16_corrected(input_dir)
fix_batch_dimension.py DELETED
@@ -1,35 +0,0 @@
1
- import argparse
2
- import onnx
3
- from onnx import numpy_helper
4
- import numpy as np
5
-
6
- def fix_batch_dimension(input_model_path, output_model_path, batch_size=1):
7
- # Load the input ONNX model
8
- model = onnx.load(input_model_path)
9
-
10
- # Iterate through the model's inputs
11
- for input_tensor in model.graph.input:
12
- # Get the tensor shape
13
- tensor_shape = input_tensor.type.tensor_type.shape
14
-
15
- # Modify the batch dimension
16
- if len(tensor_shape.dim) > 0:
17
- tensor_shape.dim[0].dim_value = batch_size
18
-
19
- # Save the modified model to the output path
20
- onnx.save(model, output_model_path)
21
- print(f"Model saved with updated batch size of {batch_size} to {output_model_path}")
22
-
23
- if __name__ == "__main__":
24
- # Setup command line arguments
25
- parser = argparse.ArgumentParser(description="Fix batch dimension of an ONNX model.")
26
- parser.add_argument("input_model_path", type=str, help="Path to the input ONNX model.")
27
- parser.add_argument("output_model_path", type=str, help="Path to save the output ONNX model with fixed batch dimension.")
28
- parser.add_argument("--batch_size", type=int, default=1, help="Value of batch size to assign (default is 1).")
29
-
30
- # Parse arguments
31
- args = parser.parse_args()
32
-
33
- # Call the function to fix the batch dimension
34
- fix_batch_dimension(args.input_model_path, args.output_model_path, args.batch_size)
35
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fix_batch_dimension_all.sh CHANGED
@@ -5,15 +5,15 @@ for model in ./*.onnx; do
5
  # Exclude processing any files that already have a batch size in their name
6
  if [[ ! $model =~ _batch[0-9]+\.onnx$ ]]; then
7
  # Process for batch size 1
8
- python fix_batch_dimension.py "$model" "${model%.onnx}_batch1.onnx" --batch_size 1
9
  echo "Generated ${model%.onnx}_batch1.onnx"
10
 
11
  # Process for batch size 2
12
- python fix_batch_dimension.py "$model" "${model%.onnx}_batch2.onnx" --batch_size 2
13
  echo "Generated ${model%.onnx}_batch2.onnx"
14
 
15
  # Process for batch size 4
16
- python fix_batch_dimension.py "$model" "${model%.onnx}_batch4.onnx" --batch_size 4
17
  echo "Generated ${model%.onnx}_batch4.onnx"
18
  fi
19
  done
 
5
  # Exclude processing any files that already have a batch size in their name
6
  if [[ ! $model =~ _batch[0-9]+\.onnx$ ]]; then
7
  # Process for batch size 1
8
+ python3 -m onnxruntime.tools.make_dynamic_shape_fixed --dim_param batch --dim_value 1 "$model" "${model%.onnx}_batch1.onnx"
9
  echo "Generated ${model%.onnx}_batch1.onnx"
10
 
11
  # Process for batch size 2
12
+ python3 -m onnxruntime.tools.make_dynamic_shape_fixed --dim_param batch --dim_value 2 "$model" "${model%.onnx}_batch2.onnx"
13
  echo "Generated ${model%.onnx}_batch2.onnx"
14
 
15
  # Process for batch size 4
16
+ python3 -m onnxruntime.tools.make_dynamic_shape_fixed --dim_param batch --dim_value 4 "$model" "${model%.onnx}_batch4.onnx"
17
  echo "Generated ${model%.onnx}_batch4.onnx"
18
  fi
19
  done
rtmo-l.fp16.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:661897e1e8cb15a9747ef0a6d86238c205e494f4861fced708d975fa54ab9a5b
3
- size 88036653
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ea87472539adf395a81f15035d0e6a0db530b22ddcc1775fd3f6847fceba7d4f
3
+ size 88035167
rtmo-l.fp16_batch1.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:431290c0e7a3025e08dd1fef85e41435e7fc4927195464c28aa9e7723e4db1c5
3
- size 88036648
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:644b8c4874bddef42b97b2ff2f2edaecd612b6f35897ceaaf6cc48caeac6a083
3
+ size 88032432
rtmo-l.fp16_batch2.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:b765824473d07936e7346ceac100007150a722f1c1406d95077c1340b65b16d9
3
- size 88036648
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6d86819d5d382afcfe4d482bae5f620fbca8e5f52f0c79a6677c91b597e8f47c
3
+ size 88032398
rtmo-l.fp16_batch4.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:006e088660b8e6be74b6572fbcbff0723c02816252876b4546466ecca4462b9b
3
- size 88036648
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:048cd749dcf87b644ce88c78ec656f63f25b47d00bc057507f95a5f5f059b770
3
+ size 88032398
rtmo-l_batch1.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:eb610ad52d5090881bbb5f7b8720773846a65257ddda8f7cba29d848688d8fb6
3
- size 175941846
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:94b757af7c448fa4985aed100e6d1122ad3559d61ed81cc87751a85288f8a647
3
+ size 175938796
rtmo-l_batch2.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:cac7daf35257f78c533e088773df544806c69d03f12aef9937ecf0e161649e4a
3
- size 175941846
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5d5d8632f9189dc1e46686c4427c0bbff624e2c8bdcaafbb7c13c31d7128dad1
3
+ size 175938796
rtmo-l_batch4.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:c83886f33bba6bda437ce9fafe2b395d33993f1bfc00a8b1f27f3a094ae69ef1
3
- size 175941846
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1ea8088c6f24454d60e4af491c6b91f0a3dd97b928c87430143fe2dd3488f405
3
+ size 175938796
rtmo-m.fp16.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:3803642557800cf7123088074513221743bbbda1f875337408925c2cf55b36d6
3
- size 44714440
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:509a805d124f6cb943405eb41a0c28bfb9ac1919631de63818abae7b3e41dfa5
3
+ size 44712954
rtmo-m.fp16_batch1.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:3ad1ffa7f34dce7df78c4b7eac88d443b36db0c1255ca19e8a5e16b24758bfbe
3
- size 44714435
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9a9f8ea58642c7e738d8f6a1c35e54c97686225e694b287525efd72e32bc3e79
3
+ size 44710494
rtmo-m.fp16_batch2.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:b2bd44af05d333eea99cf59a37797934e646bd3d8d146873d9a1c8b22bc7f130
3
- size 44714435
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ba340136e48163fe0cfd507db6a7ad5c8549c4bd07df5581156468bea6942282
3
+ size 44710460
rtmo-m.fp16_batch4.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:706a49d63bc19bb45a1de326e129d8f83f24664990b544180259fadca13a73cb
3
- size 44714435
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4437562c4dd81f5db334d753a389f550c7e00c1b65b3bbc12a20a68080db98f2
3
+ size 44710460
rtmo-m_batch1.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:26d4434838a4ff158f6cdcab421a08ddd82900f3fcc41ec0921d7245e2c5818d
3
- size 89307171
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3d7113a12ec7fb313083e61fc5a3a83e3b694cb69b793c28004b5e0a2490cae8
3
+ size 89304396
rtmo-m_batch2.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:f64f9494e826b39b8347af6de0e9fcb932c58762578fdcf4cdcc53be5852bacf
3
- size 89307171
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d782e675967e88463a29996d57ff1fb59598e6a6d81c65cf4a92ad8dcfc00b74
3
+ size 89304396
rtmo-m_batch4.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:9a263fb2e9310073d147fe1aeac80762c4213be1bcd84be689b0973a61c36efa
3
- size 89307171
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:512041bcabede48cc202e20559d8f7a8d39a63890c5ba798d5fadaaefb5d33e9
3
+ size 89304396
rtmo-s.fp16.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:8e608f9f14608fa9b868e9aa320b9066fd37e1afa90c41e5513df9ae27684bca
3
- size 19880915
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5c237799c55a2393c1562164edbb36fbb61f946c87956b69ab522f2ecdb85db4
3
+ size 19879429
rtmo-s.fp16_batch1.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:30f1264466acbc4782815f35f628d8d7ae845ecef95cf7281405d7a61d453dae
3
- size 19880910
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:91e95cc9145f6fe7605f6cb298d5f7836ce5e53b8c754e4fdb9a7e0458eadb29
3
+ size 19877304
rtmo-s.fp16_batch2.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:f0659aacd2dcb4c3d68073e126b0af38a48d8b4952ef9a6f85f376a3c95c4a8f
3
- size 19880910
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8eca1f9f073d1310b1febdd0e5e620c37c2f5fdeda539191d80fb6710be5a2ac
3
+ size 19877270
rtmo-s.fp16_batch4.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:34344796d956d2bd3e78abac5422605858a16866dae78f2e8e5fc3e9da440e8e
3
- size 19880910
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:32d98c24db0874def5032726202b4bb067ea0445e3dd030fed9a5e1a2689285a
3
+ size 19877270
rtmo-s_batch1.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:699704b00b06b791f31d6b15a7e4bb4abd57f8dab1a60c58fc22490d4ab55c93
3
- size 39651660
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:948208e964af1fca8479e9114ff3eb914ea140cf75c75d97bf67c59bf6cbca43
3
+ size 39649220
rtmo-s_batch2.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:28a5f6e563ad3a20c9e2c18355514c0a2c0aa147f09017f2fbace79a9d7ba38d
3
- size 39651660
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:686f4b1c6975a3cf53b90583947be00ccbedef978f0bb2a7b3cb6dbdb8eaacd9
3
+ size 39649220
rtmo-s_batch4.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:f5d2304570931d9351d10f2940311f0dd8d774e92cc0ec88256d1fcfe4ee796c
3
- size 39651660
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:05d78076b5dda52a03385f208a571a31795235c6cbad32dba4f20d0afd77c898
3
+ size 39649220
rtmo-t.fp16.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:5973ae0e3ac5feb91f28d113d5ca76be0f59aea137373e6231893b8853eba0e8
3
- size 13742854
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:29d0a7ee6da1eaa14509ce62deadbc6b7b16519486602394f6a19cfdaa607bb3
3
+ size 13741368
rtmo-t.fp16_batch1.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:0241fdd72ddb93b4a8aab85ca279c0e3570b5c414057ec439e2e2bc826838171
3
- size 13742849
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:611919fcc2202c06df67c8a2c7c08b2f81da2fb83f144215cc6fbb0bb8a9f2a5
3
+ size 13739243
rtmo-t.fp16_batch2.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:6cb717e8f19e544268a3ba780ae604ad1438abcb0e9fde8425b21c0faf871782
3
- size 13742849
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:747804876024f0b7c474c65477f060124c7870cdd23c9db196a2d7d15cad0520
3
+ size 13739209
rtmo-t.fp16_batch4.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:422e684cbd1710cd42f8bad12c243869c9fe3c998904d46a030186b6d3dbf286
3
- size 13742849
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b321277a64f4062a7e502cc261b77e2503b9efc8d9b031c592032209845a2e47
3
+ size 13739209
rtmo-t_batch1.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:3493fc0b00118d937db56e3aa523799441e2f1a0f40753f15c8e34f0761c3c94
3
- size 27375829
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e13642306c47ee0a29f48b99dcea2cecda8166123f838ccd1e85e8b884898517
3
+ size 27373389
rtmo-t_batch2.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:21d77708f79cd81b644f53804f6541d672e10556d789cec8eebbeafc9d41a8ce
3
- size 27375829
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8f2458615ee49aec6f4c7bc7ce1129fe82e0cbc388141670ccb42e06212d870d
3
+ size 27373389
rtmo-t_batch4.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:5024770899ee523e13a6f683a443c4c0820f90e17cdfe3985035466a4520f772
3
- size 27375829
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dd0aa5cffc9fe6cf829de801a3544a12e137e05f3ce9a0d196720c4de276d10f
3
+ size 27373389