Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

tensorflow - TFLiteConverter representative_dataset from keras.preprocessing.image_dataset_from_directory dataset

I've got a dataset coming in via

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=validation_split,
  subset="training",
  seed=seed,
  image_size=(img_height, img_width),
  batch_size=batch_size)

(Based around code from https://www.tensorflow.org/tutorials/load_data/images with very minor changes to configuration)

I'm converting the eventual model to a TFLite model, which is working, but I think the model's too large for the end device so I'm trying to run post training quantization by supplying a representative_dataset (like https://www.tensorflow.org/lite/performance/post_training_quantization)

However I can't work out how to turn the dataset generated from image_dataset_from_directory into the format expected by representative_dataset

The example provided has

def representative_dataset():
  for data in tf.data.Dataset.from_tensor_slices((images)).batch(1).take(100):
    yield [data.astype(tf.float32)]

I've tried things like

def representative_dataset():
  for data in train_ds.batch(1).take(100):
    yield [data.astype(tf.float32)]

but that wasn't it

question from:https://stackoverflow.com/questions/65902185/tfliteconverter-representative-dataset-from-keras-preprocessing-image-dataset-fr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like

def representative_dataset():
  for image_batch, labels_batch in train_ds:
    yield [image_batch]

Was what I was looking for, image_batch is already tf.float32


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...