TypeError: append() 没有关键字参数 - 用于多处理的 Python 代码以获取图像的分形维度

Posted

技术标签:

【中文标题】TypeError: append() 没有关键字参数 - 用于多处理的 Python 代码以获取图像的分形维度【英文标题】:TypeError: append() takes no keyword arguments - Python code for multiprocessing to get fractal dimentions of a image 【发布时间】:2021-11-29 15:06:55 【问题描述】:

我目前正在做一个多处理分形维数计算程序的项目。

我设计了代码,它首先将大图像分成 9 块,然后计算每个部分的分形尺寸。

from multiprocessing import process
import numpy as np
import os
import math as mt   
import cv2
import imageio
import warnings
from PIL import Image
warnings.simplefilter('ignore', Image.DecompressionBombWarning)
Image.MAX_IMAGE_PIXELS = None
def fractal_dimension(Z, threshold=0.8):
"""Returns box-counting dimension of a 2D array.
    Args:
        Z: 2D array to be analysed.
        threshold: Cutoff for converting values in Z to 1 and 0.
    Returns:
        The estimated box counting dimension.
"""

    # Only for 2d image
    assert(len(Z.shape) == 2)
    # From https://github.com/rougier/numpy-100 (#87)
    def boxcount(Z, k):
        S = np.add.reduceat(
            np.add.reduceat(Z, np.arange(0, Z.shape[0], k), axis=0),
                               np.arange(0, Z.shape[1], k), axis=1)
        # We count non-empty (0) and non-full boxes (k*k)
        return len(np.where((S > 0) & (S < k*k))[0])
    # Transform Z into a binary array
    Z = (Z < threshold)
    # Minimal dimension of image
    p = min(Z.shape)
    # Greatest power of 2 less than or equal to p
    n = 2**np.floor(np.log(p)/np.log(2))
    # Extract the exponent
    n = int(np.log(n)/np.log(2))
    # Build successive box sizes (from 2**n down to 2**1)
    sizes = 2**np.arange(n, 1, -1)
    # Actual box counting with decreasing size
    counts = []
    for size in sizes:
        counts.append(boxcount(Z, size))
    # Fit the successive log(sizes) with log (counts)
    coeffs = np.polyfit(np.log(sizes), np.log(counts), 1)
    #fracd.append(coeffs)
    return -coeffs[0]

I = imageio.imread(r"C:\Users\qkrgn\PycharmProjects\pythonProject\Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg", as_gray="True")/255.0    # Import the image in greyscale
print("Minkowski–Bouligand dimension (computed): ", fractal_dimension(I))

subset_9t1 = I[::3, ::3]
subset_9t2 = I[1::3, ::3]
subset_9t3 = I[2::3, ::3]
subset_9t4 = I[::3, 1::3]
subset_9t5 = I[1::3, 1::3]
subset_9t6 = I[2::3, 1::3]
subset_9t7 = I[::3, 2::3]
subset_9t8 = I[1::3, 2::3]
subset_9t9 = I[2::3, 2::3]
if __name__ =='__main__':

    processes:list = []

    processes.append(target=fractal_dimension, args=subset_9t1)
    processes.append(target=fractal_dimension, args=subset_9t2)
    processes.append(target=fractal_dimension, args=subset_9t3)
    processes.append(target=fractal_dimension, args=subset_9t4)
    processes.append(target=fractal_dimension, args=subset_9t5)
    processes.append(target=fractal_dimension, args=subset_9t6)
    processes.append(target=fractal_dimension, args=subset_9t7)
    processes.append(target=fractal_dimension, args=subset_9t8)
    processes.append(target=fractal_dimension, args=subset_9t9)
    
for process in processes:
    process.start()

print(sum(fracd, 0.000)/len(fracd))

错误代码如下:

traceback (most recent call last):
  File "C:/Users/qkrgn/PycharmProjects/pythonProject/main.py", line 141, in <module>
    processes.append(target=fractal_dimension, args=subset_9t1)
TypeError: append() takes no keyword arguments

我想知道为什么会出现这个错误以及如何解决它。分割图像计算分形维数并将它们组合回来得到分形维数是否会有任何问题。

【问题讨论】:

向我们展示此方法的文档。我不认识它。 list.append 方法没有记录这些参数。 【参考方案1】:

你应该这样做,而不是你实际在做的事情。

from multiprocessing import Process

# make this change for all the times you tried to append it while using the earlier way.
processes.append(Process(target=fractal_dimension, args=[subset_9t1]))

您传递的关键字参数对于 append 函数不存在,这是您不想做的。

除了手动输入这些附加内容,您还可以使用 for 循环


if __name__ == '__main__':
    VARIABLE_NAME = "subset_9t"
    
    for i in range(1, 10):
        p = Process(target=fractal_dimension, args=[eval(VARIABLE_NAME + str(i))])
        processes.append(p)

【讨论】:

以上是关于TypeError: append() 没有关键字参数 - 用于多处理的 Python 代码以获取图像的分形维度的主要内容,如果未能解决你的问题,请参考以下文章

python Python:ClassName(list),isinstance(),TypeError,ValueError,.append()

TypeError: to_append 应该是 Series 或 Series 的列表/元组,得到 DataFrame

TypeError: 'append' called on an object that does not implement interface FormData 解决方法(示例代码

TypeError:只有一个元素的整数数组可以转换为索引 3

获取“first_name”的Django是此函数的无效关键字参数“创建模型类实例时的TypeError”

TypeError:AutoProxy 对象不可迭代 - 多处理