Reputation: 69
when I try to run the following code:
def pixel_drop(image, drop_rate=0.5):
img_h, img_w, _ = image.shape
pixel_count = img_h * img_w
drop_num = pixel_count * drop_rate
for drop in range(int(drop_num)):
rand_x = random.randint(0, img_h - 1)
rand_y = random.randint(0, img_w - 1)
image[rand_x, rand_y,:] = 0
return image
I seem to get the following error:
TypeError: 'Tensor' object does not support item assignment
It looks like I can't assign things to a tensor. How should I go about implementing this?
Upvotes: 0
Views: 111
Reputation: 3197
This notebook has the details about how to assign
values to different variables and constants.
This example assigns
zeros of the appropriate shape to the tensor. But you may have a different type of variable in your code.
import tensorflow as tf
import numpy as np
def pixel_drop(image, drop_rate=0.5):
img_h, img_w, _ = image.shape
pixel_count = img_h * img_w
drop_num = pixel_count * drop_rate
for drop in range(int(drop_num)):
rand_x = np.random.randint(0, img_h - 1)
rand_y = np.random.randint(0, img_w - 1)
image[rand_x, rand_y,:].assign(tf.zeros(shape=(3,)))
return image
img_data = tf.Variable(tf.random.uniform((100, 100, 3)))
print(pixel_drop(img_data))
Upvotes: 1