You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tf_VGG.py 3.5 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import numpy as np
  2. import tensorflow as tf
  3. def conv_bn_relu(x, in_channel, out_channel):
  4. weight = tf.Variable(np.random.normal(scale=0.1, size=(
  5. out_channel, in_channel, 3, 3)).transpose([2, 3, 1, 0]).astype(np.float32))
  6. scale = tf.Variable(np.random.normal(
  7. scale=0.1, size=(out_channel,)).astype(np.float32))
  8. bias = tf.Variable(np.random.normal(
  9. scale=0.1, size=(out_channel,)).astype(np.float32))
  10. x = tf.nn.conv2d(x, weight, strides=[1, 1, 1, 1], padding='SAME')
  11. axis = list(range(len(x.shape) - 1))
  12. a_mean, a_var = tf.nn.moments(x, axis)
  13. x = tf.nn.batch_normalization(
  14. x, mean=a_mean, variance=a_var, scale=scale, offset=bias, variance_epsilon=1e-2)
  15. x = tf.nn.relu(x)
  16. return x
  17. def vgg_2block(x, in_channel, out_channel):
  18. x = conv_bn_relu(x, in_channel, out_channel)
  19. x = conv_bn_relu(x, out_channel, out_channel)
  20. x = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[
  21. 1, 2, 2, 1], padding='VALID')
  22. return x
  23. def vgg_3block(x, in_channel, out_channel):
  24. x = conv_bn_relu(x, in_channel, out_channel)
  25. x = conv_bn_relu(x, out_channel, out_channel)
  26. x = conv_bn_relu(x, out_channel, out_channel)
  27. x = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[
  28. 1, 2, 2, 1], padding='VALID')
  29. return x
  30. def vgg_4block(x, in_channel, out_channel):
  31. x = conv_bn_relu(x, in_channel, out_channel)
  32. x = conv_bn_relu(x, out_channel, out_channel)
  33. x = conv_bn_relu(x, out_channel, out_channel)
  34. x = conv_bn_relu(x, out_channel, out_channel)
  35. x = tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[
  36. 1, 2, 2, 1], padding='VALID')
  37. return x
  38. def tf_fc(x, in_feat, out_feat):
  39. weight = tf.Variable(np.random.normal(
  40. scale=0.1, size=(in_feat, out_feat)).astype(np.float32))
  41. bias = tf.Variable(np.random.normal(
  42. scale=0.1, size=(out_feat,)).astype(np.float32))
  43. x = tf.matmul(x, weight) + bias
  44. return x
  45. def tf_vgg(x, y_, num_layers, num_class=10):
  46. '''
  47. ResNet model in TensorFlow, for CIFAR10 dataset.
  48. Parameters:
  49. x: Variable(tensorflow.python.framework.ops.Tensor), shape (N, H, W, C)
  50. y_: Variable(tensorflow.python.framework.ops.Tensor), shape (N, num_classes)
  51. num_layers: 18 or 34
  52. Return:
  53. loss: Variable(tensorflow.python.framework.ops.Tensor), shape (1,)
  54. y: Variable(tensorflow.python.framework.ops.Tensor), shape (N, num_classes)
  55. '''
  56. if num_layers == 16:
  57. print('Building VGG-16 model in tensorflow')
  58. x = vgg_2block(x, 3, 64)
  59. x = vgg_2block(x, 64, 128)
  60. x = vgg_3block(x, 128, 256)
  61. x = vgg_3block(x, 256, 512)
  62. x = vgg_3block(x, 512, 512)
  63. elif num_layers == 19:
  64. print('Building VGG-19 model in tensorflow')
  65. x = vgg_2block(x, 3, 64)
  66. x = vgg_2block(x, 64, 128)
  67. x = vgg_4block(x, 128, 256)
  68. x = vgg_4block(x, 256, 512)
  69. x = vgg_4block(x, 512, 512)
  70. else:
  71. assert False, "Number of layers should be 18 or 34 !"
  72. x = tf.reshape(x, [-1, 512])
  73. x = tf_fc(x, 512, 4096)
  74. x = tf_fc(x, 4096, 4096)
  75. y = tf_fc(x, 4096, num_class)
  76. print("Number of Class: {}".format(num_class))
  77. loss = tf.nn.softmax_cross_entropy_with_logits(logits=y, labels=y_)
  78. loss = tf.reduce_mean(loss)
  79. return loss, y
  80. def tf_vgg16(x, y_, num_class=10):
  81. return tf_vgg(x, y_, 16, num_class)
  82. def tf_vgg19(x, y_, num_class=10):
  83. return tf_vgg(x, y_, 34, num_class)

分布式深度学习系统

Contributors (1)