nibabel.filebasedimages.ImageFileError for large label files - nibabel

I am getting the following error when loading a large labeled NIFTI file of size 14.4 MB.
Traceback (most recent call last):
File "/home/miran045/reine097/projects2/lab2im/lab2im/dcan/reproduce_load_error.py", line 7, in <module>
img = nib.load(file_path)
File "/home/miran045/reine097/.local/lib/python3.7/site-packages/nibabel/loadsave.py", line 55, in load
raise ImageFileError(f'Cannot work out file type of "{filename}"')
nibabel.filebasedimages.ImageFileError: Cannot work out file type of "/home/feczk001/shared/data/nnUNet/nnUNet_raw_data_base/nnUNet_raw_data/Task509_Paper/labelsTr1/1mo_sub-375518.nii.gz"
Here is the code:
import nibabel as nib
print(nib.__version__)
file_path = '/home/feczk001/shared/data/nnUNet/nnUNet_raw_data_base/nnUNet_raw_data/Task509_Paper/labelsTr1/1mo_sub' \
'-375518.nii.gz'
img = nib.load(file_path)
print(img.shape)
This does not happen when I try to open such files of smaller size (on the order of KB). I can open this file in FreeSurfer FreeView without error and it looks fine. This is happening with version 3.2.1 of NiBabel.

The file is probably not actually zipped, despite the GZ file extension. Try gzipping it.

Related

onnxruntime: Given model could not be parsed while creating inference session. Error message: Protobuf parsing failed

According to the example code mentioned below the library. I have followed the example code but it didn't work.
[Library] https://github.com/notAI-tech/NudeNet/
Code
from nudenet import NudeClassifier
import onnxruntime
classifier = NudeClassifier()
classifier.classify('/home/coremax/Downloads/DETECTOR_AUTO_GENERATED_DATA/IMAGES/3FEF7B75-3823-4153-8490-87483AAC6ABC'
'.jpg')
I have also followed the previous solution on StackOverflow but it didn't work
Error on running Super Resolution Model from ONNX
Traceback (most recent call last):
File "/snap/pycharm-community/276/plugins/python-ce/helpers/pydev/pydevd.py", line 1491, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "/snap/pycharm-community/276/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/home/coremax/Documents/NudeNet/main.py", line 3, in <module>
classifier = NudeClassifier()
File "/home/coremax/Documents/NudeNet/nudenet/classifier.py", line 37, in __init__
self.nsfw_model = onnxruntime.InferenceSession(model_path)
File "/home/coremax/anaconda3/envs/AdultNET/lib/python3.6/site-packages/onnxruntime/capi/session.py", line 158, in __init__
self._load_model(providers or [])
File "/home/coremax/anaconda3/envs/AdultNET/lib/python3.6/site-packages/onnxruntime/capi/session.py", line 166, in _load_model
True)
RuntimeError: /onnxruntime_src/onnxruntime/core/session/inference_session.cc:238 onnxruntime::InferenceSession::InferenceSession(const onnxruntime::SessionOptions&, const onnxruntime::Environment&, const string&) status.IsOK() was false. Given model could not be parsed while creating inference session. Error message: Protobuf parsing failed.
I know it is too late but hope this helps someone build a very useful software.
why it fails
the error is that for NudeClassifier to work it has to download the onnx model from this link
but github now requires you to be logged in to download any file so the constructor for the NudeClassifier fails as it tries to download this model
Solution
create a folder in your user's home folder with the name .NudeNet/
download the model from this link
save the model in the folder you created in step one
you should now have the model at the following path ~/.NudeNet/classifier_model.onnx
now you're ready to go good luck!

UnicodeDecodeError from tf.train.import_meta_graph

I serialized a Tensorflow model with the following code ...
save_path = self.saver.save(self.session, os.path.join(self.logdir, "model.ckpt"), global_step)
logging.info("Model saved in file: %s" % save_path)
... and I'm now trying to restore it from scratch in a separate file using the following code:
saver = tf.train.import_meta_graph(PROJ_DIR + '/logs/default/model.ckpt-54.meta')
session = tf.Session()
saver.restore(session, PROJ_DIR + '/logs/default/model.ckpt-54')
print('Model restored')
When tf.train.import_meta_graph is called, the following exception is thrown:
[libprotobuf ERROR google/protobuf/io/coded_stream.cc:207] A protocol message was rejected because it was too big (more than 67108864 bytes). To increase the limit (or to disable these warnings), see CodedInputStream::SetTotalBytesLimit() in google/protobuf/io/coded_stream.h.
Traceback (most recent call last):
File "/home/reid/projects/research/ccg/taggerflow_modified/test/tf_restore.py", line 4, in <module>
saver = tf.train.import_meta_graph(PROJ_DIR + '/logs/default/model.ckpt-54.meta')
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1711, in import_meta_graph
read_meta_graph_file(meta_graph_or_file), clear_devices)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1563, in read_meta_graph_file
text_format.Merge(file_content.decode("utf-8"), meta_graph_def)
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa7 in position 1: invalid start byte
For reference, here's the first few lines of <PROJ_DIR>/logs/default/model.ckpt-54.meta:
<A7>:^R<A4>:
9
^CAdd^R^F
^Ax"^AT^R^F
^Ay"^AT^Z^F
^Az"^AT"^Z
^AT^R^Dtype:^O
^M2^K^S^A^B^D^F^E^C ^R^G
I think that Tensorflow is using a different encoding when serializing vs when deserializing. How do we specify the encoding that Tensorflow uses when serializing/deserializing? Or is the solution something different?
I was facing the same issue. Have you ensured that apart from the
.meta, .data-00000-of-00001 and the .index files
the file named 'checkpoint' too is there in the directory from which you're loading the model?
My issue got resolved after I made sure of this. Hope this helps!

