<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.5">Jekyll</generator><link href="https://thetaqing.github.io/blog//feed.xml" rel="self" type="application/atom+xml" /><link href="https://thetaqing.github.io/blog//" rel="alternate" type="text/html" /><updated>2019-07-23T09:03:41+00:00</updated><id>https://thetaqing.github.io/blog//feed.xml</id><title type="html">ThetaQing</title><subtitle>App marketing template for Jekyll.</subtitle><author><name>{&quot;name&quot;=&gt;nil, &quot;email&quot;=&gt;nil, &quot;twitter&quot;=&gt;nil}</name></author><entry><title type="html">Halcon学习笔记（五）几何定位+仿射+车牌识别</title><link href="https://thetaqing.github.io/blog//2019/07/23/Halcon%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%BA%94-%E5%87%A0%E4%BD%95%E5%AE%9A%E4%BD%8D+%E4%BB%BF%E5%B0%84+%E8%BD%A6%E7%89%8C%E8%AF%86%E5%88%AB/" rel="alternate" type="text/html" title="Halcon学习笔记（五）几何定位+仿射+车牌识别" /><published>2019-07-23T00:00:00+00:00</published><updated>2019-07-23T00:00:00+00:00</updated><id>https://thetaqing.github.io/blog//2019/07/23/Halcon%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0(%E4%BA%94)%E5%87%A0%E4%BD%95%E5%AE%9A%E4%BD%8D+%E4%BB%BF%E5%B0%84+%E8%BD%A6%E7%89%8C%E8%AF%86%E5%88%AB</id><content type="html" xml:base="https://thetaqing.github.io/blog//2019/07/23/Halcon%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%BA%94-%E5%87%A0%E4%BD%95%E5%AE%9A%E4%BD%8D+%E4%BB%BF%E5%B0%84+%E8%BD%A6%E7%89%8C%E8%AF%86%E5%88%AB/">&lt;h3 id=&quot;定位&quot;&gt;定位&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Blob分析&lt;/li&gt;
  &lt;li&gt;模板匹配
    &lt;h3 id=&quot;仿射&quot;&gt;仿射&lt;/h3&gt;
    &lt;h4 id=&quot;使用单位矩阵求仿射矩阵&quot;&gt;使用单位矩阵求仿射矩阵&lt;/h4&gt;
    &lt;p&gt;&lt;strong&gt;示例&lt;/strong&gt;：affine_trans_region.hdev&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;单位矩阵&lt;code class=&quot;highlighter-rouge&quot;&gt;hom_mat2d_identity (HomMat2DIdentity)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;平移矩阵&lt;code class=&quot;highlighter-rouge&quot;&gt;hom_mat2d_translate()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;旋转矩阵&lt;code class=&quot;highlighter-rouge&quot;&gt;hom_mat2d_rotate(HomMat2DIdentity, -0.3, 256, 256, HomMat2DRotate)&lt;/code&gt;
输入是原矩阵，输出是旋转矩阵，旋转角度是顺时针0.3度，旋转中心点是(256,256)&lt;/li&gt;
  &lt;li&gt;缩放矩阵&lt;code class=&quot;highlighter-rouge&quot;&gt;hom_mat2d_scale (HomMat2DRotate, 1.5, 0.5, 256, 256, HomMat2DScale)&lt;/code&gt;
缩放点是(256,256),(1.5,0.5)表示x轴上缩放1.5倍，y轴上缩放0.5倍&lt;/li&gt;
  &lt;li&gt;仿射操作
    &lt;blockquote&gt;
      &lt;p&gt;对区域仿射&lt;code class=&quot;highlighter-rouge&quot;&gt;affine_trans_region (Region, egionAffineTrans, HomMat2DScale, 'nearest_neighbor')&lt;/code&gt;
对图像仿射&lt;code class=&quot;highlighter-rouge&quot;&gt;affine_trans_image()&lt;/code&gt;
对XLD进行仿射&lt;code class=&quot;highlighter-rouge&quot;&gt;affine_trans_xld()&lt;/code&gt;
……&lt;/p&gt;
    &lt;/blockquote&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;使用模板匹配&quot;&gt;使用模板匹配&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;示例&lt;/strong&gt;：check_blister.hdev&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;access_channel (ImageOrig, Image1, 1)  获取第一通道的图片
