我正在使用Tensorflow识别图像。为此,我将图像转换为张量并尝试对其进行训练,并希望保存该模型并根据一个输入图像进行预测。
下面是我使用的代码,但由于此错误而卡住了,这可能是在培训时出现的。
<apex:page id="PageId" showheader="false">
<head>
<title>Image Classifier with TensorFlowJS</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.15.1">
</script>
</body>
<img id="imgshow" src="{!$Resource.cat}" crossorigin="anonymous" width="224" height="224" />
<button onClick="learnlinear()" lable = "Predict">Predict</button>
<script>
async function learnlinear()
{
//img data set
const imageHTML = document.getElementById('imgshow');
console.log('imageHTML::'+imageHTML.src);
//convert to tensor
const tensorImg = tf.fromPixels(imageHTML);
tensorImg.data().then(async function (stuffTensImg){
console.log('stuffTensImg::'+stuffTensImg.toString());
});
const resize_image = tf.reshape(tensorImg, [1, 224, 224, 3],'resize');
console.log('resize_image'+resize_image);
const model = await tf.loadModel('https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json');
console.log(':::::::'+JSON.stringify(model));
// Use ADAM optimizer with learning rate of 0.0005 and MSE loss
model.compile({
optimizer: tf.train.adam(0.0005),
loss: 'meanSquaredError',
});
console.log('model.compile',model.compile);
await model.fit(resize_image, {epochs: 500});
console.log('model.fit',model.fit);
const saveResult = await model.save();
console.log('saveResult',saveResult);
model.predict(resize_image.expandDims(0)).print()
console.log('model.predict',model.predict);
}
</script>
请帮助我解决此问题或如何获取此信息。