我正在使用TensorFlow, Numpy和MatPlotLib。我对这一切都是一个真正的初学者,所以我遵循文档并使用Fashion MNIST数据集作为文档。
这是我的代码:# See tensorflow version and import other helper modules
import tensorflow as tf
from tensorflow.keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
print(tensorflow.__version__)
# Load the MNIST Fashion dataset
fashion_mnist = tf.keras.datasets.fashion_mnist
print(fashion_mnist)
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
# Show first image in training set (with MatPlotLib)
plt.figure()
plt.imshow(train_images[0]) # Shows first image in training set
plt.colorbar() # Shows colorbar at the right (a sort of legend on the scale of 0 to 255, which is the scale of colors on the image)
plt.show()
# Scale down values from '0 to 255' to '0 to 1'
train_images, test_images = train_images / 255.0, test_images / 255.0
test_img_path = '/content/test.jpg' # I'm getting my custom image here
test_img = image.load_img(test_img_path, target_size=(28, 28))
test_img_array = image.img_to_array(test_img)
plt.imshow(test_img)
plt.show()
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
plt.show()
# Create the neural network with Sequential()
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)), # Flattens 2D array to 1D array, nothing else
tf.keras.layers.Dense(1024, activation="relu"), # 128 neurons with the reLU activation function
tf.keras.layers.Dense(10)
])
# Compile the model with model.compile()
model.compile(
optimizer="adam", # Adam optimizer
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), # Use SparseCategoricalCrossentropy loss function
metrics = ['accuracy']
)
# Fit the model to the training data using model.fit()
model.fit(train_images, train_labels, epochs=10)
# Evaluate model with model.evaluate()
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
# Create probality model
probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
# Get the predictions out of the probability model
predictions = probability_model.predict(test_images)
# Use Numpy's argmax() function to see which clothing does the model think the first test image represents
np.argmax(predictions[0])
def plot_image(i, predictions_array, true_label, img):
true_label, img = true_label[i], img[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])
plt.imshow(img, cmap=plt.cm.binary)
predicted_label = np.argmax(predictions_array)
if predicted_label == true_label:
color = 'blue'
else:
color = 'red'
plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
100*np.max(predictions_array),
class_names[true_label]),
color=color)
def plot_value_array(i, predictions_array, true_label):
true_label = true_label[i]
plt.grid(False)
plt.xticks(range(10))
plt.yticks([])
thisplot = plt.bar(range(10), predictions_array, color="#777777")
plt.ylim([0, 1])
predicted_label = np.argmax(predictions_array)
thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color('blue')
i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i], test_labels)
plt.show()
img = test_img_array
print(img.shape)
# I get (28, 28, 3)
# Add the image to a batch where it's the only member.
img = (np.expand_dims(img,0))
print(img.shape)
predictions_single = probability_model.predict(img)
plt.imshow(test_img)
plt.show()
plot_value_array(1, predictions_single[0], test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)
plt.show()
(不要介意评论,我只是想让这个Colab笔记本成为一种小抄,如果我忘记了以后该怎么做)
当我打印test_img_array
的形状时,我得到了(28, 28, 3)
。但是当我在测试集中打印图像的形状时,我得到(28, 28)
。我怎么去掉它?我看到很多其他的问题,但没有一个能真正解决我的问题(或者我太笨了,不能让它工作)。
当我试图预测我的自定义图像是什么(我不想使用这个应用程序的测试集)使用predictions_single = probability_model.predict(img)
,我得到这个奇怪的错误:
ValueError: Input 0 of layer dense_6 is incompatible with the layer: expected axis -1 of input shape to have value 784 but received input with shape (None, 2352)
使用输出中出现的其他内容进行完整的回溯/堆栈跟踪:
WARNING:tensorflow:Model was constructed with shape (None, 28, 28) for input KerasTensor(type_spec=TensorSpec(shape=(None, 28, 28), dtype=tf.float32, name='sequential_5_input'), name='sequential_5_input', description="created by layer 'sequential_5_input'"), but it was called on an input with incompatible shape (None, 28, 84).
WARNING:tensorflow:Model was constructed with shape (None, 28, 28) for input KerasTensor(type_spec=TensorSpec(shape=(None, 28, 28), dtype=tf.float32, name='flatten_3_input'), name='flatten_3_input', description="created by layer 'flatten_3_input'"), but it was called on an input with incompatible shape (None, 28, 84).
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-181-b6162819a38d> in <module>()
----> 1 predictions_single = probability_model.predict(img)
2
3 plt.imshow(test_img)
4 plt.show()
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
992 except Exception as e: # pylint:disable=broad-except
993 if hasattr(e, "ag_error_metadata"):
--> 994 raise e.ag_error_metadata.to_exception(e)
995 else:
996 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1586 predict_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1576 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1286 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2849 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3632 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1569 run_step **
outputs = model.predict_step(data)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1537 predict_step
return self(x, training=False)
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:1037 __call__
outputs = call_fn(inputs, *args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/sequential.py:369 call
return super(Sequential, self).call(inputs, training=training, mask=mask)
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py:415 call
inputs, training=training, mask=mask)
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py:550 _run_internal_graph
outputs = node.layer(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:1037 __call__
outputs = call_fn(inputs, *args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/sequential.py:369 call
return super(Sequential, self).call(inputs, training=training, mask=mask)
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py:415 call
inputs, training=training, mask=mask)
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py:550 _run_internal_graph
outputs = node.layer(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:1020 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py:254 assert_input_compatibility
' but received input with shape ' + display_shape(x.shape))
ValueError: Input 0 of layer dense_6 is incompatible with the layer: expected axis -1 of input shape to have value 784 but received input with shape (None, 2352)
我认为这是图像形状的问题,所以我在StackOverflow上尝试了不同的答案,但他们都没有给我一个28x28
图像。一个我试着给我28x84
,但28x28
没有来,因为在图像的形状额外的3
。
Fashion MNIST是一个灰度图像数据集。而您的自定义图像具有形状(28,28,3)
,这意味着它是RGB图像。此外,您还需要规范化您的图像。
test_img_array = test_img_array / 255.0 # normalize
test_img_array = tf.image.rgb_to_grayscale(test_img_array) # will return shape (28, 28, 1)
test_img_array = tf.squeeze(test_img_array, axis = -1) # shape is (28, 28)
最后,您需要添加批量尺寸。
test_img_array = tf.expand_dims(test_img_array, axis = 0) # shape: (1, 28, 28)
预测:
predictions_single = probability_model.predict(test_img_array)
或者,load_img
接受一个名为color_mode
的参数,其默认值为rgb
。因此,您可以首先传递grayscale
,而不是使用tf.image.rgb_to_grayscale
。