threshold (Image1, Region, 90, 255) 阈值化
shape_trans (Region, Blister, 'convex')  凸化
orientation_region (Blister, Phi) 测量偏移角度
area_center (Blister, Area1, Row, Column)  测量面积及中心点坐标
vector_angle_to_rigid (Row1, Column1, Phi, Row2, Column2, 0, HomMat2D)  获得图片绕(Row1,Column1)点旋转Phi后得到中心点为(Row2,Column2)，偏移角度为0的图片所需要的仿射矩阵
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;https://img-blog.csdnimg.cn/20190723100934325.png&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;affine_trans_image (ImageOrig, Image2, HomMat2D, 'constant', 'false')  将仿射矩阵应用于图片
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;车牌识别&quot;&gt;车牌识别&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;1、借助助手快速识别&lt;/strong&gt;
第一步：打开一张车牌图片&lt;code class=&quot;highlighter-rouge&quot;&gt;read_image (Image, 'F:/Code/picTest/CarNumber/3.jpg')&lt;/code&gt;
第二步：转化为灰度图片 &lt;code class=&quot;highlighter-rouge&quot;&gt;rgb1_to_gray (Image, GrayImage)&lt;/code&gt;
第三步：利用助手打开新的OCR
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190723141359934.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
并根据OCR中第2点，点击任意一个绿笔头（一般选第一个，正矩形）绘制需要读取的区域，右击鼠标确定
第四步：在OCR中第3点输入你所希望读取的文本内容
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190723141851712.png&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
第五步：勾选OCR中第4点第一项
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190723141524162.png&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
第六步：应用快速设置
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190723141757958.png&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
最后得到车牌识别号
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190723141958904.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2、Blob分析+mlp分类器实现&lt;/strong&gt;
第一步：读取图片
第二步：灰度化
第三步：翻转图片亮度&lt;code class=&quot;highlighter-rouge&quot;&gt;invert_image (GrayImage, ImageInvert)&lt;/code&gt;
第四步：对灰度图片阈值化&lt;code class=&quot;highlighter-rouge&quot;&gt;threshold (GrayImage, Regions1, 96, 204)&lt;/code&gt;
第五步：Blob分析提取车牌号，这里见&lt;a href=&quot;https://blog.csdn.net/Thera_qing/article/details/96490910&quot;&gt;halcon学习笔记（二）实现相机物体抓取&lt;/a&gt;得到仅含有车牌号的区域，注意这里运用到了多个特征选择&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;connection (Regions1, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['area','row'], 'and', [0,164.27], [2945.62,191.47])
select_shape (SelectedRegions, SelectedRegions1, ['area','ratio'], 'and', [0,0], [200,2.0846])
select_shape (SelectedRegions1, SelectedRegions2, ['area','ratio','column'], 'and', [0,1.5793,138.85], [200,2,500])

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;https://img-blog.csdnimg.cn/20190723142511251.png&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
第六步：对区域进行排序&lt;code class=&quot;highlighter-rouge&quot;&gt;sort_region (SelectedRegions2, SortedRegions, 'character', 'true', 'column')&lt;/code&gt;按照列对字母进行排序。
第七步：读取OCR分类器&lt;code class=&quot;highlighter-rouge&quot;&gt;read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OCRHandle)&lt;/code&gt;这里需要注意的是要选&lt;code class=&quot;highlighter-rouge&quot;&gt;'Industrial_0-9A-Z_NoRej.omc'&lt;/code&gt;字体，及数字0-9及字母A-Z，根据以后需求不同加以更改，每一个字体不同内容可以在OCR助手字体旁边的放大镜中看。
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190723143452562.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
第八步：进行分类&lt;code class=&quot;highlighter-rouge&quot;&gt;do_ocr_multi_class_mlp (SortedRegions, ImageInvert, OCRHandle, Class, Confidence)&lt;/code&gt;由于这是识别字是黑的，背景是白的。所以在第三步对图片进行了翻转。
最后得到分类结果
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190723143753215.png&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
&lt;strong&gt;3、定位与区域分割&lt;/strong&gt;
如果直接在RGB情况下阈值化效果不好，考虑颜色空间的转换，RGB转化为HSV&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;read_image (Image, 'F:/Code/picTest/CarNumber/4.jpg')  读取图片
decompose3 (Image, Red, Green, Blue)  分解RGB通道
trans_from_rgb (Red, Green, Blue, Hue, Saturation, Intensity, 'hsv')  转化为HSV三种类型的图片
threshold (Saturation, Regions, 0, 90)  对其中最明显的图片进行阈值分割
connection (Regions, ConnectedRegions)  打散
一定要保证车牌区域是完整的，如果断开了，要么调用unionl算子将其联合成一个区域，要么先膨胀将其接上！！！
如果车牌字符与边框相连，要么重新阈值化找到没有连接的阈值；要么开运算打断
select_shape (ConnectedRegions, SelectedRegions, ['area','row'], 'and', [4041.92,186], [11826.4,354.42])  选择车牌的大致区域
fill_up (SelectedRegions, RegionFillUp)  填充孔洞
opening_rectangle1 (RegionFillUp, RegionOpening, 20, 30)  剔除矩形外多余元素（视情况而定）
opening_circle (RegionOpening4, RegionOpening4, 3.5)  剔除多余元素
shape_trans (RegionOpening4, RegionTrans, 'rectangle2')  将形状转换成矩形
orientation_region (RegionTrans, Phi)  得到区域的偏转角度
area_center (RegionTrans, Area, Row, Column)  计算区域面积和中心点坐标
vector_angle_to_rigid (Row, Column, Phi, Row, Column, 0, HomMat2D)  得到摆正图片的仿射矩阵
affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')  仿射矩阵应用于图片
affine_trans_region (RegionTrans, RegionAffineTrans, HomMat2D, 'nearest_neighbor')  仿射矩阵应用于区域
reduce_domain (ImageAffineTrans, RegionAffineTrans, ImageReduced)  从图片中截取该矩形区域
rgb1_to_gray (ImageReduced, GrayImage)  将截取的区域转化为灰度图片
……后续操作即前面提到的Blob分析得到车牌字符，最后分类器识别字符

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;显示&quot;&gt;显示&lt;/h3&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;smallest_rectangle1 (RegionOpening2, Row1, Column1, Row2, Column2)
count_obj (RegionOpening2, Number)
for i := 1 to Number by 1
    disp_message (WindowHandle, Class[i-1], 'image', Row2[i-1], Column1[i-1], 'green', 'false')
endfor
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>{&quot;name&quot;=&gt;nil, &quot;email&quot;=&gt;nil, &quot;twitter&quot;=&gt;nil}</name></author><summary type="html">定位 Blob分析 模板匹配 仿射 使用单位矩阵求仿射矩阵 示例：affine_trans_region.hdev 单位矩阵hom_mat2d_identity (HomMat2DIdentity) 平移矩阵hom_mat2d_translate() 旋转矩阵hom_mat2d_rotate(HomMat2DIdentity, -0.3, 256, 256, HomMat2DRotate) 输入是原矩阵，输出是旋转矩阵，旋转角度是顺时针0.3度，旋转中心点是(256,256) 缩放矩阵hom_mat2d_scale (HomMat2DRotate, 1.5, 0.5, 256, 256, HomMat2DScale) 缩放点是(256,256),(1.5,0.5)表示x轴上缩放1.5倍，y轴上缩放0.5倍 仿射操作 对区域仿射affine_trans_region (Region, egionAffineTrans, HomMat2DScale, 'nearest_neighbor') 对图像仿射affine_trans_image() 对XLD进行仿射affine_trans_xld() ……</summary></entry><entry><title type="html">Halcon学习笔记（四）形态学笔记</title><link href="https://thetaqing.github.io/blog//2019/07/20/halcon%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E5%9B%9B-%E5%BD%A2%E6%80%81%E5%AD%A6%E7%AC%94%E8%AE%B0/" rel="alternate" type="text/html" title="Halcon学习笔记（四）形态学笔记" /><published>2019-07-20T00:00:00+00:00</published><updated>2019-07-20T00:00:00+00:00</updated><id>https://thetaqing.github.io/blog//2019/07/20/halcon%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0(%E5%9B%9B)%E5%BD%A2%E6%80%81%E5%AD%A6%E7%AC%94%E8%AE%B0</id><content type="html" xml:base="https://thetaqing.github.io/blog//2019/07/20/halcon%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E5%9B%9B-%E5%BD%A2%E6%80%81%E5%AD%A6%E7%AC%94%E8%AE%B0/">&lt;h3 id=&quot;理论基础&quot;&gt;理论基础&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;膨胀&lt;/strong&gt;
圆形结构膨胀算子：&lt;code class=&quot;highlighter-rouge&quot;&gt;dilation_circle (WiresFilled, RegionDilation, 3.5)&lt;/code&gt;
结构元素的中心元素在filter中只要有一个元素和目标（原来的，不实时更新）产生交集时，中心元素的值改为交集处元素。膨胀&lt;strong&gt;增加&lt;/strong&gt;了元素，按照从上到下，从左到右的顺序滑动。
对灰度图像的膨胀相当于变亮&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;腐蚀&lt;/strong&gt;
圆形结构腐蚀算子：&lt;code class=&quot;highlighter-rouge&quot;&gt;erosion_circle (WiresFilled, RegionErosion, 3.5)&lt;/code&gt;
结构元素的所有元素都能与目标（原来的，最初的像素分布）完全包含时才保留中心位置的元素，否则只要有一个元素没有包含，中心位置的元素就不保留。腐蚀&lt;strong&gt;减少&lt;/strong&gt;像素
对灰度图像的腐蚀相当于变暗
&lt;strong&gt;开运算&lt;/strong&gt;
圆形结构开运算算子：&lt;code class=&quot;highlighter-rouge&quot;&gt;opening_circle (WiresFilled, Balls, 15.5)&lt;/code&gt;
先腐蚀后膨胀。开运算是断开，&lt;strong&gt;减少&lt;/strong&gt;像素
对灰度图像的开运算相当于对整体变暗
&lt;strong&gt;闭运算&lt;/strong&gt;
圆形结构闭运算算子：&lt;code class=&quot;highlighter-rouge&quot;&gt;closing_circle (WiresFilled, RegionClosing, 3.5)&lt;/code&gt;
先膨胀后腐蚀。闭运算是连接，&lt;strong&gt;增加&lt;/strong&gt;像素
对灰度图像的闭运算相当于对整体变亮
&lt;strong&gt;结构元素&lt;/strong&gt;
用什么形状的结构元素就会对什么形状造成影响，即进行运算，需要提取什么形状就选择什么形状的结构元素。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;对二值图像和区域做膨胀、腐蚀等运算是改变形状，对灰度图像是改变亮度
腐蚀比开运算强度大
膨胀比闭运算强度大&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;视觉处理常用框架&lt;/strong&gt;&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;采集图像&lt;/li&gt;
    &lt;li&gt;预处理
