Tensorflow segmentation fault in dataset generation - python

I'm running batch-wise predictions on a pre-trained model. The error is raised during the dataset creation process, specifically in a tf.data.Dataset.map() function.
This is the offending function-
paths_dataset = tf.data.Dataset.from_tensor_slices(file_list)
# #tf.autograph.experimental.do_not_convert
def path_to_wav(path):
audio_bytes = tf.io.read_file(path)
wav, sr = tf.audio.decode_wav(audio_bytes, desired_channels=1, desired_samples=example_sample_length)
wav = tfio.audio.resample(wav, rate_in=tf.cast(sr, dtype=tf.int64), rate_out=resample_rate)
return tf.transpose(wav, perm=[1, 0])
wavs_dataset = paths_dataset.map(path_to_wav)
It has been tough to debug because the script works fine in a debugger and in a notebook. It only throws an error if I run the script via terminal or send the job to a cluster node-
WARNING:tensorflow:AutoGraph could not transform <function resample at 0x2b0dc6249160> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: std::bad_cast
To silence this warning, decorate the function with #tf.autograph.experimental.do_not_convert
srun: error: node416: task 0: Segmentation fault
Adding the #tf.autograph.experimental.do_not_convert decorator suppresses the warning but throws the following error-
File "/home/s/ss645/mlos/train/estimates_dataset.py", line 120, in simple_dataset
wavs_dataset = paths_dataset.map(path_to_wav)
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 2048, in map
return MapDataset(self, map_func, preserve_cardinality=True, name=name)
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 5243, in __init__
self._map_func = structured_function.StructuredFunctionWrapper(
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/data/ops/structured_function.py", line 271, in __init__
self._function = fn_factory()
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2567, in get_concrete_function
graph_function = self._get_concrete_function_garbage_collected(
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2533, in _get_concrete_function_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2711, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2627, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 1141, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/data/ops/structured_function.py", line 248, in wrapped_fn
ret = wrapper_helper(*args)
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/data/ops/structured_function.py", line 177, in wrapper_helper
ret = autograph.tf_convert(self._func, ag_ctx)(*nested_args)
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py", line 642, in wrapper
return func(*args, **kwargs)
File "/home/s/ss645/mlos/train/estimates_dataset.py", line 115, in path_to_wav
wav = tfio.audio.resample(wav, rate_in=sr, rate_out=resample_rate)
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow_io/python/ops/audio_ops.py", line 462, in resample
value = tf.vectorized_map(f, input)
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/ops/parallel_for/control_flow_ops.py", line 549, in vectorized_map
return pfor(loop_fn, batch_size,
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/ops/parallel_for/control_flow_ops.py", line 206, in pfor
outputs = f()
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/ops/parallel_for/control_flow_ops.py", line 187, in f
return _pfor_impl(loop_fn,
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/ops/parallel_for/control_flow_ops.py", line 286, in _pfor_impl
loop_fn_outputs = f(loop_var)
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py", line 642, in wrapper
return func(*args, **kwargs)
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/ops/parallel_for/control_flow_ops.py", line 530, in loop_fn
return fn(gathered_elems)
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow_io/python/ops/audio_ops.py", line 458, in f
return core_ops.io_audio_resample(
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow_io/python/ops/__init__.py", line 88, in __getattr__
return getattr(self._load(), attrb)
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow_io/python/ops/__init__.py", line 84, in _load
self._mod = _load_library(self._library)
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow_io/python/ops/__init__.py", line 64, in _load_library
l = load_fn(f)
File "/home/s/ss645/miniconda3/envs/fsdm/lib/python3.8/site-packages/tensorflow/python/framework/load_library.py", line 54, in load_op_library
lib_handle = py_tf.TF_LoadLibrary(library_filename)
RuntimeError: std::bad_cast
srun: error: node650: task 0: Exited with exit code 1
Any help would be much appreciated! I'm currently unable to file a Tensorflow issue because the error is not reproducible in a notebook.

Related

Dreambooth - RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0

I'm trying to train a model via Dreambooth and I'm running into this problem. I've looked for solutions but none of them seem to work. I read adding ".to(device)" to variables helps but I'm not sure where to add them. I was wondering if anyone could tell where to add them by the error below, or if anyone had suggestions on how to fix it. I'm a beginner, so please bear with me.
Traceback (most recent call last):
File "main.py", line 835, in <module>
trainer.fit(model, data)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 741, in fit
self._call_and_handle_interrupt(
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 686, in _call_and_handle_interrupt
return trainer_fn(*args, **kwargs)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 778, in _fit_impl
self._run(model, ckpt_path=ckpt_path)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 1200, in _run
self._dispatch()
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 1280, in _dispatch
self.training_type_plugin.start_training(self)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\plugins\training_type\training_type_plugin.py", line 202, in start_training
self._results = trainer.run_stage()
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 1290, in run_stage
return self._run_train()
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 1312, in _run_train
self._run_sanity_check(self.lightning_module)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 1376, in _run_sanity_check
self._evaluation_loop.run()
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\loops\base.py", line 145, in run
self.advance(*args, **kwargs)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\loops\dataloader\evaluation_loop.py", line 110, in advance
dl_outputs = self.epoch_loop.run(dataloader, dataloader_idx, dl_max_batches, self.num_dataloaders)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\loops\base.py", line 145, in run
self.advance(*args, **kwargs)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\loops\epoch\evaluation_epoch_loop.py", line 122, in advance
output = self._evaluation_step(batch, batch_idx, dataloader_idx)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\loops\epoch\evaluation_epoch_loop.py", line 217, in _evaluation_step
output = self.trainer.accelerator.validation_step(step_kwargs)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\accelerators\accelerator.py", line 236, in validation_step
return self.training_type_plugin.validation_step(*step_kwargs.values())
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\pytorch_lightning\plugins\training_type\training_type_plugin.py", line 219, in validation_step
return self.model.validation_step(*args, **kwargs)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\torch\autograd\grad_mode.py", line 28, in decorate_context
return func(*args, **kwargs)
File "C:\Dreambooth-SD-optimized\ldm\models\diffusion\ddpm.py", line 368, in validation_step
_, loss_dict_no_ema = self.shared_step(batch)
File "C:\Dreambooth-SD-optimized\ldm\models\diffusion\ddpm.py", line 908, in shared_step
loss = self(x, c)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Dreambooth-SD-optimized\ldm\models\diffusion\ddpm.py", line 937, in forward
c = self.get_learned_conditioning(c)
File "C:\Dreambooth-SD-optimized\ldm\models\diffusion\ddpm.py", line 595, in get_learned_conditioning
c = self.cond_stage_model.encode(c, embedding_manager=self.embedding_manager)
File "C:\Dreambooth-SD-optimized\ldm\modules\encoders\modules.py", line 324, in encode
return self(text, **kwargs)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Dreambooth-SD-optimized\ldm\modules\encoders\modules.py", line 319, in forward
z = self.transformer(input_ids=tokens, **kwargs)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Dreambooth-SD-optimized\ldm\modules\encoders\modules.py", line 297, in transformer_forward
return self.text_model(
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Dreambooth-SD-optimized\ldm\modules\encoders\modules.py", line 258, in text_encoder_forward
hidden_states = self.embeddings(input_ids=input_ids, position_ids=position_ids, embedding_manager=embedding_manager)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Dreambooth-SD-optimized\ldm\modules\encoders\modules.py", line 180, in embedding_forward
inputs_embeds = self.token_embedding(input_ids)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\torch\nn\modules\sparse.py", line 158, in forward
return F.embedding(
File "C:\Users\User\miniconda3\envs\ldm\lib\site-packages\torch\nn\functional.py", line 2044, in embedding
return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument index in method wrapper__index_select)
Any help would be appreciated!
there are a whole bunch of functions in SD that WILL NOT work if you're running --lowvram/--medvram and several of the other command line args. tensors on multiple devices means due to your settings you're at least partially co-processing on your cpu. to test which setting, if you're not sure, don't waste time in dreambooth to troubleshoot, use the textual inversion trainer. it'll trigger the same crash for the same reasons. i have to disable --medvram just to create an embedding, so use that as your troubleshooter. try to create an embedding, change settings, repeat, till you find all the options triggering the cpu/cuda error

Issue TypeError: argument must be a string or number

There is only one categorical column and I want to encode it, it is working fine on notebook but when it is being uploaded to aicrowd platform it is creating this trouble.
There are totally 3 categorical features where one is the target feature, one is the row of ids and after excluding them for the training I am left with one feature.
df[['intersection_pos_rel_centre']]
from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
df[['intersection_pos_rel_centre']]=le.fit_transform(df[['intersection_pos_rel_centre']])
df[['intersection_pos_rel_centre']]
My error is
Selecting runtime language: python
[NbConvertApp] Converting notebook predict.ipynb to notebook
[NbConvertApp] Executing notebook with kernel: python
Traceback (most recent call last):
File "/opt/conda/bin/jupyter-nbconvert", line 11, in <module>
sys.exit(main())
File "/opt/conda/lib/python3.8/site-packages/jupyter_core/application.py", line 254, in launch_instance
return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
File "/opt/conda/lib/python3.8/site-packages/traitlets/config/application.py", line 845, in launch_instance
app.start()
File "/opt/conda/lib/python3.8/site-packages/nbconvert/nbconvertapp.py", line 350, in start
self.convert_notebooks()
File "/opt/conda/lib/python3.8/site-packages/nbconvert/nbconvertapp.py", line 524, in convert_notebooks
self.convert_single_notebook(notebook_filename)
File "/opt/conda/lib/python3.8/site-packages/nbconvert/nbconvertapp.py", line 489, in convert_single_notebook
output, resources = self.export_single_notebook(notebook_filename, resources, input_buffer=input_buffer)
File "/opt/conda/lib/python3.8/site-packages/nbconvert/nbconvertapp.py", line 418, in export_single_notebook
output, resources = self.exporter.from_filename(notebook_filename, resources=resources)
File "/opt/conda/lib/python3.8/site-packages/nbconvert/exporters/exporter.py", line 181, in from_filename
return self.from_file(f, resources=resources, **kw)
File "/opt/conda/lib/python3.8/site-packages/nbconvert/exporters/exporter.py", line 199, in from_file
return self.from_notebook_node(nbformat.read(file_stream, as_version=4), resources=resources, **kw)
File "/opt/conda/lib/python3.8/site-packages/nbconvert/exporters/notebook.py", line 32, in from_notebook_node
nb_copy, resources = super().from_notebook_node(nb, resources, **kw)
File "/opt/conda/lib/python3.8/site-packages/nbconvert/exporters/exporter.py", line 143, in from_notebook_node
nb_copy, resources = self._preprocess(nb_copy, resources)
File "/opt/conda/lib/python3.8/site-packages/nbconvert/exporters/exporter.py", line 318, in _preprocess
nbc, resc = preprocessor(nbc, resc)
File "/opt/conda/lib/python3.8/site-packages/nbconvert/preprocessors/base.py", line 47, in __call__
return self.preprocess(nb, resources)
File "/opt/conda/lib/python3.8/site-packages/nbconvert/preprocessors/execute.py", line 79, in preprocess
self.execute()
File "/opt/conda/lib/python3.8/site-packages/nbclient/util.py", line 74, in wrapped
return just_run(coro(*args, **kwargs))
File "/opt/conda/lib/python3.8/site-packages/nbclient/util.py", line 53, in just_run
return loop.run_until_complete(coro)
File "/opt/conda/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "/opt/conda/lib/python3.8/site-packages/nbclient/client.py", line 553, in async_execute
await self.async_execute_cell(
File "/opt/conda/lib/python3.8/site-packages/nbconvert/preprocessors/execute.py", line 123, in async_execute_cell
cell, resources = self.preprocess_cell(cell, self.resources, cell_index)
File "/opt/conda/lib/python3.8/site-packages/nbconvert/preprocessors/execute.py", line 146, in preprocess_cell
cell = run_sync(NotebookClient.async_execute_cell)(self, cell, index, store_history=self.store_history)
File "/opt/conda/lib/python3.8/site-packages/nbclient/util.py", line 74, in wrapped
return just_run(coro(*args, **kwargs))
File "/opt/conda/lib/python3.8/site-packages/nbclient/util.py", line 53, in just_run
return loop.run_until_complete(coro)
File "/opt/conda/lib/python3.8/site-packages/nest_asyncio.py", line 98, in run_until_complete
return f.result()
File "/opt/conda/lib/python3.8/asyncio/futures.py", line 178, in result
raise self._exception
File "/opt/conda/lib/python3.8/asyncio/tasks.py", line 280, in __step
result = coro.send(None)
File "/opt/conda/lib/python3.8/site-packages/nbclient/client.py", line 852, in async_execute_cell
self._check_raise_for_error(cell, exec_reply)
File "/opt/conda/lib/python3.8/site-packages/nbclient/client.py", line 760, in _check_raise_for_error
raise CellExecutionError.from_cell_and_msg(cell, exec_reply_content)
nbclient.exceptions.CellExecutionError: An error occurred while executing the following cell:
------------------
df[['intersection_pos_rel_centre']]
from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
df[['intersection_pos_rel_centre']]=le.fit_transform(df[['intersection_pos_rel_centre']])
df[['intersection_pos_rel_centre']]
------------------
TypeError: argument must be a string or number

How to load a model with tf.keras?

import tensorflow as tf Tensorflow 2.0
i saw that i can load a model from tensorflow like this
image_model = tf.keras.applications.MobileNet(include_top=True, weights='imagenet', pooling='avg')
Now i want to be able to load models from local machine. My issue is that i can not find an pretrained model that works like this:
image_model = tf.keras.models.load_model('inception_v4.h5') (i used h5 from here https://github.com/titu1994/Inception-v4/releases?fbclid=IwAR0pK_CZaB9RwA92nvawNOha6DjY5xI0vtkc9Ff5HTATcFT9x5vGYBUXt5Q (first h5 model))
future: <Task finished coro=<server_task.<locals>.server_work() done,
defined at ....\x.py:249> exception=ValueError('No model found in config file.')>
Traceback (most recent call last):
File "....\x.py", line 280, in server_work
image_model, layers_indices = init(model_choice, layers_to_see)
File "....\x.py", line 146, in init
image_model = options[choice]()
#tf.keras.applications.MobileNetV2(include_top=True, weights='imagenet',
pooling='avg')
File "....\x.py", line 119, in model_H5_model
image_model = tf.keras.models.load_model('..../inception_v4.h5')
File "...\Python\Python37\lib\site-
packages\tensorflow_core\python\keras\saving\save.py", line 146, in
load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
File "...\AppData\Local\Programs\Python\Python37\lib\site-
packages\tensorflow_core\python\keras\saving\hdf5_format.py", line 165, in load_model_from_hdf5
raise ValueError('No model found in config file.')
ValueError: No model found in config file.
I also tried with a model like this
image_model = tf.keras.models.load_model('model.pb')
File "....\x.py", line 280, in server_work
image_model, layers_indices = init(model_choice, layers_to_see)
File "....\x.py", line 146, in init
image_model = options[choice]()
#tf.keras.applications.MobileNetV2(include_top=True, weights='imagenet',
pooling='avg')
File "....\x.py", line 119, in model_H5_model
image_model = tf.keras.models.load_model('.../model/inceptionv4.pb')
File "...\AppData\Local\Programs\Python\Python37\lib\site-
packages\tensorflow_core\python\keras\saving\save.py", line 149, in
load_model
loader_impl.parse_saved_model(filepath)
File "...\AppData\Local\Programs\Python\Python37\lib\site-
packages\tensorflow_core\python\saved_model\loader_impl.py", line 83, in
parse_saved_model
constants.SAVED_MODEL_FILENAME_PB))
OSError: SavedModel file does not exist at:
.../model/inceptionv4.pb/{saved_model.pbtxt|saved_model.pb}
What i also tried was smth like this:
image_model = tf.keras.applications.MobileNet(include_top=True,
weights='imagenet', pooling='avg')
image_model.save('test') - > when trying to save i receive this error
File "\Python\Python37\lib\site-
packages\tensorflow_core\python\framework\func_graph.py", line 905, in
wrapper
raise e.ag_error_metadata.to_exception(e)
TypeError: in converted code:
relative to ...\Programs\Python\Python37\lib\site-packages:
tensorflow_core\python\eager\def_function.py:606 initialize_variables *
for v, init in initializer_map.items():
tensorflow_core\python\autograph\impl\api.py:438 converted_call
if not options.user_requested and
conversion.is_whitelisted_for_graph(f):
m = tf_inspect.getmodule(o)
tensorflow_core\python\util\tf_inspect.py:337 getmodule
return _inspect.getmodule(object)
pycallgraph\tracer.py:372 wrapper
if rest not in cache:
TypeError: unhashable type: 'ObjectIdentityDictionary'
tf.keras.models.load_model('test_model')
I am wondering where i can find a h5 file or pb (pretrained model) that actually works with tf.keras.models.load_model()
Based on the first comment :
future: <Task finished coro=<server_task.<locals>.server_work() done, defined at c:\Users\...\Desktop\PrivateStuff\...\...\xx.py:249> exception=TypeError("in converted code:\n relative to C:\\Users\\...\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages:\n\n tensorflow_core\\python\\eager\\def_function.py:606 initialize_variables *\n for v, init in initializer_map.items():\n tensorflow_core\\python\\autograph\\impl\\api.py:438 converted_call\n if not options.user_requested and conversion.is_whitelisted_for_graph(f):\n tensorflow_core\\python\\autograph\\impl\\conversion.py:352 is_whitelisted_for_graph\n m = tf_inspect.getmodule(o)\n tensorflow_core\\python\\util\\tf_inspect.py:337 getmodule\n return _inspect.getmodule(object)\n pycallgraph\\tracer.py:372 wrapper\n if rest not in cache:\n\n TypeError: unhashable type: 'ObjectIdentityDictionary'\n")>
Traceback (most recent call last):
File "c:\Users\...\Desktop\PrivateStuff\...\...\xx.py", line 280, in server_work
image_model, layers_indices = init(model_choice, layers_to_see)
File "c:\Users\...\Desktop\PrivateStuff\...\...\xx.py", line 146, in init
image_model = options[choice]() #tf.keras.applications.MobileNetV2(include_top=True, weights='imagenet', pooling='avg')
File "c:\Users\...\Desktop\PrivateStuff\...\...\xx.py", line 55, in model_VGG16
image_model.save(r'c:\test')
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 975, in save
signatures, options)
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\saving\save.py", line 115, in save_model
signatures, options)
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\saving\saved_model\save.py", line 74, in save
save_lib.save(model, filepath, signatures, options)
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\saved_model\save.py", line 870, in save
checkpoint_graph_view)
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\saved_model\signature_serialization.py", line 64, in find_function_to_export
functions = saveable_view.list_functions(saveable_view.root)
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\saved_model\save.py", line 141, in list_functions
self._serialization_cache)
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 2422, in _list_functions_for_serialization
.list_functions_for_serialization(serialization_cache))
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\saving\saved_model\base_serialization.py", line 91, in list_functions_for_serialization
fns = self.functions_to_serialize(serialization_cache)
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\saving\saved_model\layer_serialization.py", line 79, in
functions_to_serialize
serialization_cache).functions_to_serialize)
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\saving\saved_model\layer_serialization.py", line 94, in
_get_serialized_attributes
serialization_cache)
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\saving\saved_model\model_serialization.py", line 47, in
_get_serialized_attributes_internal
default_signature = save_impl.default_save_signature(self.obj)
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\saving\saved_model\save_impl.py", line 206, in default_save_signature
fn.get_concrete_function()
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 777, in get_concrete_function
self._initialize_uninitialized_variables(initializer_map)
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 616, in _initialize_uninitialized_variables
return initialize_variables.get_concrete_function()()
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\eager\function.py", line 1891, in get_concrete_function
graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\eager\function.py", line 2150, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\eager\function.py", line 2041, in _create_graph_function
capture_by_value=self._capture_by_value),
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\func_graph.py", line 915, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\func_graph.py", line 905, in wrapper
raise e.ag_error_metadata.to_exception(e)
TypeError: in converted code:
relative to C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages:
tensorflow_core\python\eager\def_function.py:606 initialize_variables *
for v, init in initializer_map.items():
tensorflow_core\python\autograph\impl\api.py:438 converted_call
if not options.user_requested and conversion.is_whitelisted_for_graph(f):
tensorflow_core\python\autograph\impl\conversion.py:352 is_whitelisted_for_graph
m = tf_inspect.getmodule(o)
tensorflow_core\python\util\tf_inspect.py:337 getmodule
return _inspect.getmodule(object)
pycallgraph\tracer.py:372 wrapper
if rest not in cache:
TypeError: unhashable type: 'ObjectIdentityDictionary'
I copied your code to load MobileNet. It works if your provide a full path to save the model. See code below. Note when you load a model with weights='imagenet' the weights are set for the model trained on the imagenet data set. You don't need to load any weights. Now if you want to load weights for the model pre-trained on some other data set first instantiate the model as shown below. Then load the specific weights using model.load_weights.
image_model = tf.keras.applications.MobileNet(include_top=True,
weights='imagenet', pooling='avg')
image_model.save(r'c:\test')

