考虑这个问题:从图像数据集(如 ImageNet)中的随机主题中选择随机数量的样本作为 Tensorflow 图的输入元素,该图用作对象集识别器。对于每个批次,每个类都有相同数量的样本以方便计算。但是对于一个类,不同的批次会有不同数量的图像,即 batch_0: num_imgs_per_cls=2; 批次_1000:num_imgs_per_cls=3。
如果 Tensorflow 中存在现有功能,我们将非常感谢从头开始(例如从图像目录)对整个过程的解释。
我想在 gcloud 存储上大约 2TB 的图像数据上训练一个模型。我将图像数据保存为单独的 tfrecords 并尝试在此示例之后使用 tensorflow 数据 api
https://medium.com/@moritzkrger/speeding-up-keras-with-tfrecord-datasets-5464f9836c36
但似乎 kerasmodel.fit(...)不支持基于 tfrecord 数据集的验证
https://github.com/keras-team/keras/pull/8388
是否有更好的方法来处理来自我缺少的 ml-engine 的 keras 的大量数据?
非常感谢!
keras tensorflow google-cloud-ml tfrecord tensorflow-datasets
我打算用预先训练模式一样 faster_rcnn_resnet101_pets在Tensorflow环境物体检测所描述的在这里
我已经收集了几张图像用于训练和测试集。所有这些图像大小各异。是否需要将它们调整为通用尺寸?
faster_rcnn_resnet101_pets使用输入大小为224x224x3的resnet。

