Search code examples
tensorflowscikit-learnkerasautoencoderanomaly-detection

DeepLearning Anomaly Detection for images


I am still relatively new to the world of Deep Learning. I wanted to create a Deep Learning model (preferably using Tensorflow/Keras) for image anomaly detection. By anomaly detection I mean, essentially a OneClassSVM.

I have already tried sklearn's OneClassSVM using HOG features from the image. I was wondering if there is some example of how I can do this in deep learning. I looked up but couldn't find one single code piece that handles this case.


Solution

  • The way of doing this in Keras is with the KerasRegressor wrapper module (they wrap sci-kit learn's regressor interface). Useful information can also be found in the source code of that module. Basically you first have to define your Network Model, for example:

    def simple_model():
        #Input layer
        data_in = Input(shape=(13,)) 
        #First layer, fully connected, ReLU activation
        layer_1 = Dense(13,activation='relu',kernel_initializer='normal')(data_in)   
        #second layer...etc
        layer_2 = Dense(6,activation='relu',kernel_initializer='normal')(layer_1)  
        #Output, single node without activation
        data_out = Dense(1, kernel_initializer='normal')(layer_2)     
        #Save and Compile model
        model = Model(inputs=data_in, outputs=data_out)   
        #you may choose any loss or optimizer function, be careful which you chose 
        model.compile(loss='mean_squared_error', optimizer='adam')
        return model
    

    Then, pass it to the KerasRegressor builder and fit with your data:

    from keras.wrappers.scikit_learn import KerasRegressor
    #chose your epochs and batches 
    regressor = KerasRegressor(build_fn=simple_model, nb_epoch=100, batch_size=64)
    #fit with your data
    regressor.fit(data, labels, epochs=100)
    

    For which you can now do predictions or obtain its score:

    p = regressor.predict(data_test) #obtain predicted value
    score = regressor.score(data_test, labels_test) #obtain test score
    

    In your case, as you need to detect anomalous images from the ones that are ok, one approach you can take is to train your regressor by passing anomalous images labeled 1 and images that are ok labeled 0.

    This will make your model to return a value closer to 1 when the input is an anomalous image, enabling you to threshold the desired results. You can think of this output as its R^2 coefficient to the "Anomalous Model" you trained as 1 (perfect match).

    Also, as you mentioned, Autoencoders are another way to do anomaly detection. For this I suggest you take a look at the Keras Blog post Building Autoencoders in Keras, where they explain in detail about the implementation of them with the Keras library.


    It is worth noticing that Single-class classification is another way of saying Regression.

    Classification tries to find a probability distribution among the N possible classes, and you usually pick the most probable class as the output (that is why most Classification Networks use Sigmoid activation on their output labels, as it has range [0, 1]). Its output is discrete/categorical.

    Similarly, Regression tries to find the best model that represents your data, by minimizing the error or some other metric (like the well-known R^2 metric, or Coefficient of Determination). Its output is a real number/continuous (and the reason why most Regression Networks don't use activations on their outputs). I hope this helps, good luck with your coding.