中值、均值、高斯滤波
动态范围
灰度处理
翻转，图像的线性变换&lt;/li&gt;
    &lt;li&gt;得到特征&lt;/li&gt;
    &lt;li&gt;显示
&lt;strong&gt;凸性&lt;/strong&gt;
任意两点相连都在区域内
tips：高斯滤波算子例程&lt;br /&gt;
二维码识别2d_data_codes_minimize_noise.hdev&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;</content><author><name>{&quot;name&quot;=&gt;nil, &quot;email&quot;=&gt;nil, &quot;twitter&quot;=&gt;nil}</name></author><summary type="html">理论基础 膨胀 圆形结构膨胀算子：dilation_circle (WiresFilled, RegionDilation, 3.5) 结构元素的中心元素在filter中只要有一个元素和目标（原来的，不实时更新）产生交集时，中心元素的值改为交集处元素。膨胀增加了元素，按照从上到下，从左到右的顺序滑动。 对灰度图像的膨胀相当于变亮</summary></entry><entry><title type="html">Halcon学习笔记（二）图像分割</title><link href="https://thetaqing.github.io/blog//2019/07/19/Halcon%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%BA%8C-%E5%9B%BE%E5%83%8F%E5%88%86%E5%89%B2/" rel="alternate" type="text/html" title="Halcon学习笔记（二）图像分割" /><published>2019-07-19T00:00:00+00:00</published><updated>2019-07-19T00:00:00+00:00</updated><id>https://thetaqing.github.io/blog//2019/07/19/Halcon%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0(%E4%BA%8C)%E5%9B%BE%E5%83%8F%E5%88%86%E5%89%B2</id><content type="html" xml:base="https://thetaqing.github.io/blog//2019/07/19/Halcon%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%BA%8C-%E5%9B%BE%E5%83%8F%E5%88%86%E5%89%B2/">&lt;h2 id=&quot;图像分割的主要方法&quot;&gt;图像分割的主要方法&lt;/h2&gt;
&lt;h3 id=&quot;基于阈值的图像分割&quot;&gt;基于阈值的图像分割&lt;/h3&gt;

&lt;h3 id=&quot;基于边缘的图像分割&quot;&gt;基于边缘的图像分割&lt;/h3&gt;

&lt;p&gt;###&lt;/p&gt;</content><author><name>{&quot;name&quot;=&gt;nil, &quot;email&quot;=&gt;nil, &quot;twitter&quot;=&gt;nil}</name></author><summary type="html">图像分割的主要方法 基于阈值的图像分割</summary></entry><entry><title type="html">Halcon学习笔记（二）相机采集并提取</title><link href="https://thetaqing.github.io/blog//2019/07/19/halcon%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%BA%8C-%E7%9B%B8%E6%9C%BA%E9%87%87%E9%9B%86%E5%B9%B6%E6%8F%90%E5%8F%96/" rel="alternate" type="text/html" title="Halcon学习笔记（二）相机采集并提取" /><published>2019-07-19T00:00:00+00:00</published><updated>2019-07-19T00:00:00+00:00</updated><id>https://thetaqing.github.io/blog//2019/07/19/halcon%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0(%E4%BA%8C)%E7%9B%B8%E6%9C%BA%E9%87%87%E9%9B%86%E5%B9%B6%E6%8F%90%E5%8F%96</id><content type="html" xml:base="https://thetaqing.github.io/blog//2019/07/19/halcon%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%BA%8C-%E7%9B%B8%E6%9C%BA%E9%87%87%E9%9B%86%E5%B9%B6%E6%8F%90%E5%8F%96/">&lt;p&gt;&lt;strong&gt;主要实现相机对抓取物体的单独提取功能&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id=&quot;第一步连接相机并生成代码&quot;&gt;第一步：连接相机并生成代码&lt;/h3&gt;
&lt;p&gt;打开halcon菜单栏的助手，选择打开新的Image Acquisition。&lt;br /&gt;
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190719184333269.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;br /&gt;
  点击连接，halcon会自动获取相机信息&lt;br /&gt;
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190719184547255.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;br /&gt;
  选择连接，–&amp;gt;选择采集–&amp;gt;实时，此时，窗口便会显示相机实时采集到的信息&lt;br /&gt;
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190719192210130.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;br /&gt;
  采集到合适的图片之后点击停止，选择参数&lt;br /&gt;
  &lt;img src=&quot;https://img-blog.csdnimg.cn/20190719185554420.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;br /&gt;
  在这里可以修改图片的参数，之后再选择代码生成，选择异步采集，插入代码，在程序窗口中，代码已经生成&lt;br /&gt;
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190719185743559.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;
&lt;h3 id=&quot;第二步将采集的图片转为二值图片&quot;&gt;第二步：将采集的图片转为二值图片&lt;/h3&gt;
&lt;p&gt;调用算子&lt;code class=&quot;highlighter-rouge&quot;&gt;rgb1_to_gray (Image, GrayImage)&lt;/code&gt;&lt;br /&gt;
  单步调试得到二值图片&lt;br /&gt;
  &lt;img src=&quot;https://img-blog.csdnimg.cn/20190719192240825.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;
