内容简介: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 .
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
免费:商业的未来
Chris Anderson / 中信出版集团 / 2015-10-1 / 35.40
《免费》,这是一个商业模式不断被颠覆、被改写的时代。一种商业模式既可以统摄未来市场,也可以挤垮当前市场——在我们这个现代经济社会里,这并不是一件不可能的事情。“免费”就是这样的一种商业模式,它代表了互联网时代的商业未来。 “免费”商业模式是一种建立在以电脑字节为基础上的经济学,而非过去建立在物理原子基础上的经济学。在原子经济中,随着时间的推移,我们周围的物品都在逐渐升值。但是在字节经济的网络......一起来看看 《免费:商业的未来》 这本书的介绍吧!
JS 压缩/解压工具
在线压缩/解压 JS 代码
Base64 编码/解码
Base64 编码/解码