Future Frame Prediction for Anomaly Detection 代码学习

Future Frame Prediction for Anomaly Detection – A New Baseline

这是一篇CVPR2018的文章,是中科大高盛华团队的工作,目前代码已经放出。

以下都是我根据自身需求量身打造的,也供大家学习。

PS:看大神写的代码,优雅,简洁,简直就是一种享受。

1. [python] string.format() 格式化字符串

它通过{}和:来代替%。

print('training = {}'.format(tf.get_variable_scope().name))

功能非常之强大

http://www.jb51.net/article/63672.htm

:thumbsup::thumbsup::thumbsup::thumbsup::thumbsup:

2. [TF] tf.add_n([])

tf.add_n([p1, p2, p3….])函数是实现一个列表的元素的相加。就是输入的对象是一个列表,列表里的元素可以是向量,矩阵

g_loss = tf.add_n([lp_loss * lam_lp, gdl_loss * lam_gdl, adv_loss * lam_adv, flow_loss * lam_flow], name='g_loss')

https://blog.csdn.net/uestc_c2_403/article/details/72808839

???

3. [TF] tf.train.piecewise_constant()

g_step = tf.Variable(0, dtype=tf.int32, trainable=False, name='g_step')
#分段改变学习率
g_lrate = tf.train.piecewise_constant(g_step, boundaries=BOUNDARIES, values=LRATE_G)

其中BOUNDARIES, LRATE_G都是列表

:thumbsup::thumbsup:

4. [TF] 关键字global_step的使用

终于彻底明白这家伙的用途了。

global_step经常在滑动平均,学习速率变化的时候需要用到,这个参数在tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_steps)里面有,系统会自动更新这个参数的值,从1开始。

举例

global_steps = tf.Variable(0, trainable=False)  
learning_rate = tf.train.exponential_decay(0.1, global_steps, 10, 2, staircase=False)  
loss = tf.pow(w*x-y, 2)  
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_steps)  

https://blog.csdn.net/uestc_c2_403/article/details/72403833

:thumbsup::thumbsup:

5. [TF] GPU

或者在 程序开头

#在程序的开头
os.environ['CUDA_VISIBLE_DEVICES'] = '0' #使用 GPU 0
os.environ['CUDA_VISIBLE_DEVICES'] = '0,1' # 使用 GPU 0,1

and

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config, ...)
# 使用allow_growth option,刚一开始分配少量的GPU容量,然后按需慢慢的增加,由于不会释放
#内存,所以会导致碎片

and

# per_process_gpu_memory_fraction
gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.7)
config=tf.ConfigProto(gpu_options=gpu_options)
session = tf.Session(config=config, ...)
#设置每个GPU应该拿出多少容量给进程使用,0.7代表 70%

:thumbsup:

6.[python] configparaser 读写配置文件

以下列出如何读

# set training hyper-parameters of different datasets
improt configparaser
config = configparser.ConfigParser()
assert config.read(args.config)
# 本代码中用的ini文件
import ConfigParser
config = ConfigParser.RawConfigParser()
config.read('example.cfg')#cfg文件

其中配置文件格式如下:

[section] 
name=value
或者
name: value
"#"";" 表示注释
[DEFAULT] #设置默认的变量值,初始化

#举例
[ped2]
# for lp loss. e.g, 1 or 2 for l1 and l2 loss, respectively)
L_NUM = 2
# the power to which each gradient term is raised in GDL loss
ALPHA_NUM = 1
# the percentage of the adversarial loss to use in the combined loss
LAM_ADV = 0.05
# the percentage of the lp loss to use in the combined loss
LAM_LP = 1
# the percentage of the GDL loss to use in the combined loss
LAM_GDL = 1
# the percentage of the different frame loss
LAM_FLOW = 2
# For gray scale video, such as Ped2 and Ped1, learning rate of G and D star from 1e-4 and 1e-5, respectively.
LRATE_G = [0.0001, 0.00001]
LRATE_G_BOUNDARIES = [7000]
LRATE_D = [0.00001, 0.000001]
LRATE_D_BOUNDARIES = [7000]
#如何获取之中的值,举个例
const.L_NUM = config.getint(const.DATASET, 'L_NUM')

https://blog.csdn.net/miner_k/article/details/77857292

:thumbsup::thumbsup::thumbsup:

7. [numpy] concatenate()

numpy提供了numpy.concatenate((a1,a2,…), axis=0)函数。能够一次完成多个数组的拼接。其中a1,a2,…是数组类型的参数

a=np.array([1,2,3])
b=np.array([11,22,33])
c=np.array([44,55,66])
np.concatenate((a,b,c),axis=0)  
# 默认情况下,axis=0可以不写
array([ 1,  2,  3, 11, 22, 33, 44, 55, 66])
#对于一维数组拼接,axis的值不影响最后的结果

