File size: 639 Bytes
6250360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#encoding=utf-8
"""
for theano shortcuts
"""
import theano
import theano.tensor as T
import util.rand

trng = T.shared_randomstreams.RandomStreams(util.rand.randint())
scan_until = theano.scan_module.until

def add_noise(input, noise_level):
    noise = trng.binomial(size = input.shape, n = 1, p = 1 - noise_level)
    return noise * input

def crop_into(large, small):
    """
    center crop large image into small.
    both 'large' and 'small' are 4D: (batch_size, channels, h, w)
    """
    
    h1, w1 = large.shape[2:]
    h2, w2 = small.shape[2:]
    y, x = (h1 - h2) / 2, (w1 - h2)/2
    return large[:, :, y: y + h2, x: x + w2 ]