{"id":185,"date":"2025-06-27T14:05:38","date_gmt":"2025-06-27T06:05:38","guid":{"rendered":"https:\/\/mianka.xyz\/?p=185"},"modified":"2025-06-27T14:10:16","modified_gmt":"2025-06-27T06:10:16","slug":"pytorch","status":"publish","type":"post","link":"https:\/\/www.mianka.xyz\/?p=185","title":{"rendered":"\u6c49\u5b57\uff0c\u56fe\u7247\uff0c\u97f3\u9891\uff0c\u89c6\u9891\u600e\u4e48\u8f6c\u5316\u6210pytorch\u4e2d\u7684\u5f20\u91cf"},"content":{"rendered":"<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e00\u3001\u6c49\u5b57\uff08\u6587\u672c\uff09\u8f6c\u6362\u4e3a\u5f20\u91cf<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u6587\u672c\u6570\u636e\u9700\u8981\u7ecf\u8fc7\u5206\u8bcd\u3001\u7f16\u7801\u548c\u586b\u5145\u7b49\u6b65\u9aa4\u624d\u80fd\u8f6c\u6362\u4e3a\u5f20\u91cf\u3002<\/p>\n<h4 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 18px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">1.&nbsp;<span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none;\">\u5206\u8bcd\u4e0e\u7f16\u7801<\/span><\/h4>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4f7f\u7528\u5206\u8bcd\u5de5\u5177\uff08\u5982jieba\u3001spaCy\uff09\u5c06\u53e5\u5b50\u62c6\u5206\u4e3a\u5355\u8bcd\u6216\u5b50\u8bcd\uff0c\u518d\u901a\u8fc7\u8bcd\u8868\u6620\u5c04\u4e3a\u6570\u5b57ID\u3002<\/p>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;jieba\nfrom&nbsp;torchtext.vocab&nbsp;import&nbsp;build_vocab_from_iterator\nimport&nbsp;torch\n\n#&nbsp;\u793a\u4f8b\u6587\u672c\ntexts&nbsp;=&nbsp;[&quot;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u53e5\u5b50&quot;,&nbsp;&quot;\u8fd9\u662f\u53e6\u4e00\u4e2a\u4f8b\u5b50&quot;]\n\n#&nbsp;\u5206\u8bcd\ntokenized_texts&nbsp;=&nbsp;[list(jieba.cut(text))&nbsp;for&nbsp;text&nbsp;in&nbsp;texts]\n#&nbsp;\u7ed3\u679c\uff1a[[&#39;\u8fd9\u662f&#39;,&nbsp;&#39;\u4e00\u4e2a&#39;,&nbsp;&#39;\u793a\u4f8b&#39;,&nbsp;&#39;\u53e5\u5b50&#39;],&nbsp;[&#39;\u8fd9\u662f&#39;,&nbsp;&#39;\u53e6\u4e00\u4e2a&#39;,&nbsp;&#39;\u4f8b\u5b50&#39;]]\n\n#&nbsp;\u6784\u5efa\u8bcd\u8868\nvocab&nbsp;=&nbsp;build_vocab_from_iterator(tokenized_texts,&nbsp;specials=[&quot;&lt;unk&gt;&quot;])\nvocab.set_default_index(vocab[&quot;&lt;unk&gt;&quot;])&nbsp;&nbsp;#&nbsp;\u672a\u77e5\u8bcd\u5904\u7406\n\n#&nbsp;\u6587\u672c\u8f6cID\u5e8f\u5217\ntext_ids&nbsp;=&nbsp;[vocab(tokens)&nbsp;for&nbsp;tokens&nbsp;in&nbsp;tokenized_texts]\n#&nbsp;\u7ed3\u679c\uff1a[[1,&nbsp;2,&nbsp;3,&nbsp;4],&nbsp;[1,&nbsp;5,&nbsp;6]]<\/pre>\n<h4 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 18px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">2.&nbsp;<span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none;\">\u586b\u5145\u5e8f\u5217<\/span><\/h4>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u7edf\u4e00\u5e8f\u5217\u957f\u5ea6\u4ee5\u4fbf\u6279\u91cf\u5904\u7406\u3002<\/p>\n<pre class=\"prism-highlight prism-language-python\">from&nbsp;torch.nn.utils.rnn&nbsp;import&nbsp;pad_sequence\n\n#&nbsp;\u8f6c\u6362\u4e3a\u5f20\u91cf\u5217\u8868\ntensor_list&nbsp;=&nbsp;[torch.tensor(ids)&nbsp;for&nbsp;ids&nbsp;in&nbsp;text_ids]\n\n#&nbsp;\u586b\u5145\u5e8f\u5217\uff08\u957f\u5ea6\u8bbe\u4e3a\u6700\u957f\u5e8f\u5217\uff09\npadded_tensors&nbsp;=&nbsp;pad_sequence(tensor_list,&nbsp;batch_first=True,&nbsp;padding_value=0)\n#&nbsp;\u7ed3\u679c\uff1atensor([[1,&nbsp;2,&nbsp;3,&nbsp;4],&nbsp;[1,&nbsp;5,&nbsp;6,&nbsp;0]])<\/pre>\n<hr style=\"box-sizing: content-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 20px 0px; scrollbar-width: none; height: 0px; overflow: visible; border-right: 0px; border-bottom: 0px; border-left: 0px; border-image: initial; border-top-style: solid; border-top-color: rgb(230, 230, 235); color: rgb(6, 7, 31); font-size: 15px; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"\/>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e8c\u3001\u56fe\u7247\u8f6c\u6362\u4e3a\u5f20\u91cf<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4f7f\u7528PyTorch\u5185\u7f6e\u7684<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">torchvision.transforms<\/code>\u5904\u7406\u56fe\u50cf\u6570\u636e\u3002<\/p>\n<ol class=\" list-paddingleft-2\" style=\"list-style-type: decimal;\">\n<li>\n<h4 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 18px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">&nbsp;<span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none;\">\u52a0\u8f7d\u5e76\u8f6c\u6362\u56fe\u50cf<\/span><\/h4>\n<\/li>\n<\/ol>\n<pre class=\"prism-highlight prism-language-python\">from&nbsp;PIL&nbsp;import&nbsp;Image\nimport&nbsp;torchvision.transforms&nbsp;as&nbsp;transforms\n\n#&nbsp;\u52a0\u8f7d\u56fe\u50cf\nimage_path&nbsp;=&nbsp;&quot;example.jpg&quot;\nimage&nbsp;=&nbsp;Image.open(image_path)\n\n#&nbsp;\u5b9a\u4e49\u8f6c\u6362\u7ba1\u9053\ntransform&nbsp;=&nbsp;transforms.Compose([\n&nbsp;&nbsp;&nbsp;&nbsp;transforms.Resize((256,&nbsp;256)),&nbsp;&nbsp;#&nbsp;\u8c03\u6574\u5927\u5c0f\n&nbsp;&nbsp;&nbsp;&nbsp;transforms.ToTensor(),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u8f6c\u6362\u4e3a\u5f20\u91cf\uff08\u8303\u56f4[0,1]\uff09\n&nbsp;&nbsp;&nbsp;&nbsp;transforms.Normalize(mean=[0.485,&nbsp;0.456,&nbsp;0.406],&nbsp;std=[0.229,&nbsp;0.224,&nbsp;0.225])&nbsp;&nbsp;#&nbsp;\u6807\u51c6\u5316\n])\n\n#&nbsp;\u5e94\u7528\u8f6c\u6362\nimage_tensor&nbsp;=&nbsp;transform(image)&nbsp;&nbsp;#&nbsp;\u5f62\u72b6\uff1a[C,&nbsp;H,&nbsp;W]<\/pre>\n<h4 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 18px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">2.&nbsp;<span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none;\">\u6279\u91cf\u5904\u7406<\/span><\/h4>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4f7f\u7528<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">DataLoader<\/code>\u52a0\u8f7d\u56fe\u50cf\u6570\u636e\u96c6\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">from&nbsp;torchvision.datasets&nbsp;import&nbsp;ImageFolder\nfrom&nbsp;torch.utils.data&nbsp;import&nbsp;DataLoader\n\ndataset&nbsp;=&nbsp;ImageFolder(root=&quot;path\/to\/images&quot;,&nbsp;transform=transform)\ndataloader&nbsp;=&nbsp;DataLoader(dataset,&nbsp;batch_size=32,&nbsp;shuffle=True)\n\nfor&nbsp;batch&nbsp;in&nbsp;dataloader:\n&nbsp;&nbsp;&nbsp;&nbsp;images,&nbsp;labels&nbsp;=&nbsp;batch&nbsp;&nbsp;#&nbsp;images\u5f62\u72b6\uff1a[B,&nbsp;C,&nbsp;H,&nbsp;W]<\/pre>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"><span style=\"color: rgba(0, 0, 0, 0.85); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Helvetica Neue&quot;, Arial, &quot;Noto Sans&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Noto Color Emoji&quot;; white-space-collapse: preserve;\"><\/span><\/p>\n<hr style=\"box-sizing: content-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 20px 0px; scrollbar-width: none; height: 0px; overflow: visible; border-right: 0px; border-bottom: 0px; border-left: 0px; border-image: initial; border-top-style: solid; border-top-color: rgb(230, 230, 235); color: rgb(6, 7, 31); font-size: 15px; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"\/>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e09\u3001\u97f3\u9891\u8f6c\u6362\u4e3a\u5f20\u91cf<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u97f3\u9891\u9700\u5148\u8f6c\u6362\u4e3a\u9891\u8c31\u56fe\uff08\u5982MFCC\u3001Mel\u9891\u8c31\uff09\u518d\u8f6c\u4e3a\u5f20\u91cf\u3002<\/p>\n<ol class=\" list-paddingleft-2\" style=\"list-style-type: decimal;\">\n<li>\n<h4 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 18px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">&nbsp;<span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none;\">\u4f7f\u7528torchaudio\u5904\u7406<\/span><\/h4>\n<\/li>\n<\/ol>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;torchaudio\nimport&nbsp;torchaudio.transforms&nbsp;as&nbsp;T\n\n#&nbsp;\u52a0\u8f7d\u97f3\u9891\u6587\u4ef6\nwaveform,&nbsp;sample_rate&nbsp;=&nbsp;torchaudio.load(&quot;example.wav&quot;)&nbsp;&nbsp;#&nbsp;\u5f62\u72b6\uff1a[C,&nbsp;T]\n\n#&nbsp;\u8f6c\u6362\u4e3aMel\u9891\u8c31\nmel_spectrogram&nbsp;=&nbsp;T.MelSpectrogram(sample_rate=sample_rate)(waveform)\n#&nbsp;\u7ed3\u679c\u5f62\u72b6\uff1a[F,&nbsp;T]\uff08\u9891\u7387\u00d7\u65f6\u95f4\uff09\n\n#&nbsp;\u6807\u51c6\u5316\u5e76\u6dfb\u52a0\u6279\u6b21\u7ef4\u5ea6\naudio_tensor&nbsp;=&nbsp;mel_spectrogram.log2()&nbsp;&nbsp;#&nbsp;\u5bf9\u6570\u7f29\u653e\naudio_tensor&nbsp;=&nbsp;audio_tensor.unsqueeze(0)&nbsp;&nbsp;#&nbsp;\u5f62\u72b6\uff1a[1,&nbsp;F,&nbsp;T]<\/pre>\n<h4 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 18px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">2.&nbsp;<span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none;\">\u6279\u91cf\u5904\u7406<\/span><\/h4>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u81ea\u5b9a\u4e49<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">Dataset<\/code>\u7c7b\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">from&nbsp;torch.utils.data&nbsp;import&nbsp;Dataset\n\nclass&nbsp;AudioDataset(Dataset):\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__init__(self,&nbsp;file_list):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.file_list&nbsp;=&nbsp;file_list\n\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__getitem__(self,&nbsp;idx):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;waveform,&nbsp;_&nbsp;=&nbsp;torchaudio.load(self.file_list[idx])\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mel&nbsp;=&nbsp;T.MelSpectrogram()(waveform).log2()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;mel.squeeze(0)&nbsp;&nbsp;#&nbsp;\u5f62\u72b6\uff1a[F,&nbsp;T]\n\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__len__(self):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;len(self.file_list)<\/pre>\n<hr style=\"box-sizing: content-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 20px 0px; scrollbar-width: none; height: 0px; overflow: visible; border-right: 0px; border-bottom: 0px; border-left: 0px; border-image: initial; border-top-style: solid; border-top-color: rgb(230, 230, 235); color: rgb(6, 7, 31); font-size: 15px; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"\/>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u56db\u3001\u89c6\u9891\u8f6c\u6362\u4e3a\u5f20\u91cf<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u89c6\u9891\u5904\u7406\u9700\u9010\u5e27\u63d0\u53d6\u5e76\u8f6c\u6362\u4e3a\u5f20\u91cf\u5e8f\u5217\u3002<\/p>\n<h4 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 18px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">1.&nbsp;<span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none;\">\u9010\u5e27\u5904\u7406<\/span><\/h4>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4f7f\u7528OpenCV\u63d0\u53d6\u89c6\u9891\u5e27\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;cv2\nimport&nbsp;numpy&nbsp;as&nbsp;np\n\ndef&nbsp;video_to_tensor(video_path,&nbsp;frame_interval=1):\n&nbsp;&nbsp;&nbsp;&nbsp;cap&nbsp;=&nbsp;cv2.VideoCapture(video_path)\n&nbsp;&nbsp;&nbsp;&nbsp;frames&nbsp;=&nbsp;[]\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;cap.isOpened():\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret,&nbsp;frame&nbsp;=&nbsp;cap.read()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;not&nbsp;ret:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;len(frames)&nbsp;%&nbsp;frame_interval&nbsp;==&nbsp;0:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u8f6c\u6362\u4e3aRGB\u5e76\u5f52\u4e00\u5316\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame&nbsp;=&nbsp;cv2.cvtColor(frame,&nbsp;cv2.COLOR_BGR2RGB)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame&nbsp;=&nbsp;frame.astype(np.float32)&nbsp;\/&nbsp;255.0\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frames.append(torch.from_numpy(frame).permute(2,&nbsp;0,&nbsp;1))&nbsp;&nbsp;#&nbsp;\u5f62\u72b6\uff1a[C,&nbsp;H,&nbsp;W]\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;cap.release()\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;torch.stack(frames)&nbsp;&nbsp;#&nbsp;\u5f62\u72b6\uff1a[T,&nbsp;C,&nbsp;H,&nbsp;W]\n\nvideo_tensor&nbsp;=&nbsp;video_to_tensor(&quot;example.mp4&quot;)<\/pre>\n<h4 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 18px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">2.&nbsp;<span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none;\">\u4f7f\u7528\u9884\u8bad\u7ec3\u6a21\u578b\u5904\u7406<\/span><\/h4>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4f7f\u7528<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">torchvision.models.video<\/code>\u5904\u7406\u89c6\u9891\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">from&nbsp;torchvision.models.video&nbsp;import&nbsp;r2plus1d_18\n\nmodel&nbsp;=&nbsp;r2plus1d_18(pretrained=True)\n#&nbsp;\u8f93\u5165\u5f62\u72b6\uff1a[B,&nbsp;C,&nbsp;T,&nbsp;H,&nbsp;W]\n#&nbsp;\u9700\u5148\u5bf9\u89c6\u9891\u8fdb\u884c\u9884\u5904\u7406\uff08\u8c03\u6574\u5927\u5c0f\u3001\u5f52\u4e00\u5316\u7b49\uff09<\/pre>\n<hr style=\"box-sizing: content-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 20px 0px; scrollbar-width: none; height: 0px; overflow: visible; border-right: 0px; border-bottom: 0px; border-left: 0px; border-image: initial; border-top-style: solid; border-top-color: rgb(230, 230, 235); color: rgb(6, 7, 31); font-size: 15px; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"\/>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e94\u3001\u5173\u952e\u6ce8\u610f\u4e8b\u9879<\/h3>\n<ol style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u6570\u636e\u6807\u51c6\u5316<\/span>\uff1a\u56fe\u50cf\u548c\u97f3\u9891\u901a\u5e38\u9700\u8981\u6807\u51c6\u5316\uff08\u5982\u51cf\u5747\u503c\u3001\u9664\u65b9\u5dee\uff09\u3002<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u6279\u6b21\u5904\u7406<\/span>\uff1a\u4f7f\u7528<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">DataLoader<\/code>\u5b9e\u73b0\u6279\u91cf\u52a0\u8f7d\u548c\u5e76\u884c\u5904\u7406\u3002<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u5185\u5b58\u7ba1\u7406<\/span>\uff1a\u89c6\u9891\u548c\u97f3\u9891\u6570\u636e\u53ef\u80fd\u5360\u7528\u5927\u91cf\u5185\u5b58\uff0c\u9700\u5408\u7406\u8bbe\u7f6e\u6279\u6b21\u5927\u5c0f\u6216\u4f7f\u7528\u6d41\u5f0f\u5904\u7406\u3002<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u591a\u6a21\u6001\u878d\u5408<\/span>\uff1a\u82e5\u9700\u878d\u5408\u4e0d\u540c\u6570\u636e\u7c7b\u578b\uff08\u5982\u6587\u672c+\u56fe\u50cf\uff09\uff0c\u9700\u8bbe\u8ba1\u5bf9\u9f50\u673a\u5236\uff08\u5982\u65f6\u95f4\u6233\u5bf9\u9f50\u6216\u6ce8\u610f\u529b\u673a\u5236\uff09\u3002<\/p>\n<\/li>\n<\/ol>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"><!--autointro--><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-185","post","type-post","status-publish","format-standard","hentry","category-pythonbiji"],"_links":{"self":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=185"}],"version-history":[{"count":0,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/185\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}