{"id":106468,"date":"2026-09-17T14:08:33","date_gmt":"2026-09-17T06:08:33","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/106468.html"},"modified":"2026-09-17T14:08:33","modified_gmt":"2026-09-17T06:08:33","slug":"%e5%88%9d%e8%af%86%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0-%e6%a8%a1%e5%9e%8b%e5%8a%a0%e8%bd%bd%e4%b8%8e%e6%8e%a8%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/106468.html","title":{"rendered":"\u521d\u8bc6\u6df1\u5ea6\u5b66\u4e60\u2014\u2014\u6a21\u578b\u52a0\u8f7d\u4e0e\u63a8\u7406"},"content":{"rendered":"<h3>\u4e00\u3001\u5f15\u8a00&#xff1a;\u5982\u4f55\u52a0\u8f7d\u8bad\u7ec3\u597d\u7684\u6a21\u578b<\/h3>\n<p>\u5728\u524d\u51e0\u7bc7\u535a\u5ba2\u4e2d&#xff0c;\u6211\u4eec\u4ece\u96f6\u6784\u5efa\u4e86CNN\u6a21\u578b&#xff0c;\u7528\u6570\u636e\u589e\u5f3a\u63d0\u5347\u4e86\u6cdb\u5316\u80fd\u529b&#xff0c;\u5e76\u5b66\u4f1a\u4e86\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u4fdd\u5b58\u6700\u4f18\u6a21\u578b\u3002\u73b0\u5728&#xff0c;\u6211\u4eec\u624b\u91cc\u5df2\u7ecf\u6709\u4e86\u4e24\u4e2a\u6a21\u578b\u6587\u4ef6&#xff1a;<\/p>\n<p>best2026-910.pth&#xff1a;\u4fdd\u5b58\u7684\u6a21\u578b\u53c2\u6570&#xff08;state_dict&#xff09;<\/p>\n<p>best910.pth&#xff1a;\u4fdd\u5b58\u7684\u5b8c\u6574TorchScript\u6a21\u578b<\/p>\n<p>\u4f46\u95ee\u9898\u6765\u4e86&#xff1a;\u8bad\u7ec3\u597d\u7684\u6a21\u578b&#xff0c;\u600e\u4e48\u62ff\u6765\u7528&#xff1f; \u603b\u4e0d\u80fd\u5728\u6bcf\u6b21\u9884\u6d4b\u65f6\u90fd\u91cd\u65b0\u8bad\u7ec3\u4e00\u904d\u5427&#xff1f;<\/p>\n<p>\u7b54\u6848\u5c31\u662f\u2014\u2014\u52a0\u8f7d\u6a21\u578b&#xff0c;\u8fdb\u884c\u63a8\u7406&#xff08;Inference&#xff09;\u3002\u63a8\u7406\u662f\u6307\u7528\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u5bf9\u65b0\u7684\u6570\u636e\u8fdb\u884c\u9884\u6d4b\u3002\u672c\u7bc7\u535a\u5ba2\u5c06\u57fa\u4e8e\u4e00\u4efd\u5b8c\u6574\u7684\u63a8\u7406\u4ee3\u7801&#xff0c;\u8bb2\u89e3\u5982\u4f55\u52a0\u8f7d\u6a21\u578b\u3001\u5982\u4f55\u51c6\u5907\u6570\u636e\u3001\u5982\u4f55\u5f97\u5230\u9884\u6d4b\u7ed3\u679c&#xff0c;\u5e76\u5bf9\u6bd4\u9884\u6d4b\u503c\u4e0e\u771f\u5b9e\u503c&#xff0c;\u8bc4\u4f30\u6a21\u578b\u5728\u6d4b\u8bd5\u96c6\u4e0a\u7684\u8868\u73b0\u3002<\/p>\n<h3>\u4e8c\u3001\u6a21\u578b\u52a0\u8f7d\u7684\u4e24\u79cd\u65b9\u5f0f<\/h3>\n<p>PyTorch\u63d0\u4f9b\u4e86\u4e24\u79cd\u4fdd\u5b58\u6a21\u578b\u7684\u65b9\u5f0f&#xff0c;\u5bf9\u5e94\u4e24\u79cd\u52a0\u8f7d\u65b9\u5f0f\u3002\u4ee3\u7801\u4e2d\u540c\u65f6\u5c55\u793a\u4e86\u8fd9\u4e24\u79cd\u65b9\u6cd5\u3002<\/p>\n<h4>2.1 \u65b9\u5f0f\u4e00&#xff1a;\u52a0\u8f7d\u6a21\u578b\u53c2\u6570&#xff08;state_dict&#xff09;<\/h4>\n<p>\u8fd9\u662fPyTorch\u5b98\u65b9\u63a8\u8350\u7684\u65b9\u5f0f\u3002\u4fdd\u5b58\u65f6\u53ea\u4fdd\u5b58\u4e86\u6a21\u578b\u7684\u53c2\u6570&#xff08;\u6743\u91cdw\u548c\u504f\u7f6eb&#xff09;&#xff0c;\u52a0\u8f7d\u65f6\u9700\u8981\u5148\u5b9a\u4e49\u6a21\u578b\u7ed3\u6784&#xff0c;\u518d\u52a0\u8f7d\u53c2\u6570\u3002<\/p>\n<p># \u5b9a\u4e49\u6a21\u578b\u7ed3\u6784&#xff08;\u5fc5\u987b\u4e0e\u8bad\u7ec3\u65f6\u5b8c\u5168\u4e00\u81f4&#xff09;<br \/>\ndevice &#061; &#034;cuda&#034; if torch.cuda.is_available() else &#034;mps&#034; if torch.backends.mps.is_available() else &#034;cpu&#034;<br \/>\nmodel &#061; CNN().to(device)<\/p>\n<p># \u52a0\u8f7d\u53c2\u6570<br \/>\nmodel.load_state_dict(torch.load(&#034;best2026-910.pth&#034;))<\/p>\n<p>\u6b65\u9aa4\u89e3\u6790&#xff1a;<\/p>\n<li>\n<p>CNN()&#xff1a;\u5b9e\u4f8b\u5316\u6a21\u578b&#xff0c;\u6b64\u65f6\u53c2\u6570\u662f\u968f\u673a\u521d\u59cb\u5316\u7684\u3002<\/p>\n<\/li>\n<li>\n<p>torch.load(&#034;best2026-910.pth&#034;)&#xff1a;\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u53c2\u6570\u5b57\u5178\u3002<\/p>\n<\/li>\n<li>\n<p>model.load_state_dict(&#8230;)&#xff1a;\u5c06\u8bfb\u53d6\u5230\u7684\u53c2\u6570\u586b\u5165\u6a21\u578b\u3002<\/p>\n<\/li>\n<p>\u4f18\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6587\u4ef6\u5c0f&#xff0c;\u53ea\u5b58\u53c2\u6570<\/p>\n<\/li>\n<li>\n<p>\u7075\u6d3b&#xff0c;\u53ef\u4ee5\u52a0\u8f7d\u5230\u4e0d\u540c\u4f46\u7ed3\u6784\u76f8\u540c\u7684\u6a21\u578b<\/p>\n<\/li>\n<li>\n<p>\u662fPyTorch\u63a8\u8350\u7684\u6807\u51c6\u505a\u6cd5<\/p>\n<\/li>\n<\/ul>\n<p>\u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5fc5\u987b\u77e5\u9053\u6a21\u578b\u7ed3\u6784&#xff0c;\u5e76\u6b63\u786e\u5b9a\u4e49<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u6a21\u578b\u7ed3\u6784\u6539\u53d8&#xff0c;\u65e7\u53c2\u6570\u53ef\u80fd\u65e0\u6cd5\u52a0\u8f7d<\/p>\n<\/li>\n<\/ul>\n<h4>2.2 \u65b9\u5f0f\u4e8c&#xff1a;\u52a0\u8f7d\u5b8c\u6574\u6a21\u578b&#xff08;TorchScript&#xff09;<\/h4>\n<p># \u52a0\u8f7d\u6a21\u578b<br \/>\nmodel &#061; torch.jit.load(&#034;best910.pth&#034;)<\/p>\n<p>\u8fd9\u662f\u53e6\u4e00\u79cd\u52a0\u8f7d\u65b9\u5f0f\u3002\u4fdd\u5b58\u65f6\u4f7f\u7528 torch.jit.script(model) \u548c torch.jit.save()&#xff0c;\u5c06\u6a21\u578b\u7ed3\u6784\u3001\u53c2\u6570\u548c\u8ba1\u7b97\u56fe\u4e00\u8d77\u4fdd\u5b58&#xff0c;\u52a0\u8f7d\u65f6\u65e0\u9700\u5b9a\u4e49\u6a21\u578b\u7ed3\u6784\u3002<\/p>\n<p>\u4f18\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u65e0\u9700\u5b9a\u4e49\u6a21\u578b\u7ed3\u6784&#xff0c;\u76f4\u63a5\u52a0\u8f7d\u5373\u53ef\u7528<\/p>\n<\/li>\n<li>\n<p>\u53ef\u4ee5\u8de8\u5e73\u53f0\u90e8\u7f72<\/p>\n<\/li>\n<li>\n<p>\u9002\u5408\u751f\u4ea7\u73af\u5883<\/p>\n<\/li>\n<\/ul>\n<p>\u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6587\u4ef6\u8f83\u5927<\/p>\n<\/li>\n<li>\n<p>\u67d0\u4e9b\u52a8\u6001\u7ed3\u6784\u53ef\u80fd\u65e0\u6cd5\u811a\u672c\u5316<\/p>\n<\/li>\n<\/ul>\n<h4>2.3 \u4e24\u79cd\u65b9\u5f0f\u7684\u5bf9\u6bd4<\/h4>\n<table>\n<tr>\u5bf9\u6bd4\u9879state_dictTorchScript<\/tr>\n<tbody>\n<tr>\n<td>\u4fdd\u5b58\u5185\u5bb9<\/td>\n<td>\u4ec5\u53c2\u6570<\/td>\n<td>\u7ed3\u6784&#043;\u53c2\u6570&#043;\u8ba1\u7b97\u56fe<\/td>\n<\/tr>\n<tr>\n<td>\u52a0\u8f7d\u524d\u63d0<\/td>\n<td>\u9700\u5b9a\u4e49\u6a21\u578b\u7ed3\u6784<\/td>\n<td>\u65e0\u9700\u5b9a\u4e49<\/td>\n<\/tr>\n<tr>\n<td>\u6587\u4ef6\u5927\u5c0f<\/td>\n<td>\u5c0f<\/td>\n<td>\u5927<\/td>\n<\/tr>\n<tr>\n<td>\u90e8\u7f72\u7075\u6d3b\u6027<\/td>\n<td>\u4e00\u822c<\/td>\n<td>\u9ad8<\/td>\n<\/tr>\n<tr>\n<td>\u63a8\u8350\u573a\u666f<\/td>\n<td>\u7814\u7a76\u3001\u7ee7\u7eed\u8bad\u7ec3<\/td>\n<td>\u751f\u4ea7\u3001\u8de8\u5e73\u53f0\u90e8\u7f72<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u4e09\u3001\u63a8\u7406\u524d\u7684\u51c6\u5907&#xff1a;\u6570\u636e\u53d8\u6362\u4e0e\u6570\u636e\u96c6<\/h3>\n<p>\u6a21\u578b\u52a0\u8f7d\u5b8c\u6210\u540e&#xff0c;\u8fd8\u9700\u8981\u51c6\u5907\u5f85\u63a8\u7406\u7684\u6570\u636e\u3002\u4ee3\u7801\u4e2d\u590d\u7528\u4e86\u8bad\u7ec3\u65f6\u5b9a\u4e49\u7684 data_transforms \u548c food_dataset \u7c7b\u3002<\/p>\n<h4>3.1 \u9a8c\u8bc1\u96c6\u53d8\u6362<\/h4>\n<p>data_transforms &#061; {<br \/>\n    &#039;trainda&#039;: transforms.Compose([&#8230;]),   # \u8bad\u7ec3\u53d8\u6362&#xff08;\u542b\u6570\u636e\u589e\u5f3a&#xff09;<br \/>\n    &#039;valid&#039;: transforms.Compose([<br \/>\n        transforms.Resize([256, 256]),<br \/>\n        transforms.ToTensor(),<br \/>\n        transforms.Normalize(mean&#061;[0.485, 0.456, 0.406],<br \/>\n                             std&#061;[0.229, 0.224, 0.225])<br \/>\n    ]),<br \/>\n}<\/p>\n<p>\u5173\u952e\u70b9&#xff1a;\u63a8\u7406\u65f6\u4f7f\u7528\u7684\u662f &#039;valid&#039; \u53d8\u6362&#xff0c;\u4e0d\u80fd\u4f7f\u7528 &#039;trainda&#039;\u3002\u56e0\u4e3a\u8bad\u7ec3\u53d8\u6362\u4e2d\u5305\u542b\u968f\u673a\u65cb\u8f6c\u3001\u7ffb\u8f6c\u3001\u989c\u8272\u6296\u52a8\u7b49\u6570\u636e\u589e\u5f3a\u64cd\u4f5c&#xff0c;\u8fd9\u4e9b\u64cd\u4f5c\u4f1a\u5f15\u5165\u968f\u673a\u6027&#xff0c;\u5bfc\u81f4\u540c\u4e00\u5f20\u56fe\u7247\u6bcf\u6b21\u9884\u6d4b\u7ed3\u679c\u53ef\u80fd\u4e0d\u540c\u3002\u63a8\u7406\u65f6\u9700\u8981\u7684\u662f\u786e\u5b9a\u6027\u7684\u9884\u5904\u7406\u3002<\/p>\n<h4>3.2 \u81ea\u5b9a\u4e49\u6570\u636e\u96c6\u7c7b<\/h4>\n<p>food_dataset \u8d1f\u8d23\u8bfb\u53d6 test.txt \u4e2d\u7684\u56fe\u7247\u8def\u5f84\u548c\u6807\u7b7e&#xff0c;\u5e76\u5e94\u7528\u53d8\u6362&#xff1a;<\/p>\n<p>class food_dataset(Dataset):<br \/>\n    def __init__(self, file_path, transform&#061;None):<br \/>\n        # \u8bfb\u53d6\u6587\u4ef6&#xff0c;\u4fdd\u5b58\u8def\u5f84\u548c\u6807\u7b7e<br \/>\n        &#8230;<br \/>\n    def __len__(self):<br \/>\n        return len(self.imgs)<br \/>\n    def __getitem__(self, idx):<br \/>\n        image &#061; Image.open(self.imgs[idx])<br \/>\n        if self.transform:<br \/>\n            image &#061; self.transform(image)<br \/>\n        label &#061; torch.from_numpy(np.array(self.labels[idx], dtype&#061;np.int64))<br \/>\n        return image, label<\/p>\n<h4>3.3 \u521b\u5efa\u6d4b\u8bd5\u6570\u636e\u52a0\u8f7d\u5668<\/h4>\n<p>test_data &#061; food_dataset(file_path&#061;&#039;.\/test.txt&#039;,<br \/>\n                         transform&#061;data_transforms[&#039;valid&#039;])<br \/>\ntest_loader &#061; DataLoader(test_data, batch_size&#061;1, shuffle&#061;True)<\/p>\n<p>\u6ce8\u610f&#xff1a;<\/p>\n<ul>\n<li>\n<p>batch_size&#061;1&#xff1a;\u6bcf\u6b21\u53ea\u5904\u7406\u4e00\u5f20\u56fe\u7247&#xff0c;\u4fbf\u4e8e\u9010\u6761\u8bb0\u5f55\u9884\u6d4b\u7ed3\u679c\u3002<\/p>\n<\/li>\n<li>\n<p>shuffle&#061;True&#xff1a;\u6253\u4e71\u987a\u5e8f&#xff0c;\u4f46\u56e0\u4e3a\u6211\u4eec\u540c\u65f6\u4fdd\u5b58\u9884\u6d4b\u503c\u548c\u771f\u5b9e\u503c&#xff0c;\u987a\u5e8f\u4e0d\u5f71\u54cd\u6700\u7ec8\u8bc4\u4f30\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u53ea\u60f3\u5feb\u901f\u8bc4\u4f30\u51c6\u786e\u7387&#xff0c;\u53ef\u4ee5\u8bbe\u7f6e\u66f4\u5927\u7684 batch_size \u4ee5\u52a0\u901f\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>\u56db\u3001\u6a21\u578b\u63a8\u7406&#xff1a;\u4ece\u8f93\u5165\u5230\u9884\u6d4b<\/h3>\n<p>\u52a0\u8f7d\u6a21\u578b\u548c\u51c6\u5907\u597d\u6570\u636e\u540e&#xff0c;\u5c31\u53ef\u4ee5\u8fdb\u884c\u63a8\u7406\u4e86\u3002\u00a0test_true \u51fd\u6570\u5b8c\u6210\u4e86\u6838\u5fc3\u5de5\u4f5c&#xff1a;<\/p>\n<p>results &#061; []    # \u4fdd\u5b58\u9884\u6d4b\u7ed3\u679c<br \/>\nlabels &#061; []     # \u4fdd\u5b58\u771f\u5b9e\u6807\u7b7e<\/p>\n<p>def test_true(dataloader, model):<br \/>\n    with torch.no_grad():<br \/>\n        for x, y in dataloader:<br \/>\n            x, y &#061; x.to(device), y.to(device)<br \/>\n            pred &#061; model.forward(x)<br \/>\n            results.append(pred.argmax(1).item())<br \/>\n            labels.append(y.item())<\/p>\n<p>test_true(test_loader, model)<br \/>\nprint(&#034;\u9884\u6d4b\u503c&#xff1a;\\\\t&#034;, results)<br \/>\nprint(&#034;\u771f\u5b9e\u503c&#xff1a;\\\\t&#034;, labels)<\/p>\n<h4>4.1 torch.no_grad()\u2014\u2014\u5173\u95ed\u68af\u5ea6\u8ba1\u7b97<\/h4>\n<p>\u63a8\u7406\u65f6\u4e0d\u9700\u8981\u53cd\u5411\u4f20\u64ad&#xff0c;\u56e0\u6b64\u53ef\u4ee5\u5173\u95ed\u68af\u5ea6\u8ba1\u7b97&#xff1a;<\/p>\n<p>with torch.no_grad():<br \/>\n    &#8230;<\/p>\n<p>\u4f5c\u7528&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u51cf\u5c11\u5185\u5b58\u6d88\u8017&#xff08;\u4e0d\u4fdd\u5b58\u8ba1\u7b97\u56fe&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u52a0\u5feb\u8ba1\u7b97\u901f\u5ea6<\/p>\n<\/li>\n<li>\n<p>\u9632\u6b62\u53c2\u6570\u88ab\u610f\u5916\u4fee\u6539<\/p>\n<\/li>\n<\/ul>\n<h4>4.2 \u524d\u5411\u4f20\u64ad<\/h4>\n<p>pred &#061; model.forward(x)<\/p>\n<p>model.forward(x) \u4e5f\u53ef\u4ee5\u7b80\u5199\u4e3a model(x)&#xff0c;PyTorch\u4f1a\u81ea\u52a8\u8c03\u7528 forward \u65b9\u6cd5\u3002\u8f93\u51fa pred \u7684\u5f62\u72b6\u4e3a (batch_size, 20)&#xff0c;\u8868\u793a\u6bcf\u5f20\u56fe\u7247\u5c5e\u4e8e20\u4e2a\u7c7b\u522b\u7684\u5f97\u5206\u3002<\/p>\n<h4>4.3 \u83b7\u53d6\u9884\u6d4b\u7c7b\u522b<\/h4>\n<p>results.append(pred.argmax(1).item())<\/p>\n<ul>\n<li>\n<p>pred.argmax(1)&#xff1a;\u5728\u7ef4\u5ea61&#xff08;\u7c7b\u522b\u7ef4\u5ea6&#xff09;\u4e0a\u53d6\u6700\u5927\u503c\u7684\u7d22\u5f15&#xff0c;\u5373\u9884\u6d4b\u7684\u7c7b\u522b\u3002<\/p>\n<\/li>\n<li>\n<p>.item()&#xff1a;\u5c06\u5f20\u91cf\u8f6c\u4e3aPython\u6807\u91cf\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>4.4 \u4fdd\u5b58\u771f\u5b9e\u6807\u7b7e<\/h4>\n<p>labels.append(y.item())<\/p>\n<p>y \u662f\u5f53\u524d\u6279\u6b21\u7684\u771f\u5b9e\u6807\u7b7e\u5f20\u91cf&#xff0c;.item() \u5c06\u5176\u8f6c\u4e3a\u6574\u6570\u3002<\/p>\n<h3>\u4e94\u3001\u9884\u6d4b\u7ed3\u679c\u5206\u6790<\/h3>\n<p>\u8fd0\u884c\u540e&#xff0c;\u4f1a\u6253\u5370\u51fa\u4e24\u4e2a\u5217\u8868&#xff1a;<\/p>\n<p>\u9884\u6d4b\u503c&#xff1a; [14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14]<br \/>\n\u771f\u5b9e\u503c&#xff1a; [6, 12, 9, 17, 10, 4, 6, 11, 1, 15, 8, 7, 2, 16, 3, 16, 3, 13, 14, 14, 19, 16, 5, 10, 11, 5, 13, 17, 7, 18, 1, 18, 9, 19, 2, 8, 0, 3, 4]<\/p>\n<p>\u901a\u8fc7\u5bf9\u6bd4\u8fd9\u4e24\u4e2a\u5217\u8868&#xff0c;\u6211\u4eec\u53ef\u4ee5&#xff1a;<\/p>\n<h4>5.1 \u8ba1\u7b97\u51c6\u786e\u7387<\/h4>\n<p>correct &#061; sum(1 for p, t in zip(results, labels) if p &#061;&#061; t)<br \/>\naccuracy &#061; correct \/ len(labels) * 100<br \/>\nprint(f&#034;\u51c6\u786e\u7387: {accuracy:.2f}%&#034;)<\/p>\n<h4>5.2 \u627e\u51fa\u9884\u6d4b\u9519\u8bef\u7684\u6837\u672c<\/h4>\n<p>for i, (p, t) in enumerate(zip(results, labels)):<br \/>\n    if p !&#061; t:<br \/>\n        print(f&#034;\u6837\u672c {i}: \u9884\u6d4b&#061;{p}, \u771f\u5b9e&#061;{t}&#034;)<\/p>\n<h4>5.3 \u53ef\u89c6\u5316\u9884\u6d4b\u7ed3\u679c<\/h4>\n<p>\u5982\u679c\u60f3\u67e5\u770b\u5177\u4f53\u56fe\u7247&#xff0c;\u53ef\u4ee5\u7ed3\u5408 test_data \u548c matplotlib&#xff1a;<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p># \u663e\u793a\u524d9\u5f20\u56fe\u7247\u53ca\u5176\u9884\u6d4b\u7ed3\u679c<br \/>\nfig &#061; plt.figure(figsize&#061;(10, 10))<br \/>\nfor i in range(9):<br \/>\n    img, true_label &#061; test_data[i]<br \/>\n    pred_label &#061; results[i]<br \/>\n    ax &#061; fig.add_subplot(3, 3, i&#043;1)<br \/>\n    ax.set_title(f&#034;\u9884\u6d4b: {pred_label}, \u771f\u5b9e: {true_label}&#034;)<br \/>\n    ax.axis(&#039;off&#039;)<br \/>\n    # \u53cd\u6807\u51c6\u5316\u540e\u663e\u793a<br \/>\n    img &#061; img.permute(1, 2, 0).numpy()<br \/>\n    img &#061; img * [0.229, 0.224, 0.225] &#043; [0.485, 0.456, 0.406]<br \/>\n    ax.imshow(img)<br \/>\nplt.show()<\/p>\n<h3>\u516d\u3001\u5b8c\u6574\u63a8\u7406\u6d41\u7a0b\u603b\u7ed3<\/h3>\n<p>import torch<br \/>\nfrom torch import nn<br \/>\nfrom torch.utils.data import DataLoader<br \/>\nfrom torchvision import transforms<br \/>\nfrom PIL import Image<br \/>\nimport numpy as np<\/p>\n<p># 1. \u9009\u62e9\u8bbe\u5907<br \/>\ndevice &#061; &#034;cuda&#034; if torch.cuda.is_available() else &#034;cpu&#034;<\/p>\n<p># 2. \u5b9a\u4e49\u6a21\u578b\u7ed3\u6784<br \/>\nclass CNN(nn.Module):<br \/>\n    def __init__(self):<br \/>\n        super(CNN, self).__init__()<br \/>\n        self.conv1 &#061; nn.Sequential(<br \/>\n            nn.Conv2d(3, 16, 5, 1, 2),<br \/>\n            nn.ReLU(),<br \/>\n            nn.MaxPool2d(2),<br \/>\n        )<br \/>\n        self.conv2 &#061; nn.Sequential(<br \/>\n            nn.Conv2d(16, 32, 5, 1, 2),<br \/>\n            nn.ReLU(),<br \/>\n            nn.Conv2d(32, 64, 5, 1, 2),<br \/>\n            nn.ReLU(),<br \/>\n            nn.MaxPool2d(2),<br \/>\n        )<br \/>\n        self.conv3 &#061; nn.Sequential(<br \/>\n            nn.Conv2d(64, 128, 5, 1, 2),<br \/>\n            nn.ReLU(),<br \/>\n        )<br \/>\n        self.out &#061; nn.Linear(128*64*64, 20)<\/p>\n<p>    def forward(self, x):<br \/>\n        x &#061; self.conv1(x)<br \/>\n        x &#061; self.conv2(x)<br \/>\n        x &#061; self.conv3(x)<br \/>\n        x &#061; x.view(x.size(0), -1)<br \/>\n        return self.out(x)<\/p>\n<p># 3. \u52a0\u8f7d\u6a21\u578b\u53c2\u6570<br \/>\nmodel &#061; CNN().to(device)<br \/>\nmodel.load_state_dict(torch.load(&#034;best2026-910.pth&#034;))<br \/>\nmodel.eval()<\/p>\n<p># 4. \u6570\u636e\u51c6\u5907<br \/>\ndata_transforms &#061; transforms.Compose([<br \/>\n    transforms.Resize([256, 256]),<br \/>\n    transforms.ToTensor(),<br \/>\n    transforms.Normalize(mean&#061;[0.485, 0.456, 0.406],<br \/>\n                         std&#061;[0.229, 0.224, 0.225])<br \/>\n])<\/p>\n<p>class food_dataset(Dataset):<br \/>\n    def __init__(self, file_path, transform&#061;None):<br \/>\n        self.imgs &#061; []<br \/>\n        self.labels &#061; []<br \/>\n        self.transform &#061; transform<br \/>\n        with open(file_path) as f:<br \/>\n            for line in f:<br \/>\n                img_path, label &#061; line.strip().split(&#039; &#039;)<br \/>\n                self.imgs.append(img_path)<br \/>\n                self.labels.append(label)<\/p>\n<p>    def __len__(self):<br \/>\n        return len(self.imgs)<\/p>\n<p>    def __getitem__(self, idx):<br \/>\n        image &#061; Image.open(self.imgs[idx])<br \/>\n        if self.transform:<br \/>\n            image &#061; self.transform(image)<br \/>\n        label &#061; torch.from_numpy(np.array(self.labels[idx], dtype&#061;np.int64))<br \/>\n        return image, label<\/p>\n<p>test_data &#061; food_dataset(&#039;.\/test.txt&#039;, transform&#061;data_transforms)<br \/>\ntest_loader &#061; DataLoader(test_data, batch_size&#061;1, shuffle&#061;True)<\/p>\n<p># 5. \u63a8\u7406<br \/>\nresults &#061; []<br \/>\nlabels &#061; []<br \/>\nwith torch.no_grad():<br \/>\n    for x, y in test_loader:<br \/>\n        x, y &#061; x.to(device), y.to(device)<br \/>\n        pred &#061; model(x)<br \/>\n        results.append(pred.argmax(1).item())<br \/>\n        labels.append(y.item())<\/p>\n<p># 6. \u8f93\u51fa\u7ed3\u679c<br \/>\nprint(&#034;\u9884\u6d4b\u503c&#xff1a;&#034;, results)<br \/>\nprint(&#034;\u771f\u5b9e\u503c&#xff1a;&#034;, labels)<\/p>\n<h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<p>\u672c\u7bc7\u535a\u5ba2\u56f4\u7ed5\u201c\u6a21\u578b\u52a0\u8f7d\u4e0e\u63a8\u7406\u201d\u8fd9\u4e00\u4e3b\u9898&#xff0c;\u7cfb\u7edf\u8bb2\u89e3\u4e86&#xff1a;<\/p>\n<table>\n<tr>\u77e5\u8bc6\u70b9\u6838\u5fc3\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>state_dict\u52a0\u8f7d<\/td>\n<td>\u5148\u5b9a\u4e49\u6a21\u578b\u7ed3\u6784&#xff0c;\u518d\u52a0\u8f7d\u53c2\u6570<\/td>\n<\/tr>\n<tr>\n<td>TorchScript\u52a0\u8f7d<\/td>\n<td>\u76f4\u63a5\u52a0\u8f7d\u5b8c\u6574\u6a21\u578b&#xff0c;\u65e0\u9700\u5b9a\u4e49\u7ed3\u6784<\/td>\n<\/tr>\n<tr>\n<td>\u6a21\u578b\u8bc4\u4f30\u6a21\u5f0f<\/td>\n<td>model.eval() \u56fa\u5b9a\u53c2\u6570<\/td>\n<\/tr>\n<tr>\n<td>\u63a8\u7406\u4e0a\u4e0b\u6587<\/td>\n<td>torch.no_grad() \u5173\u95ed\u68af\u5ea6\u8ba1\u7b97<\/td>\n<\/tr>\n<tr>\n<td>\u9884\u6d4b\u7c7b\u522b<\/td>\n<td>pred.argmax(1) \u53d6\u6700\u5927\u5f97\u5206\u7d22\u5f15<\/td>\n<\/tr>\n<tr>\n<td>\u7ed3\u679c\u5bf9\u6bd4<\/td>\n<td>\u9884\u6d4b\u503c\u4e0e\u771f\u5b9e\u503c\u9010\u6761\u6bd4\u8f83<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5f15\u8a00&#xff1a;\u5982\u4f55\u52a0\u8f7d\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u5728\u524d\u51e0\u7bc7\u535a\u5ba2\u4e2d&#xff0c;\u6211\u4eec\u4ece\u96f6\u6784\u5efa\u4e86CNN\u6a21\u578b&#xff0c;\u7528\u6570\u636e\u589e\u5f3a\u63d0\u5347\u4e86\u6cdb\u5316\u80fd\u529b&#xff0c;\u5e76\u5b66\u4f1a\u4e86\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u4fdd\u5b58\u6700\u4f18\u6a21\u578b\u3002\u73b0\u5728&#xff0c;\u6211\u4eec\u624b\u91cc\u5df2\u7ecf\u6709\u4e86\u4e24\u4e2a\u6a21\u578b\u6587\u4ef6&#xff1a;best2026-910.pth&#xff1a;\u4fdd\u5b58\u7684\u6a21\u578b\u53c2\u6570&#xff08;state_dict&#xff09;best910.pth&#xff1a;\u4fdd\u5b58\u7684\u5b8c\u6574TorchScript\u6a21\u578b\u4f46\u95ee\u9898\u6765\u4e86&#xff1a;\u8bad\u7ec3\u597d\u7684\u6a21\u578b&amp;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2394,50,86],"topic":[],"class_list":["post-106468","post","type-post","status-publish","format-standard","hentry","category-server","tag-cnn","tag-50","tag-86"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u521d\u8bc6\u6df1\u5ea6\u5b66\u4e60\u2014\u2014\u6a21\u578b\u52a0\u8f7d\u4e0e\u63a8\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/106468.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u521d\u8bc6\u6df1\u5ea6\u5b66\u4e60\u2014\u2014\u6a21\u578b\u52a0\u8f7d\u4e0e\u63a8\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u5f15\u8a00&#xff1a;\u5982\u4f55\u52a0\u8f7d\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u5728\u524d\u51e0\u7bc7\u535a\u5ba2\u4e2d&#xff0c;\u6211\u4eec\u4ece\u96f6\u6784\u5efa\u4e86CNN\u6a21\u578b&#xff0c;\u7528\u6570\u636e\u589e\u5f3a\u63d0\u5347\u4e86\u6cdb\u5316\u80fd\u529b&#xff0c;\u5e76\u5b66\u4f1a\u4e86\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u4fdd\u5b58\u6700\u4f18\u6a21\u578b\u3002\u73b0\u5728&#xff0c;\u6211\u4eec\u624b\u91cc\u5df2\u7ecf\u6709\u4e86\u4e24\u4e2a\u6a21\u578b\u6587\u4ef6&#xff1a;best2026-910.pth&#xff1a;\u4fdd\u5b58\u7684\u6a21\u578b\u53c2\u6570&#xff08;state_dict&#xff09;best910.pth&#xff1a;\u4fdd\u5b58\u7684\u5b8c\u6574TorchScript\u6a21\u578b\u4f46\u95ee\u9898\u6765\u4e86&#xff1a;\u8bad\u7ec3\u597d\u7684\u6a21\u578b&amp;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/106468.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-17T06:08:33+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/106468.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/106468.html\",\"name\":\"\u521d\u8bc6\u6df1\u5ea6\u5b66\u4e60\u2014\u2014\u6a21\u578b\u52a0\u8f7d\u4e0e\u63a8\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-17T06:08:33+00:00\",\"dateModified\":\"2026-09-17T06:08:33+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/106468.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/106468.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/106468.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u521d\u8bc6\u6df1\u5ea6\u5b66\u4e60\u2014\u2014\u6a21\u578b\u52a0\u8f7d\u4e0e\u63a8\u7406\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u521d\u8bc6\u6df1\u5ea6\u5b66\u4e60\u2014\u2014\u6a21\u578b\u52a0\u8f7d\u4e0e\u63a8\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/106468.html","og_locale":"zh_CN","og_type":"article","og_title":"\u521d\u8bc6\u6df1\u5ea6\u5b66\u4e60\u2014\u2014\u6a21\u578b\u52a0\u8f7d\u4e0e\u63a8\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u5f15\u8a00&#xff1a;\u5982\u4f55\u52a0\u8f7d\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u5728\u524d\u51e0\u7bc7\u535a\u5ba2\u4e2d&#xff0c;\u6211\u4eec\u4ece\u96f6\u6784\u5efa\u4e86CNN\u6a21\u578b&#xff0c;\u7528\u6570\u636e\u589e\u5f3a\u63d0\u5347\u4e86\u6cdb\u5316\u80fd\u529b&#xff0c;\u5e76\u5b66\u4f1a\u4e86\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u4fdd\u5b58\u6700\u4f18\u6a21\u578b\u3002\u73b0\u5728&#xff0c;\u6211\u4eec\u624b\u91cc\u5df2\u7ecf\u6709\u4e86\u4e24\u4e2a\u6a21\u578b\u6587\u4ef6&#xff1a;best2026-910.pth&#xff1a;\u4fdd\u5b58\u7684\u6a21\u578b\u53c2\u6570&#xff08;state_dict&#xff09;best910.pth&#xff1a;\u4fdd\u5b58\u7684\u5b8c\u6574TorchScript\u6a21\u578b\u4f46\u95ee\u9898\u6765\u4e86&#xff1a;\u8bad\u7ec3\u597d\u7684\u6a21\u578b&amp;","og_url":"https:\/\/www.wsisp.com\/helps\/106468.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-17T06:08:33+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/106468.html","url":"https:\/\/www.wsisp.com\/helps\/106468.html","name":"\u521d\u8bc6\u6df1\u5ea6\u5b66\u4e60\u2014\u2014\u6a21\u578b\u52a0\u8f7d\u4e0e\u63a8\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-17T06:08:33+00:00","dateModified":"2026-09-17T06:08:33+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/106468.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/106468.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/106468.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u521d\u8bc6\u6df1\u5ea6\u5b66\u4e60\u2014\u2014\u6a21\u578b\u52a0\u8f7d\u4e0e\u63a8\u7406"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/106468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=106468"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/106468\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=106468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=106468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=106468"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=106468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}