{"id":10021094,"date":"2025-11-27T19:11:56","date_gmt":"2025-11-27T11:11:56","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021094"},"modified":"2025-11-27T19:11:56","modified_gmt":"2025-11-27T11:11:56","slug":"%e5%88%9d%e8%af%86%e5%9b%be%e5%83%8f%e8%af%86%e5%88%ab%e6%89%8b%e5%86%99%e6%95%b0%e5%ad%970-9%e5%9f%ba%e7%a1%80","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021094","title":{"rendered":"\u521d\u8bc6\u56fe\u50cf\u8bc6\u522b\u624b\u5199\u6570\u5b570-9\u57fa\u7840"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>\u521d\u6b65\u63a2\u7d22\u4e86\u8ba1\u7b97\u673a\u8bc6\u522b\u624b\u5199\u6570\u5b57\u7684\u9886\u57df\uff0c\u5c06\u4e86\u89e3\u5230\u7684\u57fa\u672c\u77e5\u8bc6\u603b\u7ed3\u5982\u4e0b\u3002<\/p>\n<h2>\u8ba1\u7b97\u673a\u5982\u4f55&#8221;\u770b&#8221;\uff1f<\/h2>\n<p>\u53ef\u5148\u770b\u6b64\u7bc7\u6587\u7ae0\u8fdb\u884c\u5177\u4f53\u4e86\u89e3:<a href=\"https:\/\/segmentfault.com\/a\/1190000047432165\" target=\"_blank\" rel=\"noopener noreferrer\">\u6d45\u8c08\u8ba1\u7b97\u673a\u5982\u4f55\u8bc6\u522b\u548c\u5b58\u50a8\u56fe\u50cf<\/a><br \/>\u603b\u7ed3\u4e00\u53e5\u8bdd\uff1a\u8ba1\u7b97\u673a\u201c\u770b\u201d\u6570\u5b57\uff0c\u5c31\u50cf\u4eba\u773c\u770b\u6570\u5b57\u4e00\u6837\uff0c\u4f46\u5b83\u7528\u7684\u662f\u50cf\u7d20\u548c\u6570\u5b66\u6a21\u578b\uff0c\u800c\u4e0d\u662f\u773c\u775b\u548c\u5927\u8111\u3002<\/p>\n<h2>\u8bad\u7ec3\u6a21\u578b<\/h2>\n<ul>\n<li>\u9009\u7528\u6570\u636e\u96c6: MNIST\u6570\u636e\u96c6\uff0c\u5305\u62ec60,000 \u5f20\u8bad\u7ec3\u56fe\uff0c10,000 \u5f20\u6d4b\u8bd5\u56fe\uff0c\u8bad\u7ec3\u901f\u5ea6\u5feb<\/li>\n<li>\n<p>\u9009\u7528\u8bed\u8a00\uff1apython(\u8fd0\u884c\u5728pycharm)<\/p>\n<h3>1.\u52a0\u8f7d\u6570\u636e\u96c6\uff1a<\/h3>\n<pre><code>(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()\n\nprint(f&quot; Training images: {len(x_train)}&quot;)\nprint(f&quot; Test images: {len(x_test)}&quot;)\nprint(f&quot; Image size: {x_train[0].shape} (28x28 pixels)&quot;)<\/code><\/pre>\n<p>\u628a\u673a\u5668\u5b66\u4e60\u60f3\u8c61\u6210\u6559\u5c0f\u5b69\u8ba4\u6570\u5b57\uff1a<\/p>\n<\/li>\n<li>x_train = \u7ed9\u5c0f\u5b69\u770b\u7684\u56fe\u7247\uff08\u95ee\u9898\/\u7279\u5f81\uff09<\/li>\n<li>y_train = \u544a\u8bc9\u5c0f\u5b69\u7684\u6b63\u786e\u7b54\u6848\uff08\u7b54\u6848\/\u6807\u7b7e\uff09<\/li>\n<li>x_test = \u7ed9\u5c0f\u5b69\u7684\u8003\u8bd5\u9898\u76ee\uff08\u6d4b\u8bd5\u95ee\u9898\uff09<\/li>\n<li>\n<p>y_test = \u8003\u8bd5\u7684\u6807\u51c6\u7b54\u6848\uff08\u6d4b\u8bd5\u7b54\u6848\uff09<\/p>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code>   Training images: 60000\n   Test images: 10000\n   Image size: (28, 28) (28x28 pixels)\n<\/code><\/pre>\n<h3>2.\u53ef\u89c6\u5316\u5c55\u793aMNIST\u6570\u636e\u96c6\u4e2d\u7684\u624b\u5199\u6570\u5b57\u6837\u672c\uff1a<\/h3>\n<pre><code>plt.figure(figsize=(12, 6))   # \u521b\u5efa\u753b\u5e03\uff0c\u8bbe\u7f6e\u5927\u5c0f12x6\u82f1\u5bf8\n\nprint(&quot; Showing first 12 digits from training set...&quot;)\nfor i in range(12):\n    plt.subplot(3, 4, i + 1)    # \u521b\u5efa3\u884c4\u5217\u7684\u5b50\u56fe\uff0c\u5f53\u524d\u662f\u7b2ci+1\u4e2a\n    plt.imshow(x_train[i], cmap='gray')    # \u663e\u793a\u7b2ci\u5f20\u8bad\u7ec3\u56fe\u7247\uff0c\u7528\u7070\u5ea6\u8272\u5f69\n    plt.title(f'Digit: {y_train[i]}', fontsize=12)   # \u8bbe\u7f6e\u6807\u9898\u4e3a\u771f\u5b9e\u6807\u7b7e\n    plt.axis('off')    # \u5173\u95ed\u5750\u6807\u8f74\uff0c\u8ba9\u56fe\u7247\u66f4\u5e72\u51c0\n\nplt.suptitle('MNIST Handwritten Digits Samples', fontsize=16, fontweight='bold')\nplt.tight_layout()    # \u81ea\u52a8\u8c03\u6574\u5b50\u56fe\u95f4\u8ddd\uff0c\u907f\u514d\u91cd\u53e0<\/code><\/pre>\n<p>\u663e\u793a\u60c5\u51b5\uff1a<br \/>  <img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"449\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3pR\" alt=\"image.png\" title=\"image.png\"><\/p>\n<\/li>\n<\/ul>\n<h3>3.\u6570\u636e\u9884\u5904\u7406\u4e2d\u7684\u5f52\u4e00\u5316\u64cd\u4f5c:<\/h3>\n<pre><code>  x_train = x_train \/ 255.0 \n  x_test = x_test \/ 255.0<\/code><\/pre>\n<p>\u6570\u636e\u5f52\u4e00\u5316\u7684\u76ee\u7684\uff1a<br \/>\u2460 \u5927\u6570\u503c\u4f1a\u5bfc\u81f4\u8bad\u7ec3\u4e0d\u7a33\u5b9a\uff0c\u5982\u679c\u8f93\u5165\u90fd\u5f88\u5927\uff080~255\uff09\uff0c\u68af\u5ea6\u4e5f\u4f1a\u5f88\u5927\uff0c\u4f1a\u8ba9\u6a21\u578b\u9707\u8361\u3001\u96be\u4ee5\u6536\u655b\u3002<br \/>\u2461 \u8fd9\u662f\u56fe\u50cf\u5904\u7406\u9886\u57df\u7684\u6807\u51c6\u505a\u6cd5\uff0c\u51e0\u4e4e\u6240\u6709\u6df1\u5ea6\u5b66\u4e60\u4efb\u52a1\u90fd\u8981\u505a\u5f52\u4e00\u5316\uff08normalization\uff09\u3002<\/p>\n<h3>4.\u642d\u5efa\u7b80\u5355\u7684\u795e\u7ecf\u6a21\u578b\uff1a<\/h3>\n<pre><code>model = tf.keras.Sequential([\n    # \u628a 28x28 \u7684\u4e8c\u7ef4\u56fe\u7247\u62c9\u6210\u4e00\u6761 784 \u957f\u5ea6\u7684\u5411\u91cf\uff0c\u65b9\u4fbf\u795e\u7ecf\u7f51\u7edc\u5904\u7406\n    # Sequential \u8868\u793a\u201c\u6309\u987a\u5e8f\u5806\u53e0\u7684\u795e\u7ecf\u7f51\u7edc\u201d\uff0c\u4e00\u5c42\u63a5\u4e00\u5c42\uff0c\u975e\u5e38\u5e38\u89c1\u3002\n    tf.keras.layers.Flatten(input_shape=(28, 28)),\n    # \u5168\u8fde\u63a5\u5c42\uff1a128 \u4e2a\u795e\u7ecf\u5143\uff0c\u7528 ReLU \u6fc0\u6d3b\uff0c\u8d1f\u8d23\u63d0\u53d6\u7279\u5f81\n    # \u5b83\u8d1f\u8d23\u4ece\u56fe\u50cf\u4e2d\u63d0\u53d6\u7279\u5f81\uff08\u5982\uff1a\u7b14\u753b\u65b9\u5411\u3001\u7c97\u7ec6\u3001\u7ed3\u6784\uff09\u3002\n    tf.keras.layers.Dense(128, activation='relu'),\n    # \u8f93\u51fa\u5c42\uff1a10 \u4e2a\u795e\u7ecf\u5143\uff0c\u5bf9\u5e94\u6570\u5b57 0~9\uff0csoftmax \u8f93\u51fa\u6bcf\u4e2a\u6570\u5b57\u7684\u6982\u7387\n    # softmax\uff1a\u7528\u4e8e\u5206\u7c7b\uff0c\u5f97\u5230\u5c5e\u4e8e\u6bcf\u4e00\u7c7b\u7684\u6982\u7387\n    tf.keras.layers.Dense(10, activation='softmax') \n])\n\n# \u7f16\u8bd1\u6a21\u578b\uff1a\u9009\u62e9\u4f18\u5316\u5668\u3001\u635f\u5931\u51fd\u6570\u3001\u8bc4\u4f30\u6307\u6807\n# Adam \u662f\u4e00\u79cd\u52a8\u6001\u8c03\u6574\u5b66\u4e60\u7387\u7684\u4f18\u5316\u7b97\u6cd5\n# \nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])<\/code><\/pre>\n<p>\u6253\u5370\u5185\u5bb9\uff1a<\/p>\n<pre><code>Model: &quot;sequential&quot;\n\u250f\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2513\n\u2503 Layer (type)                    \u2503 Output Shape           \u2503       Param # \u2503\n\u2521\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2547\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2547\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2529\n\u2502 flatten (Flatten)               \u2502 (None, 784)            \u2502             0 \u2502\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n\u2502 dense (Dense)                   \u2502 (None, 128)            \u2502       100,480 \u2502\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n\u2502 dense_1 (Dense)                 \u2502 (None, 10)             \u2502         1,290 \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n Total params: 101,770 (397.54 KB)\n Trainable params: 101,770 (397.54 KB)\n Non-trainable params: 0 (0.00 B)\n<\/code><\/pre>\n<ol>\n<li>\n<p>flatten (\u8bd1\u4e3a\u538b\u6241\uff09\uff1a<\/p>\n<ul>\n<li>\u4f5c\u7528\uff1a\u628a28\u00d728\u7684\u56fe\u7247\u538b\u5e73\u6210784\u4e2a\u50cf\u7d20\u70b9\u7684\u4e00\u7ef4\u6570\u7ec4\uff0c<\/li>\n<li>\u8f93\u51fa\u5f62\u72b6\uff1a(None, 784)\uff0cNone = \u6279\u5904\u7406\u5927\u5c0f\uff08\u53ef\u4ee5\u662f\u4efb\u610f\u6570\u91cf\uff0c\u6bd4\u598232\u300164\u5f20\u56fe\u7247\u4e00\u6279\uff09\uff1b784 = 28\u00d728 \u4e2a\u50cf\u7d20\u70b9<\/li>\n<li>\u53c2\u6570\u6570\u91cf\uff1a0\uff08\u53ea\u662f\u91cd\u6392\u64cd\u4f5c\uff0c\u6ca1\u6709\u53ef\u5b66\u4e60\u7684\u53c2\u6570\uff09<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>dense\uff1a<\/p>\n<ul>\n<li>\u4f5c\u7528\uff1a\u5168\u8fde\u63a5\u5c42\uff0c128\u4e2a\u795e\u7ecf\u5143<\/li>\n<li>\u8f93\u51fa\u5f62\u72b6\uff1a(None, 128)\uff08\u6bcf\u5f20\u56fe\u7247\u8f93\u51fa128\u4e2a\u7279\u5f81\u503c\uff09<\/li>\n<li>\u53c2\u6570\u6570\u91cf\uff1a100,480\u4e2a\u53ef\u5b66\u4e60\u53c2\u6570\uff0c<\/li>\n<li>\u53c2\u6570\u8ba1\u7b97 = (\u8f93\u5165\u7ef4\u5ea6 \u00d7 \u8f93\u51fa\u7ef4\u5ea6) + \u8f93\u51fa\u7ef4\u5ea6\u504f\u7f6e\u7684\u4e2a\u6570\uff0c\u6709\u4e86\u504f\u7f6e\uff0c\u795e\u7ecf\u5143\u53ef\u4ee5\u201c\u6574\u4f53\u5e73\u79fb\u201d\uff0c\u5373\u4f7f\u8f93\u5165\u4e3a 0 \u4e5f\u53ef\u4ee5\u8f93\u51fa\u975e\u96f6\u503c\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>dense_1:<\/p>\n<ul>\n<li>\u4f5c\u7528\uff1a\u8f93\u51fa\u5c42\uff0c10\u4e2a\u795e\u7ecf\u5143\uff08\u5bf9\u5e940-9\u5341\u4e2a\u6570\u5b57\uff09<\/li>\n<li>\u8f93\u51fa\u5f62\u72b6\uff1a(None, 10)\uff08\u6bcf\u5f20\u56fe\u7247\u8f93\u51fa10\u4e2a\u6982\u7387\u503c\uff09<\/li>\n<li>\u53c2\u6570\u6570\u91cf\uff1a1,290\u4e2a\u53ef\u5b66\u4e60\u53c2\u6570<\/li>\n<\/ul>\n<h3>5.\u8bad\u7ec3\u6a21\u578b\uff1a<\/h3>\n<pre><code>history = model.fit(x_train, y_train,\n                    epochs=5,     # 5\u6b21\u5b8c\u6574\u904d\u5386\u8bad\u7ec3\u96c6\n                    batch_size=32,  \n                    validation_split=0.1,  # \u628a\u8bad\u7ec3\u6570\u636e\u7684 10% \u81ea\u52a8\u5212\u5206\u4e3a\u9a8c\u8bc1\u96c6\n                    verbose=1)<\/code><\/pre>\n<p>\u8bad\u7ec3\u8fc7\u7a0b\uff1a<br \/><img decoding=\"async\" width=\"723\" height=\"169\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3vp\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h3>6.\u9a8c\u8bc1\u6a21\u578b\u662f\u5426\u80fd\u771f\u6b63\u7684\u8bc6\u522b\u6570\u5b57\uff1a<\/h3>\n<pre><code>plt.figure(figsize=(15, 8))\n \n# \u968f\u673a\u6311 12 \u5f20\uff08\u4e0d\u4f1a\u91cd\u590d\uff09\nindices = np.random.choice(len(x_test), 12, replace=False)\n \nfor i, idx in enumerate(indices):\n    plt.subplot(3, 4, i + 1)\n \n    test_image = x_test[idx]\n    true_label = y_test[idx]\n \n    prediction = model.predict(np.array([test_image]), verbose=0)    # \u8ba9\u6a21\u578b\u9884\u6d4b\u8fd9\u5f20\u56fe\u7247\u5c5e\u4e8e 0~9 \u4e2d\u54ea\u4e00\u4e2a\n    predicted_label = np.argmax(prediction)   # \u627e\u5230\u6982\u7387\u6700\u5927\u7684\u6570\u5b57\uff0c\u5c31\u662f\u9884\u6d4b\u503c\n    confidence = np.max(prediction)   # \u6a21\u578b\u5bf9\u8be5\u9884\u6d4b\u7684\u7f6e\u4fe1\u5ea6\uff080~1\uff09\n \n    plt.imshow(test_image, cmap='gray')\n \n    # Check if prediction is correct\n    if predicted_label == true_label:\n        color = 'green'\n        result = &quot;\u2713 Correct&quot;\n    else:\n        color = 'red'\n        result = &quot;\u2717 Wrong&quot;\n \n    plt.title(f'True: {true_label} | Pred: {predicted_label}\/n{result} | Conf: {confidence:.2f}',\n              color=color, fontsize=10)\n    plt.axis('off')\n \nplt.suptitle('Model Prediction Results (Green=Correct, Red=Wrong)', fontsize=16, fontweight='bold')\nplt.tight_layout()<\/code><\/pre>\n<p>\u9a8c\u8bc1\u7ed3\u679c\uff1a<br \/><img decoding=\"async\" width=\"723\" height=\"459\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3vq\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u81f3\u6b64\u4e00\u4e2a\u7b80\u6d01\u7684\u8bc6\u522b\u6a21\u578b\u5df2\u7ecf\u8bad\u7ec3\u5b8c\u6210\uff01<\/p>\n<\/li>\n<\/ol>\n<hr>\n<h2>\u4f7f\u7528\u8bad\u7ec3\u7684\u6a21\u578b\u8bc6\u522b\u6211\u7684\u624b\u5199\u56fe\u7247<\/h2>\n<p>\u6211\u5229\u7528Krita\u624b\u5199\u4e860-9\u7684\u56fe\u7247\uff0c\u5982\u4e0b\uff1a<br \/>  <img decoding=\"async\" width=\"723\" height=\"260\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdm3vw\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<pre><code>def recognize_digit(image_path):\n  &quot;&quot;&quot;\u4f7f\u7528 PIL \u6253\u5f00\u56fe\u7247\u5e76\u7528\u6a21\u578b\u8bc6\u522b\u6570\u5b57&quot;&quot;&quot;\n  try:\n      img = Image.open(image_path)\n\n      # \u5982\u679c\u4e0d\u662f\u7070\u5ea6\u56fe\uff08'L' \u6a21\u5f0f\uff09\uff0c\u5219\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\n      if img.mode != 'L':\n          img = img.convert('L')\n\n      print(f&quot;\ud83d\udcca Original image size: {img.size}&quot;)\n\n      # \u8c03\u6574\u4e3a 28x28\uff08MNIST \u4f7f\u7528\u7684\u5c3a\u5bf8\uff09\uff0c\u4f7f\u7528\u9ad8\u8d28\u91cf\u91cd\u91c7\u6837\n      img_resized = img.resize((28, 28), Image.Resampling.LANCZOS)\n\n      # \u8f6c\u4e3a numpy \u6570\u7ec4\n      img_array = np.array(img_resized)\n\n      # \u81ea\u52a8\u68c0\u6d4b\u5e76\u5728\u5fc5\u8981\u65f6\u53cd\u8f6c\u989c\u8272\uff08MNIST \u662f\u767d\u5e95\u9ed1\u5b57\uff09\n      # \u5982\u679c\u5e73\u5747\u7070\u5ea6\u8f83\u9ad8\uff08\u63a5\u8fd1\u767d\u5e95\uff09\uff0c\u5219\u53cd\u8f6c\u4e3a\u767d\u5e95\u9ed1\u5b57\n      if np.mean(img_array) &gt; 128:\n          img_array = 255 - img_array\n          print(&quot;\ud83d\udd04 \u68c0\u6d4b\u5230\u767d\u8272\u80cc\u666f\uff0c\u5df2\u53cd\u8f6c\u989c\u8272&quot;)\n\n      # \u5f52\u4e00\u5316\u5230 0-1\uff08\u6d6e\u70b9\u6570\uff09\n      img_normalized = img_array \/ 255.0\n\n      print_pixel_table(img_array)\n\n      # \u9884\u6d4b\uff1a\u6ce8\u610f model \u671f\u671b\u8f93\u5165\u5f62\u72b6\u662f (batch, 28, 28)\n      prediction = model.predict(np.array([img_normalized]), verbose=0)\n      predicted_digit = np.argmax(prediction)\n      confidence = np.max(prediction)\n\n      print(f&quot;\/n\ud83c\udfaf \u9884\u6d4b\u7ed3\u679c\uff1a&quot;)\n      print(f&quot;   \ud83e\udd16 \u9884\u6d4b\u6570\u5b57: {predicted_digit}&quot;)\n      print(f&quot;   \ud83d\udcc8 \u7f6e\u4fe1\u5ea6: {confidence:.2%}&quot;)\n      print(f&quot;   \ud83d\udcc1 \u56fe\u7247\u6587\u4ef6: {os.path.basename(image_path)}&quot;)\n\n      # \u663e\u793a\u6240\u6709\u6982\u7387\uff08\u53ea\u663e\u793a\u5927\u4e8e 1% \u7684\u9879\uff0c\u4fbf\u4e8e\u9605\u8bfb\uff09\n      print(f&quot;\/n\ud83d\udcca \u5404\u6570\u5b57\u6982\u7387\uff1a&quot;)\n      for i, prob in enumerate(prediction[0]):\n          if prob &gt; 0.01:  # \u4ec5\u663e\u793a\u6982\u7387 &gt; 1%\n              marker = &quot;\ud83c\udfaf&quot; if i == predicted_digit else &quot;  &quot;\n              print(f&quot;   {marker} \u6570\u5b57 {i}: {prob:.2%}&quot;)\n\n      # \u6839\u636e\u7f6e\u4fe1\u5ea6\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u7b49\u7ea7\u63d0\u793a\n      print(f&quot;\/n \u7f6e\u4fe1\u5ea6\u7b49\u7ea7\uff1a&quot;, end=&quot;&quot;)\n      if confidence &gt; 0.9:\n          print(&quot;\u2b50\u2b50\u2b50\u2b50\u2b50 (\u975e\u5e38\u786e\u5b9a)&quot;)\n      elif confidence &gt; 0.7:\n          print(&quot;\u2b50\u2b50\u2b50\u2b50 (\u8f83\u597d)&quot;)\n      elif confidence &gt; 0.5:\n          print(&quot;\u2b50\u2b50\u2b50 (\u4e00\u822c)&quot;)\n      else:\n          print(&quot;\u2b50 (\u7f6e\u4fe1\u5ea6\u8f83\u4f4e)&quot;)\n\n      return predicted_digit, confidence\n\n  except Exception as e:\n      print(f&quot;\u8bc6\u522b\u51fa\u9519: {e}&quot;)\n      return None\n\n\ndef print_pixel_table(img_array):\n  &quot;&quot;&quot;\u6253\u5370 28x28 \u539f\u59cb\u50cf\u7d20\u503c\uff080-255\uff09&quot;&quot;&quot;\n  print(f&quot;\u56fe\u7247\u5c3a\u5bf8: {img_array.shape}&quot;)\n\n  # \u6253\u5370 28 \u884c \u00d7 28 \u5217\u50cf\u7d20\u503c\n  for i in range(28):\n      row_values = []\n      for j in range(28):\n          row_values.append(f&quot;{img_array[i, j]:3d}&quot;)  # \u683c\u5f0f\u5bf9\u9f50\n      print(&quot; &quot;.join(row_values))\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u4ee5\u6570\u5b572\u4e3a\u4f8b\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u8f93\u5165\u56fe\u7247\u8def\u5f84\uff08\u6216\u8f93\u5165 'q' \u9000\u51fa\uff09\uff1amy_handwriting\/2.png\n\u2705 \u56fe\u7247\u683c\u5f0f: PNG\n\u2705 \u56fe\u7247\u6a21\u5f0f: RGBA\n\u2705 \u56fe\u7247\u5c3a\u5bf8: (400, 300)\n\ud83d\udcca Original image size: (400, 300)\n\ud83d\udd04 \u68c0\u6d4b\u5230\u767d\u8272\u80cc\u666f\uff0c\u5df2\u53cd\u8f6c\u989c\u8272\n\u56fe\u7247\u5c3a\u5bf8: (28, 28)\n0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   0   0   1   3   4   4   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   0   3   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   1   1   0   5  80 108 101  10   0   1   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   1   0   0 113 185 155 113 146 133   0   3   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   1   0  17 178 147  31   0   0   0 150   6   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   2   9 173  88   0   0   4   5   2 136  21   0   1   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0  11  47   0   4   1   0   1   0 137  21   0   1   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   2   0   0   0   0   3 144   7   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   2   0   0   0   1   0  24 143   0   1   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   0   0   0   2   1  60 125   0   4   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   0   0   0   4   0 126  73   0   3   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   0   0   1   2  21 165   4   2   3   3   1   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   0   0   4   0 129  86   2   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   0   2   0  47 161   0   0  10  68  72  30   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   1   3   0 151  42  81 160 184 159 158 141   1   1   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   4   0 168 224 171 157  75  18   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   3   0 131 241 112  35   0   0   0   3   3   1   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   3   0  80 200  26   0   0   3   2   1   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   2   0  81  20   0   5   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   0   0   2   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   2   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0\n\n\ud83c\udfaf \u9884\u6d4b\u7ed3\u679c\uff1a\n \ud83e\udd16 \u9884\u6d4b\u6570\u5b57: 2\n \ud83d\udcc8 \u7f6e\u4fe1\u5ea6: 88.53%\n \ud83d\udcc1 \u56fe\u7247\u6587\u4ef6: 2.png\n\n\ud83d\udcca \u5404\u6570\u5b57\u6982\u7387\uff1a\n \ud83c\udfaf \u6570\u5b57 2: 88.53%\n    \u6570\u5b57 3: 3.64%\n    \u6570\u5b57 6: 3.67%\n    \u6570\u5b57 8: 1.82%\n\n \u7f6e\u4fe1\u5ea6\u7b49\u7ea7\uff1a\u2b50\u2b50\u2b50\u2b50 (\u8f83\u597d)\n<\/code><\/pre>\n<h2>\u7ed3\u8bed<\/h2>\n<p>\u81f3\u6b64\uff0c\u4e00\u4e2a\u5c0f\u578b\u7684\u56fe\u50cf\u8bc6\u522b\u57fa\u672c\u5b8c\u6210\uff0c\u4f46\u662f\u5e76\u4e0d\u662f\u6240\u6709\u7684\u624b\u5199\u6570\u5b57\u7f6e\u4fe1\u5ea6\u7b49\u7ea7\u90fd\u53ef\u4ee5\u8fbe\u5230\u8fd9\u4e48\u9ad8\uff0c\u4ec5\u4ec5\u4f9d\u9760\u8fd9\u4e2a\u5c0f\u7684\u6a21\u578b\uff0c\u8fd8\u662f\u8fdc\u8fdc\u4e0d\u591f\u7684\uff0c\u63a5\u4e0b\u6765\u4f1a\u5c1d\u8bd5\u5229\u7528CNN\uff08\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\uff09\u6765\u8fdb\u884c\u8bad\u7ec3\u3002\u7531\u4e8e\u4e0d\u592a\u61c2\u5f97\u8fd9\u65b9\u9762\u7684\u4e13\u4e1a\u77e5\u8bc6\uff0c\u53ef\u80fd\u4f1a\u5b58\u5728\u5f88\u591a\u95ee\u9898\uff0c\u6b22\u8fce\u7559\u8a00\uff0c\u6211\u4f1a\u79ef\u6781\u6539\u6b63\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047401707\">https:\/\/segmentfault.com\/a\/1190000047401707<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u521d\u6b65\u63a2\u7d22\u4e86\u8ba1\u7b97\u673a\u8bc6\u522b\u624b\u5199\u6570\u5b57\u7684\u9886\u57df\uff0c\u5c06\u4e86\u89e3\u5230\u7684\u57fa\u672c\u77e5\u8bc6\u603b\u7ed3\u5982\u4e0b\u3002 \u8ba1\u7b97\u673a\u5982\u4f55&#8221;\u770b&#038;#8221 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021094","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021094"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021094\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}