Javascript 错误:输入0与层conv2d_Conv2D1不兼容:预期ndim=4,发现ndim=5
我正在尝试训练一个带有字母“L”(132x180)的tensorflow模型,我对tf还很陌生。我希望在使用图像进行训练时能得到一些帮助。通过更改输入形状进行修复:Javascript 错误:输入0与层conv2d_Conv2D1不兼容:预期ndim=4,发现ndim=5,javascript,node.js,tensorflow,Javascript,Node.js,Tensorflow,我正在尝试训练一个带有字母“L”(132x180)的tensorflow模型,我对tf还很陌生。我希望在使用图像进行训练时能得到一些帮助。通过更改输入形状进行修复: async function train() { var labels = [0] // 0 = L var tensorLabels = tf.oneHot(tf.tensor1d(labels, 'int32'), 3); var buffer = fs.readFileSync(&quo
async function train() {
var labels = [0] // 0 = L
var tensorLabels = tf.oneHot(tf.tensor1d(labels, 'int32'), 3);
var buffer = fs.readFileSync("./train/L/L.png")
var tensorFeature = tf.node.decodeImage(buffer)
var tensorFeatures = tf.stack([tensorFeature])
const model = tf.sequential();
model.add(tf.layers.conv2d({
inputShape: [1, 132, 180, 3], // numberOfChannels = 3 for colorful images and one otherwise
filters: 32,
kernelSize: 3,
activation: 'relu',
}));
model.add(tf.layers.flatten()),
model.add(tf.layers.dense({units: 3, activation: 'softmax'}));
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'})
model.fit(tensorFeatures, tensorLabels)
}
通过更改inputShape进行修复:
async function train() {
var labels = [0] // 0 = L
var tensorLabels = tf.oneHot(tf.tensor1d(labels, 'int32'), 3);
var buffer = fs.readFileSync("./train/L/L.png")
var tensorFeature = tf.node.decodeImage(buffer)
var tensorFeatures = tf.stack([tensorFeature])
const model = tf.sequential();
model.add(tf.layers.conv2d({
inputShape: [1, 132, 180, 3], // numberOfChannels = 3 for colorful images and one otherwise
filters: 32,
kernelSize: 3,
activation: 'relu',
}));
model.add(tf.layers.flatten()),
model.add(tf.layers.dense({units: 3, activation: 'softmax'}));
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'})
model.fit(tensorFeatures, tensorLabels)
}