&lt;h3 id=&quot;第三步使用全局阈值分割图片&quot;&gt;第三步：使用全局阈值分割图片&lt;/h3&gt;
&lt;p&gt;打开灰度直方图，并打开阈值&lt;br /&gt;
  &lt;img src=&quot;https://img-blog.csdnimg.cn/20190719192321129.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;br /&gt;
  此时窗口变为：&lt;br /&gt;
  &lt;img src=&quot;https://img-blog.csdnimg.cn/2019071919060024.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;br /&gt;
  移动直方图上的坐标，直到选中物体&lt;br /&gt;
  &lt;img src=&quot;https://img-blog.csdnimg.cn/20190719192429281.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;br /&gt;
  提醒一下，使用全局阈值来分割图片必须满足目标与背景相差较大，如果相差较近，建议使用局部阈值或动态阈值。&lt;br /&gt;
  然后点击插入代码&lt;br /&gt;
  此时程序窗口中生成了&lt;code class=&quot;highlighter-rouge&quot;&gt;threshold (GrayImage, Regions, 127, 255)&lt;/code&gt;参数会根据图片变化而变化&lt;/p&gt;
&lt;h3 id=&quot;第四步打散&quot;&gt;第四步：打散&lt;/h3&gt;
&lt;p&gt;由于分割之后所有选中的物体都被认为是一个整体，为提取单个物体，需要对其进行打散操作&lt;br /&gt;
  调用算子&lt;code class=&quot;highlighter-rouge&quot;&gt;connection (Regions, ConnectedRegions)&lt;/code&gt;并运行得到&lt;br /&gt;
  &lt;img src=&quot;https://img-blog.csdnimg.cn/20190719193126284.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;br /&gt;
  此时选中的物体用其他颜色进行区别实现打散。&lt;/p&gt;
&lt;h3 id=&quot;第五步提取&quot;&gt;第五步：提取&lt;/h3&gt;
&lt;p&gt;打开特征直方图，注意要在程序运行到打散之后再打开&lt;br /&gt;
  &lt;img src=&quot;https://img-blog.csdnimg.cn/20190719193344287.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;br /&gt;
同样，选择分离的特征依据，这里以面积为例，选择颜色表示选中，这里颜色可以根据实际目标物体的情况判断，移动最小阈值轴（绿色）直到目标被选中（即仅有目标被填充为选中的颜色，这里是绿色），选择插入代码，此时程序窗口已插入：&lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 40494, 100000)&lt;/code&gt;&lt;br /&gt;
此时便完成了全部提取物体的操作，下面可以对提取的物体做进一步操作，比如这里计算面积并获得坐标&lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;area_center (SelectedRegions, Area, Row, Column)&lt;/code&gt;&lt;br /&gt;
得到&lt;br /&gt;
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190719194001683.png&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;br /&gt;
当然也可以显示信息，但在显示之前一定要先创建窗口&lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;dev_close_window ()&lt;/code&gt;&lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;dev_open_window (0, 0, 512, 512, 'black', WindowHandle)&lt;/code&gt;&lt;br /&gt;
然后再显示信息&lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;disp_message (WindowHandle, '面积'+Area+'坐标'+Column, 'window', Row, Column, 'black', 'false')&lt;/code&gt;&lt;br /&gt;
&lt;strong&gt;tips:&lt;/strong&gt;这里如果报错，可能是没有设置可视单步调试模式，选择可视化–&amp;gt;更新窗口–&amp;gt;单步模式–&amp;gt;总是&lt;br /&gt;
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190719194528574.png&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;br /&gt;
另外在每次重新运行程序前要先关闭否则可能报错，所以在&lt;code class=&quot;highlighter-rouge&quot;&gt;endwhile&lt;/code&gt;后面要加一句&lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;close_framegrabber (AcqHandle)&lt;/code&gt;&lt;br /&gt;
并在重新运行之前先运行一遍close语句（将运行光标绿箭头移到那一句点击运行即可）。&lt;/p&gt;</content><author><name>{&quot;name&quot;=&gt;nil, &quot;email&quot;=&gt;nil, &quot;twitter&quot;=&gt;nil}</name></author><summary type="html">主要实现相机对抓取物体的单独提取功能 第一步：连接相机并生成代码 打开halcon菜单栏的助手，选择打开新的Image Acquisition。 点击连接，halcon会自动获取相机信息 选择连接，–&amp;gt;选择采集–&amp;gt;实时，此时，窗口便会显示相机实时采集到的信息 采集到合适的图片之后点击停止，选择参数 在这里可以修改图片的参数，之后再选择代码生成，选择异步采集，插入代码，在程序窗口中，代码已经生成 第二步：将采集的图片转为二值图片 调用算子rgb1_to_gray (Image, GrayImage) 单步调试得到二值图片 第三步：使用全局阈值分割图片 打开灰度直方图，并打开阈值 此时窗口变为： 移动直方图上的坐标，直到选中物体 提醒一下，使用全局阈值来分割图片必须满足目标与背景相差较大，如果相差较近，建议使用局部阈值或动态阈值。 然后点击插入代码 此时程序窗口中生成了threshold (GrayImage, Regions, 127, 255)参数会根据图片变化而变化 第四步：打散 由于分割之后所有选中的物体都被认为是一个整体，为提取单个物体，需要对其进行打散操作 调用算子connection (Regions, ConnectedRegions)并运行得到 此时选中的物体用其他颜色进行区别实现打散。 第五步：提取 打开特征直方图，注意要在程序运行到打散之后再打开 同样，选择分离的特征依据，这里以面积为例，选择颜色表示选中，这里颜色可以根据实际目标物体的情况判断，移动最小阈值轴（绿色）直到目标被选中（即仅有目标被填充为选中的颜色，这里是绿色），选择插入代码，此时程序窗口已插入： select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 40494, 100000) 此时便完成了全部提取物体的操作，下面可以对提取的物体做进一步操作，比如这里计算面积并获得坐标 area_center (SelectedRegions, Area, Row, Column) 得到 当然也可以显示信息，但在显示之前一定要先创建窗口 dev_close_window () dev_open_window (0, 0, 512, 512, 'black', WindowHandle) 然后再显示信息 disp_message (WindowHandle, '面积'+Area+'坐标'+Column, 'window', Row, Column, 'black', 'false') tips:这里如果报错，可能是没有设置可视单步调试模式，选择可视化–&amp;gt;更新窗口–&amp;gt;单步模式–&amp;gt;总是 另外在每次重新运行程序前要先关闭否则可能报错，所以在endwhile后面要加一句 close_framegrabber (AcqHandle) 并在重新运行之前先运行一遍close语句（将运行光标绿箭头移到那一句点击运行即可）。</summary></entry><entry><title type="html">Halcon学习笔记（一）之基本知识点和环境配置</title><link href="https://thetaqing.github.io/blog//2019/07/17/halcon%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%B8%80-%E4%B9%8B%E5%9F%BA%E6%9C%AC%E7%9F%A5%E8%AF%86%E7%82%B9%E5%92%8C%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE/" rel="alternate" type="text/html" title="Halcon学习笔记（一）之基本知识点和环境配置" /><published>2019-07-17T00:00:00+00:00</published><updated>2019-07-17T00:00:00+00:00</updated><id>https://thetaqing.github.io/blog//2019/07/17/halcon%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0(%E4%B8%80)%E4%B9%8B%E5%9F%BA%E6%9C%AC%E7%9F%A5%E8%AF%86%E7%82%B9%E5%92%8C%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE</id><content type="html" xml:base="https://thetaqing.github.io/blog//2019/07/17/halcon%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%B8%80-%E4%B9%8B%E5%9F%BA%E6%9C%AC%E7%9F%A5%E8%AF%86%E7%82%B9%E5%92%8C%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE/">&lt;h2 id=&quot;image像素类型&quot;&gt;Image像素类型&lt;/h2&gt;
&lt;p&gt;边缘方向（direction）：边缘梯度方向
灰度图像，byte，uint2:表示标准的灰度图像编码
Difference&amp;amp;Derivative，int1，int2：表示两帧图像的差异或者整型精度的偏差
2D直方图，int4：两帧图像灰度值出现的频率统计图
Derivatives，real：边缘提取和特殊的灰度值配置
傅里叶变换，complex：用频率描述图像
色调，cyclic：每个颜色都分配了一个灰度值
光流，vector_field：描述两帧连续的图像间的光流&lt;/p&gt;

