tensorflow:Your input ran out of data when using custom generator

2024/10/10 20:19:07

I am using custom generator to pass my data. But i keep encountering an error which says i have run out of data and to use repeat() when passing the dataset. i am using plain generator therefore it is not possible to use repeat(). can someone help me to fix this issue

import os
import numpy as np
import cv2
def generator(idir,odir,batch_size,shuffle ):i_list=os.listdir(idir)o_list=os.listdir(odir)batch_index=0batch_size = batch_sizesample_count=len(i_list)while True:input_image_batch=[]output_image_batch=[]for i in range(batch_index * batch_size, (batch_index + 1) * batch_size  ): #iterate for  a batchj=i % sample_count # cycle j value over range of available  imagesk=j % batch_size  # cycle k value over batch sizeif shuffle == True: # if shuffle select a random integer between 0 and sample_count-1 to pick as the image=label pairm=np.random.randint(low=0, high=sample_count-1, size=None, dtype=int) else:m=jpath_to_in_img=os.path.join(idir,i_list[m])path_to_out_img=os.path.join(odir,o_list[m])print(path_to_in_img,path_to_out_img)input_image=cv2.imread(path_to_in_img)input_image=cv2.resize(input_image,(3200,3200))#create the target image from the input image output_image=cv2.imread(path_to_out_img)output_image=cv2.resize(output_image,(3200,3200))input_image_batch.append(input_image)output_image_batch.append(output_image)input_val1image_array=np.array(input_image_batch) input_val1image_array = input_val1image_array / 255.0print (input_val1image_array)output_val2image_array=np.array(output_image_batch)output_val2image_array = output_val2image_array / 255.0batch_index= batch_index + 1 yield (input_val1image_array, output_val2image_array)if batch_index * batch_size > sample_count:break

Calling the function

    idir = r"D:\\image\\"odir=r"D:\\image1\\"train = generator(idir,odir,4,True)model.compile(optimizer="adam", loss='mean_squared_error', metrics=['mean_squared_error'])model.fit(train,validation_data = (valin_images,valout_images),batch_size= 5,epochs = 20,steps_per_epoch = int(560/batch_size))

The error

Epoch 1/20
186/186 [==============================] - 475s 3s/step - loss: 1779.7604 - mean_squared_error: 1779.7601 - val_loss: 28278.5488 - val_mean_squared_error: 28278.5488
Epoch 2/201/186 [..............................] - ETA: 1:41 - loss: 275.7113 - mean_squared_error: 275.7113WARNING:tensorflow:Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches (in this case, 3720 batches). You may need to use the repeat() function when building your dataset.
WARNING:tensorflow:Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches (in this case, 187 batches). You may need to use the repeat() function when building your dataset.
186/186 [==============================] - 1s 235us/step - loss: 275.7113 - mean_squared_error: 275.7113
Answer

If you aren't using repeat (and even if you are it's good for debugging) the first thing you need to check is how many elements your generator creates. a quick way to do that would be with something like

len([g for g in generator(idir,odir,4,True)])

Then you need to make sure that your steps per epoch times your batch size is less than that number.

You can use repeat even with that generator though - you just need to wrap it with a tf.dataset like this:

gen = lambda : generator(idir,odir,4,True)
dataset = tf.data.Dataset.from_generator(gen, output_types=(<types>), output_shapes=(<shapes>)).repeat()

you have to specify output type and shape but then it works fine.

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

Related Q&A

script to get the max from column based on other column values

I need a script to read in a csv file(orig.csv) and output a reformatted csv file(format.csv) The orig csv file will look like this: Time,Label,frame,slot,SSN,Board,BT,SRN,LabelFrame,SRNAME,LabelID,Int…

Selenium code is not able to scrape ofashion.com.cn

I was building a web scraper by using python selenium. The script scraped sites like amazon, stack overflow and flipcart but wasnt able to scrape ofashion. It is always returning me a blank .csv file.H…

How can I access each estimater in scikit-learn pipelines? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

How do I structure a repo with Cloud Run needing higher level code?

I have added code to a repo to build a Cloud Run service. The structure is like this:I want to run b.py in cr. Is there any way I can deploy cr without just copying b.py into the cr directory? (I dont…

Unable to build kivy image loaded .py file into exe using auto-py-to-exe

I have a simple kivy file in which i want to cover the entire canvas with an image bgi.jpg MainWidget: <MainWidget>:canvas.before:Rectangle:size:self.sizesource:bgi.jpgand the .py file code i…

Pandas Panel is deprecated,

This code snippet is from one of my script which works fine in current panda version (0.23) but Panel is deprecated and will be removed in a future version.panel = pd.Panel(dict(df1=dataframe1,df2=data…

Python - Why is this data being written to file incorrectly?

Only the first result is being written to a csv, with one letter of the url per row. This is instead of all urls being written, one per row.What am I not doing right in the last section of this code t…

How does Python interpreter look for types? [duplicate]

This question already has answers here:How does Python interpreter work in dynamic typing?(3 answers)Closed 10 months ago.If I write something like:>>> a = float()how does Python interpreter …

title() method in python writing functions when word like arent

using functiondef make_cap(sentence):return sentence.title()tryining outmake_cap("hello world") Hello World# it workd but when I have world like "arent" and isnt". how to write…

Creating a C++ Qt Gui for a Python logic

I was presented with a Python logic for which I need to create a GUI. I want to use Qt for that purpose and ideally I would like to program it in C++, without using the Qt Creator.What are recommended …