Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading PNG files into TensorFlow

I'm trying to load custom made png files I generated to train my model. Following the instruction from TensorFlow guide here, I used this code:

import tensorflow as tf
import numpy as np
from pathlib import Path, WindowPath

train_df = pd.DataFrame(
    {'file_name': {0: WindowsPath('hypothesis/temp/81882f4e-0a94-4446-b4ac-7869cf198534.png'), 1: WindowsPath('hypothesis/temp/531162e2-2b4c-4e64-8b3f-1f285b0e1040.png')}, 'label': {0: -0.019687398020669655, 1: 0.0002379227226001479}}
)

file_path_list = [i.read_bytes() for i in train_df['file_name']]

dataset = tf.data.TFRecordDataset(filenames=file_path_list)

raw_example = next(iter(dataset))
parsed = tf.train.Example.FromString(raw_example.numpy())


Running the raw_example... line returns this error message:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 43: invalid start byte

I generated the PNG files using matplotlib.

like image 777
Mehdi Zare Avatar asked Sep 19 '25 01:09

Mehdi Zare


1 Answers

I suggest reading the png files with tensorflow's builtin io methods. The code snippet below will generate a list of files with .png extension and then iterate over them. During each iteration it reads the file and then decodes the png encoded image

image_dir = 'hypothesis/temp'
image_root = pathlib.Path(image_dir)
list_ds = tf.data.Dataset.list_files(str(image_root/'*.png'))
for f in list_ds:
  image = tf.io.read_file(f)
  image = tf.io.decode_png(image)
like image 68
DMolony Avatar answered Sep 20 '25 15:09

DMolony