hieupt commited on
Commit
5378323
·
verified ·
1 Parent(s): 31f503b

Upload crop.py

Browse files
Files changed (1) hide show
  1. model/crop.py +23 -0
model/crop.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def centre_crop(x, target):
2
+ '''
3
+ Center-crop 3-dim. input tensor along last axis so it fits the target tensor shape
4
+ :param x: Input tensor
5
+ :param target: Shape of this tensor will be used as target shape
6
+ :return: Cropped input tensor
7
+ '''
8
+ if x is None:
9
+ return None
10
+ if target is None:
11
+ return x
12
+
13
+ target_shape = target.shape
14
+ diff = x.shape[-1] - target_shape[-1]
15
+ assert (diff % 2 == 0)
16
+ crop = diff // 2
17
+
18
+ if crop == 0:
19
+ return x
20
+ if crop < 0:
21
+ raise ArithmeticError
22
+
23
+ return x[:, :, crop:-crop].contiguous()