使用旋转矩阵和常规 imagenet 标准差计算灰度 imagenet 像素值的标准差

Calculate standard deviation for grayscale imagenet pixel values with rotation matrix and regular imagenet standard deviation

我想训练一些模型来处理灰度图像,例如对显微镜应用很有用 (Source)。因此,我想在 graysale imagenet 上训练我的模型,使用 pytorch 灰度转换 (torchvision.transforms.Grayscale),将 RGB imagenet 转换为灰度 imagenet。 pytorch内部将颜色space从RGB旋转为YPbPr如下:

Y'为灰度通道,变换后Pb和Pr可以忽略不计。其实pytorch甚至只计算

grayscale = (0.2989 * r + 0.587 * g + 0.114 * b)

要对图像数据进行归一化,我需要知道 grayscale-imagenet 的平均像素值,以及标准差。是否可以计算这些?

我使用

成功计算了平均像素强度
meanGrayscale = 0.2989 * r.mean() + 0.587 * g.mean() + 0.114 * b.mean()

转换图像然后计算灰度均值与首先计算 RGB 均值然后将其转换为灰度均值得到相同的结果。

但是,我现在对计算方差或标准差一窍不通。有人对这个话题有任何想法,或者知道一些好的文献吗?这可能吗?

我找到了一篇刊物“Jianxin Gong - Clarifying the Standard Deviational Ellipse”......他在二维空间中做了这件事(据我所知)。我只是不知道如何在 3D 中做到这一点。

好吧,我没能按计划计算标准差,但使用下面的代码做到了。灰度imagenet的train数据集均值和标准差为(四舍五入):

平均值:0.44531356896770125

标准偏差:0.2692461874154524

import multiprocessing
import os

def calcSTD(d):
    meanValue = 0.44531356896770125
    squaredError = 0
    numberOfPixels = 0
    for f in os.listdir("/home/imagenet/ILSVRC/Data/CLS-LOC/train/"+str(d)+"/"): 
        if f.endswith(".JPEG"):
            
            image = imread("/home/imagenet/ILSVRC/Data/CLS-LOC/train/"+str(d)+"/"+str(f))
                
            ###Transform to gray if not already gray anyways  
            if  np.array(image).ndim == 3:
                matrix = np.array(image)
                blue = matrix[:,:,0]/255
                green = matrix[:,:,1]/255
                red = matrix[:,:,2]/255
                gray = (0.2989 * red + 0.587 * green + 0.114 * blue)
            else:
                gray = np.array(image)/255
            ###----------------------------------------------------       
                    
            for line in gray:
                for pixel in line:
                    squaredError += (pixel-meanValue)**2
                    numberOfPixels += 1
    
    return (squaredError, numberOfPixels)

a_pool = multiprocessing.Pool()
folders = []
[folders.append(f.name) for f in os.scandir("/home/imagenet/ILSVRC/Data/CLS-LOC/train") if f.is_dir()]
resultStD = a_pool.map(calcSTD, folders)

StD = (sum([intensity[0] for intensity in resultStD])/sum([pixels[1] for pixels in resultStD]))**0.5
print(StD)

在此过程中出现了如下错误:

/opt/conda/lib/python3.7/site-packages/PIL/TiffImagePlugin.py:771: UserWarning: Possibly corrupt EXIF data. Expecting to read 8 bytes but only got 4. Skipping tag 41486 "Possibly corrupt EXIF data. "

跳过了 2019 版 ImageNet 的相应图像。