- assert语法:
- assert expression 在expression为false的时候终止程序
- 等价于
if not expression: raise AssertionError(arguments)
- isinstance语法:
- isinstance(object,classtuple) 判断object是否是classtuple中的类型
- python staticmethod 返回函数的静态方法。
-
class C(object): @staticmethod def f(arg1, arg2, ...):
以上实例声明了静态方法 f,从而可以实现实例化使用 C().f(),当然也可以不实例化调用该方法 C.f()
-
-
numpy 和 torch中 切片设置参数None的做法。
-
本质上实际上是增加一个维度。与numpy的reshape和torch的view中设置参数-1功能相同
-
a=np.array([1,2,3,4]) b=a[:,None]
b array([[1], [2], [3], [4]])
-
- 图片读入成numpy.array形式。然后用plt.imshow()进行展示。转换方式有以下:
- matplotlib方法 matplotlib.image或者 io.imread(path)读取成nparray数据格式
- PIL.Image.open(path) 读取成图片格式。可以直接显示也可以用np.array()处理成nparray格式
- opencv方法 cv2.imread(path) 读取出nparray格式。但是注意以下cv2对应于bgr格式。这个大多时候需要转换成rgb格式
- python中__dict__。对象.__dict__以字典形式存储了self.xxx的名字和对象值
- 在实参前面加*号能够将迭代器拆分成一个个的元素。
- 实际使用样例(Sequential作为一个容器要么接收orderdict类型的数据要么接收一个个拆分的元素。此处传入的是列表类型,所以前面要加*号传入后将列表中元素拆分出来):
def _make_layer(self, block, planes, blocks, stride=1, dilate=False): norm_layer = self._norm_layer downsample = None previous_dilation = self.dilation if dilate: self.dilation *= stride stride = 1 if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( conv1x1(self.inplanes, planes * block.expansion, stride), norm_layer(planes * block.expansion), ) layers = [] layers.append(block(self.inplanes, planes, stride, downsample, self.groups, self.base_width, previous_dilation, norm_layer)) self.inplanes = planes * block.expansion for _ in range(1, blocks): layers.append(block(self.inplanes, planes, groups=self.groups, base_width=self.base_width, dilation=self.dilation, norm_layer=norm_layer)) return nn.Sequential(*layers)
- 具体代码实验
- 输入。如果输入get_item(*list01)。输出是
def get_item(*args): for i,data in enumerate(args): print('i:{},data:{}'.format(i,data)) list01=[] for i in range(10): list01.append(i)
i:0,data:0 i:1,data:1 i:2,data:2 i:3,data:3 i:4,data:4 i:5,data:5 i:6,data:6 i:7,data:7 i:8,data:8 i:9,data:9
-
如果输入是get_item(list01)。输出是
i:0,data:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- 输入。如果输入get_item(*list01)。输出是
- 实际使用样例(Sequential作为一个容器要么接收orderdict类型的数据要么接收一个个拆分的元素。此处传入的是列表类型,所以前面要加*号传入后将列表中元素拆分出来):
- 在研究mtcnn的运算过程中有一种比较有趣的写法nonzero()函数。tensor和numpy中用到,用于返回作为数据结构中值为true的位置信息