@@ -16,13 +16,16 @@ limitations under the License.
1616
1717
1818using NumSharp ;
19+ using NumSharp . Backends ;
20+ using NumSharp . Backends . Unmanaged ;
1921using OpenCvSharp ;
2022using System ;
2123using System . Collections ;
2224using System . Collections . Generic ;
2325using System . Diagnostics ;
2426using System . IO ;
2527using System . Linq ;
28+ using System . Runtime . CompilerServices ;
2629using Tensorflow ;
2730using Tensorflow . Hub ;
2831using 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