Python 从目录加载带有图像数据集的灰度PNG将返回一个3通道张量
我有一组灰度png图像,分为两个目录。根据文档,我使用了来自目录的image\u dataset\u将它们作为数据集对象加载。当我使用element_spec检查加载的内容时,它显示图像有3个通道:Python 从目录加载带有图像数据集的灰度PNG将返回一个3通道张量,python,tensorflow,keras,png,tensorflow-datasets,Python,Tensorflow,Keras,Png,Tensorflow Datasets,我有一组灰度png图像,分为两个目录。根据文档,我使用了来自目录的image\u dataset\u将它们作为数据集对象加载。当我使用element_spec检查加载的内容时,它显示图像有3个通道: 来自tensorflow.keras.preprocessing从目录导入图像\u数据集\u 数据集=图像\u数据集\u来自\u目录('path/to/files')) Dataset.element_规范 返回: 找到了14000个属于2个类的文件 (TensorSpec(shape=(None
来自tensorflow.keras.preprocessing从目录导入图像\u数据集\u
数据集=图像\u数据集\u来自\u目录('path/to/files'))
Dataset.element_规范
返回:
找到了14000个属于2个类的文件
(TensorSpec(shape=(None,256,256,3),dtype=tf.float32,name=None),TensorSpec(shape=(None,),dtype=tf.int32,name=None))
使用MATLAB将图像保存为灰度PNG,我已使用Linux命令文件确认它们为灰度:
$ file path/to/files/class_1/file_1.png
path/to/files/class_1/file_1.png:png图像数据,256 x 256,8位灰度,非隔行扫描
现在我需要告诉image\u dataset\u from\u directory以灰度形式加载这些文件,或者我需要将3通道张量数据集对象转换为1通道张量。我该怎么做呢?
编辑:
有关使用identify(来自ImageMagick)在磁盘上创建文件的更多信息:
默认情况下,目录中的图像数据集将转换为3个通道。看看下面的例子: 颜色模式:“灰度”、“rgb”、“rgba”之一。默认值:“rgb”。图像是否将转换为具有1、3或4个通道 所以只要用这句话:
Dataset = image_dataset_from_directory('path/to/files', color_mode='grayscale')
现在,您的图像将转换为(无、256、256、1)
Image: AI_Optrap/Samples/Set4/relaxed/HL60_normoxia_1_1.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: PseudoClass
Geometry: 256x256+0+0
Units: Undefined
Type: Grayscale
Base type: Grayscale
Endianess: Undefined
Colorspace: Gray
Depth: 8-bit
Channel depth:
gray: 8-bit
Channel statistics:
Pixels: 65536
Gray:
min: 0 (0)
max: 255 (1)
mean: 135.92 (0.533021)
standard deviation: 36.3709 (0.142631)
kurtosis: 1.51412
skewness: 0.035325
entropy: 0.87207
Colors: 256
tf.keras.preprocessing.image_dataset_from_directory(
directory, labels='inferred', label_mode='int', class_names=None,
color_mode='rgb', batch_size=32, image_size=(256, 256), shuffle=True, seed=None,
validation_split=None, subset=None, interpolation='bilinear', follow_links=False
)
Dataset = image_dataset_from_directory('path/to/files', color_mode='grayscale')