@@ -422,6 +422,167 @@ def test_random_horizontal_flip(self):
422
422
p_value = stats .binom_test (num_horizontal , 100 , p = 0.5 )
423
423
assert p_value > 0.05
424
424
425
+ def test_adjust_brightness (self ):
426
+ x_shape = [2 , 2 , 3 ]
427
+ x_data = [0 , 5 , 13 , 54 , 135 , 226 , 37 , 8 , 234 , 90 , 255 , 1 ]
428
+ x_np = np .array (x_data , dtype = np .uint8 ).reshape (x_shape )
429
+ x_pil = Image .fromarray (x_np , mode = 'RGB' )
430
+
431
+ # test 0
432
+ y_pil = transforms .adjust_brightness (x_pil , 1 )
433
+ y_np = np .array (y_pil )
434
+ assert np .allclose (y_np , x_np )
435
+
436
+ # test 1
437
+ y_pil = transforms .adjust_brightness (x_pil , 0.5 )
438
+ y_np = np .array (y_pil )
439
+ y_ans = [0 , 2 , 6 , 27 , 67 , 113 , 18 , 4 , 117 , 45 , 127 , 0 ]
440
+ y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
441
+ assert np .allclose (y_np , y_ans )
442
+
443
+ # test 2
444
+ y_pil = transforms .adjust_brightness (x_pil , 2 )
445
+ y_np = np .array (y_pil )
446
+ y_ans = [0 , 10 , 26 , 108 , 255 , 255 , 74 , 16 , 255 , 180 , 255 , 2 ]
447
+ y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
448
+ assert np .allclose (y_np , y_ans )
449
+
450
+ def test_adjust_contrast (self ):
451
+ x_shape = [2 , 2 , 3 ]
452
+ x_data = [0 , 5 , 13 , 54 , 135 , 226 , 37 , 8 , 234 , 90 , 255 , 1 ]
453
+ x_np = np .array (x_data , dtype = np .uint8 ).reshape (x_shape )
454
+ x_pil = Image .fromarray (x_np , mode = 'RGB' )
455
+
456
+ # test 0
457
+ y_pil = transforms .adjust_contrast (x_pil , 1 )
458
+ y_np = np .array (y_pil )
459
+ assert np .allclose (y_np , x_np )
460
+
461
+ # test 1
462
+ y_pil = transforms .adjust_contrast (x_pil , 0.5 )
463
+ y_np = np .array (y_pil )
464
+ y_ans = [43 , 45 , 49 , 70 , 110 , 156 , 61 , 47 , 160 , 88 , 170 , 43 ]
465
+ y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
466
+ assert np .allclose (y_np , y_ans )
467
+
468
+ # test 2
469
+ y_pil = transforms .adjust_contrast (x_pil , 2 )
470
+ y_np = np .array (y_pil )
471
+ y_ans = [0 , 0 , 0 , 22 , 184 , 255 , 0 , 0 , 255 , 94 , 255 , 0 ]
472
+ y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
473
+ assert np .allclose (y_np , y_ans )
474
+
475
+ def test_adjust_saturation (self ):
476
+ x_shape = [2 , 2 , 3 ]
477
+ x_data = [0 , 5 , 13 , 54 , 135 , 226 , 37 , 8 , 234 , 90 , 255 , 1 ]
478
+ x_np = np .array (x_data , dtype = np .uint8 ).reshape (x_shape )
479
+ x_pil = Image .fromarray (x_np , mode = 'RGB' )
480
+
481
+ # test 0
482
+ y_pil = transforms .adjust_saturation (x_pil , 1 )
483
+ y_np = np .array (y_pil )
484
+ assert np .allclose (y_np , x_np )
485
+
486
+ # test 1
487
+ y_pil = transforms .adjust_saturation (x_pil , 0.5 )
488
+ y_np = np .array (y_pil )
489
+ y_ans = [2 , 4 , 8 , 87 , 128 , 173 , 39 , 25 , 138 , 133 , 215 , 88 ]
490
+ y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
491
+ assert np .allclose (y_np , y_ans )
492
+
493
+ # test 2
494
+ y_pil = transforms .adjust_saturation (x_pil , 2 )
495
+ y_np = np .array (y_pil )
496
+ y_ans = [0 , 6 , 22 , 0 , 149 , 255 , 32 , 0 , 255 , 4 , 255 , 0 ]
497
+ y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
498
+ assert np .allclose (y_np , y_ans )
499
+
500
+ def test_adjust_hue (self ):
501
+ x_shape = [2 , 2 , 3 ]
502
+ x_data = [0 , 5 , 13 , 54 , 135 , 226 , 37 , 8 , 234 , 90 , 255 , 1 ]
503
+ x_np = np .array (x_data , dtype = np .uint8 ).reshape (x_shape )
504
+ x_pil = Image .fromarray (x_np , mode = 'RGB' )
505
+
506
+ with self .assertRaises (ValueError ):
507
+ transforms .adjust_hue (x_pil , - 0.7 )
508
+ transforms .adjust_hue (x_pil , 1 )
509
+
510
+ # test 0: almost same as x_data but not exact.
511
+ # probably because hsv <-> rgb floating point ops
512
+ y_pil = transforms .adjust_hue (x_pil , 0 )
513
+ y_np = np .array (y_pil )
514
+ y_ans = [0 , 5 , 13 , 54 , 139 , 226 , 35 , 8 , 234 , 91 , 255 , 1 ]
515
+ y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
516
+ assert np .allclose (y_np , y_ans )
517
+
518
+ # test 1
519
+ y_pil = transforms .adjust_hue (x_pil , 0.25 )
520
+ y_np = np .array (y_pil )
521
+ y_ans = [13 , 0 , 12 , 224 , 54 , 226 , 234 , 8 , 99 , 1 , 222 , 255 ]
522
+ y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
523
+ assert np .allclose (y_np , y_ans )
524
+
525
+ # test 2
526
+ y_pil = transforms .adjust_hue (x_pil , - 0.25 )
527
+ y_np = np .array (y_pil )
528
+ y_ans = [0 , 13 , 2 , 54 , 226 , 58 , 8 , 234 , 152 , 255 , 43 , 1 ]
529
+ y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
530
+ assert np .allclose (y_np , y_ans )
531
+
532
+ def test_adjust_gamma (self ):
533
+ x_shape = [2 , 2 , 3 ]
534
+ x_data = [0 , 5 , 13 , 54 , 135 , 226 , 37 , 8 , 234 , 90 , 255 , 1 ]
535
+ x_np = np .array (x_data , dtype = np .uint8 ).reshape (x_shape )
536
+ x_pil = Image .fromarray (x_np , mode = 'RGB' )
537
+
538
+ # test 0
539
+ y_pil = transforms .adjust_gamma (x_pil , 1 )
540
+ y_np = np .array (y_pil )
541
+ assert np .allclose (y_np , x_np )
542
+
543
+ # test 1
544
+ y_pil = transforms .adjust_gamma (x_pil , 0.5 )
545
+ y_np = np .array (y_pil )
546
+ y_ans = [0 , 35 , 57 , 117 , 185 , 240 , 97 , 45 , 244 , 151 , 255 , 15 ]
547
+ y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
548
+ assert np .allclose (y_np , y_ans )
549
+
550
+ # test 2
551
+ y_pil = transforms .adjust_gamma (x_pil , 2 )
552
+ y_np = np .array (y_pil )
553
+ y_ans = [0 , 0 , 0 , 11 , 71 , 200 , 5 , 0 , 214 , 31 , 255 , 0 ]
554
+ y_ans = np .array (y_ans , dtype = np .uint8 ).reshape (x_shape )
555
+ assert np .allclose (y_np , y_ans )
556
+
557
+ def test_adjusts_L_mode (self ):
558
+ x_shape = [2 , 2 , 3 ]
559
+ x_data = [0 , 5 , 13 , 54 , 135 , 226 , 37 , 8 , 234 , 90 , 255 , 1 ]
560
+ x_np = np .array (x_data , dtype = np .uint8 ).reshape (x_shape )
561
+ x_rgb = Image .fromarray (x_np , mode = 'RGB' )
562
+
563
+ x_l = x_rgb .convert ('L' )
564
+ assert transforms .adjust_brightness (x_l , 2 ).mode == 'L'
565
+ assert transforms .adjust_saturation (x_l , 2 ).mode == 'L'
566
+ assert transforms .adjust_contrast (x_l , 2 ).mode == 'L'
567
+ assert transforms .adjust_hue (x_l , 0.4 ).mode == 'L'
568
+ assert transforms .adjust_gamma (x_l , 0.5 ).mode == 'L'
569
+
570
+ def test_color_jitter (self ):
571
+ color_jitter = transforms .ColorJitter (2 , 2 , 2 , 0.1 )
572
+
573
+ x_shape = [2 , 2 , 3 ]
574
+ x_data = [0 , 5 , 13 , 54 , 135 , 226 , 37 , 8 , 234 , 90 , 255 , 1 ]
575
+ x_np = np .array (x_data , dtype = np .uint8 ).reshape (x_shape )
576
+ x_pil = Image .fromarray (x_np , mode = 'RGB' )
577
+ x_pil_2 = x_pil .convert ('L' )
578
+
579
+ for i in range (10 ):
580
+ y_pil = color_jitter (x_pil )
581
+ assert y_pil .mode == x_pil .mode
582
+
583
+ y_pil_2 = color_jitter (x_pil_2 )
584
+ assert y_pil_2 .mode == x_pil_2 .mode
585
+
425
586
426
587
if __name__ == '__main__' :
427
588
unittest .main ()
0 commit comments