Python Cifar100只有16个训练图像和16个训练标签
我将Tensorflow与Python3.7结合使用,并尝试使用CIFAR-100制作一个图像分类器。我希望尽可能远离KER,因为它只有有限的数据集可供我使用。这是我的代码:Python Cifar100只有16个训练图像和16个训练标签,python,tensorflow,machine-learning,python-3.7,Python,Tensorflow,Machine Learning,Python 3.7,我将Tensorflow与Python3.7结合使用,并尝试使用CIFAR-100制作一个图像分类器。我希望尽可能远离KER,因为它只有有限的数据集可供我使用。这是我的代码: import tensorflow as tf import tensorflow_datasets as tfds import numpy as np import matplotlib.pyplot as plt import PIL.Image as Image from tensorflow import ker
import tensorflow as tf
import tensorflow_datasets as tfds
import numpy as np
import matplotlib.pyplot as plt
import PIL.Image as Image
from tensorflow import keras
tf.compat.v1.enable_eager_execution()
shape = (224, 224)
labels = '/home/pi/tf/cifar_labels.txt'
labels = np.array(open(labels).read().splitlines())
img = '/home/pi/tf/lobster.jpeg'
img = Image.open(img).resize(shape)
img = np.array(img)/255.0
img = np.reshape(img, (224, 224, 3))
train = tfds.load(name="cifar100", split="train")
test = tfds.load(name="cifar100", split="test")
train = train.shuffle(1024).batch(32).prefetch(tf.data.experimental.AUTOTUNE)
test = test.shuffle(1024).batch(32).prefetch(tf.data.experimental.AUTOTUNE)
for features in train:
train_images, train_labels = features["image"], features["label"]
for features in test:
test_images, test_labels = features["image"], features["label"]
model = keras.Sequential([
keras.layers.Flatten(input_shape=(32, 32, 3)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(100, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=200, verbose=2)
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print('\nTest accuracy:', test_acc)
我猜列车中的
for特性
for循环有问题。当我打印训练图像/标签的len
时,我得到16。因此,我的模型的训练准确率为0%,损失率为16.1181%。有人能帮忙吗?要在keras模型中直接使用CIFAR-100,您应该使用as\u supervised=True
参数调用tfds.load函数。然后,它将仅使用“图像”和“标签”键加载数据集:
因此,它不能直接输入到model.fit()
中。将设置为True时,返回的数据集将只包含(u'image',u'label')
键
总之,
将tensorflow_数据集导入为TFD
从tensorflow进口keras
tf.compat.v1.enable_eager_execution()
列车=tfds.load(name=“cifar100”,split=“列车”,as\u supervised=True)
test=tfds.load(name=“cifar100”,split=“test”,as\u supervised=True)
train=train.shuffle(1024).批处理(32).预取(tf.data.experimental.AUTOTUNE)
test=test.shuffle(1024)、批处理(32)、预取(tf.data.experimental.AUTOTUNE)
模型=keras.连续([
keras.layers.Flatten(输入_形状=(32,32,3)),
keras.layers.致密(128,活化='relu'),
keras.层.致密(100,活化='softmax')
])
model.compile(优化器='adam',
损失=“稀疏”\u分类”\u交叉熵',
指标=[‘准确度’])
历史=model.fit(训练,历次=200,详细=1)
测试损失,测试acc=模型评估(测试,详细=1)
打印(“\n测试精度:”,测试依据)
注:
要使用数据集而不将as_supervised设置为True,可以使用model.train_on_batch函数。e、 g
将tensorflow_数据集导入为TFD
从tensorflow进口keras
tf.compat.v1.enable_eager_execution()
列车=tfds.load(name=“cifar100”,split=“列车”)
test=tfds.load(name=“cifar100”,split=“test”)
train=train.shuffle(1024)、repeat(200)、batch(32)、prefetch(tf.data.experimental.AUTOTUNE)
test=test.shuffle(1024)、批处理(32)、预取(tf.data.experimental.AUTOTUNE)
模型=keras.连续([
keras.layers.Flatten(输入_形状=(32,32,3)),
keras.layers.致密(128,活化='relu'),
keras.层.致密(100,活化='softmax')
])
model.compile(优化器='adam',
损失=“稀疏”\u分类”\u交叉熵',
指标=[‘准确度’])
对于范围内的历元(200):
对于列车中的功能:
图像批次,标签批次=特征[“图像”],特征[“标签”]
损失,acc=型号。批量培训(图像批、标签批)
对于测试中的功能:
图像批次,标签批次=特征[“图像”],特征[“标签”]
损失,acc=模型。批量测试(图像批、标签批)
FeaturesDict({
'coarse_label': ClassLabel(shape=(), dtype=tf.int64, num_classes=20),
'image': Image(shape=(32, 32, 3), dtype=tf.uint8),
'label': ClassLabel(shape=(), dtype=tf.int64, num_classes=100),
})