ame*_*eya 3 javascript deep-learning tensorflow tensorflow.js
我在 pytorch 的训练阶段应用了转换,然后将模型转换为在 tensorflow.js 中运行。它工作正常,但由于我没有应用相同的转换而得到了错误的预测。
test_transform = torchvision.transforms.Compose([
torchvision.transforms.Resize(size=(224, 224)),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
Run Code Online (Sandbox Code Playgroud)
我可以调整图像大小但无法标准化。我怎样才能做到这一点?
更新:-
test_transform = torchvision.transforms.Compose([
torchvision.transforms.Resize(size=(224, 224)),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
Run Code Online (Sandbox Code Playgroud)
我尝试了这段代码,但它没有正确规范化图像。
img = tf.image.resizeBilinear(img, [224, 224]).div(tf.scalar(255))
img = tf.cast(img, dtype = 'float32');
Run Code Online (Sandbox Code Playgroud)
完整功能如下——
function imgTransform(img){
img = tf.image.resizeBilinear(img, [224, 224]).div(tf.scalar(255))
img = tf.cast(img, dtype = 'float32');
/*mean of natural image*/
let meanRgb = { red : 0.485, green: 0.456, blue: 0.406 }
/* standard deviation of natural image*/
let stdRgb = { red: 0.229, green: 0.224, blue: 0.225 }
let indices = [
tf.tensor1d([0], "int32"),
tf.tensor1d([1], "int32"),
tf.tensor1d([2], "int32")
];
/* sperating tensor channelwise and applyin normalization to each chanel seperately */
let centeredRgb = {
red: tf.gather(img,indices[0],2)
.sub(tf.scalar(meanRgb.red))
.div(tf.scalar(stdRgb.red))
.reshape([224,224]),
green: tf.gather(img,indices[1],2)
.sub(tf.scalar(meanRgb.green))
.div(tf.scalar(stdRgb.green))
.reshape([224,224]),
blue: tf.gather(img,indices[2],2)
.sub(tf.scalar(meanRgb.blue))
.div(tf.scalar(stdRgb.blue))
.reshape([224,224]),
}
/* combining seperate normalized channels*/
let processedImg = tf.stack([
centeredRgb.red, centeredRgb.green, centeredRgb.blue
]).expandDims();
return processedImg;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2969 次 |
| 最近记录: |