def inference(self): with tf.variable_scope('conv1') as scope: kernel = tf.get_variable(name='weight', shape=[3, 3, 3, 32], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer1_conv/weights:0'])) biases = tf.get_variable(name='bias', shape=[32], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer1_conv/biases:0'])) net = tf.nn.conv2d(self.input, filter=kernel, strides=[1, 1, 1, 1], padding='SAME') net = tf.nn.bias_add(net, bias=biases) net = tf.nn.relu(net) print_activations(net) with tf.variable_scope('pool1') as scope: net = tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') print_activations(net) with tf.variable_scope('conv2') as scope: kernel = tf.get_variable(name='weight', shape=[3, 3, 32, 64], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer2_conv/weights:0'])) biases = tf.get_variable(name='bias', shape=[64], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer2_conv/biases:0'])) net = tf.nn.conv2d(net, filter=kernel, strides=[1, 1, 1, 1], padding='SAME') net = tf.nn.bias_add(net, bias=biases) net = tf.nn.relu(net) print_activations(net) with tf.variable_scope('pool2') as scope: net = tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') print_activations(net) with tf.variable_scope('conv3') as scope: kernel = tf.get_variable(name='weight', shape=[3, 3, 64, 128], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer3_conv/weights:0'])) biases = tf.get_variable(name='bias', shape=[128], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer3_conv/biases:0'])) net = tf.nn.conv2d(net, filter=kernel, strides=[1, 1, 1, 1], padding='SAME') net = tf.nn.bias_add(net, bias=biases) net = tf.nn.relu(net) print_activations(net) with tf.variable_scope('pool3') as scope: net = tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') print_activations(net) with tf.variable_scope('conv4') as scope: kernel = tf.get_variable(name='weight', shape=[3, 3, 128, 256], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer4_conv/weights:0'])) biases = tf.get_variable(name='bias', shape=[256], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer4_conv/biases:0'])) net = tf.nn.conv2d(net, filter=kernel, strides=[1, 1, 1, 1], padding='SAME') net = tf.nn.bias_add(net, bias=biases) net = tf.nn.relu(net) print_activations(net) with tf.variable_scope('pool4') as scope: net = tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') print_activations(net) with tf.variable_scope('conv5') as scope: kernel = tf.get_variable(name='weight', shape=[3, 3, 256, 256], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.01)) biases = tf.get_variable(name='bias', shape=[256], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.01)) net = tf.nn.conv2d(net, filter=kernel, strides=[1, 1, 1, 1], padding='SAME') net = tf.nn.bias_add(net, bias=biases) net = tf.nn.relu(net) print_activations(net) with tf.variable_scope('pool5') as scope: net = tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') print_activations(net) with tf.variable_scope('fc1') as scope: net = tf.reshape(net, [-1, 3 * 3 * 256]) weight = tf.get_variable(name='weight', shape=[3 * 3 * 256, 512], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer5_fc/weights:0'])) biases = tf.get_variable(name='bias', shape=[512], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer5_fc/biases:0'])) net = tf.matmul(net, weight) + biases net = tf.nn.relu(net) print_activations(net) with tf.variable_scope('fc2') as scope: weight = tf.get_variable(name='weight', shape=[512, 256], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer6_fc/weights:0'])) biases = tf.get_variable(name='bias', shape=[256], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer6_fc/biases:0'])) net = tf.matmul(net, weight) + biases net = tf.nn.relu(net) print_activations(net) with tf.variable_scope('fc3') as scope: weight = tf.get_variable(name='weight', shape=[256, 28], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.01)) biases = tf.get_variable(name='bias', shape=[28], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.01)) net = tf.matmul(net, weight) + biases net = tf.nn.relu(net) print_activations(net) net = tf.reshape(net, [-1, 2, 2, 7]) print_activations(net) self.predict = net return net
class hugenet(): def __init__(self, variables): self.pretrain_variables = variables self.lambda_noobj = FLAGS.lambda_noobj self.lambda_coord = FLAGS.lambda_coord self.label = tf.placeholder(tf.float32, shape=[None, 2, 2, 7]) self.input = tf.placeholder(tf.float32, [None, 100, 100, 3]) def inference(self): with tf.variable_scope('conv1') as scope: kernel = tf.get_variable(name='weight', shape=[3, 3, 3, 32], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer1_conv/weights:0'])) biases = tf.get_variable(name='bias', shape=[32], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer1_conv/biases:0'])) net = tf.nn.conv2d(self.input, filter=kernel, strides=[1, 1, 1, 1], padding='SAME') net = tf.nn.bias_add(net, bias=biases) net = tf.nn.relu(net) print_activations(net) with tf.variable_scope('pool1') as scope: net = tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') print_activations(net) with tf.variable_scope('conv2') as scope: kernel = tf.get_variable(name='weight', shape=[3, 3, 32, 64], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer2_conv/weights:0'])) biases = tf.get_variable(name='bias', shape=[64], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer2_conv/biases:0'])) net = tf.nn.conv2d(net, filter=kernel, strides=[1, 1, 1, 1], padding='SAME') net = tf.nn.bias_add(net, bias=biases) net = tf.nn.relu(net) print_activations(net) with tf.variable_scope('pool2') as scope: net = tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') print_activations(net) with tf.variable_scope('conv3') as scope: kernel = tf.get_variable(name='weight', shape=[3, 3, 64, 128], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer3_conv/weights:0'])) biases = tf.get_variable(name='bias', shape=[128], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer3_conv/biases:0'])) net = tf.nn.conv2d(net, filter=kernel, strides=[1, 1, 1, 1], padding='SAME') net = tf.nn.bias_add(net, bias=biases) net = tf.nn.relu(net) print_activations(net) with tf.variable_scope('pool3') as scope: net = tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') print_activations(net) with tf.variable_scope('conv4') as scope: kernel = tf.get_variable(name='weight', shape=[3, 3, 128, 256], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer4_conv/weights:0'])) biases = tf.get_variable(name='bias', shape=[256], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer4_conv/biases:0'])) net = tf.nn.conv2d(net, filter=kernel, strides=[1, 1, 1, 1], padding='SAME') net = tf.nn.bias_add(net, bias=biases) net = tf.nn.relu(net) print_activations(net) with tf.variable_scope('pool4') as scope: net = tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') print_activations(net) with tf.variable_scope('conv5') as scope: kernel = tf.get_variable(name='weight', shape=[3, 3, 256, 256], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.01)) biases = tf.get_variable(name='bias', shape=[256], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.01)) net = tf.nn.conv2d(net, filter=kernel, strides=[1, 1, 1, 1], padding='SAME') net = tf.nn.bias_add(net, bias=biases) net = tf.nn.relu(net) print_activations(net) with tf.variable_scope('pool5') as scope: net = tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') print_activations(net) with tf.variable_scope('fc1') as scope: net = tf.reshape(net, [-1, 3 * 3 * 256]) weight = tf.get_variable(name='weight', shape=[3 * 3 * 256, 512], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer5_fc/weights:0'])) biases = tf.get_variable(name='bias', shape=[512], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer5_fc/biases:0'])) net = tf.matmul(net, weight) + biases net = tf.nn.relu(net) print_activations(net) with tf.variable_scope('fc2') as scope: weight = tf.get_variable(name='weight', shape=[512, 256], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer6_fc/weights:0'])) biases = tf.get_variable(name='bias', shape=[256], dtype=tf.float32, initializer=tf.constant_initializer(self.pretrain_variables['layer6_fc/biases:0'])) net = tf.matmul(net, weight) + biases net = tf.nn.relu(net) print_activations(net) with tf.variable_scope('fc3') as scope: weight = tf.get_variable(name='weight', shape=[256, 28], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.01)) biases = tf.get_variable(name='bias', shape=[28], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.01)) net = tf.matmul(net, weight) + biases net = tf.nn.relu(net) print_activations(net) net = tf.reshape(net, [-1, 2, 2, 7]) print_activations(net) self.predict = net return net def train(self, net): pobj_pred = net[:, :, :, 0:1] pobj_truth = self.label[:, :, :, 0:1] print_activations(pobj_pred) print_activations(pobj_truth) box_pred = net[:, :, :, 1:5] box_truth = self.label[:, :, :, 1:5] print_activations(box_pred) print_activations(box_truth) pclass_pred = net[:, :, :, 5:] pclass_truth = self.label[:, :, :, 5:] print_activations(pclass_pred) print_activations(pclass_truth) IOU = cacl_iou(box1=box_pred, box2=box_truth) print_activations(IOU) object_mask = tf.cast(IOU >= 0.5, tf.float32) * pobj_truth print_activations(object_mask) noonject_mask = tf.ones_like(object_mask, dtype=tf.float32) - object_mask print_activations(noonject_mask) loss_obj = tf.reduce_sum(tf.square(pobj_truth - pobj_pred) * object_mask) print_activations(loss_obj) loss_noobj = tf.reduce_sum(tf.square(pobj_truth - pobj_pred) * noonject_mask) print_activations(loss_noobj) loss_coord = tf.reduce_sum(tf.reduce_sum(tf.square(box_pred[:, :, :,0:2] - box_truth[:, :, :, 0:2]), axis=3, keep_dims=True) * object_mask) + tf.reduce_sum(tf.reduce_sum(tf.square(tf.sqrt(box_pred[:, :, :, 2:4]) - tf.sqrt(box_truth[:, :, :, 2:4])), axis=3, keep_dims=True) * object_mask) print_activations(loss_coord) print_activations(box_pred[:, :, :, 2:]) print_activations(box_pred[:, :, :, 0:2]) loss_class = tf.reduce_sum(tf.square(pclass_pred - pclass_truth) * pobj_truth) print_activations(loss_class) total_loss = loss_obj + self.lambda_noobj * loss_noobj + self.lambda_coord * loss_coord + loss_class train_step = tf.train.AdamOptimizer(1e-2).minimize(total_loss / FLAGS.train_batch_size) image_batch_raw, label_batch_raw, filename_batch_raw = car_input(is_random=True, is_train=True) image_batch_test, label_batch_test, filename_batch_test = car_input(is_random=False, is_train=False) train_IOU = tf.reduce_mean(IOU) with tf.Session(config=config) as sess: tf.global_variables_initializer().run() tf.local_variables_initializer().run() coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord, sess=sess) for i in range(int(FLAGS.num_epoches * FLAGS.train_set_size / FLAGS.train_batch_size)): image_batch, label_batch = sess.run([image_batch_raw, label_batch_raw]) sess.run([train_step], feed_dict={self.input: image_batch, self.label: label_batch}) if not i % 10: predict, loss, train_iou, iou = sess.run([net, total_loss, train_IOU, IOU], feed_dict={ self.input: image_batch, self.label: label_batch}) print('step %d, loss: %.4f, train_IOU: %.4f ' % (i, loss, train_iou)) image_test, label_test = sess.run([image_batch_test, label_batch_test]) loss = sess.run(total_loss, feed_dict={self.input: image_test, self.label: label_test}) print(loss / FLAGS.test_batch_size) coord.request_stop() coord.join(threads)
def inference(self): with slim.arg_scope([slim.conv2d], kernel_size=3, reuse=None, trainable=True): with slim.arg_scope([slim.max_pool2d], kernel_size=2, stride=2, padding='VALID'): net = slim.conv2d(self.input_image, 32, scope='layer1_conv') print_activations(net) net = slim.max_pool2d(net, scope='pool1') print_activations(net) net = slim.conv2d(net, 64, scope='layer2_conv') print_activations(net) net = slim.max_pool2d(net, scope='pool2') print_activations(net) net = slim.conv2d(net, 128, scope='layer3_conv') print_activations(net) net = slim.max_pool2d(net, scope='pool3') print_activations(net) net = slim.conv2d(net, 256, scope='layer4_conv') print_activations(net) net = slim.max_pool2d(net, scope='pool4') print_activations(net) net = tf.reshape(net, [-1, 3 * 3 * 256]) net = slim.fully_connected(net, 512, scope='layer5_fc') print_activations(net) net = slim.fully_connected(net, 256, scope='layer6_fc') print_activations(net) net=slim.fully_connected(net,self.num_class,activation_fn=None, scope='layer7_fc') print_activations(net) net = slim.softmax(net) print_activations(net) return net
def train(self, net): pobj_pred = net[:, :, :, 0:1] pobj_truth = self.label[:, :, :, 0:1] print_activations(pobj_pred) print_activations(pobj_truth) box_pred = net[:, :, :, 1:5] box_truth = self.label[:, :, :, 1:5] print_activations(box_pred) print_activations(box_truth) pclass_pred = net[:, :, :, 5:] pclass_truth = self.label[:, :, :, 5:] print_activations(pclass_pred) print_activations(pclass_truth) IOU = cacl_iou(box1=box_pred, box2=box_truth) print_activations(IOU) object_mask = tf.cast(IOU >= 0.5, tf.float32) * pobj_truth print_activations(object_mask) noonject_mask = tf.ones_like(object_mask, dtype=tf.float32) - object_mask print_activations(noonject_mask) loss_obj = tf.reduce_sum(tf.square(pobj_truth - pobj_pred) * object_mask) print_activations(loss_obj) loss_noobj = tf.reduce_sum(tf.square(pobj_truth - pobj_pred) * noonject_mask) print_activations(loss_noobj) loss_coord = tf.reduce_sum(tf.reduce_sum(tf.square(box_pred[:, :, :,0:2] - box_truth[:, :, :, 0:2]), axis=3, keep_dims=True) * object_mask) + tf.reduce_sum(tf.reduce_sum(tf.square(tf.sqrt(box_pred[:, :, :, 2:4]) - tf.sqrt(box_truth[:, :, :, 2:4])), axis=3, keep_dims=True) * object_mask)
def inference(images, classes, batch_size): """Build the AlexNet logs. Args: batch_size: batch size images: Images Tensor classes: Image classes Returns: out: prediction image label. """ # conv1 with tf.name_scope('conv1') as scope: kernel = tf.Variable(tf.truncated_normal([11, 11, 3, 64]), name='weights') conv = tf.nn.conv2d(images, kernel, [1, 4, 4, 1], padding='SAME') biases = tf.Variable(tf.constant(0.1, shape=[64], dtype=utils.data_type()), name='biases') bias = tf.nn.bias_add(conv, biases) conv1 = tf.nn.relu(bias, name=scope) utils.print_activations(conv1) # lrn1 with tf.name_scope('lrn1') as scope: lrn1 = tf.nn.lrn(conv1, alpha=1e-4, beta=0.75, depth_radius=2, bias=2.0) # pool1 pool1 = tf.nn.max_pool(lrn1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool1') utils.print_activations(pool1) # conv2 with tf.name_scope('conv2') as scope: kernel = tf.Variable(tf.truncated_normal([5, 5, 64, 192]), name='weights') conv = tf.nn.conv2d(pool1, kernel, [1, 1, 1, 1], padding='SAME') biases = tf.Variable(tf.constant(0.1, shape=[192], dtype=utils.data_type()), name='biases') bias = tf.nn.bias_add(conv, biases) conv2 = tf.nn.relu(bias, name=scope) utils.print_activations(conv2) # lrn2 with tf.name_scope('lrn2') as scope: lrn2 = tf.nn.lrn(conv2, alpha=1e-4, beta=0.75, depth_radius=2, bias=2.0) # pool2 pool2 = tf.nn.max_pool(lrn2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool2') utils.print_activations(pool2) # conv3 with tf.name_scope('conv3') as scope: kernel = tf.Variable(tf.truncated_normal([3, 3, 192, 384]), name='weights') conv = tf.nn.conv2d(pool2, kernel, [1, 1, 1, 1], padding='SAME') biases = tf.Variable(tf.constant(0.1, shape=[384], dtype=utils.data_type()), name='biases') bias = tf.nn.bias_add(conv, biases) conv3 = tf.nn.relu(bias, name=scope) utils.print_activations(conv3) # conv4 with tf.name_scope('conv4') as scope: kernel = tf.Variable(tf.truncated_normal([3, 3, 384, 256]), name='weights') conv = tf.nn.conv2d(conv3, kernel, [1, 1, 1, 1], padding='SAME') biases = tf.Variable(tf.constant(0.1, shape=[256], dtype=utils.data_type()), name='biases') bias = tf.nn.bias_add(conv, biases) conv4 = tf.nn.relu(bias, name=scope) utils.print_activations(conv4) # conv5 with tf.name_scope('conv5') as scope: kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256]), name='weights') conv = tf.nn.conv2d(conv4, kernel, [1, 1, 1, 1], padding='SAME') biases = tf.Variable(tf.constant(0.1, shape=[256], dtype=utils.data_type()), name='biases') bias = tf.nn.bias_add(conv, biases) conv5 = tf.nn.relu(bias, name=scope) utils.print_activations(conv5) # pool5 pool5 = tf.nn.max_pool(conv5, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool5') utils.print_activations(pool5) # fully 1 with tf.name_scope('fc1') as scope: reshape = tf.reshape(pool5, [batch_size, -1]) dim = reshape.get_shape()[1].value kernel = tf.Variable(tf.truncated_normal([dim, 4096]), name='weights') biases = tf.Variable(tf.constant(0.1, shape=[4096], dtype=utils.data_type()), name='biases') fc1 = tf.nn.relu(tf.matmul(reshape, kernel) + biases) # fully 2 with tf.name_scope('fc2') as scope: kernel = tf.Variable(tf.truncated_normal([4096, 4096]), name='weights') biases = tf.Variable(tf.constant(0.1, shape=[4096], dtype=utils.data_type()), name='biases') fc2 = tf.nn.relu(tf.matmul(fc1, kernel) + biases) # fully 3 with tf.name_scope('fc3') as scope: kernel = tf.Variable(tf.truncated_normal([4096, classes]), name='weights') biases = tf.Variable(tf.constant(0.1, shape=[classes], dtype=utils.data_type()), name='biases') out = tf.add(tf.matmul(fc2, kernel), biases) return out