@@ -14,6 +14,7 @@ def test_layer(self):
1414 layers .RandomContrast ,
1515 init_kwargs = {
1616 "factor" : 0.75 ,
17+ "value_range" : (0 , 255 ),
1718 "seed" : 1 ,
1819 },
1920 input_shape = (8 , 3 , 4 , 3 ),
@@ -24,6 +25,7 @@ def test_layer(self):
2425 layers .RandomContrast ,
2526 init_kwargs = {
2627 "factor" : 0.75 ,
28+ "value_range" : (0 , 255 ),
2729 "seed" : 1 ,
2830 "data_format" : "channels_first" ,
2931 },
@@ -32,21 +34,67 @@ def test_layer(self):
3234 expected_output_shape = (8 , 3 , 4 , 4 ),
3335 )
3436
35- def test_random_contrast (self ):
37+ def test_random_contrast_with_value_range_0_to_255 (self ):
3638 seed = 9809
3739 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 )
4188
4289 # Actual contrast arithmetic
4390 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 )
4795 actual_outputs = (inputs - inp_mean ) * factor + inp_mean
4896 outputs = backend .convert_to_numpy (outputs )
49- actual_outputs = np .clip (outputs , 0 , 255 )
97+ actual_outputs = np .clip (actual_outputs , 0 , 1 )
5098
5199 self .assertAllClose (outputs , actual_outputs )
52100
0 commit comments