This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tensor_ops.py
55 lines (43 loc) · 1.63 KB
/
tensor_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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from keras import backend as K
from keras.engine.topology import Layer
import os
tf = K.tf
# Keras configuration directives
def SetActiveGPU(number=0):
"""
Set visibility of GPUs to the Tensorflow engine.
:param number: scalar or list of GPU indices
e.g. 0 for the 1st GPU, or [0,2] for the 1st and 3rd GPU
"""
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
if not isinstance(number,list): number=[number]
os.environ["CUDA_VISIBLE_DEVICES"] = ', '.join(map(str,number))
print 'Visible GPU(s):', os.environ["CUDA_VISIBLE_DEVICES"]
def GPUMemoryCap(fraction=1):
"""
Limit the amount of GPU memory that can be used by an active kernel.
:param fraction: in [0, 1], 1 = the entire available GPU memory.
"""
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = fraction
K.set_session(K.tf.Session(config=config))
# Metrics and losses
def plcc_tf(x, y):
"""PLCC metric"""
xc = x - K.mean(x)
yc = y - K.mean(y)
return K.mean(xc*yc) / (K.std(x)*K.std(y) + K.epsilon())
def earth_mover_loss(y_true, y_pred):
"""
Earth Mover's Distance loss.
Reproduced from https://github.com/titu1994/neural-image-assessment/blob/master/train_inception_resnet.py
"""
cdf_ytrue = K.cumsum(y_true, axis=-1)
cdf_ypred = K.cumsum(y_pred, axis=-1)
samplewise_emd = K.sqrt(K.mean(K.square(K.abs(cdf_ytrue - cdf_ypred)), axis=-1))
return K.mean(samplewise_emd)
def make_loss(loss, **params_defa):
def custom_loss(*args, **kwargs):
kwargs.update(params_defa)
return loss(*args, **kwargs)
return custom_loss