@@ -14,6 +14,7 @@ def test_layer(self):
14
14
layers .RandomContrast ,
15
15
init_kwargs = {
16
16
"factor" : 0.75 ,
17
+ "value_range" : (0 , 255 ),
17
18
"seed" : 1 ,
18
19
},
19
20
input_shape = (8 , 3 , 4 , 3 ),
@@ -24,6 +25,7 @@ def test_layer(self):
24
25
layers .RandomContrast ,
25
26
init_kwargs = {
26
27
"factor" : 0.75 ,
28
+ "value_range" : (0 , 255 ),
27
29
"seed" : 1 ,
28
30
"data_format" : "channels_first" ,
29
31
},
@@ -32,21 +34,67 @@ def test_layer(self):
32
34
expected_output_shape = (8 , 3 , 4 , 4 ),
33
35
)
34
36
35
- def test_random_contrast (self ):
37
+ def test_random_contrast_with_value_range_0_to_255 (self ):
36
38
seed = 9809
37
39
np .random .seed (seed )
38
- inputs = np .random .random ((12 , 8 , 16 , 3 ))
39
- layer = layers .RandomContrast (factor = 0.5 , seed = seed )
40
- outputs = layer (inputs )
40
+
41
+ data_format = backend .config .image_data_format ()
42
+ if data_format == "channels_last" :
43
+ inputs = np .random .random ((12 , 8 , 16 , 3 ))
44
+ height_axis = - 3
45
+ width_axis = - 2
46
+ else :
47
+ inputs = np .random .random ((12 , 3 , 8 , 16 ))
48
+ height_axis = - 2
49
+ width_axis = - 1
50
+
51
+ inputs = backend .convert_to_tensor (inputs , dtype = "float32" )
52
+ layer = layers .RandomContrast (
53
+ factor = 0.5 , value_range = (0 , 255 ), seed = seed
54
+ )
55
+ transformation = layer .get_random_transformation (inputs , training = True )
56
+ outputs = layer .transform_images (inputs , transformation , training = True )
57
+
58
+ # Actual contrast arithmetic
59
+ np .random .seed (seed )
60
+ factor = backend .convert_to_numpy (transformation ["contrast_factor" ])
61
+ inputs = backend .convert_to_numpy (inputs )
62
+ inp_mean = np .mean (inputs , axis = height_axis , keepdims = True )
63
+ inp_mean = np .mean (inp_mean , axis = width_axis , keepdims = True )
64
+ actual_outputs = (inputs - inp_mean ) * factor + inp_mean
65
+ outputs = backend .convert_to_numpy (outputs )
66
+ actual_outputs = np .clip (actual_outputs , 0 , 255 )
67
+
68
+ self .assertAllClose (outputs , actual_outputs )
69
+
70
+ def test_random_contrast_with_value_range_0_to_1 (self ):
71
+ seed = 9809
72
+ np .random .seed (seed )
73
+
74
+ data_format = backend .config .image_data_format ()
75
+ if data_format == "channels_last" :
76
+ inputs = np .random .random ((12 , 8 , 16 , 3 ))
77
+ height_axis = - 3
78
+ width_axis = - 2
79
+ else :
80
+ inputs = np .random .random ((12 , 3 , 8 , 16 ))
81
+ height_axis = - 2
82
+ width_axis = - 1
83
+
84
+ inputs = backend .convert_to_tensor (inputs , dtype = "float32" )
85
+ layer = layers .RandomContrast (factor = 0.5 , value_range = (0 , 1 ), seed = seed )
86
+ transformation = layer .get_random_transformation (inputs , training = True )
87
+ outputs = layer .transform_images (inputs , transformation , training = True )
41
88
42
89
# Actual contrast arithmetic
43
90
np .random .seed (seed )
44
- factor = np .random .uniform (0.5 , 1.5 )
45
- inp_mean = np .mean (inputs , axis = - 3 , keepdims = True )
46
- inp_mean = np .mean (inp_mean , axis = - 2 , keepdims = True )
91
+ factor = backend .convert_to_numpy (transformation ["contrast_factor" ])
92
+ inputs = backend .convert_to_numpy (inputs )
93
+ inp_mean = np .mean (inputs , axis = height_axis , keepdims = True )
94
+ inp_mean = np .mean (inp_mean , axis = width_axis , keepdims = True )
47
95
actual_outputs = (inputs - inp_mean ) * factor + inp_mean
48
96
outputs = backend .convert_to_numpy (outputs )
49
- actual_outputs = np .clip (outputs , 0 , 255 )
97
+ actual_outputs = np .clip (actual_outputs , 0 , 1 )
50
98
51
99
self .assertAllClose (outputs , actual_outputs )
52
100
0 commit comments