在github上如果看到有很不错的项目和作品,一般我们可以进行三种操作:那就是watch, star和fork.
Ubuntu 16.04下同步github中fork出来的分支
在github上fork出一些感觉比较好的项目,已经做了部分修改,由于某些原因,无法通过pull request合并到原作者的分支,但是想把原项目的最近更新代码合并进来,可以通过git fetch原始项目到本地,通过git merge的进行代码合并。
以fork出来的pyseeta项目为例
- 检出自己的代码
 
| 
					 1  | 
						$ git checkout https://github.com/wangqiang1588/pyseeta.git  | 
					
- 把原作者项目地址添加到刚刚检出的项目
 
| 
					 1  | 
						$ git remote add upstream https://github.com/TuXiaokang/pyseeta.git  | 
					
- 从原作者仓库获取到分支,及相关的提交信息
 
| 
					 1  | 
						$ git fetch upstream  | 
					
- 切换到想合并代码的分支
 
| 
					 1  | 
						$ git checkout master  | 
					
- 代码合并,并解决冲突
 
| 
					 1  | 
						$ git merge upstream/master  | 
					
- 提交合并后的代码
 
| 
					 1  | 
						$ git commit -m "merge ......."  | 
					
- 推送提交到远程服务器
 
| 
					 1  | 
						$ git push  | 
					
参考链接
TypeError: slice indices must be integers or None or have an __index__ method
执行如下Python脚本时
| 
					 1 2 3 4 5 6 7 8 9 10 11  | 
						import numpy as np dy = [0,1,2] edy = [0,1,2] dy[0:2] = np.fix(dy[0:2]) edy[0:2] = np.fix(edy[0:2]) box = [[1,2,3,4],[3,4,5,6]] box2 = box[dy[0]:edy[2]]  | 
					
会遇到错误信息
| 
					 1 2 3 4  | 
						Traceback (most recent call last):   File "test.py", line 11, in <module>     box2 = box[dy[0]:edy[2]] TypeError: slice indices must be integers or None or have an __index__ method  | 
					
这个提示非常具有迷惑性,会让人不知所措。
其实非常简单
| 
					 1  | 
						dy[0:2] = np.fix(dy[0:2])  | 
					
这行导致整个数组变成了浮点数格式的数组,可以试着打印出结果,就会发现整个里面的数字完全变成了浮点数。
而浮点数是不可以用来作为数组的下标的。
修改成如下方式即可
| 
					 1 2 3 4 5 6 7 8 9 10 11  | 
						import numpy as np dy = [0,1,2] edy = [0,1,2] dy[0:2] = np.fix(dy[0:2]) edy[0:2] = np.fix(edy[0:2]) box = [[1,2,3,4],[3,4,5,6]] box2 = box[int(dy[0]):int(edy[2])]  | 
					
请注意最后的
| 
					 1  | 
						int()  | 
					
函数,强制转换浮点数为整数。
参考链接
微软Xbox One S音频输出到普通喇叭
Xbox One S国行版,结果到货后,非常悲剧的发现音频输出部分只支持HDMI跟光纤音箱的接口。注意:主机上那个貌似耳机插孔的地方,实际上是外置红外接收器的接口(接口附近有IR Input字样)Xbox One S自带的手柄上,有一个耳机的插孔,可以插上耳机,但是问题是,长时间带耳机会比较夹耳朵,因此还是用以前买的小喇叭最好了(本人对音质的追求不高)。
搜索了半天,在淘宝找到两个解决方法:
如何用Python PIL获取图片的RGB数值
| 
					 1 2 3 4 5 6 7 8 9  | 
						from PIL import Image im = Image.open("xxx.jpeg") pix = im.load() width = im.size[0] height = im.size[1] for x in range(width):     for y in range(height):         r, g, b = pix[x, y]         print(r, g, b)  | 
					
参考链接
Ubuntu 16.04命令行监视Nvidia显卡使用情况
在使用GPU做计算,比如跑Deep Learning代码的时候,我们可能希望能够实时检测显存的使用情况。
Nvidia自带了一个名为nvidia-smi的命令行工具,会显示显存使用情况,但这个命令行工具只能输出一次结果,不支持持续监控输出。
这时候就需要用到watch命令了.
Ubuntu 16.04编译GPU(CUDA)版本的Caffe
- 配置编译环境
 
参照Ubuntu 14.04,14.10,16.04编译CPU版本Caffe确保可以正常编译通过CPU版本的Caffe.
- 安装Nvidia CUDA驱动
 
参照Ubuntu 16.04安装Nvidia CUDA驱动安装Nvidia CUDA驱动.
- 安装Nvidia CUDNN库
 
参照Ubuntu 16.04开启dlib对于AVX或者CUDA的支持里面设置Nvidia CUDNN部分,配置好Nvidia CUDNN库.
- 编译代码
 
| 
					 1 2 3 4 5  | 
						$ cd caffe $ cmake . -DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" $ make  | 
					
- 编译出错的处理
 