&lt;h2 id=&quot;image像素表示方式&quot;&gt;Image像素表示方式&lt;/h2&gt;
&lt;p&gt;byte：8 bits无符号
uint2:16bits无符号
int1：8bits有符号
int2: 16bits有符号
int4：32bits有符号
direction：8bits无符号
real：32bits浮点类型
complex：每像素两个real值
cyclic：8bits无符号，且255+1=0
vector_field：两帧real类型图像描述x或者y方向的运动&lt;/p&gt;

&lt;h2 id=&quot;tuple&quot;&gt;tuple&lt;/h2&gt;
&lt;h3 id=&quot;iconic-tuple图标数组&quot;&gt;Iconic tuple：图标数组&lt;/h3&gt;
&lt;p&gt;一个变量中包含一组数目不定的图标变量
最小的索引是1
最大的索引是元素个数
&lt;strong&gt;主要operations（操作）有：&lt;/strong&gt;
显示：display
元素个数：number of elements
选择：selection
串联：concatenation
类型校验：type checking
比较：comparison&lt;/p&gt;
&lt;h3 id=&quot;control-tuple控制数组&quot;&gt;Control tuple：控制数组&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;变量类型：&lt;/strong&gt;
Integer
Real
String
&lt;strong&gt;变量长度：&lt;/strong&gt;
如果长度为1，可以作为正常变量使用
第一个索引为0
最大的索引为变量长度-1&lt;/p&gt;

&lt;p&gt;（物体数组）&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;halcon与VC2013环境配置教程&lt;/strong&gt;
在教程前面要记得创建x64环境，然后再用这个配置https://www.cnblogs.com/oucsheep/p/5866471.html
如果报错“头文件未添加stdafx.h”则是因为没有删除halcon生成的cpp文件&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;利用Microsoft Visual Studio Installer生成MFC的exe文件&lt;/strong&gt;
参考教程：https://jingyan.baidu.com/article/b7001fe184e4e50e7382dd4e.html
记得要在需要打包的项目中右击解决方案，选择添加-&amp;gt;新建项目-&amp;gt;其他项目-&amp;gt;Visual Studio Installer-&amp;gt;setup Project（如果没有这个插件选择联机，搜索Visual Studio Installer Project，下载并安装），然后按照上述教程，另外一篇教程也不错https://blog.csdn.net/dog123xuheyin/article/details/85008071
简而言之&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;第一步：新建项目&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;第二步：修改参数，主要修改如下&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;https://img-blog.csdnimg.cn/20190717170101322.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;

    &lt;blockquote&gt;
      &lt;p&gt;Author：即作者名字&lt;br /&gt;
  Manufacture：即公司名字&lt;br /&gt;
  ProductName：应用软件名字，一般与项目名相同，为区分文件名，请修改&lt;br /&gt;
  RemovePreviousVersions：是否移除旧版本，建议修改为True，否则会安装一堆&lt;br /&gt;
  TargetPlatform：x86安装或x64安装&lt;/p&gt;
    &lt;/blockquote&gt;
  &lt;/li&gt;
  &lt;li&gt;第三步：添加主输出&lt;/li&gt;
  &lt;li&gt;第四步：添加卸载文件，路径：C:\Windows\System32\msiexec.exe，图标可以在网上下,ico格式便可&lt;/li&gt;
  &lt;li&gt;第五步：添加快捷键，并修改msiexec.exe的编码参数，这里教程里面都有&lt;/li&gt;
  &lt;li&gt;第六步：右键生成&lt;/li&gt;
  &lt;li&gt;第七步：找到输出路径安装&lt;/li&gt;
