我正在研究 X 射线图像分类,我的数据存储在 1 个目录中,我需要将其分为训练集、验证集和测试集。我确实设法使用 ImagedDataGenerator 分离了训练集和验证集,但在分离测试集时遇到了麻烦。这是我的代码。
import split
# Path
Images = 'data_processed_cropped_32'
data_set = os.path.join(r'C:\Users\320067835\Desktop\Thesis\Data\png', Images)
#split.ratio('data_processed_cropped_32', output="output", seed=1337, ratio=(0.8, 0.1,0.1))
# Image size
img_width = 32
img_height = 32
# Data augmentation
data_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale = 1/255, horizontal_flip = True,
rotation_range = 0,validation_split=0.2)
train_set = data_gen.flow_from_directory(data_set, target_size = (img_width, img_height), color_mode = 'grayscale',
class_mode = 'categorical', batch_size = 32, interpolation = 'nearest',
subset ='training')
validation_set = data_gen.flow_from_directory(data_set, target_size= (img_width,img_height), color_mode='grayscale',
batch_size=32, class_mode='categorical', interpolation= 'nearest',
subset='validation')
# Build a model
cnn = Sequential()
cnn.add(keras.Input(shape = (32,32,1)))
cnn.add(Conv2D(16,(3,3), padding = 'same', activation = 'relu', input_shape= (img_width,img_height,1)))
cnn.add(MaxPooling2D(2,2))
cnn.add(Conv2D(32,(3,3), padding = 'same',activation = 'relu', input_shape= (img_width, img_height,1)))
cnn.add(MaxPooling2D(2,2))
cnn.add(Flatten())
cnn.add(Dense(units = 100, activation = 'relu'))
cnn.add(Dense(units = 50, activation = 'relu'))
cnn.add(Dense(units=23, activation = 'softmax'))
cnn.summary()
cnn.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
cnn.fit(train_set,validation_data = validation_set,epochs = 20)
我厌倦了使用拆分文件夹,但它不起作用。我认为大多数情况下都没有正确使用它,因为我不知道在拆分数据后如何访问 3 个文件夹。或者有没有其他方法可以拆分我的测试集?