'How to implement tf.gather with some other tf ops
I want to replace tf.gather with some simple and common tf ops like concat,stack,reshape,slice etc to achieve the same result, because tf.gather is not supported in some deployment framework currently(e.g. the operators that ncnn supports).
Test code:
import numpy as np
import tensorflow as tf
params = np.random.rand(5, 2)
params = tf.constant(params)
print("==>> params: ", params)
indices = np.random.randint(low=0,high=5, size=(3,3))
indices = tf.constant(indices)
print("==>> indices: ", indices)
result = tf.gather(params, indices) # to reimplement it
print("==>> result: ", result)
print("==>> result.shape: ", result.shape)
Solution 1:[1]
It is easy, they can create two different images with eye-seeing information or as in the video game we play for safe available bandwidths. Tf.Gather can implement in many useful ways that include selective information.
[ Sample ]:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Working
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
IMAGE_1 = plt.imread("C:\\Users\\Jirayu Kaewprateep\\Pictures\\Cats\\206480490_10158968022281077_7829901115614154740_n_10158968022276077.jpg")
IMAGE_1 = tf.image.resize(IMAGE_1, [log_scales_height, log_scales_width]).numpy()
IMAGE_1 = tf.constant( IMAGE_1, dtype=tf.int32 ).numpy()
params = plt.imread("C:\\Users\\Jirayu Kaewprateep\\Pictures\\Cats\\206480490_10158968022281077_7829901115614154740_n_10158968022276077.jpg")
params = tf.reshape( params, ( params.shape[0] * params.shape[1], 3 ))
limit = tf.math.divide( params.shape[0] * params.shape[1], delta, name='Devide' ).numpy()
limit = 414720
limit = 691200
indices = tf.range(start, limit, delta)
result = tf.gather(params, indices)
result = tf.reshape( result, (240, 288, 3) )
plt.figure(figsize=(1,2))
plt.subplot(1,2,1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(IMAGE_1)
plt.xlabel(" rescaling ")
plt.subplot(1,2,2)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(result)
plt.xlabel(" logarithms pictires ")
plt.show()
input('...')
[ Output ]: See the attache picture.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Martijn Pieters |