Python 我为什么得到和如何解决ValueError:零大小数组到没有标识的最大缩减操作

Python 我为什么得到和如何解决ValueError:零大小数组到没有标识的最大缩减操作,python,h5py,Python,H5py,我正在制作一个图像分类CNN,我试图用预处理的图像创建一个h5py文件,但我得到一个值错误,它说“从零大小数组到最大缩减操作,没有标识”。有人能帮助我吗:我没有python和编码方面的丰富经验,所以我可能不完全理解人们的解释。代码如下: ''贴花图像属性 IMG_WIDTH = 64 IMG_HEIGHT = 64 CHANNELS=3 preprocess_images = () '''declare some training hyperparameters''' BATCH_SIZE =

我正在制作一个图像分类CNN,我试图用预处理的图像创建一个h5py文件,但我得到一个值错误,它说“从零大小数组到最大缩减操作,没有标识”。有人能帮助我吗:我没有python和编码方面的丰富经验,所以我可能不完全理解人们的解释。代码如下: ''贴花图像属性

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)
问题在于函数“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()
'''
感谢您的帮助

我可以通过以下方式重现错误消息:

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
[213]中的
:np.maximum.reduce([])
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
np.最大.减少([])
ValueError:从零大小数组到没有标识的最大缩减操作
或者使用
np.max([])

但是如果没有完整的回溯,我们无法帮助您确定错误发生的位置。谁在取
max
,为什么有问题的数组大小为0


不幸的是,我们看到很多这样的问题。几乎没有编程经验的人试图使用相当高级的代码,却不太了解正在发生的事情。或者如何进行基本调试:(

谢谢,但是有什么我可以特别做的吗?错误的原因(我想)这是因为python认为train_dir中没有图像,但是有,并且文件路径可以工作。我不确定这一点,但我已经研究了其他问题,并得出了结论。如果是这样的话,问题不在于计算,而在于加载,可能还有您自己的文件和目录结构。这不是我们可以解决的问题n来自远方的帮助!此实用程序应该从图像创建一个HDF5文件。您是否得到一个文件?其中是否有任何数据集(和数据)?应该有两个数据集:“y”和“y”。我在跟踪从
def到h 5py(pth)开始的代码时遇到问题:
。它的格式/缩进是否正确?所有代码是否都是函数
to_h5py()
的一部分?另外,函数
get_folders_和_classes()的定义在哪里?请清理或澄清。格式正确:def get_folders_和_classes(pth):folders=glob(pth+“*”)。classes=[get_end_斜杠(f)for f in folders]返回(文件夹、类)出于某种原因,从某一点来说,编辑器认为没有任何图像,但在此之前,代码显示在我提供的目录路径中存在图像。