-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutil.py
50 lines (40 loc) · 1.37 KB
/
util.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
import tensorflow as tf
import numpy as np
from functools import partial
# Utility functions for data processing.
def get_resize_func(method):
"""Resampling."""
if method == 'bicubic':
return tf.image.resize_bicubic
elif method == 'nn':
return tf.image.resize_nearest_neighbor
else:
raise
def image_to_patches(image, scale=1):
patch_height = 108 / scale
patch_width = 108 / scale
patch_overlap = 12 / scale
patches = tf.extract_image_patches(
image, [1, patch_height, patch_width, 1],
[1, patch_height - 2 * patch_overlap, patch_width - 2 * patch_overlap, 1],
[1, 1, 1, 1],
padding='VALID')
return tf.reshape(patches, [
tf.shape(patches)[0] * tf.shape(patches)[1] * tf.shape(patches)[2],
patch_height, patch_width, 3
])
def crop_center(image, target_shape):
origin_shape = tf.shape(image)[1:3]
return tf.slice(image, [
0, (origin_shape[0] - target_shape[0]) / 2,
(origin_shape[1] - target_shape[1]) / 2, 0
], [-1, target_shape[0], target_shape[1], -1])
def crop_by_pixel(x, num):
shape = tf.shape(x)[1:3]
return tf.slice(x, [0, num, num, 0],
[-1, shape[0] - 2 * num, shape[1] - 2 * num, -1])
def pad_boundary(image, boundary_size=15):
return tf.pad(
image, [[0, 0], [boundary_size, boundary_size],
[boundary_size, boundary_size], [0, 0]],
mode="SYMMETRIC")