frist commit
343
.gitignore
vendored
Normal file
@ -0,0 +1,343 @@
|
|||||||
|
## Ignore Visual Studio temporary files, build results, and
|
||||||
|
## files generated by popular Visual Studio add-ons.
|
||||||
|
##
|
||||||
|
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
|
||||||
|
|
||||||
|
# User-specific files
|
||||||
|
*.rsuser
|
||||||
|
*.suo
|
||||||
|
*.user
|
||||||
|
*.userosscache
|
||||||
|
*.sln.docstates
|
||||||
|
|
||||||
|
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||||
|
*.userprefs
|
||||||
|
|
||||||
|
# Build results
|
||||||
|
[Dd]ebug/
|
||||||
|
[Dd]ebugPublic/
|
||||||
|
[Rr]elease/
|
||||||
|
[Rr]eleases/
|
||||||
|
x64/
|
||||||
|
x86/
|
||||||
|
[Aa][Rr][Mm]/
|
||||||
|
[Aa][Rr][Mm]64/
|
||||||
|
bld/
|
||||||
|
[Bb]in/
|
||||||
|
[Oo]bj/
|
||||||
|
[Ll]og/
|
||||||
|
|
||||||
|
# Visual Studio 2015/2017 cache/options directory
|
||||||
|
.vs/
|
||||||
|
# Uncomment if you have tasks that create the project's static files in wwwroot
|
||||||
|
#wwwroot/
|
||||||
|
|
||||||
|
# Visual Studio 2017 auto generated files
|
||||||
|
Generated\ Files/
|
||||||
|
|
||||||
|
# MSTest test Results
|
||||||
|
[Tt]est[Rr]esult*/
|
||||||
|
[Bb]uild[Ll]og.*
|
||||||
|
|
||||||
|
# NUNIT
|
||||||
|
*.VisualState.xml
|
||||||
|
TestResult.xml
|
||||||
|
|
||||||
|
# Build Results of an ATL Project
|
||||||
|
[Dd]ebugPS/
|
||||||
|
[Rr]eleasePS/
|
||||||
|
dlldata.c
|
||||||
|
|
||||||
|
# Benchmark Results
|
||||||
|
BenchmarkDotNet.Artifacts/
|
||||||
|
|
||||||
|
# .NET Core
|
||||||
|
project.lock.json
|
||||||
|
project.fragment.lock.json
|
||||||
|
artifacts/
|
||||||
|
|
||||||
|
# StyleCop
|
||||||
|
StyleCopReport.xml
|
||||||
|
|
||||||
|
# Files built by Visual Studio
|
||||||
|
*_i.c
|
||||||
|
*_p.c
|
||||||
|
*_h.h
|
||||||
|
*.ilk
|
||||||
|
*.meta
|
||||||
|
*.obj
|
||||||
|
*.iobj
|
||||||
|
*.pch
|
||||||
|
*.pdb
|
||||||
|
*.ipdb
|
||||||
|
*.pgc
|
||||||
|
*.pgd
|
||||||
|
*.rsp
|
||||||
|
*.sbr
|
||||||
|
*.tlb
|
||||||
|
*.tli
|
||||||
|
*.tlh
|
||||||
|
*.tmp
|
||||||
|
*.tmp_proj
|
||||||
|
*_wpftmp.csproj
|
||||||
|
*.log
|
||||||
|
*.vspscc
|
||||||
|
*.vssscc
|
||||||
|
.builds
|
||||||
|
*.pidb
|
||||||
|
*.svclog
|
||||||
|
*.scc
|
||||||
|
|
||||||
|
# Chutzpah Test files
|
||||||
|
_Chutzpah*
|
||||||
|
|
||||||
|
# Visual C++ cache files
|
||||||
|
ipch/
|
||||||
|
*.aps
|
||||||
|
*.ncb
|
||||||
|
*.opendb
|
||||||
|
*.opensdf
|
||||||
|
*.sdf
|
||||||
|
*.cachefile
|
||||||
|
*.VC.db
|
||||||
|
*.VC.VC.opendb
|
||||||
|
|
||||||
|
# Visual Studio profiler
|
||||||
|
*.psess
|
||||||
|
*.vsp
|
||||||
|
*.vspx
|
||||||
|
*.sap
|
||||||
|
|
||||||
|
# Visual Studio Trace Files
|
||||||
|
*.e2e
|
||||||
|
|
||||||
|
# TFS 2012 Local Workspace
|
||||||
|
$tf/
|
||||||
|
|
||||||
|
# Guidance Automation Toolkit
|
||||||
|
*.gpState
|
||||||
|
|
||||||
|
# ReSharper is a .NET coding add-in
|
||||||
|
_ReSharper*/
|
||||||
|
*.[Rr]e[Ss]harper
|
||||||
|
*.DotSettings.user
|
||||||
|
|
||||||
|
# JustCode is a .NET coding add-in
|
||||||
|
.JustCode
|
||||||
|
|
||||||
|
# TeamCity is a build add-in
|
||||||
|
_TeamCity*
|
||||||
|
|
||||||
|
# DotCover is a Code Coverage Tool
|
||||||
|
*.dotCover
|
||||||
|
|
||||||
|
# AxoCover is a Code Coverage Tool
|
||||||
|
.axoCover/*
|
||||||
|
!.axoCover/settings.json
|
||||||
|
|
||||||
|
# Visual Studio code coverage results
|
||||||
|
*.coverage
|
||||||
|
*.coveragexml
|
||||||
|
|
||||||
|
# NCrunch
|
||||||
|
_NCrunch_*
|
||||||
|
.*crunch*.local.xml
|
||||||
|
nCrunchTemp_*
|
||||||
|
|
||||||
|
# MightyMoose
|
||||||
|
*.mm.*
|
||||||
|
AutoTest.Net/
|
||||||
|
|
||||||
|
# Web workbench (sass)
|
||||||
|
.sass-cache/
|
||||||
|
|
||||||
|
# Installshield output folder
|
||||||
|
[Ee]xpress/
|
||||||
|
|
||||||
|
# DocProject is a documentation generator add-in
|
||||||
|
DocProject/buildhelp/
|
||||||
|
DocProject/Help/*.HxT
|
||||||
|
DocProject/Help/*.HxC
|
||||||
|
DocProject/Help/*.hhc
|
||||||
|
DocProject/Help/*.hhk
|
||||||
|
DocProject/Help/*.hhp
|
||||||
|
DocProject/Help/Html2
|
||||||
|
DocProject/Help/html
|
||||||
|
|
||||||
|
# Click-Once directory
|
||||||
|
publish/
|
||||||
|
|
||||||
|
# Publish Web Output
|
||||||
|
*.[Pp]ublish.xml
|
||||||
|
*.azurePubxml
|
||||||
|
# Note: Comment the next line if you want to checkin your web deploy settings,
|
||||||
|
# but database connection strings (with potential passwords) will be unencrypted
|
||||||
|
*.pubxml
|
||||||
|
*.publishproj
|
||||||
|
|
||||||
|
# Microsoft Azure Web App publish settings. Comment the next line if you want to
|
||||||
|
# checkin your Azure Web App publish settings, but sensitive information contained
|
||||||
|
# in these scripts will be unencrypted
|
||||||
|
PublishScripts/
|
||||||
|
|
||||||
|
# NuGet Packages
|
||||||
|
*.nupkg
|
||||||
|
# The packages folder can be ignored because of Package Restore
|
||||||
|
**/[Pp]ackages/*
|
||||||
|
# except build/, which is used as an MSBuild target.
|
||||||
|
!**/[Pp]ackages/build/
|
||||||
|
# Uncomment if necessary however generally it will be regenerated when needed
|
||||||
|
#!**/[Pp]ackages/repositories.config
|
||||||
|
# NuGet v3's project.json files produces more ignorable files
|
||||||
|
*.nuget.props
|
||||||
|
*.nuget.targets
|
||||||
|
|
||||||
|
# Microsoft Azure Build Output
|
||||||
|
csx/
|
||||||
|
*.build.csdef
|
||||||
|
|
||||||
|
# Microsoft Azure Emulator
|
||||||
|
ecf/
|
||||||
|
rcf/
|
||||||
|
|
||||||
|
# Windows Store app package directories and files
|
||||||
|
AppPackages/
|
||||||
|
BundleArtifacts/
|
||||||
|
Package.StoreAssociation.xml
|
||||||
|
_pkginfo.txt
|
||||||
|
*.appx
|
||||||
|
|
||||||
|
# Visual Studio cache files
|
||||||
|
# files ending in .cache can be ignored
|
||||||
|
*.[Cc]ache
|
||||||
|
# but keep track of directories ending in .cache
|
||||||
|
!?*.[Cc]ache/
|
||||||
|
|
||||||
|
# Others
|
||||||
|
ClientBin/
|
||||||
|
~$*
|
||||||
|
*~
|
||||||
|
*.dbmdl
|
||||||
|
*.dbproj.schemaview
|
||||||
|
*.jfm
|
||||||
|
*.pfx
|
||||||
|
*.publishsettings
|
||||||
|
orleans.codegen.cs
|
||||||
|
|
||||||
|
# Including strong name files can present a security risk
|
||||||
|
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
|
||||||
|
#*.snk
|
||||||
|
|
||||||
|
# Since there are multiple workflows, uncomment next line to ignore bower_components
|
||||||
|
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
|
||||||
|
#bower_components/
|
||||||
|
# ASP.NET Core default setup: bower directory is configured as wwwroot/lib/ and bower restore is true
|
||||||
|
**/wwwroot/lib/
|
||||||
|
|
||||||
|
# RIA/Silverlight projects
|
||||||
|
Generated_Code/
|
||||||
|
|
||||||
|
# Backup & report files from converting an old project file
|
||||||
|
# to a newer Visual Studio version. Backup files are not needed,
|
||||||
|
# because we have git ;-)
|
||||||
|
_UpgradeReport_Files/
|
||||||
|
Backup*/
|
||||||
|
UpgradeLog*.XML
|
||||||
|
UpgradeLog*.htm
|
||||||
|
ServiceFabricBackup/
|
||||||
|
*.rptproj.bak
|
||||||
|
|
||||||
|
# SQL Server files
|
||||||
|
*.mdf
|
||||||
|
*.ldf
|
||||||
|
*.ndf
|
||||||
|
|
||||||
|
# Business Intelligence projects
|
||||||
|
*.rdl.data
|
||||||
|
*.bim.layout
|
||||||
|
*.bim_*.settings
|
||||||
|
*.rptproj.rsuser
|
||||||
|
|
||||||
|
# Microsoft Fakes
|
||||||
|
FakesAssemblies/
|
||||||
|
|
||||||
|
# GhostDoc plugin setting file
|
||||||
|
*.GhostDoc.xml
|
||||||
|
|
||||||
|
# Node.js Tools for Visual Studio
|
||||||
|
.ntvs_analysis.dat
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# Visual Studio 6 build log
|
||||||
|
*.plg
|
||||||
|
|
||||||
|
# Visual Studio 6 workspace options file
|
||||||
|
*.opt
|
||||||
|
|
||||||
|
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
|
||||||
|
*.vbw
|
||||||
|
|
||||||
|
# Visual Studio LightSwitch build output
|
||||||
|
**/*.HTMLClient/GeneratedArtifacts
|
||||||
|
**/*.DesktopClient/GeneratedArtifacts
|
||||||
|
**/*.DesktopClient/ModelManifest.xml
|
||||||
|
**/*.Server/GeneratedArtifacts
|
||||||
|
**/*.Server/ModelManifest.xml
|
||||||
|
_Pvt_Extensions
|
||||||
|
|
||||||
|
# Paket dependency manager
|
||||||
|
.paket/paket.exe
|
||||||
|
paket-files/
|
||||||
|
|
||||||
|
# FAKE - F# Make
|
||||||
|
.fake/
|
||||||
|
|
||||||
|
# JetBrains Rider
|
||||||
|
.idea/
|
||||||
|
*.sln.iml
|
||||||
|
|
||||||
|
# CodeRush personal settings
|
||||||
|
.cr/personal
|
||||||
|
|
||||||
|
# Python Tools for Visual Studio (PTVS)
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
|
||||||
|
# Cake - Uncomment if you are using it
|
||||||
|
# tools/**
|
||||||
|
# !tools/packages.config
|
||||||
|
|
||||||
|
# Tabs Studio
|
||||||
|
*.tss
|
||||||
|
|
||||||
|
# Telerik's JustMock configuration file
|
||||||
|
*.jmconfig
|
||||||
|
|
||||||
|
# BizTalk build output
|
||||||
|
*.btp.cs
|
||||||
|
*.btm.cs
|
||||||
|
*.odx.cs
|
||||||
|
*.xsd.cs
|
||||||
|
|
||||||
|
# OpenCover UI analysis results
|
||||||
|
OpenCover/
|
||||||
|
|
||||||
|
# Azure Stream Analytics local run output
|
||||||
|
ASALocalRun/
|
||||||
|
|
||||||
|
# MSBuild Binary and Structured Log
|
||||||
|
*.binlog
|
||||||
|
|
||||||
|
# NVidia Nsight GPU debugger configuration file
|
||||||
|
*.nvuser
|
||||||
|
|
||||||
|
# MFractors (Xamarin productivity tool) working folder
|
||||||
|
.mfractor/
|
||||||
|
|
||||||
|
# Local History for Visual Studio
|
||||||
|
.localhistory/
|
||||||
|
|
||||||
|
# BeatPulse healthcheck temp database
|
||||||
|
healthchecksdb
|
||||||
|
|
||||||
|
|
BIN
ImageData/已戴/1.jpg
Normal file
After Width: | Height: | Size: 38 KiB |
BIN
ImageData/已戴/2.png
Normal file
After Width: | Height: | Size: 510 KiB |
BIN
ImageData/已戴/3.jpg
Normal file
After Width: | Height: | Size: 150 KiB |
BIN
ImageData/已戴/4.jpg
Normal file
After Width: | Height: | Size: 108 KiB |
BIN
ImageData/已戴/5.jpg
Normal file
After Width: | Height: | Size: 134 KiB |
BIN
ImageData/已戴/6.png
Normal file
After Width: | Height: | Size: 103 KiB |
BIN
ImageData/已戴/7.jpg
Normal file
After Width: | Height: | Size: 24 KiB |
BIN
ImageData/未戴/no1.jpg
Normal file
After Width: | Height: | Size: 36 KiB |
BIN
ImageData/未戴/no2.jpg
Normal file
After Width: | Height: | Size: 44 KiB |
BIN
ImageData/未戴/no3.jpg
Normal file
After Width: | Height: | Size: 45 KiB |
BIN
ImageData/未戴/no4.jpg
Normal file
After Width: | Height: | Size: 48 KiB |
BIN
ImageData/未戴/no5.jpg
Normal file
After Width: | Height: | Size: 69 KiB |
BIN
ImageData/未戴/no6.jpg
Normal file
After Width: | Height: | Size: 54 KiB |
78
Mask.ML.WebApi/Controllers/DiscernController.cs
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
using Mask_MLML.Model;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Mask.ML.WebApi.Controllers
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
public class DiscernController : ControllerBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 上传文件:口罩验证
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stream"></param>
|
||||||
|
/// <param name="fileName"></param>
|
||||||
|
[HttpPost("upload")]
|
||||||
|
[Route("api/discern/upload")]
|
||||||
|
public IActionResult UploadFile([FromForm] IFormCollection collection)
|
||||||
|
{
|
||||||
|
//申明返回的结果
|
||||||
|
string result = "";
|
||||||
|
FormFileCollection filelist = (FormFileCollection)collection.Files;
|
||||||
|
//检查是否有文件提交上来
|
||||||
|
if (filelist != null && filelist.Any())
|
||||||
|
{
|
||||||
|
//我们只做第一个文件的检查
|
||||||
|
IFormFile file = filelist[0];
|
||||||
|
//做随机数,用到文件夹名字上,防重名
|
||||||
|
Random random = new Random();
|
||||||
|
string r = "";
|
||||||
|
int i;
|
||||||
|
for (i = 1; i < 11; i++)
|
||||||
|
{
|
||||||
|
r += random.Next(0, 9).ToString();
|
||||||
|
}
|
||||||
|
//文件路径
|
||||||
|
string FilePath = AppDomain.CurrentDomain.BaseDirectory+"/TempFiles/";
|
||||||
|
string name = file.FileName;
|
||||||
|
string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + r;
|
||||||
|
//获取文件类型
|
||||||
|
string type = System.IO.Path.GetExtension(name);
|
||||||
|
DirectoryInfo di = new DirectoryInfo(FilePath);
|
||||||
|
if (!di.Exists)
|
||||||
|
{
|
||||||
|
di.Create();
|
||||||
|
}
|
||||||
|
//文件保存的路径
|
||||||
|
var filefullname = FilePath + FileName + type;
|
||||||
|
using (FileStream fs = System.IO.File.Create(filefullname))
|
||||||
|
{
|
||||||
|
// 复制文件
|
||||||
|
file.CopyTo(fs);
|
||||||
|
// 清空缓冲区数据
|
||||||
|
fs.Flush();
|
||||||
|
fs.Close();
|
||||||
|
fs.Dispose();
|
||||||
|
}
|
||||||
|
//成功提示赋值到返回结果中
|
||||||
|
//result = "文件上传成功";
|
||||||
|
|
||||||
|
// 创建样例数据的单个实例对模型输入数据集的第一行
|
||||||
|
ModelInput sampleData = new ModelInput()
|
||||||
|
{
|
||||||
|
ImageSource = filefullname,
|
||||||
|
};
|
||||||
|
// 获取预测结果
|
||||||
|
var predictionResult = ConsumeModel.Predict(sampleData);
|
||||||
|
//System.IO.File.Delete(filefullname);
|
||||||
|
return Ok(predictionResult.Prediction);
|
||||||
|
}
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
15
Mask.ML.WebApi/Mask.ML.WebApi.csproj
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Mask.MLML.Model\Mask.MLML.Model.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
26
Mask.ML.WebApi/Program.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Mask.ML.WebApi
|
||||||
|
{
|
||||||
|
public class Program
|
||||||
|
{
|
||||||
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
CreateHostBuilder(args).Build().Run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||||
|
Host.CreateDefaultBuilder(args)
|
||||||
|
.ConfigureWebHostDefaults(webBuilder =>
|
||||||
|
{
|
||||||
|
webBuilder.UseStartup<Startup>();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
23
Mask.ML.WebApi/Properties/launchSettings.json
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||||
|
"iisSettings": {
|
||||||
|
"windowsAuthentication": false,
|
||||||
|
"anonymousAuthentication": true,
|
||||||
|
"iisExpress": {
|
||||||
|
"applicationUrl": "http://localhost:16529",
|
||||||
|
"sslPort": 0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"profiles": {
|
||||||
|
"Mask.ML.WebApi": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": "true",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "",
|
||||||
|
"applicationUrl": "http://localhost:5000",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
50
Mask.ML.WebApi/Startup.cs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.OpenApi.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Mask.ML.WebApi
|
||||||
|
{
|
||||||
|
public class Startup
|
||||||
|
{
|
||||||
|
public Startup(IConfiguration configuration)
|
||||||
|
{
|
||||||
|
Configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IConfiguration Configuration { get; }
|
||||||
|
|
||||||
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
|
||||||
|
services.AddControllers();
|
||||||
|
}
|
||||||
|
|
||||||
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
|
{
|
||||||
|
if (env.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.UseDeveloperExceptionPage();
|
||||||
|
}
|
||||||
|
|
||||||
|
app.UseRouting();
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.UseEndpoints(endpoints =>
|
||||||
|
{
|
||||||
|
endpoints.MapControllers();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
Mask.ML.WebApi/appsettings.Development.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft": "Warning",
|
||||||
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
10
Mask.ML.WebApi/appsettings.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft": "Warning",
|
||||||
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*"
|
||||||
|
}
|
37
Mask.ML.sln
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 16
|
||||||
|
VisualStudioVersion = 16.0.31205.134
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mask.MLML.Model", "Mask.MLML.Model\Mask.MLML.Model.csproj", "{952B032F-6FFE-45E8-B7B0-5A806DDBD922}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mask.MLML.ConsoleApp", "Mask.MLML.ConsoleApp\Mask.MLML.ConsoleApp.csproj", "{B3C06518-648F-4430-815D-FEC905FE3DD7}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mask.ML.WebApi", "Mask.ML.WebApi\Mask.ML.WebApi.csproj", "{64704ACF-F0F9-4442-BEC5-3279C27B332C}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{952B032F-6FFE-45E8-B7B0-5A806DDBD922}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{952B032F-6FFE-45E8-B7B0-5A806DDBD922}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{952B032F-6FFE-45E8-B7B0-5A806DDBD922}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{952B032F-6FFE-45E8-B7B0-5A806DDBD922}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{B3C06518-648F-4430-815D-FEC905FE3DD7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{B3C06518-648F-4430-815D-FEC905FE3DD7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{B3C06518-648F-4430-815D-FEC905FE3DD7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{B3C06518-648F-4430-815D-FEC905FE3DD7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{64704ACF-F0F9-4442-BEC5-3279C27B332C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{64704ACF-F0F9-4442-BEC5-3279C27B332C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{64704ACF-F0F9-4442-BEC5-3279C27B332C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{64704ACF-F0F9-4442-BEC5-3279C27B332C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {657EECC1-C95E-4F8E-BD19-7DEF54F69392}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
20
Mask.MLML.ConsoleApp/Mask.MLML.ConsoleApp.csproj
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.ML" Version="1.5.0-preview2" />
|
||||||
|
<PackageReference Include="Microsoft.ML.ImageAnalytics" Version="1.5.0-preview2" />
|
||||||
|
<PackageReference Include="Microsoft.ML.Vision" Version="1.5.0-preview2" />
|
||||||
|
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="1.14.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Mask.MLML.Model\Mask.MLML.Model.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectCapability Include="ModelBuilderGenerated" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
161
Mask.MLML.ConsoleApp/ModelBuilder.cs
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
// This file was auto-generated by ML.NET Model Builder.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.ML;
|
||||||
|
using Microsoft.ML.Data;
|
||||||
|
using Mask_MLML.Model;
|
||||||
|
using Microsoft.ML.Vision;
|
||||||
|
|
||||||
|
namespace Mask_MLML.ConsoleApp
|
||||||
|
{
|
||||||
|
public static class ModelBuilder
|
||||||
|
{
|
||||||
|
private static string TRAIN_DATA_FILEPATH = @"C:\Users\HUAWEI\AppData\Local\Temp\b441e20c-6f79-4437-a21a-be0014456e13.tsv";
|
||||||
|
private static string MODEL_FILEPATH = @"C:\Users\HUAWEI\AppData\Local\Temp\MLVSTools\Mask.MLML\Mask.MLML.Model\MLModel.zip";
|
||||||
|
// Create MLContext to be shared across the model creation workflow objects
|
||||||
|
// Set a random seed for repeatable/deterministic results across multiple trainings.
|
||||||
|
private static MLContext mlContext = new MLContext(seed: 1);
|
||||||
|
|
||||||
|
public static void CreateModel()
|
||||||
|
{
|
||||||
|
// Load Data
|
||||||
|
IDataView trainingDataView = mlContext.Data.LoadFromTextFile<ModelInput>(
|
||||||
|
path: TRAIN_DATA_FILEPATH,
|
||||||
|
hasHeader: true,
|
||||||
|
separatorChar: '\t',
|
||||||
|
allowQuoting: true,
|
||||||
|
allowSparse: false);
|
||||||
|
|
||||||
|
// Build training pipeline
|
||||||
|
IEstimator<ITransformer> trainingPipeline = BuildTrainingPipeline(mlContext);
|
||||||
|
|
||||||
|
// Train Model
|
||||||
|
ITransformer mlModel = TrainModel(mlContext, trainingDataView, trainingPipeline);
|
||||||
|
|
||||||
|
// Evaluate quality of Model
|
||||||
|
Evaluate(mlContext, trainingDataView, trainingPipeline);
|
||||||
|
|
||||||
|
// Save model
|
||||||
|
SaveModel(mlContext, mlModel, MODEL_FILEPATH, trainingDataView.Schema);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEstimator<ITransformer> BuildTrainingPipeline(MLContext mlContext)
|
||||||
|
{
|
||||||
|
// Data process configuration with pipeline data transformations
|
||||||
|
var dataProcessPipeline = mlContext.Transforms.Conversion.MapValueToKey("Label", "Label")
|
||||||
|
.Append(mlContext.Transforms.LoadRawImageBytes("ImageSource_featurized", null, "ImageSource"))
|
||||||
|
.Append(mlContext.Transforms.CopyColumns("Features", "ImageSource_featurized"));
|
||||||
|
// Set the training algorithm
|
||||||
|
var trainer = mlContext.MulticlassClassification.Trainers.ImageClassification(new ImageClassificationTrainer.Options() { LabelColumnName = "Label", FeatureColumnName = "Features" })
|
||||||
|
.Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel", "PredictedLabel"));
|
||||||
|
|
||||||
|
var trainingPipeline = dataProcessPipeline.Append(trainer);
|
||||||
|
|
||||||
|
return trainingPipeline;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ITransformer TrainModel(MLContext mlContext, IDataView trainingDataView, IEstimator<ITransformer> trainingPipeline)
|
||||||
|
{
|
||||||
|
Console.WriteLine("=============== Training model ===============");
|
||||||
|
|
||||||
|
ITransformer model = trainingPipeline.Fit(trainingDataView);
|
||||||
|
|
||||||
|
Console.WriteLine("=============== End of training process ===============");
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Evaluate(MLContext mlContext, IDataView trainingDataView, IEstimator<ITransformer> trainingPipeline)
|
||||||
|
{
|
||||||
|
// Cross-Validate with single dataset (since we don't have two datasets, one for training and for evaluate)
|
||||||
|
// in order to evaluate and get the model's accuracy metrics
|
||||||
|
Console.WriteLine("=============== Cross-validating to get model's accuracy metrics ===============");
|
||||||
|
var crossValidationResults = mlContext.MulticlassClassification.CrossValidate(trainingDataView, trainingPipeline, numberOfFolds: 5, labelColumnName: "Label");
|
||||||
|
PrintMulticlassClassificationFoldsAverageMetrics(crossValidationResults);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void SaveModel(MLContext mlContext, ITransformer mlModel, string modelRelativePath, DataViewSchema modelInputSchema)
|
||||||
|
{
|
||||||
|
// Save/persist the trained model to a .ZIP file
|
||||||
|
Console.WriteLine($"=============== Saving the model ===============");
|
||||||
|
mlContext.Model.Save(mlModel, modelInputSchema, GetAbsolutePath(modelRelativePath));
|
||||||
|
Console.WriteLine("The model is saved to {0}", GetAbsolutePath(modelRelativePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetAbsolutePath(string relativePath)
|
||||||
|
{
|
||||||
|
FileInfo _dataRoot = new FileInfo(typeof(Program).Assembly.Location);
|
||||||
|
string assemblyFolderPath = _dataRoot.Directory.FullName;
|
||||||
|
|
||||||
|
string fullPath = Path.Combine(assemblyFolderPath, relativePath);
|
||||||
|
|
||||||
|
return fullPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void PrintMulticlassClassificationMetrics(MulticlassClassificationMetrics metrics)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"************************************************************");
|
||||||
|
Console.WriteLine($"* Metrics for multi-class classification model ");
|
||||||
|
Console.WriteLine($"*-----------------------------------------------------------");
|
||||||
|
Console.WriteLine($" MacroAccuracy = {metrics.MacroAccuracy:0.####}, a value between 0 and 1, the closer to 1, the better");
|
||||||
|
Console.WriteLine($" MicroAccuracy = {metrics.MicroAccuracy:0.####}, a value between 0 and 1, the closer to 1, the better");
|
||||||
|
Console.WriteLine($" LogLoss = {metrics.LogLoss:0.####}, the closer to 0, the better");
|
||||||
|
for (int i = 0; i < metrics.PerClassLogLoss.Count; i++)
|
||||||
|
{
|
||||||
|
Console.WriteLine($" LogLoss for class {i + 1} = {metrics.PerClassLogLoss[i]:0.####}, the closer to 0, the better");
|
||||||
|
}
|
||||||
|
Console.WriteLine($"************************************************************");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void PrintMulticlassClassificationFoldsAverageMetrics(IEnumerable<TrainCatalogBase.CrossValidationResult<MulticlassClassificationMetrics>> crossValResults)
|
||||||
|
{
|
||||||
|
var metricsInMultipleFolds = crossValResults.Select(r => r.Metrics);
|
||||||
|
|
||||||
|
var microAccuracyValues = metricsInMultipleFolds.Select(m => m.MicroAccuracy);
|
||||||
|
var microAccuracyAverage = microAccuracyValues.Average();
|
||||||
|
var microAccuraciesStdDeviation = CalculateStandardDeviation(microAccuracyValues);
|
||||||
|
var microAccuraciesConfidenceInterval95 = CalculateConfidenceInterval95(microAccuracyValues);
|
||||||
|
|
||||||
|
var macroAccuracyValues = metricsInMultipleFolds.Select(m => m.MacroAccuracy);
|
||||||
|
var macroAccuracyAverage = macroAccuracyValues.Average();
|
||||||
|
var macroAccuraciesStdDeviation = CalculateStandardDeviation(macroAccuracyValues);
|
||||||
|
var macroAccuraciesConfidenceInterval95 = CalculateConfidenceInterval95(macroAccuracyValues);
|
||||||
|
|
||||||
|
var logLossValues = metricsInMultipleFolds.Select(m => m.LogLoss);
|
||||||
|
var logLossAverage = logLossValues.Average();
|
||||||
|
var logLossStdDeviation = CalculateStandardDeviation(logLossValues);
|
||||||
|
var logLossConfidenceInterval95 = CalculateConfidenceInterval95(logLossValues);
|
||||||
|
|
||||||
|
var logLossReductionValues = metricsInMultipleFolds.Select(m => m.LogLossReduction);
|
||||||
|
var logLossReductionAverage = logLossReductionValues.Average();
|
||||||
|
var logLossReductionStdDeviation = CalculateStandardDeviation(logLossReductionValues);
|
||||||
|
var logLossReductionConfidenceInterval95 = CalculateConfidenceInterval95(logLossReductionValues);
|
||||||
|
|
||||||
|
Console.WriteLine($"*************************************************************************************************************");
|
||||||
|
Console.WriteLine($"* Metrics for Multi-class Classification model ");
|
||||||
|
Console.WriteLine($"*------------------------------------------------------------------------------------------------------------");
|
||||||
|
Console.WriteLine($"* Average MicroAccuracy: {microAccuracyAverage:0.###} - Standard deviation: ({microAccuraciesStdDeviation:#.###}) - Confidence Interval 95%: ({microAccuraciesConfidenceInterval95:#.###})");
|
||||||
|
Console.WriteLine($"* Average MacroAccuracy: {macroAccuracyAverage:0.###} - Standard deviation: ({macroAccuraciesStdDeviation:#.###}) - Confidence Interval 95%: ({macroAccuraciesConfidenceInterval95:#.###})");
|
||||||
|
Console.WriteLine($"* Average LogLoss: {logLossAverage:#.###} - Standard deviation: ({logLossStdDeviation:#.###}) - Confidence Interval 95%: ({logLossConfidenceInterval95:#.###})");
|
||||||
|
Console.WriteLine($"* Average LogLossReduction: {logLossReductionAverage:#.###} - Standard deviation: ({logLossReductionStdDeviation:#.###}) - Confidence Interval 95%: ({logLossReductionConfidenceInterval95:#.###})");
|
||||||
|
Console.WriteLine($"*************************************************************************************************************");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double CalculateStandardDeviation(IEnumerable<double> values)
|
||||||
|
{
|
||||||
|
double average = values.Average();
|
||||||
|
double sumOfSquaresOfDifferences = values.Select(val => (val - average) * (val - average)).Sum();
|
||||||
|
double standardDeviation = Math.Sqrt(sumOfSquaresOfDifferences / (values.Count() - 1));
|
||||||
|
return standardDeviation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double CalculateConfidenceInterval95(IEnumerable<double> values)
|
||||||
|
{
|
||||||
|
double confidenceInterval95 = 1.96 * CalculateStandardDeviation(values) / Math.Sqrt((values.Count() - 1));
|
||||||
|
return confidenceInterval95;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
28
Mask.MLML.ConsoleApp/Program.cs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
// This file was auto-generated by ML.NET Model Builder.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using Mask_MLML.Model;
|
||||||
|
|
||||||
|
namespace Mask_MLML.ConsoleApp
|
||||||
|
{
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
// Create single instance of sample data from first line of dataset for model input
|
||||||
|
ModelInput sampleData = new ModelInput()
|
||||||
|
{
|
||||||
|
ImageSource = @"D:\LancerProject\VsProject\Mask.ML\ImageData\已戴\1.jpg",
|
||||||
|
};
|
||||||
|
|
||||||
|
// Make a single prediction on the sample data and print results
|
||||||
|
var predictionResult = ConsumeModel.Predict(sampleData);
|
||||||
|
|
||||||
|
Console.WriteLine("Using model to make single prediction -- Comparing actual Label with predicted Label from sample data...\n\n");
|
||||||
|
Console.WriteLine($"ImageSource: {sampleData.ImageSource}");
|
||||||
|
Console.WriteLine($"\n\nPredicted Label value {predictionResult.Prediction} \nPredicted Label scores: [{String.Join(",", predictionResult.Score)}]\n\n");
|
||||||
|
Console.WriteLine("=============== End of process, hit any key to finish ===============");
|
||||||
|
Console.ReadKey();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
37
Mask.MLML.Model/ConsumeModel.cs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// This file was auto-generated by ML.NET Model Builder.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.ML;
|
||||||
|
using Mask_MLML.Model;
|
||||||
|
|
||||||
|
namespace Mask_MLML.Model
|
||||||
|
{
|
||||||
|
public class ConsumeModel
|
||||||
|
{
|
||||||
|
private static Lazy<PredictionEngine<ModelInput, ModelOutput>> PredictionEngine = new Lazy<PredictionEngine<ModelInput, ModelOutput>>(CreatePredictionEngine);
|
||||||
|
|
||||||
|
// For more info on consuming ML.NET models, visit https://aka.ms/mlnet-consume
|
||||||
|
// Method for consuming model in your app
|
||||||
|
public static ModelOutput Predict(ModelInput input)
|
||||||
|
{
|
||||||
|
ModelOutput result = PredictionEngine.Value.Predict(input);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PredictionEngine<ModelInput, ModelOutput> CreatePredictionEngine()
|
||||||
|
{
|
||||||
|
// Create new MLContext
|
||||||
|
MLContext mlContext = new MLContext();
|
||||||
|
|
||||||
|
// Load model & create prediction engine
|
||||||
|
string modelPath = @"C:\Users\HUAWEI\AppData\Local\Temp\MLVSTools\Mask.MLML\Mask.MLML.Model\MLModel.zip";
|
||||||
|
ITransformer mlModel = mlContext.Model.Load(modelPath, out var modelInputSchema);
|
||||||
|
var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
|
||||||
|
|
||||||
|
return predEngine;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
BIN
Mask.MLML.Model/MLModel.zip
Normal file
22
Mask.MLML.Model/Mask.MLML.Model.csproj
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.ML" Version="1.5.0-preview2" />
|
||||||
|
<PackageReference Include="Microsoft.ML.ImageAnalytics" Version="1.5.0-preview2" />
|
||||||
|
<PackageReference Include="Microsoft.ML.Vision" Version="1.5.0-preview2" />
|
||||||
|
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="1.14.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="MLModel.zip">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectCapability Include="ModelBuilderGenerated" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
18
Mask.MLML.Model/ModelInput.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
// This file was auto-generated by ML.NET Model Builder.
|
||||||
|
|
||||||
|
using Microsoft.ML.Data;
|
||||||
|
|
||||||
|
namespace Mask_MLML.Model
|
||||||
|
{
|
||||||
|
public class ModelInput
|
||||||
|
{
|
||||||
|
[ColumnName("Label"), LoadColumn(0)]
|
||||||
|
public string Label { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
[ColumnName("ImageSource"), LoadColumn(1)]
|
||||||
|
public string ImageSource { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
16
Mask.MLML.Model/ModelOutput.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// This file was auto-generated by ML.NET Model Builder.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using Microsoft.ML.Data;
|
||||||
|
|
||||||
|
namespace Mask_MLML.Model
|
||||||
|
{
|
||||||
|
public class ModelOutput
|
||||||
|
{
|
||||||
|
// ColumnName attribute is used to change the column name from
|
||||||
|
// its default value, which is the name of the field.
|
||||||
|
[ColumnName("PredictedLabel")]
|
||||||
|
public String Prediction { get; set; }
|
||||||
|
public float[] Score { get; set; }
|
||||||
|
}
|
||||||
|
}
|