I'm working with computer vision project, where my images are combination of webp and jpeg. I'm using tensorflow '2.3.2'
You can think my directories like this :
IMAGES
|-img1.jpeg
|-img2.webp
For reading webp, I use tfio.image.decode_webp and when reading jpeg, I use tf.image.decode_jpeg(img, channels=3). Here's the code :
def load(file_path):
img = tf.io.read_file(file_path)
extension = tf.strings.split(file_path,sep=".")
if extension[-1] == "webp" :
img = tfio.image.decode_webp(img)
else :
img = tf.image.decode_jpeg(img, channels=3)
#img preprocess here
return img
def create_dataset(df,batch_size):
image = df["image_path"]
# I'm working on MultiTaskLearning so I have multiple targets
target1 = df["target1"].to_numpy()
target2 = df["target2"].to_numpy()
ds = tf.data.Dataset.from_tensor_slices((image,target1,target2))
ds = ds.map(lambda image, target1,target2: (load(image), {"target1":target1, "target2":target2}), num_parallel_calls=tf.data.experimental.AUTOTUNE)
ds = ds.batch(batch_size)
ds = ds.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
return ds
dataset = create_dataset(df,100)
The problem is, webp are converted to 4 channel(RGBA) tensor where decode jpeg is in 3 channel(RGB). This creates inconsistencies within my dataset since the model only except 3 channel images.
One solution I can think of is converting all my webp to jpeg through this. But is there any better solution for this? like converting the 4 channel into 3 channel in TensorFlow or reading webp as 3 channel in TensorFlow or anything else where I can just put the solution inside my python script?