/
semantic_pgm_var_loss_1019.py
63 lines (61 loc) · 1.95 KB
/
semantic_pgm_var_loss_1019.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
# encoding: utf-8
import sys
sys.path.insert(0,"python")
import caffe
import numpy as np
from PIL import Image
from skimage import data, img_as_float
from skimage.restoration import denoise_tv_chambolle, denoise_bilateral
from skimage import feature
from skimage import measure
from extract_segmentation_nyud import extract_seg
from bidirectional import probabilitygraph
####
#input data:rgb, depth
#output var(act*depth), the top of the loss2
#output constant epsilon. the second top of the loss2
##
def predict_seg( bottom):
rgb = bottom[0].data
# print "rbg", rgb
# print rgb.shape
# depth = bottom[1].data
predict_seg = probabilitygraph(rgb)
return predict_seg
class Pre_SegLayer(caffe.Layer):
def setup(self, bottom, top):
pass
# check input pair
# if len(bottom) != 3:
# raise Exception("Need two inputs to compute distance.")
def reshape(self, bottom, top):
# pass
self.rgb = bottom[0].data[0]
top[0].reshape(1,*self.rgb.shape)
top[1].reshape(1,*self.rgb.shape)
def forward(self, bottom, top):
from scipy import ndimage
try:
seg = predict_seg(bottom)
seg = np.uint8(seg*255)
rgb = bottom[0].data
depth = bottom[1]
cont = np.gradient(seg)
cont = cont[0]*cont[0] + cont[1]*cont[1]
cont = np.astype(float)
cont = 1/(1+cont)
cont = np.transpose(cont,(2,0,1))
# top[0].data[...] = rgb + cont.reshape(1,*cont.shape)
act = cont[:,:,0]
# rgb = bottom[0].data
top[0].data[...] = rgb
epsilon = np.ones(depth)
epsilon = np.astype(float)
var = np.log10(np.var(np.dot(depth,act)))
print var
epsilon *= np.log2(var)
except Exception as e:
print e
def backward(self, top, propagate_down, bottom):
pass