matplotlib savefig IO error

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.

Numpy Load OverflowError: length too large

I have an algorithm that runs through a dataset and creates a scipy sparse matrix which in turn is saved using:
numpy.savez
and the file is open such as:
open(file, 'wb').
The matrix can get a considerable amount of disk space (it took about 20 GB running for 30 days)
After that, those matrices are loaded into other applications such as:
file = open(path_to_file, 'rb')
matrix = load(file)
data = matrix['arr_0']
ind = matrix['arr_1']
indptr = matrix['arr_2']
For 10 days it worked fine.
When running for a dataset of 30 days the matrix was also successfully created and saved.
But when trying to load it I got the error:
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/ubuntu/recsys/Scripts/Neighborhood/s3_CRM_neighborhood.py", line 76, in <module>
data = matrix['arr_0']
File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 241, in __getitem__
return format.read_array(value)
File "/usr/lib/python2.7/dist-packages/numpy/lib/format.py", line 458, in read_array
data = fp.read(int(count * dtype.itemsize))
OverflowError: length too large
If I could successfully create and save the matrices shouldn't it be able to also load the result? Is there some overhead that is killing the loading? Is is possible to work around this issue?
Thanks in advance,
From the notes on the just published numpy version 1.8, release candidate 1:
IO compatibility with large files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Large NPZ files >2GB can be loaded on 64-bit systems.
So it seems you hit a known bug that has just been solved.

matplotlib gtk issue

Firstly, I know very very little about Python, Xwindows, Matplotlib or GTK. I am trying to run a tool called SpliceGrapher which uses the above. I get an (ugly, sorry) error:
/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py:57: GtkWarning: could not open display
warnings.warn(str(e), _gtk.Warning)
/home/my/bin/lib64/python2.6/site-packages/matplotlib/backends/backend_gtk.py:52: GtkWarning: gdk_cursor_new_for_display: assertion `GDK_IS_DISPLAY (display)' failed
cursors.MOVE : gdk.Cursor(gdk.FLEUR),
Traceback (most recent call last):
File "/home/my/bin/SpliceGrapher-0.2.0/scripts/view_splicegraphs.py", line 28, in <module>
from pylab import *
File "/home/my/bin/lib64/python2.6/site-packages/pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "/home/my/bin/lib64/python2.6/site-packages/matplotlib/pylab.py", line 264, in <module>
from matplotlib.pyplot import *
File "/home/my/bin/lib64/python2.6/site-packages/matplotlib/pyplot.py", line 95, in <module>
new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/home/my/bin/lib64/python2.6/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
File "/home/my/bin/lib64/python2.6/site-packages/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
File "/home/my/bin/lib64/python2.6/site-packages/matplotlib/backends/backend_gtk.py", line 52, in <module>
cursors.MOVE : gdk.Cursor(gdk.FLEUR),
RuntimeError: could not create GdkCursor object
If anyone has any idea what I can do, that would be great.
I am on a Linux (RedHat) system, through puTTy using Xming. I have X11 configured and xeyes shows the little eyes that follow my mouse so I know thats working.
New error:
16:00:46 view_splicegraphs.py Started
Traceback (most recent call last):
File "/home/bmoran/bin/SpliceGrapher-0.2.0/scripts/view_splicegraphs.py", line 164, in <module>
g = getFirstGraph(f)
File "/home/bmoran/bin/lib64/python2.6/site-packages/SpliceGrapher/SpliceGraph.py", line 371, in getFirstGraph
result = SpliceGraphParser(f, **args).next()
File "/home/bmoran/bin/lib64/python2.6/site-packages/SpliceGrapher/SpliceGraph.py", line 1351, in __init__
self.loadFromFile()
File "/home/bmoran/bin/lib64/python2.6/site-packages/SpliceGrapher/SpliceGraph.py", line 1424, in loadFromFile
raise ValueError("Graph feature found before graph header at line %d" % lineNo)
ValueError: Graph feature found before graph header at line 1
/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py:57: GtkWarning: could not open display
Tells you your script does not have an access to your X session.
Can you post here your script?
Did you access the machine with ssh -XC ... ?
on the shell when you issue echo $DISPLAY what do you see?
When you add in your script print os.getenv("DISPLAY") do you see the same result?
one more note ... I don't know Xming but I can recommend you to try MobaXterm, it has a builtin ssh and X11 server compiled for Windows, and my guess is that it will solve your X problem.