这是否意味着我必须在发送训练之前重新调整所有图像的大小?或TF自动照顾它。
python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/faster_rcnn_resnet101_pets.config
Run Code Online (Sandbox Code Playgroud)
通常,拥有相同尺寸的图像是一种好习惯吗?
**实施@jdehesa 后更新答案:我的代码如下所示:
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os
import PIL as pil
from tensorflow import feature_column
from tensorflow_core.python.platform.flags import FLAGS
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def print_type(name , x):
print(" {} type = {}".format(name, type(x)))
def _bytes_feature(value):
"""Returns a bytes_list from a string / byte."""
if isinstance(value, type(tf.constant(0))):
value = value.numpy() # BytesList won't unpack a string from an EagerTensor.
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _float_feature(value):
if not isinstance(value, …Run Code Online (Sandbox Code Playgroud) python machine-learning tensorflow tfrecord tensorflow-datasets
我正在关注这个教程。本教程首先加载一个监督数据集(使用tfds.loadwith as_supervised=True):
(train_data, test_data), info = tfds.load(
'imdb_reviews/subwords8k',
split = (tfds.Split.TRAIN, tfds.Split.TEST),
with_info=True, as_supervised=True)
Run Code Online (Sandbox Code Playgroud)
然后教程建议像这样洗牌和填充数据集:
电影评论的长度可以不同。我们将使用该
padded_batch方法来标准化评论的长度。
train_batches = train_data.shuffle(1000).padded_batch(10)
test_batches = test_data.shuffle(1000).padded_batch(10)
Run Code Online (Sandbox Code Playgroud)
...但不幸的是,该padded_batch方法需要一个额外的参数,教程似乎忘记了:
Traceback (most recent call last):
File "imdb_reviews.py", line 14, in <module>
train_batches = train_data.shuffle(1000).padded_batch(10)
TypeError: padded_batch() missing 1 required positional argument: 'padded_shapes'
Run Code Online (Sandbox Code Playgroud)
尽管错误堆栈表明这padded_shapes是缺少的参数,但从教程中我认为可以公平地推断出缺少的参数实际上是batch_size(应该在 之前padded_shapes)。
我认为这可能很容易解决,如下所示:
Traceback (most recent call last):
File "imdb_reviews.py", line 14, in <module>
train_batches = train_data.shuffle(1000).padded_batch(10) …Run Code Online (Sandbox Code Playgroud) 我制作了一个 TensorFlow CsvDataset,我正在尝试对数据进行标记:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from tensorflow import keras
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
import os
os.chdir('/home/nicolas/Documents/Datasets')
fname = 'rotten_tomatoes_reviews.csv'
def preprocess(target, inputs):
tok = Tokenizer(num_words=5_000, lower=True)
tok.fit_on_texts(inputs)
vectors = tok.texts_to_sequences(inputs)
return vectors, target
dataset = tf.data.experimental.CsvDataset(filenames=fname,
record_defaults=[tf.int32, tf.string],
header=True).map(preprocess)
Run Code Online (Sandbox Code Playgroud)
运行这个,给出以下错误:
ValueError: len 需要一个非标量张量,得到一个形状 Tensor("Shape:0", shape=(0,), dtype=int32)
我尝试过的:几乎任何可能性领域。请注意,如果我删除预处理步骤,一切都会运行。
数据是什么样的:
(<tf.Tensor: shape=(), dtype=int32, numpy=1>,
<tf.Tensor: shape=(), dtype=string, numpy=b" Some movie critic review...">)
Run Code Online (Sandbox Code Playgroud) 作为一些背景,我最近越来越关注 NLP 和文本处理。我对计算机视觉更加熟悉。我完全理解标记化的想法。
我的困惑源于Tokenizer可以在Tensorflow生态系统中找到的类的各种实现。
分别Tokenizer在Tensorflow Datasets( tfds) 和Tensorflow适当的: tfds.features.text.Tokenizer()& 中找到了一个类tf.keras.preprocessing.text.Tokenizer()。
我查看了源代码(链接如下),但无法收集任何有用的见解
这里的 tl;dr 问题是:您使用哪个库来做什么?一个图书馆比另一个图书馆有什么好处?
笔记
我一直在关注 Tensorflow In Practice Specialization以及本教程。TF in Practice Specialization 使用tf.Keras.preprocessing.text.Tokenizer()实现和文本加载教程使用tfds.features.text.Tokenizer()
我正在使用一个使用 MNIST 数据集的示例程序。
它尝试使用以下行加载数据集:
dataset = tfds.load(name='mnist', split=split)
Run Code Online (Sandbox Code Playgroud)
但是,这会产生以下错误:
2020-07-30 12:08:17.926262: W tensorflow/core/platform/cloud/google_auth_provider.cc:184] All attempts to get a Google authentication bearer token failed, returning an empty token. Retrieving token from files failed with "Not found: Could not locate the credentials file.". Retrieving token from GCE failed with "Failed precondition: Error executing an HTTP request: libcurl code 6 meaning 'Couldn't resolve host name', error details: Couldn't resolve host 'metadata'".
Traceback (most recent call last):
File "/home/tflynn/pylocal/lib/python3.7/site-
packages/tensorflow_datasets/core/utils/py_utils.py", line 399, in try_reraise …Run Code Online (Sandbox Code Playgroud) 我正在使用tf.keras.preprocessing.image_dataset_from_directoryTF 2.3 从目录(训练/测试拆分)加载图像。我得到的是一个 tf.data.Dataset (tensorflow.python.data.ops.dataset_ops.BatchDataset实际上)具有形状的对象:
train_ds.take(1)
# <TakeDataset shapes: ((None, 256, 256, 3), (None, 6)), types: (tf.float32, tf.float32)>
Run Code Online (Sandbox Code Playgroud)
for images, labels in train_ds.take(1):
print(images.shape)
print(images[0])
# (32, 256, 256, 3)
# tf.Tensor(
# [[[225.75 225.75 225.75 ]
# [225.75 225.75 225.75 ]
# [225.75 225.75 225.75 ]
# ...
# [215. 214. 209. ]
# [215. 214. 209. ]
# [215. 214. 209. ]]
#
# ...], shape=(256, 256, 3), dtype=float32)
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何/= 255使用该 Dataset …
我正在使用张量流 2.3.0
我有一个 python 数据生成器-
import tensorflow as tf
import numpy as np
vocab = [1,2,3,4,5]
def create_generator():
'generates a random number from 0 to len(vocab)-1'
count = 0
while count < 4:
x = np.random.randint(0, len(vocab))
yield x
count +=1
Run Code Online (Sandbox Code Playgroud)
我把它变成了一个 tf.data.Dataset 对象
gen = tf.data.Dataset.from_generator(create_generator,
args=[],
output_types=tf.int32,
output_shapes = (), )
Run Code Online (Sandbox Code Playgroud)
现在我想使用map方法对项目进行子采样,这样 tf 生成器永远不会输出任何偶数。
def subsample(x):
'remove item if it is present in an even number [2,4]'
'''
#TODO
'''
return x
gen = gen.map(subsample)
Run Code Online (Sandbox Code Playgroud)
如何使用 …
python-3.x tensorflow tensorflow-datasets tensorflow2.0 tf.data.dataset