KeyError: "Unable to open object (object 'imgs' doesn't exist)" - python

I am trying to run a source code from a Keras tutorial for image recognition. I'm getting this error,
Traceback (most recent call last):
File "ty.py", line 52, in <module>
X, Y = hf['imgs'][:], hf['labels'][:]
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 "C:\Users\alams\Anaconda3\envs\tensorflow\lib\site-
packages\h5py\_hl\group.py", line 167, in __getitem__
oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
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\h5o.pyx", line 190, in h5py.h5o.open
KeyError: "Unable to open object (object 'imgs' doesn't exist)"
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "ty.py", line 66, in <module>
label = get_class(img_path)
File "ty.py", line 48, in get_class
return int(img_path.split('/')[-2])
ValueError: invalid literal for int() with base 10: 'Final_Training'
This is my source code:
def get_class(img_path):
return int(img_path.split('/')[-2])
try:
with h5py.File('X.h5') as hf:
X, Y = hf['imgs'][:], hf['labels'][:]
except (IOError,OSError, KeyError):
root_dir = 'Data/Final_Training/Images/'
imgs = []
labels = []
all_img_paths = glob.glob(os.path.join(root_dir, '*/*.ppm'))
np.random.shuffle(all_img_paths)
for img_path in all_img_paths:
try:
img = preprocess_img(io.imread(img_path))
label = get_class(img_path)
imgs.append(img)
labels.append(label)
except (IOError, OSError):
print('missed', img_path)
pass
X = np.array(imgs, dtype='float32')
Y = np.eye(NUM_CLASSES, dtype='uint8')[labels]
with h5py.File('X.h5','w') as hf:
hf.create_dataset('imgs', data=X)
hf.create_dataset('labels', data=Y)
I tried to run this code by removing the int Conversion from the return of the first function. But seems like all the values are not write in X.h5

You have defined img=[] inside except block (locally). That's why it doesn't have access outside the block. Define it outside the block.
def get_class(img_path):
return int(img_path.split('/')[-2])
imgs=[]
labels=[]
#Your code

Related

KeyError: Caught KeyError in DataLoader worker process 0

