有没有人对如何计算一组图像(文件夹中的.jpg文件)的图像均值以及为ImageDeserializer生成正确格式的XML文件有高级解释?
我已经看到了CNTK_201A_CIFAR-10_DataLoader中的代码,它执行此操作。但是,输入的格式不同,所以目前还不清楚如何处理图像文件作为输入。
答案 0 :(得分:2)
您只需按照computes the mean input for CIFAR的代码示例操作即可。基本上它会遍历所有训练图像并计算平均值。然后它以OpenCV将理解的格式将其写入文件中。第一部分很简单,因为numpy和PIL,第二部分很容易因为minidom。
答案 1 :(得分:0)
以下是基于Nikos提供的链接的完整解决方案,用于计算存储在ZIP文件中的所有图像(如果您使用的是ZIP图像阅读器)或图像文件列表中的平均值。
关于您的问题:在DataLoader教程中,填充仅影响用于将图像保存到文件的代码路径,而不是用于计算均值。
import zipfile
import io
import numpy as np
from PIL import Image
from scipy.misc import imread, imresize, fromimage
import xml.etree.cElementTree as et
import xml.dom.minidom
# saveMean function taken from
# https://github.com/Microsoft/CNTK/blob/v2.0.beta7.0/Examples/Image/DataSets/CIFAR-10/cifar_utils.py#L84
def saveMean(fname, data, imgSize):
root = et.Element('opencv_storage')
et.SubElement(root, 'Channel').text = '3'
et.SubElement(root, 'Row').text = str(imgSize)
et.SubElement(root, 'Col').text = str(imgSize)
meanImg = et.SubElement(root, 'MeanImg', type_id='opencv-matrix')
et.SubElement(meanImg, 'rows').text = '1'
et.SubElement(meanImg, 'cols').text = str(imgSize * imgSize * 3)
et.SubElement(meanImg, 'dt').text = 'f'
et.SubElement(meanImg, 'data').text = ' '.join(['%e' % n for n in np.reshape(data, (imgSize * imgSize * 3))])
tree = et.ElementTree(root)
tree.write(fname)
x = xml.dom.minidom.parse(fname)
with open(fname, 'w') as f:
f.write(x.toprettyxml(indent = ' '))
def loadAndResize(f, networkSize):
im = Image.open(io.BytesIO(f))
n = fromimage(im).astype(np.float)
return imresize(n, (networkSize, networkSize))
然后在函数中使用这些构建块,这些函数可以从ZIP文件或原始文件中读取图像作为字节:
def meanFromZip(zipFile, networkSize):
imgSum = np.zeros((networkSize, networkSize, 3), np.float)
with zipfile.ZipFile(zipFile) as z:
allFiles = z.namelist()
for f in allFiles:
imgSum = imgSum + loadAndResize(z.read(f), networkSize)
return imgSum / len(allFiles)
def meanFromFiles(files, networkSize):
imgSum = np.zeros((networkSize, networkSize, 3), np.float)
for f in files:
with open(f, 'rb') as b:
imgSum = imgSum + loadAndResize(b.read(), networkSize)
return imgSum / len(files)
调用如下:
zipFile = "myImages.zip"
networkSize = 224
mean = meanFromZip(zipFile, networkSize)
saveMean("mean.xml", mean, networkSize)
files = ["c:/temp/Column0_Line16.jpg", "C:/temp/Column0_Line47.jpg"]
mean2 = meanFromFiles(files, networkSize)