matplotlib savefig IO error - matplotlib

I am trying to use the matplotlib.pyplot.savefig() function to save some figures.
I am saving them to a directory, however I keep getting the error:
matplotlib.pyplot.savefig(savepath,dpi=dpi,size=size)
File "C:\Anaconda\lib\site-packages\matplotlib\pyplot.py", line 577, in savefig
res = fig.savefig(*args, **kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\figure.py", line 1476, in savefig
self.canvas.print_figure(*args, **kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 161, in print_figure
FigureCanvasAgg.print_figure(self, *args, **kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\backend_bases.py", line 2211, in print_figure
**kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 526, in print_png
filename_or_obj = open(filename_or_obj, 'wb')
IOError: [Errno 2] No such file or directory:
Of course the file doesn't exist, as I am trying to save it now.
The directory does exist, I have checked repeatedly.
I am completely baffled, as this worked perfectly fine 2 days ago, but with no changes to the code it does not now. EDIT: I updated the version of the anaconda python distribution that I was using before, From 32 bit anaconda 2.0 to 64 bit 2.3, both for python 2.7.
Does anyone have any clue?
Thank you for reading my desperate plea for assistance!
EDIT:
I am also getting what I believe to be the same error from saving txt files in python now.
f = open(fname, 'w')
IOError: [Errno 2] No such file or directory: 'D:\\DropBox\\Dropbox\\abc\\Time resolved spectroscopy data\\LiHoF4\\High resolution 1cm\\power spectra\\Si\\RT\\25ns\\CUT POWER SPECTRUM LiHoF pumping 5G5 449.8nm DC si detector 1cm resolution 25ns data aquisition 2000 points 5AVG RT 450nmlongpassfilter.0.dpt_fitting_output.txt'
Could this be to do with the long filename?
I don't understand why there would be a problem with a file not existing when opening it to write to.

Related

Python libraries being incompatible with each other

I am trying to reproduce a piece of code source for Image Segmentation which involves the use of the
keras
and
segmentation-models
libraries.
I was able to run it in another PC running Python 3.9 and tensorflow==2.10.0, keras==2.10.0, segmentation-models==1.0.0. However, when I am trying to reproduce it on my PC with tensorflow==2.11.0, keras==2.11.0, and segmentation-models==1.0.1, I get this error:
File "/home/dev/anaconda3/lib/python3.9/site-packages/efficientnet/__init__.py", line 71, in init_keras_custom_objects
keras.utils.generic_utils.get_custom_objects().update(custom_objects)
AttributeError: module 'keras.utils.generic_utils' has no attribute 'get_custom_objects'
Library versions seem to be consistent with what is mentioned in requirements of source.
I tried downgrading keras, tensorflow, and segmentation-models versions to the older ones (which I reckoned had worked on the earlier PC), but that again shows up further compatibility issues. On further digging, I found a link which seemed to give the right requirements. I created a new conda environment with Python==3.7.0, and have again downgraded the versions into those suggested in link. I still am getting newer issues:
(sm) dev#Turbo:/media/dev/WinD/Curious Dev B/PROJECT STAGE - II$ python3 debug.py
Using TensorFlow backend.
Segmentation Models: using `keras` framework.
Traceback (most recent call last):
File "debug.py", line 150, in <module>
model = sm.Unet(BACKBONE, classes=n_classes, activation=activation)
File "/home/dev/anaconda3/envs/sm/lib/python3.7/site-packages/segmentation_models/__init__.py", line 34, in wrapper
return func(*args, **kwargs)
File "/home/dev/anaconda3/envs/sm/lib/python3.7/site-packages/segmentation_models/models/unet.py", line 226, in Unet
**kwargs,
File "/home/dev/anaconda3/envs/sm/lib/python3.7/site-packages/segmentation_models/backbones/backbones_factory.py", line 103, in get_backbone
model = model_fn(*args, **kwargs)
File "/home/dev/anaconda3/envs/sm/lib/python3.7/site-packages/classification_models/models_factory.py", line 78, in wrapper
return func(*args, **new_kwargs)
File "/home/dev/anaconda3/envs/sm/lib/python3.7/site-packages/efficientnet/model.py", line 538, in EfficientNetB3
**kwargs)
File "/home/dev/anaconda3/envs/sm/lib/python3.7/site-packages/efficientnet/model.py", line 368, in EfficientNet
img_input = layers.Input(shape=input_shape)
File "/home/dev/anaconda3/envs/sm/lib/python3.7/site-packages/keras/engine/topology.py", line 1439, in Input
input_tensor=tensor)
File "/home/dev/anaconda3/envs/sm/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
return func(*args, **kwargs)
File "/home/dev/anaconda3/envs/sm/lib/python3.7/site-packages/keras/engine/topology.py", line 1301, in __init__
name = prefix + '_' + str(K.get_uid(prefix))
File "/home/dev/anaconda3/envs/sm/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 63, in get_uid
graph = tf.get_default_graph()
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
I am unsure as to what went wrong here, the code worked six months ago. Now I am unable to run the same code, probably due to version changes of libraries. But at least it should have worked with the older versions that I myself used earlier, or at least with what link says.

How to solve **TypeError : expected str, bytes or os.PathLike object, not WindowsPath** when importing matplotlib to python

I am trying use matplotlib within Autodesk Revit software from the PyRevit Project
In this project, Pyrevit brings an embedded CPython interpreter (3.8.5) to Revit.
Until now, I've managed to import some packages (e.g. numpy, pandas), but unfortunately import matplotlib has been a big challenge.
When I try to import matplotlib, I got the following error:
CPython Traceback:
TypeError : expected str, bytes or os.PathLike object, not WindowsPath
File "C:\Users\carlo\pyproj\bimenv\Lib\site-packages\matplotlib\__init__.py", line 886, in <module>
rcParamsDefault = _rc_params_in_file(
File "C:\Users\carlo\pyproj\bimenv\Lib\site-packages\matplotlib\__init__.py", line 789, in _rc_params_in_file
with _open_file_or_url(fname) as fd:
File "contextlib.py", line 113, in __enter__
File "C:\Users\carlo\pyproj\bimenv\Lib\site-packages\matplotlib\__init__.py", line 765, in _open_file_or_url
fname = os.path.expanduser(fname)
File "ntpath.py", line 293, in expanduser
...\matplotlib_init_.py
File ntpath.py
Doing the same, import matplotlib, from the Python 3.8.5 in the command line it works without any problem.
enter image description here
Thank you in advance for any suggestion from your side to solve this issue.

How to resolve UnicodeError in Tensorflow 2 Object Detection API

I have a question, but when I was training the tensorflow-object-detection-API, I got the following error. Can you tell me if there is any workaround?
Conducted commnand
python model_main_tf2.py --model_dir=models/my_ssd_mobilenet_v1_fpn_640x640_coco17_tpu-8 --pipeline_config_path=models/my_ssd_mobilenet_v1_fpn_640x640_coco17_tpu-8/pipeline.config
erroer messege
File "model_main_tf2.py", line 115, in <module>
tf.compat.v1.app.run()
File "C:\Users\rh731\.virtualenvs\Tensorflow\lib\site-packages\tensorflow\python\platform\app.py", line 40, in ru
n
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "C:\Users\rh731\.virtualenvs\Tensorflow\lib\site-packages\absl\app.py", line 303, in run
_run_main(main, args)
File "C:\Users\rh731\.virtualenvs\Tensorflow\lib\site-packages\absl\app.py", line 251, in _run_main
sys.exit(main(argv))
File "model_main_tf2.py", line 106, in main
model_lib_v2.train_loop(
File "C:\Users\rh731\.virtualenvs\Tensorflow\lib\site-packages\object_detection\model_lib_v2.py", line 611, in tr
ain_loop
manager = tf.compat.v2.train.CheckpointManager(
File "C:\Users\rh731\.virtualenvs\Tensorflow\lib\site-packages\tensorflow\python\training\checkpoint_management.p
y", line 640, in __init__
recovered_state = get_checkpoint_state(directory)
File "C:\Users\rh731\.virtualenvs\Tensorflow\lib\site-packages\tensorflow\python\training\checkpoint_management.p
y", line 278, in get_checkpoint_state
file_content = file_io.read_file_to_string(
File "C:\Users\rh731\.virtualenvs\Tensorflow\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 352, in
read_file_to_string
return f.read()
File "C:\Users\rh731\.virtualenvs\Tensorflow\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 117, in
read
self._preread_check()
File "C:\Users\rh731\.virtualenvs\Tensorflow\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 79, in
_preread_check
self._read_buf = _pywrap_file_io.BufferedInputStream(
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x83 in position 108: invalid start byte
What I did
-I tried to convert the character code of pipeline.config.
-The API was tested. (It's OK like the attached image.)
-Check if there are any mistakes in the execution command.
Also, when learning on another network, I was able to finish learning to the end without such an error. This time as well, I downloaded and ran the trained model.
Reference site:
·tutorial
https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/training.html#training-the-model
・ List of trained models https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md
We apologize for the inconvenience, but thank you for your cooperation.
Most Probably it's because you are trying to run a TPU model on your local machine(I guessed that from you PyCharm ScreenShot). Try running a GPU based model or a CPU one.

Test the .trt file using tensorflow

Below output_saved_model_dir in this directory i am having trt file named final_model_gender_classification_gpu0_int8.trt
output_saved_model_dir='/home/cocoslabs/Downloads/age_gender_trt'
saved_model_loaded = tf.saved_model.load(output_saved_model_dir, tags=[tag_constants.SERVING])
When I run the above script it showing error as follows:
File "test.py", line 7, in <module>
saved_model_loaded = tf.saved_model.load(output_saved_model_dir, tags=[tag_constants.SERVING])
File "/home/cocoslabs/deepstream_docker/venv/lib/python3.6/site-packages/tensorflow_core/python/saved_model/load.py", line 528, in load
return load_internal(export_dir, tags)
File "/home/cocoslabs/deepstream_docker/venv/lib/python3.6/site-packages/tensorflow_core/python/saved_model/load.py", line 537, in load_internal
saved_model_proto = loader_impl.parse_saved_model(export_dir)
File "/home/cocoslabs/deepstream_docker/venv/lib/python3.6/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: /home/cocoslabs/Downloads/age_gender_trt/{saved_model.pbtxt|saved_model.pb}
From the above error what I understand is tf.saved_model.load() accept only .pb or .pbtxt files. Is it right ? But as per this link Load and run test a .trt model what they said is tf.saved_model.load() function will accept .trt file. Help me to rectify this error. Thank You.

Python Matplotlib errors with savefig (newbie).

All parts of Python on my computer were recently installed from the Enthought academic package, but use Pyscripter for editing and running code. I'm very early in my learning curve, and so could very well be overlooking some obvious things here.
When I try to create a plot and save it like so:
import matplotlib.pylab as pl
pl.hist(myEst, bins=20, range=(.1,.60))
pl.ylabel("Freq")
pl.xlabel("Success Probability")
pl.title('Histogram of Binomial Estimator')
pl.axis([0, 1, 0, 500])
pl.vlines (.34,0,500)
pl.savefig('TestHist.png')
pl.show()
I get these errors:
Traceback (most recent call last):
File "<editor selection>", line 9, in <module>
File "C:\Python27\lib\site-packages\matplotlib\figure.py", line 1172, in savefig
self.canvas.print_figure(*args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_wxagg.py", line 100, in print_figure
FigureCanvasAgg.print_figure(self, filename, *args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 2017, in print_figure
**kwargs)
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.py", line 450, in print_png
filename_or_obj = file(filename_or_obj, 'wb')
IOError: [Errno 13] Permission denied: 'TestHist.png'
If I take out the pl.savefig('TestHist') line everything works fine, and I can see the plot I want, but when that line is in there I get the errors.
I've checked my backend version using pl.get_backend(), it returns 'WXAgg', which according to documentation should be able to use .png format.
I've also tried including an explicit format='png' and format=png within the savefig command, but still get errors.
Can anyone give me advice on how to proceed, or another approach for saving a plot?
There's nothing wrong with your code. I just tested it locally on my machine. The issue is this error:
IOError: [Errno 13] Permission denied: 'TestHist.png'
You are most likely trying to save the file somewhere that the Python process doesn't have permission to access. What OS are you on? Where are you trying to save the file?
If it helps others, I made the silly mistake of not actually designating a file name and as a result had returned the same error message that lead me to this question for review.
Here is the code that was generating the error:
plt.savefig('C:\\Users\\bwarn\\Canopy', format='png')
Here is my correction that resolved (you'll see I designated the actual file and name)
plt.savefig('C:\\Users\\bwarn\\Canopy\\myplot.png', format='png')
The following worked for me when I was running a neural network on my windows machine:
image_path = 'A:/DeepLearning/Padhai/MLFlow/images/%s.png' % (expt_id)
plt.savefig(image_path)
Or otherwise refer:
Using 'r' in front of the path