machine-learning - 如何在内存和批处理方面使用大数据集进行多标签图像分类

标签 machine-learning keras deep-learning image-recognition multilabel-classification

我正在处理 300K 图像的数据集,进行多类图像分类。到目前为止,我获取了大约 7k 图像的小数据集,但代码要么返回内存错误,要么我的笔记本就死机了。下面的代码将所有图像立即转换为 numpy 数组,这会在执行最后一行代码时导致我的内存出现问题。 train.csv 包含图像文件名和一个热编码标签。 代码是这样的:

data = pd.read_csv('train.csv')

img_width = 400
img_height = 400

img_vectors = []

for i in range(data.shape[0]):
    path = 'Images/' + data['Id'][
    img = image.load_img(path, target_size=(img_width, img_height, 3))
    img = image.img_to_array(img)
    img = img/255.0
    img_vectors.append(img)

img_vectors = np.array(img_vectors)

错误消息:

MemoryError                               Traceback (most recent call last)
<ipython-input-13-dd2302ae54e1> in <module>
----> 1 img_vectors = np.array(img_vectors)

MemoryError: Unable to allocate array with shape (7344, 400, 400, 3) and data type float32

我想我需要一批较小的数组来处理所有图像来处理内存问题,以避免一个数组同时包含所有图像数据。

在早期的项目中,我对大约 225k 图像进行了没有多标签的图像分类。无论如何,这段代码不会将所有图像数据转换为一个巨大的数组。而是将图像数据分成更小的批处理:

#image preparation
if K.image_data_format() is "channels_first":
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

train_datagen = ImageDataGenerator(rescale=1./255, horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical')
validation_generator = test_datagen.flow_from_directory(validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical')

model = Sequential()
model.add(Conv2D(32, (3,3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
...
model.add(Dense(17))
model.add(BatchNormalization(axis=1, momentum=0.6))
model.add(Activation('softmax'))

model.summary()    

model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size,
    class_weight = class_weight
)

所以我真正需要的是一种如何处理大图像数据集以进行多标签图像分类而不会遇到内存问题的方法。 理想的情况是使用包含图像文件名和单热编码标签的 csv 文件与数组批处理相结合进行学习。

这里的任何帮助或猜测将不胜感激。

最佳答案

解决您面临的问题的最简单方法是编写一个服装数据生成器,这里有一个 tutorial这展示了如何做到这一点。这个想法是,您不使用 flow_from_directory,而是创建一个服装数据加载器,它从源路径读取每个图像并为 y 提供相应的标签。实际上,我认为您的数据存储在 .csv 文件中,其中每一行包含图像的路径以及图像中存在的标签。因此,您的数据生成器将具有一个函数 getittem(self, index) ,该函数将从原始数字索引中的路径读取图像,并与通过读取此原始数字和一个中的标签获得的目标一起返回对它们进行热编码,然后对它们求和。

关于machine-learning - 如何在内存和批处理方面使用大数据集进行多标签图像分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59808871/

相关文章:

python - 为什么 pandas 分类 DataFrame 给出真值错误?

machine-learning - 当精度比召回率重要得多时,如何调整模型以提高精度?

python - 狗与猫 : How is the data labelled?

tensorflow 错误: Failed to convert object of type <class 'dict' > to Tensor

database - 使用 hdf5 进行深度学习的计算优势

machine-learning - 恢复以保持训练不适用于 tensorflow

machine-learning - 如何在 Keras 中拟合模型期间检查预测输出?

tensorflow - Tensorflow 中的回调

python - ValueError : Error when checking target: expected dense_6 to have shape (46, )但得到形状为(1,)的数组

c++ - 如何在caffe中将float型数据写入或转换为leveldb