text stringlengths 0 4.99k |
|---|
x = layers.SeparableConv2D( |
NUM_KEYPOINTS, kernel_size=5, strides=1, activation=\"relu\" |
)(x) |
outputs = layers.SeparableConv2D( |
NUM_KEYPOINTS, kernel_size=3, strides=1, activation=\"sigmoid\" |
)(x) |
return keras.Model(inputs, outputs, name=\"keypoint_detector\") |
Our custom network is fully-convolutional which makes it more parameter-friendly than the same version of the network having fully-connected dense layers. |
get_model().summary() |
Model: \"keypoint_detector\" |
_________________________________________________________________ |
Layer (type) Output Shape Param # |
================================================================= |
input_2 (InputLayer) [(None, 224, 224, 3)] 0 |
_________________________________________________________________ |
tf.math.truediv (TFOpLambda) (None, 224, 224, 3) 0 |
_________________________________________________________________ |
tf.math.subtract (TFOpLambda (None, 224, 224, 3) 0 |
_________________________________________________________________ |
mobilenetv2_1.00_224 (Functi (None, 7, 7, 1280) 2257984 |
_________________________________________________________________ |
dropout (Dropout) (None, 7, 7, 1280) 0 |
_________________________________________________________________ |
separable_conv2d (SeparableC (None, 3, 3, 48) 93488 |
_________________________________________________________________ |
separable_conv2d_1 (Separabl (None, 1, 1, 48) 2784 |
================================================================= |
Total params: 2,354,256 |
Trainable params: 96,272 |
Non-trainable params: 2,257,984 |
_________________________________________________________________ |
Notice the output shape of the network: (None, 1, 1, 48). This is why we have reshaped the coordinates as: batch_keypoints[i, :] = np.array(kp_temp).reshape(1, 1, 24 * 2). |
Model compilation and training |
For this example, we will train the network only for five epochs. |
model = get_model() |
model.compile(loss=\"mse\", optimizer=keras.optimizers.Adam(1e-4)) |
model.fit(train_dataset, validation_data=validation_dataset, epochs=EPOCHS) |
Epoch 1/5 |
166/166 [==============================] - 85s 486ms/step - loss: 0.1087 - val_loss: 0.0950 |
Epoch 2/5 |
166/166 [==============================] - 78s 471ms/step - loss: 0.0830 - val_loss: 0.0778 |
Epoch 3/5 |
166/166 [==============================] - 78s 468ms/step - loss: 0.0778 - val_loss: 0.0739 |
Epoch 4/5 |
166/166 [==============================] - 78s 470ms/step - loss: 0.0753 - val_loss: 0.0711 |
Epoch 5/5 |
166/166 [==============================] - 78s 468ms/step - loss: 0.0735 - val_loss: 0.0692 |
<tensorflow.python.keras.callbacks.History at 0x7f3ac55b6050> |
Make predictions and visualize them |
sample_val_images, sample_val_keypoints = next(iter(validation_dataset)) |
sample_val_images = sample_val_images[:4] |
sample_val_keypoints = sample_val_keypoints[:4].reshape(-1, 24, 2) * IMG_SIZE |
predictions = model.predict(sample_val_images).reshape(-1, 24, 2) * IMG_SIZE |
# Ground-truth |
visualize_keypoints(sample_val_images, sample_val_keypoints) |
# Predictions |
visualize_keypoints(sample_val_images, predictions) |
png |
png |
Predictions will likely improve with more training. |
Going further |
Try using other augmentation transforms from imgaug to investigate how that changes the results. |
Here, we transferred the features from the pre-trained network linearly that is we did not fine-tune it. You are encouraged to fine-tune it on this task and see if that improves the performance. You can also try different architectures and see how they affect the final performance. |
Implementation of classical Knowledge Distillation. |
Introduction to Knowledge Distillation |
Knowledge Distillation is a procedure for model compression, in which a small (student) model is trained to match a large pre-trained (teacher) model. Knowledge is transferred from the teacher model to the student by minimizing a loss function, aimed at matching softened teacher logits as well as ground-truth labels. |
The logits are softened by applying a \"temperature\" scaling function in the softmax, effectively smoothing out the probability distribution and revealing inter-class relationships learned by the teacher. |
Reference: |
Hinton et al. (2015) |
Setup |
import tensorflow as tf |
from tensorflow import keras |
from tensorflow.keras import layers |
import numpy as np |
Construct Distiller() class |
The custom Distiller() class, overrides the Model methods train_step, test_step, and compile(). In order to use the distiller, we need: |
A trained teacher model |
A student model to train |
A student loss function on the difference between student predictions and ground-truth |
A distillation loss function, along with a temperature, on the difference between the soft student predictions and the soft teacher labels |
An alpha factor to weight the student and distillation loss |
An optimizer for the student and (optional) metrics to evaluate performance |
In the train_step method, we perform a forward pass of both the teacher and student, calculate the loss with weighting of the student_loss and distillation_loss by alpha and 1 - alpha, respectively, and perform the backward pass. Note: only the student weights are updated, and therefore we only calculate the gradients ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.