Traceback (most recent call last): convert_model.py - python

When implementing the code:
https://github.com/fizyr/keras-retinanet
Below is the code of convert_model.py script
# Allow relative imports when being executed as script.
if __name__ == "__main__" and __package__ is None:
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
import keras_retinanet.bin # noqa: F401
__package__ = "keras_retinanet.bin"
# Change these to absolute imports if you copy this script outside the keras_retinanet package.
from keras_retinanet.models import load_model
model_path = os.path.join('..', 'snapshots', 'resnet50_coco_best_v2.1.0.h5')
model = load_model(model_path, backbone_name='resnet50')
Executing the convert_model.py script, the below error occured:
Traceback (most recent call last):
File "convert_model.py", line 33, in <module>
model = load_model(model_path, backbone_name='resnet50')
File "..\..\keras_retinanet\models\__init__.py", line 83, in load_model
model = keras.models.load_model(filepath, custom_objects=backbone(backbone_name).custom_objects)
File "C:\Python\Python36\lib\site-packages\keras\engine\saving.py", line 249, in load_model
f = h5py.File(filepath, mode='r')
File "C:\Python\Python36\lib\site-packages\h5py\_hl\files.py", line 312, in __init__
fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
File "C:\Python\Python36\lib\site-packages\h5py\_hl\files.py", line 142, in make_fid
fid = h5f.open(name, flags, fapl=fapl)
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py\h5f.pyx", line 78, in h5py.h5f.open
OSError: Unable to open file (unable to open file: name = '..\snapshots\resnet50_coco_best_v2.1.0.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)
Could you please help me fix the issue?

Make sure that you have the file referenced downloaded and that it is in the same folder as your current python script. Then change the last line to:
model = load_model('filename', backbone_name='resnet50')

Related

OsError : Unable to open file in pixellib

I'm trying to change background of image using pixellib library but am encountering problem. I'm getting this error:
Traceback (most recent call last): File
"C:/Users/FEd/PycharmProjects/pythonProject/PIXEL.py", line 16, in
change_bg.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5")
File
"C:\Users\FEd\PycharmProjects\pythonProject\venv\lib\site-packages\pixellib\tune_bg.py",
line 43, in load_pascalvoc_model
self.model.load_weights(model_path) File "C:\Users\FEd\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\keras\engine\training.py",
line 2319, in load_weights
with h5py.File(filepath, 'r') as f: File "C:\Users\FEd\PycharmProjects\pythonProject\venv\lib\site-packages\h5py_hl\files.py",
line 427, in init
swmr=swmr) File "C:\Users\FEd\PycharmProjects\pythonProject\venv\lib\site-packages\h5py_hl\files.py",
line 190, in make_fid
fid = h5f.open(name, flags, fapl=fapl) File "h5py_objects.pyx", line 54, in h5py._objects.with_phil.wrapper File
"h5py_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py\h5f.pyx", line 96, in h5py.h5f.open OSError: Unable to open
file (unable to open file: name =
'deeplabv3_xception_tf_dim_ordering_tf_kernels.h5', errno = 2, error
message = 'No such file or directory', flags = 0, o_flags = 0)
Have used os.getcwd to get the current working directory but still am getting same error. Have found this link states i should use os.getcwd to get current working directory
Code
import pixellib
from pixellib.tune_bg import alter_bg
import os
my_dir = os.getcwd()
print(my_dir)
m1 = my_dir+"\sample.jpg"
m2 = my_dir+"\change_background.jpg"
print(m1)
print(m2)
change_bg = alter_bg()
change_bg.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5")
change_bg.change_bg_img(f_image_path =m1, b_image_path = m2, output_image_name="new_img.jpg")
print("done")

Cannot load BERT from local disk

