def mt_loop(self, net, reuse=False): bn_params = { 'decay': 0.999, 'center': True, 'scale': True, 'epsilon': 0.001, 'updates_collections': None, 'is_training': self.is_training } with tf.variable_scope('mt_loop', reuse=reuse): net = layers.conv2d( net, 48, kernel_size=3, name='conv1', reuse=reuse, stride=2, padding='valid', normalizer_fn=layers.batchNormalization, normalizer_params=bn_params, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.max_pool2d(net, 3, 2, name='pool1') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='se1', filters=48, reuse=reuse) return net
def build_resnet_18(self, net, reuse=False): bn_params = { 'decay': 0.999, 'center': True, 'scale': True, 'epsilon': 0.001, 'updates_collections': None, 'is_training': self.is_training } net = layers.conv2d( net, filters=64, kernel_size=7, stride=2, padding='SAME', name='resnet_conv1', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.max_pool2d(net, kernel_size=3, stride=2, padding='same') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='se1', filters=64) net = self.res_block(net, 64, 3, reuse=reuse, name='resnet_conv2a') net = self.res_block(net, 64, 3, reuse=reuse, name='resnet_conv2b') net = self.res_block_first(net, 128, 3, reuse=reuse, name='resnet_conv3a') net = self.res_block(net, 128, 3, reuse=reuse, name='resnet_conv3b') net = self.res_block_first(net, 256, 3, reuse=reuse, name='resnet_conv4a') net = self.res_block(net, 256, 3, reuse=reuse, name='resnet_conv4b') net = self.res_block_first(net, 512, 3, reuse=reuse, name='resnet_conv5a') net = self.res_block(net, 512, 3, reuse=reuse, name='resnet_conv5b') net = layers.avg_pool2d(net, kernel_size=7, stride=1) return net
def mt_loop(self, net, reuse=False): bn_params = {'decay': 0.999, 'center': True, 'scale': True, 'epsilon': 0.001, 'updates_collections': None, 'is_training': self.is_training} with tf.variable_scope('mt_loop', reuse=reuse): net = layers.conv2d(net, 48, kernel_size=3, name='conv1', reuse=reuse, stride=2, padding='valid', normalizer_fn=layers.batchNormalization, normalizer_params=bn_params, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.max_pool2d(net, 3, 2, name='pool1') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='se1', filters=48, reuse=reuse) return net
def res_block(self, net, filters=64, kernel=3, name='resnet_convX', reuse=False): bn_params = { 'decay': 0.999, 'center': True, 'scale': True, 'epsilon': 0.001, 'updates_collections': None, 'is_training': self.is_training } tmp_logits = net net = layers.conv2d( net, filters=filters, kernel_size=kernel, stride=1, padding='same', name=name + '_1', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d( net, filters=filters, kernel_size=kernel, stride=1, padding='same', name=name + '_2', reuse=reuse, activation_fn=None, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='se' + name, filters=filters) net = layers.add(net, tmp_logits) net = layers.relu(net) return net
def createModel(self): print("CREATING MODEL") self.global_step = tf.Variable(0, trainable=False) self.is_training = tf.placeholder_with_default(True, [], name='is_training') self.learning_rate = layers.decayLearningRate(LEARNING_RATE, self.global_step, DECAY_STEPS, DECAY_RATE) self.X = tf.placeholder(dtype=tf.float32, shape=[None, self.dataset.frames, self.dataset.h, self.dataset.w, self.dataset.c]) self.Y = tf.placeholder(dtype=tf.int32, shape=[None]) self.Yoh = layers.toOneHot(self.Y, self.dataset.num_classes) reuse = None towersLogits = [] for sequence_image in range(self.dataset.frames): net = self.vgg16(self.X[:, sequence_image], reuse) towersLogits.append(net) reuse = True net = layers.stack(towersLogits) del towersLogits[:] net = layers.transpose(net, [1, 2, 3, 0, 4]) net = layers.reshape(net, [-1, net.shape[1], net.shape[2], net.shape[3] * net.shape[4]]) net = layers.fc(net, 512, name='fc5', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='se5', filters=512) net = layers.flatten(net, name='flatten') net = layers.fc(net, 4096, name='fc6', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.fc(net, 4096, name='fc7', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) self.logits = layers.fc(net, self.dataset.num_classes, activation_fn=None, name='fc8', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) self.preds = layers.softmax(self.logits) cross_entropy_loss = layers.reduce_mean(layers.softmax_cross_entropy(logits=self.logits, labels=self.Yoh)) regularization_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) self.loss = cross_entropy_loss + REGULARIZER_SCALE * tf.reduce_sum(regularization_loss) self.opt = layers.adam(self.learning_rate) self.train_op = self.opt.minimize(self.loss, global_step=self.global_step) self.accuracy, self.precision, self.recall = self.createSummaries(self.Yoh, self.preds, self.loss, self.learning_rate)
def modelLogits(self): self.towerLogits = tf.placeholder(dtype=tf.float32, shape=[None, self.dataset.frames, 3, 3, 512]) net = self.towerLogits net = layers.transpose(net, [0, 2, 3, 1, 4]) net = layers.reshape(net, [-1, net.shape[1], net.shape[2], net.shape[3] * net.shape[4]]) net = layers.fc(net, 512, name='fc5', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='se5', filters=512) net = layers.flatten(net, name='flatten') net = layers.fc(net, 4096, name='fc6', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.fc(net, 4096, name='fc7', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) self.logits = layers.fc(net, self.dataset.num_classes, activation_fn=None, name='fc8', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE))
def createModel(self): print("CREATING MODEL") self.global_step = tf.Variable(0, trainable=False) self.is_training = tf.placeholder_with_default(True, [], name='is_training') self.learning_rate = layers.decayLearningRate(LEARNING_RATE, self.global_step, DECAY_STEPS, DECAY_RATE) self.X = tf.placeholder(dtype=tf.float32, shape=[None, self.dataset.frames, self.dataset.h, self.dataset.w, self.dataset.c]) self.Y = tf.placeholder(dtype=tf.int32, shape=[None]) self.Yoh = layers.toOneHot(self.Y, self.dataset.num_classes) net = self.X if net.shape[-1] > 1: net = layers.rgb_to_grayscale(net) if len(net.shape) > 4: net = tf.transpose(net, [0, 2, 3, 1, 4]) net = tf.reshape(net, [-1, net.shape[1], net.shape[2], net.shape[3] * net.shape[4]]) bn_params = {'decay': 0.999, 'center': True, 'scale': True, 'epsilon': 0.001, 'updates_collections': None, 'is_training': self.is_training} net = layers.conv2d(net, filters=96, kernel_size=3, padding='VALID', stride=2, name='conv1', normalizer_fn=layers.batchNormalization, normalizer_params=bn_params, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.max_pool2d(net, 3, 2, name='max_pool1') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='se1', filters=96) net = layers.conv2d(net, filters=256, kernel_size=3, padding='VALID', stride=2, name='conv2', normalizer_fn=layers.batchNormalization, normalizer_params=bn_params, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.max_pool2d(net, 3, 2, name='max_pool2') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='se2', filters=256) net = layers.conv2d(net, filters=512, kernel_size=3, padding='SAME', stride=1, name='conv3', normalizer_fn=layers.batchNormalization, normalizer_params=bn_params, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='se3', filters=512) net = layers.conv2d(net, filters=512, kernel_size=3, padding='SAME', stride=1, name='conv4', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='se4', filters=512) net = layers.conv2d(net, filters=512, kernel_size=3, padding='SAME', stride=1, name='conv5', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.max_pool2d(net, 3, 2, name='max_pool2') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='se5', filters=512) net = layers.flatten(net, name='flatten') net = layers.fc(net, 4096, name='fc6', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.fc(net, 4096, name='fc7', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) self.logits = layers.fc(net, self.dataset.num_classes, activation_fn=None, name='fc8', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) self.preds = layers.softmax(self.logits) cross_entropy_loss = layers.reduce_mean(layers.softmax_cross_entropy(logits=self.logits, labels=self.Yoh)) regularization_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) self.loss = cross_entropy_loss + REGULARIZER_SCALE * tf.reduce_sum(regularization_loss) self.opt = layers.adam(self.learning_rate) self.train_op = self.opt.minimize(self.loss, global_step=self.global_step) self.accuracy, self.precision, self.recall = self.createSummaries(self.Yoh, self.preds, self.loss, self.learning_rate)
def vgg(self, net, reuse=False): with tf.variable_scope('vgg_16', reuse=reuse): bn_params = {'decay': 0.999, 'center': True, 'scale': True, 'epsilon': 0.001, 'updates_collections': None, 'is_training': self.is_training} with tf.variable_scope('conv1', reuse=reuse): net = layers.conv2d(net, 64, name='conv1_1', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 64, name='conv1_2', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.max_pool2d(net, [2, 2], 2, name='pool1') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='vgg_se1', filters=64) with tf.variable_scope('conv2', reuse=reuse): net = layers.conv2d(net, 128, name='conv2_1', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 128, name='conv2_2', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.max_pool2d(net, [2, 2], 2, name='pool2') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='vgg_se2', filters=128) with tf.variable_scope('conv3', reuse=reuse): net = layers.conv2d(net, 256, name='conv3_1', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 256, name='conv3_2', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 256, name='conv3_3', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.max_pool2d(net, [2, 2], 2, name='pool3') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='vgg_se3', filters=256) with tf.variable_scope('conv4', reuse=reuse): net = layers.conv2d(net, 512, name='conv4_1', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 512, name='conv4_2', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 512, name='conv4_3', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.max_pool2d(net, [2, 2], 2, name='pool4') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='vgg_se4', filters=512) with tf.variable_scope('conv5', reuse=reuse): net = layers.conv2d(net, 512, name='conv5_1', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 512, name='conv5_2', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 512, name='conv5_3', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.max_pool2d(net, [2, 2], 2, name='pool5') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='vgg_se5', filters=512) return net
def createModel(self): print("CREATING MODEL") self.global_step = tf.Variable(0, trainable=False) self.is_training = tf.placeholder_with_default(True, [], name='is_training') self.learning_rate = layers.decayLearningRate(LEARNING_RATE, self.global_step, DECAY_STEPS, DECAY_RATE) self.X = tf.placeholder(dtype=tf.float32, shape=[ None, self.dataset.frames, self.dataset.h, self.dataset.w, self.dataset.c ]) self.Y = tf.placeholder(dtype=tf.int32, shape=[None]) self.Yoh = layers.toOneHot(self.Y, self.dataset.num_classes) net = self.X conv3d_kernel = [3, 3, 3] max3d_pool_kernel = [3, 3, 2] bn_params = { 'decay': 0.999, 'center': True, 'scale': True, 'epsilon': 0.001, 'updates_collections': None, 'is_training': self.is_training } net = layers.transpose(net, [0, 2, 3, 1, 4]) net = layers.conv3d( net, filters=48, kernel_size=conv3d_kernel, padding='VALID', stride=2, name='conv1', normalizer_fn=layers.batchNormalization, normalizer_params=bn_params, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.max_pool3d(net, max3d_pool_kernel, 2, padding='VALID', name='max_pool1') net = layers.squeeze_and_excite3d(net, indexHeight=1, indexWidth=2, indexSeq=3, name='se1', filters=48) net = layers.conv3d( net, filters=256, kernel_size=conv3d_kernel, padding='VALID', stride=2, name='conv2', normalizer_fn=layers.batchNormalization, normalizer_params=bn_params, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.max_pool3d(net, max3d_pool_kernel, 2, padding='VALID', name='max_pool2') net = layers.squeeze_and_excite3d(net, indexHeight=1, indexWidth=2, indexSeq=3, name='se2', filters=256) net = layers.reshape( net, [-1, net.shape[1], net.shape[2], net.shape[3] * net.shape[4]]) net = layers.conv2d( net, filters=512, kernel_size=3, padding='SAME', stride=1, name='conv3', normalizer_fn=layers.batchNormalization, normalizer_params=bn_params, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='se3', filters=512) net = layers.conv2d( net, filters=512, kernel_size=3, padding='SAME', stride=1, name='conv4', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='se4', filters=512) net = layers.conv2d( net, filters=512, kernel_size=3, padding='SAME', stride=1, name='conv5', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.max_pool2d(net, 3, 1, padding='VALID', name='max_pool5') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='se5', filters=512) net = layers.flatten(net, name='flatten') net = layers.fc( net, 4096, name='fc6', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) net = layers.fc( net, 4096, name='fc7', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) self.logits = layers.fc( net, self.dataset.num_classes, activation_fn=None, name='fc8', weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE)) self.preds = layers.softmax(self.logits) cross_entropy_loss = layers.reduce_mean( layers.softmax_cross_entropy(logits=self.logits, labels=self.Yoh)) regularization_loss = tf.get_collection( tf.GraphKeys.REGULARIZATION_LOSSES) self.loss = cross_entropy_loss + REGULARIZER_SCALE * tf.reduce_sum( regularization_loss) self.opt = layers.adam(self.learning_rate) self.train_op = self.opt.minimize(self.loss, global_step=self.global_step) self.accuracy, self.precision, self.recall = self.createSummaries( self.Yoh, self.preds, self.loss, self.learning_rate)
def vgg16(self, net, reuse=False): with tf.variable_scope('vgg_16', reuse=reuse): bn_params = {'decay': 0.999, 'center': True, 'scale': True, 'epsilon': 0.001, 'updates_collections': None, 'is_training': self.is_training} with tf.variable_scope('conv1', reuse=reuse): net = layers.conv2d(net, 64, name='conv1_1', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 64, name='conv1_2', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.max_pool2d(net, [2, 2], 2, name='pool1') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='vgg_se1', filters=64) with tf.variable_scope('conv2', reuse=reuse): net = layers.conv2d(net, 128, name='conv2_1', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 128, name='conv2_2', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.max_pool2d(net, [2, 2], 2, name='pool2') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='vgg_se2', filters=128) with tf.variable_scope('conv3', reuse=reuse): net = layers.conv2d(net, 256, name='conv3_1', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 256, name='conv3_2', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 256, name='conv3_3', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.max_pool2d(net, [2, 2], 2, name='pool3') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='vgg_se3', filters=256) with tf.variable_scope('conv4', reuse=reuse): net = layers.conv2d(net, 512, name='conv4_1', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 512, name='conv4_2', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 512, name='conv4_3', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.max_pool2d(net, [2, 2], 2, name='pool4') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='vgg_se4', filters=512) with tf.variable_scope('conv5', reuse=reuse): net = layers.conv2d(net, 512, name='conv5_1', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 512, name='conv5_2', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.conv2d(net, 512, name='conv5_3', reuse=reuse, weights_regularizer=layers.l2_regularizer(REGULARIZER_SCALE), normalizer_fn=layers.batchNormalization, normalizer_params=bn_params) net = layers.max_pool2d(net, [2, 2], 2, name='pool5') net = layers.squeeze_and_excite2d(net, indexHeight=1, indexWidth=2, name='vgg_se5', filters=512) return net