|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Created on Thu Apr 5 20:24:20 2018 |
| 5 | +
|
| 6 | +@author: wu |
| 7 | +""" |
| 8 | + |
| 9 | +import tensorflow as tf |
| 10 | +import numpy as np |
| 11 | + |
| 12 | +tf.reset_default_graph() |
| 13 | + |
| 14 | +def maxPoolLayer(x, kHeight, kWidth, strideX, strideY, name, padding = "SAME"): |
| 15 | + """max-pooling""" |
| 16 | + return tf.nn.max_pool(x, ksize = [1, kHeight, kWidth, 1], |
| 17 | + strides = [1, strideX, strideY, 1], padding = padding, name = name) |
| 18 | + |
| 19 | +def dropout(x, keepPro, name = None): |
| 20 | + """dropout""" |
| 21 | + return tf.nn.dropout(x, keepPro, name) |
| 22 | + |
| 23 | +def LRN(x, R, alpha, beta, name = None, bias = 1.0): |
| 24 | + """LRN""" |
| 25 | + return tf.nn.local_response_normalization(x, depth_radius = R, alpha = alpha, |
| 26 | + beta = beta, bias = bias, name = name) |
| 27 | + |
| 28 | +def fcLayer(x, inputD, outputD, reluFlag, name): |
| 29 | + """fully-connect""" |
| 30 | + with tf.variable_scope(name) as scope: |
| 31 | + w = tf.get_variable("weights", shape = [inputD, outputD], dtype = "float") |
| 32 | + b = tf.get_variable("biases", [outputD], dtype = "float") |
| 33 | + out = tf.nn.xw_plus_b(x, w, b, name = scope.name) |
| 34 | + if reluFlag: |
| 35 | + return tf.nn.relu(out) |
| 36 | + else: |
| 37 | + return out |
| 38 | + |
| 39 | +def convLayer(x, kHeight, kWidth, strideX, strideY, |
| 40 | + featureNum, name, padding = "SAME", groups = 1): |
| 41 | + """convolution""" |
| 42 | + channel = int(x.get_shape()[-1]) |
| 43 | + conv = lambda a, b: tf.nn.conv2d(a, b, strides = [1, strideY, strideX, 1], padding = padding) |
| 44 | + with tf.variable_scope(name) as scope: |
| 45 | + w = tf.get_variable("weights", shape = [kHeight, kWidth, channel/groups, featureNum]) |
| 46 | + b = tf.get_variable("biases", shape = [featureNum]) |
| 47 | + |
| 48 | + xNew = tf.split(value = x, num_or_size_splits = groups, axis = 3) |
| 49 | + wNew = tf.split(value = w, num_or_size_splits = groups, axis = 3) |
| 50 | + |
| 51 | + featureMap = [conv(t1, t2) for t1, t2 in zip(xNew, wNew)] |
| 52 | + mergeFeatureMap = tf.concat(axis = 3, values = featureMap) |
| 53 | + # print mergeFeatureMap.shape |
| 54 | + out = tf.nn.bias_add(mergeFeatureMap, b) |
| 55 | + return tf.nn.relu(tf.reshape(out, mergeFeatureMap.get_shape().as_list()), name = scope.name) |
| 56 | + |
| 57 | +def losses(logits, labels): |
| 58 | + with tf.variable_scope("loss") as scope: |
| 59 | + cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, |
| 60 | + labels=labels, name="xentropy_per_example") |
| 61 | + loss = tf.reduce_mean(cross_entropy, name="loss") |
| 62 | + tf.summary.scalar(scope.name + "loss", loss) |
| 63 | + return loss |
| 64 | + |
| 65 | + |
| 66 | +def training(loss, learning_rate): |
| 67 | + with tf.name_scope("optimizer"): |
| 68 | + optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) |
| 69 | + global_step = tf.Variable(0, name="global_step", trainable=False) |
| 70 | + train_op = optimizer.minimize(loss, global_step=global_step) |
| 71 | + return train_op |
| 72 | + |
| 73 | + |
| 74 | +def evaluation(logits, labels): |
| 75 | + with tf.variable_scope("accuracy") as scope: |
| 76 | + correct = tf.nn.in_top_k(logits, labels, 1) |
| 77 | + correct = tf.cast(correct, tf.float16) |
| 78 | + accuracy = tf.reduce_mean(correct) |
| 79 | + tf.summary.scalar(scope.name + "accuracy", accuracy) |
| 80 | + return accuracy |
| 81 | + |
| 82 | + |
| 83 | +def Batch_Normalization(x): |
| 84 | + """Batch Normalization layer""" |
| 85 | + x_mean, x_var = tf.nn.moments(x, axes=[0]) |
| 86 | + scale = tf.Variable(tf.ones([4096])) |
| 87 | + offset = tf.Variable(tf.zeros([4096])) |
| 88 | + variance_epsilon = 0.001 |
| 89 | + BN_x = tf.nn.batch_normalization(x, x_mean, x_var, offset, scale, variance_epsilon) |
| 90 | + return BN_x |
| 91 | + |
| 92 | + |
| 93 | +class alexNet(object): |
| 94 | + """alexNet model""" |
| 95 | + def __init__(self, x, keepPro=1.0, classNum=6, skip_layer=['conv5','fc6','fc7','fc8'], weights_path = 'DEFAULT'): |
| 96 | + self.X = x |
| 97 | + self.KEEPPRO = keepPro |
| 98 | + self.CLASSNUM = classNum |
| 99 | + self.SKIP_LAYER = skip_layer |
| 100 | + self.WEIGHTS_PATH = weights_path |
| 101 | + self.buildCNN() |
| 102 | + |
| 103 | + #fine-tune choice |
| 104 | + if weights_path == 'DEFAULT': |
| 105 | + self.WEIGHTS_PATH = '/home/wu/TF_Project/action/bvlc_alexnet.npy' |
| 106 | + #self.WEIGHTS_PATH = '/home/wu/TF_Project/action/model_tfrecord_sample/model.ckpt-1000' |
| 107 | + else: |
| 108 | + self.WEIGHTS_PATH = weights_path |
| 109 | + |
| 110 | + def load_initial_weights(self, session): |
| 111 | + weights_dict = np.load(self.WEIGHTS_PATH, encoding = 'bytes').item() |
| 112 | + for op_name in weights_dict: |
| 113 | + if op_name not in self.SKIP_LAYER: |
| 114 | + with tf.variable_scope(op_name, reuse = True): |
| 115 | + for data in weights_dict[op_name]: |
| 116 | + if len(data.shape) == 1: |
| 117 | + var = tf.get_variable('biases', trainable = False) |
| 118 | + session.run(var.assign(data)) |
| 119 | + else: |
| 120 | + var = tf.get_variable('weights', trainable = False) |
| 121 | + session.run(var.assign(data)) |
| 122 | + |
| 123 | + def buildCNN(self): |
| 124 | + """ |
| 125 | + input 100*100*3 |
| 126 | + conv1 23*23*96 |
| 127 | + pool1 11*11*96 |
| 128 | + conv2 11*11*256 |
| 129 | + pool2 5*5*256 |
| 130 | + conv3 5*5*384 |
| 131 | + conv4 5*5*384 |
| 132 | + conv5 5*5*256 |
| 133 | + pool5 2*2*256 |
| 134 | + fc1 1024->4096 |
| 135 | + fc2 4096->4096 |
| 136 | + fc3 4096->6 |
| 137 | + """ |
| 138 | + self.conv1 = convLayer(self.X, 11, 11, 4, 4, 96, "conv1", "VALID") |
| 139 | + """ |
| 140 | + split = tf.split(self.conv1,num_or_size_splits=96,axis=3) |
| 141 | + tf.summary.image("conv1_features",split[0],10) |
| 142 | + """ |
| 143 | + lrn1 = LRN(self.conv1, 2, 2e-05, 0.75, "norm1") |
| 144 | + pool1 = maxPoolLayer(lrn1, 3, 3, 2, 2, "pool1", "VALID") |
| 145 | + |
| 146 | + conv2 = convLayer(pool1, 5, 5, 1, 1, 256, "conv2", groups = 2) |
| 147 | + lrn2 = LRN(conv2, 2, 2e-05, 0.75, "lrn2") |
| 148 | + pool2 = maxPoolLayer(lrn2, 3, 3, 2, 2, "pool2", "VALID") |
| 149 | + |
| 150 | + conv3 = convLayer(pool2, 3, 3, 1, 1, 384, "conv3") |
| 151 | + |
| 152 | + conv4 = convLayer(conv3, 3, 3, 1, 1, 384, "conv4", groups = 2) |
| 153 | + |
| 154 | + conv5 = convLayer(conv4, 3, 3, 1, 1, 256, "conv5", groups = 2) |
| 155 | + pool5 = maxPoolLayer(conv5, 3, 3, 2, 2, "pool5", "VALID") |
| 156 | + |
| 157 | + fcIn = tf.reshape(pool5, [-1, 1024]) |
| 158 | + fc1 = fcLayer(fcIn, 1024, 4096, True, "fc6") |
| 159 | + #dropout1 = dropout(fc1, self.KEEPPRO) |
| 160 | + dropout1 = Batch_Normalization(fc1) |
| 161 | + |
| 162 | + fc2 = fcLayer(dropout1, 4096, 4096, True, "fc7") |
| 163 | + #dropout2 = dropout(fc2, self.KEEPPRO) |
| 164 | + dropout2 = Batch_Normalization(fc2) |
| 165 | + |
| 166 | + self.fc3 = fcLayer(dropout2, 4096, self.CLASSNUM, True, "fc8") |
| 167 | + |
0 commit comments