- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我有两个 3-D 张量 img
和 gen
。如何将 img
的 2D 子集分配给 gen
的 2D 子集?以下内容不起作用,因为 tensorflow 不允许直接分配张量。
img[96:160 , 144:240 , :] = gen[96:160 , 144:240 , :]
编辑:
这是周围的代码。所以我使用自定义的 keras 层。该层必须接收输入图像img
和生成图像x
。它必须用 x
替换部分 img
,并且必须返回修改后的 img
。
def patcher(tensors):
img = tensor[1]
gen = tensor[0]
#This is where the slicing must happen
img[96:160 , 144:240 , :] = gen[96:160 , 144:240 , :]
return [img]
img = Input( .. )
x = Conv( .. )(img)
out = Lambda(patcher,lambda a : [a[1]] )([x , img])
model = Model(img, out)
最佳答案
目前,您无法以直接的方式替换张量的切片。我其实opened an issue about it因为这是人们一直要求的东西。使用当前的 API,您必须找出构建所需张量的最佳方法。在本例中,假设 img
和 gen
具有相同的形状,这是您可以实现的一种方法:
import tensorflow as tf
import numpy as np
# Input
img = tf.placeholder(tf.float32, [None, None, None])
gen = tf.placeholder(tf.float32, [None, None, None])
row_start = tf.placeholder(tf.int32, [])
row_end = tf.placeholder(tf.int32, [])
col_start = tf.placeholder(tf.int32, [])
col_end = tf.placeholder(tf.int32, [])
# Masks rows and columns to be replaced
shape = tf.shape(img)
rows = shape[0]
cols = shape[1]
channels = shape[2]
i = tf.range(rows)
row_mask = (row_start <= i) & (i < row_end)
j = tf.range(cols)
col_mask = (col_start <= j) & (j < col_end)
# Full mask of replaced elements
mask = row_mask[:, tf.newaxis] & col_mask
# Select elements from flattened arrays
img_flat = tf.reshape(img, [-1, channels])
gen_flat = tf.reshape(gen, [-1, channels])
mask_flat = tf.reshape(mask, [-1])
result_flat = tf.where(mask_flat, gen_flat, img_flat)
# Reshape back
result = tf.reshape(result_flat, shape)
这是一个小测试:
with tf.Session() as sess:
# img is positive and gen is negative
img_val = np.arange(60).reshape((4, 5, 3))
gen_val = -img_val
# Do img[2:4, 0:3, :] = gen[2:4, 0:3, :]
result_val = sess.run(result, feed_dict={
img: img_val,
gen: gen_val,
row_start: 2,
row_end: 4,
col_start: 0,
col_end: 3,
})
# Print one channel only for clarity
print(result_val[:, :, 0])
输出:
[[ 0. 3. 6. 9. 12.]
[ 15. 18. 21. 24. 27.]
[-30. -33. -36. 39. 42.]
[-45. -48. -51. 54. 57.]]
<小时/>
编辑:
这是您发布的代码的可能实现。我在这里使用基于乘法的稍微不同的方法,我认为当您有很多图像时,这种方法更好。
import tensorflow as tf
def replace_slices(img, gen, row_start, row_end, col_start, col_end):
# Masks rows and columns to be replaced
shape = tf.shape(img)
rows = shape[1]
cols = shape[2]
i = tf.range(rows)
row_mask = (row_start <= i) & (i < row_end)
j = tf.range(cols)
col_mask = (col_start <= j) & (j < col_end)
# Full mask of replaced elements
mask = row_mask[:, tf.newaxis] & col_mask
# Add channel dimension to mask and cast
mask = tf.cast(mask[:, :, tf.newaxis], img.dtype)
# Compute result
result = img * (1 - mask) + gen * mask
return result
def patcher(tensors):
img = tensor[1]
gen = tensor[0]
img = replace_slices(img, gen, 96, 160, 144, 240)
return [img]
img = Input( .. )
x = Conv( .. )(img)
out = Lambda(patcher, ambda a: [a[1]])([x , img])
model = Model(img, out)
关于python - 如何在 Keras 中将张量的 2D 子集分配给另一个 2D 张量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53144166/
我想矢量化以下代码: def style_noise(self, y, style): n = torch.randn(y.shape) for i in range(n.shape[
对于给定的二维张量,我想检索值为 1 的所有索引。我希望能够简单地使用 torch.nonzero(a == 1).squeeze(),它将返回张量([1, 3, 2])。但是,torch.nonze
如果 x 是 dtype torch.float 的 torch.Tensor 那么操作 x.item() 和 float(x)完全一样? 最佳答案 操作x.item() 和float(x) 是不一样
我正在尝试提取 n 点 3D 坐标和 b 批处理中的特定行。本质上,我的张量 T1 的形状为 b*n*3。我有另一个形状为 b * n 的 bool 张量 T2,指示需要获取 n 的哪些行。本质上我的
以下代码掩码很好 mask = targets >= 0 targets = targets[mask] 但是,当我尝试使用两个条件进行屏蔽时,它会给出 RuntimeError: Boolean v
我正在定义一个简单的 conv2d 函数来计算输入和内核(均为 2D 张量)之间的互相关,如下所示: import torch def conv2D(X, K): h = K.shape[0]
作为训练 CNN 的一部分,我正在使用数组 inputs包含 对象。我想轮换一个人一些随机度数的对象 x ,如下所示: def rotate(inputs, x): # Rotate inpu
我有一个索引列表和一个具有形状的张量: shape = [batch_size, d_0, d_1, ..., d_k] idx = [i_0, i_1, ..., i_k] 有没有办法用索引 i_0
假设我有张量 t = torch.tensor([1,2,3,4,5]) 我想使用相同大小的索引张量来拆分它,该张量告诉我每个元素应该进行哪个拆分。 indices = torch.tensor([0
我尝试从生成器构建一个张量,如下所示: >>> torch.tensor(i**2 for i in range(10)) Traceback (most recent call last): F
假设我有一个一维 PyTorch 张量 end_index长度为L。 我想构造一个 2D PyTorch 张量 T有 L 行,其中 T[i,j] = 2什么时候j < end_index[i]和 T[
我在 pytorch 中有一个张量 x 比方说形状 (5,3,2,6) 和另一个形状 (5,3,2,1) 的张量 idx,其中包含第一个张量中每个元素的索引。我想用第二个张量的索引对第一个张量进行切片
我有以下火炬张量: tensor([[-0.2, 0.3], [-0.5, 0.1], [-0.4, 0.2]]) 以及以下 numpy 数组:(如有必要,我可以将其转换为其他内
tf.data.Dataset的构造函数接受一个参数 variant_tensor ,这只是 documented as : A DT_VARIANT tensor that represents t
我有: inp = torch.randn(4, 1040, 161) 我还有另一个名为 indices 的张量值: tensor([[124, 583, 158, 529], [1
我有一个张量 inps ,其大小为 [64, 161, 1]我有一些新数据d大小为 [64, 161] .如何添加 d至inps这样新的大小是[64, 161, 2] ? 最佳答案 使用 .unsqu
我有张量 t = torch.tensor([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]]) 和一个查询张量 q = torch.te
给定一个 3d 张量,说:batch x sentence length x embedding dim a = torch.rand((10, 1000, 96)) 以及每个句子的实际长度数组(或张
我想使用 [int, -1] 符号 reshape 张量(例如,压平图像)。但我事先并不知道第一个维度。一个用例是在大批量上进行训练,然后在较小的批量上进行评估。 为什么会出现以下错误:获取包含“_M
我有两个 torch 张量。一个形状为 [64, 4, 300],一个形状为 [64, 300]。我如何连接这两个张量以获得形状为 [64, 5, 300] 的合成张量。我知道用于此的 tensor.
我是一名优秀的程序员,十分优秀!