Creation of Tensor by using Image File is used as an input . Following listed parameters used in creation of Tensor.
Where
C is number of Channels.
3 is for RGB
1 is Black and white channel
W is width of image
H is height of image
N is number of images in a given Batch.
def load_image(filename):
raw = tf.io.read_file(filename)
image = tf.image.decode_png(raw, channels=3)
# the `print` executes during tracing.
print("Initial shape: ", image.shape)
image.set_shape([28, 28, 3])
print("Final shape: ", image.shape)
return image
tf.image.resize(image, [224,224]) is used to transform image from one shape to another shape . Image resize may be required required to use Pre trained Model.
import tensorflow as tf
# load image via tf.io
img = tf.io.read_file("golf.png")
# convert to tensor (specify 3 channels explicitly
# since png files contains additional alpha channel)
# set the dtypes to align with pytorch for
# comparison since it will use uint8 by default
tensor = tf.io.decode_image(img, channels=3, dtype=tf.dtypes.float32)
# (384, 470, 3)
# resize tensor to 224 x 224
tensor = tf.image.resize(tensor, [224, 224])
# (224, 224, 3)
# add another dimension at the front to get NHWC shape
input_tensor = tf.expand_dims(tensor, axis=0)
# (1, 224, 224, 3)
tensor = tf.transpose(tensor, perm=[2, 0, 1])