-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Project import generated by Copybara.
PiperOrigin-RevId: 363220025
- Loading branch information
Showing
18 changed files
with
346 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Simple csv reader for small classification problems.""" | ||
|
||
from model_search.data import data | ||
import tensorflow.compat.v2 as tf | ||
|
||
|
||
class Provider(data.Provider): | ||
"""A csv data provider.""" | ||
|
||
def __init__(self, input_dir, image_width, image_height, eval_fraction): | ||
self._input_dir = input_dir | ||
self._image_width = image_width | ||
self._image_height = image_height | ||
self._eval_fraction = eval_fraction | ||
|
||
def get_input_fn(self, hparams, mode, batch_size): | ||
"""See `data.Provider` get_input_fn.""" | ||
del hparams | ||
|
||
def input_fn(params=None): | ||
del params | ||
split = ('training' | ||
if mode == tf.estimator.ModeKeys.TRAIN else 'validation') | ||
|
||
dataset = tf.keras.preprocessing.image_dataset_from_directory( | ||
directory=self._input_dir, | ||
labels='inferred', | ||
label_mode='binary', | ||
class_names=None, | ||
color_mode='rgb', | ||
batch_size=batch_size, | ||
image_size=(self._image_height, self._image_width), | ||
shuffle=True, | ||
seed=73, | ||
validation_split=self._eval_fraction, | ||
subset=split, | ||
interpolation='bilinear', | ||
follow_links=False) | ||
|
||
if mode == tf.estimator.ModeKeys.TRAIN: | ||
dataset = dataset.cache().prefetch( | ||
buffer_size=tf.data.experimental.AUTOTUNE) | ||
|
||
return dataset | ||
|
||
return input_fn | ||
|
||
def get_serving_input_fn(self, hparams): | ||
"""Returns an `input_fn` for serving in an exported SavedModel. | ||
Args: | ||
hparams: tf.HParams object. | ||
Returns: | ||
Returns an `input_fn` that takes no arguments and returns a | ||
`ServingInputReceiver`. | ||
""" | ||
tf.compat.v1.disable_eager_execution() | ||
features = { | ||
'image': | ||
tf.compat.v1.placeholder( | ||
tf.float32, [None, self._image_height, self._image_width, 3], | ||
'image') | ||
} | ||
return tf.estimator.export.build_raw_serving_input_receiver_fn( | ||
features=features) | ||
|
||
def number_of_classes(self): | ||
return 2 | ||
|
||
def get_feature_columns(self): | ||
"""Returns feature columns.""" | ||
feature_columns = [ | ||
tf.feature_column.numeric_column( | ||
key='image', shape=(self._image_height, self._image_width, 3)) | ||
] | ||
return feature_columns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Simple csv reader for small classification problems.""" | ||
|
||
from absl import flags | ||
|
||
from model_search.data import data | ||
import tensorflow.compat.v2 as tf | ||
|
||
flags.DEFINE_string( | ||
'input_dir', '', 'The path containing the input data. Should provide a dir ' | ||
'that has 0 and 1 as subdirs.') | ||
|
||
flags.DEFINE_float('eval_fraction', 0.2, | ||
'The amount of data (fraction) to hold for evaluation.') | ||
|
||
flags.DEFINE_integer('image_height', 320, | ||
'The height (dimension) of the image.') | ||
|
||
flags.DEFINE_integer('image_width', 240, 'The width (dimension) of the image.') | ||
|
||
FLAGS = flags.FLAGS | ||
|
||
|
||
@data.register_provider(lookup_name='image_data_provider', init_args={}) | ||
class Provider(data.Provider): | ||
"""A csv data provider.""" | ||
|
||
def __init__(self): | ||
self._input_dir = FLAGS.input_dir | ||
self._image_width = FLAGS.image_width | ||
self._image_height = FLAGS.image_height | ||
self._eval_fraction = FLAGS.eval_fraction | ||
|
||
if '${TEST_SRCDIR}' in self._input_dir: | ||
self._input_dir = self._input_dir.replace('${TEST_SRCDIR}', | ||
FLAGS.test_srcdir) | ||
|
||
def get_input_fn(self, hparams, mode, batch_size): | ||
"""See `data.Provider` get_input_fn.""" | ||
del hparams | ||
|
||
def input_fn(params=None): | ||
del params | ||
split = ('training' | ||
if mode == tf.estimator.ModeKeys.TRAIN else 'validation') | ||
|
||
dataset = tf.keras.preprocessing.image_dataset_from_directory( | ||
directory=self._input_dir, | ||
labels='inferred', | ||
label_mode='binary', | ||
class_names=None, | ||
color_mode='rgb', | ||
batch_size=batch_size, | ||
image_size=(self._image_height, self._image_width), | ||
shuffle=True, | ||
seed=73, | ||
validation_split=self._eval_fraction, | ||
subset=split, | ||
interpolation='bilinear', | ||
follow_links=False) | ||
|
||
if mode == tf.estimator.ModeKeys.TRAIN: | ||
dataset = dataset.cache().prefetch( | ||
buffer_size=tf.data.experimental.AUTOTUNE) | ||
|
||
return dataset | ||
|
||
return input_fn | ||
|
||
def get_serving_input_fn(self, hparams): | ||
"""Returns an `input_fn` for serving in an exported SavedModel. | ||
Args: | ||
hparams: tf.HParams object. | ||
Returns: | ||
Returns an `input_fn` that takes no arguments and returns a | ||
`ServingInputReceiver`. | ||
""" | ||
tf.compat.v1.disable_eager_execution() | ||
features = { | ||
'image': | ||
tf.compat.v1.placeholder( | ||
tf.float32, [None, self._image_height, self._image_width, 3], | ||
'image') | ||
} | ||
return tf.estimator.export.build_raw_serving_input_receiver_fn( | ||
features=features) | ||
|
||
def number_of_classes(self): | ||
return 2 | ||
|
||
def get_feature_columns(self): | ||
"""Returns feature columns.""" | ||
feature_columns = [ | ||
tf.feature_column.numeric_column( | ||
key='image', shape=(self._image_height, self._image_width, 3)) | ||
] | ||
return feature_columns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Tests for model_search.single_trainer.""" | ||
|
||
import os | ||
from absl import flags | ||
from absl.testing import absltest | ||
from model_search import constants | ||
from model_search import single_trainer | ||
from model_search.data import image_data | ||
|
||
FLAGS = flags.FLAGS | ||
|
||
|
||
class SingleTrainerTest(absltest.TestCase): | ||
|
||
def test_try_models(self): | ||
# Test is source code is deployed in FLAGS.test_srcdir | ||
spec_path = os.path.join(FLAGS.test_srcdir, constants.DEFAULT_CNN) | ||
trainer = single_trainer.SingleTrainer( | ||
data=image_data.Provider( | ||
input_dir=os.path.join( | ||
FLAGS.test_srcdir, | ||
"model_search/model_search/data/testdata/images"), | ||
image_width=100, | ||
image_height=100, | ||
eval_fraction=0.2), | ||
spec=spec_path) | ||
|
||
trainer.try_models( | ||
number_models=7, | ||
train_steps=10, | ||
eval_steps=10, | ||
root_dir=FLAGS.test_tmpdir, | ||
batch_size=2, | ||
experiment_name="test", | ||
experiment_owner="test") | ||
|
||
|
||
if __name__ == "__main__": | ||
absltest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,10 @@ filegroup( | |
"csv_random_data.csv", | ||
], | ||
) | ||
|
||
filegroup( | ||
name = "image_data", | ||
srcs = glob([ | ||
"**/*.png", | ||
]), | ||
) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.