gpt4 book ai didi

python - Keras 从 CSV 加载图像

转载 作者:行者123 更新时间:2023-11-30 09:33:05 25 4
gpt4 key购买 nike

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

我的 csv 看起来像这样:

imagepath, topleft, topright, bottomleft, bottomright
train_1.jpg, 0.4343242, 0.234234, 0.323523, 0.3242342

图像是我的输入,左上角、右上角、左下角、右下角是我想要预测的标签。我看过很多教程,但它们都是关于图像分类的,并且使用 flow_from_directory 但仅适用于二进制数据。

我的代码:

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_images, W, H, C)Y=(num_images, 4) code> 所以每个图像有 4 个预测。您仍然可以通过以下方式使用 ImageDataGenerator 的额外预处理:

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

关于python - Keras 从 CSV 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50745516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com