毕设学习笔记整理
- 说明
- Python和Pycharm使用方面
- 因网络问题导致pycharm安装第三方库失败的解决办法
- 将python程序打包为exe程序
- pyinstaller
- py2exe
- pycharm取消缩进
- Python函数方面
- python中 if __name__ == '__main__': 的作用与意义
- cv2.resize的用法
- 只读取图像的单通道
- 数值最值索引
- 图像增强,数据增广的几种方法
- matlab中的一些函数
- numel()
- tic
- try/catch语句
- rand,randn,randi函数
- conv与convn的区别
- bsxfun()函数
- gzeros()
- floor()
- max()函数
- B = ceil(A)
- 调用GPU初始化数据&将数据拷贝到GPU
- 把GPU上的数据回传给CPU-——-使用gather函数
- 对比CPU和GPU两种方式哪种快?
- 保存指定变量到.mat文件
说明
毕设学习笔记整理,涉及python和matlab,部分资料已上传,有其他需要可以私信我。
Python和Pycharm使用方面
因网络问题导致pycharm安装第三方库失败的解决办法
在终端中使用pip安装,选用国内的资源链接。
比如:安装lxml库和requests库
输入:
pip install -i https://pypi.douban.com/simple lxml
pip install -i https://pypi.douban.com/simple requests
注:其他库未尝试,以上两个亲测速度飞快
将python程序打包为exe程序
pyinstaller
1、安装pyinstaller
2、输入命令:
pyinstaller -F **\**\**.py
如图里标黄所示,-F 后面内容为你想打包的python文件。
3、查找自己打包的exe文件需要在dist文件夹下查找。
参考
py2exe
py2exe 似乎只能支持 python3.3 和 pyhton3.4
参考
pycharm取消缩进
pycharm编辑器的缩进和取消缩进快捷键:
整体缩进:
tab
整体取消缩进:
tab+shift
Python函数方面
python中 if name == ‘main’: 的作用与意义
作用是以下程序只运行在主程序中,即当该程序作为子程序import到其他程序中时,以下程序不运行。
cv2.resize的用法
img_new = cv2.resize(img, crop_size, interpolation=cv2.INTER_CUBIC)
# CV_INTER_NN - 最近邻插值,
# CV_INTER_LINEAR - 双线性插值 (缺省使用)
# CV_INTER_AREA - 使用象素关系重采样。当图像缩小时候,该方法可以避免波纹出现。当图像放大时,类似于 CV_INTER_NN 方法..
# CV_INTER_CUBIC - 立方插值. 保留图像边缘与对比度。
注:不能存在中文路径,此处错误大多为文件路径造成。
只读取图像的单通道
法1:PIL
from PIL import Image
pil_im = Image.open('empire.jpg').convert('L') # 转为灰度图像
法2:cv2
import cv2
img = cv2.imread(jpg_name, cv2.IMREAD_GRAYSCALE)
数值最值索引
max()函数有一个应用很巧妙的参数key,在这里定义为operator.itemgetter(1),表示对enumerate(x)每个元素的第一维做比较(从0维开始),然后返回第一维值最大的元素,即包含索引和数值。
x = [3, 2.2, 7.4, 6, 4]
min_index, min_number = min(enumerate(x), key=operator.itemgetter(1))
# min_index=1, min_number =2.2max_index, max_number = max(enumerate(x), key=operator.itemgetter(1))
# max_index=2, max_number = 7.4
参考
图像增强,数据增广的几种方法
考虑到本课题图像的特殊性,不能进行几何变换,因此这里只用了几种色彩变换的方法。
from PIL import Image, ImageEnhance, ImageOps, ImageFile
import matplotlib.pyplot as plt
jpg_name = 'PVC_module_remove_1.jpg'
pil_im = Image.open(jpg_name).convert('L') # 打开单通道(转为灰度图像)# # 调整图像的饱和度 Color
# random_factor = np.random.randint(10, 31) / 10. # 随机因子
# image_enhance_1 = ImageEnhance.Color(pil_im).enhance(random_factor) # 调整图像的饱和度# 调整图像的亮度 Brightness
random_factor_2 = np.random.randint(10, 21) / 10. # 随机因子
image_enhance_2 = ImageEnhance.Brightness(pil_im).enhance(random_factor_2) # 调整图像的亮度# 调整图像的对比度 Contrast
random_factor_3 = np.random.randint(10, 21) / 10. # 随机因子
image_enhance_3 = ImageEnhance.Contrast(pil_im).enhance(random_factor_3) # 调整图像对比度# 调整图像锐度 Sharpness
random_factor_4 = np.random.randint(0, 31) / 10. # 随机因子
image_enhance_4 = ImageEnhance.Sharpness(pil_im).enhance(random_factor_4) # 调整图像锐度# 调整图像亮度、对比度、锐度
random_factor_5_reg = np.random.randint(10, 21) / 10. # 随机因子
image_enhance_5_reg = ImageEnhance.Contrast(image_enhance_2).enhance(random_factor_5_reg) #
random_factor_5 = np.random.randint(0, 31) / 10. # 随机因子
image_enhance_5 = ImageEnhance.Sharpness(image_enhance_5_reg).enhance(random_factor_5) # 调整图像锐度# 是否有变化?
# changes1 = np.asarray(image_enhance_1) - np.asarray(pil_im)
changes2 = np.asarray(image_enhance_2) - np.asarray(pil_im)
changes3 = np.asarray(image_enhance_3) - np.asarray(pil_im)
changes4 = np.asarray(image_enhance_4) - np.asarray(pil_im)# 添加高斯噪声
# 噪声扰动
image_enhance_6 = add_gaussian_noise(pil_im, 30)# 显示
plt.figure("原图像")
plt.imshow(pil_im, cmap=plt.cm.gray)
# plt.show()
# plt.figure("调整图像的饱和度")
# plt.imshow(image_enhance_1,cmap=plt.cm.gray)
# plt.show()
plt.figure("调整图像的亮度")
plt.imshow(image_enhance_2, cmap=plt.cm.gray)
# plt.show()
plt.figure("调整图像的对比度")
plt.imshow(image_enhance_3, cmap=plt.cm.gray)
# plt.show()
plt.figure("调整图像的锐度")
plt.imshow(image_enhance_4, cmap=plt.cm.gray)plt.figure("调整图像亮度、对比度、锐度")
plt.imshow(image_enhance_5, cmap=plt.cm.gray)plt.figure("添加噪声扰动后的图像")
plt.imshow(image_enhance_6, cmap=plt.cm.gray)
# plt.show()plt.figure("changes4")
plt.imshow(changes4, cmap=plt.cm.gray)
plt.show()# 保存图像不留空白显示,保存
plt.xticks([]), plt.yticks([]) # 隐藏x、y轴
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
plt.imshow(img2,cmap=plt.cm.gray)
plt.show()
伽马校正
def adjust_gamma(image):image_reg = np.array(image)random_factor = np.random.uniform(0.5, 1.5) # 随机因子img1 = np.power(image_reg / float(np.max(image_reg)), random_factor)return img1
程序下载链接
matlab中的一些函数
numel()
获取数组(结构体)中的元素数量
例如:num = numel(cnn.layer);
tic
通常与toc连用,计算程序运行时间。
try/catch语句
try的作用是让Matlab尝试执行一些语句, 执行过程中如果出错, 则执行catch部分的语句.
其语法:
try
尝试执行的语句块;
catch
出错后执行的语句块;
end
例如:
tryc = gpuArray(rand(varargin{:}));
catchc = rand(varargin{:});
end
rand,randn,randi函数
1,rand 生成均匀分布的伪随机数。分布在(0~1)之间
主要语法:
rand(m,n)生成m行n列的均匀分布的伪随机数
rand(m,n,‘double’)生成指定精度的均匀分布的伪随机数,参数还可以是’single’
rand(RandStream,m,n)利用指定的RandStream(我理解为随机种子)生成伪随机数
2,randn 生成标准正态分布的伪随机数(均值为0,方差为1)
主要语法:和上面一样
3,randi 生成均匀分布的伪随机 整数
主要语法:
randi(iMax)在开区间(0,iMax)生成均匀分布的伪随机整数
randi(iMax,m,n)在开区间(0,iMax)生成mXn型随机矩阵
r = randi([iMin,iMax],m,n)在开区间(iMin,iMax)生成mXn型随机矩阵
conv与convn的区别
conv只适用于两个一维信号的卷积运算
convn适合多维卷积运算;
例如:20 * 20 * 100 * 20与2 * 2卷积运算10 * 10 * 100 * 20
%full : 就是普通意义下的卷积
% same: 就是 和卷积输入的长度一样
% valid : 就是 卷积反转对齐之后,这里的对齐很特殊,不能有填充0;
bsxfun()函数
C = bsxfun(fun,A,B)使由函数句柄fun指定的逐元素二进制操作适用于数组A和B,并启用了单例扩展。
fun可以是以下内置功能之一:
语法 | 功能 |
---|---|
@plus | Plus |
@minus | Minus times .Array multiply |
@rdivide | Right array divide |
@ldivide | Left array divide |
@power | Array power |
@max | Binary maximum |
@min | Binary minimum |
@ren | Remainder after division |
@mod | Modulus after division |
@atan2 | Four quadrant inverse tangent |
@hypot | Square root of sum of squares |
@eq | Equal |
@he | Not equal |
@lt | Less than |
@le | Less than or equal to |
@pt | Greater than |
@ge | Greater than or equal to |
@and | Element-wise logical AND |
@or | Element-wise logical OR |
@sor | Logical exclusive OR |
gzeros()
return zeros array on GPU
floor()
floor函数:朝负无穷大方向取整
y = floor(x) 函数将x中元素取整,值y为不大于本身的最大整数。对于复数,分别对实部和虚部取整
max()函数
c = max(A) 返回A中的最大元素
[C,I] = max(data_array); 返回A中的最大元素赋值给C,将A中的最大元素的位置赋值给I.
B = ceil(A)
返回(对于每一个A中元素)不小于(>=)的值
调用GPU初始化数据&将数据拷贝到GPU
N = 6;
M = magic(N);%在CPU生成数组
G = gpuArray(M); %将数据从CPU拷贝到GPU
或
A = zeros(10, 'gpuArray');
把GPU上的数据回传给CPU-——-使用gather函数
最后是如何把GPU上的数据回传给CPU:
B = gather (A);
其中A是GPU上的数据,B是CPU上的数据。B的内容在回传之后等于A。
对比CPU和GPU两种方式哪种快?
%%
clc
clear all% %% 首先以200*200的矩阵做加减乘除做比较
% t = zeros(1,100);
% A = rand(200,200);B = rand(200,200);C = rand(200,200);
% for i=1:100
% tic;
% D=A+B;E=A.*D;F=B./(E+eps);
% t(i)=toc;
% end;
% t_m = mean(t);
% fprintf('CPU平均运行时间:%4f\n',t_m);
% %%%%ans = 2.4812e-04
%
% t1 = gpuArray(zeros(1,100));
% A1 = gpuArray(rand(200,200));
% B1 = gpuArray(rand(200,200));
% C1 = gpuArray(rand(200,200));
%
% for i=1:100
% tic;
% D1=A1+B1;E1=A1.*D1;F1=B1./(E1+eps);
% t1(i)=toc;
% end;
% t1_m = mean(t1);
% fprintf('GPU平均运行时间:%4f\n',t1_m);
% fprintf('速度快了%4f倍!\n',t_m / t1_m);
% %% 结果
% % CPU运行时间:0.000290
% % GPU平均运行时间:0.000429
% % 速度快了0.675780倍!%% 然后将矩阵大小提高到2000*2000做实验t = zeros(1,100);
A = rand(2000,2000);B = rand(2000,2000);C = rand(2000,2000);
for i=1:100tic;D=A+B;E=A.*D;F=B./(E+eps);t(i)=toc;
end;
t_m = mean(t);
fprintf('CPU平均运行时间:%4f\n',t_m);t1 = gpuArray(zeros(1,100));
A1 = gpuArray(rand(2000,2000));
B1 = gpuArray(rand(2000,2000));
C1 = gpuArray(rand(2000,2000));for i=1:100tic;D1=A1+B1;E1=A1.*D1;F1=B1./(E1+eps);t1(i)=toc;
end;
t1_m = mean(t1);
fprintf('GPU平均运行时间:%4f\n',t1_m);
fprintf('速度快了%4f倍!\n',t_m / t1_m);
%% 结果
% CPU平均运行时间:0.025320
% GPU平均运行时间:0.000747
% 速度快了33.911511倍!
垃圾笔记本太可怜了!运算次数少CPU快,次数多GPU快!
保存指定变量到.mat文件
fileDir = '文件路径/'; % 保存文件的路径
savePath = strcat(fileDir, num2str(1), '_result.mat'); % 拼接路径和文件名
% num2str(n) 在循环中可以生成1_result.mat, 2_result.mat....
% 拼接后的文件为 : 文件路径/1_result.mat
save(savePath, 'val1', 'val2'); % 保存变量val1,val2到1_result.mat中