【问题标题】:Custom implementation of tf.gather in Graph mode图形模式下 tf.gather 的自定义实现
【发布时间】:2022-10-24 21:30:05
【问题描述】:

我需要使用基本张量流操作编写 tf.gather 的自定义实现。我无法使用 tensorflow 2.2.0 提供的标准 tf.gather 实现,也无法升级我的 tensorflow 版本。由于我的模型需要转换为 .tflite,因此需要在图形模式下实现。

我可以使用的一些基本 tensorflow 操作是:add、concat、fill、mul、split、strided_slice、sub 和 unpack。

谢谢!

【问题讨论】:

  • 请提供足够的代码,以便其他人可以更好地理解或重现该问题。

标签: python tensorflow keras tensorflow2.0


【解决方案1】:

您可以组合张量切片或处理类似的序列。

示例:重新缩放和对数缩放图片。

当我们的公共微笑时,信息收集的新方式变得简单。

import tensorflow as tf
import matplotlib.pyplot as plt

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
None
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
config = tf.config.experimental.set_memory_growth(physical_devices[0], True)
print(physical_devices)
print(config)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
start = 0
limit = 20
delta = 10

log_scales_height = 288
log_scales_width = 144

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Working
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
IMAGE_1 = plt.imread("F:\Pictures\actor-Ploy\7-5.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("F:\Pictures\actor-Ploy\7-5.jpg")
params = tf.image.resize(params, [600, 300])
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 = 180000

indices = tf.range(start, limit, delta)
result = tf.gather(params, indices)     
result = tf.reshape( result, (150, 120, 3) )
result = tf.keras.preprocessing.image.array_to_img(
        result,
        data_format=None,
        scale=True
    )

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('...')

【讨论】:

  • 我不确定我是否理解这个问题的答案,因为您仍在使用 tf.gather 来收集您的对数图片。您是否使用 tf.gather 只是为了表明它有效并且可以通过 tf.range 以某种方式完成?
  • 我是我们如何使用它的示例,您可以创建像素和边界集合以进行计算,例如她的脸颊上的非对称形状。
猜你喜欢
  • 2018-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
相关资源
最近更新 更多