{"id":1135297,"date":"2025-01-08T21:23:55","date_gmt":"2025-01-08T13:23:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1135297.html"},"modified":"2025-01-08T21:23:58","modified_gmt":"2025-01-08T13:23:58","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%9b%be%e5%83%8f%e5%83%8f%e7%b4%a0%e6%8c%89%e5%83%8f%e7%b4%a0%e5%80%bc%e5%8c%ba%e5%88%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1135297.html","title":{"rendered":"python\u5982\u4f55\u5c06\u56fe\u50cf\u50cf\u7d20\u6309\u50cf\u7d20\u503c\u533a\u5206"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104001\/ce4a6ff7-d874-4865-ad14-ac86588f82ff.webp\" alt=\"python\u5982\u4f55\u5c06\u56fe\u50cf\u50cf\u7d20\u6309\u50cf\u7d20\u503c\u533a\u5206\" \/><\/p>\n<p><p> <strong>Python\u5c06\u56fe\u50cf\u50cf\u7d20\u6309\u50cf\u7d20\u503c\u533a\u5206\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Pillow\u5e93\u8bfb\u53d6\u56fe\u50cf\u3001\u4f7f\u7528NumPy\u8fdb\u884c\u50cf\u7d20\u503c\u64cd\u4f5c\u3001\u6839\u636e\u6761\u4ef6\u8fdb\u884c\u5206\u7c7b<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528NumPy\u8fdb\u884c\u50cf\u7d20\u503c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u8fdb\u884c\u50cf\u7d20\u503c\u64cd\u4f5c\u7684\u65b9\u6cd5\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u9996\u5148\uff0c\u4f7f\u7528Pillow\u5e93\u8bfb\u53d6\u56fe\u50cf\u5e76\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff1b\u63a5\u7740\uff0c\u4f7f\u7528NumPy\u7684\u6761\u4ef6\u7b5b\u9009\u529f\u80fd\u5bf9\u50cf\u7d20\u503c\u8fdb\u884c\u533a\u5206\u548c\u5206\u7c7b\uff1b\u6700\u540e\uff0c\u6839\u636e\u5206\u7c7b\u7ed3\u679c\u8fdb\u884c\u56fe\u50cf\u64cd\u4f5c\u6216\u8f93\u51fa\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u5904\u7406\u6548\u7387\u9ad8\uff0c\u5e76\u4e14NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u7075\u6d3b\u5904\u7406\u5404\u79cd\u56fe\u50cf\u6570\u636e\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u56fe\u50cf\u8bfb\u53d6\u4e0e\u521d\u6b65\u5904\u7406<\/strong><\/h2>\n<p><h2>1\u3001\u4f7f\u7528Pillow\u5e93\u8bfb\u53d6\u56fe\u50cf<\/h2>\n<\/p>\n<p><p>Pillow\u662fPython\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u56fe\u50cf\u8bfb\u53d6\u548c\u4fdd\u5b58\u7b49\u64cd\u4f5c\u3002\u6211\u4eec\u9996\u5148\u9700\u8981\u5b89\u88c5Pillow\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bfb\u53d6\u56fe\u50cf\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u5e76\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;path\/to\/your\/image.jpg&#39;).convert(&#39;L&#39;)<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>image_array = np.array(image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>convert(&#39;L&#39;)<\/code>\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff0c\u8fd9\u6837\u6bcf\u4e2a\u50cf\u7d20\u53ea\u6709\u4e00\u4e2a\u503c\uff080-255\uff09\uff0c\u4fbf\u4e8e\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u57fa\u7840\u56fe\u50cf\u4fe1\u606f\u83b7\u53d6<\/h2>\n<\/p>\n<p><p>\u83b7\u53d6\u56fe\u50cf\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u5982\u5c3a\u5bf8\u3001\u50cf\u7d20\u503c\u8303\u56f4\u7b49\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u56fe\u50cf\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u56fe\u50cf\u5c3a\u5bf8<\/p>\n<p>width, height = image.size<\/p>\n<h2><strong>\u83b7\u53d6\u50cf\u7d20\u503c\u8303\u56f4<\/strong><\/h2>\n<p>min_pixel = np.min(image_array)<\/p>\n<p>max_pixel = np.max(image_array)<\/p>\n<p>print(f&quot;\u56fe\u50cf\u5c3a\u5bf8: {width}x{height}&quot;)<\/p>\n<p>print(f&quot;\u50cf\u7d20\u503c\u8303\u56f4: {min_pixel}-{max_pixel}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<hr>\n<h2><strong>\u4e8c\u3001\u50cf\u7d20\u503c\u5206\u7c7b\u4e0e\u64cd\u4f5c<\/strong><\/h2>\n<p><h2>1\u3001\u4f7f\u7528NumPy\u8fdb\u884c\u50cf\u7d20\u5206\u7c7b<\/h2>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u50cf\u7d20\u503c\u8fdb\u884c\u5206\u7c7b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u50cf\u7d20\u503c\u7684\u8303\u56f4\u5bf9\u56fe\u50cf\u8fdb\u884c\u4e8c\u503c\u5316\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u9608\u503c<\/p>\n<p>threshold = 128<\/p>\n<h2><strong>\u521b\u5efa\u4e8c\u503c\u5316\u56fe\u50cf\u6570\u7ec4<\/strong><\/h2>\n<p>binary_image_array = (image_array &gt; threshold) * 255<\/p>\n<h2><strong>\u5c06\u4e8c\u503c\u5316\u6570\u7ec4\u8f6c\u6362\u4e3a\u56fe\u50cf<\/strong><\/h2>\n<p>binary_image = Image.fromarray(np.uint8(binary_image_array))<\/p>\n<p>binary_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06\u50cf\u7d20\u503c\u5927\u4e8e\u9608\u503c\u7684\u50cf\u7d20\u8bbe\u7f6e\u4e3a255\uff08\u767d\u8272\uff09\uff0c\u5c0f\u4e8e\u7b49\u4e8e\u9608\u503c\u7684\u50cf\u7d20\u8bbe\u7f6e\u4e3a0\uff08\u9ed1\u8272\uff09\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u56fe\u50cf\u7684\u4e8c\u503c\u5316\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u66f4\u591a\u590d\u6742\u7684\u50cf\u7d20\u503c\u64cd\u4f5c<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u7b80\u5355\u7684\u4e8c\u503c\u5316\u5904\u7406\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u8fdb\u884c\u66f4\u52a0\u590d\u6742\u7684\u50cf\u7d20\u503c\u64cd\u4f5c\uff0c\u4f8b\u5982\u5c06\u50cf\u7d20\u503c\u5206\u6210\u591a\u4e2a\u533a\u95f4\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u533a\u95f4\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u50cf\u7d20\u503c\u533a\u95f4<\/p>\n<p>intervals = [0, 64, 128, 192, 256]<\/p>\n<h2><strong>\u521b\u5efa\u5206\u7c7b\u6570\u7ec4<\/strong><\/h2>\n<p>classified_image_array = np.zeros_like(image_array)<\/p>\n<h2><strong>\u904d\u5386\u533a\u95f4\u5e76\u8fdb\u884c\u5206\u7c7b\u64cd\u4f5c<\/strong><\/h2>\n<p>for i in range(len(intervals) - 1):<\/p>\n<p>    lower_bound = intervals[i]<\/p>\n<p>    upper_bound = intervals[i+1]<\/p>\n<p>    mask = (image_array &gt;= lower_bound) &amp; (image_array &lt; upper_bound)<\/p>\n<p>    classified_image_array[mask] = (i * 255) \/\/ (len(intervals) - 1)<\/p>\n<h2><strong>\u5c06\u5206\u7c7b\u6570\u7ec4\u8f6c\u6362\u4e3a\u56fe\u50cf<\/strong><\/h2>\n<p>classified_image = Image.fromarray(np.uint8(classified_image_array))<\/p>\n<p>classified_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06\u50cf\u7d20\u503c\u5206\u6210\u4e86\u56db\u4e2a\u533a\u95f4\uff0c\u5e76\u5c06\u6bcf\u4e2a\u533a\u95f4\u7684\u50cf\u7d20\u503c\u6620\u5c04\u5230\u4e0d\u540c\u7684\u7070\u5ea6\u7ea7\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u66f4\u52a0\u590d\u6742\u7684\u5206\u7c7b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e09\u3001\u5e94\u7528\u573a\u666f\u4e0e\u5b9e\u6218\u6848\u4f8b<\/strong><\/h2>\n<p><h2>1\u3001\u56fe\u50cf\u5206\u5272<\/h2>\n<\/p>\n<p><p>\u56fe\u50cf\u5206\u5272\u662f\u8ba1\u7b97\u673a\u89c6\u89c9\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u4efb\u52a1\uff0c\u65e8\u5728\u5c06\u56fe\u50cf\u5212\u5206\u4e3a\u591a\u4e2a\u5177\u6709\u76f8\u4f3c\u5c5e\u6027\u7684\u533a\u57df\u3002\u4f7f\u7528\u50cf\u7d20\u503c\u5206\u7c7b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7b80\u5355\u7684\u56fe\u50cf\u5206\u5272\u3002\u4f8b\u5982\uff0c\u5c06\u524d\u666f\u548c\u80cc\u666f\u5206\u5f00\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u524d\u666f\u80cc\u666f\u9608\u503c<\/p>\n<p>foreground_threshold = 128<\/p>\n<h2><strong>\u521b\u5efa\u524d\u666f\u80cc\u666f\u63a9\u7801<\/strong><\/h2>\n<p>foreground_mask = image_array &gt; foreground_threshold<\/p>\n<p>background_mask = image_array &lt;= foreground_threshold<\/p>\n<h2><strong>\u521b\u5efa\u5206\u5272\u56fe\u50cf\u6570\u7ec4<\/strong><\/h2>\n<p>segmented_image_array = np.zeros_like(image_array)<\/p>\n<p>segmented_image_array[foreground_mask] = 255<\/p>\n<h2><strong>\u5c06\u5206\u5272\u6570\u7ec4\u8f6c\u6362\u4e3a\u56fe\u50cf<\/strong><\/h2>\n<p>segmented_image = Image.fromarray(np.uint8(segmented_image_array))<\/p>\n<p>segmented_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6839\u636e\u9608\u503c\u5c06\u56fe\u50cf\u5206\u5272\u4e3a\u524d\u666f\u548c\u80cc\u666f\uff0c\u524d\u666f\u50cf\u7d20\u8bbe\u7f6e\u4e3a\u767d\u8272\uff0c\u80cc\u666f\u50cf\u7d20\u8bbe\u7f6e\u4e3a\u9ed1\u8272\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u7b80\u5355\u7684\u56fe\u50cf\u5206\u5272\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u56fe\u50cf\u589e\u5f3a<\/h2>\n<\/p>\n<p><p>\u56fe\u50cf\u589e\u5f3a\u65e8\u5728\u63d0\u9ad8\u56fe\u50cf\u7684\u89c6\u89c9\u6548\u679c\uff0c\u4f7f\u5f97\u56fe\u50cf\u66f4\u5bb9\u6613\u88ab\u4eba\u7c7b\u6216\u8ba1\u7b97\u673a\u7406\u89e3\u3002\u901a\u8fc7\u50cf\u7d20\u503c\u5206\u7c7b\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u79cd\u56fe\u50cf\u589e\u5f3a\u64cd\u4f5c\uff0c\u4f8b\u5982\u5bf9\u6bd4\u5ea6\u589e\u5f3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u5bf9\u6bd4\u5ea6\u589e\u5f3a\u51fd\u6570<\/p>\n<p>def enhance_contrast(image_array, factor):<\/p>\n<p>    # \u8ba1\u7b97\u56fe\u50cf\u7684\u5e73\u5747\u50cf\u7d20\u503c<\/p>\n<p>    mean_pixel = np.mean(image_array)<\/p>\n<p>    # \u589e\u5f3a\u5bf9\u6bd4\u5ea6<\/p>\n<p>    enhanced_image_array = (image_array - mean_pixel) * factor + mean_pixel<\/p>\n<p>    # \u9650\u5236\u50cf\u7d20\u503c\u8303\u56f4\u57280-255<\/p>\n<p>    enhanced_image_array = np.clip(enhanced_image_array, 0, 255)<\/p>\n<p>    return enhanced_image_array<\/p>\n<h2><strong>\u589e\u5f3a\u56fe\u50cf\u5bf9\u6bd4\u5ea6<\/strong><\/h2>\n<p>contrast_factor = 1.5<\/p>\n<p>enhanced_image_array = enhance_contrast(image_array, contrast_factor)<\/p>\n<h2><strong>\u5c06\u589e\u5f3a\u540e\u7684\u6570\u7ec4\u8f6c\u6362\u4e3a\u56fe\u50cf<\/strong><\/h2>\n<p>enhanced_image = Image.fromarray(np.uint8(enhanced_image_array))<\/p>\n<p>enhanced_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u589e\u52a0\u50cf\u7d20\u503c\u4e0e\u5e73\u5747\u503c\u7684\u5dee\u8ddd\uff0c\u589e\u5f3a\u4e86\u56fe\u50cf\u7684\u5bf9\u6bd4\u5ea6\uff0c\u4f7f\u5f97\u56fe\u50cf\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u56db\u3001\u7efc\u5408\u5b9e\u4f8b\uff1a\u624b\u5199\u6570\u5b57\u8bc6\u522b<\/strong><\/h2>\n<p><h2>1\u3001\u6570\u636e\u51c6\u5907<\/h2>\n<\/p>\n<p><p>\u624b\u5199\u6570\u5b57\u8bc6\u522b\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u56fe\u50cf\u5904\u7406\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4efb\u52a1\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528MNIST\u6570\u636e\u96c6\uff0c\u8be5\u6570\u636e\u96c6\u5305\u542b\u4e86\u5927\u91cf\u7684\u624b\u5199\u6570\u5b57\u56fe\u50cf\u3002\u9996\u5148\uff0c\u5b89\u88c5\u5e76\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tensorflow numpy matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>from tensorflow.keras.datasets import mnist<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u52a0\u8f7dMNIST\u6570\u636e\u96c6<\/strong><\/h2>\n<p>(tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_images, train_labels), (test_images, test_labels) = mnist.load_data()<\/p>\n<h2><strong>\u663e\u793a\u4e00\u4e9b\u6837\u672c\u56fe\u50cf<\/strong><\/h2>\n<p>plt.figure(figsize=(10,10))<\/p>\n<p>for i in range(25):<\/p>\n<p>    plt.subplot(5, 5, i+1)<\/p>\n<p>    plt.xticks([])<\/p>\n<p>    plt.yticks([])<\/p>\n<p>    plt.grid(False)<\/p>\n<p>    plt.imshow(train_images[i], cmap=plt.cm.binary)<\/p>\n<p>    plt.xlabel(train_labels[i])<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u52a0\u8f7dMNIST\u6570\u636e\u96c6\u5e76\u663e\u793a\u4e86\u4e00\u4e9b\u6837\u672c\u56fe\u50cf\uff0c\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u6570\u636e\u7684\u683c\u5f0f\u548c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u56fe\u50cf\u9884\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u624b\u5199\u6570\u5b57\u8bc6\u522b\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u56fe\u50cf\u8fdb\u884c\u9884\u5904\u7406\uff0c\u4f8b\u5982\u5f52\u4e00\u5316\u548c\u4e8c\u503c\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5f52\u4e00\u5316\u56fe\u50cf\u50cf\u7d20\u503c\u52300-1\u8303\u56f4<\/p>\n<p>train_images = train_images \/ 255.0<\/p>\n<p>test_images = test_images \/ 255.0<\/p>\n<h2><strong>\u4e8c\u503c\u5316\u5904\u7406<\/strong><\/h2>\n<p>threshold = 0.5<\/p>\n<p>train_images_binary = (train_images &gt; threshold).astype(np.float32)<\/p>\n<p>test_images_binary = (test_images &gt; threshold).astype(np.float32)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u6784\u5efa\u548c\u8bad\u7ec3\u6a21\u578b<\/h2>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\uff08CNN\uff09\u6a21\u578b\uff0c\u5e76\u5728\u9884\u5904\u7406\u540e\u7684\u6570\u636e\u4e0a\u8fdb\u884c\u8bad\u7ec3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">model = tf.keras.Sequential([<\/p>\n<p>    tf.keras.layers.Conv2D(32, (3, 3), activation=&#39;relu&#39;, input_shape=(28, 28, 1)),<\/p>\n<p>    tf.keras.layers.MaxPooling2D((2, 2)),<\/p>\n<p>    tf.keras.layers.Conv2D(64, (3, 3), activation=&#39;relu&#39;),<\/p>\n<p>    tf.keras.layers.MaxPooling2D((2, 2)),<\/p>\n<p>    tf.keras.layers.Flatten(),<\/p>\n<p>    tf.keras.layers.Dense(64, activation=&#39;relu&#39;),<\/p>\n<p>    tf.keras.layers.Dense(10, activation=&#39;softmax&#39;)<\/p>\n<p>])<\/p>\n<p>model.compile(optimizer=&#39;adam&#39;,<\/p>\n<p>              loss=&#39;sparse_categorical_crossentropy&#39;,<\/p>\n<p>              metrics=[&#39;accuracy&#39;])<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model.fit(train_images_binary, train_labels, epochs=5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>4\u3001\u8bc4\u4f30\u6a21\u578b\u6027\u80fd<\/h2>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u5728\u6d4b\u8bd5\u6570\u636e\u4e0a\u8bc4\u4f30\u6a21\u578b\u7684\u6027\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">test_loss, test_acc = model.evaluate(test_images_binary, test_labels)<\/p>\n<p>print(f&quot;Test accuracy: {test_acc}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u624b\u5199\u6570\u5b57\u8bc6\u522b\u7cfb\u7edf\uff0c\u5e76\u4f7f\u7528\u56fe\u50cf\u50cf\u7d20\u503c\u5206\u7c7b\u7684\u65b9\u6cd5\u5bf9\u56fe\u50cf\u8fdb\u884c\u9884\u5904\u7406\uff0c\u4ece\u800c\u63d0\u9ad8\u6a21\u578b\u7684\u8bc6\u522b\u51c6\u786e\u7387\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e94\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u5c06\u56fe\u50cf\u50cf\u7d20\u6309\u50cf\u7d20\u503c\u8fdb\u884c\u533a\u5206\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528Pillow\u5e93\u8bfb\u53d6\u56fe\u50cf\u3001\u4f7f\u7528NumPy\u8fdb\u884c\u50cf\u7d20\u503c\u64cd\u4f5c\u3001\u6839\u636e\u6761\u4ef6\u8fdb\u884c\u5206\u7c7b\u7b49\u3002\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u56fe\u50cf\u5206\u5272\u3001\u56fe\u50cf\u589e\u5f3a\u4ee5\u53ca\u624b\u5199\u6570\u5b57\u8bc6\u522b\u7b49\u5e94\u7528\u573a\u666f\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1\u56fe\u50cf\u50cf\u7d20\u503c\u5206\u7c7b\u7684\u65b9\u6cd5\uff0c\u5e76\u5e94\u7528\u4e8e\u5b9e\u9645\u9879\u76ee\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5bf9\u56fe\u50cf\u8fdb\u884c\u50cf\u7d20\u503c\u5206\u7c7b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u8bf8\u5982OpenCV\u548cPillow\u7b49\u5e93\u6765\u52a0\u8f7d\u56fe\u50cf\u5e76\u5904\u7406\u50cf\u7d20\u503c\u3002\u9996\u5148\uff0c\u52a0\u8f7d\u56fe\u50cf\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u6bcf\u4e2a\u50cf\u7d20\u5e76\u6839\u636e\u5176RGB\u503c\u6216\u7070\u5ea6\u503c\u8fdb\u884c\u5206\u7c7b\u3002\u5229\u7528NumPy\u5e93\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\uff0c\u4ece\u800c\u5b9e\u73b0\u50cf\u7d20\u7684\u5206\u7c7b\u548c\u5206\u6790\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u65b9\u6cd5\u5bf9\u56fe\u50cf\u50cf\u7d20\u8fdb\u884c\u5206\u7c7b\uff1f<\/strong><br \/>\u786e\u5b9e\u53ef\u4ee5\u3002\u501f\u52a9\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u5982TensorFlow\u6216PyTorch\uff0c\u60a8\u53ef\u4ee5\u8bad\u7ec3\u6a21\u578b\u6765\u6839\u636e\u50cf\u7d20\u503c\u5bf9\u56fe\u50cf\u8fdb\u884c\u5206\u7c7b\u3002\u901a\u5e38\uff0c\u60a8\u9700\u8981\u51c6\u5907\u4e00\u4e2a\u6807\u8bb0\u597d\u7684\u6570\u636e\u96c6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\u67b6\u6784\uff0c\u5e76\u4f7f\u7528\u4f18\u5316\u7b97\u6cd5\u6765\u63d0\u5347\u5206\u7c7b\u6027\u80fd\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u50cf\u7d20\uff0c\u8fd8\u80fd\u591f\u7406\u89e3\u56fe\u50cf\u7684\u66f4\u9ad8\u5c42\u6b21\u7279\u5f81\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5206\u7c7b\u540e\u7684\u56fe\u50cf\u8fdb\u884c\u53ef\u89c6\u5316\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u5bf9\u5206\u7c7b\u540e\u7684\u56fe\u50cf\u8fdb\u884c\u53ef\u89c6\u5316\u3002\u901a\u8fc7\u5c06\u4e0d\u540c\u7c7b\u522b\u7684\u50cf\u7d20\u7528\u4e0d\u540c\u7684\u989c\u8272\u8868\u793a\uff0c\u53ef\u4ee5\u76f4\u89c2\u5730\u89c2\u5bdf\u5230\u56fe\u50cf\u4e2d\u7684\u4e0d\u540c\u533a\u57df\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528imshow\u51fd\u6570\u6765\u5c55\u793a\u56fe\u50cf\uff0c\u5e76\u4f7f\u7528colorbar\u6765\u6307\u793a\u4e0d\u540c\u50cf\u7d20\u503c\u5bf9\u5e94\u7684\u7c7b\u522b\u3002\u8fd9\u6837\uff0c\u60a8\u4e0d\u4ec5\u80fd\u770b\u5230\u5206\u7c7b\u7ed3\u679c\uff0c\u8fd8\u80fd\u5206\u6790\u56fe\u50cf\u4e2d\u7684\u7279\u5f81\u5206\u5e03\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u56fe\u50cf\u50cf\u7d20\u6309\u50cf\u7d20\u503c\u533a\u5206\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Pillow\u5e93\u8bfb\u53d6\u56fe\u50cf\u3001\u4f7f\u7528NumPy\u8fdb\u884c\u50cf\u7d20\u503c\u64cd\u4f5c\u3001\u6839\u636e\u6761 [&hellip;]","protected":false},"author":3,"featured_media":1135303,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135297"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1135297"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135297\/revisions"}],"predecessor-version":[{"id":1135305,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135297\/revisions\/1135305"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1135303"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1135297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1135297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1135297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}