今天是2021.11.28日周日。虽然年纪越来越大,但心中对生活的感悟从来没有减少过。上大学以来一开始没适应好大学节奏。成绩落下了,失去了很多机会。经常超级羡慕那些能力很强各方面都很优秀的人,也活在自卑的阴影里很长时间。自己也在努力挣扎,尽量在一些方面做的不错。来东南以后,虽然见过更多耀眼的人,但是我也发现我也不是很差,慢慢就有了自信。嵌前天跑5公里的时候,想起心中受过的苦就有点悲伤,但很快就被心中积极的力量,乐观坚定的心一扫而空。原来我是真的也有自己独特的目标,坚定的精神,也富有无穷的力量。
是时候开始准备找工作了,科研现在在做CV异常检测,(坦白说,我对CV挺感兴趣的,就是有点怕卷不过),会C++比较多,前天和同学商量,java or C++ 。考虑了很久,终于在找工作阶段放弃java。工作方向就打算找CV or C++开发 或者两个的交叉方向。打算从基础的复习起来,python基础,C++基础,计算机4件套和linux。python刚上研究生看过,不过有点忘记了,虽然很简单,但是前天改网络的时候,居然写出了if flag == True 的句子。可见先复习一下吧,一方面是复习科研,一方面让自己慢慢进入找工作的状态。leetcode也要慢慢来了。
参考书籍:《python 编程 从入门到实践 第二版》 埃里克马瑟斯
一 python基础 变量
二 列表和操作列表
三 if语句 列表
四 用户输入 while循环
五 函数 类
查看python版本
python -V
python --version
退出python
exit() or ctrl+A+D
变量 并 输出
message = "hello python world"
print(message)
字符串
字符串可以直接print,他可以是双引号,也可以是单引号 字符串有一些不常用的函数 比如 upper、title、lower
f字符串
first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name}{last_name}"
print(full_name)
# 输出ada lover
\t \n 分别是制表符和换行 这些与其他语言相同
还有python与其他语言不同的一点,可以整体赋值,如x,y,z=0,0,0
python的注释有 # 单句注释和 三个单引号的分行注释
还有前几天项目里有一句话`
print('Loss: Reconstruction {:.6f}/ Compactness {:.6f}/ Separateness {:.6f}'.format(loss_pixel.item(), compactness_loss.item(), separateness_loss.item()))
和C类语言的 %d 和 %f %lf 比较相似 .6f 是保留小数点6位
列表简介 访问列表元素 修改列表元素
bicycles = ['trek','cannodale','redline','specialized']
print(bicycles)
print(bicycles[-1])# 这是打印最后一个与元素
bicycles[0] = ‘a’ #按照索引修改
bicycles.append('b')
#还有一个insert的方法
motorcycles = [1,2,3]
motorcycles.insert(0,12)
print(motorcycles)
# 输出 [12,1,2,3]
方法有很多 比如del[0]删除列表的第一个元素
pop()方法也可以使用 每次弹出列表的最后一个元素 关键是可以用变量获取他的值 如 A=B.pop()
还有一个可以通过remove删除元素的方法,加入列表A有元素1,可以通过使用A.remove(1)的方法删除 注意 只删除第一个是1的元素
#还有一个sort方法
cars=['bmw','audi','toyota','subaru']
cars.sort()
print(cars)
# 也可以用soerted临时排序
# 也有一个reverse反置列表 好像java也有这个函数吧
# 最近做项目里dataloader也是这样读数据再排序的
videos = glob.glob(os.path.join(self.dir, '*'))
for video in sorted(videos):
video_name = video.split('/')[-1]
self.videos[video_name] = {}
self.videos[video_name]['path'] = video
self.videos[video_name]['frame'] = glob.glob(os.path.join(video, '*.jpg'))
self.videos[video_name]['frame'].sort()
self.videos[video_name]['length'] = len(self.videos[video_name]['frame'])
#这里是将数据集中的文件名(视频名排序起来)
操作列表
循环
things=['sky','cloud','mirror']
for thing in things:
print(thing)
# 创建数值猎豹
for value in range(1,5)
print(value)
# 这里其实是打印第一到第四个元素1,2,3,4
# 还有一些操作
numbers = list(range(1,6))
#[1,2,3,4,5]
even_numbers = list(range(2,11,2))
# [2,4,6,8,10]
还有一些min max 的一些基础函数
切片
[0:3] 其实切出来的元素是[0,1,2] 即起始点位置的数值要取 结束点的数值不取
#之前写一个代码的时候() 因为要通道分开 就没想通 比如A是1*12*256*256的图像
#那么倒数一张图应该是
A [:,-6:-3,:,:]
复制列表的深复制和浅复制 用切片赋值 是真的两个列表 列表名的复制 仍然是一个列表
元组 元组内容不嫩更改 做CV目前用元组的不太多 字典和列表都有用到
dimentions = (200,50)
# 依然可以通过for 和 索引访问
# 只是更改不被允许
dimentions[0] = 150 # 错误
#但是可以修改元祖变量 只是元祖变量里面的值被保护而已
f语句的判断是== 不等于是!=
and or和C/C++ 中的 && ||是一致的
例如if A=0 and B=1 do XXX
检查特定值是否在列表中
banned_user = ['andrew','carolina','david']
user ='marie'
if user not in banned_user:
print("XXXXX")
if eles 语句非常基础啦
确定列表不是空的
requested_toppings = []
if requeste_toppings:
print("NO")
else
print("Yes")
字典:字典个python的第三个DS 字典在过去用的还是比较多的
alien_0 = {'color’:'green','point':5}
print(alien_0['color'])
# 添加键值对
alien['x_postion'] = 0
#创建一个空字典
alien = {}
#修改字典中的值
#删除键值对
del alien_0['point']
#用get访问值
point = alien_0。get('points','No points value assigned')
#找不到键值对时报第二个错误
#遍历字典
user = {'A':a,'B':b,'C':'c'}
for k,v in user.items():
print(k,v)
#遍历字典中所有的键
for name in A.keys()
if name = XXXX
print name
#遍历字典中的值
for name in A.values()
if name = XXXX
print name
#字典可以与列表嵌套 也可以与字典嵌套
input可以让系统等待一段时间, 等待用户输入
name = input{"pleasr enter ypour name:"}
print(f"\n hello,{name}")
while就略了 ,没有什么值得注意的
什么是函数?
def greet_user():
print("hello")
greet_user()
多少年了实参和形参还是这么重要
传递实参 ,位置实参如下:
def describe_pet(animale_type,pet_name):
print(animals_type)
print(pet_name)
describe_pet('hamster','harry')
# 顺序千万不能过
# 关键字实参顺序可以错
describe_pet(anmimal_type='harry',petname=‘dog’)
# 默认值 等效调用很简单
# 返回值
# python中的None是这样子的不是NULL
# 返回值可以返回字典
def build_person(first_name,last_name,age=None):
person = {'first':first_name,'last':last_name}
if age:
person['age'] = age
return person
musician = build_person('jimi','hendrix',age = 27)
# 传递列表
# 在函数中修改列表 请注意 传入的列表其实是传址 函数会修改列表的
# 也可以使用切片表示法防止他修改列表
# 使用任意数量的关键字实参
def build_profile(first,last,**user_info):
user_info['first_name'] = first
user_info['last_name'] = last
return user_info
user_profile = build_profile('albert','sinstein',location = 'princeton',field = 'physics')
print(user_profile)
# 结果输出了4个字典的内容
类 (书上没啥注意的)
创建类和使用类
根据类创建实例
继承 子类要修改__init__ 函数 (重写)
下面是有一篇CVPR2020的源码
体现了继承基类神经网络基类,实现了编码器和解码器
本质是U-net
class Encoder(torch.nn.Module):
def __init__(self, t_length = 5, n_channel =3):
super(Encoder, self).__init__()
def Basic(intInput, intOutput):
return torch.nn.Sequential(
torch.nn.Conv2d(in_channels=intInput, out_channels=intOutput, kernel_size=3, stride=1, padding=1),
torch.nn.BatchNorm2d(intOutput),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=intOutput, out_channels=intOutput, kernel_size=3, stride=1, padding=1),
torch.nn.BatchNorm2d(intOutput),
torch.nn.ReLU(inplace=False)
)
# 这是先卷积 再批规范化 再激活函数
def Basic_(intInput, intOutput):
return torch.nn.Sequential(
torch.nn.Conv2d(in_channels=intInput, out_channels=intOutput, kernel_size=3, stride=1, padding=1),
torch.nn.BatchNorm2d(intOutput),
torch.nn.ReLU(inplace=False)、
torch.nn.Conv2d(in_channels=intOutput, out_channels=intOutput, kernel_size=3, stride=1, padding=1),
)
# 第二部分 这部分卷积存在于第一行和最后一行
self.moduleConv1 = Basic(n_channel*(t_length-1), 64) # 用4个帧开始预测
self.modulePool1 = torch.nn.MaxPool2d(kernel_size=2, stride=2) # 最大池化
self.moduleConv2 = Basic(64, 128)
self.modulePool2 = torch.nn.MaxPool2d(kernel_size=2, stride=2)
self.moduleConv3 = Basic(128, 256)
self.modulePool3 = torch.nn.MaxPool2d(kernel_size=2, stride=2)
self.moduleConv4 = Basic_(256, 512)
self.moduleBatchNorm = torch.nn.BatchNorm2d(512)
self.moduleReLU = torch.nn.ReLU(inplace=False) # 到最后生成了这个512通道的编码单元encoding
def forward(self, x): # 这里应该返回的是前向推导的特征空间
tensorConv1 = self.moduleConv1(x)
tensorPool1 = self.modulePool1(tensorConv1)
tensorConv2 = self.moduleConv2(tensorPool1)
tensorPool2 = self.modulePool2(tensorConv2)
tensorConv3 = self.moduleConv3(tensorPool2)
tensorPool3 = self.modulePool3(tensorConv3)
tensorConv4 = self.moduleConv4(tensorPool3)
return tensorConv4, tensorConv1, tensorConv2, tensorConv3
class Decoder(torch.nn.Module):
def __init__(self, t_length = 5, n_channel =3):
super(Decoder, self).__init__()
def Basic(intInput, intOutput):
return torch.nn.Sequential(
torch.nn.Conv2d(in_channels=intInput, out_channels=intOutput, kernel_size=3, stride=1, padding=1),
torch.nn.BatchNorm2d(intOutput),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=intOutput, out_channels=intOutput, kernel_size=3, stride=1, padding=1),
torch.nn.BatchNorm2d(intOutput),
torch.nn.ReLU(inplace=False)
)
# 两层解码网络
def Gen(intInput, intOutput, nc):
return torch.nn.Sequential(
torch.nn.Conv2d(in_channels=intInput, out_channels=nc, kernel_size=3, stride=1, padding=1),
torch.nn.BatchNorm2d(nc),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=nc, out_channels=nc, kernel_size=3, stride=1, padding=1),
torch.nn.BatchNorm2d(nc),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=nc, out_channels=intOutput, kernel_size=3, stride=1, padding=1),
torch.nn.Tanh()
)
def Upsample(nc, intOutput):
return torch.nn.Sequential(
torch.nn.ConvTranspose2d(in_channels = nc, out_channels=intOutput, kernel_size = 3, stride = 2, padding = 1, output_padding = 1),
torch.nn.BatchNorm2d(intOutput),
torch.nn.ReLU(inplace=False)
)
# 转置卷积
self.moduleConv = Basic(1024, 512)
self.moduleUpsample4 = Upsample(512, 256)
self.moduleDeconv3 = Basic(512, 256)
self.moduleUpsample3 = Upsample(256, 128)
self.moduleDeconv2 = Basic(256, 128)
self.moduleUpsample2 = Upsample(128, 64)
self.moduleDeconv1 = Gen(128,n_channel,64)
def forward(self, x, skip1, skip2, skip3): # 这里是为了U-net网络的全连接
tensorConv = self.moduleConv(x)
tensorUpsample4 = self.moduleUpsample4(tensorConv)
cat4 = torch.cat((skip3, tensorUpsample4), dim = 1)
tensorDeconv3 = self.moduleDeconv3(cat4)
tensorUpsample3 = self.moduleUpsample3(tensorDeconv3)
cat3 = torch.cat((skip2, tensorUpsample3), dim = 1)
tensorDeconv2 = self.moduleDeconv2(cat3)
tensorUpsample2 = self.moduleUpsample2(tensorDeconv2)
cat2 = torch.cat((skip1, tensorUpsample2), dim = 1)
output = self.moduleDeconv1(cat2)
return output
# pytorch内部集成了 forward方法 在实例类对象时 会先去找__call__方法 pytorch默认实例化网络时 调用__call__会去找forward方法 进而forward是个实例化工具
class convAE(torch.nn.Module):
def __init__(self, n_channel =3, t_length = 5, feature_dim = 512):
super(convAE, self).__init__()
self.encoder = Encoder(t_length, n_channel)
self.decoder = Decoder(t_length, n_channel)
def forward(self, x, keys,train=True):
fea, skip1, skip2, skip3 = self.encoder(x)
output = self.decoder(fea, skip1, skip2, skip3) # 组成的特征的样子
return output