Cython: view_as_windows与手动算法在Python中的性能



我的环境是OS: Ubuntu,语言:Python + Cython。

我有点左右为难,不知该走哪条路。我使用view_as_windows对图像进行切片,并返回一个由切片创建的所有补丁的数组。我还创建了一个算法,它做了几乎相同的事情,以更好地控制切片。我已经测试了这两种算法,他们创造了我想要的结果,我现在的问题是我需要更快的性能,所以我正在尝试cythonize的东西。我对Cython非常陌生,所以我还没有做任何更改。

view_as_windows time per image: 0.0033s

每个图像的patches_by_col时间:0.057s


问题:

给定这些运行时,我将获得更好的性能从cythonization手动算法或只是继续使用view_as_windows?我问是因为我认为我不能cythonize view_as_windows,因为它是从numpy调用的。我正在测试变量步长禁用(strideDivisor == 0和imgRegion == 0)。图像大小为1200 * 800。

GetPatchesAndCoordByRow(手动代码)

参数:

#Patch Image Settings: Should be 3x2 ratio for width to height
WIDTH = 60
HEIGHT = 40
CHANNELS = 1
ITERATIONS = 7
MULTIPLIER = 1.31
#Stride will be how big of a step each crop takes.
#If you dont want to crops to overlap, do same stride as width of image.
STRIDE = 6
# STRIDE_IMREG_DIV decreases normal stride inside an image region
    #Set amount by which to divide stride.
        #Ex: 2 would reduce stride by 50%, and generate 200% data
        #Ex contd: So it would output 40K patches instead of 20K
    #strideDivisor = 1.5
# IMG_REGION determines what % of image region will produce additional patches
    #Region of image to focus by decreasing stride. Ex: 0.5 would increase patches in inner 50% of image
    #imgRegion = 0.5
# Set STRIDE_IMREG_DIV and IMG_REGION = 0 to disable functionality.
STRIDE_IMREG_DIV = 0
IMG_REGION = 0
源代码:

def setVarStride(x2, y2, maxX, maxY, stride, div, imgReg, var):
    imgFocReg1 = imgReg/2
    imgFocReg2 = 1 - imgFocReg1
    if (var == 'x'):
        if ((x2 >= maxX*imgFocReg1) and (x2 <= maxX*imgFocReg2) and (y2 >= maxY*imgFocReg1) and (y2 <= maxY*imgFocReg2)):
            vStride = stride/div
        else:
            vStride = stride
    elif (var == 'y'):
        if ((y2 >= maxY*imgFocReg1) and (y2 <= maxY*imgFocReg2)):
            vStride = stride/div
        else:
            vStride = stride
    return vStride
def GetPatchesAndCoordByRow(image, patchHeight, patchWidth, stride, strideDivisor, imgRegion):
    x1 = 0
    y1 = 0
    x2 = patchWidth
    y2 = patchHeight
    croppedImageList = []
    maxX, maxY = image.size
    #Set variable stride to collect more data in a region of the image
    varStride = stride
    useVaraibleStride = True
    if (strideDivisor == 0 and imgRegion == 0):
        useVaraibleStride = False
    else:
        imgConcentration = (1 - imgRegion)*100
        print("Variable Stride ENABLED: Create more patches inside {0}% of the image.".format(imgConcentration))
    while y2 <= (maxY):
        while x2 <= (maxX):
            croppedImage = image.crop((x1,y1,x2,y2))
            croppedImageList.append((croppedImage,(x1, y1, x2, y2)))
            #Get 2x more patches in the center of the image
            if (useVaraibleStride):
                varStride = setVarStride(x2, y2, maxX, maxY, stride, strideDivisor, imgRegion, 'x')
            #Rows
            x1 += varStride
            x2 += varStride
            #--DEBUG
            #iX += 1
            #print("Row_{4} -> x1: {0}, y1: {1}, x2: {2}, y2: {3}".format(x1, y1, x2, y2,iX))
        #Get 2x more patches in the center of the image
        if (useVaraibleStride):
            varStride = setVarStride(x2, y2, maxX, maxY, stride, strideDivisor, imgRegion, 'y')
        #Columns
        x1  = 0
        x2  = patchWidth
        y1 += varStride
        y2 += varStride
        #--DEBUG
        #iY += 1
        #print("    Column_{4} -> x1: {0}, y1: {1}, x2: {2}, y2: {3}".format(x1, y1, x2, y2, iY))
    #Get patches at edge of image
    x1 = 0
    x2 = patchWidth
    y1 = maxY - patchHeight
    y2 = maxY
    #Bottom edge patches
    while x2 <= (maxX):
        #--DEBUG
        #iX += 1
        #print("Row_{4} -> x1: {0}, y1: {1}, x2: {2}, y2: {3}".format(x1, y1, x2, y2,iX))
        #--DEBUG
        croppedImage = image.crop((x1,y1,x2,y2))
        croppedImageList.append((croppedImage,(x1, y1, x2, y2)))
        #Rows
        x1 += stride
        x2 += stride
    #Right edge patches
    x1 = maxX - patchWidth
    x2 = maxX
    y1 = 0
    y2 = patchHeight
    while y2 <= (maxY):
        #--DEBUG
        #iY += 1
        #print("    Column_{4} -> x1: {0}, y1: {1}, x2: {2}, y2: {3}".format(x1, y1, x2, y2, iY))
        #--DEBUG
        croppedImage = image.crop((x1,y1,x2,y2))
        croppedImageList.append((croppedImage,(x1, y1, x2, y2)))
        #Columns
        y1 += stride
        y2 += stride
    #--DEBUG
    print("GetPatchesAndCoordByRow (Count={0}, W={1}, H={2}, Stride={3})".format(len(croppedImageList), int(patchWidth), int(patchHeight), int(stride)))
    return croppedImageList

view_as_windows代码

def CreatePatches(image, patchHeight, patchWidth, stride = 1):
    imageArray = numpy.asarray(image)
    patches = view_as_windows(imageArray, (patchHeight, patchWidth), stride)
    print("Raw Patches initial shape: {0}".format(patches.shape))
    return patches

我不认为你可以做得比view_as_windows更好,因为只要输入数组是连续的,它已经非常有效了。我怀疑即使把它细胞化也不会有什么不同。我研究了它的实现,实际上有点印象深刻:

numpy数组由一个底层数据数组(例如char *)和一个"strides"数组组成,"strides"数组代表每个维度,表示沿着该维度的每一步要沿着底层数组移动多远。view_as_windows的实现利用了这一点,通过创建一个新数组,共享相同的数据数组作为其输入,并简单地插入新的"strides"来添加可用于选择补丁的尺寸。这意味着它不是像你说的那样返回"一个包含所有补丁的数组",而是只返回一个数组,它的第一个维度就像一个补丁数组的索引。

因此,view_as_windows不需要复制映像中的任何数据来创建补丁,也不需要为每个补丁创建额外的narray对象。它唯一需要复制数据的时候是当它的输入数组不是连续的(例如,它是一个大数组的切片)。即使使用Cython,我也看不出你能做得比这更好。

在您的实现中,即使假设image.crop能够共享图像中的数据,您仍然要创建一个看起来像1199x799个不同image对象的数组。

你确认view_as_windows是你的算法花费大部分时间的地方吗?

相关内容

  • 没有找到相关文章

最新更新