NUDITY DETECTION MODEL

Stage 1: Preprocessing Data

Our input images have varied resolution while a model often requires fixed-size inputs so that we need to preprocess them to have a uniform size for our data. To do that, we take two bellow steps:

  • Dropping 87.5 per cent of the central region of image
  • Resizing them into the size $34 \times 34 \times 3$

In [ ]:
%matplotlib inline
%load_ext autoreload
%autoreload 2

import tensorflow as tf
from scipy.misc import imread, imresize
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
from Dataset.data import preprocess_image

image = tf.placeholder("uint8", [None, None, 3])
result_image = preprocess_image(image, 34, 34)
raw_image = imread('/home/taivu/workspace/NudityDetection/Dataset/train/normal/34.jpg')

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    result_img = sess.run(result_image, feed_dict={image:raw_image})

################ Plot the raw image and the processed image ##########################
gs = gridspec.GridSpec(1, 2, width_ratios=[3, 2]) 

fig = plt.figure()
a1 = fig.add_subplot(gs[0])
a1.set_title("Raw image")
plt.imshow(raw_image)
a2 = fig.add_subplot(gs[1])
a2.set_title("Processed image")
plt.imshow(result_img, shape =(34, 34))
plt.show()

Stage 2: Build a model

The model consists of 6 hidden layers including 2 convolutional layers, 2 pool layers, and 2 fully-connected layers. The detail of model are shown in the bellow figure

Stage 2.1: Training the model

  • To training this model, we used a training set with 4000 images with the ratio of nudity to normal images is 1:1. In additional, we alse estimated the model in the training process using a validation set with 2000 images with the ratio is similar to the training.
  • Cross-entropy is the loss function to assesses the difference between the predicted labels of model and the real labels of samples. Its notation: $L$ $$ L = - \log\left ( \frac{e^{f_y}}{\sum_{j}e^{f_j}} \right ) $$ in which, $f_y$ is the activation of neuron that present the real class of a sample
  • Mini-batch Gradient Descent algorithm is used to optimize the weights of the model $$\mathit{w}_{t} = \mathit{w}_{t-1} - \alpha \frac{1}{m} \frac{\partial L}{\partial w}$$ in which $\mathit{w}_{t}$ is the weights of model at time $t$ of the optimizing process.
  • The hyper-parameters of model such as the number of images in each mini-batch $m$, learning rate $\alpha$ are set empirically

In [ ]:
import tensorflow as tf
from vng_train import train

# Do train the model
train()

Stage 2.2: Run the model

  • After the training process, the trained weights of model are saved into a hard drive to reuse in the future
  • To run the model, we need to re-construct the model and then load the trained weights into it so that the model will not optimize again its weights in this stage. After the input images are feed-forward via the model, it will only classify them into two classes (Nudity or Normal)

In [ ]:
%matplotlib inline
%load_ext autoreload
%autoreload 2
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from execute_model import evaluate

# Evaluate the model
img, pre_lb, real_lb = evaluate()


fig = plt.figure(figsize=(20,20))

for i in range(50):
    a = fig.add_subplot(10,5, i)
    a.set_title('PL:%d'%(pre_lb[i]))
    a.set_yticklabels([])
    a.set_xticklabels([])
    plt.imshow(img[i])
plt.pause(1)
plt.show()

#print pre_lb
#print real_lb
num_err = np.absolute(pre_lb - real_lb)
print('The number of error samples: %d'%np.sum(num_err))

In [ ]:
%matplotlib inline
%load_ext autoreload
%autoreload 2
import tensorflow as tf
import matplotlib.pyplot as plt
from execute_model import evaluate
import numpy as np

# Evaluate the model
img, pre_lb, real_lb = evaluate('/home/taivu/workspace/NudityDetection/Dataset/normal_test_set.tfrecords')
#print('Predicted labels: ',pre_lb)
#print('Real labels: ',real_lb)

fig = plt.figure(figsize=(20,20))
for i in range(50):
    a = fig.add_subplot(10,5, i)
    a.set_title('PL:%d'%(pre_lb[i]))
    a.set_yticklabels([])
    a.set_xticklabels([])
    plt.imshow(img[i])
plt.show()

num_err = np.absolute(pre_lb - real_lb)
print('The number of error samples: %d'%np.sum(num_err))

In [ ]:
%matplotlib inline
%load_ext autoreload
%autoreload 2
import tensorflow as tf
import matplotlib.pyplot as plt
from execute_model import evaluate

# Evaluate the model
img, pre_lb, real_lb = evaluate('/home/taivu/workspace/NudityDetection/Dataset/nudity_test_set.tfrecords', False)

fig = plt.figure(figsize=(20,20))

for i in range(50):
    a = fig.add_subplot(10,5, i)
    a.set_title('PL:%d'%(pre_lb[i]))
    a.set_yticklabels([])
    a.set_xticklabels([])
    plt.imshow(img[i])
plt.show()

num_err = np.absolute(pre_lb - real_lb)
print('The number of error samples: %d'%np.sum(num_err))

In [3]:
############################PIPELINE INPUT DATA########################################
%matplotlib inline
%load_ext autoreload
%autoreload 2

import tensorflow as tf
import matplotlib.pyplot as plt
from execute_model import evaluate
import numpy as np

eval_img, pre_label, _ = evaluate('/home/taivu/workspace/AddPic', True)

#fig = plt.figure(figsize=(40,80))

#for i in range(80):
#    a = fig.add_subplot(16,5, i)
#    a.set_title('PL:%d'%(pre_label[i]))
#    a.set_yticklabels([])
#    a.set_xticklabels([])
#    plt.imshow(eval_img[i])
#plt.show()


The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload

Stage 3: Optimize the model

  • Apply Transfer Learning method

In [3]:
# Test program
%matplotlib inline
%load_ext autoreload
%autoreload 2
from test_program import test_program

test_program()


The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload
(1.0, 310.90027)