44using Microsoft . ML . OnnxRuntime ;
55using System . IO ;
66using System . Reflection ;
7+ using Microsoft . Windows . AI . MachineLearning ;
78
89namespace WindowsML . Shared
910{
@@ -112,14 +113,73 @@ public static string GetModelVariantPath(string executableFolder, ModelVariant v
112113 /// <summary>
113114 /// Resolve model paths with intelligent variant selection
114115 /// </summary>
115- public static ( string modelPath , string compiledModelPath , string labelsPath ) ResolvePaths ( Options options , OrtEnv ortEnv )
116+ public static async Task < ( string modelPath , string compiledModelPath , string labelsPath ) > ResolvePaths ( Options options , OrtEnv ortEnv )
116117 {
117118 string executableFolder = Path . GetDirectoryName ( Assembly . GetEntryAssembly ( ) ! . Location ) ! ;
118-
119119 string modelPath ;
120-
121- // Check if user specified a custom model path
122- if ( ! string . IsNullOrWhiteSpace ( options . ModelPath ) )
120+ string labelsPath = Path . Combine ( executableFolder , "SqueezeNet.Labels.txt" ) ;
121+
122+ if ( options . UseModelCatalog )
123+ {
124+ Console . WriteLine ( "Using model catalog..." ) ;
125+
126+ // Build source
127+ string sampleCatalogJsonPath = Path . Combine ( executableFolder , "SqueezeNetModelCatalog.json" ) ;
128+
129+ // Use intelligent model variant selection based on execution provider and device capabilities
130+ ModelVariant actualVariant = DetermineModelVariant ( options , ortEnv ) ;
131+ if ( File . Exists ( sampleCatalogJsonPath ) )
132+ {
133+ var uri = new System . Uri ( sampleCatalogJsonPath ) ;
134+ var sampleCatalogSource = await ModelCatalogSource . CreateFromUriAsync ( uri ) ;
135+ ModelCatalog modelCatalog = new ModelCatalog ( new [ ] { sampleCatalogSource } ) ;
136+ CatalogModelInfo modelFromCatalog ;
137+ string modelVariantName = ( actualVariant == ModelVariant . FP32 ) ? "squeezenet-fp32" : "squeezenet" ;
138+ modelFromCatalog = await modelCatalog . FindModelAsync ( modelVariantName ) ;
139+ if ( modelFromCatalog != null )
140+ {
141+ var additionalHeaders = new Dictionary < string , string > ( ) ;
142+ var catalogModelInstanceOp = modelFromCatalog . GetInstanceAsync ( additionalHeaders ) ;
143+
144+ catalogModelInstanceOp . Progress += ( operation , progress ) =>
145+ {
146+ Console . Write ( $ "Model download progress: { progress } %\r ") ;
147+ } ;
148+
149+ var catalogModelInstanceResult = await catalogModelInstanceOp ;
150+
151+ if ( catalogModelInstanceResult . Status == CatalogModelInstanceStatus . Available )
152+ {
153+ using var catalogModelInstance = catalogModelInstanceResult . GetInstance ( ) ;
154+ var modelPaths = catalogModelInstance . ModelPaths ;
155+
156+ string modelFolderPath = modelPaths [ 0 ] ;
157+ string modelName = $ "{ modelVariantName } .onnx";
158+ modelPath = Path . Combine ( modelFolderPath , modelName ) ;
159+ Console . WriteLine ( $ "Using model from catalog at: { modelPath } ") ;
160+
161+ // Get labels
162+ labelsPath = Path . Combine ( modelFolderPath , "SqueezeNet.Labels.txt" ) ;
163+ }
164+ else
165+ {
166+ Console . WriteLine ( "Model download failed. Falling back to executableFolder" ) ;
167+ modelPath = GetModelVariantPath ( executableFolder , actualVariant ) ;
168+ }
169+ }
170+ else
171+ {
172+ Console . WriteLine ( $ "Model with alias or ID '{ modelVariantName } ' not found in catalog. Falling back to executableFolder") ;
173+ modelPath = GetModelVariantPath ( executableFolder , actualVariant ) ;
174+ }
175+ }
176+ else
177+ {
178+ Console . WriteLine ( "Model catalog JSON file not found. Falling back to executableFolder" ) ;
179+ modelPath = GetModelVariantPath ( executableFolder , actualVariant ) ;
180+ }
181+ }
182+ else if ( ! string . IsNullOrWhiteSpace ( options . ModelPath ) )
123183 {
124184 // User provided custom model path - use it as-is
125185 modelPath = options . ModelPath . Contains ( Path . DirectorySeparatorChar ) ?
@@ -135,8 +195,6 @@ public static (string modelPath, string compiledModelPath, string labelsPath) Re
135195 string compiledModelPath = options . OutputPath . Contains ( Path . DirectorySeparatorChar ) ?
136196 options . OutputPath : Path . Combine ( executableFolder , options . OutputPath ) ;
137197
138- string labelsPath = Path . Combine ( executableFolder , "SqueezeNet.Labels.txt" ) ;
139-
140198 if ( ! File . Exists ( labelsPath ) )
141199 {
142200 throw new FileNotFoundException ( $ "Labels file not found: { labelsPath } ") ;
0 commit comments