- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 python (v3.8.3) 中使用 tensorflow (v2.4) + keras 编写了一个简单的 CNN。我正在尝试优化网络,我想要更多关于它无法预测的信息。我正在尝试添加一个混淆矩阵,我需要提供 tensorflow.math.confusion_matrix() 测试标签。
我的问题是我无法弄清楚如何从 tf.keras.preprocessing.image_dataset_from_directory() 创建的数据集对象访问标签
我的图像组织在以标签为名称的目录中。文档说该函数返回一个 tf.data.Dataset 对象。
If label_mode is None, it yields float32 tensors of shape (batch_size, image_size[0], image_size[1], num_channels), encoding
images (see below for rules regarding num_channels).Otherwise, it yields a tuple (images, labels), where images has shape (batch_size, image_size[0], image_size[1], num_channels), andlabels follows the format described below.
import tensorflow as tf
from tensorflow.keras import layers
#import matplotlib.pyplot as plt
import numpy as np
import random
import PIL
import PIL.Image
import os
import pathlib
#load the IMAGES
dataDirectory = '/p/home/username/tensorflow/newBirds'
dataDirectory = pathlib.Path(dataDirectory)
imageCount = len(list(dataDirectory.glob('*/*.jpg')))
print('Image count: {0}\n'.format(imageCount))
#test display an image
# osprey = list(dataDirectory.glob('OSPREY/*'))
# ospreyImage = PIL.Image.open(str(osprey[random.randint(1,100)]))
# ospreyImage.show()
# nFlicker = list(dataDirectory.glob('NORTHERN FLICKER/*'))
# nFlickerImage = PIL.Image.open(str(nFlicker[random.randint(1,100)]))
# nFlickerImage.show()
#set parameters
batchSize = 32
height=224
width=224
(trainData, trainLabels) = tf.keras.preprocessing.image_dataset_from_directory(
dataDirectory,
labels='inferred',
label_mode='categorical',
validation_split=0.2,
subset='training',
seed=324893,
image_size=(height,width),
batch_size=batchSize)
testData = tf.keras.preprocessing.image_dataset_from_directory(
dataDirectory,
labels='inferred',
label_mode='categorical',
validation_split=0.2,
subset='validation',
seed=324893,
image_size=(height,width),
batch_size=batchSize)
#class names and sampling a few images
classes = trainData.class_names
testClasses = testData.class_names
#plt.figure(figsize=(10,10))
# for images, labels in trainData.take(1):
# for i in range(9):
# ax = plt.subplot(3, 3, i+1)
# plt.imshow(images[i].numpy().astype("uint8"))
# plt.title(classes[labels[i]])
# plt.axis("off")
# plt.show()
#buffer to hold the data in memory for faster performance
autotune = tf.data.experimental.AUTOTUNE
trainData = trainData.cache().shuffle(1000).prefetch(buffer_size=autotune)
testData = testData.cache().prefetch(buffer_size=autotune)
#augment the dataset with zoomed and rotated images
#use convolutional layers to maintain spatial information about the images
#use max pool layers to reduce
#flatten and then apply a dense layer to predict classes
model = tf.keras.Sequential([
#layers.experimental.preprocessing.RandomFlip('horizontal', input_shape=(height, width, 3)),
#layers.experimental.preprocessing.RandomRotation(0.1),
#layers.experimental.preprocessing.RandomZoom(0.1),
layers.experimental.preprocessing.Rescaling(1./255, input_shape=(height, width, 3)),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(128, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(256, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
# layers.Conv2D(512, 3, padding='same', activation='relu'),
# layers.MaxPooling2D(),
#layers.Conv2D(1024, 3, padding='same', activation='relu'),
#layers.MaxPooling2D(),
#dropout prevents overtraining by not allowing each node to see each datapoint
#layers.Dropout(0.5),
layers.Flatten(),
layers.Dense(512, activation='relu'),
layers.Dense(len(classes))
])
model.compile(optimizer='adam',
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.summary()
epochs=2
history = model.fit(
trainData,
validation_data=testData,
epochs=epochs
)
#create confusion matrix
predictions = model.predict_classes(testData)
confusionMatrix = tf.math.confusion_matrix(labels=testClasses, predictions=predictions).numpy()
我曾尝试使用 (foo, foo1) = tf.keras.preprocessing.image_dataset_from_directory(dataDirectory, etc),但我得到
train = tf.keras.preprocessing.image_dataset_from_directory(
dataDirectory,
labels='inferred',
label_mode='categorical',
validation_split=0.2,
subset='training',
seed=324893,
image_size=(height,width),
batch_size=batchSize)
trainData = train[0]
trainLabels = train[1]
我得到 TypeError: 'BatchDataset' object is not subscriptable
2020-11-03 14:15:14.643300: Wtensorflow/core/framework/op_kernel.cc:1740] OP_REQUIRES failed atcast_op.cc:121 : Unimplemented: Cast string to int64 is not supportedTraceback (most recent call last): File "birdFake.py", line 115, inconfusionMatrix = tf.math.confusion_matrix(labels=testClasses, predictions=predictions).numpy() File"/p/home/username/miniconda3/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py",line 201, in wrapperreturn target(*args, **kwargs) File "/p/home/username/miniconda3/lib/python3.8/site-packages/tensorflow/python/ops/confusion_matrix.py",line 159, in confusion_matrixlabels = math_ops.cast(labels, dtypes.int64) File "/p/home/username/miniconda3/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py",line 201, in wrapperreturn target(*args, **kwargs) File "/p/home/username/miniconda3/lib/python3.8/site-packages/tensorflow/python/ops/math_ops.py",line 966, in castx = gen_math_ops.cast(x, base_type, name=name) File "/p/home/username/miniconda3/lib/python3.8/site-packages/tensorflow/python/ops/gen_math_ops.py",line 1827, in cast_ops.raise_from_not_ok_status(e, name) File "/p/home/username/miniconda3/lib/python3.8/site-packages/tensorflow/python/framework/ops.py",line 6862, in raise_from_not_ok_statussix.raise_from(core._status_to_exception(e.code, message), None) File "", line 3, in raise_fromtensorflow.python.framework.errors_impl.UnimplementedError: Caststring to int64 is not supported [Op:Cast]
Traceback (most recent call last): File "./birdFake.py", line 118,in labels = np.concatenate([labels, np.argmax(y.numpy(), axis=-1)]) File "<array_function internals>", line 5, in concatenateValueError: all the input arrays must have same number of dimensions,but the array at index 0 has 1 dimension(s) and the array at index 1has 0 dimension(s)
最佳答案
如果我是你,我会遍历整个 testData,我会一路保存预测和标签,最后我会构建混淆矩阵。
testData = tf.keras.preprocessing.image_dataset_from_directory(
dataDirectory,
labels='inferred',
label_mode='categorical',
seed=324893,
image_size=(height,width),
batch_size=32)
predictions = np.array([])
labels = np.array([])
for x, y in testData:
predictions = np.concatenate([predictions, model.predict_classes(x)])
labels = np.concatenate([labels, np.argmax(y.numpy(), axis=-1)])
tf.math.confusion_matrix(labels=labels, predictions=predictions).numpy()
结果是
Found 4 files belonging to 2 classes.
array([[2, 0],
[2, 0]], dtype=int32)
关于python - 使用 tensorflow image_dataset_from_directory 时从数据集中获取标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64687375/
我想从我有大约 5000 张图像的目录中加载数据(类型“png”)。但是它返回一个错误,说当明显有图像时没有图像。 这段代码: width=int(wb-wa) height=int(hb-ha) d
我正在尝试学习图像自动编码,但我无法使用输入和输出图像来训练模型 前任: 输入图像文件夹:“.../图片/输入” 输出图像文件夹:“.../图片/输出” #get input images from
我正在尝试使用 Python 在 TensorFlow 中构建 CNN。我已将图像加载到数据集中,如下所示: dataset = tf.keras.preprocessing.image_datase
我正在尝试使用 Python 在 TensorFlow 中构建 CNN。我已将图像加载到数据集中,如下所示: dataset = tf.keras.preprocessing.image_datase
我在 python (v3.8.3) 中使用 tensorflow (v2.4) + keras 编写了一个简单的 CNN。我正在尝试优化网络,我想要更多关于它无法预测的信息。我正在尝试添加一个混淆矩
这是代码 来自 https://keras.io/examples/vision/image_classification_from_scratch/ import tensorflow as tf
Keras 介绍 tf.keras.preprocessing.image_dataset_from_directory功能最近,比以前更高效ImageDataGenerator.flow_from_
我在 ubuntu-20.04 操作系统中使用了 anaconda。 Keras version is: 2.4.3 Tensorflow version: 2.2.0 我导入了以下包, import
我有目录示例中对应文件数的标签列表:[1,2,3] train_ds = tf.keras.utils.image_dataset_from_directory( train_path, la
我有目录示例中对应文件数的标签列表:[1,2,3] train_ds = tf.keras.utils.image_dataset_from_directory( train_path, la
为什么我会遇到这个问题?我可以从 kera.preprocessing 导入图像模块。但无法导入 image_dataset_from_directory。 TF版本:1.14 最佳答案 根据this
我是一名优秀的程序员,十分优秀!