美文网首页简友广场散文
python随机游走生成器和批量加载数据迭代器

python随机游走生成器和批量加载数据迭代器

作者: Cache_wood | 来源:发表于2021-12-03 20:08 被阅读0次
在这里插入图片描述

@[toc]

随机游走生成器

每一个walk是一个单独的随机游走生成器,用zip函数将多个生成器结合起来就变成了n维的随机游走生成器。迭代返回N个随机游走生成的变量。

import numpy as np
import matplotlib.pyplot as plt

def random_walk(mu,x,sigma,N):
    i = 0
    while i<N:
        yield x   #暂停并保存当前所有的运行信息,返回 yield值,并在下一次执行next()方法时从当前位置继续运行
        w = np.random.normal(0,sigma,1) #构建随机游走随机量
        #print(x,end = ' ')
        x = round(mu*x + w[0],3)  #下一次游走的起始点
        i+=1

N = 10
walk1 = list(random_walk(0.5,0,1,N))
walk2 = list(random_walk(0.3,1,2,N))
walk3 = list(random_walk(0.7,3,0.5,N))

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(walk1,walk2)
plt.savefig(f'random_walk_{N}.png')
plt.show()

for i in zip(walk1,walk2,walk3):  #zip函数构建多维随机游走
    print(i)
(0, 1, 3)
(1.598, 0.586, 2.172) 
(1.633, 1.426, 1.268) 
(2.043, 0.002, 1.091) 
(1.845, -0.729, 0.757)
(2.12, 0.264, 1.162)  
(-0.712, -3.57, 0.608)
(1.002, 1.507, 0.301) 
(0.193, 2.165, -0.032)
(2.611, 0.637, 0.031) 

批量加载数据迭代器

Dataset实现图片路径读取方法和单张图片ndarray形式的转化方法。__iter__属性主要是为了生成迭代器,__next__属性依次返回处理后的图片。

memory_profile模块的装饰器来查看分批批量加载处理后的图片的内存占用情况。

import numpy as np
from PIL import Image
from pathlib import Path
from PIL import ImageFile
from memory_profiler import profile
ImageFile.LOAD_TRUNCATED_IMAGES = True

class Dataset:
    def __init__(self,path,start=0,step=1,max=100000):
        self.path = path
        self._a = start
        self._step = step
        self._max = max

    def load_images(self):  #用于加载图片路径
        img_formats = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.dng']
        p = Path(self.path)
        images = p.rglob('*.*')
        images=[x for x in images if str(x)[-4:] in img_formats]
        self.lis = list(images)

    def transform(self,image): #用于将一张图片转化为ndarray形式
        img = Image.open(image)
        img = np.array(img)
        self.img = img
        return img
    
    def __iter__(self): #返回一个特殊的迭代器对象
        #return iter(self.img)
        return self

    def __next__(self):  #返回下一个元素并通过StopIteration异常标识迭代的完成
        if self._a <= self._max:
            x = Dataset.transform(self,self.lis[self._a])
            self._a += self._step
            return x
        else:
            raise StopIteration('大于max:{}'.format(self._max))

@profile
def main():
    path = 'E:/originalPics/'
    d = Dataset(path)
    d.load_images()

    for img in d:
        print(img)
    #for i in range(1000):
        #print(next(d))
main()
Line #    Mem usage    Increment  Occurences   Line Contents
============================================================
    37     51.9 MiB     51.9 MiB           1   @profile
    38                                         def main():
    39     51.9 MiB      0.0 MiB           1       path = 'E:/originalPics/'
    40     51.9 MiB      0.0 MiB           1       d = Dataset(path)
    41     80.4 MiB     28.6 MiB           1       d.load_images()
    42     81.8 MiB     -2.5 MiB          11       for i in range(10):
    43     82.6 MiB      3.0 MiB          10           d.transform(d.lis[i])
    44     81.8 MiB     -8.3 MiB          10           myiter = iter(d)
    45     81.8 MiB     -2.0 MiB          10           print(next(myiter))
在这里插入图片描述

上图是用批量加载方法来实现对图片的处理,可以发现对于CPU和内存的占用相对较高但是还可以接受。

相关文章

网友评论

    本文标题:python随机游走生成器和批量加载数据迭代器

    本文链接:https://www.haomeiwen.com/subject/eildxrtx.html