我是机器学习的真正入门者。但是我试图用Flask服务器部署MNIST字符识别示例。我已经建立了模型,对其进行了训练并建立了Flask。
我创建了一个简单的HTML画布,可以在其中绘制0-9之间的数字。这些通过AJAX发送到我的python后端。
在Python后端中,我收到了Base64映像并将其解码为255灰度数组。因此,我有一个包含有关图像信息的巨大数组:
[138, 102, 160, 120, 54, 173, 105, 214, 173, 106, 41, 154, 129, 239, 233, 158, 6, 218, 177, 238, 184, 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 1, 144, 0, 0, 1, 144, 8, 6, 0, 0, 0, 128, 191, 54, 204, 0, 0, 28, 81, 73, 68, 65, 84, 120, 94, 237, 221, 9, 172, 109, 87, 89, 7, 240, 63, 145, 72, 152, 154, 128, 128, 168, 16, 32, 21, 80, 169, 80, 202, 148, 20, 16, 43, 161, 45, 80, 160, 140, 50, 180, 34, 29, 24, 210, 42, 2, 130, 180, 1, 25, 211, 34, 163, 96, 27, 134, 14, 32, 101, 146, 25, 139, 165, 45, 40, 164, 12, 134, 22, 104, 9, 160, 76, 13, 40, 168, 164, 32, 36, 101, 10, 4, 131, 89, 118, 95, 122, 237, 240, 222, 121, 251, 158, 179, 246, 218, 107, 255, 78, 242, 114, 31, 183, 103, 237, 181, 190, 223, 183, 30, 255, 220, 187, 207, 222, 251, 26, 241, 34, 64, 128, 0, 1, 2, 35, 4, 174, 49, 98, 140, 33, 4, 8, 16, 32, 64, 32, 2, 196, 38, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30]
但是我找不到重塑此数组的方法,因此我可以将其输入到我的tensorflow模型中,这需要以下输入:
Tensor("shape=(28, 28, 1), dtype=float32)
您能帮我找到一种方法,将Python中的此数组转换为适当大小的张量吗?
已经谢谢
答案 0 :(得分:0)
这是一些应该起作用的代码。
import numpy as np
import tensorflow as tf
data = [138, 102, 160, 120, 54, 173, 105, 214, 173, 106, 41, 154, 129, 239, 233, 158, 6, 218, 177, 238, 184, 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 1, 144, 0, 0, 1, 144, 8, 6, 0, 0, 0, 128, 191, 54, 204, 0, 0, 28, 81, 73, 68, 65, 84, 120, 94, 237, 221, 9, 172, 109, 87, 89, 7, 240, 63, 145, 72, 152, 154, 128, 128, 168, 16, 32, 21, 80, 169, 80, 202, 148, 20, 16, 43, 161, 45, 80, 160, 140, 50, 180, 34, 29, 24, 210, 42, 2, 130, 180, 1, 25, 211, 34, 163, 96, 27, 134, 14, 32, 101, 146, 25, 139, 165, 45, 40, 164, 12, 134, 22, 104, 9, 160, 76, 13, 40, 168, 164, 32, 36, 101, 10, 4, 131, 89, 118, 95, 122, 237, 240, 222, 121, 251, 158, 179, 246, 218, 107, 255, 78, 242, 114, 31, 183, 103, 237, 181, 190, 223, 183, 30, 255, 220, 187, 207, 222, 251, 26, 241, 34, 64, 128, 0, 1, 2, 35, 4, 174, 49, 98, 140, 33, 4, 8, 16, 32, 64, 32, 2, 196, 38, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30, 32, 64, 128, 0, 129, 81, 2, 2, 100, 20, 155, 65, 4, 8, 16, 32, 32, 64, 236, 1, 2, 4, 8, 16, 24, 37, 32, 64, 70, 177, 25, 68, 128, 0, 1, 2, 2, 196, 30]
data = np.array(data).reshape(28,28,1)
这时您应该可以在此使用tensorflow模型,但是如果由于某种原因它不接受numpy数组,则可以尝试
data = tf.convert_to_tensor(data)
这会将numpy数组转换为张量流张量。希望有帮助!