不幸的是,这不是一个简单的问题,因为它可能涉及为您的 Keras 模型使用可变大小的输入。虽然我认为可以通过正确使用占位符来做到这一点,但这对于初学者来说肯定不是一个起点。您的另一个选择是数据生成器。与许多计算密集型任务一样,通常需要在计算速度和内存需求之间进行权衡,使用生成器的计算量更大,它将完全在您的 cpu 上完成(没有 gpu 加速),但不会增加内存.
数据生成器的意义在于,它会一次将操作应用于图像以生成批次,然后在该批次上进行训练,然后释放内存 - 因此您最终只会保留一批数据随时在记忆中。不幸的是,如果你有一个耗时的生成,那么这会严重影响性能。
生成器将是一个python生成器(使用'yield'关键字)并且预计会生成单批次数据,keras非常擅长使用任意批次大小,因此您始终可以使一个图像产生一批,尤其是开始。
这是 fit_generator 上的 keras 页面 - 我警告你,这很快就会开始变得很多工作,考虑购买更多内存:
https://keras.io/models/model/#fit_generator
好吧,我会为你做的:P
import numpy as np
import pandas as pd
import keras
from keras.models import Model, model_from_json
from keras.layers import Dense, Concatenate, Multiply,Add, Subtract, Input, Dropout, Lambda, Conv1D, Flatten
from tensorflow.python.client import device_lib
# check for my gpu
print(device_lib.list_local_devices())
# make some fake image data
# 1000 random widths
data_widths = np.floor(np.random.random(1000)*100)
# producing 1000 random images with dimensions w x 100 x 3
# and a vector of which vertical lines are interesting
# I assume your data looks like this
images = []
interesting = []
for w in data_widths:
images.append(np.random.random([int(w),100,3]))
interesting.append(np.random.random(int(w))>0.5)
# this is a generator
def image_generator(images, interesting):
num = 0
while num < len(images):
windows = None
truth = None
D = images[num]
# this should look familiar
# Pad left and right
s = np.repeat(D[:1], 10, axis = 0)
e = np.repeat(D[-1:], 10, axis = 0)
# D now has shape (w + 20, 100, 3)
D = np.concatenate((s, D, e))
# Sliding windows creation trick from SO question
idx = np.arange(21)[None,:] + np.arange(len(D) - 20)[:,None]
windows = D[idx]
truth = np.expand_dims(1*interesting[num],axis=1)
yield (windows, truth)
num+=1
# the generator MUST loop
if num == len(images):
num = 0
# basic model - replace with your own
input_layer = Input(shape = (21,100,3), name = "input_node")
fc = Flatten()(input_layer)
fc = Dense(100, activation='relu',name = "fc1")(fc)
fc = Dense(50, activation='relu',name = "fc2")(fc)
fc = Dense(10, activation='relu',name = "fc3")(fc)
output_layer = Dense(1, activation='sigmoid',name = "output")(fc)
model = Model(input_layer,output_layer)
model.compile(optimizer="adam", loss='binary_crossentropy')
model.summary()
#and training
training_history = model.fit_generator(image_generator(images, interesting),
epochs =5,
initial_epoch = 0,
steps_per_epoch=len(images),
verbose=1
)