Build a Real-Time Facial Expression Recognition Service with UCloud AI-as-a-Service

This guide walks you through training an Inception‑V3 model on the FER2013 dataset with TensorFlow 1.1, packaging the model, and deploying a scalable facial expression recognition API using UCloud's AI‑as‑a‑Service platform, including performance testing against GPU benchmarks.

UCloud Tech
UCloud Tech
UCloud Tech
Build a Real-Time Facial Expression Recognition Service with UCloud AI-as-a-Service

In recent years AI technology has become ubiquitous, from news recommendation to facial recognition. UCloud’s AI‑as‑a‑Service (UAI‑Service) offers a cheap, reliable, elastic, and easy‑to‑use online AI platform that frees users from complex development and operations.

Implementation Steps

The process consists of two parts: training the model with TensorFlow 1.1.0 and deploying the service using UAI‑Service.

Step 1: Model Training

Step 1.0 Install TensorFlow 1.1.0

Environment: Ubuntu 14.04.5, Python 2.7.6. Install via pip:

bash
pip install tensorflow=1.1.0

Step 1.1 Choose Dataset

UCloud uses the public FER2013 facial expression dataset (35,887 grayscale 48×48 images, 7 classes). The data is split into training, validation, and test sets.

Step 1.2 Data Preprocessing

TF‑Slim is used to convert the dataset to TFRecord format. The label mapping is stored in labels.txt. Example data layout:

Step 1.3 Training

The Inception‑V3 model is fine‑tuned on FER2013. Training command:

bash
TRAIN_DIR=./train_log
DATASET_DIR=./fer2013
PRETRAINED_CHECKPOINT_DIR=./pretrain_model

python train_image_classifier.py \
  --train_dir=${TRAIN_DIR} \
  --dataset_name=fer2013 \
  --dataset_split_name=train \
  --dataset_dir=${DATASET_DIR} \
  --model_name=inception_v3 \
  --checkpoint_path=${PRETRAINED_CHECKPOINT_DIR}/inception_v3.ckpt \
  --checkpoint_exclude_scopes=InceptionV3/Logits,InceptionV3/AuxLogits \
  --trainable_scopes=InceptionV3/Logits,InceptionV3/AuxLogits \
  --max_number_of_steps=1000 \
  --batch_size=32 \
  --learning_rate=0.01 \
  --learning_rate_decay_type=fixed \
  --save_interval_secs=60 \
  --save_summaries_secs=60 \
  --log_every_n_steps=100 \
  --optimizer=rmsprop \
  --weight_decay=0.00004

The system keeps the five latest checkpoints for recovery. Example checkpoint files:

Step 2: Deploy Online Service

After training, the model is packaged and deployed.

Step 2.0 Install UFile SDK and UAI SDK

UFile SDK (object storage) – used to upload model and code files.

UAI SDK – provides command‑line tools for service deployment.

Step 2.1 Write Inference Code

Example fer_inference.py:

python
# fer_inference.py
import numpy as np
import tensorflow as tf

from PIL import Image
from inception_v3 import *
from uai.arch.tf_model import TFAiUCloudModel

class FerModel(TFAiUCloudModel):
  def __init__(self, conf):
    super(FerModel, self).__init__(conf)

  def load_model(self):
    sess = tf.Session()
    input_tensor = tf.placeholder(tf.float32, [None, 299, 299, 3])
    arg_scope = inception_v3_arg_scope()
    with slim.arg_scope(arg_scope):
      logits, end_points = inception_v3(input_tensor, is_training=False, num_classes=7)
      saver = tf.train.Saver()
    params_file = tf.train.latest_checkpoint(self.model_dir)
    saver.restore(sess, params_file)
    self.output['sess'] = sess
    self.output['input_tensor'] = input_tensor
    self.output['logits'] = logits
    self.output['end_points'] = end_points

  def execute(self, data, batch_size):
    sess = self.output['sess']
    input_tensor = self.output['input_tensor']
    logits = self.output['logits']
    end_points = self.output['end_points']
    ims = []
    for i in range(batch_size):
      im = Image.open(data[i]).resize((299, 299))
      im = np.array(im) / 255.0
      im = im.reshape(299, 299, 3)
      ims.append(im)
    ims = np.array(ims)
    predict_values, logit_values = sess.run([end_points['Predictions'], logits], feed_dict={input_tensor: ims})
    ret = []
    for val in predict_values:
      ret_val = np.array_str(np.argmax(val)) + '
'
      ret.append(ret_val)
    return ret

Step 2.2 Package and Upload Model & Code

Directory layout (illustrated below):

Model checkpoint directory:

Packaging command:

bash
python tf_deploy.py pack --public_key=MY_PUBLIC_KEY --private_key=MY_PRIVATE_KEY --bucket=MY_BUCKET --pack_file_path=/Users/littleape1022/Desktop/fer_uaiservice --main_file=fer_inference --main_class=FerModel --model_dir=checkpoint_dir --code_files=fer_inference.py,inception_v3.py,inception_utils.py --upload_name=fer_uaiservice.tar --ai_arch_v=tensorflow-1.1.0

Step 2.3 Create Service

bash
python tf_deploy.py create  --public_key=MY_PUBLIC_KEY --private_key=MY_PRIVATE_KEY --service_name=fer_uaiservice --cpu=8 --memory=8

Creation response (example):

Step 2.4 Deploy Service

bash
python tf_deploy.py deploy --service_id=uaiservice-av4p1c --public_key=MY_PUBLIC_KEY --private_key=MY_PRIVATE_KEY --ai_arch_v=tensorflow-1.1.0 --ufile_url="MY_UFILE_URL" --pip=pillow

Deployment response (example):

The service URL is returned with status “ToStart”. After starting, the URL can be used to access the API.

Step 2.5 Start Service

bash
python tf_deploy.py start --public_key=MY_PUBLIC_KEY --private_key=MY_PRIVATE_KEY --service_name=fer_uaiservice --service_version=SERVICE_VERSION --paas_id=Srv_PAAS_ID

Start response (example):

Testing

After completing the steps, the online facial expression recognition service is operational.

URL Test

Access the service URL from a cloud host; the result shows the input image “happy.jpg” classified as class “4” (neutral), indicating room for accuracy improvement.

Online Service Performance Test

Using ApacheBench (ab) the service performance was compared with local testing and a K80 GPU. Results:

With 8 concurrent requests, the AI online service performance approaches that of a single K80 GPU.

Under concurrency, the AI service outperforms an 8‑core, 8 GB cloud host.

AI is a key component of UCloud’s “CBA” strategy. The AI‑as‑a‑Service platform enables rapid productization of AI algorithms while providing comprehensive resource management and scheduling guarantees.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AIModel DeploymentTensorFlowFacial Expression RecognitionUCloud
UCloud Tech
Written by

UCloud Tech

UCloud is a leading neutral cloud provider in China, developing its own IaaS, PaaS, AI service platform, and big data exchange platform, and delivering comprehensive industry solutions for public, private, hybrid, and dedicated clouds.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.