我使用ImageDataGenerator()
并传递tf.keras.applications.preprocessing.resnet50
作为ImageDataGenerator()
的preprocessing
参数。
原始图像值在0-255之间。但是经过预处理,我注意到图像值不在0-1之间,我得到的是val_loss: nan
。我的激活函数是selu
和softmax.
使用tf.keras.applications.preprocessing.resnet50预处理后的图像数据值
我的问题是tf.keras.applications.preprocessing.resnet50
假设规范化0-1之间的数据?
我是否有可能得到val_loss: nan
,因为我的数据没有在0-1之间缩放?
import matplotlib.pyplot as plt
import os
import pandas as pd
import numpy as np
import PIL
import pathlib
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers import Dense, Flatten, BatchNormalization, Dropout
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import EarlyStopping
def ds_generator(h, w, bs, preprocessing):
train_folder = '/home/ec2-user/SageMaker/PCM_Map_Images/train/'
test_folder = '/home/ec2-user/SageMaker/PCM_Map_Images/test/'
dir_train = pathlib.Path(train_folder)
dir_test = pathlib.Path(test_folder)
data_gen = ImageDataGenerator(preprocessing_function=preprocessing, validation_split=0.15)
train_ds = data_gen.flow_from_directory(dir_train, target_size=(h, w), class_mode='sparse',
subset='training', shuffle = False,
seed = 0, batch_size=bs)
val_ds = data_gen.flow_from_directory(dir_train, target_size=(h, w), class_mode='sparse',
subset='validation', shuffle = False,
seed = 0, batch_size=bs)
test_ds = data_gen.flow_from_directory('.', target_size=(h, w), class_mode=None, classes=['test'], shuffle=False,
batch_size=bs)
return train_ds, val_ds, test_ds
resnet50 = tf.keras.applications.resnet50
h, w, c = 32, 32, 3
batch_size = 64
train_ds, val_ds, test_ds = ds_generator(h, w, batch_size, preprocessing=resnet50.preprocess_input)
model = Sequential()
Init = tf.keras.initializers.RandomNormal(seed=0) # seed all that you can
cb = EarlyStopping(monitor='val_loss', restore_best_weights=True, patience=6)
# you execute this line of code if NG made life easier
pre_train_model = tf.keras.applications.ResNet50(include_top=False,
input_shape=(h, w, c),
pooling='max',
weights='resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')
for layer in pre_train_model.layers[:-25]:
if isinstance(layer, layers.BatchNormalization):
layer.trainable = False
else:
layer.trainable = True
model.add(pre_train_model)
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dense(256, activation='selu', kernel_initializer=Init))
model.add(Dropout(.50))
model.add(BatchNormalization())
model.add(Dense(128, activation='selu', kernel_initializer=Init))
model.add(Dropout(.30))
model.add(BatchNormalization())
model.add(Dense(3, activation='softmax', kernel_initializer=Init))
model.summary()
model.compile(optimizer=tf.keras.optimizers.Nadam(learning_rate=0.01, clipnorm=1.0, beta_1=.01), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
hist = model.fit(train_ds, epochs=6, validation_data=val_ds)
NaN不是由缩放引起的。大多数预处理函数在-1和+1之间缩放图像像素。您可以使用下面的代码作为预处理器函数
def preprocess(img)
return img=img/127.5-1
然后在ImageDataGenerator中使用
gen=tf.keras.preprocessing.image.ImageDataGenerator( preprocessing_function=preprocess)