【读书】《计算机视觉之PyTorch数字图像处理》2.Pytorch环境搭建
<p>我们先跳过第一章的计算机视觉与数字图像概述,直接从环境搭建这一节章开始学习。</p><p>在这一章中主要介绍了从零开始搭建Pytorch开发环境,包括:</p>
<ol start="1">
<li data-list="number">
<p>介绍python的下载和安装,并介绍了如何运行Python程序。</p>
</li>
<li data-list="number">
<p>介绍CUDA的用途,并介绍CUDA的下载和安装。</p>
</li>
<li data-list="number">
<p>介绍Python第三方库的作用,并介绍Python第三方库的安装方法</p>
</li>
<li data-list="number">
<p>介绍如何正确选择Pytorch的版本,以及Pytorch的安装方法。</p>
</li>
<li data-list="number">
<p>最后还介绍了可视化工具Visdom和集成开发环境Spyder的安装和使用。</p>
</li>
</ol>
<p> </p>
<p>文中对于Python、CUDA的背景和发展历程介绍的很详细,使我又增加了许多新的知识。并且对于python的安装基本上说是手把手,按照书中的步骤来基本上是没有问题的。</p>
<p> </p>
<p>此外,pip是python的一个第三方库,也是python内置的第三方库管理和维护工具,大部分python的第三方库都能够使用命令pip install xxx进行安装,xxx表示需要安装的第三方库名。以下是相关常用命令:</p>
<ol start="1">
<li data-list="number">
<p>安装库:pip install xxx</p>
</li>
<li data-list="number">
<p>升级库:pip install --upgrade xxx</p>
</li>
<li data-list="number">
<p>查看已安装的库:pip list</p>
</li>
<li data-list="number">
<p>查看库信息:pip show xxx</p>
</li>
<li data-list="number">
<p>卸载库:pip uninstall xxx</p>
</li>
<li data-list="number">
<p>下载指定库到当前目录:pip download xxx</p>
</li>
</ol>
<p> </p>
<p>此外在第三方库的安装中,作者还详细列举了与图像处理相关的库如下表所示:</p>
<p><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css"><!--br {mso-data-placement:same-cell;}--> td {white-space:nowrap;border:1px solid #dee0e3;font-size:10pt;font-style:normal;font-weight:normal;vertical-align:middle;word-break:normal;word-wrap:normal;}
</style>
</p>
<table>
<colgroup>
<col width="105" />
<col width="105" />
<col width="105" />
</colgroup>
<tbody>
<tr height="27">
<td>第三方库名</td>
<td>功能简介</td>
<td>官方网址及其文档</td>
</tr>
<tr height="42">
<td>gdal</td>
<td>支持最广泛图像(栅格)格式,主要用于图像的存取和图像文件格式的转换。</td>
<td _blank="" data-sheet-value="[{"type":"url","link":"<a target=">https://gdal.org/","text":"<a href="https://gdal.org/"" target="_blank">https://gdal.org/"</a>}]"><a href="https://gdal.org/">https://gdal.org/</a></td>
</tr>
<tr height="53">
<td>pillow</td>
<td data-sheet-value="[{"type":"text","text":"之前称","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"PIL","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"库,是最常用的图像处理库,提供了常用图像格式的读取以及较丰富的图像处理功能。","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}}]">之前称PIL库,是最常用的图像处理库,提供了常用图像格式的读取以及较丰富的图像处理功能。</td>
<td data-sheet-value="[{"type":"url","link":"https://pillow-cn.readthedocs.io/","text":"https://pillow-cn.readthedocs.io"}]"><a href="https://pillow-cn.readthedocs.io/">https://pillow-cn.readthedocs.io</a></td>
</tr>
<tr height="53">
<td>opencv-python</td>
<td data-sheet-value="[{"type":"text","text":"opencv","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"的","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"python","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"接口,提供了对","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"opencv","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"的完全支持,拥有强大的图像处理功能。","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}}]">opencv的python接口,提供了对opencv的完全支持,拥有强大的图像处理功能。</td>
<td data-sheet-value="[{"type":"url","link":"https://github.com/skvark/opencv-python","text":"https://github.com/skvark/opencv-python"}]"><a href="https://github.com/skvark/opencv-python">https://github.com/skvark/opencv-python</a></td>
</tr>
<tr height="31">
<td>simplecv</td>
<td data-sheet-value="[{"type":"text","text":"在易用性上较","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"opencv","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"好,功能与","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"opencv","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"类似。","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}}]">在易用性上较opencv好,功能与opencv类似。</td>
<td data-sheet-value="[{"type":"url","link":"http://simplecv.org/","text":"http://simplecv.org/"}]"><a href="http://simplecv.org/">http://simplecv.org/</a></td>
</tr>
<tr height="42">
<td>numpy</td>
<td>提供强大的数组计算功能,可用于图像处理,提供灵活的自定义图像处理方法。</td>
<td data-sheet-value="[{"type":"url","link":"https://numpy.org/","text":"https://numpy.org/"}]"><a href="https://numpy.org/">https://numpy.org/</a></td>
</tr>
<tr height="53">
<td>scipy</td>
<td data-sheet-value="[{"type":"text","text":"在","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"numpy","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"库的基础上,提供更高级的科学计算功能,提供了一些图像处理的方法。","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}}]">在numpy库的基础上,提供更高级的科学计算功能,提供了一些图像处理的方法。</td>
<td data-sheet-value="[{"type":"url","link":"https://scipy.org/","text":"https://scipy.org"}]"><a href="https://scipy.org/">https://scipy.org</a></td>
</tr>
<tr height="53">
<td>matplotlib</td>
<td>基于本地的数据的可视化工具,提供了丰富的数组可视化效果,并提供了方便的图像显示功能。</td>
<td data-sheet-value="[{"type":"url","link":"https://matplotlib.org/","text":"https://matplotlib.org/"}]"><a href="https://matplotlib.org/">https://matplotlib.org/</a></td>
</tr>
<tr height="42">
<td>visdom</td>
<td data-sheet-value="[{"type":"text","text":"基于","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"web","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"的数据的可视化工具,能够灵活的嵌入需要可视化的代码中。","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}}]">基于web的数据的可视化工具,能够灵活的嵌入需要可视化的代码中。</td>
<td data-sheet-value="[{"type":"url","link":"https://github.com/facebookresearch/visdom","text":"https://github.com/facebookresearch/visdom"}]"><a href="https://github.com/facebookresearch/visdom">https://github.com/facebookresearch/visdom</a></td>
</tr>
<tr height="53">
<td>tensorflow</td>
<td data-sheet-value="[{"type":"text","text":"google","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"公司开发的具有梯度反传的张量计算模型,是构建深度学习模型的主要框架之一。","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}}]">google公司开发的具有梯度反传的张量计算模型,是构建深度学习模型的主要框架之一。</td>
<td data-sheet-value="[{"type":"url","link":"https://tensorflow.google.cn/","text":"https://tensorflow.google.cn/"}]"><a href="https://tensorflow.google.cn/">https://tensorflow.google.cn/</a></td>
</tr>
<tr height="65">
<td>torchvision</td>
<td data-sheet-value="[{"type":"text","text":"与","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"PyTorch","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"进行协同的库,提供主要用于提供图像预处理方法,图像与张量的转换,以及常用的图像处理预训练模型","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}}]">与PyTorch进行协同的库,提供主要用于提供图像预处理方法,图像与张量的转换,以及常用的图像处理预训练模型</td>
<td data-sheet-value="[{"type":"url","link":"https://github.com/pytorch/vision","text":"https://github.com/pytorch/vision"}]"><a href="https://github.com/pytorch/vision">https://github.com/pytorch/vision</a></td>
</tr>
<tr height="53">
<td>PyTorch</td>
<td data-sheet-value="[{"type":"text","text":"Facebook","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}},{"type":"text","text":"公司开发的具有梯度反传的张量计算库,是构建深度学习模型的主要框架之一。","style":{"font":"6pt/1.5 MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, \"Helvetica Neue\", Tahoma, \"PingFang SC\", \"Microsoft Yahei\", Arial, \"Hiragino Sans GB\", sans-serif, \"Apple Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\", \"Noto Color Emoji\"","foreColor":"rgb(0, 0, 0)"}}]">Facebook公司开发的具有梯度反传的张量计算库,是构建深度学习模型的主要框架之一。</td>
<td data-sheet-value="[{"type":"url","link":"https://pytorch.org/","text":"https://pytorch.org/"}]"><a href="https://pytorch.org/">https://pytorch.org/</a></td>
</tr>
<tr height="53">
<td>paddlepaddle</td>
<td>百度公司开发的具有梯度反传的张量计算库,是构建深度学习模型的主要框架之一。</td>
<td data-sheet-value="[{"type":"url","link":"https://www.paddlepaddle.org.cn/","text":"https://www.paddlepaddle.org.cn/"}]"><a href="https://www.paddlepaddle.org.cn/">https://www.paddlepaddle.org.cn/</a></td>
</tr>
</tbody>
</table>
<p>其中matplotlib和visdom都是数据可视化工具,平时听说matplotlib好像更多一些,好奇作者为什么在后文中只介绍了visdom而没介绍matplotlib。</p>
<p> </p>
<p>书中共介绍了两种版本Pytorch的安装,一种是CUDA版本,一种是CPU版本。在安装CUDA版本的pytorch之前,需要拥有一个英伟达的显卡,并且完成对应版本CUDA的安装。若不需要使用GPU或者本地没有GPU,则直接安装CPU版本的就好啦。不同版本的Pytorch安装命令可以直接在官网上查看。</p>
<div style="text-align: center;"></div>
<p>从官网上可以看到Pytorch现在也支持AMD的显卡驱动了,但是目前仅提供Linux环境下的安装,还不支持Windows环境。</p>
<div style="text-align: center;"></div>
<p> </p>
<p>安装完成后可以在python环境中输入如下命令,用于测试是否安装成功</p>
<pre>
<code class="language-python">import torch</code></pre>
<pre>
如果如下图没有报错,就说明我们安装成功了</pre>
<div style="text-align: center;"></div>
<p>接下来就能够进行python和pytorch的编程了。</p>
页:
[1]