&lt;/ul&gt;</content><author><name>{&quot;name&quot;=&gt;nil, &quot;email&quot;=&gt;nil, &quot;twitter&quot;=&gt;nil}</name></author><summary type="html">Image像素类型 边缘方向（direction）：边缘梯度方向 灰度图像，byte，uint2:表示标准的灰度图像编码 Difference&amp;amp;Derivative，int1，int2：表示两帧图像的差异或者整型精度的偏差 2D直方图，int4：两帧图像灰度值出现的频率统计图 Derivatives，real：边缘提取和特殊的灰度值配置 傅里叶变换，complex：用频率描述图像 色调，cyclic：每个颜色都分配了一个灰度值 光流，vector_field：描述两帧连续的图像间的光流</summary></entry><entry><title type="html">Pytorch学习笔记（五）之tensor的高阶运算</title><link href="https://thetaqing.github.io/blog//2019/07/12/pytorch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%BA%94-%E4%B9%8Btensor%E7%9A%84%E9%AB%98%E9%98%B6%E8%BF%90%E7%AE%97/" rel="alternate" type="text/html" title="Pytorch学习笔记（五）之tensor的高阶运算" /><published>2019-07-12T00:00:00+00:00</published><updated>2019-07-12T00:00:00+00:00</updated><id>https://thetaqing.github.io/blog//2019/07/12/pytorch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0(%E4%BA%94)%E4%B9%8Btensor%E7%9A%84%E9%AB%98%E9%98%B6%E8%BF%90%E7%AE%97</id><content type="html" xml:base="https://thetaqing.github.io/blog//2019/07/12/pytorch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%BA%94-%E4%B9%8Btensor%E7%9A%84%E9%AB%98%E9%98%B6%E8%BF%90%E7%AE%97/">&lt;h2 id=&quot;where&quot;&gt;where&lt;/h2&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;torch.where(condition,x,y) #condition必须是tensor类型 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190712194225725.png) condition的维度和x，y一致，用1和0分别表示该位置的取值 例：输入：

cond = torch.tensor([[0.6, 0.7],
                    [0.3, 0.6]])
a = torch.tensor([[1., 1.],
                 [1., 1.]])
b = torch.tensor([[0., 0.],
                 [0., 0.]])
c = torch.where(cond &amp;gt; 0.5, a, b) #此时cond只有0和1的值
print(c) 输出：

tensor([[1., 1.],
        [0., 1.]])
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;高度并行&lt;/p&gt;

&lt;h2 id=&quot;gather&quot;&gt;gather&lt;/h2&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;torch.gather(input, dim, index, out=None) 相当于查表操作 举例：

prob = torch.randn(4, 10)
idx = prob.topk(dim=1, k=3)  # prob在维度1中前三个最大的数，一共有4行，返回值和对应的下标
print(&quot;all of topk idx: &quot;, idx)
idx = idx[1]
print(&quot;idx[1]: &quot;, idx)
label = torch.arange(10) + 100  # 举个例子，这里的列表表示为
# 0对应于100,1对应于101，以此类推，根据实际应用修改
result = torch.gather(label.expand(4, 10), dim=1, index=idx.long())  # lable相当于one-hot编码，index表示索引
# 换而言是是y与x的函数映射关系，index表示x
print(&quot;result:&quot;, result)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输出结果为：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;all of topk idx:  torch.return_types.topk(
values=tensor([[0.7878, 0.2928, 0.2062],
        [0.2524, 0.2094, 0.0350],
        [1.5519, 0.8405, 0.7521],
        [1.3380, 0.9290, 0.5655]]),
indices=tensor([[2, 0, 8],
        [9, 5, 6],
        [1, 2, 0],
        [3, 7, 8]]))
idx[1]:  tensor([[2, 0, 8],
        [9, 5, 6],
        [1, 2, 0],
        [3, 7, 8]])
result: tensor([[102, 100, 108],
        [109, 105, 106],
        [101, 102, 100],
        [103, 107, 108]])
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>{&quot;name&quot;=&gt;nil, &quot;email&quot;=&gt;nil, &quot;twitter&quot;=&gt;nil}</name></author><summary type="html">where</summary></entry><entry><title type="html">Pytorch学习笔记(四)之张量的高阶操作</title><link href="https://thetaqing.github.io/blog//2019/07/11/pytorch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0(%E5%9B%9B)%E4%B9%8B%E5%BC%A0%E9%87%8F%E7%9A%84%E9%AB%98%E9%98%B6%E6%93%8D%E4%BD%9C/" rel="alternate" type="text/html" title="Pytorch学习笔记(四)之张量的高阶操作" /><published>2019-07-11T00:00:00+00:00</published><updated>2019-07-11T00:00:00+00:00</updated><id>https://thetaqing.github.io/blog//2019/07/11/pytorch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0(%E5%9B%9B)%E4%B9%8B%E5%BC%A0%E9%87%8F%E7%9A%84%E9%AB%98%E9%98%B6%E6%93%8D%E4%BD%9C</id><content type="html" xml:base="https://thetaqing.github.io/blog//2019/07/11/pytorch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0(%E5%9B%9B)%E4%B9%8B%E5%BC%A0%E9%87%8F%E7%9A%84%E9%AB%98%E9%98%B6%E6%93%8D%E4%BD%9C/">&lt;h2 id=&quot;broadingcasting自动扩展&quot;&gt;Broadingcasting自动扩展&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;特点：&lt;/strong&gt;
实现维度扩展
不需要拷贝数据
&lt;strong&gt;原理：&lt;/strong&gt;
先在高维度上un squeeze一个维度，然后将维度为1的expend到相对应的维度
&lt;img src=&quot;https://img-blog.csdnimg.cn/2019071109085572.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
&lt;strong&gt;使用条件：&lt;/strong&gt;
&lt;strong&gt;从低维度开始匹配&lt;/strong&gt;所以如果操作是中间维度的运算，则需要手动unsqueeze低维度为1.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;对应运算的维度要么为1，为1表示适用于所有其他维度&lt;/li&gt;
  &lt;li&gt;对应运算的维度与另一个操作数据的对应维度相同&lt;/li&gt;
  &lt;li&gt;对应维度不存在，空值，broadcast会自动unsqueeze&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;如：A = [class][students][scores] = [4][32][8]&lt;/p&gt;

&lt;p&gt;B~1~ = [1]
此时表示对每个班的每个学生的每门课程分数都加1分&lt;/p&gt;

&lt;p&gt;B~2~ = [8]
此时表示对每个班的每个学生的八门课程加上对应的分数（如：01010340）&lt;/p&gt;

&lt;p&gt;B~3~ = [32][1]
因为要对student进行操作，所有低维度的1需要手动unsqueeze&lt;/p&gt;

&lt;p&gt;B~4~ = [4]
此时不满足条件，无法判断是哪四门课程&lt;/p&gt;

&lt;h2 id=&quot;拼接与拆分&quot;&gt;拼接与拆分&lt;/h2&gt;
&lt;h3 id=&quot;cat拼接&quot;&gt;cat()拼接&lt;/h3&gt;
&lt;p&gt;在原来的基础上叠加&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;in: a = torch.rand(4, 32, 8)
in: b = torch.rand(5, 32, 8) #除拼接维度外其他维度保持一致
in: torch.cat([a, b], dim=0).shape
out: torch.Size([9, 32, 8]) dim表示的是在第几个维度拼接，其他维度保持一致
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;stack&quot;&gt;stack()&lt;/h3&gt;
&lt;p&gt;创建一个新的维度&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;in: b = torch.rand(4, 32, 8)
in: c = torch.stack([a,b], dim = 0)
in: print(c.shape)
out: torch.Size([2, 4, 32, 8])
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;两个数据的维度必须完全一样，此时c[0]表示a，c[1]表示b&lt;/p&gt;