I am facing this error which is related to the dictionary of data loader for (MSVD dataset) while running training file for video_captioning on https://github.com/nasib-ullah/video-captioning-models-in-Pytorch
//////////////////////////////////////////////////////
code of getitem function :
def __getitem__(self,idx):
anno = random.choice(self.annotation_dict[self.v_name_list[idx]])
anno_index = []
for word in anno.split(' '):
try:
anno_index.append(self.voc.word2index[word])
except:
pass
if self.opt_truncate_caption:
if len(anno_index)> self.max_caption_length:
anno_index = anno_index[:self.max_caption_length]
anno_index = anno_index + [self.voc.cfg.EOS_token]
appearance_tensor = torch.tensor(self.appearance_feature_dict[self.v_name_list[idx]]).float()
if self.motion_feature_dict == None:
motion_tensor = torch.zeros_like(appearance_tensor)
else:
motion_tensor = torch.tensor(self.motion_feature_dict[self.v_name_list[idx]]).float()
if self.object_feature_dict == None:
object_tensor = torch.zeros_like(appearance_tensor)
else:
object_tensor = torch.tensor(self.object_feature_dict[self.v_name_list[idx]]).float()
return appearance_tensor,anno_index, self.v_name_list[idx],motion_tensor,object_tensor
Traceback (most recent call last):
File "/home/adel/Downloads/video-captioning-models-in-Pytorch-main/untitled0.py", line 84, in <module>
loss_train,ac_loss = model.train_epoch(train_loader,utils)
File "/home/adel/Downloads/video-captioning-models-in-Pytorch-main/models/MARN/model.py", line 321, in train_epoch
for data in dataloader:
File "/home/adel/anaconda3/envs/nasib/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 435, in __next__
data = self._next_data()
File "/home/adel/anaconda3/envs/nasib/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1085, in _next_data
return self._process_data(data)
File "/home/adel/anaconda3/envs/nasib/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1111, in _process_data
data.reraise()
File "/home/adel/anaconda3/envs/nasib/lib/python3.8/site-packages/torch/_utils.py", line 428, in reraise
raise self.exc_type(msg)
KeyError: Caught KeyError in DataLoader worker process 0.
Original Traceback (most recent call last):
File "/home/adel/anaconda3/envs/nasib/lib/python3.8/site-packages/torch/utils/data/_utils/worker.py", line 198, in _worker_loop
data = fetcher.fetch(index)
File "/home/adel/anaconda3/envs/nasib/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/home/adel/anaconda3/envs/nasib/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/home/adel/Downloads/video-captioning-models-in-Pytorch-main/data.py", line 88, in __getitem__
appearance_tensor = torch.tensor(self.appearance_feature_dict[self.v_name_list[idx]]).float()
KeyError: 'vid1'
////////////////////////////////////////////////////////
keys of dictionary
Keys: <KeysViewHDF5 ['vid1', 'vid10', 'vid100', 'vid1000', 'vid1001', 'vid1002', 'vid1003', 'vid1004', 'vid1005', 'vid1006', ...
<HDF5 dataset "vid1": shape (28, 1536), type "<f4">

Exception in Tkinter callback, KeyError: 0

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\range.py", line 351, in get_loc
return self._range.index(new_key)
ValueError: 0 is not in range
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "<ipython-input-18-518ef7ebbd84>", line 4, in myClick2
a.plot(p1.total_payoff()[0],p1.total_payoff()[1])
File "<ipython-input-10-57724152e4a6>", line 59, in total_payoff
prices = self.option_payoff()[0]
File "<ipython-input-10-57724152e4a6>", line 47, in option_payoff
temppayoff += callpayoff(i,j.get_strike(),j.find_bidask()[1])*j.get_quantity()
File "<ipython-input-6-59b6ad5c0680>", line 28, in find_bidask
bid = data['bid'][0]
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py", line 853, in __getitem__
return self._get_value(key)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py", line 961, in _get_value
loc = self.index.get_loc(label)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\range.py", line 353, in get_loc
raise KeyError(key) from err
KeyError: 0
This is the error message I am getting when I try to execute a function by clicking a TKinter button. The function is below, basically it takes some data (x, y), and plots it with matplotlib.
def myClick2():
f = Figure(figsize=(4,4), dpi=100)
a = f.add_subplot(111)
a.plot(p1.total_payoff()[0],p1.total_payoff()[1])
a.grid(True, which='both')
a.axhline(y=0, color='k')
a.axvline(x=0, color='k')
canvas = FigureCanvasTkAgg(f, master=root)
canvas.draw()
canvas.get_tk_widget().grid(row = 8, column = 1)
The error is saying that "total_payoff", which calls "option_payoff", which calls "find_bidask" is leading to the error. Specifically, the part which I assign bid = data['bid'][0].
def find_bidask(self):
if str.upper(self.cp) == 'C':
data = self.data['calls']
else:
data = self.data['puts']
data = data[data['contractSymbol']==self.symbol].reset_index(drop=True)
bid = data['bid'][0]
ask = data['ask'][0]
However, when I run this separately outside of TKinter, it produces no error, and ['bid'][0] is available as a value. I don't understand what is wrong with my code - is it something in the tkinter myclick2 function that is wrong?

Code runs fine in normal mode but throws exception in debug mode?

My code runs fine in normal mode but when i try to run it in debug mode it throws exception, I am trying to create a TF-IDF matrix of genre column in spotify_df. Why is this happening.
P.S - genre column of spotify_df contains 1-D list of strings and none of them is empty.
Code:
tracks_df = pd.read_csv('tracks_with_genres_v3.csv')
def create_tf_idf(spotify_df):
tfidf = TfidfVectorizer()
tfidf_matrix = tfidf.fit_transform(spotify_df['genres'].apply(lambda x: " ".join(x)))
genre_df = pd.DataFrame(tfidf_matrix.toarray())
genre_df.columns = ['genre' + "|" + i for i in tfidf.get_feature_names_out()]
genre_df.reset_index(drop=True, inplace=True)
return genre_df
def create_feature_set(spotify_df):
genre_tf_idf = create_tf_idf(spotify_df)
complete_feature_set = create_feature_set(tracks_df)
Exception:
Traceback (most recent call last):
File "D:\PyCharm\Music_Recommendation_v2\venv\lib\site-packages\numpy\core\getlimits.py", line 516, in __init__
self.dtype = numeric.dtype(int_type)
TypeError: 'NoneType' object is not callable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2022.1.1\plugins\python\helpers\pydev\pydevd.py", line 1491, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2022.1.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:/PyCharm/Music_Recommendation_v2/main.py", line 220, in <module>
begin("Heartless", "Weeknd")
File "D:/PyCharm/Music_Recommendation_v2/main.py", line 179, in begin
complete_feature_set = create_feature_set(tracks_df)
File "D:/PyCharm/Music_Recommendation_v2/main.py", line 115, in create_feature_set
genre_tf_idf = create_tf_idf(spotify_df)
File "D:/PyCharm/Music_Recommendation_v2/main.py", line 95, in create_tf_idf
tfidf_matrix = tfidf.fit_transform(spotify_df['genres'].apply(lambda x: " ".join(x)))
File "D:\PyCharm\Music_Recommendation_v2\venv\lib\site-
packages\sklearn\feature_extraction\text.py", line 2068, in fit_transform
X = super().fit_transform(raw_documents)
File "D:\PyCharm\Music_Recommendation_v2\venv\lib\site-
packages\sklearn\feature_extraction\text.py", line 1328, in fit_transform
vocabulary, X = self._count_vocab(raw_documents, self.fixed_vocabulary_)
File "D:\PyCharm\Music_Recommendation_v2\venv\lib\site-
packages\sklearn\feature_extraction\text.py", line 1222, in _count_vocab
if indptr[-1] > np.iinfo(np.int32).max: # = 2**31 - 1
File "D:\PyCharm\Music_Recommendation_v2\venv\lib\site-packages\numpy\core\getlimits.py",
line 518, in __init__
self.dtype = numeric.dtype(type(int_type))
TypeError: 'NoneType' object is not callable

error on search image in python image_match library

I'm using python image_match library. I need to use search_image method of this library. but when I se this method I got the below error:
Traceback (most recent call last):
File "/var/www/html/Panel/test2.py", line 16, in <module>
ses.search_image('https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg/687px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg')
File "/usr/local/lib/python3.10/site-packages/image_match/signature_database_base.py", line 268, in search_image
transformed_record = make_record(img, self.gis, self.k, self.N)
File "/usr/local/lib/python3.10/site-packages/image_match/signature_database_base.py", line 356, in make_record
signature = gis.generate_signature(path)
File "/usr/local/lib/python3.10/site-packages/image_match/goldberg.py", line 161, in generate_signature
im_array = self.preprocess_image(path_or_image, handle_mpo=self.handle_mpo, bytestream=bytestream)
File "/usr/local/lib/python3.10/site-packages/image_match/goldberg.py", line 257, in preprocess_image
return rgb2gray(image_or_path)
File "/usr/local/lib/python3.10/site-packages/skimage/_shared/utils.py", line 394, in fixed_func
return func(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/skimage/color/colorconv.py", line 875, in rgb2gray
rgb = _prepare_colorarray(rgb)
File "/usr/local/lib/python3.10/site-packages/skimage/color/colorconv.py", line 140, in _prepare_colorarray
raise ValueError(msg)
ValueError: the input array must have size 3 along `channel_axis`, got (1024, 687)
Can you please help me?

Handling exceptions in while loop - Python

Here is my code (almost full version for #cdhowie :)):
def getResult(method, argument=None):
result = None
while True:
print('### loop')
try:
print ('### try hard...')
if argument:
result = method(argument)
else:
result = method()
break
except Exception as e:
print('### GithubException')
if 403 == e.status:
print('Warning: ' + str(e.data))
print('I will try again after 10 minutes...')
else:
raise e
return result
def getUsernames(locations, gh):
usernames = set()
for location in locations:
print location
result = getResult(gh.legacy_search_users, location)
for user in result:
usernames.add(user.login)
print user.login,
return usernames
# "main.py"
gh = Github()
locations = ['Washington', 'Berlin']
# "main.py", line 12 is bellow
usernames = getUsernames(locations, gh)
The problem is, that exception is raised, but I can't handle it. Here is an output:
### loop
### try hard...
Traceback (most recent call last):
File "main.py", line 12, in <module>
usernames = getUsernames(locations, gh)
File "/home/ciembor/projekty/github-rank/functions.py", line 39, in getUsernames
for user in result:
File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py", line 33, in __iter__
newElements = self.__grow()
File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py", line 45, in __grow
newElements = self._fetchNextPage()
File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/Legacy.py", line 37, in _fetchNextPage
return self.get_page(page)
File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/Legacy.py", line 48, in get_page
None
File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/Requester.py", line 69, in requestAndCheck
raise GithubException.GithubException(status, output)
github.GithubException.GithubException: 403 {u'message': u'API Rate Limit Exceeded for 11.11.11.11'}
Why it doesn't print ### handling exception?
Take a close look at the stack trace in the exception:
Traceback (most recent call last):
File "main.py", line 12, in <module>
usernames = getUsernames(locations, gh)
File "/home/ciembor/projekty/github-rank/functions.py", line 39, in getUsernames
for user in result:
File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py", line 33, in __iter__
newElements = self.__grow()
...
The exception is being thrown from code being called by the line for user in result: after getResult finishes executing. This means that the API you're using is using lazy evaluation, so the actual API request doesn't quite happen when you expect it to.
In order to catch and handle this exception, you'll need to wrap the code inside the getUsernames function with a try/except handler.

Categories

Resources