当前位置: 首页 > news >正文

Tensorflow.NET训练自己模型

Tensorflow.NET是基于Tensorflow封装的一个开源C#的库 接下来我们将试着使用他搭建自己的学习分类任务

1.下载安装下面的包直接在Nuget里面搜索安装即可

TensorFlow.Keras
SciSharp.TensorFlow.Redist
OpenCvSharp4
OpenCvSharp4.runtime.win

2、开始搭建可以直接赋值官网的代码根据自己的需要在调整网络 这里我们直接赋值官方实例

var layers = keras.layers;
var inputs = keras.Input(shape: (400, 300, 3), name: "img"); //这里注意一下H W 后面3 是图片通道数
// convolutional layer
var x = layers.Conv2D(32, 3, activation: "relu").Apply(inputs);
x = layers.Conv2D(64, 3, activation: "relu").Apply(x);
var block_1_output = layers.MaxPooling2D(3).Apply(x);
x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(block_1_output);
x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(x);
var block_2_output = layers.Add().Apply(new Tensors(x, block_1_output));
x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(block_2_output);
x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(x);
var block_3_output = layers.Add().Apply(new Tensors(x, block_2_output));
x = layers.Conv2D(64, 3, activation: "relu").Apply(block_3_output);
x = layers.GlobalAveragePooling2D().Apply(x);
x = layers.Dense(256, activation: "relu").Apply(x);
x = layers.Dropout(0.5f).Apply(x);
// output layer
var outputs = layers.Dense(2).Apply(x);//写自己的分类数量 有两个类别就写2 
// build keras model
var model = keras.Model(inputs, outputs, name: "toy_resnet");
model.summary();
// compile keras model in tensorflow static graph
model.compile(optimizer: keras.optimizers.RMSprop(1e-3f),loss: keras.losses.SparseCategoricalCrossentropy(from_logits: true),metrics: new[] { "acc" });
var ((x_train, y_train), (x_test, y_test)) = cifar10Data.load_data();// keras.datasets.cifar10.load_data();
x_train = x_train / 255.0f;// Train model
model.fit(x_train, y_train, batch_size: 10, epochs: 30, validation_split: 0.2f);// Save model
model.save("自己模型保存路径");

  上面的 cifar10Data.load_data()是自己的路径 这里我封装了一个直接可以使用 使用时候注意 目录结构  

