android - 如何将通道值标准化为范围 [0.0,1.0]?
问题描述
我是 Tensorflow 和图像处理的新手,我使用将位图转换为 Inputarray 的代码来预测来自 Tensorflow Lite 库和 Firebase MLKit 自定义模型 API 的对象。我正在尝试将通道值标准化为范围 [0.0,1.0]。写的评论表明这个函数将通道值标准化为 [-1.0,1.0] 的范围,并且可以标准化为 [0.0,1.0] 但没有解释如何做到这一点。我怎样才能做到这一点?
更新
这个问题的原因是从 ibug 数据集的图像上的 tflite 模型中获取正确的值:我有一个 Tensorflow Lite 模型,我想预测眼睛区域标志值,我试图用图像作为输入来预测眼睛区域点一个值数组(x 和 y 坐标)作为输出。
我得到了一个 python 脚本,它使用 Tensorflow lite 模型从图像中获取预测。
imgUrl = "https://i.imgur.com/Micp1bv.jpg" # image from ibug dataset
img = Image.open(requests.get(imgUrl, stream=True).raw)
img.load()
img = img.resize((112, 112), PIL.Image.ANTIALIAS) # resize to width and height of input tensor parameters
# Normalize to [0, 1]
data = np.asarray( img, dtype="float32" ) /255.0
# Inference on input data normalized to [0, 1]
inputImg = np.expand_dims(data,0).astype(np.float32)
input_details = interpreter.get_input_details()
interpreter.set_tensor(input_details[0]['index'], inputImg)
interpreter.invoke()
output_details = interpreter.get_output_details()
output_data = interpreter.get_tensor(output_details[0]['index'])
print(output_data) // an arrays contains values ( x and y coordinates of landmarks) as an output
private float[][][][] bitmapToInputArray() {
// [START mlkit_bitmap_input]
Bitmap bitmap= getYourInputImage();
bitmap = Bitmap.createScaledBitmap(bitmap, 112, 112, true);
int batchNum = 0;
float[][][][] input = new float[1][112][112][3];
for (int x = 0; x < 112; x++) {
for (int y = 0; y < 112; y++) {
int pixel = bitmap.getPixel(x, y);
// Normalize channel values to [-1.0, 1.0]. This requirement varies by
// model. For example, some models might require values to be normalized
// to the range [0.0, 1.0] instead.
input[batchNum][x][y][0] = (Color.red(pixel) - 127) / 128.0f;
input[batchNum][x][y][1] = (Color.green(pixel) - 127) / 128.0f;
input[batchNum][x][y][2] = (Color.blue(pixel) - 127) / 128.0f;
Log.i("Input","input"+input[batchNum][x][y][0]);
Log.i("input","input"+input[batchNum][x][y][1]);
}
}
// [END mlkit_bitmap_input]
return input;
}
那是 python 脚本的输出:
[[0.21560352 0.37226492 0.23046923 0.3314699 0.26969466 0.31294012 0.31079206 0.32817352 0.32920814 0.367611 0.31699485 0.41163784 0.27505988 0.42706913 0.23404554 0.4119034 0.15523753 0.3914298 0.18589666 0.37025875 0.22555524 0.3547908 0.27040404 0.3469348 0.3122904 0.35289326 0.34334075 0.3686381 0.36434904 0.3907298 0.34113637 0.40280795 0.3069372 0.40720087 0.26726058 0.4101103 0.22570357 0.40932944 0.1866894 0.40394992 0.6993889 0.3493917 0.7078744 0.3110081 0.73927087 0.2935116 0.77587724 0.30691338 0.79591703 0.3434749 0.787707 0.38200834 0.7559439 0.3995327 0.7166693 0.38873363 0.6655569 0.3727552 0.68886995 0.3488456 0.720377 0.3310734 0.75820756 0.3226342 0.79140866 0.3270979 0.8144976 0.33877644 0.8281598 0.3576495 0.81746304 0.37150782 0.7971135 0.37963405 0.7682784 0.3838213 0.73269796 0.38409975 0.69557863 0.3825233]]
这是 Android Studio 的输出(Java 代码):
2019-08-17 17:19:18.754 19647-19647/com.example.irisdetection I/MLKit: 0,22430149 2019-08-17 17:19:18.756 19647-19647/com.example.irisdetection I/MLKit: 0 ,23864979 2019-08-17 17:19:18.758 19647-19647/com.example.irisdetection I/MLKit: 0,27703676 2019-08-17 17:19:18.759 19647-19647/com.example.irisdetection I/MLKit : 0,31750143 2019-08-17 17:19:18.760 19647-19647/com.example.irisdetection I/MLKit: 0,33583546 2019-08-17 17:19:18.761 19647-19647/com.example.irisdetection I /MLKit:0,32406592 2019-08-17 17:19:18.762 19647-19647/com.example.irisdetection I/MLKit:0,28302023 2019-08-17 17:19:18.763 19647-19647/com.example。 irisdetection I/MLKit: 0,24265678 2019-08-17 17:19:18.764 19647-19647/com.example.irisdetection I/MLKit: 0,16428351 2019-08-17 17:19:18.765 19647-19647/com. example.irisdetection I/MLKit:0,19479913 2019-08-17 17:19:18。766 19647-19647/com.example.irisdetection I/MLKit: 0,23414856 2019-08-17 17:19:18.767 19647-19647/com.example.irisdetection I/MLKit: 0,27845544 2019-08-17 17: 19:18.767 19647-19647/com.example.irisdetection I/MLKit: 0,31952256 2019-08-17 17:19:18.774 19647-19647/com.example.irisdetection I/MLKit: 0,34978011 2019-08-17 17:19:18.775 19647-19647/com.example.irisdetection I/MLKit: 0,37012532 2019-08-17 17:19:18.776 19647-19647/com.example.irisdetection I/MLKit: 0,34773278 2019-08 -17 17:19:18.777 19647-19647/com.example.irisdetection I/MLKit: 0,31446189 2019-08-17 17:19:18.778 19647-19647/com.example.irisdetection I/MLKit: 0,27557194 2019 -08-17 17:19:18.778 19647-19647/com.example.irisdetection I/MLKit: 0,23455118 2019-08-17 17:19:18.780 19647-19647/com.example.irisdetection I/MLKit: 0, 19578205 2019-08-17 17:19:18.782 19647-19647/com。example.irisdetection I/MLKit: 0,69702154 2019-08-17 17:19:18.785 19647-19647/com.example.irisdetection I/MLKit: 0,70499951 2019-08-17 17:19:18.786 19647-19647/ com.example.irisdetection I/MLKit: 0,73528731 2019-08-17 17:19:18.787 19647-19647/com.example.irisdetection I/MLKit: 0,77087343 2019-08-17 17:19:18.788 19647- 19647/com.example.irisdetection I/MLKit: 0,79054081 2019-08-17 17:19:18.790 19647-19647/com.example.irisdetection I/MLKit: 0,78286630 2019-08-17 17:19:18.791 19647-19647/com.example.irisdetection I/MLKit: 0,75219637 2019-08-17 17:19:18.792 19647-19647/com.example.irisdetection I/MLKit: 0,71401721 2019-08-17 17:19 :18.793 19647-19647/com.example.irisdetection I/MLKit: 0,66399622 2019-08-17 17:19:18.794 19647-19647/com.example.irisdetection I/MLKit: 0,68674380 2019-08-17 17 :19:18.795 19647-19647/com.example。irisdetection I/MLKit: 0,71746421 2019-08-17 17:19:18.797 19647-19647/com.example.irisdetection I/MLKit: 0,75426823 2019-08-17 17:19:18.798 19647-19647/com。 example.irisdetection I/MLKit: 0,78634208 2019-08-17 17:19:18.799 19647-19647/com.example.irisdetection I/MLKit: 0,80832106 2019-08-17 17:19:18.801 19647-19647/ com.example.irisdetection I/MLKit: 0,82105815 2019-08-17 17:19:18.802 19647-19647/com.example.irisdetection I/MLKit: 0,81131995 2019-08-17 17:19:18.805 19647- 19647/com.example.irisdetection I/MLKit: 0,79208875 2019-08-17 17:19:18.807 19647-19647/com.example.irisdetection I/MLKit: 0,76439416 2019-08-17 17:19:18.809 19647-19647/com.example.irisdetection I/MLKit: 0,72983956 2019-08-17 17:19:18.810 19647-19647/com.example.irisdetection I/MLKit: 0,69351745example.irisdetection I/MLKit: 0,75426823 2019-08-17 17:19:18.798 19647-19647/com.example.irisdetection I/MLKit: 0,78634208 2019-08-17 17:19:18.799 19647-19647/ com.example.irisdetection I/MLKit: 0,80832106 2019-08-17 17:19:18.801 19647-19647/com.example.irisdetection I/MLKit: 0,82105815 2019-08-17 17:19:18.802 19647- 19647/com.example.irisdetection I/MLKit: 0,81131995 2019-08-17 17:19:18.805 19647-19647/com.example.irisdetection I/MLKit: 0,79208875 2019-08-17 17:19:18.807 19647-19647/com.example.irisdetection I/MLKit: 0,76439416 2019-08-17 17:19:18.809 19647-19647/com.example.irisdetection I/MLKit: 0,72983956 2019-08-17 17:19 :18.810 19647-19647/com.example.irisdetection I/MLKit: 0,69351745example.irisdetection I/MLKit: 0,75426823 2019-08-17 17:19:18.798 19647-19647/com.example.irisdetection I/MLKit: 0,78634208 2019-08-17 17:19:18.799 19647-19647/ com.example.irisdetection I/MLKit: 0,80832106 2019-08-17 17:19:18.801 19647-19647/com.example.irisdetection I/MLKit: 0,82105815 2019-08-17 17:19:18.802 19647- 19647/com.example.irisdetection I/MLKit: 0,81131995 2019-08-17 17:19:18.805 19647-19647/com.example.irisdetection I/MLKit: 0,79208875 2019-08-17 17:19:18.807 19647-19647/com.example.irisdetection I/MLKit: 0,76439416 2019-08-17 17:19:18.809 19647-19647/com.example.irisdetection I/MLKit: 0,72983956 2019-08-17 17:19 :18.810 19647-19647/com.example.irisdetection I/MLKit: 0,69351745799 19647-19647/com.example.irisdetection I/MLKit: 0,80832106 2019-08-17 17:19:18.801 19647-19647/com.example.irisdetection I/MLKit: 0,82105815 2019-08-17 17: 19:18.802 19647-19647/com.example.irisdetection I/MLKit: 0,81131995 2019-08-17 17:19:18.805 19647-19647/com.example.irisdetection I/MLKit: 0,79208875 2019-08-17 17:19:18.807 19647-19647/com.example.irisdetection I/MLKit: 0,76439416 2019-08-17 17:19:18.809 19647-19647/com.example.irisdetection I/MLKit: 0,72983956 2019-08 -17 17:19:18.810 19647-19647/com.example.irisdetection I/MLKit:0,69351745799 19647-19647/com.example.irisdetection I/MLKit: 0,80832106 2019-08-17 17:19:18.801 19647-19647/com.example.irisdetection I/MLKit: 0,82105815 2019-08-17 17: 19:18.802 19647-19647/com.example.irisdetection I/MLKit: 0,81131995 2019-08-17 17:19:18.805 19647-19647/com.example.irisdetection I/MLKit: 0,79208875 2019-08-17 17:19:18.807 19647-19647/com.example.irisdetection I/MLKit: 0,76439416 2019-08-17 17:19:18.809 19647-19647/com.example.irisdetection I/MLKit: 0,72983956 2019-08 -17 17:19:18.810 19647-19647/com.example.irisdetection I/MLKit:0,69351745irisdetection I/MLKit:0,79208875 2019-08-17 17:19:18.807 19647-19647/com.example.irisdetection I/MLKit:0,76439416 2019-08-17 17:19:18.809 19647-19647/com。 example.irisdetection I/MLKit: 0,72983956 2019-08-17 17:19:18.810 19647-19647/com.example.irisdetection I/MLKit: 0,69351745irisdetection I/MLKit:0,79208875 2019-08-17 17:19:18.807 19647-19647/com.example.irisdetection I/MLKit:0,76439416 2019-08-17 17:19:18.809 19647-19647/com。 example.irisdetection I/MLKit: 0,72983956 2019-08-17 17:19:18.810 19647-19647/com.example.irisdetection I/MLKit: 0,69351745
解决方案
如果像素值的范围从0
到255
并且您希望它们在之间进行归一化0.0f
,1.0f
那么您应该将像素值除以 255。
所以你应该有
input[batchNum][x][y][0] = Color.red(pixel) / 255.0f;
input[batchNum][x][y][1] = Color.green(pixel) / 255.0f;
input[batchNum][x][y][2] = Color.blue(pixel) / 255.0f;
代替
input[batchNum][x][y][0] = (Color.red(pixel) - 127) / 128.0f;
input[batchNum][x][y][1] = (Color.green(pixel) - 127) / 128.0f;
input[batchNum][x][y][2] = (Color.blue(pixel) - 127) / 128.0f;
这是因为如果您的像素值处于最大值,255
那么如果您除以255.0f
结果将是1.0f
,如果您的像素值处于最小值,那么它等于0
如果您除以255.0f
您将得到0f
。
推荐阅读
- android - 图像(仅)不会出现在存储在 FireBase Storage 中的 RecyclerView 中,而出现其他项目
- sql-server - 如果不存在记录,则使用现有值或默认值插入
- c# - 用Mysql datareader多次读取一列的内容
- postgresql - 如何使用 WMS 图层限制在边界框中看到的要素数量?
- flink-batch - scala中的flink批处理(无输出)
- python - 按类别过滤
- angular - 无法在 Angular 中看到添加到 HTTP 请求的标头
- javascript - 使用画布线的 JavaScript 画布悬停
- python - 对象实例中的 Numpy 数组,dtype 错误
- node.js - Node.js - 使用 createReadStream 和 createWriteStream 时内存使用率高