为什么TensorFlow总是给我相同的结果?

时间:2018-11-27 00:38:02

标签: tensorflow machine-learning

我是机器学习的初学者,并且想使用TensorFlow了解它。我在这里遵循了教程:Basic Classification

我正在尝试重写代码,并对其进行少量修改以训练我的自定义图像。我一直在下载许多图像,但是每次使用代码测试图像时,该图像始终被检测为“袋子”。无论我使用什么图像,可能是裤子,T恤或套头衫,该图像始终被检测为“袋”。那么我的代码有什么问题呢?下面是我的代码。

import tensorflow as tf
import numpy as np
import os
import skimage

mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images/255.0
test_images = test_images/255.0

class_names = ["T-Shirt", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle Boot"]

models = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation=tf.nn.relu),
    tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
models.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

models.fit(train_images, train_labels, epochs=5)
models.evaluate(test_images, test_labels)

# list files in "data" folder
files = [f for f in os.listdir("data") if os.path.isfile(os.path.join("data", f))]

images = []

f = files[0]
# read image
image = skimage.data.imread(os.path.join("data", f))
# convert image to gray
image = skimage.color.rgb2gray(image)
# resize image to 28x28
image = skimage.transform.resize(image, (28, 28))
# set test_images at index 5 as 'image'
test_images[5] = image

predictions = models.predict(test_images)
index = np.argmax(predictions[5])
print("Result index: "+str(index))
print("Detected image: "+class_names[index])

0 个答案:

没有答案
相关问题