Python Keras从CSV加载图像

Python Keras从CSV加载图像,python,csv,machine-learning,keras,Python,Csv,Machine Learning,Keras,我想创建一个卡定位器,但不知道如何在我的程序中加载数据 我的csv如下所示: imagepath, topleft, topright, bottomleft, bottomright train_1.jpg, 0.4343242, 0.234234, 0.323523, 0.3242342 图像是我的输入,topleft、topright、bottomleft、bottomright是我想要预测的标签。我看过很多教程,但都是关于图像分类的,它们使用了来自目录的flow_,但这只适用于二进制数

我想创建一个卡定位器,但不知道如何在我的程序中加载数据

我的csv如下所示:

imagepath, topleft, topright, bottomleft, bottomright
train_1.jpg, 0.4343242, 0.234234, 0.323523, 0.3242342
图像是我的输入,topleft、topright、bottomleft、bottomright是我想要预测的标签。我看过很多教程,但都是关于图像分类的,它们使用了来自目录的flow_,但这只适用于二进制数据

我的代码:

from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Dense, Activation, Conv2D, MaxPooling2D, Flatten, Dropout
from keras import losses, optimizers, metrics, layers, models, activations
from keras import backend as K

def get_input_shape(image_width, image_heigth, num_channels):
    if K.image_data_format() == 'channels_first':
        return (num_channels, image_width, image_heigth)
    else:
        return (image_width, image_heigth, num_channels)

def create_model(image_width, image_heigth, num_channels):
    input_shape = get_input_shape(image_width, image_heigth, num_channels)
    model = Sequential()
    model.add(Conv2D(32, (3, 3), activation=activations.relu, input_shape=input_shape))
    model.add(MaxPooling2D((2, 2)))
    model.add(Conv2D(64, (3, 3), activation=activations.relu))
    model.add(MaxPooling2D((2, 2)))
    model.add(Conv2D(128, (3, 3), activation=activations.relu))
    model.add(MaxPooling2D((2, 2)))
    model.add(Conv2D(128, (3, 3), activation=activations.relu))
    model.add(MaxPooling2D((2, 2)))
    model.add(Dropout(0.5))
    model.add(Flatten())
    model.add(Dense(512, activation=activations.relu))
    model.add(Dense(4, activation=activations.linear))
    model.compile(loss=losses.mean_squared_error, optimizer=optimizers.sgd(), metrics=[metrics.mean_squared_error])
    model.summary()
    return model

train_datagen = ImageDataGenerator(
        rotation_range=40,
        width_shift_range=0.2,
        height_shift_range=0.2,
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

model = create_model(640, 480, 3)

如果有人能给我指出正确的方向,我会很高兴。

你必须自己加载图像。谢天谢地,您可以访问Keras函数来实现这一点:

from keras.preprocessing import image
img = image.load_img(path, grayscale=False, target_size=None, interpolation='nearest')
img_array = image.img_to_array(img, data_format='channels_last')
将返回NumPy阵列。现在,您可以通过处理CSV文件将所有图像加载到一个数组中,并获得
X=(num\u images,W,H,C)
Y=(num\u images,4)
so4个预测值。您仍然可以通过以下方式使用ImageDataGenerator的额外预处理:

train_datagen.fit(X) # to compute data dependant statistics
model.fit_generator(train_datagen.flow(X, Y,...), ...)

你必须自己加载图像。谢天谢地,您可以访问Keras函数来实现这一点:

from keras.preprocessing import image
img = image.load_img(path, grayscale=False, target_size=None, interpolation='nearest')
img_array = image.img_to_array(img, data_format='channels_last')
将返回NumPy阵列。现在,您可以通过处理CSV文件将所有图像加载到一个数组中,并获得
X=(num\u images,W,H,C)
Y=(num\u images,4)
so4个预测值。您仍然可以通过以下方式使用ImageDataGenerator的额外预处理:

train_datagen.fit(X) # to compute data dependant statistics
model.fit_generator(train_datagen.flow(X, Y,...), ...)

正是我需要的,很好的解释@GerjanVlot你能帮我解决这个问题吗:正是我需要的,很好的解释@你能帮我解决这个问题吗