Commit 32f4366f authored by Yuxin Wu's avatar Yuxin Wu

better bilinear & xentropy

parent 1e71e8f9
#!/usr/bin/env python2
# -*- coding: UTF-8 -*-
# File: load_vgg16.py
# File: load-vgg16.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com>
import tensorflow as tf
......@@ -41,30 +41,30 @@ class Model(ModelDesc):
# 224
l = Conv2D('conv1_1', image, 64)
l = Conv2D('conv1_2', l, 64)
l = MaxPooling('pool1', l, 2, stride=2, padding='VALID')
l = MaxPooling('pool1', l, 2)
# 112
l = Conv2D('conv2_1', l, 128)
l = Conv2D('conv2_2', l, 128)
l = MaxPooling('pool2', l, 2, stride=2, padding='VALID')
l = MaxPooling('pool2', l, 2)
# 56
l = Conv2D('conv3_1', l, 256)
l = Conv2D('conv3_2', l, 256)
l = Conv2D('conv3_3', l, 256)
l = MaxPooling('pool3', l, 2, stride=2, padding='VALID')
l = MaxPooling('pool3', l, 2)
# 28
l = Conv2D('conv4_1', l, 512)
l = Conv2D('conv4_2', l, 512)
l = Conv2D('conv4_3', l, 512)
l = MaxPooling('pool4', l, 2, stride=2, padding='VALID')
l = MaxPooling('pool4', l, 2)
# 14
l = Conv2D('conv5_1', l, 512)
l = Conv2D('conv5_2', l, 512)
l = Conv2D('conv5_3', l, 512)
l = MaxPooling('pool5', l, 2, stride=2, padding='VALID')
l = MaxPooling('pool5', l, 2)
# 7
l = FullyConnected('fc6', l, 4096)
......
......@@ -30,7 +30,6 @@ def Conv2D(x, out_channel, kernel_shape,
:returns: a NHWC tensor
"""
in_shape = x.get_shape().as_list()
num_in = np.prod(in_shape[1:])
in_channel = in_shape[-1]
assert in_channel % split == 0
assert out_channel % split == 0
......
......@@ -121,6 +121,10 @@ def BilinearUpSample(x, shape):
ch = x.get_shape().as_list()[3]
shape = int(shape)
unpool_mat = np.zeros((shape, shape), dtype='float32')
unpool_mat[-1,-1] = 1
x = FixedUnPooling('unpool', x, shape, unpool_mat)
filter_shape = 2 * shape
w = bilinear_conv_filler(filter_shape)
w = np.repeat(w, ch * ch).reshape((filter_shape, filter_shape, ch, ch))
......@@ -128,9 +132,6 @@ def BilinearUpSample(x, shape):
tf.float32,
shape=(filter_shape, filter_shape, ch, ch))
unpool_mat = np.zeros((shape, shape), dtype='float32')
unpool_mat[-1,-1] = 1
x = FixedUnPooling('unpool', x, shape, unpool_mat)
output = tf.nn.conv2d(x, weight_var, [1,1,1,1], padding='SAME')
return output
......
......@@ -69,12 +69,11 @@ def class_balanced_binary_class_cross_entropy(pred, label, name='cross_entropy_l
count_neg = tf.reduce_sum(1. - y)
count_pos = tf.reduce_sum(y)
total = tf.add(count_neg, count_pos)
beta = tf.truediv(count_neg, total)
beta = count_neg / (count_neg + count_pos)
eps = 1e-8
loss_pos = tf.mul(-beta, tf.reduce_sum(tf.mul(tf.log(tf.abs(z) + eps), y), 1))
loss_neg = tf.mul(1. - beta, tf.reduce_sum(tf.mul(tf.log(tf.abs(1. - z) + eps), 1. - y), 1))
loss_pos = -beta * tf.reduce_mean(y * tf.log(tf.abs(z) + eps), 1)
loss_neg = (1. - beta) * tf.reduce_mean((1. - y) * tf.log(tf.abs(1. - z) + eps), 1)
cost = tf.sub(loss_pos, loss_neg)
cost = tf.reduce_mean(cost, name=name)
return cost
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment