Skip to content

Commit 09f5c00

Browse files
authored
fix mat to array method bug
use new method to fix mat to array bug : WrapWithNDArray
1 parent e3e7357 commit 09f5c00

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

src/TensorFlowNET.Examples/CnnInYourOwnData/CnnInYourOwnData.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ limitations under the License.
1616

1717

1818
using NumSharp;
19+
using NumSharp.Backends;
20+
using NumSharp.Backends.Unmanaged;
1921
using OpenCvSharp;
2022
using System;
2123
using System.Collections;
2224
using System.Collections.Generic;
2325
using System.Diagnostics;
2426
using System.IO;
2527
using System.Linq;
28+
using System.Runtime.CompilerServices;
2629
using Tensorflow;
2730
using Tensorflow.Hub;
2831
using static Tensorflow.Binding;
@@ -66,7 +69,7 @@ class CnnInYourOwnData : IExample
6669
Tensor normalized;
6770
Tensor decodeJpeg;
6871

69-
int display_freq = 10;
72+
int display_freq = 2;
7073
float accuracy_test = 0f;
7174
float loss_test = 1f;
7275

@@ -547,30 +550,34 @@ private void Write_Dictionary(string path, Dictionary<Int64, string> mydic)
547550
var y_batch = y[slice];
548551
return (x_batch, y_batch);
549552
}
550-
private (NDArray, NDArray) GetNextBatch(Session sess, string[] x, NDArray y, int start, int end)
553+
private unsafe (NDArray, NDArray) GetNextBatch(Session sess, string[] x, NDArray y, int start, int end)
551554
{
552555
NDArray x_batch = np.zeros(end - start, img_h, img_w, n_channels);
553556
int n = 0;
554557
for (int i = start; i < end; i++)
555558
{
556559
Mat img1 = Cv2.ImRead(x[i], ImreadModes.Grayscale);
557-
558-
//some bugs here
559-
byte[] img2 = img1.ToBytes();
560-
// byte[,] img2 = new byte[img1.Rows, img1.Cols];
561-
// byte[] img2 = new byte[img1.Rows* img1.Cols];
562-
// img1.GetArray(img1.Rows, img1.Cols, img2);
563-
564-
NDArray img3 = new NDArray(img2);
565-
NDArray img4 = img3.reshape(img1.Height, img1.Width, n_channels);
560+
NDArray img4 = WrapWithNDArray(img1);
566561
x_batch[n] = sess.run(normalized, (decodeJpeg, img4));
567562
n++;
568563
}
569564
var slice = new Slice(start, end);
570565
var y_batch = y[slice];
571566
return (x_batch, y_batch);
572567
}
568+
//this method wraps without copying Mat.
569+
private unsafe NDArray WrapWithNDArray(Mat src)
570+
{
571+
Shape shape = (src.Height, src.Width, src.Type().Channels);
572+
var storage = new UnmanagedStorage(new ArraySlice<byte>(new UnmanagedMemoryBlock<byte>(src.DataPointer, shape.Size, () => Donothing(src))), shape); //we pass donothing as it keeps reference to src preventing its disposal by GC
573+
return new NDArray(storage);
574+
}
573575

576+
[MethodImpl(MethodImplOptions.NoOptimization)]
577+
private void Donothing(Mat m)
578+
{
579+
var a = m;
580+
}
574581
#endregion
575582

576583
public void Test(Session sess)
@@ -594,8 +601,8 @@ private void TestDataOutput()
594601
string fileName = ArrayFileName_Test[i];
595602
string real_str = Dict_Label[real];
596603
string predict_str = Dict_Label[predict];
597-
print((i + 1) + "|" + "result:" + result + "|" + "real_str:" + real_str + "|"
598-
+ "predict_str:" + predict_str + "|" + "probability:" + probability + "|"
604+
print((i + 1).ToString() + "|" + "result:" + result + "|" + "real_str:" + real_str + "|"
605+
+ "predict_str:" + predict_str + "|" + "probability:" + probability.GetSingle().ToString() + "|"
599606
+ "fileName:" + fileName);
600607
}
601608
}

0 commit comments

Comments
 (0)