Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model Zoo API #355

Closed
wants to merge 13 commits into from
60 changes: 60 additions & 0 deletions model-zoo-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Python-API-Script
Onnx Python API Script used to download and save pretrained models from [onnx model zoo](https://github.com/onnx/models). Retrieves metadata after the model is successfully downloaded.

## Features


```get_model_versions(MODEL_FOLDER_NAME)``` - Retrieves an array with strings values of all the versions of the specified model in the ONNX Model Zoo.
```
'bert-squad': ['bertsquad-10', 'bertsquad-8']
```

```get_pretrained()``` - Downloads and saves specific onnx models to desired path.


```get_metadata()``` - Retrieves metadata of the onnx model.

## Intialization and Usage
Initiate the object by calling onnx_zoo class name.
```
OBJECT_NAME = onnx_zoo()
```
The Python scipt will then ask to input the model folder name. When inputting the name, it should be in all lowercase.

```
Enter Model Name: resnet
```

After model folder name is received from input, the script will display all the model versions that exist in the folder.

```
['resnet101-v1-7', 'resnet101-v2-7', 'resnet152-v1-7', 'resnet152-v2-7', 'resnet18-v1-7', 'resnet18-v2-7', 'resnet34-v1-7', 'resnet34-v2-7', 'resnet50-caffe2-v1-3', 'resnet50-caffe2-v1-6', 'resnet50-caffe2-v1-7', 'resnet50-caffe2-v1-8', 'resnet50-caffe2-v1-9', 'resnet50-v1-7', 'resnet50-v2-7']
```

The CLI script then prompts the user to input the version (from the array of model versions) that will be downloaded and input the local directory that the model will be saved in.

```
Enter model name from options: resnet101-v1-7
Enter output directory name to save model: /Users/name/Downloads
```

To download the model and output its metadata, run the following functions:

```
OBJECT_NAME.get_pretrained()
OBJECT_NAME.get_metadata()
```

## Installation
Install onnx to check models

```pip install onnx```

Install [onnxruntime](https://github.com/microsoft/onnxruntime) to run onnx models

```pip install onnxruntime```

## Contributors
* [Kundana Pillari](https://github.com/kundanapillari)
* [Shirley Su](https://github.com/shirleysu8)
* [Jennifer Wang](https://github.com/jennifererwangg)
39 changes: 39 additions & 0 deletions model-zoo-api/dict/generate_model_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from pathlib import Path
import re
from collections import defaultdict


# identify list of onnx models in model Zoo
model_list = []

# generate default dictionary
dict = defaultdict(list)

for path in Path('text').rglob('*.onnx'):
model_list.append(str(path))
# obtains model name
pattern1 = re.compile(".*/([^/]+)/model")
m1 = pattern1.match(str(path))
if m1 != None:
model_name = m1.group(1)
# obtains file name
pattern2 = re.compile(".*/([^/]+\\.*).onnx")
m2 = pattern2.match(str(path))
file_name = m2.group(1)
dict[model_name].append(file_name)

for path in Path('vision').rglob('*.onnx'):
model_list.append(str(path))
# obtains model name
pattern1 = re.compile(".*/([^/]+)/model")
m1 = pattern1.match(str(path))
if m1 != None:
model_name = m1.group(1)
# obtains file name
pattern2 = re.compile(".*/([^/]+\\.*).onnx")
m2 = pattern2.match(str(path))
file_name = m2.group(1)
dict[model_name].append(file_name)


print(dict)
31 changes: 31 additions & 0 deletions model-zoo-api/dict/generate_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from pathlib import Path
import re

model_list = []
dict = {} # ex: "vgg" : "https://github.com/onnx/models/blob/master/vision/classification/vgg/model/vgg16-7.onnx?raw=true"
folder_names = ["text", "vision"]

def generate_paths(folder_names):
"""
Generates a list of onnx models and
stores them in `dict` and `model_list`
"""
for path in Path(folder_names).rglob('*.onnx'):
model_list.append(str(path))
if path != None:
# Generate onnx model URL
url_paths = 'https://github.com/onnx/models/blob/master/' + str(path) + '?raw=true'
# Obtain model name
pattern = re.compile(".*/([^/]+\\.*).onnx")
m = pattern.match(url_paths)
if m != None:
file_name = m.group(1)
dict[file_name] = url_paths

# generate paths to text or vision onnx models
for model_type in folder_names:
generate_paths(model_type)

# print out the keys and values from dict
for keys,values in dict.items():
print (f'"{keys}"' + " : " + f'"{values}"' + " ,")
1 change: 1 addition & 0 deletions model-zoo-api/dict/model_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
versionDict = {'bert-squad': ['bertsquad-10', 'bertsquad-8'], 'bidirectional_attention_flow': ['bidaf-9'], 'gpt-2': ['gpt2-10', 'gpt2-lm-head-10'], 'arcface': ['arcfaceresnet100-8'], 'emotion_ferplus': ['emotion-ferplus-2', 'emotion-ferplus-7', 'emotion-ferplus-8'], 'alexnet': ['bvlcalexnet-3', 'bvlcalexnet-6', 'bvlcalexnet-7', 'bvlcalexnet-8', 'bvlcalexnet-9'], 'caffenet': ['caffenet-3', 'caffenet-6', 'caffenet-7', 'caffenet-8', 'caffenet-9'], 'densenet-121': ['densenet-3', 'densenet-6', 'densenet-7', 'densenet-8', 'densenet-9'], 'efficientnet-lite4': ['efficientnet-lite4'], 'googlenet': ['googlenet-3', 'googlenet-6', 'googlenet-7', 'googlenet-8', 'googlenet-9'], 'inception_v1': ['inception-v1-3', 'inception-v1-6', 'inception-v1-7', 'inception-v1-8', 'inception-v1-9'], 'inception_v2': ['inception-v2-3', 'inception-v2-6', 'inception-v2-7', 'inception-v2-8', 'inception-v2-9'], 'mnist': ['mnist-1', 'mnist-7', 'mnist-8'], 'mobilenet': ['mobilenetv2-7'], 'rcnn_ilsvrc13': ['rcnn-ilsvrc13-3', 'rcnn-ilsvrc13-6', 'rcnn-ilsvrc13-7', 'rcnn-ilsvrc13-8', 'rcnn-ilsvrc13-9'], 'resnet': ['resnet101-v1-7', 'resnet101-v2-7', 'resnet152-v1-7', 'resnet152-v2-7', 'resnet18-v1-7', 'resnet18-v2-7', 'resnet34-v1-7', 'resnet34-v2-7', 'resnet50-caffe2-v1-3', 'resnet50-caffe2-v1-6', 'resnet50-caffe2-v1-7', 'resnet50-caffe2-v1-8', 'resnet50-caffe2-v1-9', 'resnet50-v1-7', 'resnet50-v2-7'], 'shufflenet': ['shufflenet-3', 'shufflenet-6', 'shufflenet-7', 'shufflenet-8', 'shufflenet-9', 'shufflenet-v2-10'], 'squeezenet': ['squeezenet1.0-3', 'squeezenet1.0-6', 'squeezenet1.0-7', 'squeezenet1.0-8', 'squeezenet1.0-9', 'squeezenet1.1-7'], 'vgg': ['vgg16-7', 'vgg16-bn-7', 'vgg19-7', 'vgg19-bn-7', 'vgg19-caffe2-3', 'vgg19-caffe2-6', 'vgg19-caffe2-7', 'vgg19-caffe2-8', 'vgg19-caffe2-9'], 'zfnet-512': ['zfnet512-3', 'zfnet512-6', 'zfnet512-7', 'zfnet512-8', 'zfnet512-9'], 'duc': ['ResNet101-DUC-7'], 'faster-rcnn': ['FasterRCNN-10'], 'mask-rcnn': ['MaskRCNN-10'], 'retinanet': ['retinanet-9'], 'ssd': ['ssd-10'], 'ssd-mobilenetv1': ['ssd_mobilenet_v1_10'], 'tiny-yolov2': ['tinyyolov2-1', 'tinyyolov2-7', 'tinyyolov2-8'], 'tiny-yolov3': ['tiny-yolov3-11'], 'yolov2': ['yolov2-voc-8'], 'yolov2-coco': ['yolov2-coco-9'], 'yolov3': ['yolov3-10'], 'yolov4': ['yolov4'], 'fast_neural_style': ['candy-8', 'candy-9', 'mosaic-8', 'mosaic-9', 'pointilism-8', 'pointilism-9', 'rain-princess-8', 'rain-princess-9', 'udnie-8', 'udnie-9'], 'sub_pixel_cnn_2016': ['super-resolution-10'], 'roberta': ['roberta-base-11', 'roberta-sequence-classification-9']}
Loading