@@ -602,3 +602,157 @@ def test_crop_images(
602
602
backend .convert_to_numpy (cropped_image ),
603
603
atol = 1e-5 ,
604
604
)
605
+
606
+ def test_rgb_to_grayscale_invalid_rank_two_tensor (self ):
607
+ rgb_to_gray = kimage .RGBToGrayscale ()
608
+ invalid_image = np .random .uniform (size = (10 , 10 ))
609
+ with self .assertRaisesRegex (
610
+ ValueError ,
611
+ "Invalid image rank: expected rank 3" ,
612
+ ):
613
+ rgb_to_gray .compute_output_spec (invalid_image )
614
+
615
+ def test_rgb_to_grayscale_invalid_rank_five_tensor (self ):
616
+ rgb_to_gray = kimage .RGBToGrayscale ()
617
+ invalid_image = np .random .uniform (size = (2 , 3 , 10 , 10 , 3 ))
618
+ with self .assertRaisesRegex (
619
+ ValueError ,
620
+ "Invalid image rank: expected rank 3" ,
621
+ ):
622
+ rgb_to_gray .compute_output_spec (invalid_image )
623
+
624
+ def test_rgb_to_grayscale_valid_rank_three_tensor (self ):
625
+ rgb_to_gray = kimage .RGBToGrayscale ()
626
+ valid_image = np .random .uniform (size = (10 , 10 , 3 ))
627
+ output_spec = rgb_to_gray .compute_output_spec (valid_image )
628
+ self .assertEqual (
629
+ output_spec .shape ,
630
+ (10 , 10 , 1 ),
631
+ "Output shape should match expected grayscale image shape" ,
632
+ )
633
+
634
+ def test_rgb_to_grayscale_valid_rank_four_tensor (self ):
635
+ rgb_to_gray = kimage .RGBToGrayscale ()
636
+ valid_image = np .random .uniform (size = (5 , 10 , 10 , 3 ))
637
+ output_spec = rgb_to_gray .compute_output_spec (valid_image )
638
+ self .assertEqual (
639
+ output_spec .shape ,
640
+ (5 , 10 , 10 , 1 ),
641
+ "Output shape should match expected grayscale image shape" ,
642
+ )
643
+
644
+ def test_affine_transform_compute_output_spec_image_rank_too_low (self ):
645
+ affine_transform = kimage .AffineTransform ()
646
+ # Test with an image of rank 2 (invalid)
647
+ image_2d = np .random .uniform (size = (10 , 10 ))
648
+ transform_valid = np .random .uniform (size = (6 ,))
649
+ with self .assertRaisesRegex (
650
+ ValueError , "Invalid image rank: expected rank 3"
651
+ ):
652
+ affine_transform .compute_output_spec (image_2d , transform_valid )
653
+
654
+ def test_affine_transform_compute_output_spec_image_rank_too_high (self ):
655
+ affine_transform = kimage .AffineTransform ()
656
+ # Test with an image of rank 5 (invalid)
657
+ image_5d = np .random .uniform (size = (2 , 10 , 10 , 3 , 1 ))
658
+ transform_valid = np .random .uniform (size = (6 ,))
659
+ with self .assertRaisesRegex (
660
+ ValueError , "Invalid image rank: expected rank 3"
661
+ ):
662
+ affine_transform .compute_output_spec (image_5d , transform_valid )
663
+
664
+ def test_affine_transform_compute_output_spec_transform_rank_too_high (self ):
665
+ affine_transform = kimage .AffineTransform ()
666
+ # Test with a valid image rank 3
667
+ image_valid = np .random .uniform (size = (10 , 10 , 3 ))
668
+ # Test with a transform of rank 3 (invalid)
669
+ transform_invalid_rank3 = np .random .uniform (size = (2 , 3 , 2 ))
670
+ with self .assertRaisesRegex (
671
+ ValueError , "Invalid transform rank: expected rank 1"
672
+ ):
673
+ affine_transform .compute_output_spec (
674
+ image_valid , transform_invalid_rank3
675
+ )
676
+
677
+ def test_affine_transform_compute_output_spec_transform_rank_too_low (self ):
678
+ affine_transform = kimage .AffineTransform ()
679
+ # Test with a valid image rank 3
680
+ image_valid = np .random .uniform (size = (10 , 10 , 3 ))
681
+ # Test with a transform of rank 0 (invalid)
682
+ transform_invalid_rank0 = np .random .uniform (size = ())
683
+ with self .assertRaisesRegex (
684
+ ValueError , "Invalid transform rank: expected rank 1"
685
+ ):
686
+ affine_transform .compute_output_spec (
687
+ image_valid , transform_invalid_rank0
688
+ )
689
+
690
+ def test_extract_patches_with_invalid_tuple_size (self ):
691
+ size = (3 , 3 , 3 ) # Invalid size, too many dimensions
692
+ image = np .random .uniform (size = (2 , 20 , 20 , 3 ))
693
+ with self .assertRaisesRegex (
694
+ TypeError , "Expected an int or a tuple of length 2"
695
+ ):
696
+ kimage .extract_patches (image , size )
697
+
698
+ def test_extract_patches_with_incorrect_type_size (self ):
699
+ size = "5" # Invalid size type
700
+ image = np .random .uniform (size = (2 , 20 , 20 , 3 ))
701
+ with self .assertRaisesRegex (
702
+ TypeError , "Expected an int or a tuple of length 2"
703
+ ):
704
+ kimage .extract_patches (image , size )
705
+
706
+ def test_extract_patches_with_integer_size (self ):
707
+ size = 5
708
+ # Use float32 for compatibility with TensorFlow convolution operations
709
+ image = np .random .uniform (size = (1 , 20 , 20 , 3 )).astype (np .float32 )
710
+ patches = kimage .extract_patches (image , size )
711
+ # Expecting 4x4 patches with each patch having 75 values (5x5x3)
712
+ expected_shape = (1 , 4 , 4 , 75 )
713
+ self .assertEqual (patches .shape , expected_shape )
714
+
715
+ def test_extract_patches_with_tuple_size (self ):
716
+ size = (5 , 5 )
717
+ image = np .random .uniform (size = (1 , 20 , 20 , 3 )).astype (np .float32 )
718
+ patches = kimage .extract_patches (image , size )
719
+ # Expecting 4x4 patches with each patch having 75 values (5x5x3)
720
+ expected_shape = (1 , 4 , 4 , 75 )
721
+ self .assertEqual (patches .shape , expected_shape )
722
+
723
+ def test_map_coordinates_image_coordinates_rank_mismatch (self ):
724
+ map_coordinates = kimage .MapCoordinates ()
725
+ image = np .random .uniform (size = (10 , 10 , 3 ))
726
+ coordinates = np .random .uniform (size = (2 , 10 , 10 ))
727
+ with self .assertRaisesRegex (
728
+ ValueError , "must be the same as the rank of `image`"
729
+ ):
730
+ map_coordinates .compute_output_spec (image , coordinates )
731
+
732
+ def test_map_coordinates_image_coordinates_rank_mismatch_order_zero (self ):
733
+ map_coordinates = kimage .MapCoordinates (order = 0 )
734
+ image = np .random .uniform (size = (10 , 10 , 3 ))
735
+ coordinates = np .random .uniform (size = (2 , 10 , 10 ))
736
+ with self .assertRaisesRegex (
737
+ ValueError , "must be the same as the rank of `image`"
738
+ ):
739
+ map_coordinates .compute_output_spec (image , coordinates )
740
+
741
+ def test_map_coordinates_coordinates_rank_too_low (self ):
742
+ map_coordinates = kimage .MapCoordinates ()
743
+ image = np .random .uniform (size = (10 , 10 , 3 ))
744
+ coordinates = np .random .uniform (size = (3 ,))
745
+ with self .assertRaisesRegex (ValueError , "expected at least rank 2" ):
746
+ map_coordinates .compute_output_spec (image , coordinates )
747
+
748
+ def test_map_coordinates_valid_input (self ):
749
+ map_coordinates = kimage .MapCoordinates ()
750
+ image = np .random .uniform (size = (10 , 10 , 3 ))
751
+ coordinates = np .random .uniform (size = (3 , 10 , 10 ))
752
+ output_spec = map_coordinates .compute_output_spec (image , coordinates )
753
+ expected_shape = (10 , 10 )
754
+ self .assertEqual (
755
+ output_spec .shape ,
756
+ expected_shape ,
757
+ "Output shape should be correct for valid inputs" ,
758
+ )
0 commit comments