-
Notifications
You must be signed in to change notification settings - Fork 10
/
ops.py
36 lines (29 loc) · 1.19 KB
/
ops.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
from __future__ import division
import tensorflow as tf
import numpy as np
import scipy.stats as st
mean_RGB = np.array([123.68, 116.779, 103.939])
def preprocess(img):
return (img - mean_RGB)/255
def postprocess(img):
return np.round(np.clip(img*255 + mean_RGB, 0, 255)).astype(np.uint8)
def lrelu(x , alpha = 0.2):
return tf.maximum(x , alpha*x)
def sigmoid_cross_entropy_with_logits(x, y):
try:
return tf.nn.sigmoid_cross_entropy_with_logits(logits=x, labels=y)
except:
return tf.nn.sigmoid_cross_entropy_with_logits(logits=x, targets=y)
def gauss_kernel(kernlen=21, nsig=3, channels=1):
interval = (2*nsig+1.)/(kernlen)
x = np.linspace(-nsig-interval/2., nsig+interval/2., kernlen+1)
kern1d = np.diff(st.norm.cdf(x))
kernel_raw = np.sqrt(np.outer(kern1d, kern1d))
kernel = kernel_raw/kernel_raw.sum()
out_filter = np.array(kernel, dtype = np.float32)
out_filter = out_filter.reshape((kernlen, kernlen, 1, 1))
out_filter = np.repeat(out_filter, channels, axis = 2)
return out_filter
def gaussian_blur(x):
kernel_var = gauss_kernel(21, 3, 3)
return tf.nn.depthwise_conv2d(x, kernel_var, [1, 1, 1, 1], padding='SAME')