Customize Classification Model Output Layer

栏目: IT技术 · 发布时间: 5年前

内容简介: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

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

Customize Classification Model Output Layer

Photo by David Rangel on Unsplash

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.

Customize Classification Model Output Layer

Fashion MNIST examples (by Zalando, MIT License).

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.

Custom layer with Labels and Top_k selection

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)

Customize Classification Model Output Layer

Predicting labels on the Fashion MNIST dataset

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 .


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

长尾理论2.0

长尾理论2.0

安德森 / 乔江涛、石晓燕 / 中信出版社 / 2009-5 / 42.00元

《长尾理论2.0》是克里斯·安德森对所有问题最明确的回答。在此书中,他详细阐释了长尾的精华所在,揭示了长尾现象是如何从工业资本主义原动力——规模经济与范围经济——的矛盾中产生出来的。长尾现象虽然是明显的互联网现象,但其商务逻辑本身,却是从工业经济中自然而然“长”出来的,网络只是把酝酿了几十年的供应链革命的诸多要素简单地结合在一起了。同时,长尾理论转化为行动,最有力、最可操作的就是营销长尾,通过口碑......一起来看看 《长尾理论2.0》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

URL 编码/解码
URL 编码/解码

URL 编码/解码