简体   繁体   中英

one class classification with keras

I am trying to build a model to detect whether the input image is something or not(For example, dog or not). I'm coding with keras, but the accuracy is terrible. Do you have any idea to tune this correctly? Or should I use other tools other than keras for one class classification problem? Thank you so much in advance.

Here's the code and the output I've wrote so far.

train_dir = './path/to/train_dir'
vali_dir = './path/to/validation_dir'

train_datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=False)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        train_dir, 
        target_size=(150, 150), 
        batch_size=20,
        class_mode='binary')

vali_datagen = ImageDataGenerator(rescale=1./255)

vali_generator = vali_datagen.flow_from_directory(
        vali_dir,
        target_size=(150, 150),
        batch_size=20,
        class_mode='binary')

model = Sequential()
model.add(Conv2D(16, 3, activation='relu', input_shape=(150, 150, 3)))
model.add(MaxPool2D(pool_size=2))
model.add(Conv2D(32, 3, activation='relu'))
model.add(MaxPool2D(pool_size=2))
model.add(Conv2D(64, 3, activation='relu'))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))

model.compile(
        loss='binary_crossentropy',
        optimizer=RMSprop(lr=0.003),
        metrics=['acc']
)

history = model.fit_generator(
        train_generator,
        steps_per_epoch=100,
        epochs=8,
        verbose=2,
        validation_data=vali_generator,
        validation_steps=20
)

Output:

Found 3379 images belonging to 2 classes.
Found 607 images belonging to 2 classes.
Epoch 1/8
 - 136s - loss: 7.6617 - acc: 0.5158 - val_loss: 10.5220 - val_acc: 0.3400
Epoch 2/8
 - 124s - loss: 7.7837 - acc: 0.5118 - val_loss: 10.5220 - val_acc: 0.3400
.......and this is just terrible.

Looks like there is a problem with class labels - are they related to data correctly? You can check it or post ImageDataGenerator code

There is a big difference between train accuracy and validation accuracy even from the first epoch. To me it looks like an overtraining problem. So you should give you network some more regularization. Like more Dropoutlayers or kernel_regularizer inside you convolution layers.

I tried to change and tune the parameter and the training data but I didn't get a desirable result. I came across with one class classification using Isolation forest . This is known as novelty detection, and after I used it, it performs outstandingly. Thanks for those who advised me in the comments, and I'm sorry to answer on my own.

Isolation forest is a good algorithm for anomaly detection, if there is no dependencies among the input features. However, if your input is a time series signal or image, it is better to use methods like RNN or CNN.

I have recently come across an anomaly detection model named one class CNN. It is working great if your input is image or time series signal. Here is a link to their github:

https://github.com/raghavchalapathy/oc-nn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM