coremltools导出模型时输入维度错误?

2024-05-19 01:17:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我通过coremltools导出了我的模型:

coreml_model = coremltools.converters.keras.convert(model)
coreml_model.save('my_model.mlmodel')

当我把它导入iOS时,我得到了:

input1 MultiArray (Double 1)

在我看来输入维度不正确。我认为它应该是30个双值和1个二进制输出,那么可能是一个MultiArray (Double 30)作为输入?在

不管怎样,我还是试过了:

^{pr2}$

我得到一个错误:

Input feature input1 was presented as a vector of length 30, but the model expects an input of length 1.

这是我的出口代码:

batch_size = 50
epochs = 10
test_size = 500
input_dim = 30

data = np.load(data_filename)
x, y = data['x'], data['y']  # x.shape = (3114, 30) 3114 training samples with 30 data points per sample

# y is a category matrix [0, 1, 0, 0] means that there's a one. [0, 0, 0, 1] means there is a 4. 
# y.shape = (3114, 2); In our case we only have 0's and 1's, so [1, 0] or [0, 1]

# Shuffle them
p = np.random.permutation(x.shape[0])
x, y = x[p], y[p]

x_train, x_test = x[:2000], x[2000:]
y_train, y_test = y[:2000], y[2000:]

x_train, x_test = np.expand_dims(x_train, axis=2), np.expand_dims(x_test, axis=2)
#Add another dimension. I don't really know why we do this. x_train.shape = (2000, 30, 1)

# Model Architecture
model = Sequential()

# Convolutional Layer
model.add(Conv1D(filters=10, kernel_size=5, input_shape=(input_dim, 1))) #
model.add(Flatten())

# Dense Layer
model.add(Dense(input_dim, activation='relu'))

# Logits Layer
# model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))

rmsprop = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=rmsprop,
    loss='binary_crossentropy',
    metrics=['accuracy'],
)

model.fit(x=x_train, y=y_train, batch_size=batch_size, epochs=epochs)

accuracy = model.evaluate(x=x_test, y=y_test, batch_size=batch_size)

print("Overall accuracy: {}".format(accuracy[1]))
print(model.output.op.name)

coreml_model = coremltools.converters.keras.convert(model)
coreml_model.save('my_model.mlmodel')

Tags: testaddinputdatasizemodelnpbatch

热门问题