@@ -142,17 +142,21 @@ impl Transform for Normalize {
142142 let array = data. array ( ) ?;
143143 let mean = Array :: from_vec ( self . mean . clone ( ) )
144144 . into_shape_with_order ( ( 3 , 1 , 1 ) )
145- . unwrap ( ) ;
145+ . map_err ( |e| anyhow ! ( "Failed to reshape mean array: {}" , e ) ) ? ;
146146 let std = Array :: from_vec ( self . std . clone ( ) )
147147 . into_shape_with_order ( ( 3 , 1 , 1 ) )
148- . unwrap ( ) ;
148+ . map_err ( |e| anyhow ! ( "Failed to reshape std array: {}" , e ) ) ? ;
149149
150150 let shape = array. shape ( ) . to_vec ( ) ;
151151 match shape. as_slice ( ) {
152152 [ c, h, w] => {
153- let array_normalized = array
154- . sub ( mean. broadcast ( ( * c, * h, * w) ) . unwrap ( ) )
155- . div ( std. broadcast ( ( * c, * h, * w) ) . unwrap ( ) ) ;
153+ let mean_broadcast = mean. broadcast ( ( * c, * h, * w) ) . ok_or_else ( || {
154+ anyhow ! ( "Failed to broadcast mean array to shape {:?}" , ( * c, * h, * w) )
155+ } ) ?;
156+ let std_broadcast = std. broadcast ( ( * c, * h, * w) ) . ok_or_else ( || {
157+ anyhow ! ( "Failed to broadcast std array to shape {:?}" , ( * c, * h, * w) )
158+ } ) ?;
159+ let array_normalized = array. sub ( mean_broadcast) . div ( std_broadcast) ;
156160 Ok ( TransformData :: NdArray ( array_normalized) )
157161 }
158162 _ => Err ( anyhow ! (
@@ -229,18 +233,21 @@ fn load_preprocessor(config: serde_json::Value) -> anyhow::Result<Compose> {
229233 if config[ "do_center_crop" ] . as_bool ( ) . unwrap_or ( false ) {
230234 let crop_size = config[ "crop_size" ] . clone ( ) ;
231235 let ( height, width) = if crop_size. is_u64 ( ) {
232- let size = crop_size. as_u64 ( ) . unwrap ( ) as u32 ;
236+ let size = crop_size
237+ . as_u64 ( )
238+ . ok_or_else ( || anyhow ! ( "crop_size must be a valid u64" ) ) ?
239+ as u32 ;
233240 ( size, size)
234241 } else if crop_size. is_object ( ) {
235242 (
236243 crop_size[ "height" ]
237244 . as_u64 ( )
238245 . map ( |height| height as u32 )
239- . ok_or ( anyhow ! ( "crop_size height must be contained" ) ) ?,
246+ . ok_or_else ( || anyhow ! ( "crop_size height must be contained" ) ) ?,
240247 crop_size[ "width" ]
241248 . as_u64 ( )
242249 . map ( |width| width as u32 )
243- . ok_or ( anyhow ! ( "crop_size width must be contained" ) ) ?,
250+ . ok_or_else ( || anyhow ! ( "crop_size width must be contained" ) ) ?,
244251 )
245252 } else {
246253 return Err ( anyhow ! ( "Invalid crop size: {:?}" , crop_size) ) ;
@@ -304,18 +311,21 @@ fn load_preprocessor(config: serde_json::Value) -> anyhow::Result<Compose> {
304311 if config[ "do_center_crop" ] . as_bool ( ) . unwrap_or ( false ) {
305312 let crop_size = config[ "crop_size" ] . clone ( ) ;
306313 let ( height, width) = if crop_size. is_u64 ( ) {
307- let size = crop_size. as_u64 ( ) . unwrap ( ) as u32 ;
314+ let size = crop_size
315+ . as_u64 ( )
316+ . ok_or_else ( || anyhow ! ( "crop_size must be a valid u64" ) ) ?
317+ as u32 ;
308318 ( size, size)
309319 } else if crop_size. is_object ( ) {
310320 (
311321 crop_size[ "height" ]
312322 . as_u64 ( )
313323 . map ( |height| height as u32 )
314- . ok_or ( anyhow ! ( "crop_size height must be contained" ) ) ?,
324+ . ok_or_else ( || anyhow ! ( "crop_size height must be contained" ) ) ?,
315325 crop_size[ "width" ]
316326 . as_u64 ( )
317327 . map ( |width| width as u32 )
318- . ok_or ( anyhow ! ( "crop_size width must be contained" ) ) ?,
328+ . ok_or_else ( || anyhow ! ( "crop_size width must be contained" ) ) ?,
319329 )
320330 } else {
321331 return Err ( anyhow ! ( "Invalid crop size: {:?}" , crop_size) ) ;
0 commit comments