Skip to content

Commit

Permalink
update docs & script
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-idk committed Nov 1, 2024
1 parent d8100d5 commit bef3a3a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
19 changes: 16 additions & 3 deletions docs/source/docs/objectDetection/about-object-detection.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,17 @@ git clone https://github.com/airockchip/yolov5.git airockchip-yolov5
wget https://raw.githubusercontent.com/PhotonVision/photonvision/refs/heads/master/scripts/onnx2rknn.py
```

Now install the required packages, make sure you have Python 3.9 and pip downloaded.

```bash
cd airockchip-yolov5
pip install -r requirements.txt
```

#### Export Command

```bash
cd airockchip-yolov5 && python export.py --weights '/path/to/best.pt' --rknpu --include 'onnx'
python3 export.py --weights '/path/to/model.pt' --rknpu --include 'onnx'
```

### Step 2: Converting ONNX to RKNN
Expand All @@ -60,10 +67,16 @@ Using the `onnx2rknn.py` script, convert the ONNX model to an RKNN file. This sc

#### Conversion Command

Run the script, passing in the ONNX model and a folder containing images from your dataset:
First install RKNN Toolkit:

```bash
pip install rknn-toolkit2
```

Now, run the script, passing in the ONNX model and a folder containing images from your dataset:

```bash
python onnx2rknn.py /path/to/best.onnx /path/to/export/best.rknn /path/to/dataset/valid/images
python3 onnx2rknn.py /path/to/model.onnx /path/to/export/model.rknn /path/to/dataset/valid/images
```

If you have any questions about the conversion process, ask in the PhotonVision Discord server.
Expand Down
16 changes: 12 additions & 4 deletions scripts/onnx2rknn.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ def collect_images_from_directory(directory_path):

return image_paths

def convert(srcFileName, dstFilename, dataset):
def save_dataset_to_file(dataset, output_path):
"""Save the list of image paths to a text file."""
with open(output_path, 'w') as f:
for image_path in dataset:
f.write(image_path + '\n')
return output_path

def convert(srcFileName, dstFilename, dataset_file):
platform = "rk3588"

print('--> Source file name: ' + srcFileName)
Expand All @@ -36,7 +43,7 @@ def convert(srcFileName, dstFilename, dataset):

# Build model with quantization
print('--> Building model')
ret = rknn.build(do_quantization=True, dataset=dataset)
ret = rknn.build(do_quantization=True, dataset=dataset_file)
if ret != 0:
print('build model failed.')
exit(ret)
Expand All @@ -54,7 +61,6 @@ def convert(srcFileName, dstFilename, dataset):
rknn.release()

def main():

parser = argparse.ArgumentParser(description='Transform to RKNN model')
parser.add_argument('source_file', help='Path to the ONNX model file')
parser.add_argument('description_file', help='Output path for the RKNN model file')
Expand All @@ -66,7 +72,9 @@ def main():
print(f"No images found in directory: {args.quant_dir}")
exit(1)

convert(args.source_file, args.description_file, dataset)
dataset_file = save_dataset_to_file(dataset, 'dataset.txt')

convert(args.source_file, args.description_file, dataset_file)

if __name__ == '__main__':
main()

0 comments on commit bef3a3a

Please sign in to comment.