小行星

热爱平淡,向往未知


  • 首页

  • 分类

  • 归档

  • 关于

  • 阅读排行

  • 搜索

环境变量-python

发表于 2018-03-Tue | 阅读次数:
1
2
3
PYTHONPATH

export PYTHONPATH=".:$PYTHONPATH"

pytorch使用学习

发表于 2018-03-Thu | 阅读次数:

优点

多 GPU 支持,自定义数据加载器,极简的预处理过程

模块

PyTorch 张量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
torch.Tensor(5, 3)

---------------------------------------

2.4878e+04 4.5692e-41 2.4878e+04

4.5692e-41 -2.9205e+19 4.5691e-41

1.2277e-02 4.5692e-41 -4.0170e+19

4.5691e-41 1.2277e-02 4.5692e-41

0.0000e+00 0.0000e+00 0.0000e+00

[torch.FloatTensor of size 5x3]



torch.Tensor(5, 3).uniform_(-1, 1)

---------------------------------------------

-0.2767 -0.1082 -0.1339

-0.6477 0.3098 0.1642

-0.1125 -0.2104 0.8962

-0.6573 0.9669 -0.3806

0.8008 -0.3860 0.6816

[torch.FloatTensor of size 5x3]


>>> torch.FloatTensor([[1, 2, 3], [4, 5, 6]])

1 2 3

4 5 6

[torch.FloatTensor of size 2x3]

>>> print(x[1][2])

6.0

>>> x[0][1] = 8

>>> print(x)

1 8 3

4 5 6

[torch.FloatTensor of size 2x3]

cpu 2 gpu

1
2
3
4
5
6
7
8
9
10
11
x = torch.FloatTensor(5, 3).uniform_(-1, 1)

print(x)

x = x.cuda(device=0)

print(x)

x = x.cpu()

print(x)

数学运算,自动求导模块,最优化模块,神经网络模块

python 提取所有函数

发表于 2018-02-Wed | 阅读次数:
1
2
3
model_names = sorted(name for name in models.__dict__
if name.islower() and not name.startswith("__")
and callable(models.__dict__[name]))

argparse metavar使用

发表于 2018-02-Wed | 阅读次数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
parser.add_argument('-s', '--stacks', default=8, type=int, metavar='N',
help='Number of hourglasses to stack')
parser.add_argument('--features', default=256, type=int, metavar='N',
help='Number of features in the hourglass')
parser.add_argument('-b', '--blocks', default=1, type=int, metavar='N',
help='Number of residual modules at each location in the hourglass')
parser.add_argument('--num-classes', default=16, type=int, metavar='N',
help='Number of keypoints')
------
optional arguments:
-h, --help show this help message and exit
--arch ARCH, -a ARCH model architecture: hg | preresnet110 | preresnet1202
| preresnet20 | preresnet32 | preresnet44 |
preresnet56 (default: resnet18)
-s N, --stacks N Number of hourglasses to stack
--features N Number of features in the hourglass
-b N, --blocks N Number of residual modules at each location in the
hourglass
--num-classes N Number of keypoints
-j N, --workers N number of data loading workers (default: 4)
--epochs N number of total epochs to run
--start-epoch N manual epoch number (useful on restarts)
--train-batch N train batchsize
--test-batch N test batchsize

shadowsocks ERROR method chacha20-ietf-poly1305 not supported

发表于 2018-02-Wed | 阅读次数:

使用python3

python encode decode

发表于 2018-02-Mon | 阅读次数:

encode

其他编码->unicode

decode

unicode->其他编码

unicode和utf8

unicode

正如上一节所说,世界上存在着多种编码方式,同一个二进制数字可以被解释成不同的符号。因此,要想打开一个文本文件,就必须知道它的编码方式,否则用错误的编码方式解读,就会出现乱码。为什么电子邮件常常出现乱码?就是因为发信人和收信人使用的编码方式不一样。

可以想象,如果有一种编码,将世界上所有的符号都纳入其中。每一个符号都给予一个独一无二的编码,那么乱码问题就会消失。这就是 Unicode,就像它的名字都表示的,这是一种所有符号的编码
。

Unicode 当然是一个很大的集合,现在的规模可以容纳100多万个符号。每个符号的编码都不一样,比如,U+0639表示阿拉伯字母Ain,U+0041表示英语的大写字母A,U+4E25表示汉字严。具体的符号对应表,可以查询unicode.org,或者专门的汉字对应表。

需要注意的是,Unicode 只是一个符号集,它只规定了符号的二进制代码,却没有规定这个二进制代码应该如何存储。

比如,汉字严的 Unicode 是十六进制数4E25,转换成二进制数足足有15位(100111000100101),也就是说,这个符号的表示至少需要2个字节。表示其他更大的符号,可能需要3个字节或者4个字节,甚至更多。

这里就有两个严重的问题,第一个问题是,如何才能区别 Unicode 和 ASCII ?计算机怎么知道三个字节表示一个符号,而不是分别表示三个符号呢?第二个问题是,我们已经知道,英文字母只用一个字节表示就够了,如果 Unicode 统一规定,每个符号用三个或四个字节表示,那么每个英文字母前都必然有二到三个字节是0,这对于存储来说是极大的浪费,文本文件的大小会因此大出二三倍,这是无法接受的。

