{"id":78,"date":"2021-10-07T21:41:38","date_gmt":"2021-10-07T13:41:38","guid":{"rendered":"http:\/\/scutvk.cn\/?p=78"},"modified":"2021-10-18T20:51:45","modified_gmt":"2021-10-18T12:51:45","slug":"%e6%9b%b4%e6%96%b0%e4%b8%adpytorch%e5%ad%a6%e4%b9%a0%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/scutvk.cn\/?p=78","title":{"rendered":"Pytorch\u5b66\u4e60\u8bb0\u5f55[1]"},"content":{"rendered":"\n<p>\u672c\u6587\u5185\u5bb9\u5305\u62ec: Dataset\u7c7b\u3001Tensorboard\u7684\u4f7f\u7528\u3001\u51e0\u79cd\u5e38\u89c1\u7684Transforms<\/p>\n\n\n\n<p>\u9879\u76ee\u5168\u6e90\u7801\u53ca\u6570\u636e\u96c6:<a href=\"http:\/\/netdisk.scutvk.cn\/pytorch_lesson_1.zip\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/netdisk.scutvk.cn\/pytorch_lesson_1.zip<\/a><\/p>\n\n\n\n<p style=\"font-size:24px\"><strong>P7:Dataset\u7c7b\u4ee3\u7801\u5b9e\u6218<\/strong><\/p>\n\n\n\n<p>\u6b64\u5904\u7528\u5230\u7684\u6570\u636e\u96c6\u7ed3\u6784\u5982\u56fe1<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"301\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-1024x301.png\" alt=\"\" class=\"wp-image-80\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-1024x301.png 1024w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-300x88.png 300w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-768x226.png 768w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image.png 1149w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>\u56fe1<\/figcaption><\/figure>\n\n\n\n<p>\u6e90\u7801\u5982\u4e0b:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from torch.utils.data import  Dataset\nfrom PIL import  Image\nimport os\n\nclass MyData(Dataset):#\u7ee7\u627fDataset\n    def __init__(self, root_dir, label_dir):#\u6784\u9020\u51fd\u6570\n        self.root_dir = root_dir#\u6570\u636e\u96c6\u6839\u76ee\u5f55\n        self.label_dir = label_dir#\u6570\u636e\u96c6\u6587\u4ef6\u5939\u4e2d\u7684\u5206\u7c7b\u6587\u4ef6\u5939\n        self.path = os.path.join(self.root_dir, self.label_dir)#\u5408\u5e76\u8bfb\u51fa\u603b\u7684\u56fe\u7247\u6587\u4ef6\u5939\u5730\u5740\n        self.image_list = os.listdir(self.path)\n\n    def __getitem__(self, index):#\u53ef\u4ee5\u5728\u5176\u4ed6\u5730\u65b9\u7528&#91;i]\n        img_name = self.image_list&#91;index]\n        img_item_path = os.path.join(self.path, img_name)#\u5f97\u5230\u56fe\u7247\u7edd\u5bf9\u5730\u5740\n        img = Image.open(img_item_path)\n        label = self.label_dir\n        return img, label\n    def __len__(self):#\u53ef\u4ee5\u7528len()\n        return len(self.image_list)\n\n\n\nroot_dir = \"hymenoptera_data\\\\train\"\nants_dataset = MyData(root_dir, \"ants\")\nbees_dataset = MyData(root_dir, \"bees\")\ndataset = ants_dataset + bees_dataset\nprint(len(dataset))\nimg, label = dataset&#91;244]\nimg.show()\n\n<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u672c\u96c6get:<\/strong><\/p>\n\n\n\n<p>(1):Python\u7c7b\u7ee7\u627f<\/p>\n\n\n\n<p>(2):__init__    __getitem__    __len__\u4f5c\u7528<\/p>\n\n\n\n<p>(3):os.listdir() #\u8fd4\u56de\u5305\u542b\u6587\u4ef6\u5939\u5185\u6240\u6709\u6587\u4ef6\u540d\u7684\u4e00\u4e2alist <a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/os.html?highlight=listdir#os.listdir\" target=\"_blank\" rel=\"noopener\">os &#8212; \u591a\u79cd\u64cd\u4f5c\u7cfb\u7edf\u63a5\u53e3 \u2014 Python 3.10.0 \u6587\u6863<\/a><\/p>\n\n\n\n<p><code>os.<\/code><code>listdir<\/code>(<em>path=&#8217;.&#8217;<\/em>)<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/os.html?highlight=listdir#os.listdir\" target=\"_blank\" rel=\"noopener\"><\/a><\/p>\n\n\n\n<p>\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u7531&nbsp;<em>path<\/em>&nbsp;\u6307\u5b9a\u76ee\u5f55\u4e2d\u6761\u76ee\u540d\u79f0\u7ec4\u6210\u7684\u5217\u8868\u3002 \u8be5\u5217\u8868\u6309\u4efb\u610f\u987a\u5e8f\u6392\u5217\uff0c\u5e76\u4e14\u4e0d\u5305\u62ec\u7279\u6b8a\u6761\u76ee&nbsp;<code>'.'<\/code>&nbsp;\u548c&nbsp;<code>'..'<\/code>\uff0c\u5373\u4f7f\u5b83\u4eec\u5b58\u5728\u4e8e\u76ee\u5f55\u4e2d\u3002 \u5982\u679c\u6709\u6587\u4ef6\u5728\u8c03\u7528\u6b64\u51fd\u6570\u671f\u95f4\u5728\u88ab\u79fb\u9664\u6216\u6dfb\u52a0\u5230\u76ee\u5f55\u4e2d\uff0c\u662f\u5426\u8981\u5305\u62ec\u8be5\u6587\u4ef6\u7684\u540d\u79f0\u5e76\u6ca1\u6709\u89c4\u5b9a\u3002<\/p>\n\n\n\n<p><em>path<\/em>&nbsp;\u53ef\u4ee5\u662f&nbsp;<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/glossary.html#term-path-like-object\" target=\"_blank\" rel=\"noopener\">\u7c7b\u8def\u5f84\u5bf9\u8c61<\/a>\u3002\u5982\u679c&nbsp;<em>path<\/em>&nbsp;\u662f\uff08\u76f4\u63a5\u4f20\u5165\u6216\u901a\u8fc7&nbsp;<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/os.html?highlight=listdir#os.PathLike\" target=\"_blank\" rel=\"noopener\"><code>PathLike<\/code><\/a>&nbsp;\u63a5\u53e3\u95f4\u63a5\u4f20\u5165\uff09&nbsp;<code>bytes<\/code>&nbsp;\u7c7b\u578b\uff0c\u5219\u8fd4\u56de\u7684\u6587\u4ef6\u540d\u4e5f\u5c06\u662f&nbsp;<code>bytes<\/code>&nbsp;\u7c7b\u578b\uff0c\u5176\u4ed6\u60c5\u51b5\u4e0b\u662f&nbsp;<code>str<\/code>&nbsp;\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u672c\u51fd\u6570\u4e5f\u652f\u6301&nbsp;<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/os.html?highlight=listdir#path-fd\" target=\"_blank\" rel=\"noopener\">\u6307\u5b9a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a\u53c2\u6570<\/a>\uff0c\u5176\u4e2d\u63cf\u8ff0\u7b26\u5fc5\u987b\u6307\u5411\u76ee\u5f55\u3002<\/p>\n\n\n\n<p>\u5f15\u53d1\u4e00\u4e2a&nbsp;<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/sys.html#auditing\" target=\"_blank\" rel=\"noopener\">\u5ba1\u8ba1\u4e8b\u4ef6<\/a>&nbsp;<code>os.listdir<\/code>\uff0c\u9644\u5e26\u53c2\u6570&nbsp;<code>path<\/code>\u3002<\/p>\n\n\n\n<p>(4):os.path.join() #\u8fd4\u56de\u4e24\u4e2a\u8def\u5f84\u5408\u5e76 <a href=\"https:\/\/docs.python.org\/zh-cn\/3.8\/library\/os.path.html?highlight=path%20join#os.path.join\" target=\"_blank\" rel=\"noopener\">os.path &#8212; \u5e38\u7528\u8def\u5f84\u64cd\u4f5c \u2014 Python 3.8.12 \u6587\u6863<\/a><\/p>\n\n\n\n<p><code>os.path.<\/code><code>join<\/code>(<em>path<\/em>,&nbsp;<em>*paths<\/em>)<a href=\"https:\/\/docs.python.org\/zh-cn\/3.8\/library\/os.path.html?highlight=path%20join#os.path.join\" target=\"_blank\" rel=\"noopener\"><\/a><\/p>\n\n\n\n<p>\u667a\u80fd\u5730\u62fc\u63a5\u4e00\u4e2a\u6216\u591a\u4e2a\u8def\u5f84\u90e8\u5206\u3002 \u8fd4\u56de\u503c\u662f&nbsp;<em>path<\/em>&nbsp;\u548c&nbsp;<em>*paths<\/em>&nbsp;\u7684\u6240\u6709\u6210\u5458\u7684\u62fc\u63a5\uff0c\u5176\u4e2d\u6bcf\u4e2a\u975e\u7a7a\u90e8\u5206\u540e\u9762\u90fd\u7d27\u8ddf\u4e00\u4e2a\u76ee\u5f55\u5206\u9694\u7b26\uff0c\u6700\u540e\u4e00\u4e2a\u90e8\u5206\u9664\u5916\uff0c<strong>\u8fd9\u610f\u5473\u7740\u5982\u679c\u6700\u540e\u4e00\u4e2a\u90e8\u5206\u4e3a\u7a7a\uff0c\u5219\u7ed3\u679c\u5c06\u4ee5\u5206\u9694\u7b26\u7ed3\u5c3e\u3002<\/strong> \u5982\u679c\u67d0\u4e2a\u90e8\u5206\u4e3a\u7edd\u5bf9\u8def\u5f84\uff0c\u5219\u4e4b\u524d\u7684\u6240\u6709\u90e8\u5206\u4f1a\u88ab\u4e22\u5f03\u5e76\u4ece\u7edd\u5bf9\u8def\u5f84\u90e8\u5206\u5f00\u59cb\u7ee7\u7eed\u62fc\u63a5\u3002<\/p>\n\n\n\n<p>\u5728 Windows \u4e0a\uff0c\u9047\u5230\u7edd\u5bf9\u8def\u5f84\u90e8\u5206\uff08\u4f8b\u5982&nbsp;<code>r'\\foo'<\/code>\uff09\u65f6\uff0c\u4e0d\u4f1a\u91cd\u7f6e\u76d8\u7b26\u3002\u5982\u679c\u67d0\u90e8\u5206\u8def\u5f84\u5305\u542b\u76d8\u7b26\uff0c\u5219\u4f1a\u4e22\u5f03\u6240\u6709\u5148\u524d\u7684\u90e8\u5206\uff0c\u5e76\u91cd\u7f6e\u76d8\u7b26\u3002\u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u6bcf\u4e2a\u9a71\u52a8\u5668\u90fd\u6709\u4e00\u4e2a\u201c\u5f53\u524d\u76ee\u5f55\u201d\uff0c\u6240\u4ee5&nbsp;<code>os.path.join(\"c:\",&nbsp;\"foo\")<\/code>&nbsp;\u8868\u793a\u9a71\u52a8\u5668&nbsp;<code>C:<\/code>&nbsp;\u4e0a\u5f53\u524d\u76ee\u5f55\u7684\u76f8\u5bf9\u8def\u5f84 (<code>c:foo<\/code>)\uff0c\u800c\u4e0d\u662f&nbsp;<code>c:\\foo<\/code>\u3002<\/p>\n\n\n\n<p><em>\u5728 3.6 \u7248\u66f4\u6539:&nbsp;<\/em>\u63a5\u53d7\u4e00\u4e2a&nbsp;<a href=\"https:\/\/docs.python.org\/zh-cn\/3.8\/glossary.html#term-path-like-object\" target=\"_blank\" rel=\"noopener\">\u7c7b\u8def\u5f84\u5bf9\u8c61<\/a>&nbsp;\u7528\u4e8e&nbsp;<em>path<\/em>&nbsp;\u548c&nbsp;<em>paths<\/em>&nbsp;\u3002<\/p>\n\n\n\n<p style=\"font-size:24px\"><strong>P8:Tensorboard\u7684\u4f7f\u7528<\/strong><\/p>\n\n\n\n<p>add_scalar() \u753b\u56fe<\/p>\n\n\n\n<p>python\u6e90\u7801\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from torch.utils.tensorboard import SummaryWriter\n\nwriter = SummaryWriter(\"logs\") #\u6784\u9020\u51fd\u6570\u6307\u5b9aSummaryWriter\u7684\u50a8\u5b58\u6587\u4ef6\u5939\n\nfor i in range(100):\n    writer.add_scalar(\"y = 2x\", 2*i, i) #\u5c06\u70b9\u4e00\u4e2a\u4e2a\u6807\u4e0a\u53bb\u5230\"y=2x\"\u8fd9\u4e2a\u6807\u9898(tag)\u7684\u56fe\u91cc\u9762\n\nwriter.close()<\/code><\/pre>\n\n\n\n<p>\u7ec8\u7aef\u547d\u4ee4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tensorboard --logdir=logs --port=6007 #port\u9ed8\u8ba4\u7aef\u53e3\u4e3a6006<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u672c\u96c6get:<\/strong><\/p>\n\n\n\n<p>(1)\u5728Pycharm\u91cc\u9762\uff0c\u6309\u4f4fctrl\u952e\u540e\uff0c\u9f20\u6807\u70b9\u76f8\u5e94\u7684\u5730\u65b9\uff0c\u4f1a\u5f39\u51fa\u5e2e\u52a9\u6587\u6863<\/p>\n\n\n\n<p>(2) writer.add_scalar(&#8220;y = 2x&#8221;, 2*i, i)  #\u753b\u70b9\u7684\u65f6\u5019 2*i\u662fy i\u662fx<\/p>\n\n\n\n<p>(3)\u82e5\u60f3\u753b\u4e0d\u540c\u7684\u56fe\uff0c\u4e00\u53ef\u4ee5\u6307\u5b9a\u4e0d\u540c\u7684logdir\u6587\u4ef6\u5939\uff0c\u4e8c\u53ef\u4ee5\u8be5\u8868\u6807\u91cf\u56fe\u7684tag\u56fe\u540d\uff0c\u82e5\u90fd\u4e0d\u6539\u53d8\uff0c\u5219\u4f1a\u5728\u539f\u6765\u90a3\u91cc\u52a0\u4e0a\u65b0\u753b\u7684\u70b9\uff0c\u9020\u6210\u9519\u8bef\uff0c\u6b64\u65f6\u53ef\u4ee5\u5220\u6389logdir\u91cc\u9762\u7684\u6570\u636e\u91cd\u65b0\u753b\u56fe\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"492\" height=\"322\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-1.png\" alt=\"\" class=\"wp-image-85\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-1.png 492w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-1-300x196.png 300w\" sizes=\"(max-width: 492px) 100vw, 492px\" \/><figcaption>\u56fe2\u3001\u76f8\u540ctag\u753b\u56fe\u6548\u679c<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"529\" height=\"256\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-2.png\" alt=\"\" class=\"wp-image-86\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-2.png 529w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-2-300x145.png 300w\" sizes=\"(max-width: 529px) 100vw, 529px\" \/><figcaption>\u56fe3\u3001\u9879\u76eelogdir\u6587\u4ef6\u5939<\/figcaption><\/figure>\n\n\n\n<p>add_image() \u52a0\u56fe\u7247\u4e0a\u53bb<\/p>\n\n\n\n<p>python\u6e90\u7801\u5982\u4e0b:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from torch.utils.tensorboard import SummaryWriter\nfrom PIL import Image\nimport numpy as np\nwriter = SummaryWriter(\"logs\")\n\nimage_path = \"data\/train\/ants_image\/424119020_6d57481dab.jpg\"\nimg_PIL = Image.open(image_path)\nimg_array = np.array(img_PIL)\nprint(img_array.shape)\n\nwriter.add_image(\"test\", img_array, 100, dataformats=\"HWC\")\n\nwriter.close()<\/code><\/pre>\n\n\n\n<p>add_image()\u5e2e\u52a9\u6587\u6863\u90e8\u5206\u5982\u4e0b: #add_image()\u5e2e\u52a9\u6587\u6863\u94fe\u63a5<a href=\"https:\/\/pastebin.ubuntu.com\/p\/TfnRPSjq3T\/\" target=\"_blank\" rel=\"noopener\">Ubuntu Pastebin<\/a><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>Args:\n    tag (string): Data identifier\n    img_tensor (torch.Tensor, numpy.array, or string\/blobname): Image data\n    global_step (int): Global step value to record\n    walltime (float): Optional override default walltime (time.time())\n      seconds after epoch of event<\/em><\/pre>\n\n\n\n<p>tag \u8fd8\u662f\u6211\u4eec\u7684\u8868\u793a\u547d\u540d<\/p>\n\n\n\n<p>img_tensor \u652f\u6301\u7684\u683c\u5f0f\u6709 <em>(torch.Tensor, numpy.array, or string\/blobname)<\/em> <\/p>\n\n\n\n<p>global_step \u662f\u56fe\u7247\u6253\u6807\u7684\u987a\u5e8f\u53f7\uff0c\u6548\u679c\u5982\u56fe4<\/p>\n\n\n\n<p>walltime \u6682\u65f6\u770b\u4e0d\u61c2\u662f\u5e72\u561b\u7684<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"655\" height=\"449\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-3.png\" alt=\"\" class=\"wp-image-90\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-3.png 655w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-3-300x206.png 300w\" sizes=\"(max-width: 655px) 100vw, 655px\" \/><figcaption>\u56fe4<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>Shape:<br><\/em><em>    img_tensor: Default is :math:`(3, H, W)`. You can use ``torchvision.utils.make_grid()`` to<br><\/em><em>    convert a batch of tensor into 3xHxW format or call ``add_images`` and let us do the job.<br><\/em><em>    Tensor with :math:`(1, H, W)`, :math:`(H, W)`, :math:`(H, W, 3)` is also suitable as long as<br><\/em><em>    corresponding ``dataformats`` argument is passed, e.g. ``CHW``, ``HWC``, ``HW``.<\/em><\/pre>\n\n\n\n<p>img_tensor\u9ed8\u8ba4\u5904\u7406\u7684\u77e9\u9635\u6570\u636e\u7ed3\u6784\u4e3a(\u901a\u9053C\u3001\u9ad8\u5ea6H\u3001\u5bbd\u5ea6W)\uff0c\u800c\u6211\u4eec\u4f7f\u7528PIL Image\u6784\u9020\u7684numpy array\u7684\u7ed3\u6784\u662f(HWC)\uff0c\u5728\u540e\u9762\u8981\u52a0\u4e0a <code>dataformats=\"HWC\"<\/code>  \u8bf4\u660e\uff0c\u5e2e\u52a9\u6587\u6863\u4e2d\u4e5f\u7ed9\u6211\u4eec\u5217\u51fa\u4e86\u6837\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>Examples::<br><\/em><em><br><\/em><em>    from torch.utils.tensorboard import SummaryWriter<br><\/em><em>    import numpy as np<br><\/em><em>    img = np.zeros((3, 100, 100))<br><\/em><em>    img[0] = np.arange(0, 10000).reshape(100, 100) \/ 10000<br><\/em><em>    img[1] = 1 - np.arange(0, 10000).reshape(100, 100) \/ 10000<br><\/em><em><br><\/em><em>    img_HWC = np.zeros((100, 100, 3))<br><\/em><em>    img_HWC[:, :, 0] = np.arange(0, 10000).reshape(100, 100) \/ 10000<br><\/em><em>    img_HWC[:, :, 1] = 1 - np.arange(0, 10000).reshape(100, 100) \/ 10000<br><\/em><em><br><\/em><em>    writer = SummaryWriter()<br><\/em><em>    writer.add_image('my_image', img, 0)<br><\/em><em><br><\/em><em>    # If you have non-default dimension setting, set the dataformats argument.<br><\/em><em>    writer.add_image('my_image_HWC', img_HWC, 0, dataformats='HWC')<br><\/em><em>    writer.close()<\/em><\/pre>\n\n\n\n<p style=\"font-size:24px\"><strong>P9:Transforms\u7684\u4f7f\u7528<\/strong><\/p>\n\n\n\n<p>Transforms.py\u6e90\u7801\u53ef\u89c1<a href=\"https:\/\/pastebin.ubuntu.com\/p\/Kvky8Py23n\/\" target=\"_blank\" rel=\"noreferrer noopener\">Ubuntu Pastebin<\/a><\/p>\n\n\n\n<p>\u5728\u91cc\u9762\uff0c\u6211\u4eec\u770bclass ToTensor<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class ToTensor:<br>    <em>\"\"\"Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor. This transform does not support torchscript.<br><\/em><em><br><\/em><em>    Converts a PIL Image or numpy.ndarray (H x W x C) in the range<br><\/em><em>    [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0]<br><\/em><em>    if the PIL Image belongs to one of the modes (L, LA, P, I, F, RGB, YCbCr, RGBA, CMYK, 1)<br><\/em><em>    or if the numpy.ndarray has dtype = np.uint8<br><\/em><em><br><\/em><em>    In the other cases, tensors are returned without scaling.<br><\/em><em><br><\/em><em>    .. note::<br><\/em><em>        Because the input image is scaled to [0.0, 1.0], this transformation should not be used when<br><\/em><em>        transforming target image masks. See the `references`_ for implementing the transforms for image masks.<br><\/em><em><br><\/em><em>    .. _references: https:\/\/github.com\/pytorch\/vision\/tree\/master\/references\/segmentation<br><\/em><em>    \"\"\"<br><\/em><em><br><\/em><em>    <\/em>def __call__(self, pic):<br>        <em>\"\"\"<br><\/em><em>        Args:<br><\/em><em>            pic (PIL Image or numpy.ndarray): Image to be converted to tensor.<br><\/em><em><br><\/em><em>        Returns:<br><\/em><em>            Tensor: Converted image.<br><\/em><em>        \"\"\"<br><\/em><em>        <\/em>return F.to_tensor(pic)<br><br>    def __repr__(self):<br>        return self.__class__.__name__ + '()'<\/pre>\n\n\n\n<p>\u5b83\u53ef\u4ee5\u628aPIL.Image\u6216numpy.ndarray\u7c7b\u578b\u8f6c\u6362\u4e3atensor\u7c7b\u578b<\/p>\n\n\n\n<p>\u800c\u8fd9\u662f\u901a\u8fc7\u5b83\u7684\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684__call__\u51fd\u6570\u5b9e\u73b0\u7684<\/p>\n\n\n\n<p>\u770b\u4ee5\u4e0b\u6e90\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from PIL import Image\nfrom torch.utils.tensorboard import SummaryWriter\nfrom torchvision import transforms\n\n\nimg_path = \"data\/train\/ants_image\/0013035.jpg\"\nimg = Image.open(img_path)\nwriter = SummaryWriter(\"logs\")\n\nprint(img)\n\ntensor_trans = transforms.ToTensor()\nprint(type(tensor_trans))\ntensor_img = tensor_trans(img)\n\nprint(type(tensor_img))\nwriter.add_image(\"tensor_img\", tensor_img, 0)\nwriter.close()<\/code><\/pre>\n\n\n\n<p>\u7ec8\u7aef\u8f93\u51fa\u662f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=768x512 at 0x2D304BC20A0&gt;\n&lt;class 'torchvision.transforms.transforms.ToTensor'&gt;\n&lt;class 'torch.Tensor'&gt;<\/code><\/pre>\n\n\n\n<p>\u53ef\u89c1ToTensor\u662f\u4e00\u4e2a\u7c7b &lt;class &#8216;torchvision.transforms.transforms.ToTensor&#8217;&gt; <\/p>\n\n\n\n<p>\u800ctensorboard\u5219\u5982\u56fe5<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"748\" height=\"529\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-4.png\" alt=\"\" class=\"wp-image-100\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-4.png 748w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-4-300x212.png 300w\" sizes=\"(max-width: 748px) 100vw, 748px\" \/><figcaption>\u56fe5<\/figcaption><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u672c\u96c6get:<\/strong><\/p>\n\n\n\n<p>(1)__call__()\uff0c\u53ef\u4ee5\u628a\u5b9e\u4f8b\u5316\u5bf9\u8c61\u50cf\u51fd\u6570\u4e00\u6837\u4f7f\u7528\uff0ceg: \u5bf9\u8c61\u540d()<\/p>\n\n\n\n<p>(2)__rept__()\uff0c\u7528\u4e8etype(\u5bf9\u8c61\u540d)\u7684\u65f6\u5019\u8fd4\u56de\u3002<\/p>\n\n\n\n<p>(3)pycharm\u5feb\u6377\u952e: ctrl + shift + F10 \u8fd0\u884c\u73b0\u5728\u7f16\u8f91\u7684py\u811a\u672c\u3002<\/p>\n\n\n\n<p>(4)pycharm\u5feb\u6377\u952e: alt + 7 \u663e\u793a\u6e90\u7801\u7ed3\u6784\u5982\u56fe6\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"512\" height=\"619\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-5.png\" alt=\"\" class=\"wp-image-101\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-5.png 512w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-5-248x300.png 248w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><figcaption>\u56fe6<\/figcaption><\/figure>\n\n\n\n<p style=\"font-size:24px\"><strong>P10:\u5e38\u89c1\u7684transforms<\/strong><\/p>\n\n\n\n<p>\u603b\u6e90\u7801 <a href=\"https:\/\/pastebin.ubuntu.com\/p\/smssBn5C5V\/\" target=\"_blank\" rel=\"noreferrer noopener\">Ubuntu Pastebin<\/a><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Normalize\u5f52\u4e00\u5316\u5904\u7406<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Normalize\ntrans_nor = transforms.Normalize(&#91;0.5,0.5,0.5],&#91;0.5,0.5,0.5])\ntrans_nor_2 = transforms.Normalize(&#91;1,2,3],&#91;3,2,1])\n#result = (input - 0.5)\/0.5     input&#91;0, 1] result&#91;-1, -1]\n# Tensor -&gt; Tensor\ntensor_img_nor = trans_nor(tensor_img)\ntensor_img_nor_2 = trans_nor_2(tensor_img)\nwriter.add_image(\"Normalize\", tensor_img_nor, 1)\nwriter.add_image(\"Normalize\", tensor_img_nor_2, 2)<\/code><\/pre>\n\n\n\n<p>Normalize\u6784\u9020\u51fd\u6570\u4f20\u8fdb\u53bb\u7684\u4e24\u4e2a\u77e9\u9635\u4e2d\u76843\u4e2a\u5143\u7d20\u5bf9\u5e94\u7684\u662f\u5bf9Tensor\u4e2d3\u5c42\u77e9\u9635\u7684\u5904\u7406\u53c2\u6570<\/p>\n\n\n\n<p> #result = (input &#8211; 0.5)\/0.5     input[0, 1] result[-1, -1] <\/p>\n\n\n\n<p># Tensor -&gt; Tensor <\/p>\n\n\n\n<p>\u6548\u679c\u5982\u4e0b<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"502\" height=\"810\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-8.png\" alt=\"\" class=\"wp-image-115\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-8.png 502w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-8-186x300.png 186w\" sizes=\"(max-width: 502px) 100vw, 502px\" \/><figcaption>\u56fe7\u3001\u539fTensor\u56fe<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"519\" height=\"810\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-9.png\" alt=\"\" class=\"wp-image-116\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-9.png 519w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-9-192x300.png 192w\" sizes=\"(max-width: 519px) 100vw, 519px\" \/><figcaption>\u56fe8\u3001 trans_nor \u5904\u7406<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"516\" height=\"822\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-10.png\" alt=\"\" class=\"wp-image-117\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-10.png 516w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-10-188x300.png 188w\" sizes=\"(max-width: 516px) 100vw, 516px\" \/><figcaption>\u56fe9\u3001 trans_nor_2 \u5904\u7406<\/figcaption><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Resize\u6539\u53d8\u56fe\u7247size<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"555\" height=\"433\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-11.png\" alt=\"\" class=\"wp-image-118\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-11.png 555w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-11-300x234.png 300w\" sizes=\"(max-width: 555px) 100vw, 555px\" \/><figcaption>\u56fe10\u3001Resize((128,256))\u6548\u679c<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>#Resize\nprint(img.size)\ntrans_resize = transforms.Resize((128, 256)) #\u9ed8\u8ba4\u4f20\u53c2\u4e3a\u4e00\u4e2asize\ntrans_resize_test = transforms.Resize(128) #\u82e5\u4f7f\u7528\u4e00\u4e2a\u53c2\u6570\uff0c\u884c\u6570\u53d8\u4e3a\u53c2\u6570\uff0c\u5217\u6570\u7b49\u6bd4\u4f8b\u7f29\u653e\n# PIL.Image or torch.Tensor -&gt; PIL.Image or torch.Tensor\nimg_resize = trans_resize(img)\nimg_resize_test = trans_resize_test(img)\nprint(type(img_resize))\nimg_resize = tensor_trans(img_resize)\nimg_resize_test = tensor_trans(img_resize_test)\nprint(type(img_resize))\n#writer.add_image(\"Resize\", img, 0, dataformats='HWC')\nwriter.add_image(\"Resize\", img_resize, 0)\nwriter.add_image(\"Resize\", img_resize_test, 1)\nprint(img_resize.size)<\/code><\/pre>\n\n\n\n<p>Resize\u53ef\u4ee5PIL.Image or torch.Tensor -&gt; PIL.Image or torch.Tensor <\/p>\n\n\n\n<p>\u4f20\u53c2\u8fdb\u53bb\u82e5\u662f\u4e00\u4e2a\u6570\u503c\u7684\u8bdd\uff0c \u884c\u6570\u53d8\u4e3a\u53c2\u6570\uff0c\u5217\u6570\u7b49\u6bd4\u4f8b\u7f29\u653e\uff0c\u8bc1\u660e\u5982\u4e0b<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"375\" height=\"500\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/0.png\" alt=\"\" class=\"wp-image-119\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/0.png 375w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/0-225x300.png 225w\" sizes=\"(max-width: 375px) 100vw, 375px\" \/><figcaption>\u56fe11\u3001\u539f\u59cb\u56fe\u7247<\/figcaption><\/figure>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u67e5\u770b\u8fd9\u5f20\u56fe\u7247\u7684\u539f\u59cb\u5c3a\u5bf8<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"146\" height=\"159\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-13.png\" alt=\"\" class=\"wp-image-121\"\/><figcaption>\u56fe12<\/figcaption><\/figure>\n\n\n\n<p>\u800c\u7528\u5355\u53c2\u6570\u7684Resize\u53d8\u6362\u540e<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"128\" height=\"170\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/2.png\" alt=\"\" class=\"wp-image-122\"\/><figcaption>\u56fe13\u3001\u5355\u53c2\u6570Resize\u53d8\u6362\u540e<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"176\" height=\"155\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-14.png\" alt=\"\" class=\"wp-image-123\"\/><figcaption>\u56fe14\u3001\u53d8\u6362\u540e\u7684size<\/figcaption><\/figure>\n\n\n\n<p>\u6211\u4eec\u901a\u8fc7\u8ba1\u7b97 width\/height\uff0c\u53ef\u4ee5\u53d1\u73b0\uff0c\u539f\u59cb\u56fe\u7247\u548c\u53d8\u6362\u56fe\u7247\u7684\u5bbd\u9ad8\u6bd4\u90fd\u7ea6\u7b49\u4e8e1.3\uff0c\u53ef\u89c1\u63a8\u65ad\u6210\u7acb<\/p>\n\n\n\n<p style=\"font-size:24px\"><strong>Compose\u8f6c\u6362\u7c7b\u7ec4\u5408<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Compose\ntrans_compose = transforms.Compose(&#91;trans_resize, tensor_trans])\nimg_com = trans_compose(img)\nwriter.add_image(\"Compose\", img_com, 0)<\/code><\/pre>\n\n\n\n<p>Compose\u53ef\u4ee5\u5c06transforms\u91cc\u9762\u7684\u7c7b\u8fdb\u884c\u4e00\u4e2a\u7ec4\u5408\uff0c\u7528\u4e8e\u5bf9\u67d0\u5bf9\u8c61\u8fdb\u884c\u4e00\u4e9b\u5217\u7684\u5904\u7406<\/p>\n\n\n\n<p style=\"font-size:24px\"><strong>RandomCrop\u968f\u673a\u88c1\u526a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#RandomCrop\ntrans_ran = transforms.RandomCrop(256)\ntrans_compose_2 = transforms.Compose(&#91;trans_ran, tensor_trans])\nimg_path = \"desktop.png\"\nimg = Image.open(img_path)\nfor i in range(10):\n    img_ran = trans_compose_2(img)\n    writer.add_image(\"RandomCrop\", img_ran, i)<\/code><\/pre>\n\n\n\n<p>\u4f20\u53c2\u4e3asize\uff0c\u548cResize\u7c7b\u4f3c\uff0creturn\u4e3a\u56fe\u7247\u7684\u968f\u673a\u4e00\u4e2a\u88c1\u5207\u90e8\u5206<\/p>\n\n\n\n<p>PIL.Image or torch.Tensor -&gt; PIL.Image or torch.Tensor<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"609\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/desktop-1024x609.png\" alt=\"\" class=\"wp-image-124\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/desktop-1024x609.png 1024w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/desktop-300x178.png 300w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/desktop-768x457.png 768w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/desktop-1536x913.png 1536w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/desktop.png 1687w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>\u56fe15\u3001\u539f\u4f20\u5165img<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"537\" height=\"674\" src=\"http:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-15.png\" alt=\"\" class=\"wp-image-125\" srcset=\"https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-15.png 537w, https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-15-239x300.png 239w\" sizes=\"(max-width: 537px) 100vw, 537px\" \/><figcaption>\u56fe16\u3001\u968f\u673a\u88c1\u5207\u7684\u6548\u679c<\/figcaption><\/figure>\n\n\n\n<p>\u5b66\u4e60\u89c6\u9891:<a href=\"https:\/\/www.bilibili.com\/video\/BV1hE411t7RN?spm_id_from=333.999.0.0\" target=\"_blank\" rel=\"noreferrer noopener\">PyTorch\u6df1\u5ea6\u5b66\u4e60\u5feb\u901f\u5165\u95e8\u6559\u7a0b\uff08\u7edd\u5bf9\u901a\u4fd7\u6613\u61c2\uff01\uff09\u3010\u5c0f\u571f\u5806\u3011_\u54d4\u54e9\u54d4\u54e9_bilibili<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5185\u5bb9\u5305\u62ec: Dataset\u7c7b\u3001Tensorbo&hellip;<\/p>\n","protected":false},"author":1,"featured_media":90,"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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[10,12,2],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/scutvk.cn\/wp-content\/uploads\/2021\/10\/image-3.png","_links":{"self":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts\/78"}],"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=78"}],"version-history":[{"count":17,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":166,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions\/166"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=\/wp\/v2\/media\/90"}],"wp:attachment":[{"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scutvk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}