-
Notifications
You must be signed in to change notification settings - Fork 15
/
abstract_network.py
executable file
·135 lines (111 loc) · 5.77 KB
/
abstract_network.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import tensorflow as tf
import numpy as np
import math
import glob
from matplotlib import pyplot as plt
import os, sys, re
def lrelu(x, rate=0.1):
# return tf.nn.relu(x)
return tf.maximum(tf.minimum(x * rate, 0), x)
conv2d = tf.contrib.layers.convolution2d
conv2d_t = tf.contrib.layers.convolution2d_transpose
fc_layer = tf.contrib.layers.fully_connected
def conv2d_bn_lrelu(inputs, num_outputs, kernel_size, stride):
conv = tf.contrib.layers.convolution2d(inputs, num_outputs, kernel_size, stride,
weights_initializer=tf.random_normal_initializer(stddev=0.02),
weights_regularizer=tf.contrib.layers.l2_regularizer(2.5e-5),
activation_fn=tf.identity)
conv = tf.contrib.layers.batch_norm(conv)
conv = lrelu(conv)
return conv
def conv2d_t_bn_relu(inputs, num_outputs, kernel_size, stride):
conv = tf.contrib.layers.convolution2d_transpose(inputs, num_outputs, kernel_size, stride,
weights_initializer=tf.random_normal_initializer(stddev=0.02),
weights_regularizer=tf.contrib.layers.l2_regularizer(2.5e-5),
activation_fn=tf.identity)
conv = tf.contrib.layers.batch_norm(conv)
conv = lrelu(conv)
return conv
def conv2d_t_bn(inputs, num_outputs, kernel_size, stride):
conv = tf.contrib.layers.convolution2d_transpose(inputs, num_outputs, kernel_size, stride,
weights_initializer=tf.random_normal_initializer(stddev=0.02),
weights_regularizer=tf.contrib.layers.l2_regularizer(2.5e-5),
activation_fn=tf.identity)
conv = tf.contrib.layers.batch_norm(conv)
return conv
def fc_bn_lrelu(inputs, num_outputs):
fc = tf.contrib.layers.fully_connected(inputs, num_outputs,
weights_initializer=tf.random_normal_initializer(stddev=0.02),
weights_regularizer=tf.contrib.layers.l2_regularizer(2.5e-5),
activation_fn=tf.identity)
fc = tf.contrib.layers.batch_norm(fc)
fc = lrelu(fc)
return fc
def fc_bn_relu(inputs, num_outputs):
fc = tf.contrib.layers.fully_connected(inputs, num_outputs,
weights_initializer=tf.random_normal_initializer(stddev=0.02),
weights_regularizer=tf.contrib.layers.l2_regularizer(2.5e-5),
activation_fn=tf.identity)
fc = tf.contrib.layers.batch_norm(fc)
fc = tf.nn.relu(fc)
return fc
class Network:
def __init__(self, dataset):
self.dataset = dataset
self.batch_size = dataset.batch_size
self.learning_rate_placeholder = tf.placeholder(shape=[], dtype=tf.float32, name="lr_placeholder")
gpu_options = tf.GPUOptions(allow_growth=True)
self.sess = tf.InteractiveSession(config=tf.ConfigProto(gpu_options=gpu_options, allow_soft_placement=True))
# A unique name should be given to each instance of subclasses during initialization
self.name = "default"
# These should be updated accordingly
self.iteration = 0
self.learning_rate = 0.0
self.read_only = False
def make_model_path(self):
if not os.path.isdir("models/" + self.name):
os.makedirs("models/" + self.name)
def print_network(self):
self.make_model_path()
if os.path.isdir("models/" + self.name):
for f in os.listdir("models/" + self.name):
if re.search(r"events.out*", f):
os.remove(os.path.join("models/" + self.name, f))
self.writer = tf.summary.FileWriter("models/" + self.name, self.sess.graph)
self.writer.flush()
""" Save network, if network file already exists back it up to models/old folder. Only one back up will be created
for each network """
def save_network(self):
if not self.read_only:
# Saver and Summary ops cannot run in GPU
with tf.device('/cpu:0'):
saver = tf.train.Saver()
self.make_model_path()
if not os.path.isdir("models/old"):
os.mkdir("models/old")
file_name = "models/" + self.name + "/" + self.name + ".ckpt"
if os.path.isfile(file_name):
os.rename(file_name, "models/old/" + self.name + ".ckpt")
saver.save(self.sess, file_name)
""" Either initialize or load network from file.
Always run this at end of initialization for every subclass to initialize Variables properly """
def init_network(self, restart=False):
self.sess.run(tf.global_variables_initializer())
if restart:
return
file_name = "models/" + self.name + "/" + self.name + ".ckpt"
if len(glob.glob(file_name + '*')) != 0:
saver = tf.train.Saver()
try:
saver.restore(self.sess, file_name)
except:
print("Warning: network load failed, reinitializing all variables", sys.exc_info()[0])
self.sess.run(tf.global_variables_initializer())
else:
print("No checkpoint file found, Initializing model from random")
""" This function should train on the given batch and return the training loss """
def train(self, batch_input, batch_target, condition=None):
return None
""" This function should take the input and return the reconstructed images """
def test(self, batch_input, condition=None):
return None