根據檔案tf.image.convert_image_dtype“使用浮點值表示的影像應具有 [0,1) 范圍內的值。”
但在 keras 教程(https://keras.io/examples/vision/cutmix/)中,我看到了以下預處理功能:
def preprocess_image(image, label):
image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
image = tf.image.convert_image_dtype(image, tf.float32) / 255.0
return image, label
我的問題是:為什么他們除以 255,什么時候tf.image.convert_image_dtype已經完成了這項作業?
uj5u.com熱心網友回復:
當convert_image_dtype(image, tf.float32)僅在影像中使用數字型別時,轉換為float32并且不放置 [0,1),但是當您除以時,255.0您將數字移動到 [0,1),我們這樣做是為了Convolutional Layers.
import tensorflow_datasets as tfds
import tensorflow as tf
dataset = tfds.load('cifar10', as_supervised=True, split='train').batch(1)
for image, label in dataset.take(1):
print(image[0])
IMG_SIZE = 64
def preprocess_image(image, label):
image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
image = tf.image.convert_image_dtype(image, tf.float32) / 255.0
# or
# image = tf.cast(image, tf.float32) / 255.0
return image, label
dataset = dataset.map(preprocess_image)
for image, label in dataset.take(1):
print(image[0])
輸出:
tf.Tensor(
[[[143 96 70]
[141 96 72]
[135 93 72]
...
[212 177 147]
[219 185 155]
[221 187 157]]], shape=(32, 32, 3), dtype=uint8)
tf.Tensor(
[[[0.56078434 0.3764706 0.27450982]
[0.5588235 0.3764706 0.2764706 ]
[0.55490196 0.3764706 0.28039217]
...
[0.8607843 0.72745097 0.6098039 ]
[0.86470586 0.73137254 0.6137255 ]
[0.8666667 0.73333335 0.6156863 ]]], shape=(64, 64, 3), dtype=float32)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/450725.html
上一篇:張量流中具有復指數的自定義梯度
