[英]Accessing Images and Labels Inside a tf.data.Dataset Object
I'm following along the keras tutorial on image classification.我正在关注关于图像分类的keras 教程。 I have created a tf.data.Dataset
and specified a single batch using the .take()
method:我创建了一个tf.data.Dataset
并使用.take()
方法指定了一个批次:
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
"data",
validation_split=0.2,
subset="training",
image_size=(224,224),
batch_size=32)
train_batch = train_ds.take(1)
Inspecting the train_batch
object, as expected, I see it is made up of two objects: images and labels:检查train_batch
object,不出所料,我看到它由两个对象组成:图像和标签:
<TakeDataset shapes: ((None, 224, 224, 3), (None,)), types: (tf.float32, tf.int32)>
The tutorial states uses the following code to plot the images in this batch:教程状态使用以下代码 plot 此批次中的图像:
for images, labels in train_batch:
for i in range(32):
ax = plt.subplot(4, 8, i + 1)
plt.imshow(images[i].numpy().astype("uint8"))
My question is how does for images, labels in train_batch:
manage to specify the images and labels separately.我的问题是for images, labels in train_batch:
设法分别指定图像和标签。 Apart from enumerate
I have not come across specifying two variables in a for loop.除了enumerate
之外,我还没有遇到在 for 循环中指定两个变量。 Is this the only way to access the images and labels in a batch?这是批量访问图像和标签的唯一方法吗?
train_batch returns a tuple (image,label). train_batch 返回一个元组(图像,标签)。 take for example the code below以下面的代码为例
x=(1,2,3)
a,b,c=x
print ('a= ', a,' b= ',b,' c= ', c)
# the result will be a= 1 b= 2 c= 3
same process happens in the for loop images receives the image part of the tuple and labels receives the label part of the tuple.同样的过程发生在 for 循环图像接收元组的图像部分和标签接收元组的 label 部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.