如何将图像输入CNN进行二值分类



我正在尝试创建一个卷积神经网络,可以根据一个人的面部照片来检测他们是否患有中风。我的数据集的图像包含在名为CNNImages的目录中,该目录包含两个子目录:StrokesRegularFaces。每个子目录包含jpg图像,我正试图将其输入我的神经网络。

按照本教程中使用的方法,我创建了CNN,它在使用MNIST数据集时可以工作。然而,我在将自己的图像输入神经网络时遇到了麻烦。我一直在使用Keras教程中显示的代码进行图像数据预处理,但它不起作用。

import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
import numpy as np

dataset = tf.keras.preprocessing.image_dataset_from_directory(
'C:\Users\Colin\CNNImages',
labels="inferred",
label_mode="int",
class_names=None,
color_mode="rgb",
batch_size=32,
image_size=(128, 128),
shuffle=True,
seed=1,
validation_split=0.2,
subset="training",
interpolation="bilinear",
follow_links=False,
)

当我尝试使用(x_train, y_train), (x_test, y_test) = dataset将这个数据集输入我的神经网络时,我收到以下错误:

ValueError: too many values to unpack (expected 2)

我在下面包含了我对神经网络的尝试。

batch_size = 128
num_classes = 2
epochs = 12
# input image dimensions
img_rows, img_cols = 128, 128
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = dataset
x_train = x_train.reshape(869,128,128,3)
x_test = x_test.reshape(217,128,128,3)
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(28,28,1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

我相信我是不正确地导入图像到CNN,但不确定如何解决这个问题。正确导入图像的解决方案是什么?

编辑:下面是我更新的代码尝试。由于(x_train, y_train), (x_test, y_test) = train_ds返回ValueError: too many values to unpack (expected 2)
import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
import numpy as np

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
'C:\Users\Colin\Desktop\CNNImages\Training',
validation_split=None,
subset=None,
seed=123,
image_size=(128, 128),
batch_size=32)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
'C:\Users\Colin\Desktop\CNNImages\Validation',
validation_split=None,
subset=None,
seed=123,
image_size=(128, 128),
batch_size=32)

batch_size = 128
num_classes = 2
epochs = 12
# input image dimensions
img_rows, img_cols = 128, 128
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = train_ds
x_train = x_train.reshape(869,128,128,3)
x_test = x_test.reshape(217,128,128,3)
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(28,28,1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.fit(
train_ds,
validation_data=val_ds,
epochs=3
)
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

(x_train, y_train), (x_test, y_test) = dataset部分代码引发错误。因为,当您使用tf.keras.preprocessing.image_dataset_from_director()时,它会返回成批的图像,它不会分割你的数据分为训练集和测试集。因此,您需要分别为train和test声明:

# first-approach
train_dataset = tf.keras.preprocessing.image_dataset_from_directory(train_folder, ...)
test_dataset = tf.keras.preprocessing.image_dataset_from_directory(test_folder, ...)
# second approach
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
model.fit(
train_ds,
validation_data=val_ds,
epochs=3
)

相关内容

  • 没有找到相关文章

最新更新