Python KERA中两个并行但不同的数据集作为多个输入?

Python KERA中两个并行但不同的数据集作为多个输入?,python,tensorflow,keras,Python,Tensorflow,Keras,我在谷歌上搜索了一整天,试图找到Keras中两个并行数据集的函数输入示例,但找不到一个 我的问题是我有数据集1,一组执行不同操作的人的图像。其格式为csv,如下所示: image_url,class example1.png,BRUSH_TEETH example2,BRUSH_TEETH ... example10000.png,DANCING import keras input1 = keras.layers.Input(shape=(16,)) x1 = keras.layers.D

我在谷歌上搜索了一整天,试图找到Keras中两个并行数据集的函数输入示例,但找不到一个

我的问题是我有数据集1,一组执行不同操作的人的图像。其格式为csv,如下所示:

image_url,class
example1.png,BRUSH_TEETH
example2,BRUSH_TEETH
...
example10000.png,DANCING
import keras

input1 = keras.layers.Input(shape=(16,))
x1 = keras.layers.Dense(8, activation='relu')(input1)
input2 = keras.layers.Input(shape=(32,))
x2 = keras.layers.Dense(8, activation='relu')(input2)
# equivalent to added = keras.layers.add([x1, x2])
added = keras.layers.Add()([x1, x2])

out = keras.layers.Dense(4)(added)
model = keras.models.Model(inputs=[input1, input2], outputs=out)
我将对这些进行预处理,使它们的大小都为64x64。我的第二个数据集是leap motion数据,其中每一行都是与数据集1中的对应行同时捕获的信息

(忽略列名和值,我还不确定它们是什么样子,因为我还没有收集数据,但它们将是一行,与上面的数据集1平行)

我一直在阅读有关函数API的文章,似乎我可以通过CNN从dataset1运行数据对象,同时通过deep MLP从dataset2运行相同的数据对象。然后,使用merge或concatenate,将两个输出从其最终层带到另一个深度MLP,然后最终将此最终合并模型链接到一个输出

暂时忘记CNN,API给出了一个简单的合并示例,如下所示:

image_url,class
example1.png,BRUSH_TEETH
example2,BRUSH_TEETH
...
example10000.png,DANCING
import keras

input1 = keras.layers.Input(shape=(16,))
x1 = keras.layers.Dense(8, activation='relu')(input1)
input2 = keras.layers.Input(shape=(32,))
x2 = keras.layers.Dense(8, activation='relu')(input2)
# equivalent to added = keras.layers.add([x1, x2])
added = keras.layers.Add()([x1, x2])

out = keras.layers.Dense(4)(added)
model = keras.models.Model(inputs=[input1, input2], outputs=out)
我的问题是,我需要向input1(以CNN的形式)提供csv中包含的图像,同时向input2提供包含跳跃运动数据的第二个数据集中的相关行。PS:在输出之前,在合并两个密集层之后,在上面我将如何继续模型?是否简单地说:

x3 = keras.layers.Dense(100)(added)
x3 = keras.layers.Dense(50)(x3)
out = keras.layers.Dense(4)(x3)
这可以执行吗?如果是这样的话,我将非常感谢你的帮助,我正在发疯,试图弄清楚这两个数据集是如何保持彼此同步的

一个我可以尝试并使用的示例脚本将是非常好的,因为我对Keras框架比较陌生


多谢各位

请检查这是否有用。使用Keras 2.2.4进行测试

from keras.layers import Conv2D, MaxPooling2D, Input, Dense, Flatten, concatenate
from keras.models import Model
import numpy as np

img_input = Input(shape=(64, 64, 1))  ## branch 1 with image input
x = Conv2D(64, (3, 3))(img_input)
x = Conv2D(64, (3, 3))(x)
x = MaxPooling2D((2, 2))(x)
x = Flatten()(x)
out_a = Dense(64)(x)

num_input = Input(shape=(7,))        ## branch 2 with numerical input
x1 = Dense(8, activation='relu')(num_input)
out_b = Dense(16, activation='relu')(x1)

concatenated = concatenate([out_a, out_b])    ## concatenate the two branches
out = Dense(4, activation='softmax')(concatenated)
model = Model([img_input, num_input], out)
print(model.summary())
model.compile('sgd', 'categorical_crossentropy', ['accuracy'])

### Just for sanity check
X = [np.zeros((1,64,64,1)), np.zeros((1,7))]
y = np.ones((1,4))
model.fit(X, y)
print(model.predict(X))
您可以使用Pandas读取输入数据

from PIL import Image
import pandas as pd

def get_num_input():
    df = pd.read_csv('num.csv')
    columns = list(df.columns)
    features = columns[:-1]
    cls_name = columns[-1]
    X = np.zeros((len(df), len(features)))
    Y = list()
    for i, row in df.iterrows():
        X[i] = row[features]
        Y.append(row[cls_name])

    return (X, Y)

def get_img_input():
    df = pd.read_csv('img.csv')
    X_img = np.zeros((len(df), 28, 28)) # change as per image size
    Y = list()
    for i, row in df.iterrows():
        X_img[i] = np.array(Image.open(row['image_url']))
        Y.append(row['class'])

    return (X_img, Y)


X_num, Y = get_num_input()
X_img, _ = get_img_input() # use one of the Ys
# X feature normalization, convert Y to one-hot representation
model.fit()有一个“validation\u split”参数,可以将该参数设置为0.3以创建70:30的分割。model.fit()返回一个历史对象,可用于绘制精度曲线,也可使用TensorBoard回调进行实时跟踪


你读过这个吗@MatiasValdenegro谢谢你的建议,我已经读过了,但我不太明白来自多个数据集的混合数据在哪里或如何保持同步,因为输入每个输入数组的索引必须匹配1:1,这就是你所说的同步。非常感谢Manoj,您是否可以添加我将定义两个csv数据集并将它们提供给每个分支的部分?另外,你能解释一下他们是如何在70/30分为历史的数据中使用他们定义的类别进行拟合/培训的,以便我以后可以绘制其进度图吗?根据你的问题更新了答案。Genius。非常感谢。