Jor*_*dan 2 python keras tensorflow
我一整天都在用谷歌搜索,试图在 Keras 中找到两个并行数据集的功能输入示例,但我找不到。
我的问题是我有数据集 1,一组执行不同动作的人的图像。它的格式为 csv,如下所示:
image_url,class
example1.png,BRUSH_TEETH
example2,BRUSH_TEETH
...
example10000.png,DANCING
Run Code Online (Sandbox Code Playgroud)
我将预处理这些并将它们全部设为 64x64。我的第二个数据集将是跳跃运动数据,其中每一行都是与数据集 1 中的相应行同时捕获的信息
(忽略列名和值,我不确定它们会是什么样子,因为我还没有收集数据,但它们将是一行并平行于上述数据集1)
x,y,z,a,b,c,d,class
1,2,3,4,5,6,7,BRUSH_TEETH
8,9,10,3,1,3,4,BRUSH_TEETH
...
1,2,3,4,5,6,7,DANCING
Run Code Online (Sandbox Code Playgroud)
我一直在阅读有关函数式 API 的信息,似乎我可以通过 CNN 运行来自 dataset1 的数据对象,同时运行来自 dataset2 的相同数据对象,例如通过深度 MLP。然后,使用合并或连接,将最终层的两个输出带到另一个深度 MLP,然后最终将这个最终合并的模型链接到一个输出
暂时忘记CNN,API给出了一个简单的合并示例如下:
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)
Run Code Online (Sandbox Code Playgroud)
我的问题是,我需要将 csv 中包含的图像输入 input1(以 CNN 的形式),同时向 input2 输入包含 Leap Motion 数据的第二个数据集中的相关行。PS:在上面我将如何在输出前与两个密集层合并后继续模型?会不会只是这样:
x3 = keras.layers.Dense(100)(added)
x3 = keras.layers.Dense(50)(x3)
out = keras.layers.Dense(4)(x3)
Run Code Online (Sandbox Code Playgroud)
这可以执行吗?如果是这样,我将非常感谢您的帮助,我正在失去理智,试图让我的头脑了解如何使两个数据集彼此保持同步!
我可以尝试使用的示例脚本会很棒,因为我对 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))
Run Code Online (Sandbox Code Playgroud)
您可以使用 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
Run Code Online (Sandbox Code Playgroud)
model.fit() 有一个“validation_split”参数,可以设置为 0.3 以创建 70:30 拆分。model.fit() 返回一个 History 对象,该对象可用于绘制精度曲线,或者您可以使用 TensorBoard 回调进行实时跟踪。
https://chrisalbon.com/deep_learning/keras/visualize_loss_history/ https://keras.io/callbacks/#tensorboard