-
Notifications
You must be signed in to change notification settings - Fork 332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update the API design on vectorization #1385
Comments
Thanks @bhack ! good catch! |
Also it seems that we were not really using the claimed within the batch augmentation or not #1382 (comment)? |
Yes I think it was basically wrong: import tensorflow as tf
def test_map(input):
print("Call to test")
return tf.constant([1])
def test_vectorized_map(input):
print("Call to test vectorized")
return tf.constant([1])
input_shape = (5, 2, 2, 1)
batch = tf.random.uniform(input_shape)
input = {"input": batch}
c = tf.map_fn(test_map, input, fn_output_signature=tf.int32)
print(c)
print("-----------------")
c = tf.vectorized_map(test_vectorized_map, input)
print(c) Call to test
Call to test
Call to test
Call to test
Call to test
tf.Tensor(
[[1]
[1]
[1]
[1]
[1]], shape=(5, 1), dtype=int32)
-----------------
Call to test vectorized
tf.Tensor(
[[1]
[1]
[1]
[1]
[1]], shape=(5, 1), dtype=int32) |
@bhack I'm not sure. Isn't There were Let's consider a different test: two identical images, wrapped in a batch and passed to the current implementation of If the import tensorflow as tf
from keras_cv.layers import RandomContrast
image = tf.random.uniform((224, 224, 3))
batch = tf.stack([image, image]) # (2, 224, 224, 3)
tf.debugging.assert_near(batch[0], batch[1]) # OK
l = RandomContrast(0.75)
outputs = l(batch)
tf.debugging.assert_near(outputs[0], outputs[1]) # InvalidArgumentError The second assert will raise exception, indicating that the same image, in a single batch received different augmentation. |
Ok so probabably it is internally wrapped in graph mode in import tensorflow as tf
def test_map(input):
return input["input"]+tf.random.uniform([])
def test_vectorized_map(input):
return input["input"]+tf.random.uniform([])
input_shape = (5, 1,)
batch = tf.zeros(input_shape)
input = {"input": batch}
c = tf.map_fn(test_map, input, fn_output_signature=tf.float32)
print(c)
print("-----------------")
c = tf.vectorized_map(test_vectorized_map, input)
print(c) tf.Tensor(
[[0.17567658]
[0.90278184]
[0.5280751 ]
[0.8226601 ]
[0.7671194 ]], shape=(5, 1), dtype=float32)
-----------------
tf.Tensor(
[[0.9258783 ]
[0.39560175]
[0.8852272 ]
[0.09358847]
[0.03888452]], shape=(5, 1), dtype=float32) |
The images are augmented sample wise - replace print with a tf.print() and you’ll get your expected result. |
No you will not get the expected result with a But the random op call inside the graph node is valid. Check yourself with this gist: import tensorflow as tf
def test_map(input):
print("Standard function")
return input["input"]+tf.random.uniform([])
def test_vectorized_map(input):
tf.print("Vectorized function")
return input["input"]+tf.random.uniform([])
input_shape = (5, 1,)
batch = tf.zeros(input_shape)
input = {"input": batch}
c = tf.map_fn(test_map, input, fn_output_signature=tf.float32)
print(c)
print("-----------------")
c = tf.vectorized_map(test_vectorized_map, input)
print(c) Standard function
Standard function
Standard function
Standard function
Standard function
tf.Tensor(
[[0.6856414 ]
[0.3985964 ]
[0.12619197]
[0.5390271 ]
[0.23261154]], shape=(5, 1), dtype=float32)
-----------------
Vectorized function
tf.Tensor(
[[0.5201206 ]
[0.7133293 ]
[0.45491397]
[0.55868816]
[0.9193896 ]], shape=(5, 1), dtype=float32) |
https://github.com/keras-team/keras-cv/blob/master/.github/API_DESIGN.md#vectorization
We need to updated that section of the documentation (#1331 (comment))
/cc @LukeWood
The text was updated successfully, but these errors were encountered: