TensorFlow:将图像导入张量流模型
TensorFlow: Import image into tensor-flow model
我是深度学习的新手。为了练习,我用张量流和 mnist 训练了一个简单的手写模型。加载 mnist 后,我制作了模型并对其进行了训练:
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = x_train.astype('float32')/255
x_test = x_test.astype('float32')/255
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28,28)),
keras.layers.Dense(100,activation = 'relu'),
keras.layers.Dense(100,activation = 'sigmoid'),
keras.layers.Dense(10,activation = 'sigmoid'),
])
如您所见,我将第一层展平为 784 像素的一维数组。
现在我在纸上写了一个数字:
在用图像编辑器 (GIMP) 将其比例更改为 28*28 后,我将我的图像加载到我的代码中:
img_width, img_height = 28, 28
img = image.load_img('rgb_seven.jpeg', target_size=(img_width, img_height))
x = image.img_to_array(img)
这是 x 结果:
array([[[167., 170., 179.],
[168., 171., 180.],
[168., 171., 180.],
...,
[174., 175., 180.],
[174., 175., 180.],
[167., 170., 175.]],
[[173., 176., 185.],
[172., 175., 184.],
[166., 169., 178.],
和:
images = np.vstack(x)
images
它的图像结果是:
array([[167., 170., 179.],
[168., 171., 180.],
[168., 171., 180.],
...,
[166., 173., 179.],
[166., 173., 179.],
[166., 173., 179.]], dtype=float32)
在进行预测之前,我必须对图像进行扁平化处理,所以我这样做了:
x_images_flattened = images.reshape(len(images),28*28)
但是我得到了错误:
ValueError Traceback (most recent call last)
<ipython-input-30-db6c299f19c6> in <module>
3 images = np.vstack(x)
4 # images
----> 5 x_images_flattened = images.reshape(len(images),28*28)
ValueError: cannot reshape array of size 2352 into shape (784,784)
为什么我得到 cannot reshape array of size 2352 into shape (784,784)
我的图像尺寸为 28*28。
我怎么预测呢?
我终于解决了我的问题。
img_width, img_height = 28, 28
img = image.load_img('two.jpeg', color_mode='grayscale',target_size=(img_width, img_height))
img_tensor = image.img_to_array(img)
plt.imshow(img_tensor,cmap=plt.cm.binary)
img_tensor = keras.utils.normalize(img_tensor,axis=1)
prediction = model.predict(tf.expand_dims(img_tensor, axis=0))
问题解决了。
我是深度学习的新手。为了练习,我用张量流和 mnist 训练了一个简单的手写模型。加载 mnist 后,我制作了模型并对其进行了训练:
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = x_train.astype('float32')/255
x_test = x_test.astype('float32')/255
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28,28)),
keras.layers.Dense(100,activation = 'relu'),
keras.layers.Dense(100,activation = 'sigmoid'),
keras.layers.Dense(10,activation = 'sigmoid'),
])
如您所见,我将第一层展平为 784 像素的一维数组。
现在我在纸上写了一个数字:
在用图像编辑器 (GIMP) 将其比例更改为 28*28 后,我将我的图像加载到我的代码中:
img_width, img_height = 28, 28
img = image.load_img('rgb_seven.jpeg', target_size=(img_width, img_height))
x = image.img_to_array(img)
这是 x 结果:
array([[[167., 170., 179.],
[168., 171., 180.],
[168., 171., 180.],
...,
[174., 175., 180.],
[174., 175., 180.],
[167., 170., 175.]],
[[173., 176., 185.],
[172., 175., 184.],
[166., 169., 178.],
和:
images = np.vstack(x)
images
它的图像结果是:
array([[167., 170., 179.],
[168., 171., 180.],
[168., 171., 180.],
...,
[166., 173., 179.],
[166., 173., 179.],
[166., 173., 179.]], dtype=float32)
在进行预测之前,我必须对图像进行扁平化处理,所以我这样做了:
x_images_flattened = images.reshape(len(images),28*28)
但是我得到了错误:
ValueError Traceback (most recent call last)
<ipython-input-30-db6c299f19c6> in <module>
3 images = np.vstack(x)
4 # images
----> 5 x_images_flattened = images.reshape(len(images),28*28)
ValueError: cannot reshape array of size 2352 into shape (784,784)
为什么我得到 cannot reshape array of size 2352 into shape (784,784)
我的图像尺寸为 28*28。
我怎么预测呢?
我终于解决了我的问题。
img_width, img_height = 28, 28
img = image.load_img('two.jpeg', color_mode='grayscale',target_size=(img_width, img_height))
img_tensor = image.img_to_array(img)
plt.imshow(img_tensor,cmap=plt.cm.binary)
img_tensor = keras.utils.normalize(img_tensor,axis=1)
prediction = model.predict(tf.expand_dims(img_tensor, axis=0))
问题解决了。