- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试读取用于训练的自定义映射数据集。但是在我使用 py_function 映射数据集之后,我得到了未知的形状,例如:
def process_path(file_path):
label = get_label(file_path)
img = tf.io.read_file(file_path)
img = decode_img(img)
print('image shape:', img.shape) #this print correctly: image shape: (180, 180, 3)
print('label shape:', label.shape) #this print correctly: label shape: ()
return img, label
train_ds = train_ds.map(lambda x: tf.py_function(process_path, [x], (tf.float32, tf.int32)))
print(train_ds)
# this print unknown shape <PrefetchDataset shapes: (<unknown>, <unknown>), types: (tf.float32, tf.int32)>
这将使 model.fit() 失败,所以我想将数据集 reshape 为正确的形状,例如:
<BatchDataset shapes: ((None, 180, 180, 3), (None,)), types: (tf.float32, tf.int32)>
使用:
train_ds = tf.reshape(train_ds, ((None, 180, 180, 3), (None,)))
但这会报错:
ValueError: Attempt to convert a value (<MapDataset shapes: (<unknown>, <unknown>), types: (tf.float32, tf.int32)>) with an unsupported type (<class 'tensorflow.python.data.ops.dataset_ops.MapDataset'>) to a Tensor.
如何在此步骤中正确分配(图像、标签)形状?
最佳答案
这里不需要py_function
。假设您有一个名为 /dogs
的文件夹,其中充满了 jpg
。您可以使用这两个小函数来加载和解码。
如果文件名(例如,'dogs\\dog1.jpg'
)在文件夹 dogs 中,第一个返回 1
和 0
否则。
第二个函数也接受一个文件名并将其转换为介于 0 和 1 之间的 float 。然后,它还调整图片的大小。
如果有任何不清楚的地方,请告诉我。
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
from glob2 import glob
os.chdir('c:/users/nicol/pictures')
files = glob('*/*jpg')
def get_label(file_path):
split = tf.strings.split(file_path, sep=os.sep)[0]
equal = tf.equal(split, 'dogs')
cast = tf.cast(equal, tf.int32)
return cast
def process_path(file_path):
label = get_label(file_path)
img = tf.io.read_file(file_path)
img = tf.image.decode_jpeg(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.float32)
img = tf.image.resize(img, size=(180, 180))
return img, label
train_ds = tf.data.Dataset.from_tensor_slices(files).map(process_path)
next(iter(train_ds))
(<tf.Tensor: shape=(180, 180, 3), dtype=float32, numpy=
array([[[1.41176477e-01, 9.41176564e-02, 1.33333340e-01],
[1.41176477e-01, 9.41176564e-02, 1.33333340e-01],
[1.41176477e-01, 9.41176564e-02, 1.33333340e-01],
...,
[2.63300300e-01, 2.76176542e-01, 4.67582583e-01],
[2.46176332e-01, 2.59706050e-01, 4.50785339e-01],
[2.54726082e-01, 2.68909693e-01, 4.59662050e-01]]], dtype=float32)>,
<tf.Tensor: shape=(), dtype=int32, numpy=1>)
get_label
应该返回一个整数,如果不是的话。
关于python - 如何在 py_function 之后 reshape (图像,标签)数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63749852/
如何检查字符串是否被 reshape ?示例:“aab”返回 0,因为“a”无法 reshape 为该字符串或任何其他更短的字符串。 另一个例子是“aabbaab”返回 1,因为“aabb”可以被 r
我无法清楚地理解theano的reshape。我有一个形状的图像矩阵: [batch_size, stack1_size, stack2_size, height, width] ,其中有 s
如何检查字符串是否被 reshape ?示例:“aab”返回 0,因为“a”无法 reshape 为该字符串或任何其他更短的字符串。 另一个例子是“aabbaab”返回 1,因为“aabb”可以被 r
这是原始数据 a=[[1,2,3,4,5,6], [7,8,9,10,11,12]] 我想把它转换成这样的格式: b=[[1,2,3,7,8,9], [4,5,6,10,11,12]] a
我目前正在学习 CS231 作业,我意识到一些令人困惑的事情。在计算梯度时,当我第一次 reshape x 然后得到转置时,我得到了正确的结果。 x_r=x.reshape(x.shape[0],-1
这个问题在这里已经有了答案: Reshaping multiple sets of measurement columns (wide format) into single columns (lon
我有一个包含超过 1500 列的宽格式数据集。由于许多变量都是重复的,我想将其 reshape 为长形式。然而,r 抛出一个错误: Error in guess(varying) : Failed
我有一个长格式的数据框狗,我正在尝试使用 reshape() 函数将其重新格式化为宽格式。目前看起来是这样的: dogid month year trainingtype home scho
这个问题在这里已经有了答案: how to reshape an N length vector to a 3x(N/3) matrix in numpy using reshape (1 个回答)
我对 ndarray.reshape 的结构有疑问.我读过 numpy.reshape()和 ndarray.reshape是 python 中用于 reshape 数组的等效命令。 据我所知,num
所以这是我的麻烦:我想将一个长格式的数据文件改成宽格式。但是,我没有唯一的“j”变量;长格式文件中的每条记录都有几个关键变量。 例如,我想这样做: | caseid | gender | age |
Whis 这个数据框, df df id parameter visit value sex 1 01 blood V1 1 f 2 01 saliva V
我有一个列表,其中包含几个不同形状的 numpy 数组。我想将这个数组列表 reshape 为一个 numpy 向量,然后更改向量中的每个元素,然后将其 reshape 回原始数组列表。 例如: 输入
我有一个形状为 (1800,144) 的数组 (a) 其中 a[0:900,:] 都是实数,后半部分数组 a[900:1800,:] 全部为零。我想把数组的后半部分水平地放在前半部分旁边,然后将它们推
我有一个如下所示的数组: array([[0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2
我正在创建一个 tf.Variable(),然后使用该变量创建一个简单的函数,然后我使用 tf.reshape() 展平原始变量,然后我在函数和展平变量之间使用了 tf.gradients()。为什么
我有一个名为 data 的数据框,我试图从中识别任何异常价格。 数据框头部看起来像: Date Last Price 0 29/12/2017 487.74 1 28/
我有一个 float vec 数组,我想对其进行 reshape vec.shape >>> (3,) len(vec[0]) # all 3 rows of vec have 150 columns
tl;dr 我可以在不使用 numpy.reshape 的情况下将 numpy 数组的 View 从 5x5x5x3x3x3 reshape 为 125x1x1x3x3x3 吗? 我想对一个体积(大小
set.seed(123)data <- data.frame(ID = 1:10, weight_hus = rnorm(10, 0, 1),
我是一名优秀的程序员,十分优秀!