@@ -189,6 +189,26 @@ private void CheckTrainingParameters(ImageClassificationEstimator.Options option
189189 return ( jpegData , resizedImage ) ;
190190 }
191191
192+
193+ private static Tensor Encode ( VBuffer < byte > buffer , int length )
194+ {
195+ var size = c_api . TF_StringEncodedSize ( ( UIntPtr ) length ) ;
196+ var handle = c_api . TF_AllocateTensor ( TF_DataType . TF_STRING , IntPtr . Zero , 0 , ( UIntPtr ) ( ( ulong ) size + 8 ) ) ;
197+ //AllocationType = AllocationType.Tensorflow;
198+
199+ IntPtr tensor = c_api . TF_TensorData ( handle ) ;
200+ Marshal . WriteInt64 ( tensor , 0 ) ;
201+
202+ var status = new Status ( ) ;
203+ unsafe
204+ {
205+ fixed ( byte * src = buffer . GetValues ( ) )
206+ c_api . TF_StringEncode ( src , ( UIntPtr ) length , ( sbyte * ) ( tensor + sizeof ( Int64 ) ) , size , status ) ;
207+ }
208+ status . Check ( true ) ;
209+ return new Tensor ( handle ) ;
210+ }
211+
192212 private sealed class ImageProcessor
193213 {
194214 private Runner _imagePreprocessingRunner ;
@@ -202,7 +222,7 @@ public ImageProcessor(ImageClassificationTransformer transformer)
202222
203223 public Tensor ProcessImage ( VBuffer < byte > imgBuf )
204224 {
205- var imageTensor = new Tensor ( imgBuf . DenseValues ( ) . ToArray ( ) , TF_DataType . TF_STRING ) ;
225+ var imageTensor = Encode ( imgBuf , imgBuf . Length ) ;
206226 var processedTensor = _imagePreprocessingRunner . AddInput ( imageTensor , 0 ) . Run ( ) [ 0 ] ;
207227 imageTensor . Dispose ( ) ;
208228 return processedTensor ;
@@ -221,16 +241,14 @@ private void CacheFeaturizedImagesToDisk(IDataView input, string labelColumnName
221241 labelColumn . Type . RawType . ToString ( ) ) ;
222242
223243 var imageBufColumn = input . Schema [ imageColumnName ] ;
224- var imagePathColumn = input . Schema [ "ImagePath" ] ;
225244 Runner runner = new Runner ( _session ) ;
226245 runner . AddOutputs ( outputTensorName ) ;
227246
228247 using ( TextWriter writer = File . CreateText ( cacheFilePath ) )
229- using ( var cursor = input . GetRowCursor ( input . Schema . Where ( c => c . Index == labelColumn . Index || c . Index == imageBufColumn . Index || c . Index == imagePathColumn . Index ) ) )
248+ using ( var cursor = input . GetRowCursor ( input . Schema . Where ( c => c . Index == labelColumn . Index || c . Index == imageBufColumn . Index ) ) )
230249 {
231250 var labelGetter = cursor . GetGetter < uint > ( labelColumn ) ;
232251 var imageBufGetter = cursor . GetGetter < VBuffer < byte > > ( imageBufColumn ) ;
233- var imagePathGetter = cursor . GetGetter < ReadOnlyMemory < char > > ( imagePathColumn ) ;
234252 UInt32 label = UInt32 . MaxValue ;
235253 VBuffer < byte > imageBuf = default ;
236254 ReadOnlyMemory < char > imagePath = default ;
@@ -242,16 +260,13 @@ private void CacheFeaturizedImagesToDisk(IDataView input, string labelColumnName
242260 {
243261 labelGetter ( ref label ) ;
244262 imageBufGetter ( ref imageBuf ) ;
245- imagePathGetter ( ref imagePath ) ;
246- var imagePathStr = imagePath . ToString ( ) ;
247263 var imageTensor = imageProcessor . ProcessImage ( imageBuf ) ;
248264 runner . AddInput ( imageTensor , 0 ) ;
249265 var featurizedImage = runner . Run ( ) [ 0 ] ; // Reuse memory?
250266 writer . WriteLine ( label - 1 + "," + string . Join ( "," , featurizedImage . ToArray < float > ( ) ) ) ;
251267 featurizedImage . Dispose ( ) ;
252268 imageTensor . Dispose ( ) ;
253269 metrics . Bottleneck . Index ++ ;
254- metrics . Bottleneck . Name = imagePathStr ;
255270 metricsCallback ? . Invoke ( metrics ) ;
256271 }
257272 }
0 commit comments