get_flat_tensor_specs nest.flatten(element_spec), []) results in AttributeError: 'list' object has no attribute '_flat_tensor_specs'

AttributeError: 'list' object has no attribute '_flatten_tensor_specs'
problem:
Currently, when creating an iterative process with a build federated averaging process, I am able to pass in a functools. partial successful.
however, there must be an error in the above code that produces a structure needed for the model_fn to go through
correctly.
have tried:
looking at the input data. This custom data set is built for a autoencoder and the original TF solution never used labels. As this solution is built to take in the training data, train the model, then work on the test data, and validate on the validation set to produce a threshold.
There might be a issue with the underlying production.
sources:
df, y_train = get_train_data(sysarg)
x_train, x_opt, x_test = np.split(df.sample(frac=1,
random_state=17),
[int(1 / 3 * len(df)), int(2 / 3 * len(df))])
x_train, x_opt, x_test = create_scalar(x_opt, x_test, x_train)
input_spec = tf.nest.map_structure(tf.TensorSpec.from_tensor,
[tf.convert_to_tensor(x_train),
tf.convert_to_tensor(y_train)])
assign_weights_fn = compression_process_adapter.CompressionServerState.assign_weights_to_keras_model
iterative_process = tff.learning.build_federated_averaging_process(
model_fn=functools.partial(model_builder,
input_dim=sysarg, input_spec=input_spec),
client_optimizer_fn=client_optimizer_fn,
server_optimizer_fn=server_optimizer_fn,
)
iterative_process = compression_process_adapter.CompressionProcessAdapter(iterative_process)
trackback error
Traceback (most recent call last):
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-47998fd56829>", line 1, in <module>
runfile('B:/projects/openProjects/githubprojects/BotnetTrafficAnalysisFederaedLearning/anomaly-detection/train_v04.py', args=['--experiment_name=temp', '--client_batch_size=20', '--client_optimizer=sgd', '--client_learning_rate=0.2', '--server_optimizer=sgd', '--server_learning_rate=1.0', '--total_rounds=200', '--rounds_per_eval=1', '--rounds_per_checkpoint=50', '--rounds_per_profile=0', '--root_output_dir=B:/projects/openProjects/githubprojects/BotnetTrafficAnalysisFederaedLearning/anomaly-detection/logs/fed_out/'], wdir='B:/projects/openProjects/githubprojects/BotnetTrafficAnalysisFederaedLearning/anomaly-detection')
File "B:\tools and software\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "B:\tools and software\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "B:/projects/openProjects/githubprojects/BotnetTrafficAnalysisFederaedLearning/anomaly-detection/train_v04.py", line 306, in <module>
app.run(main)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\absl\app.py", line 299, in run
_run_main(main, args)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\absl\app.py", line 250, in _run_main
sys.exit(main(argv))
File "B:/projects/openProjects/githubprojects/BotnetTrafficAnalysisFederaedLearning/anomaly-detection/train_v04.py", line 299, in main
train_main()
File "B:/projects/openProjects/githubprojects/BotnetTrafficAnalysisFederaedLearning/anomaly-detection/train_v04.py", line 262, in train_main
server_optimizer_fn=server_optimizer_fn,
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow_federated\python\learning\federated_averaging.py", line 211, in build_federated_averaging_process
stateful_delta_aggregate_fn, stateful_model_broadcast_fn)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow_federated\python\learning\framework\optimizer_utils.py", line 521, in build_model_delta_optimizer_process
model_broadcast_state_type=model_broadcast_state_type)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow_federated\python\learning\framework\optimizer_utils.py", line 368, in _build_one_round_computation
#tff.tf_computation(dataset_type, model_weights_type)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow_federated\python\core\impl\wrappers\computation_wrapper.py", line 337, in <lambda>
return lambda fn: _wrap(fn, arg_type, self._wrapper_fn)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow_federated\python\core\impl\wrappers\computation_wrapper.py", line 89, in _wrap
concrete_fn = wrapper_fn(fn, parameter_type, unpack=None)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow_federated\python\core\impl\wrappers\computation_wrapper_instances.py", line 51, in _tf_wrapper_fn
target_fn, parameter_type, ctx_stack)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow_federated\python\core\impl\tensorflow_serialization.py", line 274, in serialize_py_fn_as_tf_computation
result = target(*args)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow_federated\python\core\impl\utils\function_utils.py", line 517, in <lambda>
return lambda arg: _unpack_and_call(fn, arg_types, kwarg_types, arg)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow_federated\python\core\impl\utils\function_utils.py", line 510, in _unpack_and_call
return fn(*args, **kwargs)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow_federated\python\learning\framework\optimizer_utils.py", line 381, in _compute_local_training_and_client_delta
client_output = client_delta_fn(dataset, initial_model_weights)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow\python\eager\def_function.py", line 580, in __call__
result = self._call(*args, **kwds)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow\python\eager\def_function.py", line 627, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow\python\eager\def_function.py", line 506, in _initialize
*args, **kwds))
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow\python\eager\function.py", line 2446, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow\python\eager\function.py", line 2777, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow\python\eager\function.py", line 2667, in _create_graph_function
capture_by_value=self._capture_by_value),
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow\python\framework\func_graph.py", line 981, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow\python\eager\def_function.py", line 441, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow\python\eager\function.py", line 3299, in bound_method_wrapper
return wrapped_fn(*args, **kwargs)
File "B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow\python\framework\func_graph.py", line 968, in wrapper
raise e.ag_error_metadata.to_exception(e)
Additional Error list:
AttributeError: in user code:
B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow_federated\python\learning\federated_averaging.py:90 __call__ *
num_examples_sum = dataset.reduce(
B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py:1932 reduce **
add_to_graph=False)
B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py:3210 __init__
self._input_structure),
B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow\python\data\util\structure.py:270 get_flat_tensor_specs
nest.flatten(element_spec), [])
B:\tools and software\Anaconda\envs\bookProjects\lib\site-packages\tensorflow\python\data\util\structure.py:269 <lambda>
return functools.reduce(lambda state, value: state + value._flat_tensor_specs,
AttributeError: 'list' object has no attribute '_flat_tensor_specs'
GitHub link

<Tensorflow object detection>TypeError: resize_images() got an unexpected keyword argument 'preserve_aspect_ratio'

cuda 9.0
cudnn 7.5
python 3.5.2
tensorflow-gpu 1.8
I don't know where the error occurred, I also tried python 3.6.3. This error will also occur. Please help.
I am training model_main.py file, but I get the following error.
python model_main.py --model_dir=F:/cindy/cindybackup/tensorflow1/test/training -pipeline_config_path=F:/cindy/cindybackup/tensorflow1/test/data/faster_rcnn_inception_v2_pets.config --alsologtostderr --num_train_steps=1000 --num_eval_steps=10
It shows the following:
WARNING:tensorflow:Forced number of epochs for all eval validations to
be 1.
WARNING:tensorflow:Expected number of evaluation epochs is 1, but instead encountered eval_on_train_input_config.num_epochs = 0.
Overwriting num_epochs to 1.
WARNING:tensorflow:Using temporary folder as model directory: C:\Users\wyh\AppData\Local\Temp\tmplh3q4jn2
WARNING:tensorflow:Estimator's model_fn (.model_fn at 0x00000256FF7F1400>) includes
params argument, but params are not passed to Estimator.
WARNING:tensorflow:num_readers has been reduced to 1 to match input file shards.
Traceback (most recent call last):
File "model_main.py", line 109, in
tf.app.run()
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\platform\app.py",
line 126, in run
_sys.exit(main(argv))
File "model_main.py", line 105, in main
tf.estimator.train_and_evaluate(estimator, train_spec, eval_specs[0])
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\estimator\training.py",
line 439, in train_and_evaluate
executor.run()
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\estimator\training.py",
line 518, in run
self.run_local()
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\estimator\training.py",
line 650, in run_local
hooks=train_hooks)
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\estimator\estimator.py",
line 363, in train
loss = self._train_model(input_fn, hooks, saving_listeners)
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\estimator\estimator.py",
line 843, in _train_model
return self._train_model_default(input_fn, hooks, saving_listeners)
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\estimator\estimator.py",
line 853, in _train_model_default
input_fn, model_fn_lib.ModeKeys.TRAIN))
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\estimator\estimator.py",
line 691, in _get_features_and_labels_from_input_fn
result = self._call_input_fn(input_fn, mode)
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\estimator\estimator.py",
line 798, in _call_input_fn
return input_fn(**kwargs)
File "F:\cindy\cindybackup\tensorflow1\models\research\object_detection\inputs.py",
line 525, in _train_input_fn
batch_size=params['batch_size'] if params else train_config.batch_size)
File "F:\cindy\cindybackup\tensorflow1\models\research\object_detection\builders\dataset_builder.py",
line 149, in build
dataset = data_map_fn(process_fn, num_parallel_calls=num_parallel_calls)
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py",
line 853, in map
return ParallelMapDataset(self, map_func, num_parallel_calls)
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py",
line 1870, in init
super(ParallelMapDataset, self).init(input_dataset, map_func)
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py",
line 1839, in init
self._map_func.add_to_graph(ops.get_default_graph())
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\framework\function.py",
line 484, in add_to_graph
self._create_definition_if_needed()
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\framework\function.py",
line 319, in _create_definition_if_needed
self._create_definition_if_needed_impl()
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\framework\function.py",
line 336, in _create_definition_if_needed_impl
outputs = self._func(*inputs)
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py",
line 1804, in tf_map_func
ret = map_func(nested_args)
File "F:\cindy\cindybackup\tensorflow1\models\research\object_detection\builders\dataset_builder.py",
line 130, in process_fn
processed_tensors = transform_input_data_fn(processed_tensors)
File "F:\cindy\cindybackup\tensorflow1\models\research\object_detection\inputs.py",
line 515, in transform_and_pad_input_data_fn
tensor_dict=transform_data_fn(tensor_dict),
File "F:\cindy\cindybackup\tensorflow1\models\research\object_detection\inputs.py",
line 129, in transform_input_data
tf.expand_dims(tf.to_float(image), axis=0))
File "F:\cindy\cindybackup\tensorflow1\models\research\object_detection\meta_architectures\faster_rcnn_meta_arch.py",
line 543, in preprocess
parallel_iterations=self._parallel_iterations)
File "F:\cindy\cindybackup\tensorflow1\models\research\object_detection\utils\shape_utils.py",
line 237, in static_or_dynamic_map_fn
outputs = [fn(arg) for arg in tf.unstack(elems)]
File "F:\cindy\cindybackup\tensorflow1\models\research\object_detection\utils\shape_utils.py",
line 237, in
outputs = [fn(arg) for arg in tf.unstack(elems)]
File "F:\cindy\cindybackup\tensorflow1\models\research\object_detection\core\preprocessor.py",
line 2264, in resize_to_range
lambda: _resize_portrait_image(image))
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\util\deprecation.py",
line 432, in new_func
return func(*args, **kwargs)
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\ops\control_flow_ops.py",
line 2063, in cond
orig_res_t, res_t = context_t.BuildCondBranch(true_fn)
File "C:\Users\wyh\AppData\Local\conda\conda\envs\py352\lib\site-packages\tensorflow\python\ops\control_flow_ops.py",
line 1913, in BuildCondBranch
original_result = fn()
File "F:\cindy\cindybackup\tensorflow1\models\research\object_detection\core\preprocessor.py",
line 2263, in
lambda: _resize_landscape_image(image),
File "F:\cindy\cindybackup\tensorflow1\models\research\object_detection\core\preprocessor.py",
line 2245, in _resize_landscape_image
align_corners=align_corners, preserve_aspect_ratio=True)
TypeError: resize_images() got an unexpected keyword argument 'preserve_aspect_ratio'
Thanks~
The problem is not resolved yet in tensorflow models(https://github.com/tensorflow/models/)
I just removed the preserve_aspect_ratio in object_detection/core/preprocessor.py
align_corners=align_corners, preserve_aspect_ratio=True)
align_corners=align_corners)

Categories

Resources