{"id":2621,"date":"2023-04-21T17:32:08","date_gmt":"2023-04-21T09:32:08","guid":{"rendered":"https:\/\/divis.cn\/?p=2621"},"modified":"2026-02-07T19:57:13","modified_gmt":"2026-02-07T11:57:13","slug":"2621","status":"publish","type":"post","link":"https:\/\/www.divis.cn\/index.php\/2023\/04\/21\/2621\/","title":{"rendered":"win11\u4e0bdocker\u5b89\u88c5chinese Clip\u7684\u6559\u7a0b"},"content":{"rendered":"<blockquote><p>\u81ea\u5df1\u7684\u6570\u636e\u96c6\u91cc\u4e2d\u6587\u4e3a\u4e3b\u3002\u867d\u7136\u539f\u751fclip\u5c45\u7136\u4e5f\u80fd\u5904\u7406\u4e2d\u6587\uff0c\u4e0d\u8fc7github\u4e0a\u4e5f\u6709\u4e2achinese clip\uff0c\u636e\u8bf4\u4f7f\u7528\u5927\u89c4\u6a21\u4e2d\u6587\u6570\u636e\u8fdb\u884c\u8bad\u7ec3\uff08~2\u4ebf\u56fe\u6587\u5bf9\uff09\uff0c\u65e8\u5728\u5e2e\u52a9\u7528\u6237\u5feb\u901f\u5b9e\u73b0\u4e2d\u6587\u9886\u57df\u7684<a href=\"https:\/\/github.com\/OFA-Sys\/Chinese-CLIP#API%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B\">\u56fe\u6587\u7279\u5f81&amp;\u76f8\u4f3c\u5ea6\u8ba1\u7b97<\/a>\u3001<a href=\"https:\/\/github.com\/OFA-Sys\/Chinese-CLIP#%E8%B7%A8%E6%A8%A1%E6%80%81%E6%A3%80%E7%B4%A2\">\u8de8\u6a21\u6001\u68c0\u7d22<\/a>\u3001<a href=\"https:\/\/github.com\/OFA-Sys\/Chinese-CLIP#%E9%9B%B6%E6%A0%B7%E6%9C%AC%E5%9B%BE%E5%83%8F%E5%88%86%E7%B1%BB\">\u96f6\u6837\u672c\u56fe\u7247\u5206\u7c7b<\/a>\u7b49\u4efb\u52a1\u3002\u6240\u4ee5\u6211\u6709\u5fc5\u8981\u518d\u90e8\u7f72\u4e00\u4e2achinese clip\u8bd5\u4e00\u8bd5\u6548\u679c\uff0c\u751a\u81f3finetune\u4e00\u4e0b\u3002\u4ee5\u4e0b\u662f\u90e8\u7f72\u6d41\u7a0b\u3002<\/p><\/blockquote>\n<h2>\u524d\u7f6e\u901a\u7528\u6b65\u9aa4<\/h2>\n<p>\u5728 PowerShell \u4e2d\u8fd0\u884c\u4ee5\u4e0b\u683c\u5f0f\u547d\u4ee4\uff0c\u4ee5\u542f\u7528GPU\u7684\u65b9\u5f0f\u542f\u52a8\u5bb9\u5668\uff1a<span style=\"font-size: 1.125rem;\">docker run &#8211;name [\u7ed9\u4f60\u7684\u5bb9\u5668\u8d77\u7684\u540d\u5b57] &#8211;gpus all -it [\u4f60\u4e0b\u8f7d\u7684ubuntu\u955c\u50cfID\u6216\u540d\u5b57] \/bin\/bash<\/span><\/p>\n<blockquote><p>docker run &#8211;name chineseClip &#8211;gpus all -it ubuntu \/bin\/bash<\/p><\/blockquote>\n<p><span style=\"font-size: 1.125rem;\">\u63a5\u4e0b\u6765\u66f4\u65b0apt<\/span><\/p>\n<blockquote><p>apt-get update<\/p><\/blockquote>\n<p>\u7136\u540e\u88c5\u4e2awget\u65b9\u4fbf\u4e0b\u8f7d\u5176\u4ed6\u5404\u79cd\u5b89\u88c5\u5305<\/p>\n<blockquote><p>apt-get install wget<\/p><\/blockquote>\n<p>\u5b89\u88c5Miniconda<\/p>\n<blockquote><p>wget https:\/\/repo.anaconda.com\/miniconda\/Miniconda3-py37_23.1.0-1-Linux-x86_64.sh<\/p>\n<p><span style=\"color: #303030; font-size: 1.125rem;\">bash Miniconda3-py37_23.1.0-1-Linux-x86_64.sh<\/span><\/p><\/blockquote>\n<p>\u7136\u540e\u5173\u95ed\u5f53\u524d\u7ec8\u7aef\uff0c\u91cd\u65b0\u6253\u5f00\u4e00\u4e2a\u65b0\u7ec8\u7aef\uff0cminiconda\u624d\u80fd\u751f\u6548\u3002\u6253\u5f00\u65b0\u7ec8\u7aef\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef<\/p>\n<blockquote><p>docker exec -it [\u4f60\u7684\u5bb9\u5668\u540d\u79f0] \/bin\/bash<\/p><\/blockquote>\n<p>\u6b64\u65f6\u53ef\u4ee5\u67e5\u770b\u7248\u672c\u53f7\u547d\u4ee4\u6765\u67e5\u770bminiconda\u662f\u5426\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\u3002<\/p>\n<blockquote><p>conda <span class=\"hljs-attr\">&#8211;version<\/span><\/p>\n<p>python <span class=\"hljs-attr\">&#8211;version<\/span><\/p><\/blockquote>\n<p><span style=\"color: #303030; font-size: 1.125rem;\">\u00a0\u5982\u679c\u4ee5\u4e0a\u547d\u4ee4\u6267\u884c\u65e0\u8bef\uff0c\u5219\u53ef\u4ee5\u5220\u9664\u4e4b\u524d\u7684miniconda\u5b89\u88c5\u811a\u672c<\/span><\/p>\n<blockquote><p>rm Miniconda3-py37_23.1.0-1-Linux-x86_64.sh<\/p><\/blockquote>\n<p><span style=\"color: #303030; font-size: 1.125rem;\">\u66f4\u65b0miniconda<\/span><\/p>\n<div class=\"hljs-ln-code\">\n<div class=\"hljs-ln-code\">\n<blockquote>\n<div class=\"hljs-ln-line\">conda\u00a0install\u00a0&#8211;channel\u00a0defaults\u00a0conda\u00a0python=<span class=\"hljs-number\">3.7<\/span> &#8211;yes<\/div>\n<div class=\"hljs-ln-line\"><span style=\"font-size: 1.125rem;\">conda\u00a0update\u00a0&#8211;channel\u00a0defaults\u00a0&#8211;all\u00a0&#8211;yes<\/span><\/div>\n<\/blockquote>\n<div>\n<p>\u66f4\u65b0pip<\/p>\n<blockquote><p>pip install\u00a0<span class=\"hljs-attr\">&#8211;upgrade<\/span> pip<\/p><\/blockquote>\n<\/div>\n<h2>\u5b89\u88c5chinese Clip<\/h2>\n<p>\u6309\u7167GitHub\u4e0achinese clip\u9879\u76ee\u7684\u4f7f\u7528\u8bf4\u660e\u8fdb\u884c\u64cd\u4f5c\uff1a<a href=\"https:\/\/github.com\/OFA-Sys\/Chinese-CLIP\">https:\/\/github.com\/OFA-Sys\/Chinese-CLIP<\/a><\/p>\n<div><img loading=\"lazy\" decoding=\"async\" class=\"size-medium alignleft\" src=\"https:\/\/img.divis.cn\/2023\/04\/Pasted-19.png\" width=\"937\" height=\"502\" \/><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div>\u7b2c\u4e00\u6b65\uff0c\u4e0b\u8f7drequirements.txt<\/div>\n<div>\u6211\u6ca1\u6709\u7528git\u4e0b\u8f7d\u6574\u4e2a\u9879\u76ee\uff0c\u90a3\u6837\u592a\u5927\u4e86\u4e5f\u6ca1\u6709\u5fc5\u8981\uff0c\u6709\u4e9b\u4e1c\u897f\u76f4\u63a5\u5728github\u4e0a\u67e5\u770b\u5373\u53ef\u3002\u4f8b\u5982\u8fd9\u4e2arequirements.txt\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u6253\u5f00\u67e5\u770b\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/div>\n<div><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"https:\/\/img.divis.cn\/2023\/04\/Pasted-21.png\" width=\"931\" height=\"798\" \/><\/div>\n<div><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"https:\/\/img.divis.cn\/2023\/04\/Pasted-22.png\" width=\"1234\" height=\"377\" \/><\/div>\n<div>\u5728\u672c\u5730\u521b\u5efa\u4e00\u4e2a\u540c\u540d\u6587\u4ef6\uff0c\u590d\u5236\u4e0a\u56fe\u6240\u793a\u7684requirements.txt\u4e2d\u76847\u884c\u4ee3\u7801\u8fdb\u53bb\uff0c\u7136\u540e\u5c06\u8be5\u540c\u540d\u6587\u4ef6\u4e0a\u4f20\u5230\u4f60\u7684docker\u5bb9\u5668\u7684\u6587\u4ef6\u76ee\u5f55\u4e2d\uff0c\u63a5\u4e0b\u6765\u8fd0\u884c\uff1a<\/div>\n<div><\/div>\n<blockquote>\n<div>pip install -r requirements.txt<\/div>\n<\/blockquote>\n<div>\u7136\u540e\u4f60\u4f1a\u770b\u5230\u7cfb\u7edf\u5471\u5527\u5471\u5527\u4e0b\u8f7d\u4e86\u5f88\u591a\u4f9d\u8d56\u3002<\/div>\n<div><img loading=\"lazy\" decoding=\"async\" class=\"size-medium alignleft\" src=\"https:\/\/img.divis.cn\/2023\/04\/Pasted-23.png\" width=\"1462\" height=\"460\" \/>\u7136\u540e\u5b89\u88c5cn_clip<\/div>\n<div>pip install cn_clip<\/div>\n<div>\u5b89\u88c5\u5f88\u5feb\uff0c\u4f46\u662f\u53ea\u4e0b\u8f7d\u4e86cn_clip\u7684packages\uff0c\u5e76\u6ca1\u6709\u4e0b\u8f7d\u6a21\u578b\u672c\u8eab\u3002<\/div>\n<div>\u63a5\u4e0b\u6765\u8fd0\u884c\u6d4b\u8bd5\u4ee3\u7801\u3002<\/div>\n<hr \/>\n<h2>Chinese Clip\u6a21\u578b\u7684\u4f7f\u7528\u6d4b\u8bd5<\/h2>\n<p>\u5728\u5bbf\u4e3b\u673a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3atest_clip.py\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<blockquote><p>import torch<br \/>\nfrom PIL import Image<\/p>\n<p>import cn_clip.clip as clip<br \/>\nfrom cn_clip.clip import load_from_name, available_models<br \/>\nprint(&#8220;Available models:&#8221;, available_models())<br \/>\n# Available models: [&#8216;ViT-B-16&#8217;, &#8216;ViT-L-14&#8217;, &#8216;ViT-L-14-336&#8217;, &#8216;ViT-H-14&#8217;, &#8216;RN50&#8242;]<\/p>\n<p>device = &#8220;cuda&#8221; if torch.cuda.is_available() else &#8220;cpu&#8221;<br \/>\nmodel, preprocess = load_from_name(&#8220;ViT-B-16&#8221;, device=device, download_root=&#8217;.\/&#8217;)<br \/>\nmodel.eval()<br \/>\nimage = preprocess(Image.open(&#8220;test.jpeg&#8221;)).unsqueeze(0).to(device)<br \/>\ntext = clip.tokenize([&#8220;\u5e02\u573a&#8221;, &#8220;\u5de5\u5730&#8221;, &#8220;\u4eba&#8221;, &#8220;\u72d7&#8221;, &#8220;\u9c7c&#8221;, &#8220;\u867e&#8221;, &#8220;\u87f9&#8221;]).to(device)<\/p>\n<p>with torch.no_grad():<br \/>\nimage_features = model.encode_image(image)<br \/>\ntext_features = model.encode_text(text)<\/p>\n<p># \u5bf9\u7279\u5f81\u8fdb\u884c\u5f52\u4e00\u5316\uff0c\u8bf7\u4f7f\u7528\u5f52\u4e00\u5316\u540e\u7684\u56fe\u6587\u7279\u5f81\u7528\u4e8e\u4e0b\u6e38\u4efb\u52a1<br \/>\nimage_features \/= image_features.norm(dim=-1, keepdim=True)<br \/>\ntext_features \/= text_features.norm(dim=-1, keepdim=True)<\/p>\n<p>logits_per_image, logits_per_text = model.get_similarity(image, text)<br \/>\nprobs = logits_per_image.softmax(dim=-1).cpu().numpy()<\/p>\n<p>print(&#8220;Label probs:&#8221;, probs) # [[1.268734e-03 5.436878e-02 6.795761e-04 9.436829e-01]]<\/p><\/blockquote>\n<p>\u7136\u540e\u518d\u627e\u4e2a\u56fe\u7247\uff0c\u91cd\u547d\u540d\u4e3atest.jpg\uff0c\u6bd4\u5982\u8fd9\u4e2a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium alignnone\" src=\"https:\/\/img.divis.cn\/2023\/04\/test.jpeg\" width=\"640\" height=\"289\" \/><\/p>\n<p>\u7136\u540e\u628atest_clip.py\u548cexample.jpg\u90fd\u653e\u5230\u4e00\u4e2atest\u6587\u4ef6\u5939\u91cc\uff0c\u7136\u540eimport\u5230\u6211\u7684linux\u5b50\u7cfb\u7edf\u4e2d\u3002\u5177\u4f53\u7684\u8bf4\uff0c\u6211\u4e0a\u4f20\u5230\u4e86opt\u76ee\u5f55\u4e0b\u3002<\/p>\n<p data-wp-editing=\"1\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium alignleft\" src=\"https:\/\/img.divis.cn\/2023\/04\/Pasted-13.png\" width=\"1617\" height=\"477\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"https:\/\/img.divis.cn\/2023\/04\/Pasted-12.png\" width=\"1678\" height=\"812\" \/><\/p>\n<p>\u7136\u540e\u56de\u5230linux\u5b50\u7cfb\u7edf\u7684\u7ec8\u7aef\uff0c\u8fdb\u5165\u8be5\u76ee\u5f55\uff0c\u5e76\u8fd0\u884cpython\u6587\u4ef6<\/p>\n<blockquote><p>cd opt\/test<\/p>\n<p>python test_clip.py<\/p><\/blockquote>\n<p>\u8fd4\u56de\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium alignleft\" src=\"https:\/\/img.divis.cn\/2023\/04\/Pasted-24.png\" width=\"1455\" height=\"241\" \/><\/p>\n<p>\u663e\u793a\u4ee5\u4e0a\u5185\u5bb9\u8bf4\u660e\u8fd0\u884c\u6210\u529f\u3002\u4e0a\u9762\u90a3\u4e9b8.877e-01\u4e4b\u7c7b\u7684\u90fd\u662f\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u7684\u6570\u5b57\uff0c\u4f8b\u59828.877e-01\u76f8\u5f53\u4e8e0.8877\uff0c\u5bf9\u5e94\u5e02\u573a\u8fd9\u4e2a\u8bcd\u7684\u6982\u7387\uff0c\u662f\u51e0\u4e2a\u6807\u7b7e\uff08&#8221;\u5e02\u573a&#8221;, &#8220;\u5de5\u5730&#8221;, &#8220;\u4eba&#8221;, &#8220;\u72d7&#8221;, &#8220;\u9c7c&#8221;, &#8220;\u867e&#8221;, &#8220;\u87f9&#8221;\uff09\u4e2d\u4e0e\u56fe\u7247\u76f8\u4f3c\u5ea6\u6700\u9ad8\u7684\u3002<\/p>\n<p>\u53e6\u5916\u6267\u884c\u8fd9\u4e00\u6b65\u4f1a\u6bd4\u8f83\u82b1\u65f6\u95f4\uff0c\u56e0\u4e3a\u8981\u4e0b\u8f7d\u6a21\u578b\u3002\u67e5\u770b\u4ee3\u7801\uff0c\u5b9e\u9645\u4e0a\u4e0b\u8f7d\u7684\u6a21\u578b\u662f\uff1aViT-B-16\uff0c \u5e76\u4e14\u4e0b\u8f7d\u5230\u4e86test\u6587\u4ef6\u5939\u4e2d\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"https:\/\/img.divis.cn\/2023\/04\/Pasted-25.png\" width=\"849\" height=\"409\" \/><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u81ea\u5df1\u7684\u6570\u636e\u96c6\u91cc\u4e2d\u6587\u4e3a\u4e3b\u3002\u867d\u7136\u539f\u751fclip<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2621","post","type-post","status-publish","format-standard","hentry","category-ai-application"],"_links":{"self":[{"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/posts\/2621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2621"}],"version-history":[{"count":1,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/posts\/2621\/revisions"}],"predecessor-version":[{"id":3088,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/posts\/2621\/revisions\/3088"}],"wp:attachment":[{"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}