{"id":8824,"date":"2024-05-25T16:01:01","date_gmt":"2024-05-25T08:01:01","guid":{"rendered":""},"modified":"2024-05-25T16:01:01","modified_gmt":"2024-05-25T08:01:01","slug":"\u3010Pytorch\u3011\u8fc1\u79fb\u5b66\u4e60(Transfer Learning)\u300c\u5efa\u8bae\u6536\u85cf\u300d","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8824.html","title":{"rendered":"\u3010Pytorch\u3011\u8fc1\u79fb\u5b66\u4e60(Transfer Learning)\u300c\u5efa\u8bae\u6536\u85cf\u300d"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

1.\u4ec0\u4e48\u662f\u8fc1\u79fb\u5b66\u4e60<\/h3>\n

\u8fc1\u79fb\u5b66\u4e60\u662f\u4e00\u79cd\u673a\u5668\u5b66\u4e60\u6280\u672f\uff0c\u5176\u4e2d\u4e00\u4e2a\u6a21\u578b\u5df2\u5728\u4e00\u4e2a\u4efb\u52a1\u4e0a\u8bad\u7ec3\u597d\uff0c\u5e76\u4e14\u8be5\u6a21\u578b\u7684\u7ecf\u9a8c\u53ef\u4ee5\u7528\u6765\u66f4\u5feb\u5730\u8bad\u7ec3\u53e6\u4e00\u4e2a\u76f8\u4f3c\u7684\u4efb\u52a1\u3002\u8fd9\u79cd\u6280\u672f\u7684\u76ee\u7684\u662f\u4e3a\u4e86\u51cf\u5c11\u5728\u65b0\u4efb\u52a1\u4e0a\u7684\u8bad\u7ec3\u65f6\u95f4\uff0c\u56e0\u4e3a\u8bad\u7ec3\u6a21\u578b\u9700\u8981\u5927\u91cf\u7684\u6570\u636e\u548c\u65f6\u95f4\u3002<\/p>\n

2.\u5e38\u89c1\u8fc1\u79fb\u5b66\u4e60\u65b9\u5f0f<\/h3>\n
    \n
  1. \u8f7d\u5165\u6743\u91cd\u540e\u8bad\u7ec3\u6240\u6709\u53c2\u6570<\/li>\n
  2. \u8f7d\u5165\u6743\u91cd\u540e\u53ea\u8bad\u7ec3\u6700\u540e\u51e0\u5c42\u53c2\u6570<\/li>\n
  3. \u8f7d\u5165\u6743\u91cd\u540e\u5728\u539f\u7f51\u7edc\u57fa\u7840\u4e0a\u518d\u6dfb\u52a0\u4e00\u5c42\u5168\u94fe\u63a5\u5c42\uff0c\u4ec5\u8bad\u7ec3\u6700\u540e\u4e00\u4e2a\u5168\u94fe\u63a5\u5c42<\/li>\n<\/ol>\n

    3.\u4f8b\u5b50<\/h3>\n

    \u4ee5kaggle\u4e2d\u732b\u72d7\u6570\u636e\u96c6\u4e3a\u4f8b\uff0c\u732b\u72d7\u6570\u636e\u96c6
    \u5bfc\u5305<\/p>\n

    import torch\nimport torchvision\nfrom torch import nn, optim\nfrom torchvision import transforms, datasets, models\nfrom tqdm import tqdm\nimport sys\n<\/code><\/pre>\n

    tqdm\u548csys\uff0c\u5982\u679c\u4e0d\u9700\u8981\u8fdb\u5ea6\u6761\u663e\u793a\u53ef\u4e0d\u5bfc\u5165\u3002
    pytorch\u4e2d\u8fc1\u79fb\u5b66\u4e60\u6a21\u578b\u5728torchvision.models<\/code>\u91cc<\/p>\n

      \n
    1. vgg16<\/li>\n<\/ol>\n
      model = models.vgg16(pretrained=True)  # pretrained=True\u5373\u4e3a\u8fd4\u56de\u5728 ImageNet \uff08\u662f\u6570\u636e\u96c6\uff09\u4e0a\u9884\u8bad\u7ec3\u7684\u6a21\u578b\nfor parameter in model.parameters():\n    parameter.requires_grad = False\t   # \u51bb\u7ed3\u4e86\u6240\u6709\u5c42\uff08\u53c2\u6570\u4e0d\u4f1a\u66f4\u65b0\uff09\n<\/code><\/pre>\n

      \u6b64\u65f6\u6a21\u578b\u5df2\u5bfc\u5165\u3002\u53ef\u7528model.buffer<\/code>\u6216\u76f4\u63a5print(model)<\/code>\u67e5\u770b\u6a21\u578b\u3002
      \"\u3010Pytorch\u3011\u8fc1\u79fb\u5b66\u4e60(Transfer
      \u732b\u72d7\u6570\u636e\u96c6\u4e3a\u4e8c\u5206\u7c7b\uff0c\u6240\u4ee5\u6700\u540e\u4e00\u5c42\u5168\u8fde\u63a5\u5c42\u8f93\u51fa\u5e94\u4e3a2,\u4fee\u6539\u4e3a\uff1a<\/p>\n

      model.classifier[6] = nn.Linear(in_features=4096, out_features=2, bias=True)\n<\/code><\/pre>\n

      \u4fee\u6539\u8fc7\u540e\u8be5\u5168\u8fde\u63a5\u5c42\u4e0d\u518d\u88ab\u51bb\u7ed3\uff0c\u53c2\u6570\u53ef\u88ab\u66f4\u65b0\u3002
      \u53ef\u7528\u4ee5\u4e0b\u65b9\u5f0f\u67e5\u770b\u6a21\u578b\u5404\u5c42\u7684\u51bb\u7ed3\u60c5\u51b5\uff1a<\/p>\n

      for m, n in model.named_parameters():\n    print(m, n.requires_grad)\n<\/code><\/pre>\n

      \"\u3010Pytorch\u3011\u8fc1\u79fb\u5b66\u4e60(Transfer
      \u6700\u540e\u5c31\u662f\u8bad\u7ec3\uff0c\u8bad\u7ec3\u6700\u540e\u4e00\u5c42\u5168\u8fde\u63a5\u5c42\u7684\u53c2\u6570\u3002<\/p>\n

      optimizer = optim.Adam(model.classifier.parameters(), lr=0.0001)\n\nsave_path = '.\/save_path\/vgg16_1.pth'\n\nepochs = 15\ntrain_steps = len(train_dataloader)\nval_length = len(val_dataset)\nbest_acc = 0.0\n\nfor epoch in range(epochs):\n    running_loss = 0.0\n    model.train()\n    train_bar = tqdm(train_dataloader, file=sys.stdout)\n    for step, data in enumerate(train_bar):\n        images, labels = data\n        optimizer.zero_grad()\n        output = model(images.to(device))\n        loss = loss_function(output, labels.to(device))\n        loss.backward()\n        optimizer.step()\n        running_loss += loss\n        train_bar.desc = 'epoch:{}\/{} loss:{:.3f}'.format(epoch+1, epochs, loss)\n        \n    model.eval()\n    acc = 0.0\n    with torch.no_grad():\n        val_bar = tqdm(val_dataloader, file=sys.stdout)\n        for data in val_bar:\n            val_images, val_labels = data\n            val_output = model(val_images.to(device))\n            predict = torch.max(val_output, 1)[1]\n            acc += torch.eq(predict, val_labels.to(device)).sum().item()\n    \n        val_accurate = acc\/val_length\n        print('epoch:{}\/{} train_loss:{:.3f} val_accurate:{:.3f}'.format(epoch+1, epochs, running_loss\/train_steps, val_accurate))\n\n        if val_accurate > best_acc:\n            best_acc = val_accurate\n            torch.save(model.state_dict(), save_path)\n<\/code><\/pre>\n

      \u4e00\u5171\u8bad\u7ec3\u4e8615\u4e2aepoch\uff0c\u4e00\u4e2aepoch\u5dee\u4e0d\u591a43s\uff0c\u8bad\u7ec3\u8fc7\u7a0b\uff1a
      \"\u3010Pytorch\u3011\u8fc1\u79fb\u5b66\u4e60(Transfer
      \u53ef\u4ee5\u770b\u5230\u9a8c\u8bc1\u96c6\u51c6\u786e\u7387\u5dee\u4e0d\u591a95%\u3002<\/p>\n

      \u8fd8\u53ef\u53c2\u8003\u8fd9\u7bc7\u6587\u7ae0<\/p>\n","protected":false},"excerpt":{"rendered":"\u3010Pytorch\u3011\u8fc1\u79fb\u5b66\u4e60(Transfer Learning)\u300c\u5efa\u8bae\u6536\u85cf\u300d\u4ec0\u4e48\u662f\u8fc1\u79fb\u5b66\u4e60\uff0c\u5e38\u89c1\u8fc1\u79fb\u5b66\u4e60\u65b9\u5f0f\uff0c\u4f8b\u5b50_pytorch\u8fc1\u79fb\u5b66\u4e60","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8824"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=8824"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8824\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}