-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
60 lines (46 loc) · 1.62 KB
/
Copy pathutil.py
File metadata and controls
60 lines (46 loc) · 1.62 KB
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
55
56
57
58
59
60
"""
Author: Sigve Rokenes
Date: February, 2019
Utility functions for variational autoencoder
"""
import skimage as sk
from skimage import io
import tensorflow as tf
import numpy as np
# ===================================== #
# #
# Utility Functions #
# #
# ===================================== #
def print_tensor(tensor, name):
shape = tensor.get_shape()
size = 1
for dim in shape.as_list():
if dim is not None:
size *= int(dim)
print("{:8} {:20} {:8}".format(name, str(shape), str(size)))
def conv(source, filters, strides=2, kernel=3, activation=tf.nn.leaky_relu):
return tf.layers.conv2d(source, filters, kernel,
strides=strides, padding="same", activation=activation)
def deconv(source, filters, strides=2, kernel=3, activation=tf.nn.leaky_relu):
return tf.layers.conv2d_transpose(source, filters, kernel,
strides=strides, padding="same", activation=activation)
def gray2rgb(img):
rgb = []
for row in img:
n_row = []
for pix in row:
if type(pix) is int:
n_row.append([pix, pix, pix])
else:
value = np.mean(pix)
n_row.append([value, value, value])
rgb.append(n_row)
return np.array(rgb)
def save_image(filename, img, resize=None):
img = np.clip(np.array(img), 0, 1)
if np.shape(img)[2] == 1:
img = gray2rgb(img)
if resize:
img = sk.transform.resize(img, resize)
sk.io.imsave(filename, img)