[英]Why am I getting and how to solve ValueError: zero-size array to reduction operation maximum which has no identity
I am making a image-classification CNN and I am trying to create a h5py file with the preprocessed images but I get a ValueError, which says "zero-size array to reduction operation maximum which has no identity".我正在制作一个图像分类 CNN,我正在尝试使用经过预处理的图像创建一个 h5py 文件,但我得到一个 ValueError,上面写着“零大小数组到没有标识的最大缩减操作”。 Can someone please help me: I don't have loads of experience with python and coding so I might not fully understand what people explain.有人可以帮助我吗:我在 python 和编码方面没有丰富的经验,所以我可能不完全理解人们的解释。 This is the code: '''decalre image properties这是代码:'''decalre image properties
IMG_WIDTH = 64
IMG_HEIGHT = 64
CHANNELS=3
preprocess_images = ()
'''declare some training hyperparameters'''
BATCH_SIZE = 50
EPOCHS = 100
import cv2
import numpy as np
from glob import glob
import matplotlib.pyplot as plt
import sys
from PIL import Image
import os
import random
import keras
from keras.utils import to_categorical
import h5py
import json
import tensorflow as tf
import keras
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import json
import sklearn.datasets
from keras.models import Sequential,Input,Model
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import LeakyReLU
from keras.regularizers import l2
#declare the directories that hold the training and test data
train_dir='/content/gdrive/MyDrive/Images/recycling'
test_dir ='/content/gdrive/MyDrive/Images/non-recyclable'
to_h5py(train_dir)
to_h5py(test_dir)
the issue is with the function 'to_h5py'but I don't know how too solve it.问题出在 function 'to_h5py' 但我不知道如何解决它。
def to_h5py(pth):
#get list of folders and classes
(folders,classes)= get_folders_and_classes(pth)
#set the file name that the dataset will be saved under
input_fname = 'processed_datasets/' +get_end_slash(pth)
#checks if this particular file already exists and asks the user if it should be overwritten
if (os.path.isfile(input_fname)):
inp= input('overwrite file' + input_fname + '?, y/n')
if inp.lower()=="y":
print("file will be overwritten")
os.remove(input_fname)
elif inp.lower()=="n":
input_fname = input("enter a new filename: ") + '.h5'
print(input_fname)
else:
print("incorrect input, preprocessing failed")
return
#create h5py file to store dataset
hf=h5py.File(input_fname)
#get list of all images and number of images
all_images = glob(pth+"**/*.jpg", recursive=True)
n_images = len(all_images)
#create dataset X and label list
X=hf.create_dataset(
name="X",
shape=(n_images, IMG_WIDTH, IMG_HEIGHT, CHANNELS),
maxshape=(None,IMG_WIDTH,IMG_HEIGHT,None),
compression="gzip",
compression_opts=9)
label_lis = []
#set an index to iterate through
x_ind=0
#go through all the folders
for i, folder in enumerate(folders):
images = glob(folder+"*.jpg")
total_images = len(images)
print(classes[i], total_images)
#process each image in each folder and add the class and the processed image to the image array list
for i, image_pth in enumerate(images):
img= process_single_img(image_pth, IMG_WIDTH, IMG_HEIGHT)
X[x_ind] = img
label_lis.append(i)
print("{}/{} fname={}".format(i,total_images, get_pic_name(image_pth)))
x_ind+=1
#store the labels under the y set
hf.create_dataset(
name= 'y',
compression="gzip",
compression_opts=9,
data=label_lis)
#convert the labels to one-hot values (i.e. 2 -> [0 0 1 0 0]) if there were 5 possible values)
y_one_hot = to_categorical(np.array(label_lis))
hf.create_dataset(
name= 'y_one_hot',
compression="gzip",
compression_opts=9,
data=y_one_hot)
#close the opened file
hf.close()
''' UPDATE: this is the full traceback thank you for any help '''更新:这是完整的回溯谢谢你的帮助
I can reproduce the error message with:我可以通过以下方式重现错误消息:
In [213]: np.maximum.reduce([])
Traceback (most recent call last):
File "<ipython-input-213-73052b120c74>", line 1, in <module>
np.maximum.reduce([])
ValueError: zero-size array to reduction operation maximum which has no identity
or with np.max([])
.或使用np.max([])
。
But without the full traceback we can't help you identify where the error occurs.但是如果没有完整的追溯,我们无法帮助您确定错误发生的位置。 Who is taking the max
and why is the array in question size 0?谁在取max
,为什么有问题的数组大小为 0?
Unfortunately we see a lot of questions like this.不幸的是,我们看到很多这样的问题。 People with little programming experience trying to use fairly advanced code without much understanding of what is going on.几乎没有编程经验的人试图使用相当高级的代码,而对正在发生的事情没有太多了解。 Or how to do basic debugging:(或者如何进行基本调试:(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.