I am trying to use Huggingface transformer api to load a locally downloaded M-BERT model but it is throwing an exception.
I clone this repo: https://huggingface.co/bert-base-multilingual-cased
bert = TFBertModel.from_pretrained("input/bert-base-multilingual-cased")
The directory structure is:
But I am getting this error:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_utils.py", line 1277, in from_pretrained
missing_keys, unexpected_keys = load_tf_weights(model, resolved_archive_file, load_weight_prefix)
File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_utils.py", line 467, in load_tf_weights
with h5py.File(resolved_archive_file, "r") as f:
File "/usr/local/lib/python3.7/dist-packages/h5py/_hl/files.py", line 408, in __init__
swmr=swmr)
File "/usr/local/lib/python3.7/dist-packages/h5py/_hl/files.py", line 173, in make_fid
fid = h5f.open(name, flags, fapl=fapl)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py/h5f.pyx", line 88, in h5py.h5f.open
OSError: Unable to open file (file signature not found)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "train.py", line 81, in <module>
__main__()
File "train.py", line 59, in __main__
model = create_model(num_classes)
File "/content/drive/My Drive/msc-project/code/model.py", line 26, in create_model
bert = TFBertModel.from_pretrained("input/bert-base-multilingual-cased")
File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_utils.py", line 1280, in from_pretrained
"Unable to load weights from h5 file. "
OSError: Unable to load weights from h5 file. If you tried to load a TF 2.0 model from a PyTorch checkpoint, please set from_pt=True.
Where am I going wrong?
Need help!
Thanks in advance.
As it was already pointed in the comments - your from_pretrained param should be either id of a model hosted on huggingface.co or a local path:
A path to a directory containing model weights saved using
save_pretrained(), e.g., ./my_model_directory/.
See documentation
Looking at your stacktrace it seems like your code is run inside:
/content/drive/My Drive/msc-project/code/model.py so unless your model is in:
/content/drive/My Drive/msc-project/code/input/bert-base-multilingual-cased/ it won't load.
I would also set the path to be similar to documentation example ie:
bert = TFBertModel.from_pretrained("./input/bert-base-multilingual-cased/")

PDF to Image converter code error in Python

Unable to run the code in VS code with python version 3.8.1:-
Coming with below error in terminal
pdftojpg.py
import os
import tempfile
#import cv2
from pdf2image import convert_from_path
filename = 'E:/djcodes/pyproject/ID.pdf'
with tempfile.TemporaryDirectory() as path:
images_from_path = convert_from_path(filename, output_folder=path, last_page=1, first_page =0)
base_filename = os.path.splitext(os.path.basename(filename))[0] + '.jpg'
save_dir = 'E:/djcodes/pyproject/image'
for page in images_from_path:
page.save(os.path.join(save_dir, base_filename), 'JPEG')
#img = cv2.imread(save_dir+os.path.splitext(os.path.basename(filename))[0]+'jpeg')
Terminal error:-
Traceback (most recent call last):
File "c:\Users\asus\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\ptvsd_launcher.py", line 48, in <module>
main(ptvsdArgs)
File "c:\Users\asus\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 432, in main
run()
File "c:\Users\asus\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 300, in run_file
setup_connection()
File "c:\Users\asus\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 288, in setup_connection
if not daemon.wait_for_launch(addr):
File "c:\Users\asus\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\runner.py", line 61, in wait_for_launch
_, start_session = self.start_client(addr)
File "c:\Users\asus\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\daemon.py", line 217, in start_client
connect(client, addr)
File "c:\Users\asus\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\socket.py", line 197, in connect
sock.connect(addr)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Please help to resolve the issue

Python 3.5: Open an image with underscore in file name?

I have a script (seen below) that opens a collection of images. It took me a while to figure this out, but I realized that the script can't seem to open any of the image files with an underscore in the name. An example of my image directory is as follows:
Images\img1.jpg
Images\img2.jpg
Images\img3_c.jpg
The script will behave as expected when dealing with "normal" image file names. The moment the script has to open a file with an underscore in the name, it returns an error as follows:
OSError: cannot identify image file <_io.BufferedReader name='TrainingSets\\CottonWoolSpots\\0006_c.jpg'>
I would like to avoid changing my file names if possible. Any help is appreciated.
Script:
import numpy as np
from skimage import io
import glob
import os
def importAllImgs():
imagePath = glob.glob("TrainingSets\CottonWoolSpots\*.jpg")
im_coll = io.ImageCollection(imagePath)
im_array = []
for i in range(len(im_coll)):
image = im_coll[i]
im_array.append(image)
return im_array
if __name__ == "__main__":
testArray = importAllImgs()
EDIT FULL STACKTRACE + ERROR:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 734, in debugfile
debugger.run("runfile(%r, args=%r, wdir=%r)" % (filename, args, wdir))
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\bdb.py", line 431, in run
exec(cmd, globals, locals)
File "<string>", line 1, in <module>
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
execfile(filename, namespace)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "//mspm1bnas50s/home58/biegad1/python scripts/batchimportimgs_0_1.py", line 54, in <module>
testArray = importAllImgs()
File "//mspm1bnas50s/home58/biegad1/python scripts/batchimportimgs_0_1.py", line 36, in importAllImgs
image = im_coll[i]
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\skimage\io\collection.py", line 264, in __getitem__
self.data[idx] = self.load_func(self.files[n], **kwargs)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\skimage\io\_io.py", line 61, in imread
img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\skimage\io\manage_plugins.py", line 211, in call_plugin
return func(*args, **kwargs)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\skimage\io\_plugins\pil_plugin.py", line 36, in imread
im = Image.open(f)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\PIL\Image.py", line 2309, in open
% (filename if filename else fp))
OSError: cannot identify image file <_io.BufferedReader name='TrainingSets\\CottonWoolSpots\\0006_c.jpg'>
OKAY! So. It appears that I've been working with corrupted image files the entire time, hence the errors.

