为什么我的灰度图像无法正常显示?

问题描述 投票:0回答:1

我试图从DDSM数据库中加载灰度图像,并使用这些图像训练我的Tensorflow模型。但当我尝试使用 flow_from_directory 的函数,并将该数据传递给一个显示批次的函数,它没有得到正确的显示。

全局变量

#variables  
BATCH_SIZE = 25
IMG_HEIGHT = 350
IMG_WIDTH = 350
trainDir = "./data/pictures/"

显示批次代码

def show_batch(image_batch, label_batch):
    plt.figure(figsize=(10,10))

    # Show every picture in batch
    for n in range(BATCH_SIZE):
        ax = plt.subplot(5,5, n+1) 
        #Squeeze from [350, 350, 1] to [350, 350]
        arr = nmp.asarray(nmp.squeeze(image_batch[n]))

        plt.imshow(arr, cmap='gray', vmin=0, vmax=1)
        # Find the correct class
        plt.title(CLASS_NAMES[label_batch[n] == 1][0].title())
        plt.axis('off')
    plt.show()

编码

# my image data generator and normalize (0 to 1)
train_generator = 
tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)

# Definin the path to my training data (grayscale images)
traindir = pathlib.Path(trainDir)

# Here I am counting the amount of training images (around 500)
train_count = len(list(traindir.glob('*/*.png')))

# getting all the class names (cancer, benign, normal) from the directory
CLASS_NAMES = nmp.array([item.name for item in traindir.glob('*')])

#The flow_from_directory function
train_data_gen = train_generator.flow_from_directory(
    directory = str(traindir),
    batch_size= BATCH_SIZE,
    shuffle=True,
    color_mode='grayscale', # Grayscale mode
    target_size=(IMG_HEIGHT, IMG_WIDTH),
    classes=list(CLASS_NAMES)
) 

sample_train_images, lbl = next(train_data_gen)
# Here I call the function to show the images.
show_batch(sample_train_images, lbl)

所以当我运行上面的代码时,我确实得到了一个显示,但灰度图像却不能正常显示。我在网上找了很多,也试了很多东西,但我无法解决这个问题。

我目前错误的输出

The output from show_batch.

图片应该是这样的Grayscale image that I need to see (in a batch)

我担心由于,我看不清楚灰度图像的原因,导致模型不能很好的训练。

编辑1我试着将我的数组乘以255,这并没有改变输出。我可能是认为flow_from_directory没有工作好。

python tensorflow matplotlib tensorflow2.0 grayscale
1个回答
0
投票

你已经在 tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)在你的 show_batch 函数将您的图像乘以255。

© www.soinside.com 2019 - 2024. All rights reserved.