Advertisement
LovelessIsma

utils.py

Aug 31st, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.54 KB | None | 0 0
  1. # -----------------------------------------------------------------------------------------
  2. # Code taken from https://github.com/iwantooxxoox/Keras-OpenFace (with minor modifications)
  3. # -----------------------------------------------------------------------------------------
  4.  
  5. import tensorflow as tf
  6. import numpy as np
  7. import os
  8.  
  9. from numpy import genfromtxt
  10. from keras.layers import Conv2D, ZeroPadding2D, Activation
  11. from keras.layers.normalization import BatchNormalization
  12.  
  13. _FLOATX = 'float32'
  14.  
  15. def variable(value, dtype=_FLOATX, name=None):
  16.   v = tf.Variable(np.asarray(value, dtype=dtype), name=name)
  17.   _get_session().run(v.initializer)
  18.   return v
  19.  
  20. def shape(x):
  21.   return x.get_shape()
  22.  
  23. def square(x):
  24.   return tf.square(x)
  25.  
  26. def zeros(shape, dtype=_FLOATX, name=None):
  27.   return variable(np.zeros(shape), dtype, name)
  28.  
  29. def concatenate(tensors, axis=-1):
  30.   if axis < 0:
  31.       axis = axis % len(tensors[0].get_shape())
  32.   return tf.concat(axis, tensors)
  33.  
  34. def LRN2D(x):
  35.   return tf.nn.lrn(x, alpha=1e-4, beta=0.75)
  36.  
  37. def conv2d_bn(
  38.   x,
  39.   layer=None,
  40.   cv1_out=None,
  41.   cv1_filter=(1, 1),
  42.   cv1_strides=(1, 1),
  43.   cv2_out=None,
  44.   cv2_filter=(3, 3),
  45.   cv2_strides=(1, 1),
  46.   padding=None,
  47. ):
  48.   num = '' if cv2_out == None else '1'
  49.   tensor = Conv2D(cv1_out, cv1_filter, strides=cv1_strides, name=layer+'_conv'+num)(x)
  50.   tensor = BatchNormalization(axis=3, epsilon=0.00001, name=layer+'_bn'+num)(tensor)
  51.   tensor = Activation('relu')(tensor)
  52.   if padding == None:
  53.     return tensor
  54.   tensor = ZeroPadding2D(padding=padding)(tensor)
  55.   if cv2_out == None:
  56.     return tensor
  57.   tensor = Conv2D(cv2_out, cv2_filter, strides=cv2_strides, name=layer+'_conv'+'2')(tensor)
  58.   tensor = BatchNormalization(axis=3, epsilon=0.00001, name=layer+'_bn'+'2')(tensor)
  59.   tensor = Activation('relu')(tensor)
  60.   return tensor
  61.  
  62. weights = [
  63.   'conv1', 'bn1', 'conv2', 'bn2', 'conv3', 'bn3',
  64.   'inception_3a_1x1_conv', 'inception_3a_1x1_bn',
  65.   'inception_3a_pool_conv', 'inception_3a_pool_bn',
  66.   'inception_3a_5x5_conv1', 'inception_3a_5x5_conv2', 'inception_3a_5x5_bn1', 'inception_3a_5x5_bn2',
  67.   'inception_3a_3x3_conv1', 'inception_3a_3x3_conv2', 'inception_3a_3x3_bn1', 'inception_3a_3x3_bn2',
  68.   'inception_3b_3x3_conv1', 'inception_3b_3x3_conv2', 'inception_3b_3x3_bn1', 'inception_3b_3x3_bn2',
  69.   'inception_3b_5x5_conv1', 'inception_3b_5x5_conv2', 'inception_3b_5x5_bn1', 'inception_3b_5x5_bn2',
  70.   'inception_3b_pool_conv', 'inception_3b_pool_bn',
  71.   'inception_3b_1x1_conv', 'inception_3b_1x1_bn',
  72.   'inception_3c_3x3_conv1', 'inception_3c_3x3_conv2', 'inception_3c_3x3_bn1', 'inception_3c_3x3_bn2',
  73.   'inception_3c_5x5_conv1', 'inception_3c_5x5_conv2', 'inception_3c_5x5_bn1', 'inception_3c_5x5_bn2',
  74.   'inception_4a_3x3_conv1', 'inception_4a_3x3_conv2', 'inception_4a_3x3_bn1', 'inception_4a_3x3_bn2',
  75.   'inception_4a_5x5_conv1', 'inception_4a_5x5_conv2', 'inception_4a_5x5_bn1', 'inception_4a_5x5_bn2',
  76.   'inception_4a_pool_conv', 'inception_4a_pool_bn',
  77.   'inception_4a_1x1_conv', 'inception_4a_1x1_bn',
  78.   'inception_4e_3x3_conv1', 'inception_4e_3x3_conv2', 'inception_4e_3x3_bn1', 'inception_4e_3x3_bn2',
  79.   'inception_4e_5x5_conv1', 'inception_4e_5x5_conv2', 'inception_4e_5x5_bn1', 'inception_4e_5x5_bn2',
  80.   'inception_5a_3x3_conv1', 'inception_5a_3x3_conv2', 'inception_5a_3x3_bn1', 'inception_5a_3x3_bn2',
  81.   'inception_5a_pool_conv', 'inception_5a_pool_bn',
  82.   'inception_5a_1x1_conv', 'inception_5a_1x1_bn',
  83.   'inception_5b_3x3_conv1', 'inception_5b_3x3_conv2', 'inception_5b_3x3_bn1', 'inception_5b_3x3_bn2',
  84.   'inception_5b_pool_conv', 'inception_5b_pool_bn',
  85.   'inception_5b_1x1_conv', 'inception_5b_1x1_bn',
  86.   'dense_layer'
  87. ]
  88.  
  89. conv_shape = {
  90.   'conv1': [64, 3, 7, 7],
  91.   'conv2': [64, 64, 1, 1],
  92.   'conv3': [192, 64, 3, 3],
  93.   'inception_3a_1x1_conv': [64, 192, 1, 1],
  94.   'inception_3a_pool_conv': [32, 192, 1, 1],
  95.   'inception_3a_5x5_conv1': [16, 192, 1, 1],
  96.   'inception_3a_5x5_conv2': [32, 16, 5, 5],
  97.   'inception_3a_3x3_conv1': [96, 192, 1, 1],
  98.   'inception_3a_3x3_conv2': [128, 96, 3, 3],
  99.   'inception_3b_3x3_conv1': [96, 256, 1, 1],
  100.   'inception_3b_3x3_conv2': [128, 96, 3, 3],
  101.   'inception_3b_5x5_conv1': [32, 256, 1, 1],
  102.   'inception_3b_5x5_conv2': [64, 32, 5, 5],
  103.   'inception_3b_pool_conv': [64, 256, 1, 1],
  104.   'inception_3b_1x1_conv': [64, 256, 1, 1],
  105.   'inception_3c_3x3_conv1': [128, 320, 1, 1],
  106.   'inception_3c_3x3_conv2': [256, 128, 3, 3],
  107.   'inception_3c_5x5_conv1': [32, 320, 1, 1],
  108.   'inception_3c_5x5_conv2': [64, 32, 5, 5],
  109.   'inception_4a_3x3_conv1': [96, 640, 1, 1],
  110.   'inception_4a_3x3_conv2': [192, 96, 3, 3],
  111.   'inception_4a_5x5_conv1': [32, 640, 1, 1,],
  112.   'inception_4a_5x5_conv2': [64, 32, 5, 5],
  113.   'inception_4a_pool_conv': [128, 640, 1, 1],
  114.   'inception_4a_1x1_conv': [256, 640, 1, 1],
  115.   'inception_4e_3x3_conv1': [160, 640, 1, 1],
  116.   'inception_4e_3x3_conv2': [256, 160, 3, 3],
  117.   'inception_4e_5x5_conv1': [64, 640, 1, 1],
  118.   'inception_4e_5x5_conv2': [128, 64, 5, 5],
  119.   'inception_5a_3x3_conv1': [96, 1024, 1, 1],
  120.   'inception_5a_3x3_conv2': [384, 96, 3, 3],
  121.   'inception_5a_pool_conv': [96, 1024, 1, 1],
  122.   'inception_5a_1x1_conv': [256, 1024, 1, 1],
  123.   'inception_5b_3x3_conv1': [96, 736, 1, 1],
  124.   'inception_5b_3x3_conv2': [384, 96, 3, 3],
  125.   'inception_5b_pool_conv': [96, 736, 1, 1],
  126.   'inception_5b_1x1_conv': [256, 736, 1, 1],
  127. }
  128.  
  129. def load_weights():
  130.   weightsDir = './weights'
  131.   fileNames = filter(lambda f: not f.startswith('.'), os.listdir(weightsDir))
  132.   paths = {}
  133.   weights_dict = {}
  134.  
  135.   for n in fileNames:
  136.     paths[n.replace('.csv', '')] = weightsDir + '/' + n
  137.  
  138.   for name in weights:
  139.     if 'conv' in name:
  140.       conv_w = genfromtxt(paths[name + '_w'], delimiter=',', dtype=None)
  141.       conv_w = np.reshape(conv_w, conv_shape[name])
  142.       conv_w = np.transpose(conv_w, (2, 3, 1, 0))
  143.       conv_b = genfromtxt(paths[name + '_b'], delimiter=',', dtype=None)
  144.       weights_dict[name] = [conv_w, conv_b]    
  145.     elif 'bn' in name:
  146.       bn_w = genfromtxt(paths[name + '_w'], delimiter=',', dtype=None)
  147.       bn_b = genfromtxt(paths[name + '_b'], delimiter=',', dtype=None)
  148.       bn_m = genfromtxt(paths[name + '_m'], delimiter=',', dtype=None)
  149.       bn_v = genfromtxt(paths[name + '_v'], delimiter=',', dtype=None)
  150.       weights_dict[name] = [bn_w, bn_b, bn_m, bn_v]
  151.     elif 'dense' in name:
  152.       dense_w = genfromtxt(weightsDir+'/dense_w.csv', delimiter=',', dtype=None)
  153.       dense_w = np.reshape(dense_w, (128, 736))
  154.       dense_w = np.transpose(dense_w, (1, 0))
  155.       dense_b = genfromtxt(weightsDir+'/dense_b.csv', delimiter=',', dtype=None)
  156.       weights_dict[name] = [dense_w, dense_b]
  157.  
  158.   return weights_dict
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement