You can create constants or variables in TensorFlow.
black_img = np.zeros([5, 5, 3], dtype = np.int32)
black_img_tf = tf.constant(np.zeros([5, 5, 3], dtype = np.int32))
Output:
<tf.Tensor: shape=(5, 5, 3), dtype=int32, numpy=
array([[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]])>
However, TensorFlow does not support item assignment like NumPy.
However, you can create a new constant or variable using results obtained from NumPy operations.
Code:
black_img[size, size] = white_rect
tf.constant(black_img)
Output:
<tf.Tensor: shape=(5, 5, 3), dtype=int32, numpy=
array([[[1, 1, 1],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[1, 1, 1],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[1, 1, 1],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]])>
Note that individual item assignment is not allowed but you can do operations like addition, subtraction on tensors.
c = tf.constant(np.ones([3, 3], dtype = np.int32)) + tf.constant(np.ones([3, 3], dtype = np.int32))
c
Output:
<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[2, 2, 2],
[2, 2, 2],
[2, 2, 2]])>