< Summary

Information
Class: InferenceEngine.Core.ModelProviders.ModelRegistryData
Assembly: InferenceEngine.Core
File(s): /home/runner/work/inference-engine-lib/inference-engine-lib/InferenceEngine.Core/ModelProviders/ModelRegistry.cs
Line coverage
100%
Covered lines: 1
Uncovered lines: 0
Coverable lines: 1
Total lines: 270
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Models()100%11100%

File(s)

/home/runner/work/inference-engine-lib/inference-engine-lib/InferenceEngine.Core/ModelProviders/ModelRegistry.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Linq;
 5using System.Text.Json;
 6using Microsoft.Extensions.Logging;
 7using Microsoft.Extensions.Logging.Abstractions;
 8
 9namespace InferenceEngine.Core.ModelProviders
 10{
 11    /// <summary>
 12    /// Registry of known models and their download URLs from popular model hubs.
 13    /// Allows engines to specify only a model name for commonly known models.
 14    /// </summary>
 15    public class ModelRegistry
 16    {
 17        private readonly Dictionary<string, ModelRegistryEntry> _entries;
 18        private readonly ILogger _logger;
 19        private static readonly string DefaultRegistryPath = Path.Combine(
 20            AppContext.BaseDirectory,
 21            "models.json");
 22
 23        /// <summary>
 24        /// Creates a model registry from a JSON file.
 25        /// </summary>
 26        /// <param name="registryPath">Path to the JSON registry file. Uses default if null.</param>
 27        /// <param name="logger">Optional logger</param>
 28        public ModelRegistry(string? registryPath = null, ILogger? logger = null)
 29        {
 30            _logger = logger ?? NullLogger.Instance;
 31            var path = registryPath ?? DefaultRegistryPath;
 32            _entries = LoadRegistry(path);
 33        }
 34
 35        /// <summary>
 36        /// Gets download URLs for a model by name.
 37        /// </summary>
 38        /// <param name="modelName">Name of the model file (e.g., "yolo11n.onnx")</param>
 39        /// <returns>Array of URLs to download the model from, or null if not found</returns>
 40        public string[]? GetModelUrls(string modelName)
 41        {
 42            if (_entries.TryGetValue(modelName, out var entry))
 43            {
 44                _logger.LogDebug("Found model '{ModelName}' in registry with {UrlCount} source(s)", modelName, entry.Url
 45                return entry.Urls;
 46            }
 47
 48            _logger.LogDebug("Model '{ModelName}' not found in registry", modelName);
 49            return null;
 50        }
 51
 52        /// <summary>
 53        /// Gets the full registry entry for a model by name.
 54        /// </summary>
 55        /// <param name="modelName">Name of the model file (e.g., "yolo11n.onnx")</param>
 56        /// <returns>The registry entry, or null if not found</returns>
 57        public ModelRegistryEntry? GetModelEntry(string modelName)
 58        {
 59            return _entries.TryGetValue(modelName, out var entry) ? entry : null;
 60        }
 61
 62        /// <summary>
 63        /// Checks if a model is registered.
 64        /// </summary>
 65        /// <param name="modelName">Name of the model file</param>
 66        /// <returns>True if the model is in the registry</returns>
 67        public bool IsModelRegistered(string modelName)
 68        {
 69            return _entries.ContainsKey(modelName);
 70        }
 71
 72        /// <summary>
 73        /// Gets all registered model names.
 74        /// </summary>
 75        /// <returns>Collection of registered model names</returns>
 76        public IReadOnlyCollection<string> GetRegisteredModels()
 77        {
 78            return _entries.Keys;
 79        }
 80
 81        private Dictionary<string, ModelRegistryEntry> LoadRegistry(string path)
 82        {
 83            if (!File.Exists(path))
 84            {
 85                _logger.LogWarning("Model registry file not found at '{Path}'. Using empty registry.", path);
 86                return new Dictionary<string, ModelRegistryEntry>(StringComparer.OrdinalIgnoreCase);
 87            }
 88
 89            try
 90            {
 91                var json = File.ReadAllText(path);
 92                var registry = JsonSerializer.Deserialize<ModelRegistryData>(json, new JsonSerializerOptions
 93                {
 94                    PropertyNameCaseInsensitive = true
 95                });
 96
 97                if (registry?.Models == null)
 98                {
 99                    _logger.LogWarning("Model registry file is empty or invalid. Using empty registry.");
 100                    return new Dictionary<string, ModelRegistryEntry>(StringComparer.OrdinalIgnoreCase);
 101                }
 102
 103                var entries = new Dictionary<string, ModelRegistryEntry>(StringComparer.OrdinalIgnoreCase);
 104                foreach (var entry in registry.Models)
 105                {
 106                    if (string.IsNullOrWhiteSpace(entry.Name) || entry.Urls == null || entry.Urls.Length == 0)
 107                    {
 108                        _logger.LogWarning("Skipping invalid registry entry: missing name or URLs");
 109                        continue;
 110                    }
 111
 112                    entries[entry.Name] = entry;
 113                }
 114
 115                _logger.LogInformation("Loaded {Count} models from registry", entries.Count);
 116                return entries;
 117            }
 118            catch (JsonException ex)
 119            {
 120                _logger.LogError(ex, "Failed to parse model registry JSON");
 121                return new Dictionary<string, ModelRegistryEntry>(StringComparer.OrdinalIgnoreCase);
 122            }
 123            catch (Exception ex)
 124            {
 125                _logger.LogError(ex, "Failed to load model registry from '{Path}'", path);
 126                return new Dictionary<string, ModelRegistryEntry>(StringComparer.OrdinalIgnoreCase);
 127            }
 128        }
 129
 130        /// <summary>
 131        /// Creates a default registry file with common models if it doesn't exist.
 132        /// </summary>
 133        /// <param name="path">Path to create the registry file</param>
 134        public static void CreateDefaultRegistry(string? path = null)
 135        {
 136            var registryPath = path ?? DefaultRegistryPath;
 137            if (File.Exists(registryPath))
 138            {
 139                return;
 140            }
 141
 142            var directory = Path.GetDirectoryName(registryPath);
 143            if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
 144            {
 145                Directory.CreateDirectory(directory);
 146            }
 147
 148            var defaultRegistry = new ModelRegistryData
 149            {
 150                Models = new[]
 151                {
 152                    new ModelRegistryEntry
 153                    {
 154                        Name = "yolo11n.onnx",
 155                        Description = "YOLO11 Nano object detection model",
 156                        Source = "Ultralytics",
 157                        Urls = new[]
 158                        {
 159                            "https://github.com/ultralytics/assets/releases/download/v8.3.0/yolo11n.onnx",
 160                            "https://huggingface.co/ultralytics/YOLO11/resolve/main/yolo11n.onnx"
 161                        }
 162                    },
 163                    new ModelRegistryEntry
 164                    {
 165                        Name = "yolo11s.onnx",
 166                        Description = "YOLO11 Small object detection model",
 167                        Source = "Ultralytics",
 168                        Urls = new[]
 169                        {
 170                            "https://github.com/ultralytics/assets/releases/download/v8.3.0/yolo11s.onnx",
 171                            "https://huggingface.co/ultralytics/YOLO11/resolve/main/yolo11s.onnx"
 172                        }
 173                    },
 174                    new ModelRegistryEntry
 175                    {
 176                        Name = "yolo11m.onnx",
 177                        Description = "YOLO11 Medium object detection model",
 178                        Source = "Ultralytics",
 179                        Urls = new[]
 180                        {
 181                            "https://github.com/ultralytics/assets/releases/download/v8.3.0/yolo11m.onnx",
 182                            "https://huggingface.co/ultralytics/YOLO11/resolve/main/yolo11m.onnx"
 183                        }
 184                    },
 185                    new ModelRegistryEntry
 186                    {
 187                        Name = "yolov8n.onnx",
 188                        Description = "YOLOv8 Nano object detection model",
 189                        Source = "Ultralytics",
 190                        Urls = new[]
 191                        {
 192                            "https://github.com/ultralytics/assets/releases/download/v8.3.0/yolov8n.onnx",
 193                            "https://huggingface.co/ultralytics/YOLOv8/resolve/main/yolov8n.onnx"
 194                        }
 195                    },
 196                    new ModelRegistryEntry
 197                    {
 198                        Name = "yolov8n-face.onnx",
 199                        Description = "YOLOv8 Nano single-class face detection model",
 200                        Source = "akanametov/yolo-face",
 201                        Urls = new[]
 202                        {
 203                            "https://github.com/akanametov/yolo-face/releases/download/1.0.0/yolov8n-face.onnx"
 204                        }
 205                    },
 206                    new ModelRegistryEntry
 207                    {
 208                        Name = "arcface-recognition.onnx",
 209                        Description = "ArcFace ResNet50 face recognition (insightface buffalo_l w600k_r50), 512-dim embe
 210                        Source = "InsightFace via HuggingFace mirror richarrrddd/w600k_r50_v1",
 211                        Urls =
 212                        [
 213                            "https://huggingface.co/richarrrddd/w600k_r50_v1/resolve/main/w600k_r50.onnx"
 214                        ]
 215                    }
 216                }
 217            };
 218
 219            var json = JsonSerializer.Serialize(defaultRegistry, new JsonSerializerOptions
 220            {
 221                WriteIndented = true,
 222                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
 223            });
 224
 225            File.WriteAllText(registryPath, json);
 226        }
 227    }
 228
 229    /// <summary>
 230    /// Data class for the registry JSON structure.
 231    /// </summary>
 232    public class ModelRegistryData
 233    {
 90234        public ModelRegistryEntry[] Models { get; set; } = Array.Empty<ModelRegistryEntry>();
 235    }
 236
 237    /// <summary>
 238    /// Entry in the model registry.
 239    /// </summary>
 240    public class ModelRegistryEntry
 241    {
 242        /// <summary>
 243        /// Name of the model file (e.g., "yolo11n.onnx").
 244        /// </summary>
 245        public string Name { get; set; } = string.Empty;
 246
 247        /// <summary>
 248        /// Human-readable description of the model.
 249        /// </summary>
 250        public string Description { get; set; } = string.Empty;
 251
 252        /// <summary>
 253        /// Source organization or author of the model.
 254        /// </summary>
 255        public string Source { get; set; } = string.Empty;
 256
 257        /// <summary>
 258        /// URLs to download the model from, in priority order.
 259        /// </summary>
 260        public string[] Urls { get; set; } = Array.Empty<string>();
 261
 262        /// <summary>
 263        /// Optional expected SHA-256 of the model file, as 64 lowercase hex characters.
 264        /// When present, <see cref="DownloadingModelProvider"/> verifies the downloaded
 265        /// file against it and rejects mismatches; when absent, the download is accepted
 266        /// but logged as unverified.
 267        /// </summary>
 268        public string? Sha256 { get; set; }
 269    }
 270}

Methods/Properties

get_Models()