-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proper way of making a data generator which can handle multiple workers #1638
Comments
Hi there, I have encountered this rather uninformative error message multiple times, and here are some of the possible root causes:
Hope this helps! |
When I make The function |
I found that making a data generator threadsafe works (of course, you should first check that your data generator has no other errors and they are solely arising from running it on two workers). The detailed procedure is given in this link and that is what I have followed. For completeness, the original code snippet can be modified as follows. The code included only between
It works with Python 2.7 and latest Theano. However, if I apply the same trick on the
You can simply the above function as:
This doesn't work. As far as this issue is concerned, I am closing it. |
Right. It is specified in the documentation that using a number of workers On 10 February 2016 at 13:01, parag2489 notifications@github.com wrote:
|
@parag2489 thanks for sharing! |
@parag2489 could u tell me why 32*1875, I cannot figure out the meanings of the parameter? thanks a lot,same problems with u |
@wjbaibai There are a total of 60,000 examples, and since he's using a batch size of 32 images, that makes 60,000/32 = 1875 iterations per epoch. |
You have to write a loop which will continuously fetch the data in the batches of 32. Training set of MNIST has 60000 examples. Also, 32*1875 = 60000 (or 60000/32 = 1875). So, the following piece of code will give you a chunk of 32 examples at a time.
|
The comments and suggestions in this issue and its cousin #1627 were very helpful for me to efficiently process large numbers of images. I wrote it all up in a tutorial fashion that I hope can help others. |
A python2/3 compatible version of the decorator that @parag2489 posted:
|
@remiresnap It looks like your snippet lacks
|
Does making it threadsafe with the above decorator(s) actually result in any speed up, though? I'm finding that it is the same speed (or maybe even a little slower) with my generator. Which makes sense because it looks like it's locking every time it does anything... |
From my experience, if your data generator takes very little time to
prepare and yield the batch, then multithreading is not very effective.
In other words, if the overhead of distributing the batches to multiple
workers is more than the time being saved in preprocessing by parallelizing
that operation, then I did not see much improvement.
…Sent from my iPhone
On Nov 28, 2017, at 1:56 AM, Kevin Rose <notifications@github.com> wrote:
Does making it threadsafe with the above decorator(s) actually result in
any speed up, though? I'm finding that it is the same speed (or maybe even
a little slower) with my generator. Which makes sense because it looks like
it's locking every time it does *anything*...
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1638 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AGBWb1kfR_iVA5woHL6YnRerNya9Eh8xks5s6xrmgaJpZM4HTKEJ>
.
|
@parag2489
in code this would look something like that:
My guess is that I need a generator wrapper. |
In my case, if the batch size is really large and the data augmentation take really long, even use multithread, the thread safe generator solution is not fast enough because. The lock in the thread safe generator will block the generator from yielding the next batch of data. So technically it's not multithreading anything. Sequence class is a better choice cus it's using thread pool and it's real multithreading. |
Hello how are you? I apologize for the inconvenience. Could you help me. I'm trying to create my own generator from the above comments. However, when I apply model.fit_generator, I realize that my network does not use batch_size. For example, if I have 32676 images and batch_size of 64, I should realize 510 iterations per epoch. But my network has 32676 iterations per epoch. My dataset is large and with two channel images, so I need to create my own generator. I can not use the commands ImageDataGenerator, flow_from_directory and model.fit_generator direct from keras, because my images have two channels and these commands only work with 1 and 3 channel images. Would it be possible for you to help me? I also did a generator for validation. That's why I use validationGenerator (). I send my own generator to you:
I thank you for your attention, |
- Keras requires a thread-safe generator - See keras-team/keras#1638
- Keras requires a thread-safe generator - See keras-team/keras#1638
- Keras requires a thread-safe generator - See keras-team/keras#1638
@ysyyork what strategy did you end up adopting? I have the same issue and I'm seeing that ADDING workers increases the time to process the same number of images. sample profile
when using workers > 1 and multiprocessing = F on a Sequence object. Looks to me its doing alot of waiting. |
Isn't Python's Global Interpreter Lock (GIL) the real culprit here? |
I am having difficulty in writing a data generator which can work with multiple workers. My data generator works fine with one worker, but with > 1 workers, it gives me the following error:
I have tried many things such as declaring
X_train, X_test, y_train, y_test
as global. I also tried wrapping themyGenerator()
function into a Python mutex. Those solutions didn't work.My system: Ubuntu 14.04, Python 2.7, Tesla K40 GPU
A script and its sample out to reproduce the issue is given below:
Sample output:
P.S. I searched about this issue but the above issue is not similar to most other ones such as this.
The text was updated successfully, but these errors were encountered: