【问题标题】:Keras creating three classes instead of twoKeras 创建三个类而不是两个类
【发布时间】:2019-03-07 15:59:38
【问题描述】:

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

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

模型分类代码:

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.

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

datareal
        valid
                forest
                fire
        train
                forest
                fire

如何让模型只标记 2 个类而不是 3 个?

【问题讨论】:

    标签: python python-2.7 tensorflow keras linode


    【解决方案1】:

    我想通了。问题是我的火车和验证文件夹中仍然有一些隐藏文件夹。

    【讨论】:

    • 很好的修复+1。我注意到 Jupyter 在训练和验证目录中有一个隐藏目录 .ipynb_checkpoints
    • @user3177617 你是怎么解决这个问题的?
    【解决方案2】:

    对于仍在 colab 中苦苦挣扎的人: 额外的文件是隐藏的,因此首先你必须找到它。你可以这样写:

    import os
    import shutil
    
    os.listdir("the_target_directory_trining_for_example")
    

    那么结果将是隐藏文件夹的名称以及您的文件夹(类)的名称。

    ['class2', 'classs1', '.ipynb_checkpoints']
    

    接下来,您必须使用以下行删除添加的文件夹(.ipynb_checkpoints),但要小心使用它。

    shutil.rmtree("the_target_directory_trining_for_example/.ipynb_checkpoints")
    

    【讨论】:

      猜你喜欢
      • 2020-05-06
      • 2015-02-22
      • 2016-04-28
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多