TensorFlow はローカル画像をロードします

 TensorFlow バージョン: 2.5.0

# 方式一
import tensorflow as tf

img_path = 'cat.jpg'
img = tf.io.read_file(img_path) # 返回整个输入图像的Tensor,没有任何解析,是输入管道的第一步。
img = tf.io.decode_jpeg(img, channels=3) # 将JPEG编码的图像解码为uint8 tensor.
img = tf.image.resize(img, [224, 224]) # 调整图像大小(可以指定方法)type:float32 tensor
img = tf.expand_dims(img, axis=0) # 增加维度 type:float32 tensor

# 方式二

from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array, array_to_img

IMG_DIM = (224, 224)
sample_img_path = 'benign_test.png'
sample_img = load_img(sample_img_path, target_size=IMG_DIM)
sample_img_tensor = img_to_array(sample_img)
sample_img_tensor = np.expand_dims(sample_img_tensor, axis=0)
sample_img_tensor /= 255. # 像素归一化

1.tf.expand_dims():

 

2. 画像正規化: /=255. および /=127.5 - 1

 /=255.画像を正規化します。範囲は [0, 1]、/=127.5 - 1画像を正規化します。範囲は [-1, 1]、これら 2 つは正規化範囲が異なるだけです。直観的に 2 つの違いがあるため、画像は 2 つの方法で処理されます。

 次の図に示すように、2 つの方法のヒストグラム分布は同じであり、/=127.5 - 1 の分布は比較的まばらです。

 

 参考ブログ:

https://blog.csdn.net/qq_20014079/article/details/82804374

https://blog.csdn.net/wind82465/article/details/108711150

https://blog.csdn.net/Forrest97/article/details/105895591

おすすめ

転載: blog.csdn.net/qq_40108803/article/details/118712351