11using Microsoft . ML . OnnxRuntime . Tensors ;
22using OnnxStack . Core . Image ;
3- using OnnxStack . StableDiffusion . Models ;
43using SixLabors . ImageSharp ;
54using SixLabors . ImageSharp . PixelFormats ;
65using SixLabors . ImageSharp . Processing ;
76using System ;
87using System . IO ;
9- using System . Threading . Tasks ;
108
119namespace OnnxStack . StableDiffusion . Helpers
1210{
@@ -55,18 +53,18 @@ public static Stream ToImageStream(this DenseTensor<float> imageTensor)
5553 /// <param name="imageData">The image data.</param>
5654 /// <param name="dimensions">The dimensions.</param>
5755 /// <returns></returns>
58- public static DenseTensor < float > ToDenseTensor ( this InputImage imageData , ReadOnlySpan < int > dimensions )
56+ public static DenseTensor < float > ToDenseTensor ( this InputImage imageData , ReadOnlySpan < int > dimensions , bool normalize = true )
5957 {
6058 if ( ! string . IsNullOrEmpty ( imageData . ImageBase64 ) )
61- return TensorFromBase64 ( imageData . ImageBase64 , dimensions ) ;
59+ return TensorFromBase64 ( imageData . ImageBase64 , dimensions , normalize ) ;
6260 if ( imageData . ImageBytes != null )
63- return TensorFromBytes ( imageData . ImageBytes , dimensions ) ;
61+ return TensorFromBytes ( imageData . ImageBytes , dimensions , normalize ) ;
6462 if ( imageData . ImageStream != null )
65- return TensorFromStream ( imageData . ImageStream , dimensions ) ;
63+ return TensorFromStream ( imageData . ImageStream , dimensions , normalize ) ;
6664 if ( imageData . ImageTensor != null )
6765 return imageData . ImageTensor . ToDenseTensor ( ) ; // Note: Tensor Copy // TODO: Reshape to dimensions
6866
69- return TensorFromImage ( imageData . Image , dimensions ) ;
67+ return TensorFromImage ( imageData . Image , dimensions , normalize ) ;
7068 }
7169
7270
@@ -147,12 +145,12 @@ public static void TensorToImageDebug2(DenseTensor<float> imageTensor, string fi
147145 /// <param name="image">The image.</param>
148146 /// <param name="dimensions">The dimensions.</param>
149147 /// <returns></returns>
150- public static DenseTensor < float > TensorFromImage ( Image < Rgba32 > image , ReadOnlySpan < int > dimensions )
148+ public static DenseTensor < float > TensorFromImage ( Image < Rgba32 > image , ReadOnlySpan < int > dimensions , bool normalize )
151149 {
152150 using ( image )
153151 {
154152 Resize ( image , dimensions ) ;
155- return ProcessPixels ( image , dimensions ) ;
153+ return ProcessPixels ( image , dimensions , normalize ) ;
156154 }
157155 }
158156
@@ -164,12 +162,12 @@ public static DenseTensor<float> TensorFromImage(Image<Rgba32> image, ReadOnlySp
164162 /// <param name="width">The width.</param>
165163 /// <param name="height">The height.</param>
166164 /// <returns></returns>
167- public static DenseTensor < float > TensorFromFile ( string filename , ReadOnlySpan < int > dimensions )
165+ public static DenseTensor < float > TensorFromFile ( string filename , ReadOnlySpan < int > dimensions , bool normalize )
168166 {
169167 using ( var image = Image . Load < Rgba32 > ( filename ) )
170168 {
171169 Resize ( image , dimensions ) ;
172- return ProcessPixels ( image , dimensions ) ;
170+ return ProcessPixels ( image , dimensions , normalize ) ;
173171 }
174172 }
175173
@@ -180,12 +178,12 @@ public static DenseTensor<float> TensorFromFile(string filename, ReadOnlySpan<in
180178 /// <param name="base64Image">The base64 image.</param>
181179 /// <param name="width">The width.</param>
182180 /// <param name="height">The height.</param>
183- public static DenseTensor < float > TensorFromBase64 ( string base64Image , ReadOnlySpan < int > dimensions )
181+ public static DenseTensor < float > TensorFromBase64 ( string base64Image , ReadOnlySpan < int > dimensions , bool normalize )
184182 {
185183 using ( var image = Image . Load < Rgba32 > ( Convert . FromBase64String ( base64Image . Split ( ',' ) [ 1 ] ) ) )
186184 {
187185 Resize ( image , dimensions ) ;
188- return ProcessPixels ( image , dimensions ) ;
186+ return ProcessPixels ( image , dimensions , normalize ) ;
189187 }
190188 }
191189
@@ -198,12 +196,12 @@ public static DenseTensor<float> TensorFromBase64(string base64Image, ReadOnlySp
198196 /// <param name="height">The height.</param>
199197 /// <returns>
200198 /// </returns>
201- public static DenseTensor < float > TensorFromBytes ( byte [ ] imageBytes , ReadOnlySpan < int > dimensions )
199+ public static DenseTensor < float > TensorFromBytes ( byte [ ] imageBytes , ReadOnlySpan < int > dimensions , bool normalize )
202200 {
203201 using ( var image = Image . Load < Rgba32 > ( imageBytes ) )
204202 {
205203 Resize ( image , dimensions ) ;
206- return ProcessPixels ( image , dimensions ) ;
204+ return ProcessPixels ( image , dimensions , normalize ) ;
207205 }
208206 }
209207
@@ -215,12 +213,12 @@ public static DenseTensor<float> TensorFromBytes(byte[] imageBytes, ReadOnlySpan
215213 /// <param name="width">The width.</param>
216214 /// <param name="height">The height.</param>
217215 /// <returns></returns>
218- public static DenseTensor < float > TensorFromStream ( Stream imageStream , ReadOnlySpan < int > dimensions )
216+ public static DenseTensor < float > TensorFromStream ( Stream imageStream , ReadOnlySpan < int > dimensions , bool normalize )
219217 {
220218 using ( var image = Image . Load < Rgba32 > ( imageStream ) )
221219 {
222220 Resize ( image , dimensions ) ;
223- return ProcessPixels ( image , dimensions ) ;
221+ return ProcessPixels ( image , dimensions , normalize ) ;
224222 }
225223 }
226224
@@ -231,7 +229,7 @@ public static DenseTensor<float> TensorFromStream(Stream imageStream, ReadOnlySp
231229 /// <param name="image">The image.</param>
232230 /// <param name="dimensions">The dimensions.</param>
233231 /// <returns></returns>
234- private static DenseTensor < float > ProcessPixels ( Image < Rgba32 > image , ReadOnlySpan < int > dimensions )
232+ private static DenseTensor < float > ProcessPixels ( Image < Rgba32 > image , ReadOnlySpan < int > dimensions , bool normalize )
235233 {
236234 var width = dimensions [ 3 ] ;
237235 var height = dimensions [ 2 ] ;
@@ -244,11 +242,11 @@ private static DenseTensor<float> ProcessPixels(Image<Rgba32> image, ReadOnlySpa
244242 for ( int y = 0 ; y < height ; y ++ )
245243 {
246244 var pixelSpan = img . GetRowSpan ( y ) ;
247- imageArray [ 0 , 0 , y , x ] = ( pixelSpan [ x ] . R / 255.0f ) * 2.0f - 1.0f ;
248- imageArray [ 0 , 1 , y , x ] = ( pixelSpan [ x ] . G / 255.0f ) * 2.0f - 1.0f ;
249- imageArray [ 0 , 2 , y , x ] = ( pixelSpan [ x ] . B / 255.0f ) * 2.0f - 1.0f ;
245+ imageArray [ 0 , 0 , y , x ] = normalize ? ( pixelSpan [ x ] . R / 255.0f ) * 2.0f - 1.0f : ( pixelSpan [ x ] . R / 255.0f ) ;
246+ imageArray [ 0 , 1 , y , x ] = normalize ? ( pixelSpan [ x ] . G / 255.0f ) * 2.0f - 1.0f : ( pixelSpan [ x ] . G / 255.0f ) ;
247+ imageArray [ 0 , 2 , y , x ] = normalize ? ( pixelSpan [ x ] . B / 255.0f ) * 2.0f - 1.0f : ( pixelSpan [ x ] . B / 255.0f ) ;
250248 if ( channels == 4 )
251- imageArray [ 0 , 3 , y , x ] = ( pixelSpan [ x ] . A / 255.0f ) * 2.0f - 1.0f ;
249+ imageArray [ 0 , 3 , y , x ] = normalize ? ( pixelSpan [ x ] . A / 255.0f ) * 2.0f - 1.0f : ( pixelSpan [ x ] . A / 255.0f ) ;
252250 }
253251 }
254252 } ) ;
0 commit comments