What does this capistrano error mean when doing an AWS file fetch via cap tasks?

In my deploy/development.rb, I have the following line:-
set :env_path, 's3api get-object --bucket <bucket_name> --key <file_name> <file_name>'
In my cap file, I have the following task defined:
task :download_configs do
on roles(:all) do
within release_path do
execute :aws, "#{fetch(:env_path)}"
end
end
# on roles(:db) do
# within release_path do
# execute :aws, "#{fetch(:env_path)}"
# end
# end
end
But on cap deploy,I am seeing the following errors:-
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as ubuntu#54.213.226.117: aws exit status: 1
aws stdout: Traceback (most recent call last):
File "/usr/bin/aws", line 15, in <module>
import awscli.clidriver
File "/usr/share/awscli/awscli/clidriver.py", line 30, in <module>
from awscli.help import ProviderHelpCommand
File "/usr/share/awscli/awscli/help.py", line 20, in <module>
from docutils.core import publish_string
File "/usr/lib/python3/dist-packages/docutils/core.py", line 20, in <module>
from docutils import frontend, io, utils, readers, writers
File "/usr/lib/python3/dist-packages/docutils/frontend.py", line 41, in <module>
import docutils.utils
File "/usr/lib/python3/dist-packages/docutils/utils/__init__.py", line 20, in <module>
import docutils.io
File "/usr/lib/python3/dist-packages/docutils/io.py", line 18, in <module>
from docutils.utils.error_reporting import locale_encoding, ErrorString, ErrorOutput
File "/usr/lib/python3/dist-packages/docutils/utils/error_reporting.py", line 47, in <module>
locale_encoding = locale.getlocale()[1] or locale.getdefaultlocale()[1]
File "/usr/lib/python3.4/locale.py", line 556, in getdefaultlocale
return _parse_localename(localename)
File "/usr/lib/python3.4/locale.py", line 484, in _parse_localename
raise ValueError('unknown locale: %s' % localename)
ValueError: unknown locale: UTF-8
aws stderr: Nothing written
SSHKit::Command::Failed: aws exit status: 1
aws stdout: Traceback (most recent call last):
File "/usr/bin/aws", line 15, in <module>
import awscli.clidriver
File "/usr/share/awscli/awscli/clidriver.py", line 30, in <module>
from awscli.help import ProviderHelpCommand
File "/usr/share/awscli/awscli/help.py", line 20, in <module>
from docutils.core import publish_string
File "/usr/lib/python3/dist-packages/docutils/core.py", line 20, in <module>
from docutils import frontend, io, utils, readers, writers
File "/usr/lib/python3/dist-packages/docutils/frontend.py", line 41, in <module>
import docutils.utils
File "/usr/lib/python3/dist-packages/docutils/utils/__init__.py", line 20, in <module>
import docutils.io
File "/usr/lib/python3/dist-packages/docutils/io.py", line 18, in <module>
from docutils.utils.error_reporting import locale_encoding, ErrorString, ErrorOutput
File "/usr/lib/python3/dist-packages/docutils/utils/error_reporting.py", line 47, in <module>
locale_encoding = locale.getlocale()[1] or locale.getdefaultlocale()[1]
File "/usr/lib/python3.4/locale.py", line 556, in getdefaultlocale
return _parse_localename(localename)
File "/usr/lib/python3.4/locale.py", line 484, in _parse_localename
raise ValueError('unknown locale: %s' % localename)
ValueError: unknown locale: UTF-8
aws stderr: Nothing written
Please help me identify what is going wrong here!
Shot in the dark (I'm not a Python dev), but you might be able add the following to deploy.rb:
set :default_env, {
'LANG' => 'en_US.UTF-8'
}
Derived mostly from https://github.com/matplotlib/matplotlib/issues/5420#issuecomment-154454607
I would check to see if you can run the command manually on the server. If you cannot, then figure out how to without Capistrano. If you can, perhaps see if there is some configuration in .bashrc or .bash_profile that can be added to the Capistrano config.

Categories