Error: load_weights requires h5py when loading weights from HDF5 - keras

I used both commands below and received a message about being successful install and restarting the kernel
conda uninstall h5py
pip install h5py
But still, get this error below:
ImportError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_14156/3723649668.py in 1 import
tensorflow as tf 2 model = tf.keras.Sequential([
tf.keras.layers.Dense(5, input_shape=(3,)),
tf.keras.layers.Softmax()]) ----> 3
model.load_weights('my_weights.h5')
~\Anaconda3\lib\site-packages\keras\engine\training.py in
load_weights(self, filepath, by_name, skip_mismatch, options) 2344
status = None 2345 if h5py is None: -> 2346 raise ImportError( 2347
'load_weights requires h5py when loading weights from HDF5.') 2348 if
not self._is_graph_network and not self.built:
ImportError: load_weights requires h5py when loading weights from
HDF5.

Related

Problem with loading spacy.load('en_core_web_md')

I have installed in anaconda the packages shown below:
spacy 2.2.2
spacy-model-en_core_web_md 2.2.5
spacy-model-en_core_web_sm 2.2.5
python 3.6.2
The above packages were installed in conda with the commands shown below:
conda install -c conda-forge spacy=2.2.2
conda install -c conda-forge spacy-model-en_core_web_sm
conda install -c conda-forge spacy-model-en_core_web_md
When i load en_core_web_md and en_core_web_sm, i get an error message shown below:
import spacy
import en_core_web_sm
nlp = spacy.load('en_core_web_sm')
Error message:
-> ---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-39-d6345e302427> in <module>
1 import spacy
2 import en_core_web_sm
----> 3 nlp = spacy.load('en_core_web_sm')
4
5 #import en_core_web_sm
~\anaconda3\envs\ADS99\lib\site-packages\spacy\__init__.py in load(name, **overrides)
17 from . import util
18 from .util import register_architecture, get_architecture
---> 19 from .language import component
20
21
~\anaconda3\envs\ADS99\lib\site-packages\spacy\util.py in load_model(name, **overrides)
117
118 path (unicode or Path): Path to new data directory.
--> 119 """
120 global _data_path
121 _data_path = ensure_path(path)
OSError: Can't find model 'en_core_web_sm'
I tried a different way of loading en_core_web_sm but again i got a different error:
import spacy
import en_core_web_sm
nlp = en_core_web_sm.load()
Error message:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-40-9427d7caa44a> in <module>
1 import spacy
2 import en_core_web_sm
----> 3 nlp = en_core_web_sm.load()
4
5
~\anaconda3\envs\ADS99\lib\site-packages\en_core_web_sm\__init__.py in load(**overrides)
10
11 def load(**overrides):
---> 12 return load_model_from_init_py(__file__, **overrides)
~\anaconda3\envs\ADS99\lib\site-packages\spacy\util.py in load_model_from_init_py(init_file, **overrides)
174 return Path(path)
175 else:
--> 176 return path
177
178
~\anaconda3\envs\ADS99\lib\site-packages\spacy\util.py in load_model_from_path(model_path, meta, **overrides)
143
144
--> 145 def make_layer(arch_config):
146 arch_func = get_architecture(arch_config["arch"])
147 return arch_func(arch_config["config"])
~\anaconda3\envs\ADS99\lib\site-packages\spacy\util.py in get_lang_class(lang)
47
48 factories = "spacy_factories"
---> 49 languages = "spacy_languages"
50 displacy_colors = "spacy_displacy_colors"
51 lookups = "spacy_lookups"
~\anaconda3\envs\ADS99\lib\importlib\__init__.py in import_module(name, package)
124 break
125 level += 1
--> 126 return _bootstrap._gcd_import(name[level:], package, level)
127
128
~\anaconda3\envs\ADS99\lib\importlib\_bootstrap.py in _gcd_import(name, package, level)
~\anaconda3\envs\ADS99\lib\importlib\_bootstrap.py in _find_and_load(name, import_)
~\anaconda3\envs\ADS99\lib\importlib\_bootstrap.py in _find_and_load_unlocked(name, import_)
~\anaconda3\envs\ADS99\lib\importlib\_bootstrap.py in _load_unlocked(spec)
~\anaconda3\envs\ADS99\lib\importlib\_bootstrap_external.py in exec_module(self, module)
~\anaconda3\envs\ADS99\lib\importlib\_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)
~\anaconda3\envs\ADS99\lib\site-packages\spacy\lang\en\__init__.py in <module>
12 from ..tokenizer_exceptions import BASE_EXCEPTIONS
13 from ..norm_exceptions import BASE_NORMS
---> 14 from ...language import Language
15 from ...attrs import LANG, NORM
16 from ...util import update_exc, add_lookups
~\anaconda3\envs\ADS99\lib\site-packages\spacy\language.py in <module>
18 from .vocab import Vocab
19 from .lemmatizer import Lemmatizer
---> 20 from .lookups import Lookups
21 from .analysis import analyze_pipes, analyze_all_pipes, validate_attrs
22 from .compat import izip, basestring_, is_python2, class_types
~\anaconda3\envs\ADS99\lib\site-packages\spacy\lookups.py in <module>
4 import srsly
5 from collections import OrderedDict
----> 6 from preshed.bloom import BloomFilter
7
8 from .errors import Errors
bloom.pyx in init preshed.bloom()
AttributeError: type object 'preshed.bloom.BloomFilter' has no attribute '__reduce_cython__'
If someone can provide me any hint on how this could issue could be fixed, i would be really grateful.
you can try using anaconda prompt, and you can do this:
conda install -c conda-forge spacy
python -m spacy download en
after that, you can load the model via its full package name.
import spacy
nlp = spacy.load('en_core_web_sm')
and you can try to test it, like this:
check = nlp("How's your spicy its really spicy, don't you think?")
and do this
for token in check:
print (token)
if its really work, it turn out be like this.
How
's
your
spicy
its
really
spicy
,
do
n't
you
think
?
good luck.
find a location of your 'en_core_web_sm' in spacy use in load directory.
e.g
model = spacy.load('path/....')
Is the version of Python you're using to install the en_core_web_sm model the same as the version of Python you're using to consume the model? If not, it could be that the SpaCy model cannot be found by your application because it is installed under a different Python version.
You can easily confirm this by running the following tests:
From console:
python --version
From your code:
import sys
print(sys.version)
If you have successfully downloaded the model check where the model is installed. I have found that downloading en_core_web_md in a conda environment can lead it to be saved in the site-packages folder directly (using windows) NOT the data folder in the spacy package where the load_model looks by default.

Error in pytesseract while running in google colab

I have installed tesseract in Google colab using the command
!pip3 install pytesseract
!pip3 install tesseract-ocr
!sudo apt install libtesseract-dev
Then I ran the command
from pytesseract import image_to_string
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'/usr/local/bin/pytesseract'
pytesseract.image_to_string(img, lang='eng', config=r'--oem 3 --psm 7')
Then I got the error
---------------------------------------------------------------------------
TesseractError Traceback (most recent call last)
<ipython-input-42-3e07674434a3> in <module>()
3
----> 4 pytesseract.image_to_string(im_bw, lang='eng', config=r'--oem 3 --psm 7')
3 frames
/usr/local/lib/python3.6/dist-packages/pytesseract/pytesseract.py in run_tesseract(input_filename, output_filename_base, extension, lang, config, nice, timeout)
234 with timeout_manager(proc, timeout) as error_string:
235 if proc.returncode:
--> 236 raise TesseractError(proc.returncode, get_errors(error_string))
237
238
TesseractError: (2, 'Usage: pytesseract [-l lang] input_file')

Pymc3 theano import failure

I have been unable to identify the issue with Pymc3's inability to correctly run Theano. I have also been unable to find a satisfactory answer anywhere else online. I am working in Mac OSX Mojave using Anaconda and Python 3.6. I am trying to run the code on a Jupyter Notebook from "Probabilistic Programming and Bayesian Methods for Hackers".
https://nbviewer.jupyter.org/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/blob/master/Chapter1_Introduction/Ch1_Introduction_PyMC3.ipynb
Code breaks with the first attempt to utilize Pymc3:
import pymc3 as pm
import theano.tensor as tt
with pm.Model() as model:
alpha = 1.0/count_data.mean() # Recall count_data is the
# variable that holds our txt counts
lambda_1 = pm.Exponential("lambda_1", alpha)
lambda_2 = pm.Exponential("lambda_2", alpha)
tau = pm.DiscreteUniform("tau", lower=0, upper=n_count_data - 1)
Error code follows, below. Thanks in advance.
ImportError Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/theano/gof/lazylinker_c.py in <module>
80 version,
---> 81 actual_version, force_compile, _need_reload))
82 except ImportError:
ImportError: Version check of the existing lazylinker compiled file. Looking for version 0.211, but found None. Extra debug information: force_compile=False, _need_reload=True
During handling of the above exception, another exception occurred:
ImportError Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/theano/gof/lazylinker_c.py in <module>
104 version,
--> 105 actual_version, force_compile, _need_reload))
106 except ImportError:
ImportError: Version check of the existing lazylinker compiled file. Looking for version 0.211, but found None. Extra debug information: force_compile=False, _need_reload=True
During handling of the above exception, another exception occurred:
Exception Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/theano/gof/vm.py in <module>
673 raise theano.gof.cmodule.MissingGXX('lazylinker will not be imported if theano.config.cxx is not set.')
--> 674 from . import lazylinker_c
675
/anaconda3/lib/python3.6/site-packages/theano/gof/lazylinker_c.py in <module>
139 cmodule.GCC_compiler.compile_str(dirname, code, location=loc,
--> 140 preargs=args)
141 # Save version into the __init__.py file.
/anaconda3/lib/python3.6/site-packages/theano/gof/cmodule.py in compile_str(module_name, src_code, location, include_dirs, lib_dirs, libs, preargs, py_module, hide_symbols)
2398 raise Exception('Compilation failed (return status=%s): %s' %
-> 2399 (status, compile_stderr.replace('\n', '. ')))
2400 elif config.cmodule.compilation_warning and compile_stderr:
Exception: Compilation failed (return status=1): In file included from /Users/kthln/.theano/compiledir_Darwin-18.7.0-x86_64-i386-64bit-i386-3.6.9-64/lazylinker_ext/mod.cpp:1:. In file included from /anaconda3/include/python3.6m/Python.h:25:. /anaconda3/bin/../include/c++/v1/stdio.h:108:15: fatal error: 'stdio.h' file not found. #include_next <stdio.h>. ^~~~~~~~~. 1 error generated..
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-48-f2e1580aa39f> in <module>
1 import pymc3 as pm
----> 2 import theano.tensor as tt
3
4
5 with pm.Model() as model:
/anaconda3/lib/python3.6/site-packages/theano/__init__.py in <module>
108 object2, utils)
109
--> 110 from theano.compile import (
111 SymbolicInput, In,
112 SymbolicOutput, Out,
/anaconda3/lib/python3.6/site-packages/theano/compile/__init__.py in <module>
10 from theano.compile.function_module import *
11
---> 12 from theano.compile.mode import *
13
14 from theano.compile.io import *
/anaconda3/lib/python3.6/site-packages/theano/compile/mode.py in <module>
9 import theano
10 from theano import gof
---> 11 import theano.gof.vm
12 from theano import config
13 from six import string_types
/anaconda3/lib/python3.6/site-packages/theano/gof/vm.py in <module>
681 except ImportError:
682 pass
--> 683 except (OSError, theano.gof.cmodule.MissingGXX) as e:
684 # OSError happens when g++ is not installed. In that case, we
685 # already changed the default linker to something else then CVM.
AttributeError: module 'theano' has no attribute 'gof'
Disregard. I noticed with an update I was running Python 3.7. Set up a separate conda env with Python 3.6 and it works fine.

How to fix "AttributeError: module 'pycocotools' has no attribute 'mask'" error in python

I am facing some issues even though I installed pycocotools correctly. Also, tried on colab where they have pycocotools already installed..
Installation method:
!git clone https://github.com/cocodataset/cocoapi.git
%cd /content/cocoapi/PythonAPI
!make
!sudo make install
!sudo python setup.py install
%cd /content
AttributeError Traceback (most recent call last)
<ipython-input-11-6a1416ecd890> in <module>()
----> 1 new_mask = get_mask(label['segmentations'], mask)
/content/datasets/coco.py in get_mask(segmentations, mask)
17 def get_mask(segmentations, mask):
18 for segmentation in segmentations:
---> 19 rle = pycocotools.mask.frPyObjects(segmentation, mask.shape[0], mask.shape[1])
20 mask[pycocotools.mask.decode(rle) > 0.5] = 0
21 return mask
AttributeError: module 'pycocotools' has no attribute 'mask'
Line which throws this error:
pycocotools.mask.frPyObjects(segmentation, mask.shape[0], mask.shape[1])
If you just imported pycocotools, mask module is not imported.
Try to import mask explicitly:
import pycocotools.mask

Keras: ImportError: `save_model` requires h5py even thought the code already imported h5py

I ran into some trouble when trying to save a Keras model:
Here is my code:
import h5py
from keras.models import load_model
try:
import h5py
print ('import fine')
except ImportError:
h5py = None
left.save('left.h5') # creates a HDF5 file 'my_model.h5'
left_load = load_model('left.h5')
But I got the following errors even though the code print 'import fine':
import fine
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-145-b641e79036fa> in <module>()
8 h5py = None
9
---> 10 left.save('left.h5') # creates a HDF5 file 'my_model.h5'
/usr/local/lib/python3.4/dist-packages/keras/engine/topology.py in save(self, filepath, overwrite, include_optimizer)
2504 """
2505 from ..models import save_model
-> 2506 save_model(self, filepath, overwrite, include_optimizer)
2507
2508 def save_weights(self, filepath, overwrite=True):
/usr/local/lib/python3.4/dist-packages/keras/models.py in save_model(model, filepath, overwrite, include_optimizer)
53
54 if h5py is None:
---> 55 raise ImportError('`save_model` requires h5py.')
56
57 def get_json_type(obj):
ImportError: `save_model` requires h5py.
Make sure you use the latest version of Keras.
Also, this error has been reported here in the keras github:
https://github.com/fchollet/keras/issues/3426
on linux:
sudo apt-get install libhdf5
sudo pip install h5py
Have you tried directly installing h5py? http://docs.h5py.org/en/latest/build.html
Try running pip install h5py

Resources