我是計算機視覺模型結構的新手,我正在使用 Tensorflow for Node JS@tensorflow/tfjs-node來讓一些模型檢測一些物件。使用 Mobilenet 和 Resnet SSD,模型使用該Channels Last格式,因此當我創建tf.node.decodeImage默認格式的張量時Channels Last,例如shape: [1, 1200, 1200, 3]3 通道,并且預測資料作業良好,能夠識別物件。
但是模型來自 Pytorch,轉換為 ONNX,然后轉換為 Protobuf PB 格式,saved_model.pb具有Channels First格式,如shape: [1, 3, 1200, 1200].
現在我需要從影像創建張量,但使用Channels First格式。我發現了很多創建conv1d, conv2d指定格式的例子dataFormat='channelsFirst'。但我不知道如何將其應用于影像資料。這是 API https://js.tensorflow.org/api/latest/#layers.conv2d。
這是張量代碼:
const tf = require('@tensorflow/tfjs-node');
let imgTensor = tf.node.decodeImage(new Uint8Array(subBuffer), 3);
imgTensor = imgTensor.cast('float32').div(255);
imgTensor = imgTensor.expandDims(0); // to add the most left axis of size 1
console.log('tensor', imgTensor);
這給了我最后一個通道的形狀,它與首先通道的模型形狀不兼容:
tensor Tensor {
kept: false,
isDisposedInternal: false,
shape: [ 1, 1200, 1200, 3 ],
dtype: 'float32',
size: 4320000,
strides: [ 4320000, 3600, 3 ],
dataId: {},
id: 7,
rankType: '4',
scopeId: 4
}
我知道tf.shape,但它在沒有先轉換為通道的情況下進行了重塑,結果在預測結果中似乎毫無用處。不知道我錯過了什么。
uj5u.com熱心網友回復:
你可以使用這樣的東西:
const nchw = tf.transpose(nhwc, [0, 3, 1, 2]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/396762.html
標籤:节点.js 张量流 计算机视觉 重塑 tensorflow.js
上一篇:資料集似乎缺少批處理維度-AKA如何構建具有多個輸出的資料集
下一篇:為影像資料分配標簽時遇到問題