它们造成的结果是:1)出现了 Unicode 的多种存储方式,也就是说有许多种不同的二进制格式,可以用来表示 Unicode。2)Unicode 在很长一段时间内无法推广,直到互联网的出现。

utf8

互联网的普及,强烈要求出现一种统一的编码方式。UTF-8 就是在互联网上使用最广的一种 Unicode 的实现方式。其他实现方式还包括 UTF-16(字符用两个字节或四个字节表示)和 UTF-32(字符用四个字节表示),不过在互联网上基本不用。重复一遍,这里的关系是,UTF-8 是 Unicode 的实现方式之一。

UTF-8 最大的一个特点,就是它是一种变长的编码方式。它可以使用1~4个字节表示一个符号,根据不同的符号而变化字节长度。

UTF-8 的编码规则很简单,只有二条:

1)对于单字节的符号,字节的第一位设为0,后面7位为这个符号的 Unicode 码。因此对于英语字母,UTF-8 编码和 ASCII 码是相同的。

2)对于n字节的符号(n > 1),第一个字节的前n位都设为1,第n + 1位设为0,后面字节的前两位一律设为10。剩下的没有提及的二进制位,全部为这个符号的 Unicode 码。

下表总结了编码规则,字母x表示可用编码的位。

常见编码

utf8: /xe4/xb8/xad/xe6/x96/x87
unicode: u’’

示例

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python  
#coding=utf-8
s="中文"

if isinstance(s, unicode):
#s=u"中文"
print s.encode('gb2312')
else:
#s="中文"
print s.decode('utf-8').encode('gb2312')

其他

如:s=’中文’
如果是在utf8的文件中,该字符串就是utf8编码,如果是在gb2312的文件中,则其编码为gb2312。

已经是unicode

如果一个字符串已经是unicode了,再进行解码则将出错,因此通常要对其编码方式是否为unicode进行判断:
isinstance(s, unicode) #用来判断是否为unicode

str.class

使用str.class可以查看str的编码形式

h5py使用

发表于 2018-02-Mon | 阅读次数:

groups类比词典,dataset类比Numpy中的数组

读取H5文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>> import h5py
>>> import numpy as np
>>> # 打开文件
>>> f = h5py.File('test-dev.h5', 'r')
>>> f.keys()
[u'my_xmax', u'my_xmin', u'my_ymax', u'my_ymin']
>>> xmax = f['my_xmax']

>>> xmax = f['my_xmax']
>>> type(xmax)
h5py._hl.dataset.Dataset
>>> xmax = f['my_xmax'][:]
>>> type(xmax)
numpy.ndarray

>>> xmax.shape
(1257351,)
>>> xmax.dtype
dtype('int64')
>>> xmax[...]
array([527, 260, 638, ..., 365, 334, 262])

写入H5文件

1
2
3
4
5
6
7
8
9
10
11
12
>>> f = h5py.File('test-dev.h5','w')

>>> f.create_dataset('bndbox', data=h5_bndbox)
>>> f.create_dataset('imgname', data=h5_imgname)
>>> f.create_dataset('part', data=h5_part)


>>> f['bndbox'] = h5_bndbox
>>> f['imgname'] = h5_imgname
>>> f['part'] = h5_part

>>> f.close()

字符串的特殊处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>>> f = h5py.File('annot.h5','r')
>>> imgname = f['imgname'][:]
>>> imgname.shape
(146545, 31)
>>> img0 = imgname[0]
>>> img.dtype
dtype('float64')
>>> img0[...]
array([ 67., 79., 67., 79., 95., 116., 114., 97., 105.,
110., 50., 48., 49., 52., 95., 48., 48., 48.,
48., 48., 48., 50., 54., 50., 49., 52., 53.,
46., 106., 112., 103.])

# 先转化编码格式
>>> img0 = img0.astype(np.uint8)
>>> img0.tostring()
'COCO_train2014_000000262145.jpg'

>>> img0.tostring().decode('ascii')
u'COCO_train2014_000000262145.jpg'

np.fromstring(imagename,dtype=np.uint8).astype('float64')
# 写进h5
f.create_dataset('imgname', data=imgname)

http://jeff-leaf.site/2017/09/29/Python%E5%A4%84%E7%90%86HDF5%E6%96%87%E4%BB%B6/

name_scope和 variable_scope区别

发表于 2018-02-Mon | 阅读次数:

变量的全局名和variable_scope有关,和name_scope无关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import tensorflow as tf
v = tf.Variable( [2,2,3,32],name='weights')
with tf.variable_scope('variable_scope'):
v1 = tf.get_variable('weights', [2,2,3,32])

with tf.name_scope('name_scope'):
v2 = tf.get_variable('weights',[2,2,3,32])

print v.name
print v1.name
print v2.name
---
weights:0
variable_scope/weights:0
weights_1:0

tensorflow cpu

发表于 2018-02-Mon | 阅读次数:
1
2
conda create -n tf-cpu python=2.7
conda install -c conda-forge tensorflow

conda prompt disable

发表于 2018-02-Mon | 阅读次数:

~/.condarc

1
changeps1: False

1…474849…59
fangyh

fangyh

最爱的是那苍穹之外的浩渺宇宙

588 日志
4 分类
66 标签
© 2020 fangyh
由 Hexo 强力驱动
|
主题 — NexT.Mist v5.1.3
|本站总访问量次