"""
Created on Mon Apr 2 17:34:55 2018
@author: 帅老板
"""
import tensorflow as tf
import numpy as np
class Vggnet(object):
def _init_(self, x, keep_prob, num_classes):
self.X = x
self.KEEP_PROB = keep_prob
self.NUM_CLASSES = num_classes
self.p = []
self.inference()
def inference(self):
'first layer'
conv1_1 = conv_op(self.X, name='conv1_1', kh=3, kw=3, n_out=64, dh=1, dw=1, p=self.p)
conv1_2 = conv_op(conv1_1, name='conv1_2', kh=3, kw=3, n_out=64, dh=1, dw=1,p=self.p)
pool1 = mpool_op(conv1_2, name='pool1', kh=2, kw=2, dh=2, dw=2)
'second layer'
conv2_1 = conv_op(pool1, name='conv2_1', kh=3, kw=3, n_out=128, dh=1, dw=1, p=self.p)
conv2_2 = conv_op(conv2_1, name='conv2_2', kh=3, kw=3, n_out=128, dh=1, dw=1, p=self.p)
pool2 = mpool_op(conv2_2, name='pool2', kh=2, kw=2 ,dh=2, dw=2)
'third layer'
conv3_1 = conv_op(pool2, name='conv3_1', kh=3, kw=3, n_out=256, dh=1, dw=1,p=self.p)
conv3_2 = conv_op(conv3_1, name='conv3_2', kh=3, kw=3, n_out=256, dh=1, dw=1,p=self.p)
conv3_3 = conv_op(conv3_2, name='conv3_3',kh=3, kw=3, n_out=256, dh=1, dw=1, p=self.p)
pool3 = mpool_op(conv3_3, name='pool3', kh=2, kw=2, dh=2, dw=2)
'firth layer'
conv4_1 = conv_op(pool3, name='conv4_1', kh=3, kw=3, n_out=512, dh=1, dw=1,p=self.p)
conv4_2 = conv_op(conv4_1, name='conv4_2', kh=3, kw=3, n_out=512, dh=1, dw=1,p=self.p)
conv4_3 = conv_op(conv4_2, name='conv4_3',kh=3, kw=3, n_out=512, dh=1, dw=1, p=self.p)
pool4 = mpool_op(conv4_3, name='pool4', kh=2, kw=2, dh=2, dw=2)
'fifth layer'
conv5_1 = conv_op(pool4, name='conv5_1', kh=3, kw=3, n_out=512, dh=1, dw=1,p=self.p)
conv5_2 = conv_op(conv5_1, name='conv5_2', kh=3, kw=3, n_out=512, dh=1, dw=1,p=self.p)
conv5_3 = conv_op(conv5_2, name='conv5_3',kh=3, kw=3, n_out=512, dh=1, dw=1, p=self.p)
pool5 = mpool_op(conv5_3, name='pool5', kh=2, kw=2, dh=2, dw=2)
shp = pool5.get_shape()
flatten = shp[1].value*shp[2].value*shp[3].value
resh1 = tf.reshape(pool5, [-1,flatten], name='resh')
fc6 = fc_op(resh1, 'fc6', 4096, p=self.p)
fc6_drop = tf.nn.dropout(fc6, self.KEEP_PROB, name='fc6_drop')
fc7 = fc_op(fc6_drop, 'fc7', 4096, p=self.p)
fc7_drop = tf.nn.drop(fc7, self.KEEP_PROB, name='fc7_drop')
fc8 = fc_op(fc7_drop, 'fc8', n_out=self.NUM_CLASSES, p=self.p)
softmax = tf.nn.soft(fc8)
prediction = tf.argmax(softmax, 1)
return prediction, softmax, fc8, self.p
def conv_op(x, name, kh, kw, n_out, dh, dw, p):
n_in = x.get_shape()[-1].value
with tf.name_scope(name) as scope:
kernel = tf.get_Variable(scope+'w',
shape = [kh, kw, n_in, n_out],
dtype = tf.float32,
initializer = tf.contrib.layers.xavier_initializer_conv2D())
conv = tf.nn.conv2D(x, kernel, (1, dh, dw, 1), padding = 'SAME')
bias_init_val = tf.constant(0.0, shape = [n_out], dtype = tf.float32)
biases = tf.get_Variable(bias_init_val, trainable = True, name = 'b')
z = tf.nn.add(conv, biases)
activation = tf.nn.relu(z, name = scope)
p += [kernel,biases]
return activation
def fc_op(x, name, n_out, p):
n_in = x.get_shape()[-1].value
with tf.name_scope(name) as scope:
kernel = tf.get_Variable(scope+'w',
shape = [n_in, n_out],
dtype = tf.float32,
initializer = tf.contrib.layers.xavier_initializer())
biases = tf.Variable(tf.constant(0.1, shape = [n_out], dtype = tf.float32), name = 'b')
activation = tf.nn.relu_layer(x, kernel, biases, name = scope)
p += [kernel, biases]
return activation
def mpool_op(x, name, kh, kw, dh, dw):
return tf.nn.max_pool(x,
ksize = [1, kh, kw, 1],
strides = [1, dh, dw ,1],
padding = 'SAME',
name = name)