&lt;h3 id=&quot;split&quot;&gt;split()&lt;/h3&gt;
&lt;p&gt;根据长度拆分
&lt;img src=&quot;https://img-blog.csdnimg.cn/2019071119444923.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
拆分的长度一定要足够&lt;/p&gt;</content><author><name>{&quot;name&quot;=&gt;nil, &quot;email&quot;=&gt;nil, &quot;twitter&quot;=&gt;nil}</name></author><summary type="html">Broadingcasting自动扩展 特点： 实现维度扩展 不需要拷贝数据 原理： 先在高维度上un squeeze一个维度，然后将维度为1的expend到相对应的维度 使用条件： 从低维度开始匹配所以如果操作是中间维度的运算，则需要手动unsqueeze低维度为1. 对应运算的维度要么为1，为1表示适用于所有其他维度 对应运算的维度与另一个操作数据的对应维度相同 对应维度不存在，空值，broadcast会自动unsqueeze</summary></entry><entry><title type="html">Pytorch学习笔记之tensor的基本运算</title><link href="https://thetaqing.github.io/blog//2019/07/11/pytorch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8Btensor%E7%9A%84%E5%9F%BA%E6%9C%AC%E8%BF%90%E7%AE%97/" rel="alternate" type="text/html" title="Pytorch学习笔记之tensor的基本运算" /><published>2019-07-11T00:00:00+00:00</published><updated>2019-07-11T00:00:00+00:00</updated><id>https://thetaqing.github.io/blog//2019/07/11/pytorch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8Btensor%E7%9A%84%E5%9F%BA%E6%9C%AC%E8%BF%90%E7%AE%97</id><content type="html" xml:base="https://thetaqing.github.io/blog//2019/07/11/pytorch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8Btensor%E7%9A%84%E5%9F%BA%E6%9C%AC%E8%BF%90%E7%AE%97/">&lt;h2 id=&quot;加减乘除&quot;&gt;加减乘除&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;https://img-blog.csdnimg.cn/20190711195108688.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;矩阵相乘&quot;&gt;矩阵相乘&lt;/h2&gt;
&lt;p&gt;torch.mm 只适用于2d，不推荐
torch.matmul 推荐使用
@ 运算符重载
&lt;img src=&quot;https://img-blog.csdnimg.cn/2019071120060893.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;次方运算&quot;&gt;次方运算&lt;/h2&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a = torch.full([2,2],3)
aa = a.pow(2)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;操作运算如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;a.pow(n)&lt;/code&gt;  表示a^n^
&lt;code class=&quot;highlighter-rouge&quot;&gt;a**n&lt;/code&gt;  表示a^n^
&lt;code class=&quot;highlighter-rouge&quot;&gt;aa.sqrt()&lt;/code&gt;  表示aa ^1/2^
&lt;code class=&quot;highlighter-rouge&quot;&gt;aa.rsqrt()&lt;/code&gt;  表示aa^1/3^
    注意这里没有torch.&lt;/p&gt;

&lt;h2 id=&quot;幂对数&quot;&gt;幂/对数&lt;/h2&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a = torch.exp(torch.one(2,2)) #指数运算
b = torch.log(a) #对数运算，默认底数为e
c = torch.log2(a) #对数运算，改变底数
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;近似值&quot;&gt;近似值&lt;/h2&gt;
&lt;p&gt;向下取整，floor()
向上取整，ceil()
取整数部分，trunc()
取小数部分，frac()
四舍五入法，round()
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190711202056683.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;clamp&quot;&gt;clamp()&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;clamp(min)&lt;/li&gt;
  &lt;li&gt;clamp(min,max)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;https://img-blog.csdnimg.cn/20190711202657582.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;
&lt;h1 id=&quot;数据统计&quot;&gt;数据统计&lt;/h1&gt;
&lt;h2 id=&quot;norm范数&quot;&gt;norm范数&lt;/h2&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a.norm(n,dim=m) #n表示求n的范数，m表示第m维
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;https://img-blog.csdnimg.cn/20190711203928869.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;meansumminmaxprod累乘argmax返回最大值的下标argmin返回最小值的索引&quot;&gt;mean(),sum(),min(),max(),prod()累乘,argmax()返回最大值的下标,argmin()返回最小值的索引&lt;/h2&gt;
&lt;p&gt;argmax(dim=n)表示在第n维的最大值，如果不指定维度，则会打平为一维数据，返回所有数据的最大值的索引。
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190711204813147.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;dimkeepdim&quot;&gt;dim,keepdim&lt;/h2&gt;
&lt;p&gt;keepdim表示维度信息和原来的一样
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190711205535691.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;top-kk-th&quot;&gt;Top-k/k-th&lt;/h2&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a.topk(k,dim=n， largest=False) #k表示前k个，dim表示维度，largest表示最大值，
默认True，要得到最小值就改为Flase a.kthvalue(k,dim=n) #表示第n维的第k小的值 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190711210355611.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70) ## 比较 `&amp;gt;,&amp;gt;=,&amp;lt;,&amp;lt;=,!=,==` 判断每一个元素是否相等`torch.eq(a,b)`，返回维度相同，值为0/1的矩阵 判断整个数据是否相等`torch.equal(a,b)`，返回True或者False
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>{&quot;name&quot;=&gt;nil, &quot;email&quot;=&gt;nil, &quot;twitter&quot;=&gt;nil}</name></author><summary type="html">加减乘除 矩阵相乘 torch.mm 只适用于2d，不推荐 torch.matmul 推荐使用 @ 运算符重载 次方运算</summary></entry><entry><title type="html">Pytorch学习笔记之tensor的基本操作</title><link href="https://thetaqing.github.io/blog//2019/07/10/pytorch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8Btensor%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/" rel="alternate" type="text/html" title="Pytorch学习笔记之tensor的基本操作" /><published>2019-07-10T00:00:00+00:00</published><updated>2019-07-10T00:00:00+00:00</updated><id>https://thetaqing.github.io/blog//2019/07/10/pytorch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8Btensor%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C</id><content type="html" xml:base="https://thetaqing.github.io/blog//2019/07/10/pytorch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8Btensor%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/">&lt;h2 id=&quot;创建tensor&quot;&gt;创建tensor&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;从numpy导入&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710153332121.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;numpy导入&quot; /&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;从list里面导入&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;img src=&quot;https://img-blog.csdnimg.cn/201907101534291.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
&lt;strong&gt;tips:&lt;/strong&gt; orch.tensor()里面的参数只能是numpy数据或list,即现成的数据；torch.Tensor/FloatTensor()参数为数据的维度,如果是接收现成的数据，要用list形式，如第四行表示方法（不建议使用，易混淆），如果是torch.Tensor/FloatTensor(2,3)表示数据shape为2*3&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;创建未初始化数据，即申请内存空间&lt;/p&gt;

    &lt;p&gt;三种方法，在数据使用前记得覆盖原有的随机初始化的值，小括号内的参数均表示数据的维度，即shape
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710154728620.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
&lt;strong&gt;Tips:默认类型&lt;/strong&gt;
设置默认数据类型的方法：&lt;/p&gt;

    &lt;p&gt;torch.set_default_tensor_type(torch.DoubleTensor)&lt;/p&gt;

    &lt;p&gt;否则默认数据类型为torch.FloatTensor&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;随机初始化&lt;/p&gt;

    &lt;p&gt;rand()表示在[0,1]之间均匀采样
