加速器数据应该具有什么样的结构来进行模型预测?

What structure should the accelerator data have for model prediction?

我创建了 Keras 模型,我将其转换为 TensorFlow,然后将其转换为 TensorFlow lite。我想使用我的 TFLite 模型使用来自移动 phone 的加速器信号来预测人类 activity。这是我的模型序列:

N_FEATURES = 3
PERIOD = 80


model = Sequential()
model.add(Reshape((const.PERIOD, const.N_FEATURES), input_shape=(const.PERIOD * const.N_FEATURES,)))
model.add(Conv1D(100, 10, activation='relu', input_shape=(const.PERIOD, const.N_FEATURES)))
model.add(Conv1D(100, 10, activation='relu'))
model.add(MaxPooling1D(const.N_FEATURES))
model.add(Conv1D(160, 10, activation='relu'))
model.add(Conv1D(160, 10, activation='relu'))
model.add(Flatten())
model.add(Dropout(0.2))
model.add(Dense(7, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])


x_test.shape = (12369, 240)
y_test.shape = (12369, 7)
x_train.shape = (49476, 240)
y_train.shape = (49476, 7)

我想问一下我需要传递给 Android 应用中的模型以预测 activity 的数据的形状。我使用的函数是Interpreter.runForMultipleInputsOutputs。我需要使用包含三个列表的数组吗?他们每个人都从加速器的一个轴(x、y 和 z)获取数据,或者我需要创建其他东西?

这是我的第一个模型,欢迎任何其他提示。

编辑:

        List<Sample> samples = collector.getSamples();
        float[][] floatInputBuffer = new float[200][3];

        for(int i = 0; i < 200; i++) {
            floatInputBuffer[i][0] = samples.get(i).getX();
            floatInputBuffer[i][1] = samples.get(i).getY();
            floatInputBuffer[i][2] = samples.get(i).getZ();
        }

        Object[] inputArray = {floatInputBuffer, new int[]{5000}};
        Map<Integer, Object> outputMap = new HashMap<>();
        outputMap.put(0, new float[1][labels.size()]);
        Interpreter interpeter = null;
        try {
            interpeter = new Interpreter(loadModel(getAssets(), MODEL_PATH.split("file:///android_asset/", -1)[1]));
        } catch (IOException e) {
            e.printStackTrace();
        }

        interpeter.runForMultipleInputsOutputs(inputArray, outputMap);

您需要一个形状为 (examples_, time_frames, dimensions_x_y_z)

的数组

它已经在您的模型中用 input_shape=(const.PERIOD, const.N_FEATURES) 明确定义了。

因此,(examples, const.PERIOD, const.N_FEATURES) 是您的数据格式。

无论在训练模型时使用何种输入大小,在使用解释器执行推理时都必须发送相同大小的输入(批量大小除外)。如果您在解释器上使用 run 方法,那么您可能只传递了一个输​​入,但由于您正在使用 runForMultipleInputsOutputs,您可以将数据批处理在一起然后发送。

因此,您的模型的输入形状为 (const.PERIOD * const.N_FEATURES)。我相信这个产品是 240。所以你输入到 TFLite 模型的形状必须是 (<<No. of inputs>>, 240).