简体   繁体   English

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

[英]How to feed images into a CNN for binary classification

I am trying to create a convolutional neural network that can detect whether or not a person is having a stroke, based upon a picture of their face.我正在尝试创建一个卷积神经网络,该网络可以根据一个人的面部图片检测一个人是否中风。 The images for my dataset are contained within a directory called CNNImages , which contains two subdirectories: Strokes and RegularFaces .我的数据集的图像包含在一个名为CNNImages的目录中,该目录包含两个子目录: StrokesRegularFaces Each subdirectory contains jpg images that I'm trying to feed into my neural network.每个子目录都包含我试图输入我的神经网络的 jpg 图像。

Following the approach used in this tutorial , I have created the CNN, which works when fed with the MNIST dataset.按照本教程中使用的方法,我创建了 CNN,它在输入 MNIST 数据集时可以工作。 However, I am having trouble feeding my own images into the neural network.但是,我无法将自己的图像输入神经网络。 I have been using the code shown by the Keras tutorial for Image data preprocessing, but it isn't working.我一直在使用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,
)

When I try to feed this dataset into my neural network using (x_train, y_train), (x_test, y_test) = dataset , I receive the following error:当我尝试使用(x_train, y_train), (x_test, y_test) = dataset将此数据集输入我的神经网络时,我收到以下错误:

ValueError: too many values to unpack (expected 2)

I've included my attempt at a neural network below.我在下面的神经网络中包含了我的尝试。

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])

I believe I am importing the images incorrectly into the CNN, but am unsure of how to fix this.我相信我将图像错误地导入 CNN,但不确定如何解决这个问题。 What would be the solution to getting the images to import correctly?让图像正确导入的解决方案是什么?

Edit: Below is my updated code attempt.编辑:下面是我更新的代码尝试。 It is unable to function, due to (x_train, y_train), (x_test, y_test) = train_ds returning ValueError: too many values to unpack (expected 2)无法 function,由于(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 part of the code raises error. (x_train, y_train), (x_test, y_test) = dataset部分引发错误。 Because, when you use tf.keras.preprocessing.image_dataset_from_director() , it returns batches of images, it does not split your data into train set and test set.因为,当您使用tf.keras.preprocessing.image_dataset_from_director()时,它会返回批量图像,它不会将您的数据拆分为训练集和测试集。 So you need to declare seperately for train and 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
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM