Tensorflow Datasets Reshape Images

2024/11/14 12:42:23

I want to build a data pipeline using tensorflow dataset. Because each data has different shapes, I can't build a data pipeline.

import tensorflow_datasets as tfds
import tensorflow as tfdataset_builder = tfds.builder("oxford_flowers102")
dataset_builder.download_and_prepare()train_data = dataset_builder.as_dataset(split=tfds.Split.TRAIN)
train_data = train_data.repeat().batch(32)
train_data = train_data.prefetch(tf.data.experimental.AUTOTUNE)
train_iterator = train_data.make_one_shot_iterator()
train_next_element = train_iterator.get_next()with tf.Session() as sess:train_batch = sess.run(train_next_element)

Above code gives me the error:

"tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot batch tensors with different shapes in component 1. First element had shape [500,666,3] and element 1 had shape [752,500,3]."

I want all images to be in the shape of [224,224,3]. How can I reshape images in the existing tensorflow dataset?

Answer

You can dynamically resize the images like:

train_data = train_data.map(lambda image: tf.image.resize_image_with_crop_or_pad(image, 224, 224))

right before doing train_data = train_data.repeat().batch(32). Also, using the tf.data.Dataset.map(...) method you can apply a variety of transformations on your images before batching them.

https://en.xdnf.cn/q/72486.html

Related Q&A

Why is the python client not receiving SSE events?

I am have a python client listening to SSE events from a server with node.js APIThe flow is I sent an event to the node.js API through call_notification.py and run seevents.py in loop using run.sh(see …

sklearn Pipeline: argument of type ColumnTransformer is not iterable

I am attempting to use a pipeline to feed an ensemble voting classifier as I want the ensemble learner to use models that train on different feature sets. For this purpose, I followed the tutorial avai…

PyQT Window: I want to remember the location it was closed at

I have a QDialog, and when the user closes the QDialog, and reopens it later, I want to remember the location and open the window at the exact same spot. How would I exactly remember that location?

Django Reusable Application Configuration

I have some Django middleware code that connects to a database. I want to turn the middleware into a reusable application ("app") so I can package it for distribution into many other project…

executable made with py2exe doesnt run on windows xp 32bit

I created an executable with py2exe on a 64bit windows 7 machine, and distributed the program.On a windows xp 32bit machine the program refuses to run exhibiting the following behavior:a popup window s…

Pandas reading NULL as a NaN float instead of str [duplicate]

This question already has answers here:How to treat NULL as a normal string with pandas?(4 answers)Closed 5 years ago.Given the file:$ cat test.csv a,b,c,NULL,d e,f,g,h,i j,k,l,m,nWhere the 3rd colum…

How to invert differencing in a Python statsmodels ARIMA forecast?

Im trying to wrap my head around ARIMA forecasting using Python and Statsmodels. Specifically, for the ARIMA algorithm to work, the data needs to be made stationary via differencing (or similar method)…

how to see the content of a particular file in .tar.gz archive without unzipping the contents?

for ex abc.tar.gz has abc/file1.txt abc/file2.txt abc/abc1/file3.txt abc/abc2/file4.txt i need to read/display the contents of file3.txt without extracting the file.Thanks for any input.

Matplotlib animation not showing

When I try this on my computer at home, it works, but not on my computer at work. Heres the codeimport numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation import sys im…

Extracting Fields Names of an HTML form - Python

Assume that there is a link "http://www.someHTMLPageWithTwoForms.com" which is basically a HTML page having two forms (say Form 1 and Form 2). I have a code like this ...import httplib2 from …