Skip to content

Tutorial

Pedro Antonio edited this page Sep 22, 2023 · 1 revision

Classify images of clothing

This is inspired by the tensorflow basic classification tutorial. It trains a local binary neural network model to classify images of clothing.

Import libraries

In addition to common development libraries using tensorflow, it is necessary to import some classes from lbccn.py, as follows:

import tensorflow as tf
from tensorflow import keras
import numpy as np
from lbcnn import *

Import the Fashion MNIST dataset

This tutorial also uses Fashion MNIST dataset, the tensorflow basic classification tutorial explains the dataset.

You can import and load directly from tensorflow.

fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

Ensure that the data has the same type as the anchor weights, let's use float32.

train_images = train_images.astype('float32')
test_images = test_images.astype('float32')

The dataset doesn't have the class names, so store in a list for later use.

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

2D convolutions require 4D inputs. The Fashion MNIST images have 3 dimensions, so you need to reshape the images dataset to apply it to a local binary convolution neural network.

train_reshaped = np.reshape(train_images, (train_images.shape[0], train_images.shape[1], train_images.shape[2], 1))
test_reshaped = np.reshape(test_images, (test_images.shape[0], test_images.shape[1], test_images.shape[2], 1))

Build the model

First define the anchor weights used in the first step of a local binary convolutional layer. It must only have 3 possible values: -1, 0 and 1.

anchor_weights = np.array(
            [
                [[[1,0,0,0,1,0,0,0]],[[1,0,0,0,0,1,0,0]],[[1,0,0,0,0,0,1,0]],[[1,0,0,0,0,0,0,1]]],
                [[[0,1,0,0,1,0,0,0]],[[0,1,0,0,0,1,0,0]],[[0,1,0,0,0,0,1,0]],[[0,1,0,0,0,0,0,1]]],
                [[[0,0,1,0,1,0,0,0]],[[0,0,1,0,0,1,0,0]],[[0,0,1,0,0,0,1,0]],[[0,0,1,0,0,0,0,1]]],
                [[[0,0,0,1,1,0,0,0]],[[0,0,0,1,0,1,0,0]],[[0,0,0,1,0,0,1,0]],[[0,0,0,1,0,0,0,1]]]
                
            ], dtype=np.float32
        )

You can use LBC2D just like any other tensorflow layer, it works similar to CONV2D. Use the anchor weights as a parameter and include the layer in keras.Sequential.

To better understand how it works, check out the documentation and this article.

lbcnn = keras.Sequential([
    LBC2D(anchor_weights, padding='SAME'),
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10, activation='softmax')
])

The compilation works normally, there is no need for any changes. Add optimizer, loss function, metrics and others of your choice.

lbcnn.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

Train the model

Finaly, run lbcnn.fit and collect your metrics with lbcnn.evaluate

lbcnn.fit(train_reshaped, train_labels, epochs=10)

test_loss, test_acc = lbcnn.evaluate(test_reshaped,  test_labels, verbose=2)

print('\nTest accuracy:', test_acc)

As you can see, LBC2D works perfectly with tf.keras.Sequential. This happens because LBC2D inherits from tf.keras.layers.Layer, so we can use it like any other layer.

Clone this wiki locally