77
88use Codewithkyrian \Transformers \Tensor \Tensor ;
99use Codewithkyrian \Transformers \Utils \Image ;
10+ use Exception ;
1011use Imagine \Image \Point ;
1112
1213class ImageFeatureExtractor extends FeatureExtractor
1314{
14- /**
15- * The mean values for image normalization.
16- *
17- * @var int|int[]
18- */
15+ /** The mean values for image normalization. */
1916 protected int |array |null $ imageMean ;
2017
21- /**
22- * The standard deviation values for image normalization.
23- *
24- * @var int|int[]
25- */
18+ /** The standard deviation values for image normalization. */
2619 protected int |array |null $ imageStd ;
2720
28- /*
29- * What method to use for resampling.
30- */
21+ /* What method to use for resampling. */
3122 protected int $ resample ;
3223
33- /**
34- * Whether to rescale the image pixel values to the [0,1] range.
35- *
36- * @var bool
37- */
24+ /** Whether to rescale the image pixel values to the [0,1] range. */
3825 protected bool $ doRescale ;
3926
40- /**
41- * The factor to use for rescaling the image pixel values.
42- *
43- * @var float
44- */
27+ /** The factor to use for rescaling the image pixel values. */
4528 protected float $ rescaleFactor ;
4629
47- /**
48- * Whether to normalize the image pixel values.
49- *
50- * @var ?bool
51- */
30+ /** Whether to normalize the image pixel values. */
5231 protected ?bool $ doNormalize ;
5332
54- /**
55- * Whether to resize the image.
56- *
57- * @var ?bool
58- */
33+ /** Whether to resize the image. */
5934 protected ?bool $ doResize ;
6035
36+ /** The size to resize the image to. */
6137 protected ?bool $ doThumbnail ;
6238
63- /**
64- * The size to resize the image to.
65- *
66- * @var ?array
67- */
39+ /** The size to resize the image to. */
6840 protected ?array $ size ;
6941 protected mixed $ sizeDivisibility ;
7042 protected ?bool $ doCenterCrop ;
@@ -171,7 +143,7 @@ public function cropMargin(Image $image, int $grayThreshold = 200): static
171143 * @param int $constantValues The constant value to use for padding.
172144 *
173145 * @return Tensor The padded pixel data and image dimensions.
174- * @throws \ Exception
146+ * @throws Exception
175147 */
176148 public function padImage (
177149 Tensor $ imageTensor ,
@@ -233,7 +205,7 @@ public function padImage(
233205
234206 if ($ mode === 'symmetric ' ) {
235207 if ($ center ) {
236- throw new \ Exception ('`center` padding is not supported when `mode` is set to `symmetric`. ' );
208+ throw new Exception ('`center` padding is not supported when `mode` is set to `symmetric`. ' );
237209 // TODO: Implement this
238210 }
239211 $ h1 = $ imageHeight - 1 ;
@@ -278,6 +250,7 @@ private function calculateReflectOffset(int $val, int $max): int
278250 * @param int|array|null $size The size to use for resizing the image.
279251 *
280252 * @return array The target (width, height) dimension of the output image after resizing.
253+ * @throws Exception
281254 */
282255 public function getResizeOutputImageSize (Image $ image , int |array |null $ size ): array
283256 {
@@ -351,7 +324,7 @@ public function getResizeOutputImageSize(Image $image, int|array|null $size): ar
351324 } elseif ($ this ->sizeDivisibility != null ) {
352325 return $ this ->enforceSizeDivisibility ([$ srcWidth , $ srcHeight ], $ this ->sizeDivisibility );
353326 } else {
354- throw new \ Exception ("Could not resize image due to unsupported 'size' parameter passed: " .json_encode ($ size ));
327+ throw new Exception ("Could not resize image due to unsupported 'size' parameter passed: " .json_encode ($ size ));
355328 }
356329 }
357330
@@ -360,13 +333,13 @@ public function getResizeOutputImageSize(Image $image, int|array|null $size): ar
360333 * Preprocesses the given image.
361334 *
362335 * @param Image $image The image to preprocess.
363- * @param ?bool $doNormalize
364- * @param ?bool $doPad
365- * @param ?bool $doConvertRGB
366- * @param ?bool $doConvertGrayscale
336+ * @param ?bool $doNormalize Whether to normalize the image.
337+ * @param ?bool $doPad Whether to pad the image.
338+ * @param ?bool $doConvertRGB Whether to convert the image to RGB.
339+ * @param ?bool $doConvertGrayscale Whether to convert the image to grayscale.
367340 *
368341 * @return array The preprocessed image.
369- * @throws \ Exception
342+ * @throws Exception
370343 */
371344 public function preprocess (
372345 Image $ image ,
@@ -382,7 +355,6 @@ public function preprocess(
382355 $ image = $ image ->cropMargin ();
383356 }
384357
385-
386358 $ originalInputSize = $ image ->size (); // original image size
387359
388360 // Convert image to RGB if specified in config.
@@ -414,7 +386,7 @@ public function preprocess(
414386 $ cropHeight = $ this ->cropSize ['height ' ];
415387 }
416388
417- $ image = $ image ->centerCrop ($ cropWidth , $ cropHeight );
389+ $ image = $ image ->centerCrop ($ cropWidth , $ cropHeight );
418390 }
419391
420392 $ reshapedInputSize = $ image ->size ();
@@ -449,7 +421,7 @@ public function preprocess(
449421 $ imageStd = $ imageStd ->reshape ($ imageTensor ->shape ());
450422
451423 if (count ($ imageMean ) !== $ image ->channels || count ($ imageStd ) !== $ image ->channels ) {
452- throw new \ Exception ("When set to arrays, the length of `imageMean` ( " .count ($ imageMean ).") and `imageStd` ( " .count ($ imageStd ).") must match the number of channels in the image ( {$ image ->channels }). " );
424+ throw new Exception ("When set to arrays, the length of `imageMean` ( " .count ($ imageMean ).") and `imageStd` ( " .count ($ imageStd ).") must match the number of channels in the image ( {$ image ->channels }). " );
453425 }
454426
455427 // Normalize pixel data
@@ -485,25 +457,18 @@ public function preprocess(
485457 */
486458 public function __invoke (Image |array $ images , ...$ args ): array
487459 {
488- // Ensure $images is an array
489460 if (!is_array ($ images )) {
490461 $ images = [$ images ];
491462 }
492463
493- // Preprocess each image
494- $ imageData = [];
495- foreach ($ images as $ image ) {
496- $ imageData [] = $ this ->preprocess ($ image );
497- }
464+ $ imageData = array_map ([$ this , 'preprocess ' ], $ images );
498465
499466 $ pixelValues = array_column ($ imageData , 'pixel_values ' );
500467 $ originalSizes = array_column ($ imageData , 'original_size ' );
501468 $ reshapedInputSizes = array_column ($ imageData , 'reshaped_input_size ' );
502469
503- $ stackedPixelValues = Tensor::stack ($ pixelValues , 0 );
504-
505470 return [
506- 'pixel_values ' => $ stackedPixelValues ,
471+ 'pixel_values ' => Tensor:: stack ( $ pixelValues ) ,
507472 'original_sizes ' => $ originalSizes ,
508473 'reshaped_input_sizes ' => $ reshapedInputSizes
509474 ];
0 commit comments