|
| 1 | +import numpy as np |
| 2 | +import tensorflow as tf |
| 3 | + |
| 4 | +import Configuration as cfg |
| 5 | + |
| 6 | +class AlexNet: |
| 7 | + def __init__(self, model, mean, trainable): |
| 8 | + self.model = model |
| 9 | + self.mean = mean |
| 10 | + self.var_dict = {} |
| 11 | + self.trainable = trainable |
| 12 | + |
| 13 | + def build(self, img_holder, label_holder=None): |
| 14 | + b, g, r = tf.split(axis=3, num_or_size_splits=3, value=img_holder) |
| 15 | + bgr = tf.concat(axis=3, values=[b - self.mean[0], g - self.mean[1], r - self.mean[2]]) |
| 16 | + |
| 17 | + self.conv1 = self.conv_layer(bgr, 3, 96, 11, 4, 'VALID', 'conv1') |
| 18 | + self.norm1 = self.lr_norm(self.conv1, 'norm1') |
| 19 | + self.pool1 = self.max_pool(self.norm1, 3, 2, 'VALID', 'pool1') |
| 20 | + |
| 21 | + self.conv2 = self.conv_layer(self.pool1, 96, 256, 5, 1, 'SAME', 'conv2') |
| 22 | + self.norm2 = self.lr_norm(self.conv2, 'norm2') |
| 23 | + self.pool2 = self.max_pool(self.norm2, 3, 2, 'VALID', 'pool2') |
| 24 | + |
| 25 | + self.conv3 = self.conv_layer(self.pool2, 256, 384, 3, 1, 'SAME', 'conv3') |
| 26 | + self.conv4 = self.conv_layer(self.conv3, 384, 384, 3, 1, 'SAME', 'conv4') |
| 27 | + self.conv5 = self.conv_layer(self.conv4, 384, 256, 3, 1, 'SAME', 'conv5') |
| 28 | + self.pool5 = self.max_pool(self.conv5, 3, 2, 'VALID', 'pool5') |
| 29 | + |
| 30 | + self.fc6 = self.fc_layer(self.pool5, 9216, 4096, 'fc6') |
| 31 | + self.relu6 = tf.nn.relu(self.fc6) |
| 32 | + |
| 33 | + if self.trainable: |
| 34 | + self.relu6 = tf.nn.dropout(self.relu6, 0.5) |
| 35 | + |
| 36 | + self.fc7 = self.fc_layer(self.relu6, 4096, 4096, 'fc7') |
| 37 | + self.relu7 = tf.nn.relu(self.fc7) |
| 38 | + |
| 39 | + if self.trainable: |
| 40 | + self.relu7 = tf.nn.dropout(self.relu7, 0.5) |
| 41 | + |
| 42 | + self.fc8 = self.fc_layer(self.relu7, 4096, cfg.object_class_num, 'fc8') |
| 43 | + |
| 44 | + self.prob = tf.nn.softmax(self.fc8, name='prob') |
| 45 | + |
| 46 | + if self.trainable: |
| 47 | + self.loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.fc8, labels=label_holder) |
| 48 | + self.loss_mean = tf.reduce_mean(self.loss) |
| 49 | + self.optimizer = tf.train.AdamOptimizer(learning_rate=0.0025).minimize(self.loss_mean) |
| 50 | + |
| 51 | + self.correct_prediction = tf.equal(tf.argmax(self.fc8, 1), tf.argmax(label_holder, 1)) |
| 52 | + self.accuracy_mean = tf.reduce_mean(tf.cast(self.correct_prediction, tf.float32)) |
| 53 | + |
| 54 | + self.model = None |
| 55 | + |
| 56 | + def build_finetune(self, box_holder=None, label_holder=None): |
| 57 | + self.finetune_roi_pool5 = self.roi_pool(self.conv5, box_holder, 14, 'finetune_roi_pool5') |
| 58 | + self.finetune_pool5 = self.max_pool(self.finetune_roi_pool5, 2, 2, 'SAME', 'finetune_pool5') |
| 59 | + |
| 60 | + self.finetune_fc6 = self.fc_layer(self.finetune_pool5, 12544, 4096, 'finetune_fc6') |
| 61 | + self.finetune_relu6 = tf.nn.relu(self.finetune_fc6) |
| 62 | + |
| 63 | + if self.trainable: |
| 64 | + self.finetune_relu6 = tf.nn.dropout(self.finetune_relu6, 0.5) |
| 65 | + |
| 66 | + self.finetune_fc7 = self.fc_layer(self.finetune_relu6, 4096, 4096, 'finetune_fc7') |
| 67 | + self.finetune_relu7 = tf.nn.relu(self.finetune_fc7) |
| 68 | + |
| 69 | + if self.trainable: |
| 70 | + self.finetune_relu7 = tf.nn.dropout(self.finetune_relu7, 0.5) |
| 71 | + |
| 72 | + self.finetune_fc8 = self.fc_layer(self.finetune_relu7, 4096, cfg.object_class_num + 1, 'finetune_fc8') |
| 73 | + |
| 74 | + self.finetune_prob = tf.nn.softmax(self.finetune_fc8, name='finetune_prob') |
| 75 | + |
| 76 | + self.finetune_bbox1 = self.fc_layer(self.finetune_relu7, 4096, cfg.object_class_num * 4, 'finetune_bbox1') |
| 77 | + |
| 78 | + if self.trainable: |
| 79 | + self.finetune_cls_loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.finetune_fc8, labels=label_holder) |
| 80 | + self.finetune_cls_loss_sum = tf.reduce_sum(self.finetune_cls_loss, 0) |
| 81 | + |
| 82 | + box_rect = box_holder[1, :] |
| 83 | + self.finetune_bbox_loss = tf.square(box_rect - self.finetune_bbox1) |
| 84 | + self.finetune_bbox_loss_sum = tf.reduce_sum(self.finetune_bbox_loss, 0) |
| 85 | + |
| 86 | + valid_bbox = np.ones((box_holder, 1)) |
| 87 | + valid_bbox[valid_bbox == cfg.object_class_num] = 0.0 |
| 88 | + |
| 89 | + self.finetune_loss = finetune_cls_loss_sum + 1 * valid_bbox * finetune_bbox_loss_sum |
| 90 | + self.finetune_loss_mean = tf.reduce_mean(self.finetune_loss) |
| 91 | + self.finetune_optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.0001).minimize(self.finetune_loss_mean) |
| 92 | + |
| 93 | + self.finetune_correct_prediction = tf.equal(tf.argmax(self.finetune_fc8, 1), tf.argmax(label_holder, 1)) |
| 94 | + self.finetune_accuracy_mean = tf.reduce_mean(tf.cast(self.finetune_correct_prediction, tf.float32)) |
| 95 | + |
| 96 | + def get_var(self, initial_value, name, idx, var_name): |
| 97 | + if self.model is not None and name in self.model: |
| 98 | + value = self.model[name][idx] |
| 99 | + else: |
| 100 | + value = initial_value |
| 101 | + |
| 102 | + var = tf.Variable(value, name=var_name) |
| 103 | + |
| 104 | + self.var_dict[(name, idx)] = var |
| 105 | + |
| 106 | + return var |
| 107 | + |
| 108 | + def get_conv_var(self, filter_size, in_channels, out_channels, name): |
| 109 | + initial_value = tf.truncated_normal([filter_size, filter_size, in_channels, out_channels], 0.0, 0.001) |
| 110 | + filters = self.get_var(initial_value, name, 0, name + '_filters') |
| 111 | + |
| 112 | + initial_value = tf.truncated_normal([out_channels], 0.0, 0.001) |
| 113 | + biases = self.get_var(initial_value, name, 1, name + '_biases') |
| 114 | + |
| 115 | + return filters, biases |
| 116 | + |
| 117 | + def get_fc_var(self, in_size, out_size, name): |
| 118 | + initial_value = tf.truncated_normal([in_size, out_size], 0.0, 0.001) |
| 119 | + weights = self.get_var(initial_value, name, 0, name + '_weights') |
| 120 | + |
| 121 | + initial_value = tf.truncated_normal([out_size], 0.0, 0.001) |
| 122 | + biases = self.get_var(initial_value, name, 1, name + '_biases') |
| 123 | + |
| 124 | + return weights, biases |
| 125 | + |
| 126 | + def conv_layer(self, bottom, in_channels, out_channels, filter_size, stride_size, padding_type, name): |
| 127 | + with tf.variable_scope(name): |
| 128 | + filt, conv_biases = self.get_conv_var(filter_size, in_channels, out_channels, name) |
| 129 | + |
| 130 | + conv = tf.nn.conv2d(bottom, filter=filt, strides=[1, stride_size, stride_size, 1], padding=padding_type) |
| 131 | + bias = tf.nn.bias_add(conv, conv_biases) |
| 132 | + relu = tf.nn.relu(bias) |
| 133 | + |
| 134 | + return relu |
| 135 | + |
| 136 | + def fc_layer(self, bottom, in_size, out_size, name): |
| 137 | + with tf.variable_scope(name): |
| 138 | + weights, biases = self.get_fc_var(in_size, out_size, name) |
| 139 | + |
| 140 | + x = tf.reshape(bottom, [-1, in_size]) |
| 141 | + fc = tf.nn.bias_add(tf.matmul(x, weights), biases) |
| 142 | + |
| 143 | + return fc |
| 144 | + |
| 145 | + def lr_norm(self, bottom, name): |
| 146 | + return tf.nn.local_response_normalization(bottom, depth_radius=2, alpha=1e-4, beta=0.75, name=name) |
| 147 | + |
| 148 | + def avg_pool(self, bottom, name): |
| 149 | + return tf.nn.avg_pool(bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name) |
| 150 | + |
| 151 | + def max_pool(self, bottom, kernel_size, stride_size, padding_type, name): |
| 152 | + return tf.nn.max_pool(bottom, ksize=[1, kernel_size, kernel_size, 1], strides=[1, stride_size, stride_size, 1], padding=padding_type, name=name) |
| 153 | + |
| 154 | + def roi_pool(self, bottom, box_holder, crop_size, name): |
| 155 | + box_rect = box_holder[1, :] |
| 156 | + box_batch_idx = box_holder[0] |
| 157 | + return tf.image.crop_and_resize(bottom, box_rect, box_batch_idx, [crop_size, crop_size], name=name) |
| 158 | + |
| 159 | + def get_var_count(self): |
| 160 | + count = 0 |
| 161 | + for var in list(self.var_dict.values()): |
| 162 | + count += np.multiply(var.get_shape().as_list()) |
| 163 | + return count |
0 commit comments