def subnet1(image, reuse=False): with tf.variable_scope("subnet1", reuse=reuse): def _vgg_net(weights, image): print('setting up vgg model initialized params --> extractor1') layers = ( 'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1', 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2', 'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3', 'relu3_3', 'conv3_4', 'relu3_4', 'pool3', 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3', 'relu4_3', 'conv4_4', 'relu4_4', 'pool4', 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3', 'relu5_3', 'conv5_4', 'relu5_4' ) net = {} current = image for i, name in enumerate(layers): kind = name[:4] if kind == 'conv': kernels, bias = weights[i][0][0][0][0] # kernels are [width, height, in_channles, out_channles] # tensorflow are [height, width, in channles, out_channles] kernels = utils.get_variable( np.transpose(kernels, (1, 0, 2, 3)), name=name + '_w') bias = utils.get_variable(bias.reshape(-1), name=name + '_b') current = utils.conv2d_basic(current, kernels, bias) elif kind == 'relu': current = tf.nn.relu(current, name=name) elif kind == 'pool': current = utils.avg_pool_2x2(current) net[name] = current return net model_data = utils.get_model_data("data", MODEL_URL) weights = np.squeeze(model_data['layers']) with tf.variable_scope('inference'): image = tf.reshape(image, [-1, IMAGE_SIZE, IMAGE_SIZE, IMAGE_CHANNLES]) image_net = _vgg_net(weights, image) conv_final_layer = image_net['relu5_4'] pool5 = utils.max_pool_2x2(conv_final_layer) W6 = utils.weights_variable([4, 4, 512, 4096], name="W6") b6 = utils.bias_variable([4096], name='b6') conv6 = utils.conv2d_strided(pool5, W6, b6, stride=1) relu6 = tf.nn.relu(conv6, name='relu6') relu6 = utils.max_pool_2x2(relu6) disc_out = tf.reshape(relu6, [-1, 4096]) return disc_out
def subnet1(image, keep_prob, reuse=False): with tf.variable_scope("subnet1", reuse=reuse): def _vgg_net(weights, image): print('setting up vgg model initialized params --> extractor1') layers = ( 'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1', 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2', 'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3', 'relu3_3', 'conv3_4', 'relu3_4', 'pool3', 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3', 'relu4_3', 'conv4_4', 'relu4_4', 'pool4', 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3', 'relu5_3', 'conv5_4', 'relu5_4' ) net = {} current = image for i, name in enumerate(layers): kind = name[:4] if kind == 'conv': kernels, bias = weights[i][0][0][0][0] # kernels are [width, height, in_channles, out_channles] # tensorflow are [height, width, in channles, out_channles] kernels = utils.get_variable( np.transpose(kernels, (1, 0, 2, 3)), name=name + '_w') bias = utils.get_variable(bias.reshape(-1), name=name + '_b') current = utils.conv2d_basic(current, kernels, bias) elif kind == 'relu': current = tf.nn.relu(current, name=name) elif kind == 'pool': current = utils.avg_pool_2x2(current) net[name] = current return net model_data = utils.get_model_data("data", MODEL_URL) weights = np.squeeze(model_data['layers']) with tf.variable_scope('inference'): image_net = _vgg_net(weights, image) conv_final_layer = image_net['relu5_4'] pool5 = utils.max_pool_2x2(conv_final_layer) pool5_flatten = tf.reshape(pool5, [-1, 7 * 7 * 512]) W1 = utils.get_weights_variable(7 * 7 * 512, 4096, name='W1') b1 = utils.get_bias_variable(4096, name='b1') matmul1 = tf.nn.relu(tf.nn.bias_add(tf.matmul(pool5_flatten, W1), b1)) matmul1 = tf.nn.dropout(matmul1, keep_prob) W2 = utils.get_weights_variable(4096, 4096, name='W2') b2 = utils.get_bias_variable(4096, name='b2') matmul2 = tf.nn.bias_add(tf.matmul(matmul1, W2), b2) matmul2 = tf.nn.dropout(matmul2, keep_prob) W3 = utils.get_weights_variable(4096, CLASS_COUNTS, name='W3') b3 = utils.get_bias_variable(CLASS_COUNTS, name='b3') matmul3 = tf.nn.bias_add(tf.matmul(matmul2, W3), b3) return matmul3
def encoder(h, noise_std, update_BN): # Perform encoding for each layer h += tf.random_normal(tf.shape(h)) * noise_std h = tf.identity(h, "h0") for i, layer_spec in enumerate(layers): with tf.variable_scope("encoder_bloc_" + str(i + 1), reuse=tf.AUTO_REUSE): # Create an encoder bloc if the layer type is dense or conv2d if layer_spec["type"] == "flat": h = flat(h, output_name="h") elif layer_spec["type"] == "max_pool_2x2": h = max_pool_2x2(h, output_name="h") else: if i == L - 1: activation = tf.nn.softmax # Only for the last layer else: activation = tf.nn.relu h = encoder_bloc(h, layer_spec, noise_std, update_BN=update_BN, activation=activation) y = tf.identity(h, name="y") return y
def get_VGG_layers(images_ph, dropout_keep_prob_ph, train_fc_layers=False): """ Args: images: Images placeholder in NHWC. Returns: dict of tensors, keys are original layer names. """ x = images_ph layers = dict() for layer in VGG_layers: layer_type = layer[0] layer_name = layer[1] if layer_type == 'conv': with tf.variable_scope(layer_name) as scope: x = conv_layer(x, layer[2], 3, layer_name, train=False) elif layer_type == 'pool': with tf.variable_scope(layer_name) as scope: x = max_pool_2x2(x, layer_name) elif layer_type == 'fc': with tf.variable_scope(layer_name) as scope: x = conv_layer(x, layer[2], layer[3], layer_name, train=train_fc_layers) elif layer_type == 'relu': x = tf.nn.relu(x, layer_name) elif layer_type == 'dropout': x = tf.nn.dropout(x, dropout_keep_prob_ph, name=layer_name) layers[layer_name] = x return layers
def _vgg_net(weights, image): layers = ( 'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1', 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2', 'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3', 'relu3_3', 'conv3_4', 'relu3_4', 'pool3', 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3', 'relu4_3', 'conv4_4', 'relu4_4', 'pool4', 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3', 'relu5_3', 'conv5_4', 'relu5_4' ) net = {} current = image for i, name in enumerate(layers): kind = name[:4] if kind == 'conv': kernels, bias = weights[i][0][0][0][0] kernels = utils.get_variable( np.transpose(kernels, (1, 0, 2, 3)), name=name + '_w') bias = utils.get_variable(bias.reshape(-1), name=name + '_b') current = utils.conv2d_basic(current, kernels, bias) tf.add_to_collection("losses", tf.contrib.layers.l2_regularizer(0.0005)(kernels)) elif kind == 'relu': current = tf.nn.relu(current, name=name) elif kind == 'pool': current = utils.max_pool_2x2(current) net[name] = current return net
def _pool_layer_2(self): """ Second pooling layer. """ W_conv2 = utils.weight_variable([5, 5, 32, 64]) b_conv2 = utils.bias_variable([64]) h_conv2 = tf.nn.relu(utils.conv2d(self._pool_layer_1, W_conv2) + b_conv2) h_pool2 = utils.max_pool_2x2(h_conv2) # image size is 7 x 7 return h_pool2
def _pool_layer_1(self): """ First pooling layer. """ W_conv1 = utils.weight_variable([5, 5, 1, 32]) b_conv1 = utils.bias_variable([32]) x_image = tf.reshape(self.image, [-1, 28, 28, 1]) h_conv1 = tf.nn.relu(utils.conv2d(x_image, W_conv1) + b_conv1) h_pool1 = utils.max_pool_2x2(h_conv1) # image size is 14 x 14 here return h_pool1
def model(self, X, Y): feature = int(np.prod(X.get_shape()[1:])) classes = int(np.prod(Y.get_shape()[1:])) x_image = tf.reshape(X, [-1, feature, 1, 1]) # 1st conv layer with tf.name_scope('conv1'): W = weight_variable([5, 1, 1, 32]) b = bias_variable([32]) h = tf.nn.relu(conv2d(x_image, W) + b) conv1 = max_pool_2x2(h) # 2nd conv layer with tf.name_scope('conv2'): W = weight_variable([5, 1, 32, 64]) b = bias_variable([64]) conv2 = tf.nn.relu(conv2d(conv1, W) + b) keep_prob = tf.placeholder(tf.float32) # 1st fc layer with tf.name_scope('fc1'): shape = int(np.prod(conv2.get_shape()[1:])) W = weight_variable([shape, 1024]) b = bias_variable([1024]) conv2_flat = tf.reshape(conv2, [-1, shape]) h = tf.nn.relu(tf.matmul(conv2_flat, W) + b) fc1 = tf.nn.dropout(h, keep_prob) # 2nd fc layer with tf.name_scope('fc2'): W = weight_variable([1024, classes]) b = bias_variable([classes]) logits = tf.matmul(fc1, W) + b entropy = tf.nn.softmax_cross_entropy_with_logits(logits, Y, name='loss') loss = tf.reduce_mean(entropy) variable_summaries(loss, 'loss') return logits, loss, keep_prob, 'cnn'
lr = 1e-4 batch_size = 64 num_steps = 20000 coral_param = 0.01 with tf.name_scope('input'): x = tf.placeholder("float", shape=[None, 784]) y_ = tf.placeholder("float", shape=[None, 10]) x_image = tf.reshape(x, [-1, 28, 28, 1]) train_flag = tf.placeholder(tf.bool) with tf.name_scope('feature_generator'): W_conv1 = utils.weight_variable([5, 5, 1, 32], 'conv1_weight') b_conv1 = utils.bias_variable([32], 'conv1_bias') h_conv1 = tf.nn.relu(utils.conv2d(x_image, W_conv1) + b_conv1) h_pool1 = utils.max_pool_2x2(h_conv1) W_conv2 = utils.weight_variable([5, 5, 32, 64], 'conv2_weight') b_conv2 = utils.weight_variable([64], 'conv2_bias') h_conv2 = tf.nn.relu(utils.conv2d(h_pool1, W_conv2) + b_conv2) h_pool2 = utils.max_pool_2x2(h_conv2) h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) W_fc1 = utils.weight_variable([7 * 7 * 64, 1024], 'fc1_weight') b_fc1 = utils.bias_variable([1024], 'fc1_bias') h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) with tf.name_scope('slice_data'): h_s = tf.cond(train_flag, lambda: tf.slice(h_fc1, [0, 0], [batch_size / 2, -1]), lambda: h_fc1)
import reader import params import utils as u batch = reader.get_justOne_batch(100,"training") oh = tf.one_hot(batch[1], params.OUT_CLASSES, dtype=tf.int32) x_image = batch[0] y_ = oh coords_ = batch[2] #CONVOLUTIONAL LAYERS x_image = tf.reshape(x_image, [-1, 128,128,3]) sq1 = u.create_fire_module(x_image,16,64,64,3) mp1 = u.max_pool_2x2(sq1) #down to 64x64 sq2 = u.create_fire_module(mp1, 16,64,64,128) sq3 = u.create_fire_module(sq2, 16,64,64,128) sq4 = u.create_fire_module(sq3, 32,128,128,128) mp4 = u.max_pool_2x2(sq4) #down to 32x32 sq5 = u.create_fire_module(mp4, 32,128,128,256) sq6 = u.create_fire_module(sq5, 48,192,192,256) sq7 = u.create_fire_module(sq6, 48,192,192,384) sq8 = u.create_fire_module(sq7, 64,256,256,384) mp8 = u.max_pool_2x2(sq8)#down to 16x16 sq9 = u.create_fire_module(mp8, 64,256,256,512)
def MyNet(p1, p2, p3): with tf.variable_scope("p1_3d_tensor_process"): # 可以用placeholder来看shape的变化 #p1 = tf.placeholder(tf.float32, [None, 20, 40, 3]) #p1 = tf.reshape(p1,[None,20, 40, 3]) # 第一层卷积:5×5×1卷积核6个 [5,5,3,6] W_conv1 = weight_variable([5, 5, 3, 6]) b_conv1 = bias_variable([6]) h_conv1 = tf.nn.relu(conv2d1(p1, W_conv1) + b_conv1) # 第一个pooling 层 h_pool1 = max_pool_2x2(h_conv1) # 第二层卷积:3×3×6卷积核12个 [3,3,6,12] W_conv2 = weight_variable([3, 3, 6, 12]) b_conv2 = bias_variable([12]) h_conv2 = tf.nn.relu(conv2d2(h_pool1, W_conv2) + b_conv2) # 第二个pooling 层,输出(?, 20, 40, 12) h_pool2 = max_pool_2x2(h_conv2) #print(p1,h_conv1,h_pool1,h_conv2,h_pool2) # flatten h_pool2_flat = tf.reshape(h_pool2, [-1, 20 * 40 * 12]) # fc1 W_fc1 = weight_variable([20 * 40 * 12, 10]) b_fc1 = bias_variable([10]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) # dropout: 输出的维度和h_fc1一样,只是随机部分值被值为零 ##rate1 = tf.placeholder(tf.float32) h_fc1_drop = tf.nn.dropout(h_fc1, rate=C.DROPOUT_RATE) # 输出层 W_fc2 = weight_variable([10, 10]) b_fc2 = bias_variable([10]) p1_output = tf.nn.sigmoid(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) #print(p1_output) with tf.variable_scope("p2_2d_tensor_process", reuse=tf.AUTO_REUSE): #p2 = tf.placeholder(tf.float32,[None,8, 10]) #p2 = tf.reshape(p2, [None,8, 10]) # Define lstm cells with tensorflo # Forward direction cell rate2 = tf.placeholder(tf.float32) with tf.variable_scope("lstm1"): lstm_fw_cell = get_lstm(C.HIDDEN_SIZE, 1 - C.DROPOUT_RATE, name="lstm_fw") # Backward direction cell with tf.variable_scope("lstm2"): lstm_bw_cell = get_lstm(C.HIDDEN_SIZE, 1 - C.DROPOUT_RATE, name="lstm_bw") outputs, states = tf.nn.bidirectional_dynamic_rnn(cell_fw=lstm_fw_cell, cell_bw=lstm_bw_cell, dtype=tf.float32, inputs=p2) output_fw, output_bw = outputs states_fw, states_bw = states #print(output_fw,output_bw) #print(states_fw,states_bw) lstm_output = tf.concat([output_fw, output_bw], 2) #print(lstm_output.shape) #lstm_output = tf.reshape(lstm_output, [None, 8*40]) lstm_output = tf.layers.flatten(lstm_output) #print(lstm_output.shape) # 输出层 W_fc_lstm = weight_variable([8 * 40, 10]) b_fc_lstm = bias_variable([10], relu=False) p2_output = tf.nn.sigmoid( tf.matmul(lstm_output, W_fc_lstm) + b_fc_lstm) #print(p2_output) with tf.variable_scope("p3_1d_tensor_process"): #p3 = tf.placeholder(tf.float32,[None,10]) #p3 = tf.reshape(p3, [None,10]) W_fc_p3 = weight_variable([10, 10]) b_fc_p3 = bias_variable([10], relu=False) output = tf.nn.sigmoid(tf.matmul(p3, W_fc_p3) + b_fc_p3) W_fc_p3_ = weight_variable([10, 10]) b_fc_p3_ = bias_variable([10], relu=False) p3_output = tf.nn.sigmoid(tf.matmul(output, W_fc_p3_) + b_fc_p3_) #print(p1_output,p2_output,p3_output) all_concat = tf.concat([p1_output, p2_output, p3_output], 1) W_fc_all = weight_variable([3 * 10, 1]) b_fc_all = bias_variable([1], relu=False) y_pred = tf.nn.sigmoid(tf.matmul(all_concat, W_fc_all) + b_fc_all) #print(y_pred) return y_pred
import tensorflow as tf import reader import params import utils as u batch = reader.get_cifar_batch(1000, "cifar-10-batches-py/data_batch_1") oh = tf.one_hot(batch[1], params.OUT_CLASSES, dtype=tf.int32) x_image = batch[0] y_ = oh keep_prob = tf.placeholder(tf.float32) #CONVOLUTIONAL LAYERS x_image = tf.reshape(x_image, [-1, 32, 32, 3]) sq1 = u.create_fire_module(x_image, 16, 64, 64, 3) mp1 = u.max_pool_2x2(sq1) #down to 16x16 sq2 = u.create_fire_module(mp1, 16, 64, 64, 128) sq3 = u.create_fire_module(sq2, 16, 64, 64, 128) sq4 = u.create_fire_module(sq3, 32, 128, 128, 128) mp4 = u.max_pool_2x2(sq4) #down to 8x8 #sq5 = u.create_fire_module(mp4, 32,128,128,256) #sq6 = u.create_fire_module(sq5, 48,192,192,256) #sq7 = u.create_fire_module(sq6, 48,192,192,384) #sq8 = u.create_fire_module(sq7, 64,256,256,384) #mp8 = u.max_pool_2x2(sq8)#down to 4x4 sq9 = u.create_fire_module(mp4, 32, 128, 128, 256) #(mp8, 64,256,256,512)
def segmentation(image, keep_prob): """ 图像语义分割模型定义 Parameters ---------- image: 输入图像,每个通道的像素值为0到255 keep_prob: 防止过拟合的dropout参数 Returns ------- """ print("setting up vgg initialized conv layers ...") model_data = utils.get_model_data(FLAGS.model_dir) # vgg模型的权重值 weights = np.squeeze(model_data['layers']) # 计算图片像素值的均值, 然后对图像加上均值 mean = model_data['normalization'][0][0][0] mean_pixel = np.mean(mean, axis=(0, 1)) processed_image = utils.process_image(image, mean_pixel) # 共享变量名空间-segmentation with tf.variable_scope("segmentation"): image_net = vgg_net(weights, processed_image) conv_final_layer = image_net["conv5_3"] pool5 = utils.max_pool_2x2(conv_final_layer) # 全连接层用卷积层来代替 W6 = utils.weight_variable([7, 7, 512, 4096], name = "W6") b6 = utils.bias_variable([4096], name="b6") conv6 = utils.conv2d_basic(pool5, W6, b6) relu6 = tf.nn.relu(conv6, name="relu6") if FLAGS.debug: utils.add_activation_summary(relu6) # 随机去掉一些神经元防止过拟合 relu_dropout6 = tf.nn.dropout(relu6, keep_prob=keep_prob) W7 = utils.weight_variable([1, 1, 4096, 4096], name="W7") b7 = utils.bias_variable([4096], name="b7") conv7 = utils.conv2d_basic(relu_dropout6, W7, b7) relu7 = tf.nn.relu(conv7, name="relu7") if FLAGS.debug: utils.add_activation_summary(relu7) relu_dropout7 = tf.nn.dropout(relu7, keep_prob=keep_prob) W8 = utils.weight_variable([1, 1, 4096, NUM_OF_CLASSESS], name="W8") b8 = utils.bias_variable([NUM_OF_CLASSESS], name="b8") conv8 = utils.conv2d_basic(relu_dropout7, W8, b8) # annotation_pred1 = tf.argmax(conv8, dimension=3, name="prediction1") # now to upscale to actual image size deconv_shape1 = image_net["pool4"].get_shape() W_t1 = utils.weight_variable([4, 4, deconv_shape1[3].value, NUM_OF_CLASSESS], name="W_t1") b_t1 = utils.bias_variable([deconv_shape1[3].value], name="b_t1") conv_t1 = utils.conv2d_transpose_strided(conv8, W_t1, b_t1, output_shape=tf.shape(image_net["pool4"])) fuse_1 = tf.add(conv_t1, image_net["pool4"], name="fuse_1") deconv_shape2 = image_net["pool3"].get_shape() W_t2 = utils.weight_variable([4, 4, deconv_shape2[3].value, deconv_shape1[3].value], name="W_t2") b_t2 = utils.bias_variable([deconv_shape2[3].value], name="b_t2") conv_t2 = utils.conv2d_transpose_strided(fuse_1, W_t2, b_t2, output_shape=tf.shape(image_net["pool3"])) fuse_2 = tf.add(conv_t2, image_net["pool3"], name="fuse_2") shape = tf.shape(image) deconv_shape3 = tf.stack([shape[0], shape[1], shape[2], NUM_OF_CLASSESS]) W_t3 = utils.weight_variable([16, 16, NUM_OF_CLASSESS, deconv_shape2[3].value], name="W_t3") b_t3 = utils.bias_variable([NUM_OF_CLASSESS], name = "b_t3") conv_t3 = utils.conv2d_transpose_strided(fuse_2, W_t3, b_t3, output_shape = deconv_shape3, stride = 8) # 预测结果层 annotation_pred = tf.argmax(conv_t3, dimension = 3, name = "prediction") return tf.expand_dims(annotation_pred, dim = 3), conv_t3
def inference(image, keep_prob, train=False): """ Semantic segmentation network definition :param image: input image. Should have values in range 0-255 :param keep_prob: :return: """ print("setting up vgg initialized conv layers ...") model_data = utils.get_model_data(FLAGS.model_dir, MODEL_URL) mean = model_data['normalization'][0][0][0] mean_pixel = np.mean(mean, axis=(0, 1)) weights = np.squeeze(model_data['layers']) # accounts for the mean being subtracted from the image processed_image = utils.process_image(image, mean_pixel) with tf.variable_scope("inference"): image_net = vgg_net(weights, processed_image) conv_final_layer = image_net["conv5_3"] pool5 = utils.max_pool_2x2(conv_final_layer) W6 = utils.weight_variable([7, 7, 512, 4096], name="W6") b6 = utils.bias_variable([4096], name="b6") conv6 = utils.conv2d_basic(pool5, W6, b6) relu6 = tf.nn.relu(conv6, name="relu6") if FLAGS.debug: utils.add_activation_summary(relu6) if train: relu6 = tf.nn.dropout(relu6, keep_prob=keep_prob) W7 = utils.weight_variable([1, 1, 4096, 4096], name="W7") b7 = utils.bias_variable([4096], name="b7") conv7 = utils.conv2d_basic(relu6, W7, b7) relu7 = tf.nn.relu(conv7, name="relu7") if FLAGS.debug: utils.add_activation_summary(relu7) if train: relu7 = tf.nn.dropout(relu7, keep_prob=keep_prob) W8 = utils.weight_variable([1, 1, 4096, FLAGS.NUM_OF_CLASSES], name="W8") b8 = utils.bias_variable([FLAGS.NUM_OF_CLASSES], name="b8") conv8 = utils.conv2d_basic(relu7, W8, b8) # annotation_pred1 = tf.argmax(conv8, dimension=3, name="prediction1") # now to upscale to actual image size deconv_shape1 = image_net["pool4"].get_shape() W_t1 = utils.weight_variable([4, 4, deconv_shape1[3].value, FLAGS.NUM_OF_CLASSES], name="W_t1") b_t1 = utils.bias_variable([deconv_shape1[3].value], name="b_t1") conv_t1 = utils.conv2d_transpose_strided(conv8, W_t1, b_t1, output_shape=tf.shape(image_net["pool4"])) fuse_1 = tf.add(conv_t1, image_net["pool4"], name="fuse_1") deconv_shape2 = image_net["pool3"].get_shape() W_t2 = utils.weight_variable([4, 4, deconv_shape2[3].value, deconv_shape1[3].value], name="W_t2") b_t2 = utils.bias_variable([deconv_shape2[3].value], name="b_t2") conv_t2 = utils.conv2d_transpose_strided(fuse_1, W_t2, b_t2, output_shape=tf.shape(image_net["pool3"])) fuse_2 = tf.add(conv_t2, image_net["pool3"], name="fuse_2") shape = tf.shape(image) deconv_shape3 = tf.pack([shape[0], shape[1], shape[2], FLAGS.NUM_OF_CLASSES]) W_t3 = utils.weight_variable([16, 16, FLAGS.NUM_OF_CLASSES, deconv_shape2[3].value], name="W_t3") b_t3 = utils.bias_variable([FLAGS.NUM_OF_CLASSES], name="b_t3") conv_t3 = utils.conv2d_transpose_strided(fuse_2, W_t3, b_t3, output_shape=deconv_shape3, stride=8) annotation_pred = tf.argmax(conv_t3, dimension=3, name="prediction") return tf.expand_dims(annotation_pred, dim=3), conv_t3
def inference(image, keep_prob): """ Semantic segmentation network definition :param image: :param keep_prob: :return: """ print('setting up vgg model initialized params') model_data = utils.get_model_data("data", MODEL_URL) mean = model_data['normalization'][0][0][0] mean_pixel = np.mean(mean, axis=(0, 1)) weights = np.squeeze(model_data['layers']) processed_image = utils.process_image(image, mean_pixel) with tf.name_scope('inference'): image_net = vgg_net(weights, processed_image) conv_final_layer = image_net['conv5_3'] pool5 = utils.max_pool_2x2(conv_final_layer) W6 = utils.weights_variable([7, 7, 512, 4096], name="W6") b6 = utils.bias_variable([4096], name='b6') conv6 = utils.conv2d_basic(pool5, W6, b6) relu6 = tf.nn.relu(conv6, name='relu6') relu_dropout6 = tf.nn.dropout(relu6, keep_prob=keep_prob) W7 = utils.weights_variable([1, 1, 4096, 4096], name="W7") b7 = utils.bias_variable([4096], name="b7") conv7 = utils.conv2d_basic(relu_dropout6, W7, b7) relu7 = tf.nn.relu(conv7, name="relu7") relu_dropout7 = tf.nn.dropout(relu7, keep_prob=keep_prob) W8 = utils.weights_variable([1, 1, 4096, NUM_OF_CLASSESS], name='W8') b8 = utils.bias_variable([NUM_OF_CLASSESS], name="b8") conv8 = utils.conv2d_basic(relu_dropout7, W8, b8) #unsampling to actual image size deconv_shape1 = image_net['pool4'].get_shape() W_t1 = utils.weights_variable( [4, 4, deconv_shape1[3].value, NUM_OF_CLASSESS], name='W_t1') b_t1 = utils.bias_variable([deconv_shape1[3].value], name="b_t1") conv_t1 = utils.conv2d_transpose_strided(conv8, W_t1, b_t1, output_shape=tf.shape( image_net['pool4'])) fuse_1 = tf.add(conv_t1, image_net['pool4'], name='fuse_1') deconv_shape2 = image_net['pool3'].get_shape() W_t2 = utils.weights_variable( [4, 4, deconv_shape2[3].value, deconv_shape1[3].value], name='W_t2') b_t2 = utils.bias_variable([deconv_shape2[3].value], name="b_t2") conv_t2 = utils.conv2d_transpose_strided(fuse_1, W_t2, b_t2, output_shape=tf.shape( image_net['pool3'])) fuse_2 = tf.add(conv_t2, image_net["pool3"], name="fuse_2") shape = tf.shape(image) output_shape = tf.stack( [shape[0], shape[1], shape[2], NUM_OF_CLASSESS]) W_t3 = utils.weights_variable( [7, 7, NUM_OF_CLASSESS, deconv_shape2[3].value], name='W_t3') b_t3 = utils.bias_variable([NUM_OF_CLASSESS], name="b_t3") conv_t3 = utils.conv2d_transpose_strided(fuse_2, W_t3, b_t3, output_shape=output_shape) annotation_pre = tf.argmax(conv_t3, dimension=3, name='prediction') return tf.expand_dims(annotation_pre, dim=3), conv_t3
def segment(image, keep_prob_conv, input_channels, output_channels, scope): with tf.variable_scope(scope): ############### # downsample # ############### W2 = utils.weight_variable([3, 3, input_channels, 64], name="W2") b2 = utils.bias_variable([64], name="b2") conv2 = utils.conv2d_basic(image, W2, b2, name="conv2") relu2 = tf.nn.relu(conv2, name="relu2") pool2 = utils.max_pool_2x2(relu2) dropout2 = tf.nn.dropout(pool2, keep_prob=keep_prob_conv) W3 = utils.weight_variable([3, 3, 64, 128], name="W3") b3 = utils.bias_variable([128], name="b3") conv3 = utils.conv2d_basic(dropout2, W3, b3, name="conv3") relu3 = tf.nn.relu(conv3, name="relu3") pool3 = utils.max_pool_2x2(relu3) dropout3 = tf.nn.dropout(pool3, keep_prob=keep_prob_conv) W4 = utils.weight_variable([3, 3, 128, 256], name="W4") b4 = utils.bias_variable([256], name="b4") conv4 = utils.conv2d_basic(dropout3, W4, b4, name="conv4") relu4 = tf.nn.relu(conv4, name="relu4") pool4 = utils.max_pool_2x2(relu4) dropout4 = tf.nn.dropout(pool4, keep_prob=keep_prob_conv) W5 = utils.weight_variable([3, 3, 256, 512], name="W5") b5 = utils.bias_variable([512], name="b5") conv5 = utils.conv2d_basic(dropout4, W5, b5, name="conv5") relu5 = tf.nn.relu(conv5, name="relu5") pool5 = utils.max_pool_2x2(relu5) dropout5 = tf.nn.dropout(pool5, keep_prob=keep_prob_conv) W6 = utils.weight_variable([3, 3, 512, 512], name="W6") b6 = utils.bias_variable([512], name="b6") conv6 = utils.conv2d_basic(dropout5, W6, b6, name="conv6") relu6 = tf.nn.relu(conv6, name="relu6") pool6 = utils.max_pool_2x2(relu6) dropout6 = tf.nn.dropout(pool6, keep_prob=keep_prob_conv) W7 = utils.weight_variable([3, 3, 512, 4096], name="W7") b7 = utils.bias_variable([4096], name="b7") conv7 = utils.conv2d_basic(dropout6, W7, b7, name="conv7") ############ # upsample # ############ deconv_shape1 = pool5.get_shape() W_t1 = utils.weight_variable([4, 4, deconv_shape1[3].value, 4096], name="W_t1") b_t1 = utils.bias_variable([deconv_shape1[3].value], name="b_t1") conv_t1 = utils.conv2d_transpose_strided(conv7, W_t1, b_t1, output_shape=tf.shape(pool5)) stacked_1 = tf.concat([conv_t1, pool5], -1) fuse_1_1 = conv_layer(stacked_1, 1, 2 * deconv_shape1[3].value, deconv_shape1[3].value, "fuse_1_1") fuse_1_2 = conv_layer(fuse_1_1, 1, deconv_shape1[3].value, deconv_shape1[3].value, "fuse_1_2") deconv_shape2 = pool4.get_shape() W_t2 = utils.weight_variable( [4, 4, deconv_shape2[3].value, deconv_shape1[3].value], name="W_t2") b_t2 = utils.bias_variable([deconv_shape2[3].value], name="b_t2") conv_t2 = utils.conv2d_transpose_strided(fuse_1_2, W_t2, b_t2, output_shape=tf.shape(pool4)) stacked_2 = tf.concat([conv_t2, pool4], -1) fuse_2_1 = conv_layer(stacked_2, 1, 2 * deconv_shape2[3].value, deconv_shape2[3].value, "fuse_2_1") fuse_2_2 = conv_layer(fuse_2_1, 1, deconv_shape2[3].value, deconv_shape2[3].value, "fuse_2_2") deconv_shape3 = pool3.get_shape() W_t3 = utils.weight_variable( [4, 4, deconv_shape3[3].value, deconv_shape2[3].value], name="W_t3") b_t3 = utils.bias_variable([deconv_shape3[3].value], name="b_t3") conv_t3 = utils.conv2d_transpose_strided(fuse_2_2, W_t3, b_t3, output_shape=tf.shape(pool3)) stacked_3 = tf.concat([conv_t3, pool3], -1) fuse_3_1 = conv_layer(stacked_3, 1, 2 * deconv_shape3[3].value, deconv_shape3[3].value, "fuse_3_1") fuse_3_2 = conv_layer(fuse_3_1, 1, deconv_shape3[3].value, deconv_shape3[3].value, "fuse_3_2") deconv_shape4 = pool2.get_shape() W_t4 = utils.weight_variable( [4, 4, deconv_shape4[3].value, deconv_shape3[3].value], name="W_t4") b_t4 = utils.bias_variable([deconv_shape4[3].value], name="b_t4") conv_t4 = utils.conv2d_transpose_strided(fuse_3_2, W_t4, b_t4, output_shape=tf.shape(pool2)) stacked_4 = tf.concat([conv_t4, pool2], -1) fuse_4_1 = conv_layer(stacked_4, 1, 2 * deconv_shape4[3].value, deconv_shape4[3].value, "fuse_4_1") fuse_4_2 = conv_layer(fuse_4_1, 1, deconv_shape4[3].value, deconv_shape4[3].value, "fuse_4_2") # do the final upscaling shape = tf.shape(image) deconv_shape5 = tf.stack( [shape[0], shape[1], shape[2], output_channels]) W_t5 = utils.weight_variable( [16, 16, output_channels, deconv_shape4[3].value], name="W_t5") b_t5 = utils.bias_variable([output_channels], name="b_t5") conv_t5 = utils.conv2d_transpose_strided(fuse_4_2, W_t5, b_t5, output_shape=deconv_shape5, stride=2) annotation_pred = tf.argmax(conv_t5, dimension=3, name="prediction") return tf.expand_dims(annotation_pred, dim=3), conv_t5
def fine_tune_net(image, keep_prob): """ the network to be fine tuned and used to perform the semantic segmentation :param image: input image. :param keep_prob: for doupout :return: annotation prediction, probability map and 2nd last layer of vgg """ print("setting up vgg initialized conv layers ...") model_data = utils.get_model_data(FLAGS.model_dir, MODEL_URL) mean = model_data['normalization'][0][0][0] mean_pixel = np.mean(mean, axis=(0, 1)) weights = np.squeeze(model_data['layers']) processed_image = image - mean_pixel with tf.variable_scope("fine_tune"): image_net = vgg_net(weights, processed_image) conv_final_layer = image_net["conv5_3"] # 14x14x512 pool5 = utils.max_pool_2x2(conv_final_layer) # 7x7x512 W6 = utils.weight_variable([7, 7, 512, 4096], name="W6") b6 = utils.bias_variable([4096], name="b6") conv6 = utils.conv2d_basic(pool5, W6, b6) relu6 = tf.nn.relu(conv6, name="relu6") relu_dropout6 = tf.nn.dropout(relu6, keep_prob=keep_prob) # 7x7x4096 W7 = utils.weight_variable([1, 1, 4096, 4096], name="W7") b7 = utils.bias_variable([4096], name="b7") conv7 = utils.conv2d_basic(relu_dropout6, W7, b7) relu7 = tf.nn.relu(conv7, name="relu7") relu_dropout7 = tf.nn.dropout(relu7, keep_prob=keep_prob) # 7x7x4096 W8 = utils.weight_variable([1, 1, 4096, NUM_OF_CLASSESS], name="W8") b8 = utils.bias_variable([NUM_OF_CLASSESS], name="b8") conv8 = utils.conv2d_basic(relu_dropout7, W8, b8) # annotation_pred1 = tf.argmax(conv8, dimension=3, name="prediction1") # upscale deconv_shape1 = image_net["pool4"].get_shape() # 14x14x512 W_t1 = utils.weight_variable( [4, 4, deconv_shape1[3].value, NUM_OF_CLASSESS], name="W_t1") b_t1 = utils.bias_variable([deconv_shape1[3].value], name="b_t1") conv_t1 = utils.conv2d_transpose_strided(conv8, W_t1, b_t1, output_shape=tf.shape( image_net["pool4"])) # fuse_1 = tf.add(conv_t1, image_net["pool4"], name="fuse_1") deconv_shape2 = image_net["pool3"].get_shape() W_t2 = utils.weight_variable( [4, 4, deconv_shape2[3].value, deconv_shape1[3].value], name="W_t2") b_t2 = utils.bias_variable([deconv_shape2[3].value], name="b_t2") conv_t2 = utils.conv2d_transpose_strided(fuse_1, W_t2, b_t2, output_shape=tf.shape( image_net["pool3"])) fuse_2 = tf.add(conv_t2, image_net["pool3"], name="fuse_2") shape = tf.shape(image) deconv_shape3 = tf.stack( [shape[0], shape[1], shape[2], NUM_OF_CLASSESS]) W_t3 = utils.weight_variable( [4, 4, NUM_OF_CLASSESS, deconv_shape2[3].value], name="W_t3") b_t3 = utils.bias_variable([NUM_OF_CLASSESS], name="b_t3") conv_t3 = utils.conv2d_transpose_strided(fuse_2, W_t3, b_t3, output_shape=deconv_shape3, stride=8) #conv_t3 = tf.layers.conv2d_transpose(fuse_2,NUM_OF_CLASSESS,16,strides=(8,8),padding='SAME') #conv_t3.set_shape([None,IMAGE_SIZE,IMAGE_SIZE,NUM_OF_CLASSESS]) conv_t3 = tf.nn.softmax(conv_t3, axis=-1) annotation_pred = tf.argmax(conv_t3, axis=-1, name="prediction") return tf.expand_dims(annotation_pred, axis=-1), conv_t3, conv_final_layer
def prediction(self): """This function runs the forward propagation and output predictions.""" ### Construct the network # First layer suit (1 layer suit = 1 conv layer + (dropout) + 1 pooling layer + 1 NiN layer) W_conv1 = utl.weight_variable([3, 3, 3, 300], name="w_conv1") b_conv1 = utl.bias_variable([300], name="b_conv1") h_conv1 = utl._leakyrelu(utl.conv2d(self.image, W_conv1) + b_conv1, alpha=0.33) #h_conv1 = tf.keras.layers.LeakyRelu(utl.conv2d(x_image, W_conv1) + b_conv1, alpha=0.33) h_pool1 = utl.max_pool_2x2(h_conv1) W_nin1 = utl.weight_variable([1, 1, 300, 300], name="w_nin1") b_nin1 = utl.bias_variable([300], name="b_nin1") h_nin1 = utl._leakyrelu(utl.conv2d(h_pool1, W_nin1) + b_nin1, alpha=0.33) # Second layer suit W_conv2 = utl.weight_variable([2, 2, 300, 600], name="w_conv2") b_conv2 = utl.bias_variable([600], name="b_conv2") h_conv2 = utl._leakyrelu(utl.conv2d(h_nin1, W_conv2) + b_conv2, alpha=0.33) #self.keep_prob2 = tf.placeholder(tf.float32) h_conv2_drop = tf.nn.dropout(h_conv2, self.keep_prob2) h_pool2 = utl.max_pool_2x2(h_conv2_drop) W_nin2 = utl.weight_variable([1, 1, 600, 600], name="w_nin2") b_nin2 = utl.bias_variable([600], name="b_nin2") h_nin2 = utl._leakyrelu(utl.conv2d(h_pool2, W_nin2) + b_nin2, alpha=0.33) # Third layer suit W_conv3 = utl.weight_variable([2, 2, 600, 900], name="w_conv3") b_conv3 = utl.bias_variable([900], name="b_conv3") h_conv3 = utl._leakyrelu(utl.conv2d(h_nin2, W_conv3) + b_conv3, alpha=0.33) #self.keep_prob3 = tf.placeholder(tf.float32) h_conv3_drop = tf.nn.dropout(h_conv3, self.keep_prob3) h_pool3 = utl.max_pool_2x2(h_conv3_drop) W_nin3 = utl.weight_variable([1, 1, 900, 900], name="w_nin3") b_nin3 = utl.bias_variable([900], name="b_nin3") h_nin3 = utl._leakyrelu(utl.conv2d(h_pool3, W_nin3) + b_nin3, alpha=0.33) # Fourth layer suit W_conv4 = utl.weight_variable([2, 2, 900, 1200], name="w_conv4") b_conv4 = utl.bias_variable([1200], name="b_conv4") h_conv4 = utl._leakyrelu(utl.conv2d(h_nin3, W_conv4) + b_conv4, alpha=0.33) #self.keep_prob4 = tf.placeholder(tf.float32) h_conv4_drop = tf.nn.dropout(h_conv4, self.keep_prob4) h_pool4 = utl.max_pool_2x2(h_conv4_drop) W_nin4 = utl.weight_variable([1, 1, 1200, 1200], name="w_nin4") b_nin4 = utl.bias_variable([1200], name="b_nin4") h_nin4 = utl._leakyrelu(utl.conv2d(h_pool4, W_nin4) + b_nin4, alpha=0.33) # Fifth layer suit W_conv5 = utl.weight_variable([2, 2, 1200, 1500], name="w_conv5") b_conv5 = utl.bias_variable([1500], name="b_conv5") h_conv5 = utl._leakyrelu(utl.conv2d(h_nin4, W_conv5) + b_conv5, alpha=0.33) #self.keep_prob5 = tf.placeholder(tf.float32) h_conv5_drop = tf.nn.dropout(h_conv5, self.keep_prob5) h_pool5 = utl.max_pool_2x2(h_conv5_drop) W_nin5 = utl.weight_variable([1, 1, 1500, 1500], name="w_nin5") b_nin5 = utl.bias_variable([1500], name="b_nin5") h_nin5 = utl._leakyrelu(utl.conv2d(h_pool5, W_nin5) + b_nin5, alpha=0.33) # Sixth conv layer W_conv6 = utl.weight_variable([2, 2, 1500, 1800], name="w_conv6") b_conv6 = utl.bias_variable([1800], name="b_conv6") h_conv6 = utl._leakyrelu(utl.conv2d(h_nin5, W_conv6) + b_conv6, alpha=0.33) #self.keep_prob6 = tf.placeholder(tf.float32) h_conv6_drop = tf.nn.dropout(h_conv6, self.keep_prob6) W_nin6 = utl.weight_variable([1, 1, 1800, 1800], name="w_nin6") b_nin6 = utl.bias_variable([1800], name="b_nin6") h_nin6 = utl._leakyrelu(utl.conv2d(h_conv6_drop, W_nin6) + b_nin6, alpha=0.33) h_nin6_flat = tf.reshape(h_nin6, [-1, 1 * 1 * 1800]) # Softmax classification layer W_sm = utl.weight_variable([1 * 1 * 1800, 10], name="w_sm") b_sm = utl.bias_variable([10], name="b_sm") y_conv = tf.matmul(h_nin6_flat, W_sm) + b_sm return y_conv
def NetworkModel(image, keep_prob, reuse=False): with tf.variable_scope('NetworkModel', reuse=reuse): def _vgg_net(weights, image): layers = ( 'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1', 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2', 'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3', 'relu3_3', 'conv3_4', 'relu3_4', 'pool3', 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3', 'relu4_3', 'conv4_4', 'relu4_4', 'pool4', 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3', 'relu5_3', 'conv5_4', 'relu5_4' ) net = {} current = image for i, name in enumerate(layers): kind = name[:4] if kind == 'conv': kernels, bias = weights[i][0][0][0][0] kernels = utils.get_variable( np.transpose(kernels, (1, 0, 2, 3)), name=name + '_w') bias = utils.get_variable(bias.reshape(-1), name=name + '_b') current = utils.conv2d_basic(current, kernels, bias) tf.add_to_collection("losses", tf.contrib.layers.l2_regularizer(0.0005)(kernels)) elif kind == 'relu': current = tf.nn.relu(current, name=name) elif kind == 'pool': current = utils.max_pool_2x2(current) net[name] = current return net print('setting up vgg model initialized params --> extractor') model_data = utils.get_model_data("data", MODEL_URL) weights = np.squeeze(model_data['layers']) with tf.name_scope('inference'): image_net = _vgg_net(weights, image) conv_final_layer = image_net['relu5_4'] pool5 = utils.max_pool_2x2(conv_final_layer) pool5_flatten = tf.reshape(pool5, [-1, 7 * 7 * 512]) W1 = utils.get_weights_variable(7 * 7 * 512, 4096, name='W1') tf.add_to_collection("losses", tf.contrib.layers.l2_regularizer(0.0005)(W1)) b1 = utils.get_bias_variable(4096, name='b1') matmul1 = tf.nn.bias_add(tf.matmul(pool5_flatten, W1), b1) matmul1 = tf.nn.relu(matmul1) matmul1 = tf.nn.dropout(matmul1, keep_prob) W2 = utils.get_weights_variable(4096, 4096, name='W2') tf.add_to_collection("losses", tf.contrib.layers.l2_regularizer(0.0005)(W2)) b2 = utils.get_bias_variable(4096, name='b2') matmul2 = tf.nn.bias_add(tf.matmul(matmul1, W2), b2) matmul2 = tf.nn.relu(matmul2) matmul2 = tf.nn.dropout(matmul2, keep_prob) W3 = utils.get_weights_variable(4096, CLASS_COUNTS, name='W3') tf.add_to_collection("losses", tf.contrib.layers.l2_regularizer(0.0005)(W3)) b3 = utils.get_bias_variable(CLASS_COUNTS, name='b3') pre_logits = tf.nn.bias_add(tf.matmul(matmul2, W3), b3) # logits = tf.nn.softmax(pre_logits) # regularizer_losses = tf.get_collection("losses") return pre_logits #, logits
def model(self, X, Y): feature = int(np.prod(X.get_shape()[1:])) classes = int(np.prod(Y.get_shape()[1:])) x_image = tf.reshape(X, [-1, feature, 1, 1]) # 1st conv layer with tf.name_scope('conv1') as scope: w = weight_variable([5, 1, 1, 32]) b = bias_variable([32]) h = tf.nn.relu(conv2d(x_image, w) + b) conv1 = max_pool_2x2(h) # print "conv1 shape: ", h.get_shape() # print "pool1 shape: ", conv1.get_shape() # 2nd conv layer with tf.name_scope('conv2') as scope: w = weight_variable([5, 1, 32, 64]) b = bias_variable([64]) h = tf.nn.relu(conv2d(conv1, w) + b) conv2 = max_pool_2x2(h) # print "conv2 shape: ", h.get_shape() # print "pool2 shape: ", conv2.get_shape() # 3rd conv layer with tf.name_scope('conv3') as scope: w = weight_variable([5, 1, 64, 64]) b = bias_variable([64]) conv3 = tf.nn.relu(conv2d(conv2, w) + b) # print "conv3 shape: ", conv3.get_shape() # 4th conv layer with tf.name_scope('conv4') as scope: w = weight_variable([5, 1, 64, 64]) b = bias_variable([64]) conv4 = tf.nn.relu(conv2d(conv3, w) + b) # print "conv4 shape: ", conv4.get_shape() # 5th conv layer with tf.name_scope('conv5') as scope: w = weight_variable([5, 1, 64, 64]) b = bias_variable([64]) h = tf.nn.relu(conv2d(conv4, w) + b) conv5 = max_pool_2x2(h) # print "conv5 shape: ", h.get_shape() # print "pool5 shape: ", conv5.get_shape() # dropout keep_prob = tf.placeholder(tf.float32) # 1st fc layer with tf.name_scope('fc1') as scope: shape = int(np.prod(conv5.get_shape()[1:])) print('shape: ', shape) conv5_flat = tf.reshape(conv5, [-1, shape]) w = weight_variable([shape, 1024]) b = bias_variable([1024]) h = tf.nn.relu(tf.matmul(conv5_flat, w) + b) fc1 = tf.nn.dropout(h, keep_prob) # print "fc1 shape: ", fc1.get_shape() # 2nd fc layer with tf.name_scope('fc2') as scope: w = weight_variable([1024, 512]) b = bias_variable([512]) h = tf.nn.relu(tf.matmul(fc1, w) + b) fc2 = tf.nn.dropout(h, keep_prob) # print "fc2 shape: ", fc2.get_shape() # 3rd fc layer with tf.name_scope('fc3') as scope: w = weight_variable([512, classes]) b = bias_variable([classes]) logits = tf.matmul(fc2, w) + b # print "logits shape: ", logits.get_shape() entropy = tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=logits, name='loss') loss = tf.reduce_mean(entropy) variable_summaries(loss, 'loss') return logits, loss, keep_prob, "alex"
def u_net(image, phase_train, train=True, reuse=False, dtype=t): with tf.variable_scope("u_net", reuse=reuse): w1_1 = utils.weight_variable([3,3,int(image.shape[3]),32],name="w1_1", dtype=dtype) b1_1 = utils.bias_variable([32],name="b1_1", dtype=dtype) conv1_1 = utils.conv2d_basic(image,w1_1,b1_1, dtype=dtype) relu1_1 = tf.nn.relu(conv1_1, name="relu1_1") w1_2 = utils.weight_variable([3,3,32,32],name="w1_2", dtype=dtype) b1_2 = utils.bias_variable([32],name="b1_2", dtype=dtype) conv1_2 = utils.conv2d_basic(relu1_1,w1_2,b1_2, dtype=dtype) relu1_2 = tf.nn.relu(conv1_2, name="relu1_2") pool1 = utils.max_pool_2x2(relu1_2, dtype=dtype) bn1 = utils.batch_norm(pool1,pool1.get_shape()[3],phase_train,scope="bn1", is_train=train, dtype=dtype) w2_1 = utils.weight_variable([3,3,32,64],name="w2_1", dtype=dtype) b2_1 = utils.bias_variable([64],name="b2_1", dtype=dtype) conv2_1 = utils.conv2d_basic(bn1,w2_1,b2_1, dtype=dtype) relu2_1 = tf.nn.relu(conv2_1, name="relu2_1") w2_2 = utils.weight_variable([3,3,64,64],name="w2_2", dtype=dtype) b2_2 = utils.bias_variable([64],name="b2_2", dtype=dtype) conv2_2 = utils.conv2d_basic(relu2_1,w2_2,b2_2, dtype=dtype) relu2_2 = tf.nn.relu(conv2_2, name="relu2_2") pool2 = utils.max_pool_2x2(relu2_2, dtype=dtype) bn2 = utils.batch_norm(pool2,pool2.get_shape()[3],phase_train,scope="bn2", is_train=train, dtype=dtype) w3_1 = utils.weight_variable([3,3,64,128],name="w3_1", dtype=dtype) b3_1 = utils.bias_variable([128],name="b3_1", dtype=dtype) conv3_1 = utils.conv2d_basic(bn2,w3_1,b3_1, dtype=dtype) relu3_1 = tf.nn.relu(conv3_1, name="relu3_1") w3_2 = utils.weight_variable([3,3,128,128],name="w3_2", dtype=dtype) b3_2 = utils.bias_variable([128],name="b3_2", dtype=dtype) conv3_2 = utils.conv2d_basic(relu3_1,w3_2,b3_2, dtype=dtype) relu3_2 = tf.nn.relu(conv3_2, name="relu3_2") pool3 = utils.max_pool_2x2(relu3_2) bn3 = utils.batch_norm(pool3,pool3.get_shape()[3],phase_train,scope="bn3", is_train=train, dtype=dtype) w4_1 = utils.weight_variable([3,3,128,256],name="w4_1", dtype=dtype) b4_1 = utils.bias_variable([256],name="b4_1", dtype=dtype) conv4_1 = utils.conv2d_basic(bn3,w4_1,b4_1, dtype=dtype) relu4_1 = tf.nn.relu(conv4_1, name="relu4_1") w4_2 = utils.weight_variable([3,3,256,256],name="w4_2", dtype=dtype) b4_2 = utils.bias_variable([256],name="b4_2", dtype=dtype) conv4_2 = utils.conv2d_basic(relu4_1,w4_2,b4_2, dtype=dtype) relu4_2 = tf.nn.relu(conv4_2, name="relu4_2") bn4 = utils.batch_norm(relu4_2,relu4_2.get_shape()[3],phase_train,scope="bn4", is_train=train, dtype=dtype) W_t1 = utils.weight_variable([2, 2, 128, 256], name="W_t1", dtype=dtype) b_t1 = utils.bias_variable([128], name="b_t1", dtype=dtype) conv_t1 = utils.conv2d_transpose_strided(bn4, W_t1, b_t1, output_shape=tf.shape(relu3_2),dtype=dtype) merge1 = tf.concat([conv_t1,relu3_2],3) w5_1 = utils.weight_variable([3,3,256,128],name="w5_1", dtype=dtype) b5_1 = utils.bias_variable([128],name="b5_1", dtype=dtype) conv5_1 = utils.conv2d_basic(merge1,w5_1,b5_1, dtype=dtype) relu5_1 = tf.nn.relu(conv5_1, name="relu6_1") w5_2 = utils.weight_variable([3,3,128,128],name="w5_2", dtype=dtype) b5_2 = utils.bias_variable([128],name="b5_2", dtype=dtype) conv5_2 = utils.conv2d_basic(relu5_1,w5_2,b5_2,dtype=dtype) relu5_2 = tf.nn.relu(conv5_2, name="relu5_2") bn5 = utils.batch_norm(relu5_2,relu5_2.get_shape()[3],phase_train,scope="bn5", is_train=train, dtype=dtype) W_t2 = utils.weight_variable([2, 2, 64, 128], name="W_t2", dtype=dtype) b_t2 = utils.bias_variable([64], name="b_t2", dtype=dtype) conv_t2 = utils.conv2d_transpose_strided(bn5, W_t2, b_t2, output_shape=tf.shape(relu2_2),dtype=dtype) merge2 = tf.concat([conv_t2,relu2_2],3) w6_1= utils.weight_variable([3,3,128,64],name="w6_1", dtype=dtype) b6_1= utils.bias_variable([64],name="b6_1", dtype=dtype) conv6_1 = utils.conv2d_basic(merge2,w6_1,b6_1, dtype=dtype) relu6_1 = tf.nn.relu(conv6_1, name="relu6_1") w6_2 = utils.weight_variable([3,3,64,64],name="w6_2", dtype=dtype) b6_2 = utils.bias_variable([64],name="b6_2", dtype=dtype) conv6_2 = utils.conv2d_basic(relu6_1,w6_2,b6_2, dtype=dtype) relu6_2 = tf.nn.relu(conv6_2, name="relu6_2") bn6 = utils.batch_norm(relu6_2,relu6_2.get_shape()[3],phase_train,scope="bn6", is_train=train, dtype=dtype) W_t3 = utils.weight_variable([2, 2, 32, 64], name="W_t3", dtype=dtype) b_t3 = utils.bias_variable([32], name="b_t3", dtype=dtype) conv_t3 = utils.conv2d_transpose_strided(bn6, W_t3, b_t3, output_shape=tf.shape(relu1_2),dtype=dtype) merge3 = tf.concat([conv_t3,relu1_2],3) w7_1 = utils.weight_variable([3,3,64,32],name="w7_1", dtype=dtype) b7_1 = utils.bias_variable([32],name="b7_1", dtype=dtype) conv7_1 = utils.conv2d_basic(merge3,w7_1,b7_1, dtype=dtype) relu7_1 = tf.nn.relu(conv7_1, name="relu7_1") w7_2 = utils.weight_variable([3,3,32,32],name="w7_2", dtype=dtype) b7_2 = utils.bias_variable([32],name="b7_2", dtype=dtype) conv7_2 = utils.conv2d_basic(relu7_1,w7_2,b7_2, dtype=dtype) relu7_2 = tf.nn.relu(conv7_2, name="relu7_2") bn7 = utils.batch_norm(relu7_2,relu7_2.get_shape()[3],phase_train,scope="bn8", is_train=train, dtype=dtype) w8 = utils.weight_variable([1, 1, 32, 1], name="w8", dtype=dtype) b8 = utils.bias_variable([1],name="b8", dtype=dtype) conv8 = utils.conv2d_basic(bn7,w8,b8, dtype=dtype) return conv8