Keras创建了三个类,而不是两个类 [英] Keras creating three classes instead of two

查看:75
本文介绍了Keras创建了三个类,而不是两个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试训练一个模型,以识别包含火灾VS包含森林的图像的图像.我正在使用Linode在远程服务器上训练模型.我正在使用Python 2.7和Ubuntu 16.04.5.

I am trying to train a model to identify images containing fire VS images that contain forests. I am training the model on a remote server using Linode. I am using Python 2.7 and Ubuntu 16.04.5.

当我在本地或Jupyter笔记本中运行以下代码时,它将创建2个类,但是当我想在服务器上运行时,它将创建3个类.

When i run the following code locally or in Jupyter notebooks it will create 2 classes, but when i want to run it on the server it creates 3 classes.

对模型进行分类的代码:

The code that classifies the model:

def onehot(x): return np.array(OneHotEncoder().fit_transform(x.reshape(-1,1)).todense())


model = keras.applications.InceptionV3(weights='imagenet', include_top=False)


batch_size=16

train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)


train_generator = train_datagen.flow_from_directory(
        'datareal/train',  # this is the target directory
        batch_size=batch_size,
        target_size=(224, 224),
        class_mode='binary')

test_datagen = ImageDataGenerator(rescale=1./255)

validation_generator = test_datagen.flow_from_directory(
        'datareal/valid',
        batch_size=batch_size,
        target_size=(224, 224),
        class_mode='binary')


x = model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer -- let's say we have 2 classes
predictions = Dense(2, activation='softmax')(x)
newmodel = Model(inputs=model.input, outputs=predictions)

newmodel.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])

输出:

Found 173 images belonging to 3 classes.
Found 40 images belonging to 3 classes.

包含所有图像的目录的结构如下:

The directory containing all the images is structured as follows:

datareal
        valid
                forest
                fire
        train
                forest
                fire

我如何使模型仅标记2个类而不是3个?

How do i get the model to label only 2 classes instead of 3?

推荐答案

我知道了.问题是我的火车和验证文件夹中仍然有一些隐藏的文件夹.

I figured it out. the problem was that i still had some hidden folders in my train and validation folder.

这篇关于Keras创建了三个类,而不是两个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