File size: 1,594 Bytes
6ac63e1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #if UNITY_EDITOR
using System;
using Unity.InferenceEngine;
using UnityEditor;
using UnityEngine;
// A custom editor window to demonstrate saving an onnx model as a Quantized .sentis model
public class QuantizeAndSaveModel : EditorWindow
{
public ModelAsset modelAsset;
public QuantizationType quantizationType;
[MenuItem("Sentis/Sample/Quantize and save model")]
public static void ShowExample()
{
QuantizeAndSaveModel wnd = GetWindow<QuantizeAndSaveModel>();
wnd.titleContent = new GUIContent("Quantize and save model");
}
void OnGUI()
{
EditorGUILayout.BeginHorizontal();
modelAsset = EditorGUILayout.ObjectField("Source model", modelAsset, typeof(ModelAsset), true) as ModelAsset;
EditorGUILayout.EndHorizontal();
quantizationType = (QuantizationType)EditorGUILayout.EnumPopup("Quantization type", quantizationType);
GUILayout.Space(10);
if (!GUILayout.Button("Quantize and save") || modelAsset == null)
return;
var path = EditorUtility.SaveFilePanel("Save Quantized model", "", modelAsset.name + "_" + Enum.GetName(typeof(QuantizationType), quantizationType), "sentis");
if (string.IsNullOrEmpty(path))
return;
// Load model using ModelLoader.
var model = ModelLoader.Load(modelAsset);
// Quantize model using ModelQuantizer.
ModelQuantizer.QuantizeWeights(quantizationType, ref model);
// Save model using ModelWriter.
ModelWriter.Save(path, model);
AssetDatabase.Refresh();
}
}
#endif
|