python常用小技巧
文件夹篇
1、遍历文件夹,同时根据编号进行排序
文件格式如下:
import os
from tqdm import tqdm
id_name = "14、VAE_checkpoint_VAE32"
checkpoint_path = "./models/VAE_models/" + id_name
files = os.listdir(checkpoint_path)
files.sort(key=lambda x: int(x[15:-3])) #根据最后的编号将文件夹排序
pbar = tqdm(files)
for f in pbar:
**************
任务代码填充
**************
with open(vae_search_path+"/search.txt", "a+") as f:
crr_time = str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
f.write("crr_time: {}, best score: {}, file_name: {}\n".format(crr_time, score, state_dict_path))
#实时更新滚动条信息
pbar.set_description("best score: {} ".format(score))
实时效果如下:
函数操作
1、利用lambda可以构造函数数组
g=[lambda a:a*2,lambda b:b*3]
print(g[0](5)) #调用
print(g[1](6))
'''
10
18
'''
2、@装饰器简单介绍
即在一个函数中定义一个函数,然后这个里面的函数可以调用外面函数的变量。
class funA():
def __init__(self, level):
self.level = level
def __call__(self, func): #实现了__call__方法的对象,相当于重载了(),可以实现调用功能。这个func就是传入的funB函数
def wrapper(*args, **kwargs):
print("[{0}]: enter {1}()".format(self.level, func.__name__))
return func(*args, **kwargs)
return wrapper # 然后funB的函数就被修改成了这个返回值
@funA(level="TEST")
def funB(a, b, c):
print(a, b, c)
funB("hello,","good","morning")
输出结果:
-----------------------------------------------
[TEST]: enter funB()
hello, good morning
上面的代码与下面的等价:
class funA():
def __init__(self, level):
self.level = level
def __call__(self, func): #实现了__call__方法的对象,相当于重载了(),可以实现调用功能。这个func就是传入的funB函数
def wrapper(*args, **kwargs):
print("[{0}]: enter {1}()".format(self.level, func.__name__))
return func(*args, **kwargs)
return wrapper # 然后funB的函数就被修改成了这个返回值
def funB(a, b, c):
print(a, b, c)
funB = funA("TEST")(funB)
funB("hello,","good","morning")
输出结果:
-----------------------------------------------
[TEST]: enter funB()
hello, good morning
3、两种方法过滤数组,list中的filter和循环过滤
# 等价于 seconde_floor_v = latent_vectors[[i for i in range(latent_vectors.shape[0]) if (i-1) % 10 == 0]]
# filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。
seconde_floor_v = latent_vectors[list(filter(lambda x: (x-1)%10==0, range(latent_vectors.shape[0])))]
print(latent_vectors.shape)
print(seconde_floor_v.shape)
-------------------------------------------------------
torch.Size([80, 512])
torch.Size([8, 512])
系统
1、os.sched_getaffinity(0)
通过os.sched_getaffinity(pid)
获取在指定进程号pid上,在哪些cpu上可以运行:参考链接
其中计算fid的函数在:github
示例代码
# importing os module
import os
# Get the set of CPUs
# on which the calling process
# is eligible to run.
# 0 as pid represents the
# calling process
pid = 0
affinity = os.sched_getaffinity(pid)
# Print the result
print(affinity)
# Change the CPU affinity mask
# of the calling process
# using os.sched_setaffinity() method
# Below CPU affinity mask will
# restrict a process to only
# these 2 CPUs (0, 1) i.e process can
# run on these CPUs only
affinity_mask = {0, 1}
pid = 0
os.sched_setaffinity(0, affinity_mask)
# Now again, Get the set of CPUs
# on which the calling process
# is eligible to run.
pid = 0
affinity = os.sched_getaffinity(pid)
# Print the result
print(affinity)
实际应用场景
path1 = "/home/kuangjielong/my_idea/robustmap/我的开始/statick/data/CIFAR10/50000_Origin_PIC"
path2 = "/home/kuangjielong/my_idea/robustmap/我的开始/statick/data/CIFAR10/10000_Random_PIC"
dims = 2048
batch_size = 50
num_avail_cpus = len(os.sched_getaffinity(0))
num_workers = min(num_avail_cpus, 8)
m1, s1 = official_fid.compute_statistics_of_path(path1, model, batch_size,
dims, device, num_workers)
m2, s2 = official_fid.compute_statistics_of_path(path2, model, batch_size,
dims, device, num_workers)
fid_value = official_fid.calculate_frechet_distance(m1, s1, m2, s2)