rand_like()的参数是tensor
randint(min,max,shape)
randn(shape)表示正态分布的随机数据
normal(mean,std)
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710161307163.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;其他常用API
  full(shape, num) 生成一个全为num的，shape为shape的数
 arange/range(min,max,step)
 linspace/logspace(min,max,total_num)
  ones/zeros/eye(shape),其中eye()只能是一维或两维
  randperm(num)随机打散num个数
    &lt;h2 id=&quot;索引与切片&quot;&gt;索引与切片&lt;/h2&gt;
  &lt;/li&gt;
  &lt;li&gt;下标索引
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710162547570.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/li&gt;
  &lt;li&gt;切片
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710162846681.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/li&gt;
  &lt;li&gt;隔行采样
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710163136926.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
&lt;strong&gt;两个冒号连在一起表示隔行取样：：&lt;/strong&gt;，中间没有逗号&lt;/li&gt;
  &lt;li&gt;具体维度下的采样
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710163648259.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;…&lt;/strong&gt;任意多的维度
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710163852566.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
主要用在取中间的全部，但是最后一个维度另外操作，比如隔行取样&lt;/li&gt;
  &lt;li&gt;掩码索引
使用不多，因为会把数据打平
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710164644678.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/li&gt;
  &lt;li&gt;打平索引
使用不多
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710164844341.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
    &lt;h2 id=&quot;维度变换&quot;&gt;维度变换&lt;/h2&gt;
    &lt;p&gt;操作方法&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;view/reshape()，功能一样
只要保证总的数据维度不变（1.）
忽略位置信息，通道信息，适合全连接层（2.）
只关注宽的信息，把所有照片的行和通道信息合并不关注（3.）
照片和通道信息合并，不关注来自哪张照片和哪个通道，只看行列信息（4.）
恢复数据时必须满足之前的维度信息
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710171406429.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;squeeze()/unsqueeze()
 &lt;strong&gt;unsqueeze()&lt;/strong&gt;
 index为正数，则在index之前插入，index是负数，则在index位置之后插入。前后按照正常的从左到右，右边是后面，&lt;strong&gt;插入的是是维度&lt;/strong&gt;
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710172412808.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710173311599.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
&lt;strong&gt;squeeze()&lt;/strong&gt;
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710175654824.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;expand/repeat()
&lt;strong&gt;expend()&lt;/strong&gt;只能将原来是1的地方变为N，-1表示保持不变
不会主动复制数据，推荐
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710192301568.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;repeat()&lt;/strong&gt;参数表示对应位置拷贝的次数&lt;/p&gt;

    &lt;p&gt;不推荐，因为会更改memory，占有内存变多，不能使用原来的数据
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710192535470.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;** .t()**&lt;/li&gt;
&lt;/ol&gt;

   		矩阵的转置，只能适用于二维矩阵

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Transpose()&lt;/strong&gt; 一次只能两两交换&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710194139728.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;permute()&lt;/strong&gt;参数对应依次表示变换之前的下标
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710194744165.png&quot; alt=&quot;在这里插入图片描述&quot; /&gt;&lt;/li&gt;
&lt;/ol&gt;</content><author><name>{&quot;name&quot;=&gt;nil, &quot;email&quot;=&gt;nil, &quot;twitter&quot;=&gt;nil}</name></author><summary type="html">创建tensor</summary></entry><entry><title type="html">Pytorch学习笔记之基本数据类型</title><link href="https://thetaqing.github.io/blog//2019/07/10/pytorch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/" rel="alternate" type="text/html" title="Pytorch学习笔记之基本数据类型" /><published>2019-07-10T00:00:00+00:00</published><updated>2019-07-10T00:00:00+00:00</updated><id>https://thetaqing.github.io/blog//2019/07/10/pytorch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B</id><content type="html" xml:base="https://thetaqing.github.io/blog//2019/07/10/pytorch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/">&lt;h2 id=&quot;数据类型&quot;&gt;数据类型&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710094520590.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;python与pytorch的数据类型对照&quot; /&gt;
那么怎么表达string呢？&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;one-hot
[1 0] dog	[0 1] cat
每一个位置代表一个意思，相关性不强&lt;/li&gt;
  &lt;li&gt;Embedding编码&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710095455293.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;Data type&quot; /&gt;
打勾部分是常用的类型&lt;/p&gt;
&lt;h2 id=&quot;数据表示&quot;&gt;数据表示&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;维度为0的标量用torch.tensor(1.3)表示。&lt;/li&gt;
  &lt;li&gt;维度为1的向量/张量
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710101239635.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;维度为1的张量创建方法&quot; /&gt;&lt;/li&gt;
  &lt;li&gt;维度为2 的向量表示
&lt;img src=&quot;https://img-blog.csdnimg.cn/2019071010455878.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;二维向量&quot; /&gt;&lt;/li&gt;
  &lt;li&gt;维度为3
&lt;img src=&quot;https://img-blog.csdnimg.cn/2019071011322131.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;在这里插入图片描述&quot; /&gt;
适合处理RNN，文字处理&lt;/li&gt;
  &lt;li&gt;维度为4
&lt;img src=&quot;https://img-blog.csdnimg.cn/20190710113044543.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RoZXJhX3Fpbmc=,size_16,color_FFFFFF,t_70&quot; alt=&quot;维度为4&quot; /&gt;
适合神经网络，图像处理&lt;/li&gt;
&lt;/ul&gt;</content><author><name>{&quot;name&quot;=&gt;nil, &quot;email&quot;=&gt;nil, &quot;twitter&quot;=&gt;nil}</name></author><summary type="html">数据类型 那么怎么表达string呢？ one-hot [1 0] dog [0 1] cat 每一个位置代表一个意思，相关性不强 Embedding编码</summary></entry></feed>