本文整理汇总了Python中tensorflow.python.lib.io.file_io.file_exists函数的典型用法代码示例。如果您正苦于以下问题:Python file_exists函数的具体用法?Python file_exists怎么用?Python file_exists使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了file_exists函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _run_training_transform
def _run_training_transform(self):
"""Runs training starting with transformed tf.example files."""
cloud = True
if cloud:
cmd = ['gcloud ml-engine jobs submit training test_mltoolbox_train_%s' % uuid.uuid4().hex,
'--runtime-version=1.0',
'--scale-tier=STANDARD_1',
'--stream-logs']
else:
cmd = ['gcloud ml-engine local train']
cmd = cmd + [
'--module-name trainer.task',
'--job-dir=' + self._train_output,
'--package-path=' + os.path.join(CODE_PATH, 'trainer'),
'--',
'--train=' + os.path.join(self._transform_output, 'features_train*'),
'--eval=' + os.path.join(self._transform_output, 'features_eval*'),
'--analysis=' + self._analysis_output,
'--model=linear_regression',
'--train-batch-size=10',
'--eval-batch-size=10',
'--max-steps=' + str(self._max_steps)]
self._logger.debug('Running subprocess: %s \n\n' % ' '.join(cmd))
subprocess.check_call(' '.join(cmd), shell=True)
# Check the saved model was made.
self.assertTrue(file_io.file_exists(
os.path.join(self._train_output, 'model', 'saved_model.pb')))
self.assertTrue(file_io.file_exists(
os.path.join(self._train_output, 'evaluation_model', 'saved_model.pb')))
示例2: _save_and_write_assets
def _save_and_write_assets(self, assets_collection_to_add=None):
"""Saves asset to the meta graph and writes asset files to disk.
Args:
assets_collection_to_add: The collection where the asset paths are setup.
"""
asset_source_filepath_list = _maybe_save_assets(assets_collection_to_add)
# Return if there are no assets to write.
if len(asset_source_filepath_list) is 0:
tf_logging.info("No assets to write.")
return
assets_destination_dir = os.path.join(
compat.as_bytes(self._export_dir),
compat.as_bytes(constants.ASSETS_DIRECTORY))
if not file_io.file_exists(assets_destination_dir):
file_io.recursive_create_dir(assets_destination_dir)
# Copy each asset from source path to destination path.
for asset_source_filepath in asset_source_filepath_list:
asset_source_filename = os.path.basename(asset_source_filepath)
asset_destination_filepath = os.path.join(
compat.as_bytes(assets_destination_dir),
compat.as_bytes(asset_source_filename))
# Only copy the asset file to the destination if it does not already
# exist. This is to ensure that an asset with the same name defined as
# part of multiple graphs is only copied the first time.
if not file_io.file_exists(asset_destination_filepath):
file_io.copy(asset_source_filepath, asset_destination_filepath)
tf_logging.info("Assets written to: %s", assets_destination_dir)
示例3: test_relative_path
def test_relative_path(self):
m = keras.Model()
v = m.add_weight(name='v', shape=[])
os.chdir(self.get_temp_dir())
prefix = 'ackpt'
self.evaluate(v.assign(42.))
m.save_weights(prefix)
self.assertTrue(file_io.file_exists('ackpt.index'))
self.evaluate(v.assign(1.))
m.load_weights(prefix)
self.assertEqual(42., self.evaluate(v))
prefix = 'subdir/ackpt'
self.evaluate(v.assign(43.))
m.save_weights(prefix)
self.assertTrue(file_io.file_exists('subdir/ackpt.index'))
self.evaluate(v.assign(2.))
m.load_weights(prefix)
self.assertEqual(43., self.evaluate(v))
prefix = 'ackpt/'
self.evaluate(v.assign(44.))
m.save_weights(prefix)
self.assertTrue(file_io.file_exists('ackpt/.index'))
self.evaluate(v.assign(3.))
m.load_weights(prefix)
self.assertEqual(44., self.evaluate(v))
示例4: testRename
def testRename(self):
file_path = os.path.join(self._base_dir, "temp_file")
file_io.FileIO(file_path, mode="w").write("testing")
rename_path = os.path.join(self._base_dir, "rename_file")
file_io.rename(file_path, rename_path)
self.assertTrue(file_io.file_exists(rename_path))
self.assertFalse(file_io.file_exists(file_path))
示例5: testRename
def testRename(self):
file_path = os.path.join(self._base_dir, "temp_file")
file_io.write_string_to_file(file_path, "testing")
rename_path = os.path.join(self._base_dir, "rename_file")
file_io.rename(file_path, rename_path)
self.assertTrue(file_io.file_exists(rename_path))
self.assertFalse(file_io.file_exists(file_path))
示例6: _read_config_files
def _read_config_files(self, run_paths):
configs = {}
config_fpaths = {}
for run_name, logdir in run_paths.items():
config_fpath = os.path.join(logdir, PROJECTOR_FILENAME)
if not file_io.file_exists(config_fpath):
# Skip runs that have no config file.
continue
# Read the config file.
file_content = file_io.read_file_to_string(config_fpath).decode('utf-8')
config = ProjectorConfig()
text_format.Merge(file_content, config)
if not config.model_checkpoint_path:
# See if you can find a checkpoint file in the logdir.
ckpt_path = latest_checkpoint(logdir)
if not ckpt_path:
# Or in the parent of logdir.
ckpt_path = latest_checkpoint(os.path.join('../', logdir))
if not ckpt_path:
logging.warning('Cannot find model checkpoint in %s', logdir)
continue
config.model_checkpoint_path = ckpt_path
# Sanity check for the checkpoint file.
if not file_io.file_exists(config.model_checkpoint_path):
logging.warning('Checkpoint file %s not found',
config.model_checkpoint_path)
continue
configs[run_name] = config
config_fpaths[run_name] = config_fpath
return configs, config_fpaths
示例7: testCreateRecursiveDir
def testCreateRecursiveDir(self):
dir_path = os.path.join(self._base_dir, "temp_dir/temp_dir1/temp_dir2")
file_io.recursive_create_dir(dir_path)
file_path = os.path.join(dir_path, "temp_file")
file_io.FileIO(file_path, mode="w").write("testing")
self.assertTrue(file_io.file_exists(file_path))
file_io.delete_recursively(os.path.join(self._base_dir, "temp_dir"))
self.assertFalse(file_io.file_exists(file_path))
示例8: testRenameOverwriteFalse
def testRenameOverwriteFalse(self):
file_path = os.path.join(self._base_dir, "temp_file")
file_io.FileIO(file_path, mode="w").write("testing")
rename_path = os.path.join(self._base_dir, "rename_file")
file_io.FileIO(rename_path, mode="w").write("rename")
with self.assertRaises(errors.AlreadyExistsError):
file_io.rename(file_path, rename_path, overwrite=False)
self.assertTrue(file_io.file_exists(rename_path))
self.assertTrue(file_io.file_exists(file_path))
示例9: testRenameOverwrite
def testRenameOverwrite(self):
file_path = os.path.join(self.get_temp_dir(), "temp_file")
file_io.write_string_to_file(file_path, "testing")
rename_path = os.path.join(self.get_temp_dir(), "rename_file")
file_io.write_string_to_file(rename_path, "rename")
file_io.rename(file_path, rename_path, overwrite=True)
self.assertTrue(file_io.file_exists(rename_path))
self.assertFalse(file_io.file_exists(file_path))
file_io.delete_file(rename_path)
示例10: testRenameOverwriteFalse
def testRenameOverwriteFalse(self):
file_path = os.path.join(self.get_temp_dir(), "temp_file")
file_io.write_string_to_file(file_path, "testing")
rename_path = os.path.join(self.get_temp_dir(), "rename_file")
file_io.write_string_to_file(rename_path, "rename")
with self.assertRaises(errors.AlreadyExistsError):
file_io.rename(file_path, rename_path, overwrite=False)
self.assertTrue(file_io.file_exists(rename_path))
self.assertTrue(file_io.file_exists(file_path))
file_io.delete_file(rename_path)
file_io.delete_file(file_path)
示例11: save_model
def save_model(model, saved_model_path):
"""Save a `tf.keras.Model` into Tensorflow SavedModel format.
`save_model` generates such files/folders under the `saved_model_path` folder:
1) an asset folder containing the json string of the model's
configuration(topology).
2) a checkpoint containing the model weights.
Note that subclassed models can not be saved via this function, unless you
provide an implementation for get_config() and from_config().
Also note that `tf.keras.optimizers.Optimizer` instances can not currently be
saved to checkpoints. Use optimizers from `tf.train`.
Args:
model: A `tf.keras.Model` to be saved.
saved_model_path: a string specifying the path to the SavedModel directory.
Raises:
NotImplementedError: If the passed in model is a subclassed model.
"""
if not model._is_graph_network:
raise NotImplementedError
# save model configuration as a json string under assets folder.
model_json = model.to_json()
assets_destination_dir = os.path.join(
compat.as_bytes(saved_model_path),
compat.as_bytes(constants.ASSETS_DIRECTORY))
if not file_io.file_exists(assets_destination_dir):
file_io.recursive_create_dir(assets_destination_dir)
model_json_filepath = os.path.join(
compat.as_bytes(assets_destination_dir),
compat.as_bytes(constants.SAVED_MODEL_FILENAME_JSON))
file_io.write_string_to_file(model_json_filepath, model_json)
# save model weights in checkpoint format.
checkpoint_destination_dir = os.path.join(
compat.as_bytes(saved_model_path),
compat.as_bytes(constants.VARIABLES_DIRECTORY))
if not file_io.file_exists(checkpoint_destination_dir):
file_io.recursive_create_dir(checkpoint_destination_dir)
checkpoint_prefix = os.path.join(
compat.as_text(checkpoint_destination_dir),
compat.as_text(constants.VARIABLES_FILENAME))
model.save_weights(checkpoint_prefix, save_format='tf', overwrite=True)
示例12: testCopy
def testCopy(self):
file_path = os.path.join(self._base_dir, "temp_file")
file_io.FileIO(file_path, mode="w").write("testing")
copy_path = os.path.join(self._base_dir, "copy_file")
file_io.copy(file_path, copy_path)
self.assertTrue(file_io.file_exists(copy_path))
self.assertEqual(b"testing", file_io.read_file_to_string(file_path))
示例13: testAssets
def testAssets(self):
export_dir = self._get_export_dir("test_assets")
builder = saved_model_builder.SavedModelBuilder(export_dir)
with self.test_session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
# Build an asset collection.
ignored_filepath = os.path.join(
compat.as_bytes(test.get_temp_dir()), compat.as_bytes("ignored.txt"))
file_io.write_string_to_file(ignored_filepath, "will be ignored")
asset_collection = self._build_asset_collection("hello42.txt",
"foo bar baz",
"asset_file_tensor")
builder.add_meta_graph_and_variables(
sess, ["foo"], assets_collection=asset_collection)
# Save the SavedModel to disk.
builder.save()
with self.test_session(graph=ops.Graph()) as sess:
foo_graph = loader.load(sess, ["foo"], export_dir)
self._validate_asset_collection(export_dir, foo_graph.collection_def,
"hello42.txt", "foo bar baz",
"asset_file_tensor:0")
ignored_asset_path = os.path.join(
compat.as_bytes(export_dir),
compat.as_bytes(constants.ASSETS_DIRECTORY),
compat.as_bytes("ignored.txt"))
self.assertFalse(file_io.file_exists(ignored_asset_path))
示例14: cloud_batch_predict
def cloud_batch_predict(training_dir, prediction_input_file, output_dir, mode, batch_size,
shard_files, output_format):
"""See batch_predict"""
# from . import predict as predict_module
from .prediction import predict as predict_module
if mode == 'evaluation':
model_dir = os.path.join(training_dir, 'evaluation_model')
elif mode == 'prediction':
model_dir = os.path.join(training_dir, 'model')
else:
raise ValueError('mode must be evaluation or prediction')
if not file_io.file_exists(model_dir):
raise ValueError('Model folder %s does not exist' % model_dir)
_assert_gcs_files([training_dir, prediction_input_file, output_dir])
cmd = ['predict.py',
'--cloud',
'--project-id=%s' % _default_project(),
'--predict-data=%s' % prediction_input_file,
'--trained-model-dir=%s' % model_dir,
'--output-dir=%s' % output_dir,
'--output-format=%s' % output_format,
'--batch-size=%s' % str(batch_size),
'--shard-files' if shard_files else '--no-shard-files',
'--extra-package=%s' % _TF_GS_URL,
'--extra-package=%s' % _PROTOBUF_GS_URL,
'--extra-package=%s' % _package_to_staging(output_dir)
]
return predict_module.main(cmd)
示例15: _save_and_write_assets
def _save_and_write_assets(self, assets_collection_to_add=None):
"""Saves asset to the meta graph and writes asset files to disk.
Args:
assets_collection_to_add: The collection where the asset paths are setup.
"""
asset_filename_map = _maybe_save_assets(assets_collection_to_add)
# Return if there are no assets to write.
if not asset_filename_map:
tf_logging.info("No assets to write.")
return
assets_destination_dir = saved_model_utils.get_or_create_assets_dir(
self._export_dir)
# Copy each asset from source path to destination path.
for asset_basename, asset_source_filepath in asset_filename_map.items():
asset_destination_filepath = os.path.join(
compat.as_bytes(assets_destination_dir),
compat.as_bytes(asset_basename))
# Only copy the asset file to the destination if it does not already
# exist. This is to ensure that an asset with the same name defined as
# part of multiple graphs is only copied the first time.
if not file_io.file_exists(asset_destination_filepath):
file_io.copy(asset_source_filepath, asset_destination_filepath)
tf_logging.info("Assets written to: %s",
compat.as_text(assets_destination_dir))