I am new to Python and would like your help please.
I am copying the NorthernBobwhiteCNN code from Github to try to use the program on my computer: https://github.com/GAMELab-UGA/NorthernBobwhiteCNN. I cloned the Github files as my own Jupyter Notebook that I launched from the Command Prompt.
However, when I try to run the cells in model_prediction_example.ipynb after the import statement cell, I receive multiple errors for all the cells and the code won't run, even though everything is the exact same from Github.
Here are the errors I get using Load Trained Model cell:
RuntimeError Traceback (most recent call last)
Input In [2], in <cell line: 8>()
4 model = net.Net(params).cuda() if torch.cuda.is_available() else net.Net(params)
7 restore_path = os.path.join(model_dir, 'pretrained.pth.tar')
----> 8 _ = utils.load_checkpoint(restore_path, model, optimizer=None)
File ~\NorthernBobwhiteCNN\PythonCode\utils.py:136, in load_checkpoint(checkpoint, model, optimizer)
134 if not os.path.exists(checkpoint):
135 raise("File doesn't exist {}".format(checkpoint))
--> 136 checkpoint = torch.load(checkpoint)
137 model.load_state_dict(checkpoint['state_dict'])
139 if optimizer:
File ~\anaconda3\lib\site-packages\torch\serialization.py:789, in load(f, map_location, pickle_module, weights_only, **pickle_load_args)
787 except RuntimeError as e:
788 raise pickle.UnpicklingError(UNSAFE_MESSAGE + str(e)) from None
--> 789 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
790 if weights_only:
791 try:
File ~\anaconda3\lib\site-packages\torch\serialization.py:1131, in _load(zip_file, map_location, pickle_module, pickle_file, **pickle_load_args)
1129 unpickler = UnpicklerWrapper(data_file, **pickle_load_args)
1130 unpickler.persistent_load = persistent_load
-> 1131 result = unpickler.load()
1133 torch._utils._validate_loaded_sparse_tensors()
1135 return result
File ~\anaconda3\lib\site-packages\torch\serialization.py:1101, in _load.<locals>.persistent_load(saved_id)
1099 if key not in loaded_storages:
1100 nbytes = numel * torch._utils._element_size(dtype)
-> 1101 load_tensor(dtype, nbytes, key, _maybe_decode_ascii(location))
1103 return loaded_storages[key]
File ~\anaconda3\lib\site-packages\torch\serialization.py:1083, in _load.<locals>.load_tensor(dtype, numel, key, location)
1079 storage = zip_file.get_storage_from_record(name, numel, torch.UntypedStorage).storage().untyped()
1080 # TODO: Once we decide to break serialization FC, we can
1081 # stop wrapping with TypedStorage
1082 loaded_storages[key] = torch.storage.TypedStorage(
-> 1083 wrap_storage=restore_location(storage, location),
1084 dtype=dtype)
File ~\anaconda3\lib\site-packages\torch\serialization.py:215, in default_restore_location(storage, location)
213 def default_restore_location(storage, location):
214 for _, _, fn in _package_registry:
--> 215 result = fn(storage, location)
216 if result is not None:
217 return result
File ~\anaconda3\lib\site-packages\torch\serialization.py:182, in _cuda_deserialize(obj, location)
180 def _cuda_deserialize(obj, location):
181 if location.startswith('cuda'):
--> 182 device = validate_cuda_device(location)
183 if getattr(obj, "_torch_load_uninitialized", False):
184 with torch.cuda.device(device):
File ~\anaconda3\lib\site-packages\torch\serialization.py:166, in validate_cuda_device(location)
163 device = torch.cuda._utils._get_device_index(location, True)
165 if not torch.cuda.is_available():
--> 166 raise RuntimeError('Attempting to deserialize object on a CUDA '
167 'device but torch.cuda.is_available() is False. '
168 'If you are running on a CPU-only machine, '
169 'please use torch.load with map_location=torch.device(\'cpu\') '
170 'to map your storages to the CPU.')
171 device_count = torch.cuda.device_count()
172 if device >= device_count:
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.
I think that the errors are due to incorrect or missing library installations on my virtual environment.
First, I created the virtual environment "bobwhite" using conda create bobwhite in my command prompt.
Then, I did multiple conda installations based on the import statements in the model_prediction_example.ipynb.
from matplotlib import pyplot as plt
import librosa
import numpy as np
import os
from scipy import ndimage as ndi
from skimage.feature import peak_local_max
import torch
import utils
import model.net as net
So, I did the following installs in my command prompt:
conda install matplotlib
conda install -c conda-forge librosa
conda install numpy
conda install scipy
conda install scikit-image
conda install pytorch torchvision torchaudio cpuonly -c pytorch
conda install pip
pip install utils
However, I am not sure that I have installed the correct libraries to get the notebook to run. How would I find out which libraries are needed based on the import statements? Would I also need to import the libraries used in the python code net.py and utils.py as well? Additionally, I do not understand the import model.net as net statement. Is this referencing the net.py python script also found on the Github? If so, would I need to use a conda install for that, and how would I do it?
Change this line to
map_location = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
checkpoint = torch.load(checkpoint, map_location=map_location)
The existing code is trying to load to cuda even if it is not available. This will make sure it loads to CPU if cuda is not available.
This part of your error's stacktrace points out the issue
File ~\NorthernBobwhiteCNN\PythonCode\utils.py:136, in load_checkpoint(checkpoint, model, optimizer)
134 if not os.path.exists(checkpoint):
135 raise("File doesn't exist {}".format(checkpoint))
--> 136 checkpoint = torch.load(checkpoint)
137 model.load_state_dict(checkpoint['state_dict'])
139 if optimizer:
Related
I am relatively new to NLP and mostly use Jupyter, please let me know what I'm doing wrong:
I followed all the instructions provided here:
https://github.com/explosion/sense2vec
but when I try to use the reddit_vectors as described here:
s2v = Sense2VecComponent(nlp.vocab).from_disk("/path/to/s2v_reddit_2015_md")
I get a ValueError as shown below:
ValueError Traceback (most recent call last)
<ipython-input-36-0d396d0145de> in <module>
----> 1 s2v=Sense2Vec().from_disk('reddit_vectors-1.1.0/vectors.bin/')
~/.conda/envs/NewEnv6/lib/python3.7/site-packages/sense2vec/sense2vec.py in from_disk(self, path,
exclude)
343 cache_path = path / "cache"
344 self.vectors = Vectors().from_disk(path)
--> 345 self.cfg.update(srsly.read_json(path / "cfg"))
346 if freqs_path.exists():
347 self.freqs = dict(srsly.read_json(freqs_path))
~/.conda/envs/NewEnv6/lib/python3.7/site-packages/srsly/_json_api.py in read_json(location)
48 data = sys.stdin.read()
49 return ujson.loads(data)
---> 50 file_path = force_path(location)
51 with file_path.open("r", encoding="utf8") as f:
52 return ujson.load(f)
~/.conda/envs/NewEnv6/lib/python3.7/site-packages/srsly/util.py in force_path(location,
require_exists)
19 location = Path(location)
20 if require_exists and not location.exists():
---> 21 raise ValueError("Can't read file: {}".format(location))
22 return location
23
ValueError: Can't read file: reddit_vectors-1.1.0/vectors.bin/cfg
*I installed all the appropriate versions of libraries/packages required in the requirements.txt
This is what worked for me:
If you are not using a virtual environment check if all the libraries from libraries/packages required in the requirements.txt actually got installed properly, in my case one of them was not properly installed.
The path should lead to the folder containing the cfg file. (After the last update I recommend using the entire computer path instead of navigating inside the same project)
Check your path to reddit_vectors-x.x.x folder.
Put your reddit_vectors-x.x.x folder in the same folder where your .py file is.
Use pathlib to be sure your path is correct.
from pathlib import Path
path = Path(__file__).parent.joinpath('reddit_vectors-1.1.0')
s2v.from_disk(path)
If you still get the error, delete your reddit_vectors-x.x.x folder and re untar/unzip the original reddit_vectors-x.x.x.tar.gz or reddit_vectors-x.x.x.zip
I have anaconda python3 kernel with pytorch and numpy installed in the environment. In jupyter notebook first line 'import torch' produces error.
I am using anaconda navigator to launch jupyter notebook and enter my environment and see pytorch is installed but not being imported. Tried various dir extensions 'from torch... import * but more error
import torch
ImportError Traceback (most recent call last)
<ipython-input-1-20507c95d9af> in <module>
1
----> 2 import torch
3
4
~/anaconda3/envs/udacity1/lib/python3.6/site-packages/torch/__init__.py in <module>
100 pass
101
--> 102 from torch._C import *
103
104 __all__ += [name for name in dir(_C)
ImportError: /home/frida/anaconda3/envs/udacity1/lib/python3.6/site-packages/torch/lib/libtorch.so.1: undefined symbol: nvrtcGetProgramLogSize
I was able to add this to the end of my path to establish a 'backend ' for my notebook kernel. Thanks to Kris Stern!
-m ipykernel install --user
first get your kernel path with
(which python3)
then connect it using
sudo (your path)/anaconda3/bin/python3 -m ipykernel install --user
When i try to import "sklearn" and use it, i am getting "Attribute Error",
## Modeling
# Multiple Linear REgression (Least Square fitting)
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LinearRegression
The error from Jupyter notebook is as below,
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-33-9fc361c79a41> in <module>()
2 # Multiple Linear REgression (Least Square fitting)
3
----> 4 from sklearn.model_selection import cross_val_score
5 from sklearn.linear_model import LinearRegression
6 #from sklearn.model_selection.train_test_split import train_test_split
D:\Anaconda_details\lib\site-packages\sklearn\model_selection\__init__.py in <module>()
17 from ._split import check_cv
18
---> 19 from ._validation import cross_val_score
20 from ._validation import cross_val_predict
21 from ._validation import cross_validate
D:\Anaconda_details\lib\site-packages\sklearn\model_selection\_validation.py in <module>()
29 from ..utils._joblib import logger
30 from ..externals.six.moves import zip
---> 31 from ..metrics.scorer import check_scoring, _check_multimetric_scoring
32 from ..exceptions import FitFailedWarning
33 from ._split import check_cv
D:\Anaconda_details\lib\site-packages\sklearn\metrics\__init__.py in <module>()
5
6
----> 7 from .ranking import auc
8 from .ranking import average_precision_score
9 from .ranking import coverage_error
D:\Anaconda_details\lib\site-packages\sklearn\metrics\ranking.py in <module>()
34 from ..utils.sparsefuncs import count_nonzero
35 from ..exceptions import UndefinedMetricWarning
---> 36 from ..preprocessing import label_binarize
37
38 from .base import _average_binary_score
D:\Anaconda_details\lib\site-packages\sklearn\preprocessing\__init__.py in <module>()
4 """
5
----> 6 from ._function_transformer import FunctionTransformer
7
8 from .data import Binarizer
D:\Anaconda_details\lib\site-packages\sklearn\preprocessing\_function_transformer.py in <module>()
3 from ..base import BaseEstimator, TransformerMixin
4 from ..utils import check_array
----> 5 from ..utils.testing import assert_allclose_dense_sparse
6 from ..externals.six import string_types
7
D:\Anaconda_details\lib\site-packages\sklearn\utils\testing.py in <module>()
758 import pytest
759
--> 760 skip_if_32bit = pytest.mark.skipif(_IS_32BIT,
761 reason='skipped on 32bit platforms')
762 skip_travis = pytest.mark.skipif(os.environ.get('TRAVIS') == 'true',
AttributeError: module 'pytest' has no attribute 'mark'
I have tried install/uninstall using pip
pip uninstall scikit-learn
pip install scikit-learn
(base) C:\Users\shashi>pip install scikit-learn
I am not able to uninstall sckit-learn package, it seems to be some path issue,
(base) C:\Users\shashi>pip uninstall scikit-learn
Uninstalling scikit-learn-0.20.0:
Would remove:
d:\anaconda_details\lib\site-packages\scikit_learn-0.20.0-py3.5.egg-info
d:\anaconda_details\lib\site-packages\sklearn
Proceed (y/n)? y
Exception:
Traceback (most recent call last):
File "D:\Anaconda_details\lib\shutil.py", line 544, in move
os.rename(src, real_dst)
OSError: [WinError 17] The system cannot move the file to a different disk drive: 'd:\\anaconda_details\\lib\\site-packages\\sklearn' -> 'C:\\Users\\sh289528\\AppData\\Local\\Temp\\pip-uninstall-w3d2aqtz\\anaconda_details\\lib\\site-packages\\sklearn'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Anaconda_details\lib\site-packages\pip\_internal\basecommand.py", line 228, in main
status = self.run(options, args)
File "D:\Anaconda_details\lib\site-packages\pip\_internal\commands\uninstall.py", line 68, in run
auto_confirm=options.yes, verbose=self.verbosity > 0,
File "D:\Anaconda_details\lib\site-packages\pip\_internal\req\req_install.py", line 661, in uninstall
uninstalled_pathset.remove(auto_confirm, verbose)
File "D:\Anaconda_details\lib\site-packages\pip\_internal\req\req_uninstall.py", line 219, in remove
renames(path, new_path)
File "D:\Anaconda_details\lib\site-packages\pip\_internal\utils\misc.py", line 273, in renames
shutil.move(old, new)
File "D:\Anaconda_details\lib\shutil.py", line 556, in move
rmtree(src)
File "D:\Anaconda_details\lib\shutil.py", line 494, in rmtree
return _rmtree_unsafe(path, onerror)
File "D:\Anaconda_details\lib\shutil.py", line 384, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
File "D:\Anaconda_details\lib\shutil.py", line 389, in _rmtree_unsafe
onerror(os.unlink, fullname, sys.exc_info())
File "D:\Anaconda_details\lib\shutil.py", line 387, in _rmtree_unsafe
os.unlink(fullname)
PermissionError: [WinError 5] Access is denied: 'd:\\anaconda_details\\lib\\site-packages\\sklearn\\feature_extraction\\_hashing.cp35-win_amd64.pyd'
You are using pip version 18.0, however version 19.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
I am getting error again when i try to pip install.
(base) C:\Users\shashi>pip install scikit-learn
Collecting scikit-learn
Downloading https://files.pythonhosted.org/packages/d3/fa/b50821115c16e9b8ca307d3788e3dd1ec71cade3e564953ed7330a1fa3e0/scikit_learn-0.20.3-cp35-cp35m-win_amd64.whl (4.8MB)
100% |################################| 4.8MB 546kB/s
Requirement already satisfied: scipy>=0.13.3 in d:\anaconda_details\lib\site-packages (from scikit-learn) (1.1.0)
Requirement already satisfied: numpy>=1.8.2 in d:\anaconda_details\lib\site-packages (from scikit-learn) (1.15.2)
twisted 18.7.0 requires PyHamcrest>=1.9.0, which is not installed.
Installing collected packages: scikit-learn
Could not install packages due to an EnvironmentError: [WinError 5] Access is denied: 'D:\\Anaconda_details\\Lib\\site-packages\\sklearn\\feature_extraction\\_hashing.cp35-win_amd64.pyd'
Consider using the `--user` option or check the permissions.
You are using pip version 18.0, however version 19.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
I have tried with conda uninstall and install. but that didn't help me either.
(base) C:\Users\shashi>conda update scikit-learn
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.5.5
latest version: 4.6.9
Please update conda by running
$ conda update -n base -c defaults conda
I recently made an update of IPython to 4.0.0 and installed Jupyter 4.0.6.
I wanted to use Ipython parallel, and after starting the engines in the notebook, I imported:
from IPython import parallel
And it fails:
~/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/IPython/utils/traitlets.py:5: UserWarning: IPython.utils.traitlets has moved to a top-level traitlets package.
warn("IPython.utils.traitlets has moved to a top-level traitlets package.")
~/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/IPython/utils/pickleutil.py:3: UserWarning: IPython.utils.pickleutil has moved to ipykernel.pickleutil
warn("IPython.utils.pickleutil has moved to ipykernel.pickleutil")
~/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/IPython/utils/jsonutil.py:3: UserWarning: IPython.utils.jsonutil has moved to jupyter_client.jsonutil
warn("IPython.utils.jsonutil has moved to jupyter_client.jsonutil")
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-5652e9e33a4d> in <module>()
----> 1 from IPython import parallel
~/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/IPython/parallel/__init__.py in <module>()
31
32 from .client.asyncresult import *
---> 33 from .client.client import Client
34 from .client.remotefunction import *
35 from .client.view import *
~/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/IPython/parallel/client/client.py in <module>()
38 from IPython.utils.capture import RichOutput
39 from IPython.utils.coloransi import TermColors
---> 40 from IPython.utils.jsonutil import rekey, extract_dates, parse_date
41 from IPython.utils.localinterfaces import localhost, is_local_ip
42 from IPython.utils.path import get_ipython_dir
ImportError: cannot import name rekey
So I tried:
pip install rekey
But no distribution were found.
Note that it fails the same way in the notebook, be it open with ipython notebook or jupyter notebook, and in the console.
Also note that there is a warning:
UserWarning: IPython.utils.jsonutil has moved to jupyter_client.jsonutil
But rekey does not exist in the module jupyter_client.jsonutil
Question: How can I have IPython parallel to work within Jupyter ?
What am I missing ?
I found the problem I think (at least it works):
First, I had to import ipyparallel instead of IPython.parallel
See here: http://jupyter.readthedocs.org/en/latest/migrating.html#imports
EDIT: I get this OSError, but the fix was apparently useless, and it works without. I still don't get why I had this error, though.
Then, I had another error, when starting the client:
OSError: Connection file '~/.ipython/profile_default/security/ipcontroller-client.json' not found.
You have attempted to connect to an IPython Cluster but no Controller could be found.
Please double-check your configuration and ensure that a cluster is running.
So I just copy the directory ~/.ipython/profile_default to ~/.jupyter/profile_default
And it works!
I am using Enthought's Canopy environment on a 64 bit Linux OS. Everything works fine in the Ipython console which is attached with the editor. But when I ipython in the terminal and try to use magic functions, I get the following error.
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-3-29a4050aa687> in <module>()
----> 1 get_ipython().show_usage()
/home/shahensha/Development/Canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in show_usage(self)
2931 def show_usage(self):
2932 """Show a usage message"""
-> 2933 page.page(IPython.core.usage.interactive_usage)
2934
2935 def extract_input_lines(self, range_str, raw=False):
/home/shahensha/Development/Canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/site-packages/IPython/core/page.pyc in page(strng, start, screen_lines, pager_cmd)
188 if screen_lines <= 0:
189 try:
--> 190 screen_lines += _detect_screen_size(screen_lines_def)
191 except (TypeError, UnsupportedOperation):
192 print(str_toprint, file=io.stdout)
/home/shahensha/Development/Canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/site-packages/IPython/core/page.pyc in _detect_screen_size(screen_lines_def)
112 # Proceed with curses initialization
113 try:
--> 114 scr = curses.initscr()
115 except AttributeError:
116 # Curses on Solaris may not be complete, so we can't use it there
/home/shahensha/Development/Canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/curses/__init__.pyc in initscr()
31 # instead of calling exit() in error cases.
32 setupterm(term=_os.environ.get("TERM", "unknown"),
---> 33 fd=_sys.__stdout__.fileno())
34 stdscr = _curses.initscr()
35 for key, value in _curses.__dict__.items():
error: setupterm: could not find terminfo database
So, I installed a bare bones iPython shell which is not the one provided by Canopy and tried the same magic functions in there and it works fine.
Have I done something wrong with the installation? Please help
Thanks a lot
shahensha
This is not a solution, but just an observation. My desktop is MacOS-X and I connect to a Centos machine to run Enthought Canopy both 64 bit. I get the same error message as OP if I ssh from iterm2, but not if I use the Terminal app.
I am not sure what the underlying reason is, but may be someone can verify if a similar situation is true for linux. Interestingly I can use either iterm2 or Terminal on the local canopy without any issues.
Update:
I just noticed that the TERM environment variable in iterm2 was set to "xterm" while the Terminal app was showing "xterm-256color". Issuing the command export TERM="xterm-256color" before running the Canopy ipython in terminal solves the issue for me in iterm2.
Problem reproduction:
$ python -c 'import curses; curses.setupterm()'
Traceback (most recent call last):
File "<string>", line 1, in <module>
_curses.error: setupterm: could not find terminfo database
This irc log gave me the idea that this error was to do with libncursesw.
My Canopy version is 1.0.3.1262.rh5-x86_64. I have installed it to ~/src/canopy.
In ~/src/canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib we can see that my canopy install has libncursesw.so.5.7.
My machine (Debian Wheezy 64bit) has libncursesw.so.5.9 (in /lib/x86_64-linux-gnu/libncursesw.so.5.9). I made canopy use this. You can toggle the problem on / off by using LD_PRELOAD and pointing at the .so file.
Solution
Replace libncurses.so.5.7 with libncurses.so.5.9:
CANOPYDIR=$HOME/src/canopy
CANOPYLIBS=$CANOPYDIR/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/
SYSTEMLIBS=/lib/x86_64-linux-gnu
cp $SYSTEMLIBS/libncurses.so.5.9 $CANOPYLIBS
ln -sf $CANOPYLIBS/libncurses.so.5.9 $CANOPYLIBS/libncurses.so.5
It appears that Canopy User Python is not your default. See this article:
https://support.enthought.com/entries/23646538-Make-Canopy-s-Python-be-your-default-Python-i-e-on-the-PATH-
Update: Not true here -- instead, see batu's workaround answer.