Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 它在Keras、Classifier.Fit_生成器中显示错误_Python_Tensorflow_Machine Learning_Keras_Deep Learning - Fatal编程技术网

Python 它在Keras、Classifier.Fit_生成器中显示错误

Python 它在Keras、Classifier.Fit_生成器中显示错误,python,tensorflow,machine-learning,keras,deep-learning,Python,Tensorflow,Machine Learning,Keras,Deep Learning,当我尝试运行此代码时,在classifier.fit_生成器行中出现错误 “零误差”。这有点像“整数除零或模零” 它只在第一个纪元中给出错误 找到0个属于0个类的图像。 纪元1/5 即使为图像提供了正确的路径,以便使用flow\u from\u目录。您必须具有以下文件夹结构 # Convolutional Neural Network # Installing Theano # pip install --upgrade --no-deps git+git://github.com/Theano

当我尝试运行此代码时,在classifier.fit_生成器行中出现错误 “零误差”。这有点像“整数除零或模零”

它只在第一个纪元中给出错误

找到0个属于0个类的图像。 纪元1/5


即使为图像提供了正确的路径

,以便使用
flow\u from\u目录
。您必须具有以下文件夹结构

# Convolutional Neural Network
# Installing Theano
# pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git
# Installing Tensorflow
# pip install tensorflow
# Installing Keras
# pip install --upgrade keras
# Part 1 - Building the CNN
# Importing the Keras libraries and packages

    from keras.models import Sequential
    from keras.layers import Conv2D
    from keras.layers import MaxPooling2D
    from keras.layers import Flatten
    from keras.layers import Dense

# Initialising the CNN

    classifier = Sequential()

# Step 1 - Convolution

    classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))

# Step 2 - Pooling

    classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Adding a second convolutional layer

    classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
    classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Step 3 - Flattening

    classifier.add(Flatten())

# Step 4 - Full connection

    classifier.add(Dense(units = 128, activation = 'relu'))
    classifier.add(Dense(units = 3, activation = 'softmax'))

# Compiling the CNN

    classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# Part 2 - Fitting the CNN to the images

    from keras.preprocessing.image import ImageDataGenerator
    train_datagen = ImageDataGenerator(rescale = 1./255,shear_range = 0.2,zoom_range = 0.2,horizontal_flip = True)
    test_datagen = ImageDataGenerator(rescale = 1./255)
    training_set = train_datagen.flow_from_directory('E:\Major Project\Data\Wheat',target_size = (64, 64),batch_size = 32,class_mode = 'categorical')
    test_set = test_datagen.flow_from_directory('E:\Major Project\Data\Wheat1',target_size = (64, 64),batch_size = 32,class_mode = 'categorical')
    classifier.fit_generator(training_set,steps_per_epoch = 100,epochs = 5,validation_data = test_set,validation_steps = 200)
其中文件夹_i包含类i的图像

在路径
E:\Major Project\Data
中,必须有
n
文件夹,每个文件夹对应于每个类

然后您可以从_目录调用flow _,如下所示

train\u datagen.flow\u from\u目录('E:\Major Project\Data\',target\u size=(64,64),batch\u size=32,class\u mode='categorical')

您将得到如下输出

./Dataset/
    ./Train/
        ../Folder_1/
            ../img_1.jpg
            ../img_2.jpg
            ............
        ../Folder_2/
            ../img_1.jpg
            ../img_2.jpg
如果其他一切都正确,模型将开始训练

培训后,如果您想使用
flow\u from\u directory
predict\u generator
的帮助下进行预测,您可以这样做

您可以将flow_中batch_size的值从_目录从默认值(即batch_size=32)更改为batch_size=1。然后将predict_generator的步骤设置为测试图像的总数

Found xxxx images belonging to yyyy classes
如果您想在单个图像上进行预测

test_datagen = ImageDataGenerator(rescale=1./255)

test_generator = test_datagen.flow_from_directory(
        test_dir,
        target_size=(200, 200),
        color_mode="rgb",
        shuffle = False,
        class_mode='categorical',
        batch_size=1)

filenames = test_generator.filenames
nb_samples = len(filenames)

predict = model.predict_generator(test_generator,steps = nb_samples)

你能显示文件夹结构吗?请包括完整的错误回溯。你能解释一下预测部分是如何工作的吗?至于预测,我使用了从keras导入的np作为np。预处理导入图像test\u image=image.load\u img('E:/Major Project/Data/Wheaty.jpg',target\u size=(64,64))test\u image=image.img\u to_数组(test\u image)test_image=np.expand_dims(test_image,axis=0)result=classifier.predict(test_image)training_set.class_index#{‘番茄’:0,‘大米’:1,‘小麦’:2}if int(result[0][0])==1:prediction=‘番茄’elif int(result[0][1])==1:prediction=‘大米’否则:prediction=‘小麦’print(prediction)@HimanshuBaghmar您想使用来自目录的流量进行预测。?不,对于预测,我不想使用来自目录的流量,我想测试单个图像如果解决了问题,考虑接受答案
import cv2
import numpy as np

img = cv2.imread('path_to_file')
img = cv2.resize(img, (64, 64))

img = img.reshape(1, 64, 64, 3)

model.predict(img)