concatenate()效率高,适合大规模的数据拼接

https://blog.csdn.net/zyl1042635242/article/details/43162031

8.[TF] tf.data API

这个地方要学的太多了,收获满满~~

强烈建议看看机器之心的这篇文章和知乎专栏

:thumbsup::thumbsup::thumbsup::thumbsup::thumbsup:

9. [python] 类__call__

Python中的函数是一级对象。这意味着Python中的函数的引用可以作为输入传递到其他的函数/方法中,并在其中被执行。
而Python中类的实例(对象)可以被当做函数对待。也就是说,我们可以将它们作为输入传递到其他的函数/方法中并调用他们,正如我们调用一个正常的函数那样。而类中__call__()函数的意义正在于此。为了将一个类实例当做函数调用,我们需要在类中实现__call__()方法。也就是我们要在类中实现如下方法:def __call__(self, *args)。这个方法接受一定数量的变量作为输入。
假设x是X类的一个实例。那么调用x.__call__(1,2)等同于调用x(1,2)。这个实例本身在这里相当于一个函数。

class X(object):
    def __init__(self, a, b, range):
        self.a = a
        self.b = b
        self.range = range
    def __call__(self, a, b):
        self.a = a
        self.b = b
        print('__call__ with ({}, {})'.format(self.a, self.b))
    def __del__(self, a, b, range):
        del self.a
        del self.b
        del self.range
#
xInstance = X(1, 2, 3)
xInstance(1,2)#作为函数使用

https://blog.csdn.net/yaokai_assultmaster/article/details/70256621

:thumbsup::thumbsup::thumbsup::thumbsup:

10.[numpy] np.identity()

两个函数的原型为:

np.identity(n, dtype=None)

np.eye(N, M=None, k=0, dtype=<type ‘float’>)

np.identity只能创建方形矩阵

np.eye可以创建矩形矩阵,且k值可以调节,为1的对角线的位置偏离度,0居中,1向上偏离1,2偏离2,以此类推,-1向下偏离。值绝对值过大就偏离出去了,整个矩阵就全是0了

https://blog.csdn.net/gobsd/article/details/56485791

:thumbsup:

昨晚失眠,今天早点回去。。。先写这么多。

2018-04-10 23:01

11.[TF] Gradient Loss

这个地方取自paperDeep multi-scale video prediction beyond mean square error, 代码github

def gradient_loss(gen_frames, gt_frames, alpha):
    """
    Calculates the sum of GDL losses between the predicted and ground truth frames.

    @param gen_frames: The predicted frames at each scale.
    @param gt_frames: The ground truth frames at each scale
    @param alpha: The power to which each gradient term is raised.

    @return: The GDL loss.
    """
    # calculate the loss for each scale
    # create filters [-1, 1] and [[1],[-1]] for diffing to the left and down respectively.
    #水平和垂直方向各自3个卷积核,即每次只对一个通道进行卷积求梯度。水平卷积核大小[1x2x3]比如[[[-1,0,0],
    #[1,0,0]]]是R通道 1对应h方向,2对应w方向,3对应channel方向
    #垂直的[2x1x3] 同上
    channels = gen_frames.get_shape().as_list()[-1]
    pos = tf.constant(np.identity(channels), dtype=tf.float32)     # 3 x 3
    neg = -1 * pos
    filter_x = tf.expand_dims(tf.stack([neg, pos]), 0)  # [-1, 1]
    filter_y = tf.stack([tf.expand_dims(pos, 0), tf.expand_dims(neg, 0)])  # [[1],[-1]]
    strides = [1, 1, 1, 1]  # stride of (1, 1)
    padding = 'SAME'
    #每个的大小为height*width*3
    gen_dx = tf.abs(tf.nn.conv2d(gen_frames, filter_x, strides, padding=padding))
    gen_dy = tf.abs(tf.nn.conv2d(gen_frames, filter_y, strides, padding=padding))
    gt_dx = tf.abs(tf.nn.conv2d(gt_frames, filter_x, strides, padding=padding))
    gt_dy = tf.abs(tf.nn.conv2d(gt_frames, filter_y, strides, padding=padding))

    grad_diff_x = tf.abs(gt_dx - gen_dx)
    grad_diff_y = tf.abs(gt_dy - gen_dy)

    # condense into one tensor and avg
    #return gen_dx, gen_dy
    return tf.reduce_mean(grad_diff_x ** alpha + grad_diff_y ** alpha) 

其中 gen_frames, gt_frames都是tensor.这个必须是对3通道的图像进行计算,即水平垂直个3个边缘卷积核分别都RGB通道进行计算。

猜你喜欢

转载自blog.csdn.net/windows_peng/article/details/79905075
今日推荐