我正在尝试找出如何正确使用ConvNetShar来学习某些图像中的某些形状的方法。我有由白色背景上的黑色形状组成的测试数据,所有这些形状都属于1类(道路),并且有和没有图像。我尝试改编2d演示,它确实可以学习,但是当它测试失败时...
下面是我的代码...只是想知道是否有人在使用图像工作示例?
谢谢 (刚刚更新的代码)似乎正在学习..虽然仍然无法通过测试...
using System;
using System.Collections.Generic;
using ConvNetSharp.Core;
using ConvNetSharp.Core.Layers.Double;
using ConvNetSharp.Core.Training;
using ConvNetSharp.Volume;
using ConvNetSharp.Volume.Double;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using NDNBackpropNnTrainer;
namespace ClassifyImageDemo
{
internal class Program
{
private static void ClassifyImage()
{
string inputFolder = GetInputFolder();
var filelist = Directory.EnumerateFiles(inputFolder, "*.png");
List<List<double>> matrix = new List<List<double>>();
List<int> expetedList = new List<int>();
int looper = 0;
int width = 1;
int height = 1;
foreach (var fileInImage in filelist)
{
matrix.Add(new List<double>());
Bitmap source = (Bitmap) Image.FromFile(fileInImage);
List<double> innerList = new List<double>();
using (var bmp = new LockBitmap(source))
{
width = bmp.Width;
height = bmp.Height;
for (var y = 0; y < bmp.Height; y++)
{
for (var x = 0; x < bmp.Width; x++)
{
var color = bmp.GetPixel(x, y);
int myR = color.R;
int myG = color.G;
int myB = color.B;
int total = myR + myG + myB;
//Adds new sub List
innerList.Add(total);
}
}
}
matrix[looper]=innerList; //Add values to the sub List at index 0
looper = looper + 1 ;
byte[] myImageRaw = File.ReadAllBytes(fileInImage);
int len = myImageRaw.Length;
var lastOneByte = (byte) myImageRaw[len - 1];
expetedList.Add(lastOneByte);
}
var net = new Net<double>();
net.AddLayer(new InputLayer(1, 1, (width * height)));
net.AddLayer(new FullyConnLayer(6));
net.AddLayer(new TanhLayer());
net.AddLayer(new FullyConnLayer(2));
net.AddLayer(new TanhLayer());
net.AddLayer(new FullyConnLayer(2));
net.AddLayer(new SoftmaxLayer(2));
// Data
var data = new List<double[]>();
var labels = new List<int>();
foreach (var lstInputs in matrix)
{
double[] arrayIn = lstInputs.ToArray();
data.Add(arrayIn);
}
foreach (var lbl in expetedList)
{
labels.Add(lbl);
}
var n = labels.Count;
var trainer = new SgdTrainer<double>(net) { LearningRate = 0.01, L2Decay = 0.001, BatchSize = n };
double loss = 1.0;
// Training
while (loss > 0.00001)
{
loss = ClassifyImageUpdate(width, height, n, data, trainer, labels);
}
// Testing
var netx = BuilderInstance.Volume.From(new double[(width * height) * n], new Shape(1, 1, (width*height), n));
for (var ix = 0; ix < n; ix++)
{
int subLooper2 = 0;
foreach (var dtA2 in data[ix])
{
netx.Set(0, 0, subLooper2, ix, dtA2);
subLooper2 = subLooper2 + 1;
}
}
var result = net.Forward(netx);
var c = net.GetPrediction();
var accurate = c[0] == labels[0];
Console.ReadLine();
}
private static string GetInputFolder()
{
return @"D:\temp\filtered\blackroads\subset_500";
}
private static double ClassifyImageUpdate(int width, int height, int n, List<double[]> data, TrainerBase<double> trainer, List<int> labels)
{
var avloss = 0.0;
int dimensions = width * height;
var netx = BuilderInstance.Volume.SameAs(new Shape(1, 1, dimensions, n));
var hotLabels = BuilderInstance.Volume.SameAs(new Shape(1, 1, 1, n));
for (var ix = 0; ix < n; ix++)
{
hotLabels.Set(0, 0, 0, ix, labels[ix]);
int subLooper = 0;
foreach (var dtA in data[ix])
{
netx.Set(0, 0, subLooper, ix, dtA);
subLooper = subLooper + 1;
}
// netx.Set(0, 0, 1, ix, data[ix][1]);
}
for (var iters = 0; iters < 50; iters++)
{
trainer.Train(netx, hotLabels);
avloss += trainer.Loss;
}
avloss /= 50.0;
Console.WriteLine(" Loss:" + avloss);
return avloss;
}
private static void Main(string[] args)
{
ClassifyImage();
}
}
}