cifar10Data的代码如下

  1 using OpenCvSharp;
  2 using System;
  3 using System.Collections.Generic;
  4 using System.IO;
  5 using System.Linq;
  6 using System.Runtime.InteropServices;
  7 using System.Threading.Tasks;
  8 using Tensorflow;
  9 using Tensorflow.Keras.Datasets;
 10 using Tensorflow.NumPy;
 11 using Tensorflow.Operations.Initializers;
 12 using static Tensorflow.Binding;
 13 using static Tensorflow.KerasApi;
 14 
 15 public class Cifar10Data
 16 {
 17     // 本地数据集根目录
 18     private readonly string _datasetRoot = "E:\\Modelinmg\\tf001";
 19     private readonly string _trainFolder;
 20     private readonly string _testFolder;
 21 
 22     // 图片尺寸:宽300,高400
 23     private const int ImgWidth = 300;
 24     private const int ImgHeight = 400;
 25     private const int Channel = 3;
 26 
 27     public Cifar10Data()
 28     {
 29         _trainFolder = Path.Combine(_datasetRoot, "train");//训练数据
 30         _testFolder = Path.Combine(_datasetRoot, "test");//测试数据
 31     }
 32 
 33     public DatasetPass load_data()
 34     {
 35         var (trainX, trainY) = LoadImageFolder(_trainFolder);
 36         var (testX, testY) = LoadImageFolder(_testFolder);
 37 
 38         return new DatasetPass
 39         {
 40             Train = (trainX, trainY),
 41             Test = (testX, testY)
 42         };
 43     }
 44 
 45     private (NDArray imageNd, NDArray labelNd) LoadImageFolder(string folderPath)
 46     {
 47         if (!Directory.Exists(folderPath))
 48             throw new DirectoryNotFoundException($"数据集文件夹不存在:{folderPath}");
 49 
 50         var classDirs = Directory.GetDirectories(folderPath).OrderBy(p => p).ToList();
 51         Dictionary<string, int> classLabelMap = new Dictionary<string, int>();
 52         for (int i = 0; i < classDirs.Count; i++)
 53         {
 54             string className = Path.GetFileName(classDirs[i]);
 55             classLabelMap[className] = i;
 56         }
 57         //var shuffledList= classLabelMap.ToList();
 58         // Random rng = new Random();
 59         // shuffledList = shuffledList.OrderBy(x => rng.Next()).ToList();
 60         // // 如果需要重新放回字典(但字典本身仍然无序)
 61         // classLabelMap = new Dictionary<string, int>(shuffledList);
 62         List<string> imageMatList = new List<string>();
 63         List<int> labelList = new List<int>();
 64 
 65         foreach (var classDir in classDirs)
 66         {
 67             string className = Path.GetFileName(classDir);
 68             int label = classLabelMap[className];
 69             var imgPaths = Directory.GetFiles(classDir, "*.*")
 70                 .Where(p => p.EndsWith(".jpg") || p.EndsWith(".jpeg") || p.EndsWith(".png"));
 71 
 72             foreach (var imgPath in imgPaths)
 73             {
 74                 imageMatList.Add(imgPath);
 75                 labelList.Add(label);
 76             }
 77         }
 78 
 79         var pairList = imageMatList.Zip(labelList, (path, label) => new { Img = path, Label = label }).ToList();
 80         Random random = new Random(42);
 81         pairList = pairList.OrderBy(_ => random.Next()).ToList();
 82         imageMatList.Clear();
 83         labelList.Clear();
 84         foreach (var item in pairList)
 85         {
 86             imageMatList.Add(item.Img);
 87             labelList.Add(item.Label);
 88         }
 89 
 90         int sampleCount = imageMatList.Count;//数据总量
 91         if (sampleCount == 0)
 92             throw new Exception($"文件夹 {folderPath} 未找到任何有效图片");
 93 
 94         // ========== 核心修复:直接创建NDArray,删除全局超大byte[] ==========
 95         long[] imgShape = { sampleCount, ImgWidth, ImgHeight, Channel };
 96         //NDArray imageND = np.zeros(imgShape, np.float32);
 97         NDArray imageNd = np.zeros(imgShape, np.uint8);
 98 
 99         Mat bgrImg = new Mat();
100         Mat rgbImg = new Mat();
101         //Mat[] channels = null;
102         int idx = 0;
103         //byte[] bytes = new byte[ImgHeight * ImgWidth * Channel* sampleCount];
104         int singleByte = ImgHeight * ImgWidth * 3;
105         foreach (var item in imageMatList)
106         {
107             bgrImg = Cv2.ImRead(item, ImreadModes.Color);
108             if (bgrImg.Empty())
109             {
110                 Console.WriteLine($"跳过损坏/无法读取图片:{item}");
111                 bgrImg.Dispose();
112                 continue;
113             }
114             if (bgrImg.Channels() != Channel)
115             {
116                 Console.WriteLine($"跳过非3通道图片:{item}");
117                 bgrImg.Dispose();
118                 continue;
119             }
120             if (bgrImg.Rows != ImgHeight || bgrImg.Cols != ImgWidth)
121             {
122                 bgrImg.Dispose();
123                 throw new Exception($"图片尺寸不匹配 {item},预期高{ImgHeight}×宽{ImgWidth}");
124             }
125             Cv2.CvtColor(bgrImg, rgbImg, ColorConversionCodes.BGR2RGB);
126             // 单张图片总字节 H*W*3 
127             byte[] pixelData = new byte[singleByte];
128             // 直接拷贝整张RGB图像内存,天然NHWC排布
129             Marshal.Copy(rgbImg.Data, pixelData, 0, singleByte);
130             // 一维数组直接重塑 (H,W,3),无任何通道转换
131             imageNd[idx] = np.array(pixelData).reshape((ImgHeight, ImgWidth, 3));
132             idx++;
133             //rgbImg.SetTo(0);
134         }
135         bgrImg.Dispose();
136         rgbImg.Dispose();
137         // 归一化转float32
138         // imageNd = imageNd.astype(np.float32);
139         //imageNd = imageNd / 255.0f;
140 
141         NDArray labelNd = np.array(labelList.ToArray()).astype(np.int32);
142         return (imageNd, labelNd);
143     }
144 
145     public NDArray oneImg(string imgpath)
146     {
147 
148         Mat bgrImg = new Mat();
149         Mat rgbImg = new Mat();
150         bgrImg = Cv2.ImRead(imgpath, ImreadModes.Color);
151         if (bgrImg.Empty())
152         {
153             bgrImg.Dispose();
154             throw new Exception($"跳过损坏/无法读取图片:{imgpath}");
155         }
156         if (bgrImg.Channels() != Channel)
157         {
158             bgrImg.Dispose();
159             throw new Exception("$\"跳过非3通道图片:{imgpath}\"");
160         }        
161         Cv2.CvtColor(bgrImg, rgbImg, ColorConversionCodes.BGR2RGB);
162         int singleByte = rgbImg.Width * rgbImg.Height * 3;
163         // 单张图片总字节 H*W*3 
164         byte[] pixelData = new byte[singleByte];
165         // 直接拷贝整张RGB图像内存,天然NHWC排布
166         Marshal.Copy(rgbImg.Data, pixelData, 0, singleByte);
167         // 一维数组直接重塑 (H,W,3),无任何通道转换
168         int[] shape = new int[] { 1, rgbImg.Height, rgbImg.Width, 3 };
169         return np.array(pixelData).reshape(shape);
170 
171     }
172     //模型预测
173     public int Modelpredict(string Modelpath, string imgpath)
174     {
175         NDArray nD=  oneImg(imgpath);
176         //NDArray input_batch = np.expand_dims(nD, axis: 0);
177         var modeltest = keras.models.load_model(Modelpath);
178         nD = nD / 255.0f;
179         var logits = modeltest.predict(nD);
180         NDArray prob = tf.nn.softmax(logits).numpy();
181         //5.找出概率最大下标,就是预测类别
182         int predictClass = np.argmax(prob[0]);
183         
184         return predictClass;
185     }
186 }

 

