Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
bob.learn.tensorflow
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
10
Issues
10
List
Boards
Labels
Milestones
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bob
bob.learn.tensorflow
Commits
70e0b851
Commit
70e0b851
authored
Jun 28, 2019
by
Amir MOHAMMADI
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow shuffle on epoch end in generator
parent
6aabd230
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
16 deletions
+23
-16
bob/learn/tensorflow/dataset/generator.py
bob/learn/tensorflow/dataset/generator.py
+23
-16
No files found.
bob/learn/tensorflow/dataset/generator.py
View file @
70e0b851
import
six
import
tensorflow
as
tf
import
random
import
logging
logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -22,18 +23,21 @@ class Generator:
which takes a sample and loads it.
samples : [:obj:`object`]
A list of samples to be given to ``reader`` to load the data.
shuffle_on_epoch_end : :obj:`bool`, optional
If True, it shuffle the samples at the end of each epoch.
output_types : (object, object, object)
The types of the returned samples.
output_shapes : ``(tf.TensorShape, tf.TensorShape, tf.TensorShape)``
The shapes of the returned samples.
"""
def
__init__
(
self
,
samples
,
reader
,
multiple_samples
=
False
,
**
kwargs
):
def
__init__
(
self
,
samples
,
reader
,
multiple_samples
=
False
,
shuffle_on_epoch_end
=
False
,
**
kwargs
):
super
().
__init__
(
**
kwargs
)
self
.
reader
=
reader
self
.
samples
=
list
(
samples
)
self
.
multiple_samples
=
multiple_samples
self
.
epoch
=
0
self
.
shuffle_on_epoch_end
=
shuffle_on_epoch_end
# load one data to get its type and shape
dlk
=
self
.
reader
(
self
.
samples
[
0
])
...
...
@@ -81,31 +85,34 @@ class Generator:
yield
dlk
self
.
epoch
+=
1
logger
.
info
(
"Elapsed %d epoch(s)"
,
self
.
epoch
)
if
self
.
shuffle_on_epoch_end
:
logger
.
info
(
"Shuffling samples"
)
random
.
shuffle
(
self
.
samples
)
def
dataset_using_generator
(
*
args
,
**
kwargs
):
def
dataset_using_generator
(
samples
,
reader
,
**
kwargs
):
"""
A generator class which wraps samples so that they can
be used with tf.data.Dataset.from_generator
Attribute
s
Parameter
s
----------
samples : [:obj:`object`]
A list of samples to be given to ``reader`` to load the data.
reader : :obj:`object`, optional
A callable with the signature of ``data, label, key = reader(sample)``
which takes a sample and loads it.
**kwargs
Extra keyword arguments are passed to Generator
multiple_samples : :obj:`bool`, optional
If true, it assumes that the bio database's samples actually contain
multiple samples. This is useful for when you want to for example treat
video databases as image databases.
Returns
-------
object
A tf.data.Dataset
"""
generator
=
Generator
(
*
args
,
**
kwargs
)
generator
=
Generator
(
samples
,
reader
,
**
kwargs
)
dataset
=
tf
.
data
.
Dataset
.
from_generator
(
generator
,
generator
.
output_types
,
generator
.
output_shapes
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment