{"id":2169,"date":"2024-12-24T15:59:33","date_gmt":"2024-12-24T07:59:33","guid":{"rendered":"http:\/\/blog.xtaa.cn\/?p=2169"},"modified":"2024-12-24T15:59:33","modified_gmt":"2024-12-24T07:59:33","slug":"%e9%9b%b6%e5%9f%ba%e7%a1%80ai%e5%85%a5%e9%97%a8%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"http:\/\/blog.xtaa.cn\/index.php\/2024\/12\/24\/%e9%9b%b6%e5%9f%ba%e7%a1%80ai%e5%85%a5%e9%97%a8%e6%8c%87%e5%8d%97\/","title":{"rendered":"\u96f6\u57fa\u7840AI\u5165\u95e8\u6307\u5357"},"content":{"rendered":"\n<p>\u672c\u6587\u4ee5\u5de5\u7a0b\u5e08\u7684\u89c6\u89d2\u4ece\u96f6\u5f00\u59cb\u642d\u5efa\u5e76\u8fd0\u884c\u4e00\u4e2aAI\u5c0f\u6a21\u578b\uff0c\u5e76\u628a\u5b83\u5b8c\u5168\u8fd0\u884c\u8d77\u6765\u4ee5\u7406\u89e3AI\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u975e\u5e38\u63a5\u5730\u6c14\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">AI\u6a21\u578b\u662f\u5982\u4f55\u5de5\u4f5c\u7684<\/h3>\n\n\n\n<p>\u795e\u7ecf\u7f51\u7edc\u662fAI\u7684\u4e00\u79cd\u91cd\u8981\u7684\u8ba1\u7b97\u6a21\u578b\uff0c\u6df1\u5ea6\u5b66\u4e60\u662f\u901a\u8fc7\u795e\u7ecf\u7f51\u7edc\u5b9e\u73b0\u7279\u5f81\u5b66\u4e60\u548c\u6a21\u5f0f\u5206\u6790\uff0c\u5927\u91cf\u7528\u4e8e\u56fe\u50cf\u8bc6\u522b\u7b49\u9886\u57df\u3002\u6211\u4eec\u4ee5\u6700\u57fa\u7840\u7684\u624b\u5199\u6570\u5b57\u8bc6\u522b\u4e3a\u4f8b\uff0c\u770b\u770b\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\u7684AI\u6a21\u578b\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002<\/p>\n\n\n\n<p>MNIST\uff08Modified National Institute of Stands and Technology\uff09\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u6570\u636e\u96c6\uff0c\u5b83\u5305\u542b\u4e866\u4e07\u4e2a\u624b\u5199\u7684\u6570\u5b57\u56fe\u50cf\uff0c\u6bcf\u4e2a\u56fe\u50cf\u90fd\u662f28&#215;28\u9ed1\u5e95\u767d\u5b57\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/liaoxuefeng.com\/blogs\/all\/2023-05-08-mnist\/mnist.png\" alt=\"mnist-preview\"\/><\/figure>\n\n\n\n<p>\u6709\u4e86\u8fd9\u4e2a\u5f00\u6e90\u7684\u6570\u636e\u96c6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8bad\u7ec3\u4e00\u4e2a\u8bc6\u522b\u624b\u5199\u6570\u5b57\u7684AI\u6a21\u578b\uff0c\u8fd9\u4e2a\u7ec3\u4e60\u582a\u79f0AI\u754c\u7684\u201cHello, world\u201d\u3002<\/p>\n\n\n\n<p>\u8981\u7f16\u5199\u8fd9\u4e2aAI\u6a21\u578b\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u79cd\u79f0\u4e3a\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\uff08CNN\uff1aConvolutional Neural Network\uff09\u7684\u795e\u7ecf\u7f51\u7edc\u7ed3\u6784\uff0c\u5177\u4f53\u5230\u4ee3\u7801\u5c42\u9762\uff0c\u5219\u9700\u8981\u4f7f\u7528<a href=\"https:\/\/pytorch.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">PyTorch<\/a>\u8fd9\u6837\u7684\u8bad\u7ec3\u6846\u67b6\u3002PyTorch\u5e95\u5c42\u7528C++\u5f00\u53d1\uff0c\u5916\u5c42\u7528Python\u8c03\u7528\uff0c\u975e\u5e38\u65b9\u4fbf\u6613\u7528\u3002\u5148\u786e\u4fdd\u673a\u5668\u5b89\u88c5\u4e86Python3\uff0c\u7136\u540e\uff0c\u5b89\u88c5PyTorch 2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install torch torchvision torchaudio\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u672c\u673a\u6709CUDA\u73af\u5883\uff0c\u4e5f\u53ef\u4ee5\u5b89\u88c5GPU\u7248\u672c\uff0c\u8bad\u7ec3\u901f\u5ea6\u66f4\u5feb\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7f16\u5199\u6a21\u578b<\/h3>\n\n\n\n<p>\u51c6\u5907\u597d\u73af\u5883\u540e\uff0c\u6211\u4eec\u5f00\u59cb\u7f16\u5199\u6a21\u578b\u3002\u5148\u8ba9AI\u5199\u4e00\u4e2a\u7528CNN\u8bc6\u522bMNIST\u6570\u636e\u96c6\u7684PyTorch\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch.nn as nn\n\nclass NeuralNetwork(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = nn.Conv2d(1, 32, 3, 1)\n        self.conv2 = nn.Conv2d(32, 64, 3, 1)\n        self.fc1 = nn.Linear(in_features=64 * 5 * 5, out_features=128)\n        self.fc2 = nn.Linear(in_features=128, out_features=10)\n\n    def forward(self, x):\n        x = nn.functional.relu(self.conv1(x))\n        x = nn.functional.max_pool2d(x, kernel_size=2)\n        x = nn.functional.relu(self.conv2(x))\n        x = nn.functional.max_pool2d(x, kernel_size=2)\n        x = x.view(-1, 64 * 5 * 5)\n        x = nn.functional.relu(self.fc1(x))\n        x = self.fc2(x)\n        return x\n<\/code><\/pre>\n\n\n\n<p>\u770b\u4e0d\u61c2\u4e0d\u8981\u7d27\uff0c\u53ef\u4ee5\u63a5\u7740\u95eeAI\uff0c\u5b83\u4f1a\u544a\u8bc9\u6211\u4eec\uff0c\u8fd9\u4e2a\u795e\u7ecf\u7f51\u7edc\u5b9a\u4e49\u4e86\u4e24\u4e2aCNN\u5377\u79ef\u5c42\u548c\u4e24\u4e2a\u5168\u8fde\u63a5\u5c42\uff0c\u603b\u7684\u6765\u8bf4\u5c31\u662f\uff0c\u8fd9\u4e2a\u6a21\u578b\u5b9a\u4e49\u4e862\u5c42\u5377\u79ef\u7f51\u7edc\u52a02\u5c42\u5168\u8fde\u63a5\u5c42\uff0c\u8f93\u5165\u4e3a1\u901a\u9053\u56fe\u7247\uff0c\u7ecf\u8fc7\u5377\u79ef\u548c\u6c60\u5316\u540e\u8fdb\u5165\u5168\u8fde\u63a5\u5c42\uff0c\u6700\u540e\u8f93\u51fa10\u4e2a\u5206\u7c7b\u7ed3\u679c\uff0c\u5206\u522b\u4ee3\u88680\uff5e9\u8fd910\u4e2a\u6570\u5b57\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8bad\u7ec3<\/h3>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u8981\u4f7f\u7528MNIST\u6570\u636e\u96c6\u6765\u8bad\u7ec3\u8fd9\u4e2a\u6a21\u578b\u3002\u53d7\u76ca\u4e8ePyTorch\u8fd9\u4e2a\u6846\u67b6\uff0c\u6211\u4eec\u8fde\u4e0b\u8f7d\u548c\u8bfb\u53d6\u6570\u636e\u96c6\u90fd\u7701\u4e86\uff0c\u56e0\u4e3aPyTorch\u5df2\u7ecf\u96c6\u6210\u4e86\u8fd9\u4e2a\u6570\u636e\u96c6\uff0c\u76f4\u63a5\u4e0b\u8f7d\u3001\u52a0\u8f7d\u3001\u8bad\u7ec3\uff0c\u4e00\u6b65\u5230\u4f4d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from time import time\n\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\n\nfrom torchvision import datasets\nfrom torch.utils.data import DataLoader\nfrom torchvision.transforms import ToTensor\n\nfrom model import NeuralNetwork\n\ndef train(dataloader, device, model, loss_fn, optimizer):\n    model.train()\n    running_loss = 0.0\n    for batch, (inputs, labels) in enumerate(dataloader):\n        inputs = inputs.to(device)\n        labels = labels.to(device)\n        optimizer.zero_grad()\n        outputs = model(inputs)\n        loss = loss_fn(outputs, labels)\n        loss.backward()\n        optimizer.step()\n        running_loss += loss.item()\n    print(f'loss: {running_loss\/len(dataloader):&gt;0.3f}')\n\ndef test(dataloader, device, model):\n    model.eval()\n    correct = 0\n    total = 0\n    with torch.no_grad():\n        for inputs, labels in dataloader:\n            inputs = inputs.to(device)\n            labels = labels.to(device)\n            outputs = model(inputs)\n            _, predicted = torch.max(outputs.data, 1)\n            total += labels.size(0)\n            correct += (predicted == labels).sum().item()\n    print(f'accuracy: {100.0*correct\/total:&gt;0.2f} %')\n\ndef main():\n    print('loading training data...')\n    train_data = datasets.MNIST(\n        root='.\/data', train=True, download=True, transform=ToTensor())\n    print('loading test data...')\n    test_data = datasets.MNIST(\n        root='.\/data', train=False, download=True, transform=ToTensor())\n\n    train_dataloader = DataLoader(train_data, batch_size=64)\n    test_dataloader = DataLoader(test_data, batch_size=64)\n\n    device = 'cuda' if torch.cuda.is_available() else 'cpu'\n    print(f'using {device}')\n    model = NeuralNetwork().to(device)\n    print(model)\n\n    loss_fn = nn.CrossEntropyLoss()\n    optimizer = optim.Adam(model.parameters(), lr=0.001)\n    epochs = 5\n    for t in range(epochs):\n        start_time = time()\n        print(f'epoch {t+1} \/ {epochs}\\n--------------------')\n        train(train_dataloader, device, model, loss_fn, optimizer)\n        test(test_dataloader, device, model)\n        end_time = time()\n        print(f'time: {end_time-start_time:&gt;0.2f} seconds')\n    print('done!')\n    path = 'mnist.pth'\n    torch.save(model.state_dict(), path)\n    print(f'model saved: {path}')\n\nif __name__ == '__main__':\n    main()\n<\/code><\/pre>\n\n\n\n<p>\u6570\u636e\u96c6\u5206\u4e24\u90e8\u5206\uff1a\u4e00\u4e2a\u7528\u4e8e\u8bad\u7ec3\uff0c\u4e00\u4e2a\u7528\u4e8e\u6d4b\u8bd5\u8bad\u7ec3\u6548\u679c\uff0c\u7528PyTorch\u7684<code>datasets.MNIST()<\/code>\u81ea\u52a8\u4e0b\u8f7d\u3001\u89e3\u538b\u5e76\u52a0\u8f7d\u6570\u636e\u96c6\uff08\u89e3\u538b\u540e\u7ea655M\u6570\u636e\uff0c\u4ec5\u7b2c\u4e00\u6b21\u9700\u8981\u4e0b\u8f7d\uff09\u3002\u7136\u540e\uff0c\u5b9a\u4e49\u635f\u5931\u51fd\u6570\u548c\u4f18\u5316\u5668\uff0c\u7528<code>train()<\/code>\u505a\u8bad\u7ec3\uff0c\u7528<code>test()<\/code>\u6d4b\u8bd5\u8bad\u7ec3\u6548\u679c\uff0c\u8bad\u7ec35\u6b21\uff0c\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python3 train.py\nloading training data...\nDownloading http:\/\/yann.lecun.com\/exdb\/mnist\/train-images-idx3-ubyte.gz\n...\u7b2c\u4e00\u6b21\u8fd0\u884c\u4f1a\u81ea\u52a8\u4e0b\u8f7d\u6570\u636e\u5230data\u76ee\u5f55\u5e76\u89e3\u538b...\n\nloading test data...\nusing cpu\nNeuralNetwork(\n  (conv1): Conv2d(1, 32, kernel_size=(3, 3), stride=(1, 1))\n  (conv2): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1))\n  (fc1): Linear(in_features=1600, out_features=128, bias=True)\n  (fc2): Linear(in_features=128, out_features=10, bias=True)\n)\nepoch 1 \/ 5\n--------------------\nloss: 0.177\naccuracy: 97.21 %\ntime: 30.96 seconds\nepoch 2 \/ 5\n--------------------\nloss: 0.053\naccuracy: 98.62 %\ntime: 32.24 seconds\nepoch 3 \/ 5\n--------------------\nloss: 0.035\naccuracy: 98.70 %\ntime: 33.70 seconds\nepoch 4 \/ 5\n--------------------\nloss: 0.025\naccuracy: 98.90 %\ntime: 35.10 seconds\nepoch 5 \/ 5\n--------------------\nloss: 0.018\naccuracy: 98.95 %\ntime: 32.02 seconds\ndone!\nmodel saved: mnist.pth\n<\/code><\/pre>\n\n\n\n<p>\u7ecf\u8fc75\u8f6e\u8bad\u7ec3\uff0c\u6bcf\u8f6e\u8017\u65f6\u7ea630\u79d2\uff08\u8fd9\u91cc\u7528CPU\u8bad\u7ec3\uff0c\u5982\u679c\u662fGPU\u5219\u53ef\u4ee5\u5927\u5927\u63d0\u901f\uff09\uff0c\u51c6\u786e\u7387\u53ef\u4ee5\u8fbe\u523099%\u3002\u8bad\u7ec3\u7ed3\u675f\u540e\uff0c\u5c06\u6a21\u578b\u4fdd\u5b58\u81f3<code>mnist.pth<\/code>\u6587\u4ef6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u6a21\u578b<\/h3>\n\n\n\n<p>\u6709\u4e86\u9884\u8bad\u7ec3\u7684\u6a21\u578b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7528\u5b9e\u9645\u7684\u624b\u5199\u56fe\u7247\u6d4b\u8bd5\u4e00\u4e0b\u3002\u7528PS\u624b\u7ed8\u51e0\u5f20\u624b\u5199\u6570\u5b57\u56fe\u7247\uff0c\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch\nfrom torchvision import transforms\n\nfrom PIL import Image, ImageOps\nfrom model import NeuralNetwork\n\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'\nprint(f'using {device}')\nmodel = NeuralNetwork().to(device)\npath = '.\/mnist.pth'\nmodel.load_state_dict(torch.load(path))\nprint(f'loaded model from {path}')\nprint(model)\n\ndef test(path):\n    print(f'test {path}...')\n    image = Image.open(path).convert('RGB').resize((28, 28))\n    image = ImageOps.invert(image)\n\n    trans = transforms.Compose(&#91;\n        transforms.Grayscale(1),\n        transforms.ToTensor()\n    ])\n    image_tensor = trans(image).unsqueeze(0).to(device)\n    model.eval()\n    with torch.no_grad():\n        output = model(image_tensor)\n        probs = torch.nn.functional.softmax(output&#91;0], 0)\n    predict = torch.argmax(probs).item()\n    return predict, probs&#91;predict], probs\n\ndef main():\n    for i in range(10):\n        predict, prob, probs = test(f'.\/input\/test-{i}.png')\n        print(f'expected {i}, actual {predict}, {prob}, {probs}')\n\n\nif __name__ == '__main__':\n    main()\n<\/code><\/pre>\n\n\n\n<p>\u56e0\u4e3a\u8bad\u7ec3\u65f6\u8f93\u5165\u7684\u56fe\u7247\u662f\u9ed1\u5e95\u767d\u5b57\uff0c\u800c\u6d4b\u8bd5\u56fe\u7247\u662f\u767d\u5e95\u9ed1\u5b57\uff0c\u6240\u4ee5\u5148\u7528PIL\u628a\u56fe\u7247\u5904\u7406\u621028&#215;28\u7684\u9ed1\u5e95\u767d\u5b57\uff0c\u518d\u6d4b\u8bd5\uff0c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python3 test.py \nusing cpu\nloaded model from .\/mnist.pth\nNeuralNetwork(\n  (conv1): Conv2d(1, 32, kernel_size=(3, 3), stride=(1, 1))\n  (conv2): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1))\n  (fc1): Linear(in_features=1600, out_features=128, bias=True)\n  (fc2): Linear(in_features=128, out_features=10, bias=True)\n)\ntest .\/input\/test-0.png...\nexpected 0, actual 0, 0.9999996423721313, tensor(&#91;1.0000e+00, 2.3184e-10, 1.7075e-08, 7.6250e-16, 1.2966e-12, 5.7179e-11,\n        2.1766e-07, 1.8820e-12, 1.1260e-07, 2.2463e-09])\n...\n<\/code><\/pre>\n\n\n\n<p>\u4ee5\u56fe\u7247<code>0<\/code>\u4e3a\u4f8b\uff0c\u6211\u4eec\u8981\u4f7f\u7528\u6a21\u578b\uff0c\u9700\u8981\u628a\u8f93\u5165\u56fe\u7247\u53d8\u6210\u6a21\u578b\u53ef\u63a5\u53d7\u7684\u53c2\u6570\uff0c\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2aTensor\uff08\u5f20\u91cf\uff09\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u4efb\u610f\u7ef4\u5ea6\u7684\u6570\u7ec4\uff0c\u800c\u6a21\u578b\u7684\u8f93\u51fa\u4e5f\u662f\u4e00\u4e2aTensor\uff0c\u5b83\u662f\u4e00\u4e2a\u5305\u542b10\u4e2a\u5143\u7d20\u76841\u7ef4\u6570\u7ec4\uff0c\u5206\u522b\u8868\u793a\u6bcf\u4e2a\u8f93\u51fa\u7684\u6982\u7387\u3002\u5bf9\u56fe\u7247<code>0<\/code>\u7684\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1.0000e+00<\/li>\n\n\n\n<li>2.3184e-10<\/li>\n\n\n\n<li>1.7075e-08<\/li>\n\n\n\n<li>7.6250e-16<\/li>\n\n\n\n<li>1.2966e-12<\/li>\n\n\n\n<li>5.7179e-11<\/li>\n\n\n\n<li>2.1766e-07<\/li>\n\n\n\n<li>1.8820e-12<\/li>\n\n\n\n<li>1.1260e-07<\/li>\n\n\n\n<li>2.2463e-09<\/li>\n<\/ul>\n\n\n\n<p>\u9664\u4e86\u7b2c\u4e00\u4e2a\u8f93\u51fa\u4e3a1\uff0c\u5176\u4ed6\u8f93\u51fa\u90fd\u975e\u5e38\u63a5\u8fd1\u4e8e0\uff0c\u53ef\u89c1\u6a21\u578b\u4ee599.99996423721313%\u7684\u6982\u7387\u8ba4\u4e3a\u56fe\u7247\u662f<code>0<\/code>\uff0c\u662f\u5176\u4ed6\u6570\u5b57\u7684\u6982\u7387\u4f4e\u5230\u63a5\u8fd1\u4e8e0\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u8fd9\u4e2aMNIST\u6a21\u578b\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u56fe\u7247\u5206\u7c7b\u5668\uff0c\u6216\u8005\u8bf4\u9884\u6d4b\u5668\uff0c\u5b83\u9488\u5bf9\u4efb\u610f\u56fe\u7247\u8f93\u5165\uff0c\u90fd\u4f1a\u4ee5\u6982\u7387\u5f62\u5f0f\u7ed9\u51fa10\u4e2a\u9884\u6d4b\uff0c\u6211\u4eec\u627e\u51fa\u63a5\u8fd1\u4e8e1\u7684\u8f93\u51fa\uff0c\u5c31\u662f\u5206\u7c7b\u5668\u7ed9\u51fa\u7684\u9884\u6d4b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ea7\u54c1\u5316<\/h3>\n\n\n\n<p>\u867d\u7136\u6211\u4eec\u5df2\u7ecf\u6709\u4e86\u9884\u8bad\u7ec3\u6a21\u578b\uff0c\u4e5f\u53ef\u4ee5\u7528\u6a21\u578b\u8fdb\u884c\u624b\u5199\u6570\u5b57\u8bc6\u522b\uff0c\u4f46\u662f\uff0c\u8981\u8ba9\u7528\u6237\u80fd\u65b9\u4fbf\u5730\u4f7f\u7528\u8fd9\u4e2a\u6a21\u578b\uff0c\u8fd8\u9700\u8981\u8fdb\u4e00\u6b65\u4f18\u5316\uff0c\u81f3\u5c11\u9700\u8981\u63d0\u4f9b\u4e00\u4e2aUI\u3002\u6211\u4eec\u8ba9AI\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u9875\u9762\uff0c\u5141\u8bb8\u7528\u6237\u5728\u9875\u9762\u7528\u9f20\u6807\u624b\u5199\u6570\u5b57\uff0c\u7136\u540e\uff0c\u901a\u8fc7API\u83b7\u5f97\u8bc6\u522b\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/liaoxuefeng.com\/blogs\/all\/2023-05-08-mnist\/ui.png\" alt=\"mnist-ui\"\/><\/figure>\n\n\n\n<p>\u56e0\u6b64\uff0c\u6700\u540e\u4e00\u6b65\u662f\u628a\u6a21\u578b\u7684\u8f93\u5165\u8f93\u51fa\u7528API\u5c01\u88c5\u4e00\u4e0b\u3002\u56e0\u4e3a\u6a21\u578b\u57fa\u4e8ePyTorch\uff0c\u6240\u4ee5\u4f7f\u7528Python\u7684Flask\u6846\u67b6\u662f\u6700\u7b80\u5355\u7684\u3002API\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import base64\nimport torch\nfrom io import BytesIO\nfrom PIL import Image\nfrom flask import Flask, request, redirect, jsonify\nfrom torchvision import transforms\nfrom model import NeuralNetwork\n\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'\nprint(f'using {device}')\nmodel = NeuralNetwork().to(device)\npath = '.\/mnist.pth'\nmodel.load_state_dict(torch.load(path))\nprint(f'loaded model from {path}')\nprint(model)\nparams = model.state_dict()\nprint(params)\n\napp = Flask(__name__)\n\n@app.route('\/')\ndef index():\n    return redirect('\/static\/index.html')\n\n@app.route('\/api', methods=&#91;'POST'])\ndef api():\n    data = request.get_json()\n    image_data = base64.b64decode(data&#91;'image'])\n    image = Image.open(BytesIO(image_data))\n    trans = transforms.Compose(&#91;\n        transforms.Grayscale(1),\n        transforms.ToTensor()\n    ])\n    image_tensor = trans(image).unsqueeze(0).to(device)\n    model.eval()\n    with torch.no_grad():\n        output = model(image_tensor)\n        probs = torch.nn.functional.softmax(output&#91;0], 0)\n    predict = torch.argmax(probs).item()\n    prob = probs&#91;predict]\n    print(f'predict: {predict}, prob: {prob}, probs: {probs}')\n    return jsonify({\n        'result': predict,\n        'probability': prob.item()\n    })\n\nif __name__ == '__main__':\n    app.run(port=5000)\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684API\u670d\u52a1\uff0c\u6ce8\u610f\u5c1a\u672a\u5bf9\u5e76\u53d1\u8bbf\u95ee\u505a\u5904\u7406\uff0c\u6240\u4ee5\u53ea\u80fd\u7b97\u4e00\u4e2a\u53ef\u7528\u7684DEMO\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u601d\u8003<\/h3>\n\n\n\n<p>\u5bf9\u4e8eAI\u7a0b\u5e8f\uff0c\u6211\u4eec\u53d1\u73b0\uff0c\u6a21\u578b\u5b9a\u4e49\u975e\u5e38\u7b80\u5355\uff0c\u4e00\u5171\u4e5f\u5c3120\u884c\u4ee3\u7801\u3002\u8bad\u7ec3\u4ee3\u7801\u4e5f\u5f88\u5c11\uff0c\u4e0d\u8d85\u8fc7100\u884c\u3002\u5b83\u548c\u4f20\u7edf\u7684\u7a0b\u5e8f\u6700\u5927\u7684\u533a\u522b\u5728\u54ea\u5462\uff1f<\/p>\n\n\n\n<p>\u65e0\u8bba\u662f\u4f20\u7edf\u7684\u7a0b\u5e8f\uff0c\u8fd8\u662fAI\u7a0b\u5e8f\uff0c\u5728\u8ba1\u7b97\u673a\u770b\u6765\uff0c\u672c\u8d28\u4e0a\u662f\u4e00\u6837\u7684\uff0c\u5373\u7ed9\u5b9a\u4e00\u4e2a\u8f93\u5165\uff0c\u901a\u8fc7\u4e00\u4e2a\u51fd\u6570\u8ba1\u7b97\uff0c\u83b7\u5f97\u8f93\u51fa\u3002\u4e0d\u540c\u70b9\u5728\u4e8e\uff0c\u5bf9\u4e8e\u4f20\u7edf\u7a0b\u5e8f\uff0c\u8f93\u5165\u662f\u975e\u5e38\u7b80\u5355\u7684\uff0c\u4f8b\u5982\u7528\u6237\u6ce8\u518c\uff0c\u4ec5\u4ec5\u9700\u8981\u51e0\u4e2a\u5b57\u6bb5\uff0c\u800c\u5904\u7406\u51fd\u6570\u5c11\u5219\u51e0\u5343\u884c\uff0c\u591a\u5219\u51e0\u5341\u4e07\u884c\u3002\u867d\u7136\u4ee3\u7801\u91cf\u5f88\u5927\uff0c\u4f46\u6267\u884c\u8def\u5f84\u5374\u975e\u5e38\u6e05\u6670\uff0c\u901a\u8fc7\u8ddf\u8e2a\u6267\u884c\uff0c\u80fd\u8f7b\u6613\u83b7\u5f97\u4e00\u4e2a\u786e\u5b9a\u7684\u6267\u884c\u8def\u5f84\uff0c\u4ece\u800c\u6700\u7ec8\u83b7\u5f97\u4e00\u4e2a\u786e\u5b9a\u6027\u7684\u7ed3\u679c\u3002\u786e\u5b9a\u6027\u5c31\u662f\u4f20\u7edf\u7a0b\u5e8f\u7684\u7279\u70b9\uff0c\u6216\u8005\u8bf4\uff0c\u4f20\u7edf\u7a0b\u5e8f\u7684\u4ee3\u7801\u91cf\u867d\u7136\u5927\uff0c\u4f46\u8f93\u5165\u7b80\u5355\uff0c\u8def\u5f84\u6e05\u6670\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>f(x1, x2, x3)\n  \u2502\n  \u25bc\n \u250c\u2500\u2510\u25c0\u2500\u2510\n \u2514\u2500\u2518  \u2502\n  \u2502   \u2502\n  \u25bc   \u2502\n \u250c\u2500\u2510  \u2502\n \u2514\u2500\u2518  \u2502\n  \u2502   \u2502\n  \u25bc   \u2502\n \u250c\u2500\u2510\u2500\u2500\u2518\n \u2514\u2500\u2518\n  \u2502\n  \u25bc\n \u250c\u2500\u2510\n \u2514\u2500\u2518\n<\/code><\/pre>\n\n\n\n<p>AI\u7a0b\u5e8f\u5219\u4e0d\u540c\uff0c\u5b83\u53ea\u7ecf\u8fc7\u51e0\u5c42\u8ba1\u7b97\uff0c\u590d\u6742\u7684\u5927\u6a21\u578b\u4e5f\u5c31100\u6765\u5c42\uff0c\u5c31\u53ef\u4ee5\u8f93\u51fa\u7ed3\u679c\u3002\u4f46\u662f\uff0c\u5b83\u7684\u8f93\u5165\u6570\u636e\u91cf\u5927\uff0c\u6bcf\u4e00\u5c42\u7684\u6570\u636e\u91cf\u66f4\u5927\uff0c\u5c31\u50cf\u4e00\u4e2a\u6241\u5e73\u7684\u5de8\u5927\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>       f(x1, x2, x3, ... , x998, x999, x1000)\n         \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502\n         \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc\n        \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510\n        \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518\n         \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502\n \u250c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2510\n \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc   \u25bc\n\u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510\n\u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518\n \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502   \u2502\n \u2514\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2518\n                 \u25bc   \u25bc   \u25bc   \u25bc   \u25bc\n                \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510 \u250c\u2500\u2510\n                \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518 \u2514\u2500\u2518\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u7684\u8ba1\u7b97\u5e76\u4e0d\u590d\u6742\uff0c\u6bcf\u4e00\u5c42\u90fd\u662f\u7b80\u5355\u7684\u77e9\u9635\u8ba1\u7b97\uff0c\u4f46\u5e76\u884c\u7a0b\u5ea6\u5f88\u9ad8\uff0c\u6240\u4ee5\u9700\u8981\u7528GPU\u52a0\u901f\u3002\u590d\u6742\u5ea6\u5728\u4e8e\u6bcf\u4e00\u5c42\u90fd\u6709\u5927\u91cf\u7684\u53c2\u6570\uff0c\u8fd9\u4e9b\u53c2\u6570\u4e0d\u662f\u5f00\u53d1\u8005\u5199\u6b7b\u7684\uff0c\u800c\u662f\u901a\u8fc7\u8bad\u7ec3\u786e\u5b9a\u7684\uff0c\u6bcf\u6b21\u5bf9\u53c2\u6570\u8fdb\u884c\u5fae\u8c03\uff0c\u7136\u540e\u6839\u636e\u6548\u679c\u662f\u53d8\u5f97\u66f4\u597d\u8fd8\u662f\u66f4\u574f\u51b3\u5b9a\u5fae\u8c03\u65b9\u5411\u3002\u6211\u4eec\u8fd9\u4e2a\u7b80\u5355\u7684\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u53c2\u6570\u4ec5\u51e0\u4e07\u4e2a\uff0c\u8bad\u7ec3\u7684\u76ee\u7684\u5b9e\u9645\u4e0a\u5c31\u662f\u4e3a\u4e86\u628a\u8fd9\u51e0\u4e07\u4e2a\u53c2\u6570\u786e\u5b9a\u4e0b\u6765\uff0c\u76ee\u6807\u662f\u4f7f\u5f97\u8bc6\u522b\u7387\u6700\u9ad8\u3002\u8bad\u7ec3\u8fd9\u51e0\u4e07\u4e2a\u53c2\u6570\u5c31\u82b1\u4e86\u51e0\u5206\u949f\u65f6\u95f4\uff0c\u5982\u679c\u662f\u51e0\u4ebf\u4e2a\u751a\u81f3\u51e0\u767e\u4ebf\u4e2a\u53c2\u6570\uff0c\u53ef\u60f3\u800c\u77e5\u8bad\u7ec3\u6240\u9700\u7684\u65f6\u95f4\u548c\u7b97\u529b\u90fd\u9700\u8981\u767e\u4e07\u500d\u7684\u589e\u957f\uff0c\u6240\u4ee5\uff0cAI\u6a21\u578b\u7684\u4ee3\u7801\u5e76\u4e0d\u590d\u6742\uff0c\u6a21\u578b\u89c4\u6a21\u5927\u4f46\u672c\u8eab\u7ed3\u6784\u5e76\u4e0d\u590d\u6742\uff0c\u4f46\u4e3a\u4e86\u786e\u5b9a\u6a21\u578b\u4e2d\u6bcf\u4e00\u5c42\u7684\u6210\u5343\u4e0a\u4e07\u4e2a\u53c2\u6570\uff0c\u65f6\u95f4\u548c\u7b97\u529b\u4e3b\u8981\u6d88\u8017\u5728\u8bad\u7ec3\u4e0a\u3002<\/p>\n\n\n\n<p>\u6bd4\u8f83\u4e00\u4e0b\u4f20\u7edf\u7a0b\u5e8f\u548cAI\u7a0b\u5e8f\u7684\u5dee\u5f02\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><\/th><th>\u4f20\u7edf\u7a0b\u5e8f<\/th><th>AI\u7a0b\u5e8f<\/th><\/tr><\/thead><tbody><tr><td>\u4ee3\u7801\u91cf<\/td><td>\u5927<\/td><td>\u5c11<\/td><\/tr><tr><td>\u8f93\u5165\u53c2\u6570<\/td><td>\u5c11<\/td><td>\u591a<\/td><\/tr><tr><td>\u8f93\u51fa\u7ed3\u679c<\/td><td>\u7cbe\u786e\u8f93\u51fa<\/td><td>\u4e0d\u786e\u5b9a\u6027\u8f93\u51fa<\/td><\/tr><tr><td>\u4ee3\u7801\u53c2\u6570<\/td><td>\u7531\u5f00\u53d1\u8bbe\u5b9a<\/td><td>\u7531\u8bad\u7ec3\u51b3\u5b9a<\/td><\/tr><tr><td>\u6267\u884c\u5c42\u6b21<\/td><td>\u53ef\u8fbe\u6570\u767e\u4e07\u884c<\/td><td>\u4ec5\u82e5\u5e72\u5c42\u7f51\u7edc<\/td><\/tr><tr><td>\u6267\u884c\u8def\u5f84<\/td><td>\u80fd\u7cbe\u786e\u8ddf\u8e2a<\/td><td>\u65e0\u6cd5\u8ddf\u8e2a<\/td><\/tr><tr><td>\u5e76\u884c<\/td><td>\u4e32\u884c\u6216\u5c11\u91cf\u5e76\u884c<\/td><td>\u5927\u89c4\u6a21\u5e76\u884c<\/td><\/tr><tr><td>\u8ba1\u7b97<\/td><td>\u4ee5CPU\u4e3a\u4e3b<\/td><td>\u4ee5GPU\u4e3a\u4e3b<\/td><\/tr><tr><td>\u5f00\u53d1\u65f6\u95f4<\/td><td>\u4e3b\u8981\u6d88\u8017\u5728\u7f16\u5199\u4ee3\u7801<\/td><td>\u4e3b\u8981\u6d88\u8017\u5728\u8bad\u7ec3<\/td><\/tr><tr><td>\u6570\u636e<\/td><td>\u4e3b\u8981\u5b58\u50a8\u7528\u6237\u4ea7\u751f\u7684\u6570\u636e<\/td><td>\u9700\u8981\u9884\u5907\u5927\u91cf\u8bad\u7ec3\u6570\u636e<\/td><\/tr><tr><td>\u7a0b\u5e8f\u8d28\u91cf<\/td><td>\u53d6\u51b3\u4e8e\u8bbe\u8ba1\u67b6\u6784\u3001\u4ee3\u7801\u4f18\u5316\u7b49<\/td><td>\u53d6\u51b3\u4e8e\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u548c\u8bad\u7ec3\u6570\u636e\u8d28\u91cf<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4f20\u7edf\u7a0b\u5e8f\u7684\u7279\u70b9\u662f\u7cbe\u786e\u6027\uff1a\u7cbe\u786e\u7684\u8f93\u5165\u53ef\u4ee5\u5b9e\u73b0\u7cbe\u786e\u5730\u6267\u884c\u8def\u5f84\uff0c\u6700\u7ec8\u83b7\u5f97\u7cbe\u786e\u7684\u7ed3\u679c\u3002\u800cAI\u7a0b\u5e8f\u5219\u662f\u4e00\u79cd\u6982\u7387\u8f93\u51fa\uff0c\u7531\u4e8e\u6a21\u578b\u7684\u53c2\u6570\u662f\u8bad\u7ec3\u751f\u6210\u7684\uff0c\u56e0\u6b64\uff0c\u5c31\u8fde\u5f00\u53d1\u8005\u81ea\u5df1\u4e5f\u65e0\u6cd5\u77e5\u9053\u8bad\u7ec3\u540e\u7684\u67d0\u4e2a\u53c2\u6570\u6bd4\u5982<code>0.123<\/code>\u7a76\u7adf\u662f\u4ec0\u4e48\u610f\u4e49\uff0c\u8c03\u5927\u6216\u8005\u8c03\u5c0f\u5bf9\u8f93\u51fa\u6709\u4ec0\u4e48\u5f71\u54cd\u3002\u4f20\u7edf\u7a0b\u5e8f\u7684\u903b\u8f91\u662f\u767d\u76d2\uff0cAI\u7a0b\u5e8f\u7684\u903b\u8f91\u5c31\u662f\u9ed1\u76d2\uff0c\u53ea\u80fd\u901a\u8fc7\u8c03\u6574\u795e\u7ecf\u7f51\u7edc\u7684\u89c4\u6a21\u3001\u5c42\u6b21\u3001\u8bad\u7ec3\u96c6\u548c\u8bad\u7ec3\u65b9\u5f0f\u6765\u8bc4\u4f30\u8f93\u51fa\u7ed3\u679c\uff0c\u65e0\u6cd5\u4e8b\u5148\u7ed9\u51fa\u4e00\u4e2a\u51c6\u786e\u7684\u9884\u4f30\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6e90\u7801\u4e0b\u8f7d<\/h3>\n\n\n\n<p>\u672c\u6587\u6e90\u7801\u53ef\u901a\u8fc7<a href=\"https:\/\/github.com\/michaelliao\/mnist\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub<\/a>\u4e0b\u8f7d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4ee5\u5de5\u7a0b\u5e08\u7684\u89c6\u89d2\u4ece\u96f6\u5f00\u59cb\u642d\u5efa\u5e76\u8fd0\u884c\u4e00\u4e2aAI\u5c0f\u6a21\u578b\uff0c\u5e76\u628a\u5b83\u5b8c\u5168\u8fd0\u884c\u8d77\u6765\u4ee5\u7406\u89e3AI\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u975e\u5e38\u63a5\u5730\u6c14\u3002 AI [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-2169","post","type-post","status-publish","format-standard","hentry","category-30"],"_links":{"self":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/2169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2169"}],"version-history":[{"count":1,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/2169\/revisions"}],"predecessor-version":[{"id":2170,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/2169\/revisions\/2170"}],"wp:attachment":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2169"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}