Efficient way to know if an image related to a dataset that was used to train convolutional neural network
<p>Currently I'm using VGG16 + Keras + Theano thought the Transfer Learning methodology to recognize plants classes. It works just fine and gives me a good accuracy. But the next problem I'm trying to solve - is to find a way of identifying if an input image contains plant at all. I don't want to have another one classifier that will do it, because it's not really efficiently.</p>

<p>So I did some search and have found that we can get activations from the latest model layer (before activation layer) and analyze it.</p>



<pre class="lang-py prettyprint-override"><code>from keras import backend as K

model = util.load_model() # VGG16 model
model.load_weights(path_to_weights)

def get_activations(m, layer, X_batch):
    x = [m.layers[0].input, K.learning_phase()]
    y = [m.get_layer(layer).output]
    get_activations = K.function(x, y)
    activations = get_activations([X_batch, 0])

    # trying to get some features from activations
    # to understand how can we identify if an image is relevant
    for l in activations[0]:
        not_nulls = [x for x in l if x &gt; 0]

        # shows percentage of activated neurons
        c1 = float(len(not_nulls)) / len(l)
        n_activated = len(not_nulls)
        print 'c1:{}, n_activated:{}'.format(c1, n_activated)

    return activations

get_activations(model, 'the_latest_layer_name', inputs)
</code></pre>

<p>From the above code I've noticed that when we have very irrelevant image, the number of activated neurons is bigger than for images that contain plants:</p>

<ol>
<li>For images that was using for model training, number of activated neurons 19%-23%</li>
<li>For images that contain unknown plants species 20%-26%</li>
<li>For irrelevant images 24%-28%</li>
</ol>

<p>It's not really a good feature to understand if an image relevant as percentage values are intersect.</p>

<p>So, is there a good way to resolve this issue?</p>