Esempio n. 1
0
        ///<summary>
        ///Downloads model from Coach service to disk
        ///<param>Name of model</param>
        ///<param>Path to cache model</param>
        ///<param>If true, the download will be skipped if the model of the same filename already exists</param>
        ///<param>The type of model to be cached, default is Unity</param>
        ///</summary>
        public async Task CacheModel(string modelName, string path = ".", bool skipMatch = true, ModelType modelType = ModelType.Unity)
        {
            if (path == ".")
            {
                path = Application.persistentDataPath;
            }

            if (!IsAuthenticated())
            {
                throw new Exception("User is not authenticated");
            }

            ModelDef model = this.Profile.models.SingleOrDefault(m => m.name == modelName);

            if (model == null)
            {
                throw new Exception($"{modelName} is an invalid model");
            }

            int version = model.version;

            string modelDir        = Path.Combine(path, modelName);
            string profileManifest = Path.Combine(modelDir, "manifest.json");

            if (File.Exists(profileManifest))
            {
                // Load existing model manifest
                ModelDef manifest = ReadManifest(profileManifest);

                int manifestVersion = manifest.version;
                int profileVersion  = model.version;

                if (profileVersion == manifestVersion && skipMatch)
                {
                    if (this.IsDebug)
                    {
                        Console.WriteLine("Version match, skipping model download");
                    }
                    return;
                }
            }
            else if (!Directory.Exists(modelDir))
            {
                Directory.CreateDirectory(modelDir);
            }

            // Write downloaded manifest
            var json = model.ToJson();

            File.WriteAllText(profileManifest, json);

            var baseUrl = $"https://la41byvnkj.execute-api.us-east-1.amazonaws.com/prod/{this.Profile.bucket}/model-bin?object=trained/{modelName}/{version}/model";

            string modelFile = String.Empty;

            if (modelType == ModelType.Frozen)
            {
                modelFile = "frozen.pb";
            }
            else if (modelType == ModelType.Unity)
            {
                modelFile = "unity.bytes";
            }
            else if (modelType == ModelType.Mobile)
            {
                modelFile = "mobile.tflite";
            }

            var modelUrl = $"{baseUrl}/{modelFile}";

            byte[] modelBytes = await Networking.GetContentAsync(modelUrl, ApiKey);

            var writePath = Path.Combine(path, modelName, modelFile);

            File.WriteAllBytes(writePath, modelBytes);
        }
Esempio n. 2
0
        private ModelDef ReadManifest(string path)
        {
            var json = File.ReadAllText(path);

            return(ModelDef.FromJson(json));
        }