[英]Loading grayscale pngs with image_dataset_from_directory returns a 3-channel tensor
我有一組分成 2 個目錄的灰度 png 圖像。 根據文檔,我使用 image_dataset_from_directory 將它們加載為 Dataset 對象。 當我使用 element_spec 檢查已加載的內容時,它說圖像有 3 個通道:
from tensorflow.keras.preprocessing import image_dataset_from_directory
Dataset = image_dataset_from_directory('path/to/files')
Dataset.element_spec
返回:
找到屬於 2 個類的 14000 個文件。
(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 位灰度,非隔行
編輯:
使用識別(來自 ImageMagick)有關磁盤上文件的更多信息:
$ identify -verbose path/to/files/class_1/file_1.png
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
默認情況下, image_dataset_from_directory
轉換為 3 個通道。 查看文檔:
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
)
顏色模式: “灰度”、“rgb”、“rgba”之一。 默認值:“RGB”。 圖像是否將轉換為具有 1、3 或 4 個通道。
所以只需使用這一行:
Dataset = image_dataset_from_directory('path/to/files', color_mode='grayscale')
現在您的圖像將被轉換為(None, 256, 256, 1)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.