1
1
import torch
2
2
import torchvision .transforms as transforms
3
+ import torchvision .transforms .functional as F
3
4
import unittest
4
5
import random
5
6
import numpy as np
14
15
except ImportError :
15
16
stats = None
16
17
17
-
18
18
GRACE_HOPPER = 'assets/grace_hopper_517x606.jpg'
19
19
20
20
@@ -347,7 +347,7 @@ def verify_img_data(img_data, expected_output, mode):
347
347
assert img .mode == mode
348
348
split = img .split ()
349
349
for i in range (3 ):
350
- assert np .allclose (expected_output [i ].numpy (), transforms .to_tensor (split [i ]).numpy ())
350
+ assert np .allclose (expected_output [i ].numpy (), F .to_tensor (split [i ]).numpy ())
351
351
352
352
img_data = torch .Tensor (3 , 4 , 4 ).uniform_ ()
353
353
expected_output = img_data .mul (255 ).int ().float ().div (255 )
@@ -391,7 +391,7 @@ def verify_img_data(img_data, expected_output, mode):
391
391
392
392
split = img .split ()
393
393
for i in range (4 ):
394
- assert np .allclose (expected_output [i ].numpy (), transforms .to_tensor (split [i ]).numpy ())
394
+ assert np .allclose (expected_output [i ].numpy (), F .to_tensor (split [i ]).numpy ())
395
395
396
396
img_data = torch .Tensor (4 , 4 , 4 ).uniform_ ()
397
397
expected_output = img_data .mul (255 ).int ().float ().div (255 )
@@ -491,19 +491,19 @@ def test_adjust_brightness(self):
491
491
x_pil = Image .fromarray (x_np , mode = 'RGB' )
492
492
493
493
# test 0
494
- y_pil = transforms .adjust_brightness (x_pil , 1 )
494
+ y_pil = F .adjust_brightness (x_pil , 1 )
495
495
y_np = np .array (y_pil )
496
496
assert np .allclose (y_np , x_np )
497
497
498
498
# test 1
499
- y_pil = transforms .adjust_brightness (x_pil , 0.5 )
499
+ y_pil = F .adjust_brightness (x_pil , 0.5 )
500
500
y_np = np .array (y_pil )
501
501
y_ans = [0 , 2 , 6 , 27 , 67 , 113 , 18 , 4 , 117 , 45 , 127 , 0 ]
502
502
y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
503
503
assert np .allclose (y_np , y_ans )
504
504
505
505
# test 2
506
- y_pil = transforms .adjust_brightness (x_pil , 2 )
506
+ y_pil = F .adjust_brightness (x_pil , 2 )
507
507
y_np = np .array (y_pil )
508
508
y_ans = [0 , 10 , 26 , 108 , 255 , 255 , 74 , 16 , 255 , 180 , 255 , 2 ]
509
509
y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
@@ -516,19 +516,19 @@ def test_adjust_contrast(self):
516
516
x_pil = Image .fromarray (x_np , mode = 'RGB' )
517
517
518
518
# test 0
519
- y_pil = transforms .adjust_contrast (x_pil , 1 )
519
+ y_pil = F .adjust_contrast (x_pil , 1 )
520
520
y_np = np .array (y_pil )
521
521
assert np .allclose (y_np , x_np )
522
522
523
523
# test 1
524
- y_pil = transforms .adjust_contrast (x_pil , 0.5 )
524
+ y_pil = F .adjust_contrast (x_pil , 0.5 )
525
525
y_np = np .array (y_pil )
526
526
y_ans = [43 , 45 , 49 , 70 , 110 , 156 , 61 , 47 , 160 , 88 , 170 , 43 ]
527
527
y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
528
528
assert np .allclose (y_np , y_ans )
529
529
530
530
# test 2
531
- y_pil = transforms .adjust_contrast (x_pil , 2 )
531
+ y_pil = F .adjust_contrast (x_pil , 2 )
532
532
y_np = np .array (y_pil )
533
533
y_ans = [0 , 0 , 0 , 22 , 184 , 255 , 0 , 0 , 255 , 94 , 255 , 0 ]
534
534
y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
@@ -541,19 +541,19 @@ def test_adjust_saturation(self):
541
541
x_pil = Image .fromarray (x_np , mode = 'RGB' )
542
542
543
543
# test 0
544
- y_pil = transforms .adjust_saturation (x_pil , 1 )
544
+ y_pil = F .adjust_saturation (x_pil , 1 )
545
545
y_np = np .array (y_pil )
546
546
assert np .allclose (y_np , x_np )
547
547
548
548
# test 1
549
- y_pil = transforms .adjust_saturation (x_pil , 0.5 )
549
+ y_pil = F .adjust_saturation (x_pil , 0.5 )
550
550
y_np = np .array (y_pil )
551
551
y_ans = [2 , 4 , 8 , 87 , 128 , 173 , 39 , 25 , 138 , 133 , 215 , 88 ]
552
552
y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
553
553
assert np .allclose (y_np , y_ans )
554
554
555
555
# test 2
556
- y_pil = transforms .adjust_saturation (x_pil , 2 )
556
+ y_pil = F .adjust_saturation (x_pil , 2 )
557
557
y_np = np .array (y_pil )
558
558
y_ans = [0 , 6 , 22 , 0 , 149 , 255 , 32 , 0 , 255 , 4 , 255 , 0 ]
559
559
y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
@@ -566,26 +566,26 @@ def test_adjust_hue(self):
566
566
x_pil = Image .fromarray (x_np , mode = 'RGB' )
567
567
568
568
with self .assertRaises (ValueError ):
569
- transforms .adjust_hue (x_pil , - 0.7 )
570
- transforms .adjust_hue (x_pil , 1 )
569
+ F .adjust_hue (x_pil , - 0.7 )
570
+ F .adjust_hue (x_pil , 1 )
571
571
572
572
# test 0: almost same as x_data but not exact.
573
573
# probably because hsv <-> rgb floating point ops
574
- y_pil = transforms .adjust_hue (x_pil , 0 )
574
+ y_pil = F .adjust_hue (x_pil , 0 )
575
575
y_np = np .array (y_pil )
576
576
y_ans = [0 , 5 , 13 , 54 , 139 , 226 , 35 , 8 , 234 , 91 , 255 , 1 ]
577
577
y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
578
578
assert np .allclose (y_np , y_ans )
579
579
580
580
# test 1
581
- y_pil = transforms .adjust_hue (x_pil , 0.25 )
581
+ y_pil = F .adjust_hue (x_pil , 0.25 )
582
582
y_np = np .array (y_pil )
583
583
y_ans = [13 , 0 , 12 , 224 , 54 , 226 , 234 , 8 , 99 , 1 , 222 , 255 ]
584
584
y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
585
585
assert np .allclose (y_np , y_ans )
586
586
587
587
# test 2
588
- y_pil = transforms .adjust_hue (x_pil , - 0.25 )
588
+ y_pil = F .adjust_hue (x_pil , - 0.25 )
589
589
y_np = np .array (y_pil )
590
590
y_ans = [0 , 13 , 2 , 54 , 226 , 58 , 8 , 234 , 152 , 255 , 43 , 1 ]
591
591
y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
@@ -598,19 +598,19 @@ def test_adjust_gamma(self):
598
598
x_pil = Image .fromarray (x_np , mode = 'RGB' )
599
599
600
600
# test 0
601
- y_pil = transforms .adjust_gamma (x_pil , 1 )
601
+ y_pil = F .adjust_gamma (x_pil , 1 )
602
602
y_np = np .array (y_pil )
603
603
assert np .allclose (y_np , x_np )
604
604
605
605
# test 1
606
- y_pil = transforms .adjust_gamma (x_pil , 0.5 )
606
+ y_pil = F .adjust_gamma (x_pil , 0.5 )
607
607
y_np = np .array (y_pil )
608
608
y_ans = [0 , 35 , 57 , 117 , 185 , 240 , 97 , 45 , 244 , 151 , 255 , 15 ]
609
609
y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
610
610
assert np .allclose (y_np , y_ans )
611
611
612
612
# test 2
613
- y_pil = transforms .adjust_gamma (x_pil , 2 )
613
+ y_pil = F .adjust_gamma (x_pil , 2 )
614
614
y_np = np .array (y_pil )
615
615
y_ans = [0 , 0 , 0 , 11 , 71 , 200 , 5 , 0 , 214 , 31 , 255 , 0 ]
616
616
y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
@@ -623,11 +623,11 @@ def test_adjusts_L_mode(self):
623
623
x_rgb = Image .fromarray (x_np , mode = 'RGB' )
624
624
625
625
x_l = x_rgb .convert ('L' )
626
- assert transforms .adjust_brightness (x_l , 2 ).mode == 'L'
627
- assert transforms .adjust_saturation (x_l , 2 ).mode == 'L'
628
- assert transforms .adjust_contrast (x_l , 2 ).mode == 'L'
629
- assert transforms .adjust_hue (x_l , 0.4 ).mode == 'L'
630
- assert transforms .adjust_gamma (x_l , 0.5 ).mode == 'L'
626
+ assert F .adjust_brightness (x_l , 2 ).mode == 'L'
627
+ assert F .adjust_saturation (x_l , 2 ).mode == 'L'
628
+ assert F .adjust_contrast (x_l , 2 ).mode == 'L'
629
+ assert F .adjust_hue (x_l , 0.4 ).mode == 'L'
630
+ assert F .adjust_gamma (x_l , 0.5 ).mode == 'L'
631
631
632
632
def test_color_jitter (self ):
633
633
color_jitter = transforms .ColorJitter (2 , 2 , 2 , 0.1 )
0 commit comments