I have a deep network using Keras and I need to apply cropping on the output of one layer and then send to the next layer. for this aim, I write the following code as a lambda layer:
def cropping_fillzero(img, rate=0.6): # Q = percentage
residual_shape = img.get_shape().as_list()
h, w = residual_shape[1:3]
blacked_pixels = int((rate) * (h*w))
ratio = int(np.floor(np.sqrt(blacked_pixels)))
# width = int(np.ceil(np.sqrt(blacked_pixels)))
x = np.random.randint(0, w - ratio)
y = np.random.randint(0, h - ratio)
cropingImg = img[y:y+ratio, x:x+ratio].assign(tf.zeros([ratio, ratio]))
return cropingImg
decoded_noise = layers.Lambda(cropping_fillzero, arguments={'rate':residual_cropRate}, name='residual_cropout_attack')(bncv11)
but it produces the following error and I do not know why?!
Traceback (most recent call last):
File "", line 1, in runfile('E:/code_v28-7-19/CIFAR_los4x4convolvedw_5_cropAttack_RandomSize_Pad.py', wdir='E:/code_v28-7-19')
File "D:\software\Anaconda3\envs\py36\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile execfile(filename, namespace)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace)
File "E:/code_v28-7-19/CIFAR_los4x4convolvedw_5_cropAttack_RandomSize_Pad.py", line 143, in decoded_noise = layers.Lambda(cropping_fillzero, arguments={'rate':residual_cropRate}, name='residual_cropout_attack')(bncv11)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\base_layer.py", line 457, in call output = self.call(inputs, **kwargs)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\layers\core.py", line 687, in call return self.function(inputs, **arguments)
File "E:/code_v28-7-19/CIFAR_los4x4convolvedw_5_cropAttack_RandomSize_Pad.py", line 48, in cropping_fillzero cropingImg = img[y:y+ratio, x:x+ratio].assign(tf.zeros([ratio, ratio]))
File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\ops\array_ops.py", line 700, in assign raise ValueError("Sliced assignment is only supported for variables")
ValueError: Sliced assignment is only supported for variables
could you please tell me how can I solve this error? Thank you
img[y:y=ratio,x:x+ratio].assign(...)
) is only supported for Tensorflow Variable objects, not for Tensor objects. – Jos