@@ -1367,149 +1367,6 @@ def test_random_affine(self):
1367
1367
t = transforms .RandomAffine (10 , interpolation = 2 )
1368
1368
self .assertEqual (t .interpolation , transforms .InterpolationMode .BILINEAR )
1369
1369
1370
- def test_to_grayscale (self ):
1371
- """Unit tests for grayscale transform"""
1372
-
1373
- x_shape = [2 , 2 , 3 ]
1374
- x_data = [0 , 5 , 13 , 54 , 135 , 226 , 37 , 8 , 234 , 90 , 255 , 1 ]
1375
- x_np = np .array (x_data , dtype = np .uint8 ).reshape (x_shape )
1376
- x_pil = Image .fromarray (x_np , mode = 'RGB' )
1377
- x_pil_2 = x_pil .convert ('L' )
1378
- gray_np = np .array (x_pil_2 )
1379
-
1380
- # Test Set: Grayscale an image with desired number of output channels
1381
- # Case 1: RGB -> 1 channel grayscale
1382
- trans1 = transforms .Grayscale (num_output_channels = 1 )
1383
- gray_pil_1 = trans1 (x_pil )
1384
- gray_np_1 = np .array (gray_pil_1 )
1385
- self .assertEqual (gray_pil_1 .mode , 'L' , 'mode should be L' )
1386
- self .assertEqual (gray_np_1 .shape , tuple (x_shape [0 :2 ]), 'should be 1 channel' )
1387
- assert_equal (gray_np , gray_np_1 )
1388
-
1389
- # Case 2: RGB -> 3 channel grayscale
1390
- trans2 = transforms .Grayscale (num_output_channels = 3 )
1391
- gray_pil_2 = trans2 (x_pil )
1392
- gray_np_2 = np .array (gray_pil_2 )
1393
- self .assertEqual (gray_pil_2 .mode , 'RGB' , 'mode should be RGB' )
1394
- self .assertEqual (gray_np_2 .shape , tuple (x_shape ), 'should be 3 channel' )
1395
- assert_equal (gray_np_2 [:, :, 0 ], gray_np_2 [:, :, 1 ])
1396
- assert_equal (gray_np_2 [:, :, 1 ], gray_np_2 [:, :, 2 ])
1397
- assert_equal (gray_np , gray_np_2 [:, :, 0 ], check_stride = False )
1398
-
1399
- # Case 3: 1 channel grayscale -> 1 channel grayscale
1400
- trans3 = transforms .Grayscale (num_output_channels = 1 )
1401
- gray_pil_3 = trans3 (x_pil_2 )
1402
- gray_np_3 = np .array (gray_pil_3 )
1403
- self .assertEqual (gray_pil_3 .mode , 'L' , 'mode should be L' )
1404
- self .assertEqual (gray_np_3 .shape , tuple (x_shape [0 :2 ]), 'should be 1 channel' )
1405
- assert_equal (gray_np , gray_np_3 )
1406
-
1407
- # Case 4: 1 channel grayscale -> 3 channel grayscale
1408
- trans4 = transforms .Grayscale (num_output_channels = 3 )
1409
- gray_pil_4 = trans4 (x_pil_2 )
1410
- gray_np_4 = np .array (gray_pil_4 )
1411
- self .assertEqual (gray_pil_4 .mode , 'RGB' , 'mode should be RGB' )
1412
- self .assertEqual (gray_np_4 .shape , tuple (x_shape ), 'should be 3 channel' )
1413
- assert_equal (gray_np_4 [:, :, 0 ], gray_np_4 [:, :, 1 ])
1414
- assert_equal (gray_np_4 [:, :, 1 ], gray_np_4 [:, :, 2 ])
1415
- assert_equal (gray_np , gray_np_4 [:, :, 0 ], check_stride = False )
1416
-
1417
- # Checking if Grayscale can be printed as string
1418
- trans4 .__repr__ ()
1419
-
1420
- @unittest .skipIf (stats is None , 'scipy.stats not available' )
1421
- def test_random_grayscale (self ):
1422
- """Unit tests for random grayscale transform"""
1423
-
1424
- # Test Set 1: RGB -> 3 channel grayscale
1425
- random_state = random .getstate ()
1426
- random .seed (42 )
1427
- x_shape = [2 , 2 , 3 ]
1428
- x_np = np .random .randint (0 , 256 , x_shape , np .uint8 )
1429
- x_pil = Image .fromarray (x_np , mode = 'RGB' )
1430
- x_pil_2 = x_pil .convert ('L' )
1431
- gray_np = np .array (x_pil_2 )
1432
-
1433
- num_samples = 250
1434
- num_gray = 0
1435
- for _ in range (num_samples ):
1436
- gray_pil_2 = transforms .RandomGrayscale (p = 0.5 )(x_pil )
1437
- gray_np_2 = np .array (gray_pil_2 )
1438
- if np .array_equal (gray_np_2 [:, :, 0 ], gray_np_2 [:, :, 1 ]) and \
1439
- np .array_equal (gray_np_2 [:, :, 1 ], gray_np_2 [:, :, 2 ]) and \
1440
- np .array_equal (gray_np , gray_np_2 [:, :, 0 ]):
1441
- num_gray = num_gray + 1
1442
-
1443
- p_value = stats .binom_test (num_gray , num_samples , p = 0.5 )
1444
- random .setstate (random_state )
1445
- self .assertGreater (p_value , 0.0001 )
1446
-
1447
- # Test Set 2: grayscale -> 1 channel grayscale
1448
- random_state = random .getstate ()
1449
- random .seed (42 )
1450
- x_shape = [2 , 2 , 3 ]
1451
- x_np = np .random .randint (0 , 256 , x_shape , np .uint8 )
1452
- x_pil = Image .fromarray (x_np , mode = 'RGB' )
1453
- x_pil_2 = x_pil .convert ('L' )
1454
- gray_np = np .array (x_pil_2 )
1455
-
1456
- num_samples = 250
1457
- num_gray = 0
1458
- for _ in range (num_samples ):
1459
- gray_pil_3 = transforms .RandomGrayscale (p = 0.5 )(x_pil_2 )
1460
- gray_np_3 = np .array (gray_pil_3 )
1461
- if np .array_equal (gray_np , gray_np_3 ):
1462
- num_gray = num_gray + 1
1463
-
1464
- p_value = stats .binom_test (num_gray , num_samples , p = 1.0 ) # Note: grayscale is always unchanged
1465
- random .setstate (random_state )
1466
- self .assertGreater (p_value , 0.0001 )
1467
-
1468
- # Test set 3: Explicit tests
1469
- x_shape = [2 , 2 , 3 ]
1470
- x_data = [0 , 5 , 13 , 54 , 135 , 226 , 37 , 8 , 234 , 90 , 255 , 1 ]
1471
- x_np = np .array (x_data , dtype = np .uint8 ).reshape (x_shape )
1472
- x_pil = Image .fromarray (x_np , mode = 'RGB' )
1473
- x_pil_2 = x_pil .convert ('L' )
1474
- gray_np = np .array (x_pil_2 )
1475
-
1476
- # Case 3a: RGB -> 3 channel grayscale (grayscaled)
1477
- trans2 = transforms .RandomGrayscale (p = 1.0 )
1478
- gray_pil_2 = trans2 (x_pil )
1479
- gray_np_2 = np .array (gray_pil_2 )
1480
- self .assertEqual (gray_pil_2 .mode , 'RGB' , 'mode should be RGB' )
1481
- self .assertEqual (gray_np_2 .shape , tuple (x_shape ), 'should be 3 channel' )
1482
- assert_equal (gray_np_2 [:, :, 0 ], gray_np_2 [:, :, 1 ])
1483
- assert_equal (gray_np_2 [:, :, 1 ], gray_np_2 [:, :, 2 ])
1484
- assert_equal (gray_np , gray_np_2 [:, :, 0 ], check_stride = False )
1485
-
1486
- # Case 3b: RGB -> 3 channel grayscale (unchanged)
1487
- trans2 = transforms .RandomGrayscale (p = 0.0 )
1488
- gray_pil_2 = trans2 (x_pil )
1489
- gray_np_2 = np .array (gray_pil_2 )
1490
- self .assertEqual (gray_pil_2 .mode , 'RGB' , 'mode should be RGB' )
1491
- self .assertEqual (gray_np_2 .shape , tuple (x_shape ), 'should be 3 channel' )
1492
- assert_equal (x_np , gray_np_2 )
1493
-
1494
- # Case 3c: 1 channel grayscale -> 1 channel grayscale (grayscaled)
1495
- trans3 = transforms .RandomGrayscale (p = 1.0 )
1496
- gray_pil_3 = trans3 (x_pil_2 )
1497
- gray_np_3 = np .array (gray_pil_3 )
1498
- self .assertEqual (gray_pil_3 .mode , 'L' , 'mode should be L' )
1499
- self .assertEqual (gray_np_3 .shape , tuple (x_shape [0 :2 ]), 'should be 1 channel' )
1500
- assert_equal (gray_np , gray_np_3 )
1501
-
1502
- # Case 3d: 1 channel grayscale -> 1 channel grayscale (unchanged)
1503
- trans3 = transforms .RandomGrayscale (p = 0.0 )
1504
- gray_pil_3 = trans3 (x_pil_2 )
1505
- gray_np_3 = np .array (gray_pil_3 )
1506
- self .assertEqual (gray_pil_3 .mode , 'L' , 'mode should be L' )
1507
- self .assertEqual (gray_np_3 .shape , tuple (x_shape [0 :2 ]), 'should be 1 channel' )
1508
- assert_equal (gray_np , gray_np_3 )
1509
-
1510
- # Checking if RandomGrayscale can be printed as string
1511
- trans3 .__repr__ ()
1512
-
1513
1370
def test_autoaugment (self ):
1514
1371
for policy in transforms .AutoAugmentPolicy :
1515
1372
for fill in [None , 85 , (128 , 128 , 128 )]:
@@ -1992,5 +1849,150 @@ def test_lambda():
1992
1849
trans .__repr__ ()
1993
1850
1994
1851
1852
+ def test_to_grayscale ():
1853
+ """Unit tests for grayscale transform"""
1854
+
1855
+ x_shape = [2 , 2 , 3 ]
1856
+ x_data = [0 , 5 , 13 , 54 , 135 , 226 , 37 , 8 , 234 , 90 , 255 , 1 ]
1857
+ x_np = np .array (x_data , dtype = np .uint8 ).reshape (x_shape )
1858
+ x_pil = Image .fromarray (x_np , mode = 'RGB' )
1859
+ x_pil_2 = x_pil .convert ('L' )
1860
+ gray_np = np .array (x_pil_2 )
1861
+
1862
+ # Test Set: Grayscale an image with desired number of output channels
1863
+ # Case 1: RGB -> 1 channel grayscale
1864
+ trans1 = transforms .Grayscale (num_output_channels = 1 )
1865
+ gray_pil_1 = trans1 (x_pil )
1866
+ gray_np_1 = np .array (gray_pil_1 )
1867
+ assert gray_pil_1 .mode == 'L' , 'mode should be L'
1868
+ assert gray_np_1 .shape == tuple (x_shape [0 :2 ]), 'should be 1 channel'
1869
+ assert_equal (gray_np , gray_np_1 )
1870
+
1871
+ # Case 2: RGB -> 3 channel grayscale
1872
+ trans2 = transforms .Grayscale (num_output_channels = 3 )
1873
+ gray_pil_2 = trans2 (x_pil )
1874
+ gray_np_2 = np .array (gray_pil_2 )
1875
+ assert gray_pil_2 .mode == 'RGB' , 'mode should be RGB'
1876
+ assert gray_np_2 .shape == tuple (x_shape ), 'should be 3 channel'
1877
+ assert_equal (gray_np_2 [:, :, 0 ], gray_np_2 [:, :, 1 ])
1878
+ assert_equal (gray_np_2 [:, :, 1 ], gray_np_2 [:, :, 2 ])
1879
+ assert_equal (gray_np , gray_np_2 [:, :, 0 ], check_stride = False )
1880
+
1881
+ # Case 3: 1 channel grayscale -> 1 channel grayscale
1882
+ trans3 = transforms .Grayscale (num_output_channels = 1 )
1883
+ gray_pil_3 = trans3 (x_pil_2 )
1884
+ gray_np_3 = np .array (gray_pil_3 )
1885
+ assert gray_pil_3 .mode == 'L' , 'mode should be L'
1886
+ assert gray_np_3 .shape == tuple (x_shape [0 :2 ]), 'should be 1 channel'
1887
+ assert_equal (gray_np , gray_np_3 )
1888
+
1889
+ # Case 4: 1 channel grayscale -> 3 channel grayscale
1890
+ trans4 = transforms .Grayscale (num_output_channels = 3 )
1891
+ gray_pil_4 = trans4 (x_pil_2 )
1892
+ gray_np_4 = np .array (gray_pil_4 )
1893
+ assert gray_pil_4 .mode == 'RGB' , 'mode should be RGB'
1894
+ assert gray_np_4 .shape == tuple (x_shape ), 'should be 3 channel'
1895
+ assert_equal (gray_np_4 [:, :, 0 ], gray_np_4 [:, :, 1 ])
1896
+ assert_equal (gray_np_4 [:, :, 1 ], gray_np_4 [:, :, 2 ])
1897
+ assert_equal (gray_np , gray_np_4 [:, :, 0 ], check_stride = False )
1898
+
1899
+ # Checking if Grayscale can be printed as string
1900
+ trans4 .__repr__ ()
1901
+
1902
+
1903
+ @pytest .mark .skipif (stats is None , reason = "scipy.stats not available" )
1904
+ def test_random_grayscale ():
1905
+ """Unit tests for random grayscale transform"""
1906
+
1907
+ # Test Set 1: RGB -> 3 channel grayscale
1908
+ random_state = random .getstate ()
1909
+ random .seed (42 )
1910
+ x_shape = [2 , 2 , 3 ]
1911
+ x_np = np .random .randint (0 , 256 , x_shape , np .uint8 )
1912
+ x_pil = Image .fromarray (x_np , mode = 'RGB' )
1913
+ x_pil_2 = x_pil .convert ('L' )
1914
+ gray_np = np .array (x_pil_2 )
1915
+
1916
+ num_samples = 250
1917
+ num_gray = 0
1918
+ for _ in range (num_samples ):
1919
+ gray_pil_2 = transforms .RandomGrayscale (p = 0.5 )(x_pil )
1920
+ gray_np_2 = np .array (gray_pil_2 )
1921
+ if np .array_equal (gray_np_2 [:, :, 0 ], gray_np_2 [:, :, 1 ]) and \
1922
+ np .array_equal (gray_np_2 [:, :, 1 ], gray_np_2 [:, :, 2 ]) and \
1923
+ np .array_equal (gray_np , gray_np_2 [:, :, 0 ]):
1924
+ num_gray = num_gray + 1
1925
+
1926
+ p_value = stats .binom_test (num_gray , num_samples , p = 0.5 )
1927
+ random .setstate (random_state )
1928
+ assert p_value > 0.0001
1929
+
1930
+ # Test Set 2: grayscale -> 1 channel grayscale
1931
+ random_state = random .getstate ()
1932
+ random .seed (42 )
1933
+ x_shape = [2 , 2 , 3 ]
1934
+ x_np = np .random .randint (0 , 256 , x_shape , np .uint8 )
1935
+ x_pil = Image .fromarray (x_np , mode = 'RGB' )
1936
+ x_pil_2 = x_pil .convert ('L' )
1937
+ gray_np = np .array (x_pil_2 )
1938
+
1939
+ num_samples = 250
1940
+ num_gray = 0
1941
+ for _ in range (num_samples ):
1942
+ gray_pil_3 = transforms .RandomGrayscale (p = 0.5 )(x_pil_2 )
1943
+ gray_np_3 = np .array (gray_pil_3 )
1944
+ if np .array_equal (gray_np , gray_np_3 ):
1945
+ num_gray = num_gray + 1
1946
+
1947
+ p_value = stats .binom_test (num_gray , num_samples , p = 1.0 ) # Note: grayscale is always unchanged
1948
+ random .setstate (random_state )
1949
+ assert p_value > 0.0001
1950
+
1951
+ # Test set 3: Explicit tests
1952
+ x_shape = [2 , 2 , 3 ]
1953
+ x_data = [0 , 5 , 13 , 54 , 135 , 226 , 37 , 8 , 234 , 90 , 255 , 1 ]
1954
+ x_np = np .array (x_data , dtype = np .uint8 ).reshape (x_shape )
1955
+ x_pil = Image .fromarray (x_np , mode = 'RGB' )
1956
+ x_pil_2 = x_pil .convert ('L' )
1957
+ gray_np = np .array (x_pil_2 )
1958
+
1959
+ # Case 3a: RGB -> 3 channel grayscale (grayscaled)
1960
+ trans2 = transforms .RandomGrayscale (p = 1.0 )
1961
+ gray_pil_2 = trans2 (x_pil )
1962
+ gray_np_2 = np .array (gray_pil_2 )
1963
+ assert gray_pil_2 .mode == 'RGB' , 'mode should be RGB'
1964
+ assert gray_np_2 .shape == tuple (x_shape ), 'should be 3 channel'
1965
+ assert_equal (gray_np_2 [:, :, 0 ], gray_np_2 [:, :, 1 ])
1966
+ assert_equal (gray_np_2 [:, :, 1 ], gray_np_2 [:, :, 2 ])
1967
+ assert_equal (gray_np , gray_np_2 [:, :, 0 ], check_stride = False )
1968
+
1969
+ # Case 3b: RGB -> 3 channel grayscale (unchanged)
1970
+ trans2 = transforms .RandomGrayscale (p = 0.0 )
1971
+ gray_pil_2 = trans2 (x_pil )
1972
+ gray_np_2 = np .array (gray_pil_2 )
1973
+ assert gray_pil_2 .mode == 'RGB' , 'mode should be RGB'
1974
+ assert gray_np_2 .shape == tuple (x_shape ), 'should be 3 channel'
1975
+ assert_equal (x_np , gray_np_2 )
1976
+
1977
+ # Case 3c: 1 channel grayscale -> 1 channel grayscale (grayscaled)
1978
+ trans3 = transforms .RandomGrayscale (p = 1.0 )
1979
+ gray_pil_3 = trans3 (x_pil_2 )
1980
+ gray_np_3 = np .array (gray_pil_3 )
1981
+ assert gray_pil_3 .mode == 'L' , 'mode should be L'
1982
+ assert gray_np_3 .shape == tuple (x_shape [0 :2 ]), 'should be 1 channel'
1983
+ assert_equal (gray_np , gray_np_3 )
1984
+
1985
+ # Case 3d: 1 channel grayscale -> 1 channel grayscale (unchanged)
1986
+ trans3 = transforms .RandomGrayscale (p = 0.0 )
1987
+ gray_pil_3 = trans3 (x_pil_2 )
1988
+ gray_np_3 = np .array (gray_pil_3 )
1989
+ assert gray_pil_3 .mode == 'L' , 'mode should be L'
1990
+ assert gray_np_3 .shape == tuple (x_shape [0 :2 ]), 'should be 1 channel'
1991
+ assert_equal (gray_np , gray_np_3 )
1992
+
1993
+ # Checking if RandomGrayscale can be printed as string
1994
+ trans3 .__repr__ ()
1995
+
1996
+
1995
1997
if __name__ == '__main__' :
1996
1998
unittest .main ()
0 commit comments