def _decoder(self, z): with tf.variable_scope('decoder', reuse=tf.AUTO_REUSE): z = fc(z, 3 * 3 * self._nd_latent, 'fc-z') z = tf.reshape(z, (-1, 3, 3, self._nd_latent)) h1 = conv2d_transpose(z, 256, name='up1', padding='VALID', training=self.is_training, use_bn=True) h2 = conv2d_transpose(h1, 128, name='up2', training=self.is_training, use_bn=True) h3 = conv2d_transpose(h2, 64, name='up3') recon = conv2d(h3, self._img_shape[-1], kernel_size=1, name='out') return tf.nn.sigmoid(recon)
def _generator(self, z, scope, reuse=False): with tf.variable_scope(scope, reuse=reuse): z = tf.reshape(z, (-1, 1, 1, self._nd_z)) h1 = conv2d_transpose(z, 1024, name='up1', padding='VALID') h2 = conv2d_transpose(h1, 512, name='up2', padding='VALID', training=self.is_training, use_bn=True) h3 = conv2d_transpose(h2, 256, name='up3', training=self.is_training, use_bn=True) h4 = conv2d_transpose(h3, 128, name='up4', training=self.is_training, use_bn=True) recon = conv2d(h4, self._img_shape[-1], kernel_size=1, name='out') return tf.nn.sigmoid(recon)
def decoder(self, z, training=True, reuse=None, name=None): # [None, z_dim] --> [None, 1024] h = dense(z, 1024, reuse=reuse, name='d_dense_1') h = batch_norm(h, training=training, reuse=reuse, name='d_bn_1') h = tf.nn.relu(h) # [None, 1024] --> [None, 7*7*128] h = dense(h, self.min_res*self.min_res*self.min_chans, reuse=reuse, name='d_dense_2') h = batch_norm(h, training=training, reuse=reuse, name='d_bn_2') h = tf.nn.relu(h) # [None, 7*7*128] --> [None, 7, 7, 128] h = tf.reshape(h, [-1, self.min_res, self.min_res, self.min_chans]) # [None, 7, 7, 128] --> [None, 14, 14, 64] h = conv2d_transpose(h, 64, kernel_size=4, strides=2, reuse=reuse, name='d_tconv_1') h = batch_norm(h, training=training, reuse=reuse, name='d_bn_3') h = tf.nn.relu(h) # [None, 14, 14, 64] --> [None, 28, 28, 1] h = conv2d_transpose(h, 1, kernel_size=4, strides=2, activation=tf.nn.sigmoid, reuse=reuse, name='d_tconv_2') # Assign name to final output return tf.identity(h, name=name)
def generator(n_samples, noise=None, dim=64): with tf.variable_scope('generator', reuse=tf.AUTO_REUSE): if noise is None: noise = tf.random_normal([n_samples, 128]) x = linear('input', 128, 8 * 4 * 4 * dim, noise) x = tf.reshape(x, [-1, 8 * dim, 4, 4]) x = batch_norm('bn1', x) x = tf.nn.relu(x) x = conv2d_transpose('c2', 8 * dim, 4 * dim, 5, x) x = batch_norm('bn2', x) x = tf.nn.relu(x) x = conv2d_transpose('c3', 4 * dim, 2 * dim, 5, x) x = batch_norm('bn3', x) x = tf.nn.relu(x) x = conv2d_transpose('c4', 2 * dim, dim, 5, x) x = batch_norm('bn4', x) x = tf.nn.relu(x) x = conv2d_transpose('c5', dim, 3, 5, x) x = tf.tanh(x) return tf.reshape(x, [-1, 3 * dim * dim])
def _generator(self, z, scope, reuse=False, activation_fn=tf.nn.sigmoid): with tf.variable_scope(scope, reuse=reuse): z = fc(z, 3 * 3 * self._zdim, 'fc-z') z = tf.reshape(z, (-1, 3, 3, self._zdim)) h1 = conv2d_transpose(z, 256, name='up1', padding='VALID', training=self.is_training, use_bn=True) h2 = conv2d_transpose(h1, 128, name='up2', training=self.is_training, use_bn=True) h3 = conv2d_transpose(h2, 64, name='up3', training=self.is_training, use_bn=True) recon = conv2d(h3, self._img_shape[-1], 'out', kernel_size=1, activation_fn=lambda x: x) return activation_fn(recon)
def first_block(x, target_size, noise_dim, upsampling='deconv', normalization='batch', is_training=True): if upsampling == 'deconv': _x = reshape(x, (1, 1, noise_dim)) _x = conv2d_transpose(_x, 1024, target_size, strides=(1, 1), padding='valid') elif upsampling == 'dense': _x = dense(x, target_size[0] * target_size[1] * 1024) _x = reshape(_x, (target_size[1], target_size[0], 1024)) else: raise ValueError if normalization == 'batch': _x = batch_norm(_x, is_training=is_training) elif normalization == 'layer': _x = layer_norm(_x, is_training=is_training) elif normalization is None: pass else: raise ValueError _x = activation(_x, 'relu') return _x
def formNet(self, img_ph, ann_ph, base_filter_num=8): down_layer_list = {} curr_layer = img_ph # Down sampling for i in range(5): num_filter = base_filter_num * 2**i if i == 0: conv1 = layers.conv2d( curr_layer, W=[3, 3, img_ph.get_shape().as_list()[-1], num_filter], b=[num_filter]) else: conv1 = layers.conv2d(curr_layer, W=[3, 3, num_filter // 2, num_filter], b=[num_filter]) relu1 = tf.nn.relu(conv1) conv2 = layers.conv2d(relu1, W=[3, 3, num_filter, num_filter], b=[num_filter]) down_layer_list[i] = tf.nn.relu(conv2) print('layer: ', i, '\tsize: ', down_layer_list[i].get_shape().as_list()) if i < 4: curr_layer = layers.max_pool(down_layer_list[i]) curr_layer = down_layer_list[4] # Up sampling for i in range(3, -1, -1): num_filter = base_filter_num * 2**(i + 1) deconv_output_shape = tf.shape(down_layer_list[i]) deconv1 = layers.conv2d_transpose( curr_layer, W=[3, 3, num_filter // 2, num_filter], b=[num_filter // 2], stride=2) concat1 = layers.crop_and_concat(tf.nn.relu(deconv1), down_layer_list[i]) conv1 = layers.conv2d(concat1, W=[3, 3, num_filter, num_filter // 2], b=[num_filter // 2], strides=[1, 1, 1, 1]) relu1 = tf.nn.relu(conv1) conv2 = layers.conv2d(relu1, W=[3, 3, num_filter // 2, num_filter // 2], b=[num_filter // 2], strides=[1, 1, 1, 1]) relu2 = tf.nn.relu(conv2) curr_layer = relu2 # Output conv = layers.conv2d(curr_layer, W=[1, 1, base_filter_num, 3], b=[3]) relu = tf.nn.relu(conv) print('final relu: ', relu.get_shape().as_list()) return tf.expand_dims(tf.argmax(relu, axis=-1), axis=-1), relu
def __init__(self, path, num_threads, batch_size, learning_rate, input_height, input_width, output_height, output_width, channels): input_shape = (batch_size, input_height, input_width, channels) output_shape = (batch_size, output_height, output_width, channels) self.input_images = tf.placeholder(tf.float32, input_shape, 'input_images') self.output_images = tf.placeholder(tf.float32, output_shape, 'output_images') kernel_size = (3, 3) strides = (2, 2) self.gen_images = tf.nn.leaky_relu( tf.layers.batch_normalization( conv2d_transpose( self.input_images, (batch_size, output_height, output_width, channels), kernel_size, strides, 'gen_images'))) flatten_output_images = tf.reshape(self.output_images, [batch_size, -1]) flatten_gen_images = tf.reshape(self.gen_images, [batch_size, -1]) self.loss = tf.losses.mean_squared_error(self.output_images, self.gen_images) optimizer = tf.train.AdamOptimizer(learning_rate) self.train_step = optimizer.minimize(self.loss) self.sess = tf.Session() self.coord = tf.train.Coordinator() self.queue = BatchQueue(path, batch_size, num_threads, (input_height, input_width), (output_height, output_width), self.sess, self.coord)
def __init__(self, train_data, val_data, num_class): self.num_class = num_class re_iter = tf.data.Iterator.from_structure(train_data.output_types, train_data.output_shapes) self.train_init_op = re_iter.make_initializer(train_data) self.val_init_op = re_iter.make_initializer(val_data) x, y = re_iter.get_next() x_and_offset = tf.map_fn(lambda t: random_crop(t, (320, 480)), x, dtype=(tf.int32, tf.int32)) x = x_and_offset[0] self.Y = tf.map_fn(lambda t: fixed_crop(t[0], t[1], (320, 480)), (y, x_and_offset[1]), dtype=tf.int32) self.X = tf.cast(x, tf.float32) # backbone: VGG16 # todo: stride param of conv layers. f = conv2d(self.X, [3, 3, 3, 64], "conv1", activation=tf.nn.relu, padding="SAME", strides=(1, 1, 1, 1)) f = conv2d(f, [3, 3, 64, 64], "conv2", activation=tf.nn.relu, padding="SAME", strides=(1, 1, 1, 1)) f = tf.nn.max_pool(f, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1), padding="SAME") f = conv2d(f, [3, 3, 64, 128], "conv3", activation=tf.nn.relu, padding="SAME", strides=(1, 1, 1, 1)) f = conv2d(f, [3, 3, 128, 128], "conv4", activation=tf.nn.relu, padding="SAME", strides=(1, 1, 1, 1)) f = tf.nn.max_pool(f, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1), padding="SAME") f = conv2d(f, [3, 3, 128, 256], "conv5", activation=tf.nn.relu, padding="SAME", strides=(1, 1, 1, 1)) f = conv2d(f, [3, 3, 256, 256], "conv6", activation=tf.nn.relu, padding="SAME", strides=(1, 1, 1, 1)) f = conv2d(f, [3, 3, 256, 256], "conv7", activation=tf.nn.relu, padding="SAME", strides=(1, 1, 1, 1)) p3 = tf.nn.max_pool(f, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1), padding="SAME") f = conv2d(p3, [3, 3, 256, 512], "conv8", activation=tf.nn.relu, padding="SAME", strides=(1, 1, 1, 1)) f = conv2d(f, [3, 3, 512, 512], "conv9", activation=tf.nn.relu, padding="SAME", strides=(1, 1, 1, 1)) f = conv2d(f, [3, 3, 512, 512], "conv10", activation=tf.nn.relu, padding="SAME", strides=(1, 1, 1, 1)) p4 = tf.nn.max_pool(f, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1), padding="SAME") f = conv2d(p4, [3, 3, 512, 512], "conv11", activation=tf.nn.relu, padding="SAME", strides=(1, 1, 1, 1)) f = conv2d(f, [3, 3, 512, 512], "conv12", activation=tf.nn.relu, padding="SAME", strides=(1, 1, 1, 1)) f = conv2d(f, [3, 3, 512, 512], "conv13", activation=tf.nn.relu, padding="SAME", strides=(1, 1, 1, 1)) p5 = tf.nn.max_pool(f, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1), padding="SAME") conv6 = conv2d(p5, [1, 1, 512, 4096], "conv14", activation=tf.nn.relu, padding="SAME", strides=(1, 1, 1, 1)) conv7 = conv2d(conv6, [1, 1, 4096, 4096], "conv15", activation=tf.nn.relu, padding='SAME', strides=(1, 1, 1, 1)) f32 = conv2d(conv7, [1, 1, 4096, num_class], "f32", padding='SAME', strides=(1, 1, 1, 1)) # /32 f32 = conv2d_transpose(f32, [4, 4, 512, num_class], "convt1", padding='SAME', strides=(1, 2, 2, 1), output_shape=tf.shape(p4), kernel_initializer=bilinear_initializer( num_class, 512, 4)) f16 = conv2d_transpose(f32 + p4, [4, 4, 256, 512], "convt2", padding='SAME', strides=(1, 2, 2, 1), output_shape=tf.shape(p3), kernel_initializer=bilinear_initializer( 512, 256, 4)) output_shape = tf.concat([tf.shape(self.X)[:-1], [num_class]], axis=-1) self.logits = f8 = conv2d_transpose( f16 + p3, [7, 7, num_class, 256], "convt3", padding="SAME", strides=(1, 8, 8, 1), output_shape=output_shape, kernel_initializer=bilinear_initializer(256, num_class, 7)) self.y_pred = tf.cast(tf.argmax(f8, axis=-1), tf.int32) self.loss = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.logits, labels=tf.squeeze( self.Y, axis=[3]))) self.pxacc = tf.reduce_mean( tf.cast(tf.equal(tf.squeeze(self.Y, axis=[3]), self.y_pred), tf.float32)) self.train_op = tf.train.AdamOptimizer().minimize(self.loss) self.session = get_session(debug=False) initialize()