内容简介:Image classification is a handbook example of deep learning applications. The standard way of making a classification model involves a preprocessing step where the human-readable class labels (eg.: ‘car’, ‘person’, ‘cat’) are changed to machine-ready numbe
Save classification labels and top confidences in a custom layer using Keras
Mar 11 ·4min read
Image classification is a handbook example of deep learning applications. The standard way of making a classification model involves a preprocessing step where the human-readable class labels (eg.: ‘car’, ‘person’, ‘cat’) are changed to machine-ready numbers (eg.: 0,1,2). The most common way of doing this is to map the list of possible classes with their indices. Of course, this requires a postprocessing step as well, where the result is converted to the expected form. A common way is to store the label of the class with the highest score and the score (a widely used term for this is confidence ).
In this story, I will show an example of storing the labels in the model using a custom layer at the end of the model. The preferred output of my model is a list of the top k labels and their confidence scores. The formated output can be useful in production where the product is a model, and the labels have to be stored inside the model. Or, when the list of the labels changes with each iteration of models.
Input: image(W,H,C) Outputs: labels(k) string, confidences(k) float
Training a Classification Model
For this story, I will use a simple classification model. This Colab notebook shows an example of a classifier trained on the Fashion MNIST dataset (trained on 60000 images and tested on 10000). The model expects 28x28x1 grayscale images and returns with a softmax probability of 10 classes. The list of the class labels are:
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
A straightforward way to handle the output of the model can be a simple mapping: finding the index with the most score and use the label of that index.
class_names[np.argmax(predictions[0])]
to get the label of the image 0.
Building a Custom Layer
To understand how to make a custom layer in Keras, I suggest reading the original documentation in Keras and TensorFlow .
I want to implement a custom layer that stores the labels
and a topn
value, so the output of the layer can be the top n confidence labels and their scores. For this, we have to overwrite the __init__
, the call
, compute_output_shape
and get_config
functions of the layer.
Init
In the init function, we store the constructor parameters as the class’ field. Don’t forget to call the parent class’ constructor! super(LabelLimitLayer, self).__init__(**kwargs)
Call
The call function expects the output of the previous layer as an input and calculates the top_k
classes and their labels. For this, I used TensorFlow functions.
To create a (?,len(labels))
shaped tensor from the list of labels, we first create a tensor using the list (parameter of our custom class) and then expand it using the shape of the previous layer’s output (we extract the batch_size
from it). The steps are:
tf_labels = tf.constant([self.labels], dtype=”string”)
string type tensor
tf_labels = tf.tile(tf_labels,[batch_size,1])
expanding to get (?,1) dynamic shape handling the batches.
To select the top k scores, I used the corresponding TensorFlow function. We store the indices so we can map the labels for those indices as well as the confidence values.
top_k = tf.nn.top_k(x, k=self.topn, sorted=True, name=”top_k”).indices
To get the values of a tensor using indices from another tensor, I use the tf.gather function.
top_conf = tf.gather(x, top_k, batch_dims=1) top_labels = tf.gather(tf_labels, top_k, batch_dims=1)
Finally, the layer returns with the last two tensors.
return [top_conf, top_labels]
Compute_output_shape
Because of the two output tensor, the Keras layer can’t automatically compute the output shape. Fortunately, it can be calculated as follows:
top_shape = (batch_size, self.topn) return [top_shape, top_shape]
Get_config
To serialize the custom layer (when saving a model), one has to update the config with the values of the class parameters. Don’t forget to add the superclass’ config to the dictionary!
Adding the Output Layer to the Model
In this example, I added a custom layer at the end of the base classification model, with labels ( class_names
) and top_k value ( 2
).
label_layer = LabelLimitLayer(class_names, 2)(base_model.output) label_model = Model(base_model.input, label_layer)
Saving and Loading Model
Finally, to save a model, we can use the Keras model’s save function. To load a model with a custom layer, we have to define this custom layer at the custom_objects
parameter.
label_model.save(‘test.h5’) restored_model = keras.models.load_model(“test.h5”, custom_objects={“LabelLimitLayer”:LabelLimitLayer})
Summary
This snippet shows how to use Keras custom layers to create string labels as the output of a model. The story also uses top_k to keep only the relevant classes. The corresponding codes are available at this Colab notebook .
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
C++标准模板库编程实战
Ivor Horton / 郭小虎、程聪 / 2017-1
《C++标准模板库编程实战》介绍最新的C++14标准的API、库和扩展,以及如何将它们运用到C++14程序中。在书中,作者Ivor Horton 则阐述了什么是STL,以及如何将它们应用到程序中。我们将学习如何使用容器、迭代器,以及如何定义、创建和应用算法。此外,还将学习函数对象和适配器,以及它们的用法。 阅读完本书之后,你将能够了解如何扩展STL,如何定义自定义类型的C++组件,你还将能够......一起来看看 《C++标准模板库编程实战》 这本书的介绍吧!