def inference_model(self, inp, training, reuse=False, resize=True): # construct the graph of the inference net a = time.time() with tf.variable_scope("inference", reuse=reuse): # define variable scope if resize: inp = layers.max_pool_layer(inp, pool_size=(2,2), strides=(2,2), padding=(12,12)) inp = layers.max_pool_layer(inp, pool_size=(2,2), strides=(2,2)) inp = layers.max_pool_layer(inp, pool_size=(2,2), strides=(2,2)) inp = layers.max_pool_layer(inp, pool_size=(2,2), strides=(2,2)) inp = layers.max_pool_layer(inp, pool_size=(2,2), strides=(2,2)) flat = tf.reshape(inp, [self.batch_size, -1]) dense1 = layers.dense_layer(flat, units=1024, use_bias=True) relu1 = tf.nn.softplus(dense1) # <-------------------------- maybe try softplus dense2 = layers.dense_layer(relu1, units=512, use_bias=True) relu2 = tf.nn.softplus(dense2) dense3 = layers.dense_layer(relu2, units=512, use_bias=True) relu3 = tf.nn.softplus(dense3) dense4 = layers.dense_layer(relu3, units=2*self.latent_dim, use_bias=True) mean, logvar = tf.split(dense4, num_or_size_splits=2, axis=1) print("Built Inference model in {} s".format(time.time()-a)) list_ops = [flat, dense1, relu1, dense2, relu2, dense3, relu3, dense4] # list of operations, can be used to run the graph up to a certain op # i,e get the subgraph return inp, mean, logvar, list_ops
def largeFOV(x, c_prime): batch_size = tf.shape(x)[0] # NHWC TO NCHW ***************************************************************************************************** x = tf.transpose(x, [0, 3, 1, 2]) # DEFINE MODEL ***************************************************************************************************** # Convolution 1 x = layers.conv_layer(x, [3, 3, c_prime, 96], name = "d_conv1", data_format='NCHW') # Convolution 2 x = layers.conv_layer(x, [3, 3, 96, 128], name="d_conv2", data_format='NCHW') # Convolution 3 x = layers.conv_layer(x, [3, 3, 128, 128], name="d_conv3", data_format='NCHW') # Max-Pooling 1 x = layers.max_pool_layer(x, padding='SAME', data_format='NCHW') # Convolution 4 x = layers.conv_layer(x, [3, 3, 128, 256], name="d_conv4", data_format='NCHW') # Convolution 5 x = layers.conv_layer(x, [3, 3, 256, 256], name="d_conv5", data_format='NCHW') # Max-Pooling 2 x = layers.max_pool_layer(x, padding='SAME', data_format='NCHW') # Convolution 6 x = layers.conv_layer(x, [3, 3, 256, 512], name="d_conv6", data_format='NCHW') # Convolution 7 x = layers.conv_layer(x, [3, 3, 512, 2], name="d_conv7", data_format='NCHW', relu='no') # NCHW TO NHWC ***************************************************************************************************** x = tf.transpose(x, [0, 2, 3, 1]) return x
def stanford_bd_model(image, segmentation, c_prime): # NHWC TO NCHW ***************************************************************************************************** image = tf.transpose(image, [0, 3, 1, 2]) segmentation = tf.transpose(segmentation, [0, 3, 1, 2]) # DEFINE MODEL ***************************************************************************************************** # Branch 1 # Convolution 1 b1 = layers.conv_layer(segmentation, [5, 5, c_prime, 64], name="b1_conv1", data_format='NCHW') # Branch 2 # Convolution 1 b2 = layers.conv_layer(image, [5, 5, 3, 16], name="b2_conv1", data_format='NCHW') # Convolution 2 b2 = layers.conv_layer(b2, [5, 5, 16, 64], name="b2_conv2", data_format='NCHW') # Feature concatenation feat_concat = tf.concat([b1, b2], axis=1) # Merged branch # Convolution 1 x = layers.conv_layer(feat_concat, [3, 3, 128, 128], name="m_conv1", data_format='NCHW') # Max-Pooling 1 x = layers.max_pool_layer(x, padding='SAME', data_format='NCHW') # Convolution 2 x = layers.conv_layer(x, [3, 3, 128, 256], name="m_conv2", data_format='NCHW') # Max-Pooling 2 x = layers.max_pool_layer(x, padding='SAME', data_format='NCHW') # Convolution 3 x = layers.conv_layer(x, [3, 3, 256, 512], name="m_conv3", data_format='NCHW') # Convolution 4 x = layers.conv_layer(x, [3, 3, 512, 2], name="m_conv4", data_format='NCHW', relu='no') # Average-Pooling x = tf.transpose(x, [0, 2, 3, 1]) #x = tf.reduce_mean(x, axis = [1,2]) # Reshape #x = tf.reshape(x, (1, 2)) return x
def discriminator_model(self, inp, feats, training, reuse=False, resize=False, minibatch=False): # construct the graph of the discriminator a = time.time() with tf.variable_scope("discriminator", reuse=reuse): # define variable scope to easily retrieve vars of the discriminator if resize: inp = layers.max_pool_layer(inp, pool_size=(2, 2), strides=(2, 2), padding=(12, 12)) inp = layers.max_pool_layer(inp, pool_size=(2, 2), strides=(2, 2)) inp = layers.max_pool_layer(inp, pool_size=(2, 2), strides=(2, 2)) inp = layers.max_pool_layer(inp, pool_size=(2, 2), strides=(2, 2)) conv1 = layers.conv_block_mcgan(inp, training, momentum=0.8, out_channels=128, filter_size=(4, 4), strides=(2, 2), padding="same", use_bias=True, batch_norm=False, alpha=0.3) # shape=(batch_size, 128, 32, 32) conv2 = layers.conv_block_mcgan(conv1, training, momentum=0.8, out_channels=256, filter_size=(4, 4), strides=(2, 2), padding="same", use_bias=True, alpha=0.3) # shape=(batch_size, 256, 16, 16) conv3 = layers.conv_block_mcgan(conv2, training, momentum=0.8, out_channels=512, filter_size=(4, 4), strides=(2, 2), padding="same", use_bias=True, alpha=0.3) # shape=(batch_size, 512, 8, 8) conv4 = layers.conv_block_mcgan(conv3, training, momentum=0.8, out_channels=1024, filter_size=(4, 4), strides=(2, 2), padding="same", use_bias=True, alpha=0.3) # shape=(batch_size, 1024, 4, 4) flat = tf.reshape(conv4, [-1, 1024 * 4 * 4]) if(minibatch): minibatched = layers.minibatch(flat, num_kernels=5, kernel_dim=3) dense1 = layers.dense_layer(minibatched, 128, use_bias=True) else: dense1 = layers.dense_layer(flat, 128, use_bias=True) drop1 = layers.dropout_layer(dense1, training, dropout_rate=0.3) LRU1 = layers.leaky_relu_layer(drop1, alpha=0.3) dense2 = layers.dense_layer(feats, units=3, use_bias=True) relu2 = layers.relu_layer(dense2) bn2 = layers.batch_norm_layer_mcgan(relu2, training, 0.8) dense3 = layers.dense_layer(bn2, units=1) merged = tf.concat([LRU1, dense3], axis=-1) logits = layers.dense_layer(merged, units=2, use_bias=True) out = logits print("Built Discriminator model in {} s".format(time.time() - a)) list_ops = {"inp": inp, "conv1": conv1, "conv2": conv2, "conv3": conv3, "conv4": conv4, "flat": flat, "dense1":dense1, "drop1":drop1, "LRU1":LRU1, "dense2":dense2, "relu2":relu2, "bn2":bn2, "dense3":dense3, "logits": logits, "out": out} return out, list_ops
def __call__( self, inp, training, pad=True, zero_centered=False ): # pad by 12 pixels in each side to get 1024x1024 image if the image size is 1000x1000 a = time.time() pad_value = 0 if zero_centered: pad_value = -1 with tf.variable_scope("Scorer"): # define variable scope # histograms = tf.map_fn(lambda a: tf.cast(tf.histogram_fixed_width((a+1)*128.0 if zero_centered else (a*255.0), value_range=[0.0, 255.0], nbins=10), tf.float32), inp) if pad: inp = layers.padding_layer(inp, padding=(12, 12), pad_values=pad_value) # 1024x1024 max_pool1 = layers.max_pool_layer(inp, pool_size=(2, 2), strides=(2, 2)) # 512x512 max_pool2 = layers.max_pool_layer(max_pool1, pool_size=(2, 2), strides=(2, 2)) # 256x256 max_pool3 = layers.max_pool_layer(max_pool2, pool_size=(2, 2), strides=(2, 2)) # 128x128 max_pool4 = layers.max_pool_layer(max_pool3, pool_size=(2, 2), strides=(2, 2)) # 64x64 resized = layers.resize_layer( inp, new_size=[64, 64], resize_method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) concat1 = tf.concat([max_pool4, resized], axis=1) # conv1 = layers.conv_block_scorer(inp, training, out_channels=8, filter_size=(4, 4), strides=(2, 2), padding=(1, 1), pad_values=pad_value, use_bias=True, alpha=0.2) # 512x512 # conv2 = layers.conv_block_scorer(conv1, training, out_channels=16, filter_size=(4, 4), strides=(2, 2), padding=(1, 1), pad_values=pad_value, use_bias=True, alpha=0.2) # 256x256 # conv3 = layers.conv_block_scorer(conv2, training, out_channels=32, filter_size=(4, 4), strides=(2, 2), padding=(1, 1), pad_values=pad_value, use_bias=True, alpha=0.2) # 128x128 # conv4 = layers.conv_block_scorer(conv3, training, out_channels=64, filter_size=(4, 4), strides=(2, 2), padding=(1, 1), pad_values=pad_value, use_bias=True, alpha=0.2) # 64x64 # # concat2 = tf.concat([concat1, conv4], axis=1) conv5 = layers.conv_block_scorer(concat1, training, out_channels=128, filter_size=(4, 4), strides=(2, 2), padding=(1, 1), pad_values=pad_value, use_bias=True, alpha=0.2) # 32x32 conv6 = layers.conv_block_scorer(conv5, training, out_channels=256, filter_size=(4, 4), strides=(2, 2), padding=(1, 1), pad_values=pad_value, use_bias=True, alpha=0.2) # 16x16 conv7 = layers.conv_block_scorer(conv6, training, out_channels=512, filter_size=(4, 4), strides=(2, 2), padding=(1, 1), pad_values=pad_value, use_bias=True, alpha=0.2) # 8x8 conv8 = layers.conv_block_scorer(conv7, training, out_channels=1024, filter_size=(4, 4), strides=(2, 2), padding=(1, 1), pad_values=pad_value, use_bias=True, alpha=0.2) # 4x4 flat = tf.reshape(conv8, [-1, 1024 * 4 * 4]) # concat3 = tf.concat([flat, histograms], axis=-1) dense1 = layers.dense_block_scorer(flat, training, units=1024, use_bias=True, dropout_rate=0.3) # dense2 = layers.dense_block_scorer(dense1, training, units=256, use_bias=True, dropout_rate=0.3) # dense3 = layers.dense_block_scorer(dense2, training, units=128, use_bias=True, dropout_rate=0.3) dense4 = layers.dense_layer(dense1, units=1, use_bias=True) # print(dense4.shape) # sys.exit(0) output = dense4 print("Scorer Model built in {} s".format(time.time() - a)) return output
def discriminator_model( self, inp, training, reuse=False, resize=False, minibatch=False): # construct the graph of the discriminator a = time.time() with tf.variable_scope( "discriminator", reuse=reuse ): # define variable scope to easily retrieve vars of the discriminator if resize: inp = layers.max_pool_layer(inp, pool_size=(2, 2), strides=(2, 2), padding=(12, 12)) inp = layers.max_pool_layer(inp, pool_size=(2, 2), strides=(2, 2)) inp = layers.max_pool_layer(inp, pool_size=(2, 2), strides=(2, 2)) inp = layers.max_pool_layer(inp, pool_size=(2, 2), strides=(2, 2)) conv1 = layers.conv_block_dcgan( inp, training, out_channels=128, filter_size=(4, 4), strides=(2, 2), padding="same", use_bias=False, alpha=0.3) # shape=(batch_size, 128, 32, 32) conv2 = layers.conv_block_dcgan( conv1, training, out_channels=256, filter_size=(4, 4), strides=(2, 2), padding="same", use_bias=False, alpha=0.3) # shape=(batch_size, 256, 16, 16) conv3 = layers.conv_block_dcgan( conv2, training, out_channels=512, filter_size=(4, 4), strides=(2, 2), padding="same", use_bias=False, alpha=0.3) # shape=(batch_size, 512, 8, 8) conv4 = layers.conv_block_dcgan( conv3, training, out_channels=1024, filter_size=(4, 4), strides=(2, 2), padding="same", use_bias=False, alpha=0.3) # shape=(batch_size, 1024, 4, 4) flat = tf.reshape(conv4, [-1, 1024 * 4 * 4]) if (minibatch): minibatched = layers.minibatch(flat, num_kernels=5, kernel_dim=3) logits = layers.dense_layer(minibatched, units=2, use_bias=True) else: logits = layers.dense_layer(flat, units=2, use_bias=True) out = logits print("Built Discriminator model in {} s".format(time.time() - a)) list_ops = { "inp": inp, "conv1": conv1, "conv2": conv2, "conv3": conv3, "conv4": conv4, "flat": flat, "logits": logits, "out": out } return out, list_ops
def msca_front(x, weights, pkeep, classes, pretrained=0): if pretrained == 0: pretrained_vars = [0] * 15 else: pretrained_vars = [ \ [weights["conv1_1_W"], weights["conv1_1_b"]], [weights["conv1_2_W"], weights["conv1_2_b"]], [weights["conv2_1_W"], weights["conv2_1_b"]], [weights["conv2_2_W"], weights["conv2_2_b"]], [weights["conv3_1_W"], weights["conv3_1_b"]], [weights["conv3_2_W"], weights["conv3_2_b"]], [weights["conv3_3_W"], weights["conv3_3_b"]], [weights["conv4_1_W"], weights["conv4_1_b"]], [weights["conv4_2_W"], weights["conv4_2_b"]], [weights["conv4_3_W"], weights["conv4_3_b"]], [weights["conv5_1_W"], weights["conv5_1_b"]], [weights["conv5_2_W"], weights["conv5_2_b"]], [weights["conv5_3_W"], weights["conv5_3_b"]], [weights["fc6_W"], weights["fc6_b"]], [weights["fc7_W"], weights["fc7_b"]]] with tf.variable_scope('generator'): # NHWC TO NCHW ************************************************************************************************* x = tf.transpose(x, [0, 3, 1, 2]) # DEFINE MODEL ************************************************************************************************* print( 'Building generator front model for multi scale context aggregation' ) print('\tBuilding unit: conv1') # Convolution 1 x = layers.conv_layer(x, [3, 3, 3, 64], pkeep, "conv1_1", pretrained_vars[0], data_format='NCHW') # Convolution 2 x = layers.conv_layer(x, [3, 3, 64, 64], pkeep, "conv1_2", pretrained_vars[1], data_format='NCHW') # Max-Pooling 1 x = layers.max_pool_layer(x, padding='SAME', data_format='NCHW') print('\tBuilding unit: conv2') # Convolution 3 x = layers.conv_layer(x, [3, 3, 64, 128], pkeep, "conv2_1", pretrained_vars[2], data_format='NCHW') # Convolution 4 x = layers.conv_layer(x, [3, 3, 128, 128], pkeep, "conv2_2", pretrained_vars[3], data_format='NCHW') # Max-Pooling 2 x = layers.max_pool_layer(x, padding='SAME', data_format='NCHW') print('\tBuilding unit: conv3') # Convolution 5 x = layers.conv_layer(x, [3, 3, 128, 256], pkeep, "conv3_1", pretrained_vars[4], data_format='NCHW') # Convolution 6 x = layers.conv_layer(x, [3, 3, 256, 256], pkeep, "conv3_2", pretrained_vars[5], data_format='NCHW') # Convolution 7 x = layers.conv_layer(x, [3, 3, 256, 256], pkeep, "conv3_3", pretrained_vars[6], data_format='NCHW') # Max-Pooling 3 x = layers.max_pool_layer(x, padding='SAME', data_format='NCHW') print('\tBuilding unit: conv4') # Convolution 8 x = layers.conv_layer(x, [3, 3, 256, 512], pkeep, "conv4_1", pretrained_vars[7], data_format='NCHW') # Convolution 9 x = layers.conv_layer(x, [3, 3, 512, 512], pkeep, "conv4_2", pretrained_vars[8], data_format='NCHW') # Convolution 10 x = layers.conv_layer(x, [3, 3, 512, 512], pkeep, "conv4_3", pretrained_vars[9], data_format='NCHW') x = tf.transpose(x, [0, 2, 3, 1]) print('\tBuilding unit: conv5') # Convolution 11 - dilated x 2 x = layers.atrous_conv_layer(x, [3, 3, 512, 512], output_stride=2, name='conv5_1', weights=pretrained_vars[10], pkeep=pkeep) # Convolution 12 - dilated x 2 x = layers.atrous_conv_layer(x, [3, 3, 512, 512], output_stride=2, name='conv5_2', weights=pretrained_vars[11], pkeep=pkeep) # Convolution 13 - dilated x 2 x = layers.atrous_conv_layer(x, [3, 3, 512, 512], output_stride=2, name='conv5_3', weights=pretrained_vars[12], pkeep=pkeep) print('\tBuilding unit: fully conv') # Dense-Conv 1 - dilated x 4 x = layers.atrous_conv_layer(x, [7, 7, 512, 4096], output_stride=4, name='fc6', weights=pretrained_vars[13], pkeep=pkeep) # Dense-Conv 2 - dilated x 4 x = layers.atrous_conv_layer(x, [1, 1, 4096, 4096], output_stride=4, name='fc7', weights=pretrained_vars[14], pkeep=pkeep) # Final conv x = layers.conv_layer(x, [1, 1, 4096, classes], name="final", relu='no') # Upsample size = tf.shape(x) height = size[1] width = size[2] logits = tf.image.resize_images(x, tf.stack([height * 8, width * 8]), method=tf.image.ResizeMethod.BILINEAR, align_corners=False) return logits
def msca_global(x, weights, pkeep, num_classes, pretrained=0): if pretrained == 0: pretrained_vars = [0] * 15 else: pretrained_vars = [ \ [weights["conv1_1_W"], weights["conv1_1_b"]], [weights["conv1_2_W"], weights["conv1_2_b"]], [weights["conv2_1_W"], weights["conv2_1_b"]], [weights["conv2_2_W"], weights["conv2_2_b"]], [weights["conv3_1_W"], weights["conv3_1_b"]], [weights["conv3_2_W"], weights["conv3_2_b"]], [weights["conv3_3_W"], weights["conv3_3_b"]], [weights["conv4_1_W"], weights["conv4_1_b"]], [weights["conv4_2_W"], weights["conv4_2_b"]], [weights["conv4_3_W"], weights["conv4_3_b"]], [weights["conv5_1_W"], weights["conv5_1_b"]], [weights["conv5_2_W"], weights["conv5_2_b"]], [weights["conv5_3_W"], weights["conv5_3_b"]], [weights["fc6_W"], weights["fc6_b"]], [weights["fc7_W"], weights["fc7_b"]]] with tf.variable_scope('generator'): # NHWC TO NCHW ************************************************************************************************* x = tf.transpose(x, [0, 3, 1, 2]) # DEFINE MODEL ************************************************************************************************* print( 'Building generator global model for multi scale context aggregation' ) print('\tBuilding unit: conv1') # Convolution 1 x = layers.conv_layer(x, [3, 3, 3, 64], pkeep, "conv1_1", pretrained_vars[0], data_format='NCHW') # Convolution 2 x = layers.conv_layer(x, [3, 3, 64, 64], pkeep, "conv1_2", pretrained_vars[1], data_format='NCHW') # Max-Pooling 1 x = layers.max_pool_layer(x, padding='SAME', data_format='NCHW') print('\tBuilding unit: conv2') # Convolution 3 x = layers.conv_layer(x, [3, 3, 64, 128], pkeep, "conv2_1", pretrained_vars[2], data_format='NCHW') # Convolution 4 x = layers.conv_layer(x, [3, 3, 128, 128], pkeep, "conv2_2", pretrained_vars[3], data_format='NCHW') # Max-Pooling 2 x = layers.max_pool_layer(x, padding='SAME', data_format='NCHW') print('\tBuilding unit: conv3') # Convolution 5 x = layers.conv_layer(x, [3, 3, 128, 256], pkeep, "conv3_1", pretrained_vars[4], data_format='NCHW') # Convolution 6 x = layers.conv_layer(x, [3, 3, 256, 256], pkeep, "conv3_2", pretrained_vars[5], data_format='NCHW') # Convolution 7 x = layers.conv_layer(x, [3, 3, 256, 256], pkeep, "conv3_3", pretrained_vars[6], data_format='NCHW') # Max-Pooling 3 x = layers.max_pool_layer(x, padding='SAME', data_format='NCHW') print('\tBuilding unit: conv4') # Convolution 8 x = layers.conv_layer(x, [3, 3, 256, 512], pkeep, "conv4_1", pretrained_vars[7], data_format='NCHW') # Convolution 9 x = layers.conv_layer(x, [3, 3, 512, 512], pkeep, "conv4_2", pretrained_vars[8], data_format='NCHW') # Convolution 10 x = layers.conv_layer(x, [3, 3, 512, 512], pkeep, "conv4_3", pretrained_vars[9], data_format='NCHW') x = tf.transpose(x, [0, 2, 3, 1]) print('\tBuilding unit: conv5') # Convolution 11 - dilated x 2 x = layers.atrous_conv_layer(x, [3, 3, 512, 512], output_stride=2, name='conv5_1', weights=pretrained_vars[10], pkeep=pkeep) # Convolution 12 - dilated x 2 x = layers.atrous_conv_layer(x, [3, 3, 512, 512], output_stride=2, name='conv5_2', weights=pretrained_vars[11], pkeep=pkeep) # Convolution 13 - dilated x 2 x = layers.atrous_conv_layer(x, [3, 3, 512, 512], output_stride=2, name='conv5_3', weights=pretrained_vars[12], pkeep=pkeep) print('\tBuilding unit: fully conv') # Dense-Conv 1 - dilated x 4 x = layers.atrous_conv_layer(x, [7, 7, 512, 4096], output_stride=4, name='fc6', weights=pretrained_vars[13], pkeep=pkeep) # Dense-Conv 2 - dilated x 4 x = layers.atrous_conv_layer(x, [1, 1, 4096, 4096], output_stride=4, name='fc7', weights=pretrained_vars[14], pkeep=pkeep) # Final conv x = layers.conv_layer(x, [1, 1, 4096, num_classes], name="final", relu='yes') with tf.device('/cpu:0'): print('Building Context module') print('\tBuilding Context units 1-8') # Context-Layer 1 x = tf.pad(x, [[0, 0], [1, 1], [1, 1], [0, 0]], mode='CONSTANT') x = layers.atrous_conv_layer(x, [3, 3, num_classes, num_classes], name='context1', output_stride=1, identity_init='yes') # Context-Layer 2 x = tf.pad(x, [[0, 0], [1, 1], [1, 1], [0, 0]], mode='CONSTANT') x = layers.atrous_conv_layer(x, [3, 3, num_classes, num_classes], name='context2', output_stride=1, identity_init='yes') # Context-Layer 3 x = tf.pad(x, [[0, 0], [2, 2], [2, 2], [0, 0]], mode='CONSTANT') x = layers.atrous_conv_layer(x, [3, 3, num_classes, num_classes], name='context3', output_stride=2, identity_init='yes') # Context-Layer 4 x = tf.pad(x, [[0, 0], [4, 4], [4, 4], [0, 0]], mode='CONSTANT') x = layers.atrous_conv_layer(x, [3, 3, num_classes, num_classes], name='context4', output_stride=4, identity_init='yes') # Context-Layer 5 x = tf.pad(x, [[0, 0], [8, 8], [8, 8], [0, 0]], mode='CONSTANT') x = layers.atrous_conv_layer(x, [3, 3, num_classes, num_classes], name='context5', output_stride=8, identity_init='yes') # Context-Layer 6 x = tf.pad(x, [[0, 0], [16, 16], [16, 16], [0, 0]], mode='CONSTANT') x = layers.atrous_conv_layer(x, [3, 3, num_classes, num_classes], name='context6', output_stride=16, identity_init='yes') # Context-Layer 7 x = tf.pad(x, [[0, 0], [1, 1], [1, 1], [0, 0]], mode='CONSTANT') x = layers.atrous_conv_layer(x, [3, 3, num_classes, num_classes], name='context7', output_stride=1, identity_init='yes') # Context-Layer 8 x = layers.conv_layer(x, [1, 1, num_classes, num_classes], name='global_final', relu='no') # Upsample size = tf.shape(x) height = size[1] width = size[2] logits = tf.image.resize_images(x, tf.stack([height * 8, width * 8]), method=tf.image.ResizeMethod.BILINEAR, align_corners=False) return logits #X = tf.ones([1, 256, 256, 3], dtype=tf.float32) #msca_global(X, weights=0, pkeep=1, num_classes=8, pretrained=0)
def unet(x, pkeep, num_classes, channels=3, num_layers=5): features_root = 64 filter_size = 3 pool_size = 2 in_node = x pools = OrderedDict() deconv = OrderedDict() dw_h_convs = OrderedDict() up_h_convs = OrderedDict() # DEFINE MODEL ***************************************************************************************************** with tf.variable_scope('generator'): print('\nBuilding generator model u-net') print('\tBuilding encoder') # down layers for layer in range(0, num_layers): features = 2**layer * features_root print('\t layer ' + str(layer) + ': ' + str(features) + ' features channels') if layer == 0: filter_shape1 = [filter_size, filter_size, channels, features] filter_shape2 = [filter_size, filter_size, features, features] else: filter_shape1 = [ filter_size, filter_size, features // 2, features ] filter_shape2 = [filter_size, filter_size, features, features] conv1 = layers.conv_layer(in_node, filter_shape1, pkeep, 'down_conv1_layer' + str(layer), padding='VALID') conv2 = layers.conv_layer(conv1, filter_shape2, pkeep, 'down_conv2_layer' + str(layer), padding='VALID') dw_h_convs[layer] = conv2 if layer < num_layers - 1: pools[layer] = layers.max_pool_layer(dw_h_convs[layer]) in_node = pools[layer] in_node = dw_h_convs[num_layers - 1] print('\tBuilding decoder') print('\t layer ' + str(layer) + ': ' + str(features) + ' features channels') # up layers for layer in range(num_layers - 2, -1, -1): features = 2**(layer + 1) * features_root filter_shape = [pool_size, pool_size, features // 2, features] h_deconv = layers.deconv_layer(in_node, filter_shape, 'deconv_layer' + str(layer), padding='VALID') h_deconv_concat = layers.crop_and_concat_layer( dw_h_convs[layer], h_deconv) deconv[layer] = h_deconv_concat filter_shape1 = [filter_size, filter_size, features, features // 2] filter_shape2 = [ filter_size, filter_size, features // 2, features // 2 ] print('\t layer ' + str(layer) + ': ' + str(features // 2) + ' features channels') conv1 = layers.conv_layer(h_deconv_concat, filter_shape1, pkeep, 'up_conv1_layer' + str(layer), padding='VALID') conv2 = layers.conv_layer(conv1, filter_shape2, pkeep, 'up_conv2_layer' + str(layer), padding='VALID') up_h_convs[layer] = conv2 in_node = up_h_convs[layer] # Output Map filter_shape = [1, 1, features_root, num_classes] output_map = layers.final_layer(in_node, filter_shape, padding='VALID') logits = output_map return logits #X = tf.ones((1, 476, 636, 3)) #y = tf.ones((1, 476, 636, 1)) #model = unet(X, pkeep = 1 , is_train = tf.constant(True)) #for i in range (0, 1000): # valid_size = is_valid_input_unet(i, num_layers=4) # if valid_size == 1: # print('Imput size ' + str(i) + ' is valid') # else: # print('Imput size ' + str(i) + ' is not valid')
def fcn32(x, pkeep, classes, pretrained=1): weights = np.load(weights_path) if pretrained == 0: pretrained_vars = [0] * 15 else: pretrained_vars = [ \ [weights["conv1_1_W"], weights["conv1_1_b"]], [weights["conv1_2_W"], weights["conv1_2_b"]], [weights["conv2_1_W"], weights["conv2_1_b"]], [weights["conv2_2_W"], weights["conv2_2_b"]], [weights["conv3_1_W"], weights["conv3_1_b"]], [weights["conv3_2_W"], weights["conv3_2_b"]], [weights["conv3_3_W"], weights["conv3_3_b"]], [weights["conv4_1_W"], weights["conv4_1_b"]], [weights["conv4_2_W"], weights["conv4_2_b"]], [weights["conv4_3_W"], weights["conv4_3_b"]], [weights["conv5_1_W"], weights["conv5_1_b"]], [weights["conv5_2_W"], weights["conv5_2_b"]], [weights["conv5_3_W"], weights["conv5_3_b"]], [weights["fc6_W"], weights["fc6_b"]], [weights["fc7_W"], weights["fc7_b"]]] with tf.variable_scope('generator'): # NHWC TO NCHW ************************************************************************************************* x = tf.transpose(x, [0, 3, 1, 2]) # DEFINE MODEL ************************************************************************************************* print('Building generator model fcn32') print('\tBuilding unit: conv1') # Convolution 1 x = layers.conv_layer(x, [3, 3, 3, 64], pkeep, "conv1_1", pretrained_vars[0], data_format='NCHW') # Convolution 2 x = layers.conv_layer(x, [3, 3, 64, 64], pkeep, "conv1_2", pretrained_vars[1], data_format='NCHW') # Max-Pooling 1 x = layers.max_pool_layer(x, padding='SAME', data_format='NCHW') print('\tBuilding unit: conv2') # Convolution 3 x = layers.conv_layer(x, [3, 3, 64, 128], pkeep, "conv2_1", pretrained_vars[2], data_format='NCHW') # Convolution 4 x = layers.conv_layer(x, [3, 3, 128, 128], pkeep, "conv2_2", pretrained_vars[3], data_format='NCHW') # Max-Pooling 2 x = layers.max_pool_layer(x, padding='SAME', data_format='NCHW') print('\tBuilding unit: conv3') # Convolution 5 x = layers.conv_layer(x, [3, 3, 128, 256], pkeep, "conv3_1", pretrained_vars[4], data_format='NCHW') # Convolution 6 x = layers.conv_layer(x, [3, 3, 256, 256], pkeep, "conv3_2", pretrained_vars[5], data_format='NCHW') # Convolution 7 x = layers.conv_layer(x, [3, 3, 256, 256], pkeep, "conv3_3", pretrained_vars[6], data_format='NCHW') # Max-Pooling 3 x = layers.max_pool_layer(x, padding='SAME', data_format='NCHW') print('\tBuilding unit: conv4') # Convolution 8 x = layers.conv_layer(x, [3, 3, 256, 512], pkeep, "conv4_1", pretrained_vars[7], data_format='NCHW') # Convolution 9 x = layers.conv_layer(x, [3, 3, 512, 512], pkeep, "conv4_2", pretrained_vars[8], data_format='NCHW') # Convolution 10 x = layers.conv_layer(x, [3, 3, 512, 512], pkeep, "conv4_3", pretrained_vars[9], data_format='NCHW') # Max-Pooling 4 x = layers.max_pool_layer(x, padding='SAME', data_format='NCHW') print('\tBuilding unit: conv5') # Convolution 11 x = layers.conv_layer(x, [3, 3, 512, 512], pkeep, "conv5_1", pretrained_vars[10], data_format='NCHW') # Convolution 12 x = layers.conv_layer(x, [3, 3, 512, 512], pkeep, "conv5_2", pretrained_vars[11], data_format='NCHW') # Convolution 13 x = layers.conv_layer(x, [3, 3, 512, 512], pkeep, "conv5_3", pretrained_vars[12], data_format='NCHW') # Max-Pooling 5 x = layers.max_pool_layer(x, padding='SAME', data_format='NCHW') print('\tBuilding unit: fully conv') # Dense-Conv 1 x = layers.conv_layer(x, [7, 7, 512, 4096], pkeep, "fc6", pretrained_vars[13], data_format='NCHW') # Dense-Conv 2 x = layers.conv_layer(x, [1, 1, 4096, 4096], pkeep, "fc7", pretrained_vars[14], data_format='NCHW') # Score Predictions x = layers.conv_layer(x, [1, 1, 4096, classes], name="final", data_format='NCHW', relu='no') unscaled = tf.transpose(x, [0, 2, 3, 1]) # Upsample size = tf.shape(x) height = size[1] width = size[2] logits = tf.image.resize_images(unscaled, tf.stack([height * 32, width * 32]), method=tf.image.ResizeMethod.BILINEAR, align_corners=False) return unscaled, logits
# data real_im, _, nb_reals, _ = create_dataloader_train_labeled( data_root=DATA_ROOT, batch_size=BATCH_SIZE, batches_to_prefetch=BATCHES_TO_PREFETCH, all_data=False) training_pl = tf.placeholder(dtype=tf.bool, shape=[]) real_im = (real_im + 1) / 2 # renormalize images to the range [0, 1] # image preprocessing padded = layers.padding_layer(real_im, padding=(12, 12), pad_values=0) # 1024x1024 max_pool1 = layers.max_pool_layer(padded, pool_size=(2, 2), strides=(2, 2)) # 512x512 max_pool2 = layers.max_pool_layer(max_pool1, pool_size=(2, 2), strides=(2, 2)) # 256x256 max_pool3 = layers.max_pool_layer(max_pool2, pool_size=(2, 2), strides=(2, 2)) # 128x128 max_pool4 = layers.max_pool_layer(max_pool3, pool_size=(2, 2), strides=(2, 2)) # 64x64 outputs_gt = [max_pool3, max_pool2, max_pool1, padded] # outputs to predict #model print("Building model ...")