【问题标题】:How to normalize channel values to the range[0.0,1.0]?如何将通道值标准化为范围 [0.0,1.0]?
【发布时间】:2019-12-23 12:36:17
【问题描述】:

我是 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.695578​​63 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,69351745

【问题讨论】:

    标签: android image firebase tensorflow colors


    【解决方案1】:

    如果像素值的范围从0255,并且您希望它们在0.0f1.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

    【讨论】:

    • 谢谢,但这对我不起作用。我有一个 python 脚本,它从 TensorFlow Lite 模型中获取预测,其中图像(用于在 Android 上测试预测的相同图像)作为输入,数组作为输出。 python脚本的数组(输出)的值与java代码(Android Studio)完全不同,请参阅我的帖子的更新
    • 如果你使用我提供的这些计算,你不会得到介于 0.0 和 1.0 之间的值吗?
    • 价值观是什么?你的意思是 input[batchNum][x][y][0] 的值吗?
    • 您的问题是如何将通道值标准化为 [0.0, 1.0] 范围。您当前在问题中提供的代码是将值标准化为 [-1.0, 1.0] 范围 (input[batchNum][x][y][0], input[batchNum][x][y][1], input[batchNum ][x][y][2])。所以我的答案是将您当前的计算更改为我在答案中提供的计算。这些值是否不在想要的范围内,或者其他东西对您不起作用?
    • 我使用 float32 而不是 float 64bit,这是我使用的代码(来自 firebase MLKit)private FirebaseModelInputOutputOptions createInputOutputOptions() throws FirebaseMLException { FirebaseModelInputOutputOptions inputOutputOptions = new FirebaseModelInputOutputOptions.Builder() .setInputFormat(0, FirebaseModelDataType.FLOAT32, new int[]{1, 112, 112, 3}) .setOutputFormat(0, FirebaseModelDataType.FLOAT32, new int[]{1, 80}) .build(); return inputOutputOptions; }
    猜你喜欢
    • 1970-01-01
    • 2019-08-12
    • 2018-06-15
    • 2016-01-07
    • 1970-01-01
    • 2019-03-02
    • 2016-10-31
    • 2016-08-19
    • 2014-02-01
    相关资源
    最近更新 更多