简体   繁体   English

如何将图像输入 CNN

[英]How to feed images into a CNN

I am new to tensorflow, and am trying to create a convolutional neural network for binary classification that can distinguish the difference between a normal face and the face of someone who is having a stroke.我是 tensorflow 的新手,我正在尝试创建一个用于二元分类的卷积神经网络,以区分正常人脸和中风患者的脸。

The images for my dataset are contained within a directory called CNNImages , and contains two subdirectories: RegularFaces and Strokes .我的数据集的图像包含在一个名为CNNImages的目录中,并包含两个子目录: RegularFacesStrokes Within each subdirector are the PNG images I'm trying to feed into the neural network.在每个子目录中都是我试图输入神经网络的 PNG 图像。

Following the approach suggested in this reference: https://towardsdatascience.com/build-your-own-convolution-neural-network-in-5-mins-4217c2cf964f , I've successfully used Spyder to create the neural network itself, which works when ran with mnist.load_data().按照本参考资料中建议的方法: https://towardsdatascience.com/build-your-own-convolution-neural-network-in-5-mins-4217c2cf964f ,我已经成功使用 Spyder 创建了神经网络本身,与 mnist.load_data() 一起运行时工作。

However, I am having trouble using tf.data.Dataset to upload my own images into the neural network.但是,我无法使用 tf.data.Dataset 将自己的图像上传到神经网络中。 When I try to train my neural network on the image database I created, it returns a ValueError and states "too many values to unpack (expected 2)".当我尝试在我创建的图像数据库上训练我的神经网络时,它返回一个 ValueError 并指出“要解包的值太多(预期为 2)”。 I believe I'm either calling my database incorrectly or messsed something up with the database creation.我相信我要么错误地调用了我的数据库,要么在创建数据库时搞砸了。

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

os.chdir("/Users/Colin/CNNImages")

files = tf.data.Dataset.list_files("/Users/Colin/CNNImages/*/*.png")

def load_images(path):
    image = tf.io.read_file(path)
    image = tf.io.decode_jpeg(image)
    image = tf.image.convert_image_dtype(image, tf.float32)
    image = tf.image.resize(image, (128, 128))

    parts = tf.strings.split(path, os.path.sep)
    bool_values = tf.equal(parts[-2], 'strokes')
    indices = tf.cast(bool_values, tf.int32)
    return image, indices

ds = files.map(load_images).batch(1)

next(iter(ds))

"""
Above: Image Formatter
Below: CNN
"""


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

When I call ds for (x_train, y_train), (x_test, y_test) = ds , I receive the ValueError which states "too many values to unpack (expected 2)".当我为(x_train, y_train), (x_test, y_test) = ds时,我收到 ValueError ,其中指出“要解压的值太多(预期为 2)”。 Did I mess that line up?我把那条线弄乱了吗? Or did I design my tf dataset improperly?还是我设计的 tf 数据集不正确?

Each element of your tf.data.Dataset is a tuple (img,label) . tf.data.Dataset的每个元素都是一个元组(img,label) If you want to create a validation split, you should use take and skip to create it.如果要创建验证拆分,则应使用takeskip来创建它。 You also can't reshape and apply functions to the Dataset the way you are doing it later in the script.您也无法按照稍后在脚本中执行的方式重塑函数并将其应用于Dataset


To create a train/validation split on the dataset, use skip and take :要在数据集上创建训练/验证拆分,请使用skiptake

# number of element in the validation dataset
n_elem_validation_ds = 267 
val_ds = ds.take(n_elem_validation_ds)
train_ds = ds.skip(n_elem_validation_ds)

To apply functions to your dataset, use map :要将函数应用于您的数据集,请使用map

# convert class vectors to binary class matrices
helper_categorical = lambda x: keras.utils.to_categorical(x, num_classes)
ds = ds.map(lambda img, label: (img, helper_categorical(label)))

Note: you can skip that keras.utils.to_categorical( function and use sparse_categorical_crossentropy as a loss function instead.注意:您可以跳过keras.utils.to_categorical( function 并使用sparse_categorical_crossentropy作为损失 ZC1C425268E68385D1AB5074C17A94F1。


To fit your model on the dataset, simply pass the tf.data.Dataset to the fit function:要在数据集上拟合 model,只需将tf.data.Dataset传递给拟合 function:

model.fit(train_ds, validation_data=val_ds)

To go further, you should read the following guide: tf.data: Build TensorFlow input pipelines .要进一步了解 go,您应该阅读以下指南: tf.data: Build TensorFlow input pipelines

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

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