{"id":635,"date":"2023-03-16T10:36:18","date_gmt":"2023-03-16T02:36:18","guid":{"rendered":"https:\/\/scutvk.cn\/?p=635"},"modified":"2023-03-16T10:36:24","modified_gmt":"2023-03-16T02:36:24","slug":"%e4%bd%bf%e7%94%a8beit%e6%8f%90%e5%8f%96%e5%9b%be%e7%89%87%e7%89%b9%e5%be%81","status":"publish","type":"post","link":"https:\/\/scutvk.cn\/?p=635","title":{"rendered":"\u4f7f\u7528Beit\u63d0\u53d6\u56fe\u7247\u7279\u5f81"},"content":{"rendered":"\n<p>\u5f53\u4eca\uff0c\u8ba1\u7b97\u673a\u89c6\u89c9\u6280\u672f\u5728\u5404\u4e2a\u9886\u57df\u4e2d\u5f97\u5230\u4e86\u5e7f\u6cdb\u5e94\u7528\uff0c\u5176\u4e2d\u56fe\u7247\u7279\u5f81\u63d0\u53d6\u662f\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u4e2d\u7684\u91cd\u8981\u4e00\u73af\u3002\u800c\u57282021\u5e74\u7684\u7814\u7a76\u4e2d\uff0c\u5fae\u8f6f\u63a8\u51fa\u4e86\u4e00\u79cd\u9884\u8bad\u7ec3\u6a21\u578b\u2014\u2014Beit\uff0c\u5b83\u5728\u63d0\u53d6\u56fe\u7247\u7279\u5f81\u65b9\u9762\u8868\u73b0\u51fa\u4e86\u5353\u8d8a\u7684\u6027\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecdBeit\u6a21\u578b\u5e76\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528\u5b83\u6765\u63d0\u53d6\u56fe\u7247\u7279\u5f81\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"678\" src=\"https:\/\/x2.mday.top\/wp-content\/uploads\/2023\/03\/image-1024x678.png\" alt=\"\" class=\"wp-image-636\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2023\/03\/image-1024x678.png 1024w, https:\/\/scutvk.cn\/wp-content\/uploads\/2023\/03\/image-300x198.png 300w, https:\/\/scutvk.cn\/wp-content\/uploads\/2023\/03\/image-768x508.png 768w, https:\/\/scutvk.cn\/wp-content\/uploads\/2023\/03\/image-769x509.png 769w, https:\/\/scutvk.cn\/wp-content\/uploads\/2023\/03\/image.png 1194w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e00\u3001\u56fe\u7247Resize<\/strong><\/p>\n\n\n\n<p>\u5728\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u4e2d\uff0c\u4e0d\u540c\u5927\u5c0f\u7684\u56fe\u7247\u901a\u5e38\u9700\u8981\u88ab\u5904\u7406\u6210\u76f8\u540c\u7684\u5927\u5c0f\uff0c\u4ee5\u4fbf\u8fdb\u884c\u540e\u7eed\u7684\u7279\u5f81\u63d0\u53d6\u6216\u8005\u6a21\u578b\u8bad\u7ec3\u3002\u6b64\u5916\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5982\u79fb\u52a8\u7aef\u6216\u8005\u7f51\u7edc\u4f20\u8f93\u65f6\uff0c\u66f4\u5c0f\u7684\u56fe\u7247\u4e5f\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u548c\u6548\u7387\u3002\u56e0\u6b64\uff0c\u56fe\u7247Resize\u662f\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u4e2a\u6b65\u9aa4\u3002<\/p>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pillow\uff08Python Imaging Library\uff0c\u4e5f\u53eb\u505aPIL\uff09\u5e93\u6765\u8fdb\u884c\u56fe\u7247\u7684Resize\u64cd\u4f5c\u3002\u5728\u4f7f\u7528Beit\u63d0\u53d6\u7279\u5f81\u65f6\uff0c\u6211\u4eec\u5148\u5c06\u56fe\u7247Resize\u672a[3, 224, 224]\u7684\u5927\u5c0f\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">import torch\r\nimport torchvision\r\nfrom torch import nn\r\nfrom torchvision import transforms\n\n# Define image transform\ntransform = transforms.Compose([\n    transforms.Resize((224, 224)),\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=[0.485, 0.456, 0.406],\n        std=[0.229, 0.224, 0.225]\n    )\n])\n\n# Load image\nimage = Image.open(\"image.jpg\")\n\n# Apply transform to image\nimage = transform(image)<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e8c\u3001\u521d\u59cb\u5316\u6a21\u578b\u5e76\u52a0\u8f7d\u53c2\u6570<\/strong><\/p>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528PyTorch\u5e93\u6765\u521d\u59cb\u5316Beit\u6a21\u578b\uff0c\u5e76\u52a0\u8f7d\u9884\u8bad\u7ec3\u53c2\u6570\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">from transformers import BeitForImageClassification\nfrom PIL import Image\nimport requests\n\nurl = 'http:\/\/images.cocodataset.org\/val2017\/000000039769.jpg'\nimage = Image.open(requests.get(url, stream=True).raw)\n\nmodel = BeitForImageClassification.from_pretrained('microsoft\/beit-large-patch16-224-pt22k-ft22k')<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e09\u3001\u53bb\u9664\u539f\u6a21\u578b\u5206\u7c7b\u5668<\/strong><\/p>\n\n\n\n<p>BeitForImageClassification\u539f\u6a21\u578b\u6700\u540e\u4e00\u5c42\u662f\u4e00\u4e2a\u5c061024\u7ef4\u6620\u5c04\u523021841\u7ef4\u7684MLP\uff0c\u4ee5\u7528\u4e8e\u9884\u6d4b\u56fe\u7247\u548c\u8bcd\u5178\u4e2d\u7684\u4ec0\u4e48token\u6700\u76f8\u4f3c\uff0c\u5e76\u7ed9\u51fa\u7ed3\u679c\u3002\u90a3\u4e48\u6211\u4eec\u5982\u679c\u8981\u63d0\u53d6\u56fe\u7247\u7279\u5f81\uff0c\u4e5f\u5c31\u662f\u5012\u6570\u7b2c\u4e8c\u5c42\u4e2d\u8f93\u51fa\u76841024\u7ef4\u7684\u7279\u5f81\uff0c\u6211\u4eec\u5219\u9700\u8981\u5c06\u6700\u540e\u4e00\u5c42\u5206\u7c7b\u5668\u53bb\u9664\u3002\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\u89c2\u5bdf\u6a21\u578b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">print(model)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"966\" height=\"809\" src=\"https:\/\/x2.mday.top\/wp-content\/uploads\/2023\/03\/image-1.png\" alt=\"\" class=\"wp-image-638\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2023\/03\/image-1.png 966w, https:\/\/scutvk.cn\/wp-content\/uploads\/2023\/03\/image-1-300x251.png 300w, https:\/\/scutvk.cn\/wp-content\/uploads\/2023\/03\/image-1-768x643.png 768w, https:\/\/scutvk.cn\/wp-content\/uploads\/2023\/03\/image-1-769x644.png 769w\" sizes=\"(max-width: 966px) 100vw, 966px\" \/><\/figure>\n\n\n\n<p>\u53bb\u9664\u6700\u540e\u4e00\u5c42\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">import torch\r\nnew_model = torch.nn.Sequential( *( list(model.children())[:-1] ) )<\/code><\/pre>\n\n\n\n<p>\u518d\u6b21\u89c2\u5bdf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">print(new_model)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"700\" src=\"https:\/\/x2.mday.top\/wp-content\/uploads\/2023\/03\/image-2-1024x700.png\" alt=\"\" class=\"wp-image-639\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2023\/03\/image-2-1024x700.png 1024w, https:\/\/scutvk.cn\/wp-content\/uploads\/2023\/03\/image-2-300x205.png 300w, https:\/\/scutvk.cn\/wp-content\/uploads\/2023\/03\/image-2-768x525.png 768w, https:\/\/scutvk.cn\/wp-content\/uploads\/2023\/03\/image-2-769x526.png 769w, https:\/\/scutvk.cn\/wp-content\/uploads\/2023\/03\/image-2.png 1040w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u90a3\u4e48\uff0c\u6211\u4eec\u4fbf\u5f97\u5230\u4e86\u4e00\u4e2aBeit\u63d0\u53d6\u7279\u5f81\u6a21\u578b\u3002\u4f7f\u7528\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">feature = new_model(image)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u4eca\uff0c\u8ba1\u7b97\u673a\u89c6\u89c9\u6280\u672f\u5728\u5404\u4e2a\u9886\u57df\u4e2d\u5f97\u5230\u4e86\u5e7f\u6cdb\u5e94\u7528\uff0c\u5176&hellip;<\/p>\n","protected":false},"author":1,"featured_media":636,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[2,31,30],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/scutvk.cn\/wp-content\/uploads\/2023\/03\/image.png","_links":{"self":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts\/635"}],"collection":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=635"}],"version-history":[{"count":2,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts\/635\/revisions"}],"predecessor-version":[{"id":640,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts\/635\/revisions\/640"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/media\/636"}],"wp:attachment":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}