【问题标题】:Problems with keras preprocessingkeras 预处理的问题
【发布时间】:2021-02-18 19:00:52
【问题描述】:

您好,我正在预处理一些图像数据以在简单的 FF 网络中运行:

我有两个选项在我看来是相同的,但一个比另一个执行得好很多:

选项 1

我将图像保存在具有相应子目录的目录中并运行:

xy_training = tf.keras.preprocessing.image_dataset_from_directory("/content/data/train", image_size=(48,48), color_mode='grayscale',label_mode="int")
xy_validation = tf.keras.preprocessing.image_dataset_from_directory("/content/data/valid", image_size=(48,48), color_mode='grayscale',label_mode="int")
xy_testing = tf.keras.preprocessing.image_dataset_from_directory("/content/data/test", image_size=(48,48), color_mode='grayscale',label_mode="int")

选项 2 我有灰度图像的原始数组并执行此操作

def preprocess(data):
    X = []
    pixels_list = data["pixels"].values
    
    for pixels in pixels_list:
        single_image = np.reshape(pixels.split(" "), (WIDTH,HEIGHT)).astype("float")
        X.append(single_image)
        
    # Convert list to 4D array:
    X = np.expand_dims(np.array(X), -1)
    
    # Normalize pixel values to be between 0 and 1
    X = X / 255.0
    return X

train_images= preprocess(train_data)
valid_images= preprocess(valid_data)
test_images= preprocess(test_data)

选项 2 的效果比 选项 1 好得多。 tf.keras.preprocessing.image_dataset_from_directory(里面有没有参数我没设置?

谢谢!

【问题讨论】:

    标签: python image tensorflow keras image-preprocessing


    【解决方案1】:

    这很可能是由于

    tf.keras.preprocessing.image_dataset_from_directory 
    

    没有内置的标准化功能。您拥有的另一个自定义函数是应用规范化,因此不是苹果对苹果的比较。

    在使用 image_dataset_from_directory 加载数据集后,您必须在稍后的步骤中进行标准化。 这是加载批处理数据集后进行规范化的示例代码:

    def normalize(image,label):
        image = tf.cast(image/255. ,tf.float32)
        label = tf.cast(label ,tf.float32)
    
        return image,label
    
    xy_training = xy_training.map(normalize)
    xy_validation = xy_validation.map(normalize)
    xy_testing = xy_testing.map(normalize)
    

    【讨论】:

      猜你喜欢
      • 2018-08-07
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      相关资源
      最近更新 更多