http://www.jsqmd.com/news/1293971/

相关文章:

  • STM32 SPI时钟分频配置详解:从理论计算到实战调试
  • 石家庄保险理赔律师推荐:专业选择标准与综合知名度评价 - 云间寄笔
  • 终极指南:CoolProp热力学物性计算库从入门到精通
  • 基于MongoDB的AI Agent内存系统架构设计与实现
  • 步进电机驱动原理与STM32控制实战:从开环到细分技术详解
  • 3步轻松搞定老旧Mac升级:OpenCore Legacy Patcher终极指南
  • 2026年苏州浩锦怎么样?你需要了解的真相 - 品牌排行榜
  • 告别无效写作|本科论文高分规范与避错逻辑(导师评审视角)
  • LLM、RAG、SFT、LoRA……AI黑话速查手册,零基础30分钟建立专业认知框架
  • PixVerse语音驱动Avatar口型同步实战:从本地测试到小程序集成
  • Unity ML-Agents训练不收敛?7大原因与调参实战指南
  • roLabelImg 傻瓜式使用教程
  • 好用还专业!盘点2026年碾压级的一键生成论文工具
  • 工程测量交点法:完整与非完整缓和曲线坐标计算详解
  • 别再裸奔做AI副业!20年风控专家私藏的「五维动态风险仪表盘」(含可下载Excel自评模型)
  • 2026年8月淮北非急救救护车转运指南:术后出院如何安排 - 小校长
  • 为什么92%的AI副业半年内熄火?资深架构师拆解可持续性的4层技术护城河(含可落地的ROI测算表)
  • PDF-Lib终极指南:专业JavaScript PDF处理库深度解析与实战应用
  • Unity多场景异步加载实战:基于UniTask与Addressables的零卡顿解决方案
  • 2026年探秘:柯洛克密室主打机械解谜还是NPC演绎?
  • 2026年8月衡阳非急救救护车转运指南:术后出院如何安排 - 小校长
  • @IntDef 替代 enum:Android 官方推荐的轻量常量方案
  • Python图像批量处理实战:Pillow与OpenCV自动化操作指南
  • 【限时释放】AI副业成本诊断矩阵(含12维成本健康度评分+3个立即止损点)
  • 2027年二建备考启动!3款口碑题库,按需挑选不踩坑
  • Web安全入门实战:从浏览器工具到SQL注入的攻防世界通关指南
  • C++ 高并发服务器设计与实现:从基础原理到实战优化
  • 极速提升小店履约效率!2026 抖音小店发货提速、商家高效运营全方位提升指南 - 抖掌柜
  • 2026年8月衡水长途非急救转运解析:车辆、陪护与设备如何匹配 - 小校长
  • ADHD儿童神经发育障碍的识别与HFS训练系统解析