[Resolved] ValueError: zero-size array to reduction operation minimum which has no identity

Problem description

Taking over the previous bug problem, the details are as follows:

Traceback (most recent call last):
File “embed.py”, line 159, in
main()
File “embed.py”, line 138, in main
(x_wm, y_wm), defense = defense_config.embed(defense=defense,
File “/home/visionx/anaconda3/envs/watermark/lib/python3.8/site-packages/mlconfig/config.py”, line 20, in __call__
return self.instantiate(*args, **kwargs)
File “/home/visionx/anaconda3/envs/watermark/lib/python3.8/site-packages/mlconfig/config.py”, line 65, in instantiate
return func_or_cls(*args, **kwargs)
File “/home/visionx/project/Watermark-Robustness-Toolbox/wrt/defenses/watermark/adi.py”, line 188, in wm_adi_embed
return defense.embed(**kwargs), defense
File “/home/visionx/project/Watermark-Robustness-Toolbox/wrt/defenses/watermark/adi.py”, line 111, in embed
train_and_wm_loader = train_loader.add_numpy_data(x_wm, y_wm, boost_factor=boost_factor)
File “/home/visionx/project/Watermark-Robustness-Toolbox/wrt/training/datasets/wrt_data_loader.py”, line 125, in add_numpy_data
if np.min(x) < 0 or np.max(x) > 1:
File “<__array_function__ internals>“, line 200, in amin
File “/home/visionx/anaconda3/envs/watermark/lib/python3.8/site-packages/numpy/core/fromnumeric.py”, line 2946, in amin
return _wrapreduction(a, np.minimum, ‘min’, axis, None, out,
File “/home/visionx/anaconda3/envs/watermark/lib/python3.8/site-packages/numpy/core/fromnumeric.py”, line 86, in _wrapreduction
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
ValueError: zero-size array to reduction operation minimum which has no identity

Cause analysis and solutions

This error usually occurs because when performing some kind of summary operation (such as minimum value, maximum value, average value, etc.), the array being operated on is a zero-size array, and this operation does not support operations on zero-size arrays.

To resolve this issue, you can take the following steps:

  1. Check data: First, make sure the input data array is not an empty array. You can use conditional statements to check if the array is empty before performing the aggregation operation.

  2. Handling special cases: If your code logic needs to handle zero-sized arrays, you can add conditional statements to handle this situation. For example, you could check the size of an array before performing a min operation and skip the min operation if the array is empty.

Preliminary analysis

Find the error location:

Print the corresponding data, but find that the corresponding x is a null value, then there must be no way to find the corresponding minimum or maximum value! Let’s see where this x comes from?

Continue to look for

ok, now you understand that the null value is loaded when loading the data, so where is the loaded data?

Continue analysis

At this time, you will find that the information given above

url = "https://www.dropbox.com/s/z11ds7jvewkgv18/adi.zip?dl=1"

But when we clicked on this, we found that the share had been deleted by the author, so let’s have a private chat with the author and ask for it:

At this time we downloaded it and needed to change it to a local address. After changing it, we found:

Not useful at all!!!

Then the problem is not loaded here, that’s fun, it must be somewhere else, but where exactly is it? Recall that we used the corresponding yaml file when loading data, then the yaml file must have the corresponding corresponding Configuration file.

In-depth analysis

See, we just need to unzip the adi.zip above and put it in the ~/.wrt/data directory

At this time, if we continue to operate, we will find other errors:

New error: IsADirectoryError: [Errno 21] Is a directory: ‘/home/visionx/.wrt/data/adi/pics’

This is a bit strange. Why is this error reported? Let’s take a look at the detailed error message:

Traceback (most recent call last):
File “embed.py”, line 159, in
main()
File “embed.py”, line 138, in main
(x_wm, y_wm), defense = defense_config.embed(defense=defense,
File “/home/visionx/anaconda3/envs/watermark/lib/python3.8/site-packages/mlconfig/config.py”, line 20, in __call__
return self.instantiate(*args, **kwargs)
File “/home/visionx/anaconda3/envs/watermark/lib/python3.8/site-packages/mlconfig/config.py”, line 65, in instantiate
return func_or_cls(*args, **kwargs)
File “/home/visionx/project/Watermark-Robustness-Toolbox/wrt/defenses/watermark/adi.py”, line 188, in wm_adi_embed
return defense.embed(**kwargs), defense
File “/home/visionx/project/Watermark-Robustness-Toolbox/wrt/defenses/watermark/adi.py”, line 110, in embed
x_wm, y_wm = self.keygen(wm_loader, num_classes=self.num_classes, keylength=keylength)
File “/home/visionx/project/Watermark-Robustness-Toolbox/wrt/defenses/watermark/adi.py”, line 67, in keygen
x_wm = collect_n_samples(n=keylength, data_loader=wm_loader, has_labels=False)
File “/home/visionx/project/Watermark-Robustness-Toolbox/wrt/training/datasets/utils.py”, line 53, in collect_n_samples
for x in data_loader:
File “/home/visionx/anaconda3/envs/watermark/lib/python3.8/site-packages/torch/utils/data/dataloader.py”, line 630, in __next__
data = self._next_data()
File “/home/visionx/anaconda3/envs/watermark/lib/python3.8/site-packages/torch/utils/data/dataloader.py”, line 1345, in _next_data
return self._process_data(data)
File “/home/visionx/anaconda3/envs/watermark/lib/python3.8/site-packages/torch/utils/data/dataloader.py”, line 1371, in _process_data
data.reraise()
File “/home/visionx/anaconda3/envs/watermark/lib/python3.8/site-packages/torch/_utils.py”, line 694, in reraise
raise exception
IsADirectoryError: Caught IsADirectoryError in DataLoader worker process 0.
Original Traceback (most recent call last):
File “/home/visionx/anaconda3/envs/watermark/lib/python3.8/site-packages/torch/utils/data/_utils/worker.py”, line 308, in _worker_loop
data = fetcher.fetch(index)
File “/home/visionx/anaconda3/envs/watermark/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py”, line 51, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File “/home/visionx/anaconda3/envs/watermark/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py”, line 51, in data = [self.dataset[idx] for idx in possibly_batched_index]
File “/home/visionx/project/Watermark-Robustness-Toolbox/wrt/training/datasets/trigger_datasets.py”, line 26, in __getitem__
image = Image.open(os.path.join(self.root, img_name)).convert(‘RGB’)

File “/home/visionx/anaconda3/envs/watermark/lib/python3.8/site-packages/PIL/Image.py”, line 3218, in open
fp = builtins.open(filename, “rb”)
IsADirectoryError: [Errno 21] Is a directory: ‘/home/visionx/.wrt/data/adi/pics’

The key point is the above sentence (highlighted and blackmarked), find the corresponding code

We will find that this is scanning pictures directly in the root directory, so it is definitely wrong to have folders in the folder. Be bold and make some changes to change all the adi file directories into pictures, that is, unzip other directories and put them in Just go to the upper directory and see the result:

ok, this way there won’t be any other problems

Finished with flowers

See the results:

It’s great to be able to run.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 57158 people are learning the system