如果编译时候出现如下错误信息
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13  | 
						/usr/include/string.h: In function ‘void* __mempcpy_inline(void*, const void*, size_t)’: /usr/include/string.h:652:42: error: ‘memcpy’ was not declared in this scope    return (char *) memcpy (__dest, __src, __n) + __n;                                           ^ CMake Error at cuda_compile_generated_softmax_layer.cu.o.cmake:266 (message):   Error generating file   /home/longsky/Source/caffe/src/caffe/CMakeFiles/cuda_compile.dir/layers/./cuda_compile_generated_softmax_layer.cu.o src/caffe/CMakeFiles/caffe.dir/build.make:469: recipe for target 'src/caffe/CMakeFiles/cuda_compile.dir/layers/cuda_compile_generated_softmax_layer.cu.o' failed make[2]: *** [src/caffe/CMakeFiles/cuda_compile.dir/layers/cuda_compile_generated_softmax_layer.cu.o] Error 1 CMakeFiles/Makefile2:272: recipe for target 'src/caffe/CMakeFiles/caffe.dir/all' failed make[1]: *** [src/caffe/CMakeFiles/caffe.dir/all] Error 2  | 
					
则需要在执行cmake的时候增加
| 
					 1  | 
						-DCUDA_NVCC_FLAGS="-D_FORCE_INLINES"  | 
					
的定义。
- 编译选项
 
某些库在使用Nvidia CUDNN的时候会崩溃,此时我们可以在编译的时候,排除Nvidia CUDNN即可。
例子如下:
| 
					 1  | 
						$ cmake . -DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" -DUSE_CUDNN=OFF  | 
					
参考链接
Python 2.7在调用自定义类函数时候报错"exceptions.TypeError: 'int' object is not callable"
Python 2.7中按照如下方式定义类
| 
					 1 2 3 4 5 6 7 8 9 10 11  | 
						class Rectangle:     def __init__(self,l):         self.left = l     def left(self):         return self.left if __name__ == '__main__':     rect = Rectangle(1)     print rect.left()  | 
					
在执行脚本的时候会报告如下错误
| 
					 1 2 3 4  | 
						Traceback (most recent call last):   File "test.py", line 11, in <module>     print rect.left() TypeError: 'int' object is not callable  | 
					
造成错误的原因在于Python 2.7中当类的成员变量跟方法名重名的时候,默认是使用类的成员变量,而成员变量是无法被调用的,因而导致报错。
解决方法就是避免方法名跟成员变量同名即可,比如上面的定义修改成如下方式即可
| 
					 1 2 3 4 5 6 7 8 9 10 11  | 
						class Rectangle:     def __init__(self,l):         self.l = l     def left(self):         return self.l if __name__ == '__main__':     rect = Rectangle(1)     print rect.left()  | 
					
Python 2.7使用multiprocessing报错PicklingError: Can't pickle : attribute lookup __builtin__.instancemethod failed 
  Python 2.7中使用下面代码的时候
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18  | 
						import multiprocessing class examplePool:     def __init__(self):         self.pool = multiprocessing.Pool()     def run(self,args):         print args     def runProcessPool(self):         self.pool.apply(self.run,(60,))         self.pool.close()         self.pool.join() if __name__ == '__main__':     pool = examplePool()     pool.runProcessPool()  | 
					
会报告如下错误:
| 
					 1 2 3 4 5 6 7 8 9 10  | 
						Traceback (most recent call last):   File "text.py", line 18, in <module>     pool.runProcessPool()	   File "text.py", line 12, in runProcessPool     self.pool.apply(self.run,(60,))   File "/usr/lib/python2.7/multiprocessing/pool.py", line 244, in apply     return self.apply_async(func, args, kwds).get()   File "/usr/lib/python2.7/multiprocessing/pool.py", line 567, in get     raise self._value cPickle.PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed  | 
					
这种错误发生的原因是Python 2.7版本中的multiprocessing模块的进程池部分存在BUG,无法正常处理这种写法的代码,据说在Python 3.4版本之后解决了此问题。
目前测试来看,不用Python 2.7中的进程池,而是自己创建进程,自己管理进程的方式,可以比较简单的解决此类问题。
比如代码修改成如下样子,即可正常运行
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  | 
						import multiprocessing class exampleProcess:     def __init__(self):         self.process = multiprocessing.Process(target=self.run,args=(60,))     def run(self,args):         print args     def runProcess(self):         self.process.start()         self.process.join() if __name__ == '__main__':     process = exampleProcess()     process.runProcess()  | 
					
至于多进程的管理,则只需要使用
| 
					 1  | 
						from multiprocessing import Process, Queue  | 
					
引入multiprocessing模块的Queue,实现一个简单的生产者,消费者模型即可。
参考链接
WARNING:libav.swscaler:Warning: data is not aligned! This can lead to a speedloss
在Python中使用libav视频解码的时候,如果需要更改最后输出的视频的宽高,比如如下代码:
| 
					 1 2 3 4 5 6 7 8 9  | 
						width = 240 container = av.open(url) video_stream = next(s for s in container.streams if s.type == 'video') for packet in container.demux(video_stream):    for frame in packet.decode():         if frame is None:             break         frame = frame.reformat(width,height)         img = frame.to_image()  | 
					
可能会收到一条警告信息
| 
					 1  | 
						WARNING:libav.swscaler:Warning: data is not aligned! This can lead to a speedloss  | 
					
导致警告的原因是swscaler的缩放的目标尺寸不合适,它预期的大小是16的倍数,这个倍数可以保证swscaler以最高效的方式进行图片的缩放处理。
解决警告的方式就是保证宽高都是16的倍数即可。
参考链接
[swscaler] Warning: data is not aligned! This can lead to a speedloss 的解决方法【FFmpeg】