i made a datagenerator for a multi input model using this two references
https://datascience.stackexchange.com/questions/47623/how-feed-a-numpy-array-in-batches-in-keras
Keras open images and create a batch with them
so my generator looks like this
def load_data(df,path,position):
X_numerical = []
X_img = []
Y = []
for i in df.index.values:
# read one or more samples from your storage, do pre-processing, etc.
# for example:
basePath = os.path.sep.join([path, "{}-*".format(i)])
LiqPaths = sorted(list(glob.glob(basePath)))
img = tf.keras.preprocessing.image.load_img(LiqPaths[0], target_size=(100, 100))
img = tf.keras.preprocessing.image.img_to_array(img)
img = np.expand_dims(img, axis=0)
x=np.asarray(df.iloc[i]).astype('float32')
x=tf.expand_dims(x, axis=-1)
y = position[i]
X_numerical.append(x)
X_img.append(img)
Y.append(y)
return [np.array(X_numerical), np.array(X_img)], np.array(Y)
this is the generator
def batch_generator(df, path, position, batch_size):
batch=[]
# print(len(df.index.values))
while True:
for i in df.index.values:
batch.append(i)
if len(batch)==batch_size:
yield load_data(df,path,position)
batch=[]
and my model is this one
input_data = Input(name="input_numerical",shape=(2,1,))
x1 = LSTM(
units = 1024,
return_sequences=True
)(input_data)
x1 = LSTM(
units = 1024,
return_sequences=False
)(x1)
input_img = Input(name="input_images",shape=(60,30,3,))
x2 = Conv2D(128, (3, 3), name="first_conv", activation='relu', input_shape=(60, 30, 3))(input_img)
x2 =MaxPooling2D((2, 2),name="first_pooling")(x2)
x2 =Conv2D(256, (3, 3),name="second_conv", activation='relu')(x2)
x2 =MaxPooling2D((2, 2),name="seccond_pooling")(x2)
x2 = Flatten(name="flatten",)(x2)
x2 =Dense(1024, name="first_dense", activation='relu')(x2)
merge1 =concatenate([x1,x2], axis = 1)
x3 =Dense(1024,name="seccond_dense", activation='relu')(merge1)
output_liq =Dense(1,name="output", activation='sigmoid')(x3)
model = Model([input_img,input_data], output_liq)
model.summary()
tf.keras.utils.plot_model(
model, show_shapes=True
)
model.compile(
loss="BinaryCrossentropy",
optimizer=tf.keras.optimizers.Nadam(learning_rate=1e-6),
metrics=["BinaryAccuracy"])
history = model.fit(gen,
epochs= 30,
shuffle=False,
batch_size=256
#validation_data=([val_img, test_numerical1], val_y)
)
but it is to slow, to much i think, so i would like to know if you know how to make it perform faster, my dataset is about 44k pictures and a pandas whit 44k rows whit 2 features, i hope you can helpme