def inference(image, keep_prob): print("setting up vgg initialized conv layers ...") model_data = utils.get_model_data(FLAGS.model_path) 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.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, "pool5") W6 = utils.weight_variable([7, 7, 512, 4096], name="W6") b6 = utils.bias_variable([4096], name="b6") conv6 = utils.conv2d_basic(pool5, W6, b6, name="conv6") 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.weight_variable([4096], name="b7") conv7 = utils.conv2d_basic(relu_dropout6, W7, b7, name="conv7") 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_CLASSES], name="W8") b8 = utils.bias_variable([NUM_OF_CLASSES], name="b8") conv8 = utils.conv2d_basic(relu_dropout7, W8, b8, name="conv8") # 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_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, "conv_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, "conv_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_CLASSES]) W_t3 = utils.weight_variable([16, 16, NUM_OF_CLASSES, deconv_shape2[3].value], name="W_t3") b_t3 = utils.bias_variable([NUM_OF_CLASSES], name="b_t3") conv_t3 = utils.conv2d_transpose_strided(fuse_2, W_t3, b_t3, "conv_t3", output_shape=deconv_shape3, stride=8) annotation_pred = tf.argmax(conv_t3, axis=2, name="prediction") return tf.expand_dims(annotation_pred, axi=3), conv_t3
def _spatial_transformer(self, name, x, in_filters, arr_out_filters): width = x.get_shape().as_list()[1] height = x.get_shape().as_list()[2] with tf.variable_scope(name): _x = MaxPooling2D(x, name='pool1') with tf.variable_scope('conv_1'): _x = Conv2D(_x, in_filters, 5, arr_out_filters[0], name='conv_1') _x = BatchNormalization(_x, self.mode == 'train', name='batch1') _x = MaxPooling2D(_x, use_relu=True, name='pool2') with tf.variable_scope('conv_2'): _x = Conv2D(_x, arr_out_filters[0], 5, arr_out_filters[1], name='conv_2') _x = BatchNormalization(_x, self.mode == 'train', name='batch2') _x = MaxPooling2D(_x, use_relu=True, name='pool3') with tf.variable_scope('fc1'): _x_flat, _x_size = Flatten(_x) W_fc_loc1 = weight_variable([_x_size, arr_out_filters[2]]) b_fc_loc1 = bias_variable([arr_out_filters[2]]) h_fc_loc1 = tf.nn.tanh( tf.matmul(_x_flat, W_fc_loc1) + b_fc_loc1) h_fc_loc1 = slim.dropout(h_fc_loc1, self._dropout, is_training=(self.mode == 'train' and self._dropout > 0), scope='dropout') with tf.variable_scope('fc2'): W_fc_loc2 = weight_variable([arr_out_filters[2], 6]) # Use identity transformation as starting point initial = np.array([[1., 0, 0], [0, 1., 0]]) initial = initial.astype('float32') initial = initial.flatten() b_fc_loc2 = tf.Variable(initial_value=initial, name='b_fc_loc2') h_fc_loc2 = tf.nn.tanh( tf.matmul(h_fc_loc1, W_fc_loc2) + b_fc_loc2) # %% We'll create a spatial transformer module to identify discriminative # %% patches out_size = (width, height) h_trans = transformer(x, h_fc_loc2, out_size) h_trans = tf.reshape( h_trans, [self.hps.batch_size, width, height, in_filters]) return h_trans
def _spatial_transformer(self, name, x, in_filters, arr_out_filters): width = x.get_shape().as_list()[1] height = x.get_shape().as_list()[2] with tf.variable_scope(name): W_fc_loc1 = weight_variable([width * height * in_filters, arr_out_filters[2]]) b_fc_loc1 = bias_variable([arr_out_filters[2]]) W_fc_loc2 = weight_variable([arr_out_filters[2], 6]) # Use identity transformation as starting point initial = np.array([[1., 0, 0], [0, 1., 0]]) initial = initial.astype('float32') initial = initial.flatten() b_fc_loc2 = tf.Variable(initial_value=initial, name='b_fc_loc2') x_reshape = tf.reshape(x, [self.hps.batch_size, -1]) # %% Define the two layer localisation network h_fc_loc1 = tf.nn.tanh(tf.matmul(x_reshape, W_fc_loc1) + b_fc_loc1) h_fc_loc1 = self._batch_norm2(name, h_fc_loc1) h_fc_loc1 = self._relu(h_fc_loc1) h_fc_loc1 = slim.dropout(h_fc_loc1, self._dropout, is_training=(self.mode == 'train' and self._dropout and self._dropout > 0), scope='dropout') h_fc_loc2 = tf.nn.tanh(tf.matmul(h_fc_loc1, W_fc_loc2) + b_fc_loc2) # %% We'll create a spatial transformer module to identify discriminative # %% patches out_size = (width, height) h_trans = transformer(x, h_fc_loc2, out_size) h_trans = tf.reshape(h_trans, [self.hps.batch_size, width, height, in_filters]) return h_trans
def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu, method="xavier"): """Reusable code for making a simple neural net layer. It does a matrix multiply, bias add, and then uses relu to nonlinearize. It also sets up name scoping so that the resultant graph is easy to read, and adds a number of summary ops. """ # Adding a name scope ensures logical grouping of the layers in the graph. with tf.name_scope(layer_name): # This Variable will hold the state of the weights for the layer with tf.name_scope('weights'): weights = weight_variable([input_dim, output_dim], method=method, name=layer_name) variable_summaries(weights, layer_name + '/weights') with tf.name_scope('biases'): biases = bias_variable([output_dim]) variable_summaries(biases, layer_name + '/biases') with tf.name_scope('Wx_plus_b'): preactivate = tf.matmul(input_tensor, weights) + biases tf.histogram_summary(layer_name + '/pre_activations', preactivate) if act is None: activations = preactivate else: activations = act(preactivate, 'activation') tf.histogram_summary(layer_name + '/activations', activations) return activations
def buildConv2D(inputWidth, inputHeight, inputConv, n_input_filters,filter_size, n_filters, stride, pad): WConv = weight_variable([filter_size, filter_size, n_input_filters, n_filters]) bConv = bias_variable([n_filters]) hConv = tf.nn.relu( tf.nn.conv2d(input=inputConv, filter=WConv, strides=stride, padding=pad) + bConv) w,h = computeVolume(inputWidth, inputHeight, stride) return hConv,w,h
def block_flow(x, layers, in_features, growth, is_training, keep_prob, weights): current = x features = in_features for idx in range(layers): print(current.get_shape()) W = weight_variable([3, 3, features, growth]) weights.append(W) b = bias_variable([growth]) tmp = batch_activ_conv(current, is_training, keep_prob, W, b) current = tf.concat((current, tmp), axis=3) features += growth return current, features, weights
def block_single(x1, x2, x3, layers, in_features, growth, is_training, keep_prob, weights): current1 = x1 current2 = x2 current3 = x3 features = in_features for idx in range(layers): print(current1.get_shape()) W = weight_variable([3, 3, features, growth]) weights.append(W) b = bias_variable([growth]) tmp1 = batch_activ_conv(current1, is_training, keep_prob, W, b) tmp2 = batch_activ_conv(current2, is_training, keep_prob, W, b) tmp3 = batch_activ_conv(current3, is_training, keep_prob, W, b) current1 = tf.concat((current1, tmp1), axis=3) current2 = tf.concat((current2, tmp2), axis=3) current3 = tf.concat((current3, tmp3), axis=3) features += growth return current1, current2, current3, features, weights
def spTrans(x_tensor,width, height, channels, n_loc,keep_prob): resolution = width * height * channels W_fc_loc1 = weight_variable([resolution, n_loc]) b_fc_loc1 = bias_variable([n_loc]) W_fc_loc2 = weight_variable([n_loc, 6]) # Use identity transformation as starting point initial = np.array([[1., 0, 0], [0, 1., 0]]) initial = initial.astype('float32') initial = initial.flatten() b_fc_loc2 = tf.Variable(initial_value=initial, name='b_fc_loc2') # Two layer localisation network h_fc_loc1 = tf.nn.tanh(tf.matmul(x, W_fc_loc1) + b_fc_loc1) # dropout (reduce overfittin) h_fc_loc1_drop = tf.nn.dropout(h_fc_loc1, keep_prob) # %% Second layer h_fc_loc2 = tf.nn.tanh(tf.matmul(h_fc_loc1_drop, W_fc_loc2) + b_fc_loc2) # spatial transformer out_size = (width, height) h_trans = transformer(x_tensor, h_fc_loc2, out_size) return h_trans, b_fc_loc2, h_fc_loc2
x = tf.placeholder(tf.float32, [None, resolution]) y = tf.placeholder(tf.float32, [None, numIndiv]) # %% Since x is currently [batch, height*width], we need to reshape to a # 4-D tensor to use it in a convolutional graph. If one component of # `shape` is the special value -1, the size of that dimension is # computed so that the total size remains constant. Since we haven't # defined the batch dimension's shape yet, we use -1 to denote this # dimension should not change size. x_tensor = tf.reshape(x, [-1, imsize[1], imsize[2], imsize[0]]) # %% We'll setup the two-layer localisation network to figure out the # %% parameters for an affine transformation of the input # %% Create variables for fully connected layer W_fc_loc1 = weight_variable([resolution, 20]) b_fc_loc1 = bias_variable([20]) # 6 is the number of parameters needed for the affine transformer W_fc_loc2 = weight_variable([20, 6]) # Use identity transformation as starting point # the multiplication (2x2) matrix is set to be the identity and the # sum (translation) is [0,0] initial = np.array([[1., 0, 0], [0, 1., 0]]) initial = initial.astype('float32') initial = initial.flatten() b_fc_loc2 = tf.Variable(initial_value=initial, name='b_fc_loc2') # %% Define the two layer localisation network h_fc_loc1 = tf.nn.tanh(tf.matmul(x, W_fc_loc1) + b_fc_loc1) # %% We can add dropout for regularizing and to reduce overfitting like so: keep_prob = tf.placeholder(tf.float32) h_fc_loc1_drop = tf.nn.dropout(h_fc_loc1, keep_prob)
def buildSoftMax(inputSoftMax,n_fc,classes): W_fc = weight_variable([n_fc, classes]) b_fc = bias_variable([classes]) y_logits = tf.matmul(inputSoftMax, W_fc) + b_fc return y_logits
def buildFc(inputFc,height,width,n_filters,n_fc,keep_prob): W_fc = weight_variable([height * width * n_filters, n_fc]) b_fc = bias_variable([n_fc]) h_fc = tf.nn.relu(tf.matmul(inputFc, W_fc) + b_fc) h_fc_drop = tf.nn.dropout(h_fc, keep_prob) return h_fc_drop
l_drp2_out = tf.nn.dropout(l_pool2_out,keep_prob) W_conv2_out = weight_variable([3,3,32,32]) l_conv2_out = tf.nn.relu(tf.nn.conv2d(l_drp2_out,W_conv2_out,strides=[1,1,1,1],padding='VALID')) # %% We'll now reshape so we can connect to a fully-connected layer: l_conv2_out_flat = tf.reshape(l_conv2_out, [-1, 4*4*32]) # %% Create a fully-connected layer: n_fc = 400 W_fc1 = weight_variable([4*4*32, n_fc]) b_fc1 = bias_variable([n_fc]) h_fc1 = tf.nn.relu(tf.matmul(l_conv2_out_flat, W_fc1) + b_fc1) # %% And finally our softmax layer: W_fc2 = weight_variable([n_fc, 10]) b_fc2 = bias_variable([10]) y_logits = tf.matmul(h_fc1, W_fc2) + b_fc2 # %% Define loss/eval/training functions cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(y_logits,y)) opt = tf.train.RMSPropOptimizer(0.0005) #opt = tf.train.AdamOptimizer() optimizer = opt.minimize(cross_entropy) # %% Monitor accuracy
# %% Placeholders for 40x40 resolution x = tf.placeholder(tf.float32, [None, 1600]) y = tf.placeholder(tf.float32, [None, 10]) # %% Since x is currently [batch, height*width], we need to reshape to a # 4-D tensor to use it in a convolutional graph. If one component of # `shape` is the special value -1, the size of that dimension is # computed so that the total size remains constant. Since we haven't # defined the batch dimension's shape yet, we use -1 to denote this # dimension should not change size. x_tensor = tf.reshape(x, [-1, 40, 40, 1]) # %% We'll setup the two-layer localisation network to figure out the parameters for an affine transformation of the input # %% Create variables for fully connected layer W_fc_loc1 = weight_variable([1600, 20]) b_fc_loc1 = bias_variable([20]) W_fc_loc2 = weight_variable([20, 6]) initial = np.array([[1., 0, 0], [0, 1., 0]]) # Use identity transformation as starting point initial = initial.astype('float32') initial = initial.flatten() b_fc_loc2 = tf.Variable(initial_value=initial, name='b_fc_loc2') # %% Define the two layer localisation network h_fc_loc1 = tf.nn.tanh(tf.matmul(x, W_fc_loc1) + b_fc_loc1) # %% We can add dropout for regularizing and to reduce overfitting like so: keep_prob = tf.placeholder(tf.float32) h_fc_loc1_drop = tf.nn.dropout(h_fc_loc1, keep_prob) # %% Second layer
# %% Placeholders for 40x40 resolution x = tf.placeholder(tf.float32, [None, 1600]) y = tf.placeholder(tf.float32, [None, 10]) # %% Since x is currently [batch, height*width], we need to reshape to a # 4-D tensor to use it in a convolutional graph. If one component of # `shape` is the special value -1, the size of that dimension is # computed so that the total size remains constant. Since we haven't # defined the batch dimension's shape yet, we use -1 to denote this # dimension should not change size. x_tensor = tf.reshape(x, [-1, 40, 40, 1]) # %% We'll setup the two-layer localisation network to figure out the parameters for an affine transformation of the input # %% Create variables for fully connected layer W_fc_loc1 = weight_variable([1600, 20]) b_fc_loc1 = bias_variable([20]) W_fc_loc2 = weight_variable([20, 6]) initial = np.array([[1.,0, 0],[0,1.,0]]) # Use identity transformation as starting point initial = initial.astype('float32') initial = initial.flatten() b_fc_loc2 = tf.Variable(initial_value=initial, name='b_fc_loc2') # %% Define the two layer localisation network h_fc_loc1 = tf.nn.tanh(tf.matmul(x, W_fc_loc1) + b_fc_loc1) # %% We can add dropout for regularizing and to reduce overfitting like so: keep_prob = tf.placeholder(tf.float32) h_fc_loc1_drop = tf.nn.dropout(h_fc_loc1, keep_prob) # %% Second layer h_fc_loc2 = tf.nn.tanh(tf.matmul(h_fc_loc1_drop, W_fc_loc2) + b_fc_loc2)
def main(): """ Runs a simple linear regression model on the mnist dataset. """ # Load the mnist dataset. Class stores the train, validation and testing sets as numpy arrays. mnist = input_data.read_data_sets('MNIST_data', one_hot=True) # Create a tensforlow session. sess = tf.InteractiveSession() # Create the computational graph. Start with creating placeholders for the input and output data. # Input placeholder. input_placeholder = tf.placeholder(tf.float32, shape=[None, 784]) # Output placeholder. labeled_data = tf.placeholder(tf.float32, shape=[None, 10]) # Reshape input to a 4D tensor of [ -1 , width, height, channels]. -1 ensures the size remains consitent with # the original size. image_shape = [-1, 28, 28, 1] input_image = tf.reshape(input_placeholder, image_shape) # Create convolutional layers containing 2 convolutional layers and 1 fully connected layer. # Layer 1 computes 32 features for each 5x5 patch. conv1_weights = tf_utils.weight_variable([5, 5, 1, 32]) conv1_bias = tf_utils.bias_variable([32]) # Apply ReLU activation and max pool. conv1_act = tf.nn.relu( tf_utils.conv2d(input_image, conv1_weights) + conv1_bias) conv1_pool = tf_utils.max_pool_2x2(conv1_act) # Layer 2 computes 64 features of 5x5 patch. conv2_weights = tf_utils.weight_variable([5, 5, 32, 64]) conv2_bias = tf_utils.bias_variable([64]) # Apply ReLU activation and max pool. conv2_act = tf.nn.relu( tf_utils.conv2d(conv1_pool, conv2_weights) + conv2_bias) conv2_pool = tf_utils.max_pool_2x2(conv2_act) # Add fully connected layers. fc1_weights = tf_utils.weight_variable([7 * 7 * 64, 1024]) fc1_bias = tf_utils.bias_variable([1024]) # Apply Relu activation to flattened conv2d pool layer. conv2_flat = tf.reshape(conv2_pool, [-1, 7 * 7 * 64]) fc1_act = tf.nn.relu(tf.matmul(conv2_flat, fc1_weights) + fc1_bias) # Add dropout before the readout layer. keep_prob = tf.placeholder(tf.float32) dropout = tf.nn.dropout(fc1_act, keep_prob) # Add the readout layer for the 10 classes. readout_weights = tf_utils.weight_variable([1024, 10]) readout_bias = tf_utils.bias_variable([10]) readout_act = tf.matmul(dropout, readout_weights) + readout_bias # Cross entropy loss between the output labels and the model. cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=labeled_data, logits=readout_act)) # Define the training step with a learning rate for gradient descent and our cross entropy loss. learning_rate = 1e-4 train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy) # Initialize all variables. sess.run(tf.global_variables_initializer()) # Training model evaluation placeholders. # Define a placeholder for comparing equality between output and labels. predictions = tf.equal(tf.argmax(labeled_data, 1), tf.argmax(readout_act, 1)) accuracy = tf.reduce_mean(tf.cast(predictions, tf.float32)) # Run the training for a n steps. steps = 10000 batch_size = 50 for step in xrange(steps): # Sample a batch from the mnist dataset. batch = mnist.train.next_batch(batch_size) # Create a dict of the data from the sampled batch and run one training step. train_step.run(feed_dict={ input_placeholder: batch[0], labeled_data: batch[1], keep_prob: 0.5 }) # Print the training error after every 100 steps. if step % 100 == 0: train_accuracy = accuracy.eval( feed_dict={ input_placeholder: batch[0], labeled_data: batch[1], keep_prob: 1.0 }) print "Step: ", step, " | Train Accuracy: ", train_accuracy print "Accuracy: ", accuracy.eval( feed_dict={ input_placeholder: mnist.test.images, labeled_data: mnist.test.labels, keep_prob: 1.0 })
convs = [] dw_h_convs = OrderedDict() dw_h_convs_test = OrderedDict() print("in_node:", in_node.get_shape()) for layer in range(0, layers): stddev = np.sqrt(2 / (filter_size**2 * features)) if layer == 0: w1 = weight_variable( [filter_size, filter_size, n_channels, features], stddev) else: w1 = weight_variable( [filter_size, filter_size, features_bottleneck, features], stddev) b1 = bias_variable([features]) w2 = weight_variable([1, 1, features, features_bottleneck], stddev) b2 = bias_variable([features_bottleneck]) conv1 = conv2d(in_node, w1, keep_prob) print("layer:", layer, "conv1:", conv1.get_shape()) conv1_bn = tf.layers.batch_normalization(inputs=conv1, axis=-1, momentum=0.9, epsilon=0.001, center=True, scale=True, training=is_training) conv2 = conv2d(conv1_bn, w2, keep_prob) print("layer:", layer, "conv2:", conv2.get_shape())
# Since x is currently [batch, height*width], we need to reshape to a # 4-D tensor to use it in a convolutional graph. If one component of # `shape` is the special value -1, the size of that dimension is # computed so that the total size remains constant. Since we haven't # defined the batch dimension's shape yet, we use -1 to denote this # dimension should not change size. x_tensor = tf.reshape(x, [-1, 40, 40, 1]) # We'll setup the two-layer localisation network to figure out the # parameters for an affine transformation of the input # Create variables for fully connected layer numPixels = 1600 numNodesL1 = 20 numNodesL2 = 6 W_fc_loc1 = weight_variable([numPixels, numNodesL1]) b_fc_loc1 = bias_variable([numNodesL1]) W_fc_loc2 = weight_variable([numNodesL1, numNodesL2]) # Use identity transformation as starting point initial = np.array([[1., 0, 0], [0, 1., 0]]) initial = initial.astype('float32') initial = initial.flatten() b_fc_loc2 = tf.Variable(initial_value=initial, name='b_fc_loc2') # Define the connection of first layer keep_prob = tf.placeholder(tf.float32) h_fc_loc1 = tf.nn.tanh(tf.matmul(x, W_fc_loc1) + b_fc_loc1) h_fc_loc1_drop = tf.nn.dropout(h_fc_loc1, keep_prob) # Define the connection of second layer h_fc_loc2 = tf.nn.tanh(tf.matmul(h_fc_loc1_drop, W_fc_loc2) + b_fc_loc2)
#weights.append(w1) #biases.append(b1) convs.append(conv1) if 0 < layer < layers - 1: in_node = tf.concat((in_node, dw_h_convs[layer]), axis=-1) elif layer == 0: in_node = dw_h_convs[layer] print(in_node.get_shape()) #in_node = dw_h_convs[layers-1] print(in_node.get_shape()[3]) # Output Map stddev = np.sqrt(2 / (filter_size**2 * (in_node.get_shape()[3].value))) weight = weight_variable([1, 1, (in_node.get_shape()[3].value), 1], stddev) bias = bias_variable([1], -0.5) conv = conv2d(in_node, weight, tf.constant(1.0)) pred_mask = tf.nn.sigmoid(conv + bias) #pred_mask = tf.nn.relu(conv + bias) # loss #loss = tf.losses.mean_squared_error(pred_mask, crop(s_img,pred_mask)) #loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=s_, labels=s)) #loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=s_, labels=s)) loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=conv + bias, labels=s_img)) #sigmoid #loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=pred_mask, labels=s_img)) #relu loss_sum = tf.summary.scalar("cross_entropy_densenet", loss)
dw_h_convs1 = OrderedDict() dw_h_convs1_test = OrderedDict() convs2 = [] dw_h_convs2 = OrderedDict() dw_h_convs2_test = OrderedDict() convs3 = [] dw_h_convs3 = OrderedDict() dw_h_convs3_test = OrderedDict() convs_comb = [] dw_h_convs_comb = OrderedDict() dw_h_convs_comb_test = OrderedDict() #flow path W = weight_variable([filter_size, filter_size, n_channels, 16]) b = bias_variable([16]) weights.append(W) current_flow = conv2d_same(in_node, W, b) current_flow, features, weights = block_flow(current_flow, layers_flow, 16, 12, is_training, keep_prob, weights) print("Flow after Block1:", current_flow.get_shape()) W = weight_variable([1, 1, features, features // 4]) b = bias_variable([features // 4]) weights.append(W) current_flow = batch_activ_conv(current_flow, is_training, keep_prob, W, b) #current_flow = avg_pool(current_flow, 2) current_flow, features, weights = block_flow(current_flow, layers_flow,
up_h_convs = OrderedDict() # down layers for layer in range(0, layers): features = 2**layer * features_root stddev = np.sqrt(2 / (filter_size**2 * features)) if layer == 0: w1 = weight_variable( [filter_size, filter_size, n_channels, features], stddev) else: w1 = weight_variable( [filter_size, filter_size, features // 2, features], stddev) w2 = weight_variable([filter_size, filter_size, features, features], stddev) b1 = bias_variable([features]) b2 = bias_variable([features]) conv1 = conv2d(in_node, w1, keep_prob) tmp_h_conv = tf.nn.relu(conv1 + b1) conv2 = conv2d(tmp_h_conv, w2, keep_prob) dw_h_convs[layer] = tf.nn.relu(conv2 + b2) weights.append((w1, w2)) biases.append((b1, b2)) convs.append((conv1, conv2)) if layer < layers - 1: pools[layer] = max_pool(dw_h_convs[layer], pool_size) in_node = pools[layer]
def GEDDnet(face, left_eye, right_eye, keep_prob, is_train, subj_id, vgg_path, num_subj=15, rf=[[2, 2], [3, 3], [5, 5], [11, 11]], num_face=[64, 128, 64, 64, 128, 256, 64], r=[[2, 2], [3, 3], [4, 5], [5, 11]], num_eye=[64, 128, 64, 64, 128, 256], num_comb=[0, 256]): num_comb[0] = num_face[-1] + 2 * num_eye[-1] vgg = np.load(vgg_path) with tf.variable_scope("transfer"): W_conv1_1 = tf.Variable(vgg['conv1_1_W']) b_conv1_1 = tf.Variable(vgg['conv1_1_b']) W_conv1_2 = tf.Variable(vgg['conv1_2_W']) b_conv1_2 = tf.Variable(vgg['conv1_2_b']) W_conv2_1 = tf.Variable(vgg['conv2_1_W']) b_conv2_1 = tf.Variable(vgg['conv2_1_b']) W_conv2_2 = tf.Variable(vgg['conv2_2_W']) b_conv2_2 = tf.Variable(vgg['conv2_2_b']) del vgg """ define network """ # face face_h_conv1_1 = tf.nn.relu(conv2d(face, W_conv1_1) + b_conv1_1) face_h_conv1_2 = tf.nn.relu(conv2d(face_h_conv1_1, W_conv1_2) + b_conv1_2) face_h_pool1 = max_pool_2x2(face_h_conv1_2) face_h_conv2_1 = tf.nn.relu(conv2d(face_h_pool1, W_conv2_1) + b_conv2_1) face_h_conv2_2 = tf.nn.relu(conv2d(face_h_conv2_1, W_conv2_2) + b_conv2_2) / 100. with tf.variable_scope("face"): face_W_conv2_3 = weight_variable([1, 1, num_face[1], num_face[2]], std=0.125) face_b_conv2_3 = bias_variable([num_face[2]], std=0.001) face_W_conv3_1 = weight_variable([3, 3, num_face[2], num_face[3]], std=0.06) face_b_conv3_1 = bias_variable([num_face[3]], std=0.001) face_W_conv3_2 = weight_variable([3, 3, num_face[3], num_face[3]], std=0.06) face_b_conv3_2 = bias_variable([num_face[3]], std=0.001) face_W_conv4_1 = weight_variable([3, 3, num_face[3], num_face[4]], std=0.08) face_b_conv4_1 = bias_variable([num_face[4]], std=0.001) face_W_conv4_2 = weight_variable([3, 3, num_face[4], num_face[4]], std=0.07) face_b_conv4_2 = bias_variable([num_face[4]], std=0.001) face_W_fc1 = weight_variable([6 * 6 * num_face[4], num_face[5]], std=0.035) face_b_fc1 = bias_variable([num_face[5]], std=0.001) face_W_fc2 = weight_variable([num_face[5], num_face[6]], std=0.1) face_b_fc2 = bias_variable([num_face[6]], std=0.001) face_h_conv2_3 = tf.nn.relu( conv2d(face_h_conv2_2, face_W_conv2_3) + face_b_conv2_3) face_h_conv2_3_norm = tf.layers.batch_normalization(face_h_conv2_3, training=is_train, scale=False, renorm=True, name="f_conv2_3") face_h_conv3_1 = tf.nn.relu( dilated2d(face_h_conv2_3_norm, face_W_conv3_1, rf[0]) + face_b_conv3_1) face_h_conv3_1_norm = tf.layers.batch_normalization(face_h_conv3_1, training=is_train, scale=False, renorm=True, name="f_conv3_1") face_h_conv3_2 = tf.nn.relu( dilated2d(face_h_conv3_1_norm, face_W_conv3_2, rf[1]) + face_b_conv3_2) face_h_conv3_2_norm = tf.layers.batch_normalization(face_h_conv3_2, training=is_train, scale=False, renorm=True, name="f_conv3_2") face_h_conv4_1 = tf.nn.relu( dilated2d(face_h_conv3_2_norm, face_W_conv4_1, rf[2]) + face_b_conv4_1) face_h_conv4_1_norm = tf.layers.batch_normalization(face_h_conv4_1, training=is_train, scale=False, renorm=True, name="f_conv4_1") face_h_conv4_2 = tf.nn.relu( dilated2d(face_h_conv4_1_norm, face_W_conv4_2, rf[3]) + face_b_conv4_2) face_h_conv4_2_norm = tf.layers.batch_normalization(face_h_conv4_2, training=is_train, scale=False, renorm=True, name="f_conv4_2") face_h_pool4_flat = tf.reshape(face_h_conv4_2_norm, [-1, 6 * 6 * num_face[4]]) face_h_fc1 = tf.nn.relu( tf.matmul(face_h_pool4_flat, face_W_fc1) + face_b_fc1) face_h_fc1_norm = tf.layers.batch_normalization(face_h_fc1, training=is_train, scale=False, renorm=True, name="f_fc1") face_h_fc1_drop = tf.nn.dropout(face_h_fc1_norm, keep_prob) face_h_fc2 = tf.nn.relu( tf.matmul(face_h_fc1_drop, face_W_fc2) + face_b_fc2) face_h_fc2_norm = tf.layers.batch_normalization(face_h_fc2, training=is_train, scale=False, renorm=True, name="f_fc2") eye1_h_conv1_1 = tf.nn.relu(conv2d(left_eye, W_conv1_1) + b_conv1_1) eye1_h_conv1_2 = tf.nn.relu(conv2d(eye1_h_conv1_1, W_conv1_2) + b_conv1_2) eye1_h_pool1 = max_pool_2x2(eye1_h_conv1_2) eye1_h_conv2_1 = tf.nn.relu(conv2d(eye1_h_pool1, W_conv2_1) + b_conv2_1) eye1_h_conv2_2 = tf.nn.relu(conv2d(eye1_h_conv2_1, W_conv2_2) + b_conv2_2) / 100. eye2_h_conv1_1 = tf.nn.relu(conv2d(right_eye, W_conv1_1) + b_conv1_1) eye2_h_conv1_2 = tf.nn.relu(conv2d(eye2_h_conv1_1, W_conv1_2) + b_conv1_2) eye2_h_pool1 = max_pool_2x2(eye2_h_conv1_2) eye2_h_conv2_1 = tf.nn.relu(conv2d(eye2_h_pool1, W_conv2_1) + b_conv2_1) eye2_h_conv2_2 = tf.nn.relu(conv2d(eye2_h_conv2_1, W_conv2_2) + b_conv2_2) / 100. with tf.variable_scope("eye"): # left eye eye_W_conv2_3 = weight_variable([1, 1, num_eye[1], num_eye[2]], std=0.125) eye_b_conv2_3 = bias_variable([num_eye[2]], std=0.001) eye_W_conv3_1 = weight_variable([3, 3, num_eye[2], num_eye[3]], std=0.06) eye_b_conv3_1 = bias_variable([num_eye[3]], std=0.001) eye_W_conv3_2 = weight_variable([3, 3, num_eye[3], num_eye[3]], std=0.06) eye_b_conv3_2 = bias_variable([num_eye[3]], std=0.001) eye_W_conv4_1 = weight_variable([3, 3, num_eye[3], num_eye[4]], std=0.06) eye_b_conv4_1 = bias_variable([num_eye[4]], std=0.001) eye_W_conv4_2 = weight_variable([3, 3, num_eye[4], num_eye[4]], std=0.04) eye_b_conv4_2 = bias_variable([num_eye[4]], std=0.001) eye1_W_fc1 = weight_variable([4 * 6 * num_eye[4], num_eye[5]], std=0.026) eye1_b_fc1 = bias_variable([num_eye[5]], std=0.001) eye2_W_fc1 = weight_variable([4 * 6 * num_eye[4], num_eye[5]], std=0.026) eye2_b_fc1 = bias_variable([num_eye[5]], std=0.001) eye1_h_conv2_3 = tf.nn.relu( conv2d(eye1_h_conv2_2, eye_W_conv2_3) + eye_b_conv2_3) eye1_h_conv2_3_norm = tf.layers.batch_normalization(eye1_h_conv2_3, training=is_train, scale=False, renorm=True, name="e_conv2_3") eye1_h_conv3_1 = tf.nn.relu( dilated2d(eye1_h_conv2_3_norm, eye_W_conv3_1, r[0]) + eye_b_conv3_1) eye1_h_conv3_1_norm = tf.layers.batch_normalization(eye1_h_conv3_1, training=is_train, scale=False, renorm=True, name="e_conv3_1") eye1_h_conv3_2 = tf.nn.relu( dilated2d(eye1_h_conv3_1_norm, eye_W_conv3_2, r[1]) + eye_b_conv3_2) eye1_h_conv3_2_norm = tf.layers.batch_normalization(eye1_h_conv3_2, training=is_train, scale=False, renorm=True, name="e_conv3_2") eye1_h_conv4_1 = tf.nn.relu( dilated2d(eye1_h_conv3_2_norm, eye_W_conv4_1, r[2]) + eye_b_conv4_1) eye1_h_conv4_1_norm = tf.layers.batch_normalization(eye1_h_conv4_1, training=is_train, scale=False, renorm=True, name="e_conv4_1") eye1_h_conv4_2 = tf.nn.relu( dilated2d(eye1_h_conv4_1_norm, eye_W_conv4_2, r[3]) + eye_b_conv4_2) eye1_h_conv4_2_norm = tf.layers.batch_normalization(eye1_h_conv4_2, training=is_train, scale=False, renorm=True, name="e_conv4_2") eye1_h_pool4_flat = tf.reshape(eye1_h_conv4_2_norm, [-1, 4 * 6 * num_eye[4]]) eye1_h_fc1 = tf.nn.relu( tf.matmul(eye1_h_pool4_flat, eye1_W_fc1) + eye1_b_fc1) eye1_h_fc1_norm = tf.layers.batch_normalization(eye1_h_fc1, training=is_train, scale=False, renorm=True, name="e1_fc1") # right eye eye2_h_conv2_3 = tf.nn.relu( conv2d(eye2_h_conv2_2, eye_W_conv2_3) + eye_b_conv2_3) eye2_h_conv2_3_norm = tf.layers.batch_normalization(eye2_h_conv2_3, training=is_train, scale=False, renorm=True, name="e_conv2_3", reuse=True) eye2_h_conv3_1 = tf.nn.relu( dilated2d(eye2_h_conv2_3_norm, eye_W_conv3_1, r[0]) + eye_b_conv3_1) eye2_h_conv3_1_norm = tf.layers.batch_normalization(eye2_h_conv3_1, training=is_train, scale=False, renorm=True, name="e_conv3_1", reuse=True) eye2_h_conv3_2 = tf.nn.relu( dilated2d(eye2_h_conv3_1_norm, eye_W_conv3_2, r[1]) + eye_b_conv3_2) eye2_h_conv3_2_norm = tf.layers.batch_normalization(eye2_h_conv3_2, training=is_train, scale=False, renorm=True, name="e_conv3_2", reuse=True) eye2_h_conv4_1 = tf.nn.relu( dilated2d(eye2_h_conv3_2_norm, eye_W_conv4_1, r[2]) + eye_b_conv4_1) eye2_h_conv4_1_norm = tf.layers.batch_normalization(eye2_h_conv4_1, training=is_train, scale=False, renorm=True, name="e_conv4_1", reuse=True) eye2_h_conv4_2 = tf.nn.relu( dilated2d(eye2_h_conv4_1_norm, eye_W_conv4_2, r[3]) + eye_b_conv4_2) eye2_h_conv4_2_norm = tf.layers.batch_normalization(eye2_h_conv4_2, training=is_train, scale=False, renorm=True, name="e_conv4_2", reuse=True) eye2_h_pool4_flat = tf.reshape(eye2_h_conv4_2_norm, [-1, 4 * 6 * num_eye[4]]) eye2_h_fc1 = tf.nn.relu( tf.matmul(eye2_h_pool4_flat, eye2_W_fc1) + eye2_b_fc1) eye2_h_fc1_norm = tf.layers.batch_normalization(eye2_h_fc1, training=is_train, scale=False, renorm=True, name="e2_fc1") # combine both eyes and face with tf.variable_scope("combine"): cls1_W_fc2 = weight_variable([num_comb[0], num_comb[1]], std=0.07) cls1_b_fc2 = bias_variable([num_comb[1]], std=0.001) cls1_W_fc3 = weight_variable([num_comb[1], 2], std=0.125) cls1_b_fc3 = bias_variable([2], std=0.001) cls1_h_fc1_norm = tf.concat( [face_h_fc2_norm, eye1_h_fc1_norm, eye2_h_fc1_norm], axis=1) cls1_h_fc1_drop = tf.nn.dropout(cls1_h_fc1_norm, keep_prob) cls1_h_fc2 = tf.nn.relu( tf.matmul(cls1_h_fc1_drop, cls1_W_fc2) + cls1_b_fc2) cls1_h_fc2_norm = tf.layers.batch_normalization(cls1_h_fc2, training=is_train, scale=False, renorm=True, name="c_fc2") cls1_h_fc2_drop = tf.nn.dropout(cls1_h_fc2_norm, keep_prob) t_hat = tf.matmul(cls1_h_fc2_drop, cls1_W_fc3) + cls1_b_fc3 """ bias learning from subject id """ num_bias = (2 * num_subj, ) with tf.variable_scope("bias"): bias_W_fc = weight_variable([num_bias[0], 2], std=0.125) b_hat = tf.matmul(subj_id, bias_W_fc) g_hat = t_hat + b_hat l2_loss = (1e-2 * tf.nn.l2_loss(W_conv1_1) + 1e-2 * tf.nn.l2_loss(W_conv1_2) + 1e-2 * tf.nn.l2_loss(W_conv2_1) + 1e-2 * tf.nn.l2_loss(W_conv2_2) + tf.nn.l2_loss(face_W_conv2_3) + tf.nn.l2_loss(face_W_conv3_1) + tf.nn.l2_loss(face_W_conv3_2) + tf.nn.l2_loss(face_W_conv4_1) + tf.nn.l2_loss(face_W_conv4_2) + tf.nn.l2_loss(face_W_fc1) + tf.nn.l2_loss(face_W_fc2) + tf.nn.l2_loss(eye_W_conv2_3) + tf.nn.l2_loss(eye_W_conv3_1) + tf.nn.l2_loss(eye_W_conv3_2) + tf.nn.l2_loss(eye_W_conv4_1) + tf.nn.l2_loss(eye_W_conv4_2) + tf.nn.l2_loss(eye1_W_fc1) + tf.nn.l2_loss(eye2_W_fc1) + tf.nn.l2_loss(cls1_W_fc2) + tf.nn.l2_loss(cls1_W_fc3)) return g_hat, t_hat, bias_W_fc, l2_loss
# Create variables for fully connected layer for the localisation network # def weight_variable(shape): # initial = tf.random_uniform(shape, minval=0, maxval = 0.1) # # initial = tf.random_normal(shape, mean= 0, stddev= 0.1) # # initial = tf.random_uniform(shape, minval=-0.1, maxval = 0.1) * tf.sqrt(1.0/shape[0]) # return tf.Variable(initial) # # def bias_variable(shape): # initial = tf.random_uniform(shape, minval=0, maxval = 0.1) # # initial = tf.random_normal(shape, mean= 0, stddev= 0.1) # # initial = tf.random_uniform(shape, minval=-0.1, maxval = 0.1) * tf.sqrt(1.0/shape[0]) # return tf.Variable(initial) W_fc_loc1 = weight_variable([mri_height * mri_width * mri_channel, 50]) b_fc_loc1 = bias_variable([50]) W_fc_loc2 = weight_variable([50, param]) b_fc_loc2 = tf.Variable(initial_value=initial, name='b_fc_loc2') keep_prob = tf.placeholder(tf.float32) x_flat = tf.layers.flatten(x) h_fc_loc1 = tf.nn.tanh(tf.matmul(x_flat, W_fc_loc1) + b_fc_loc1) h_fc_loc1_drop = tf.nn.dropout(h_fc_loc1, keep_prob) h_fc_loc2 = tf.nn.tanh(tf.matmul(h_fc_loc1_drop, W_fc_loc2) + b_fc_loc2) h_fc_loc2_mat = tf.reshape(h_fc_loc2, (-1, 3, 4)) h_trans = spatial_transformer_network(x, h_fc_loc2) filter_size = 3
# %% Since x is currently [batch, height*width], we need to reshape to a # 4-D tensor to use it in a convolutional graph. If one component of # `shape` is the special value -1, the size of that dimension is # computed so that the total size remains constant. Since we haven't # defined the batch dimension's shape yet, we use -1 to denote this # dimension should not change size. x_tensor = tf.reshape(x, [-1, 16, 16, 16, 1]) filter_size = 3 keep_prob = tf.placeholder(tf.float32) # %% We'll setup the there-layer localisation network to figure out the # %% parameters for an rotation transformation of the input f1 = 8 w1 = weight_variable([filter_size, filter_size, filter_size, 1, f1]) b1 = bias_variable([f1]) h1 = tf.nn.relu( tf.nn.conv3d( input=x_tensor, filter=w1, strides=[1, 2, 2, 2, 1], padding='SAME') + b1) f2 = 4 w2 = weight_variable([filter_size, filter_size, filter_size, f1, f2]) b2 = bias_variable([f2]) h2 = tf.nn.relu( tf.nn.conv3d(input=h1, filter=w2, strides=[1, 2, 2, 2, 1], padding='SAME') + b2) h2 = tf.reshape(h2, [-1, 256]) w3 = weight_variable([256, 4]) b3 = bias_variable([4]) h_fc_loc2 = tf.nn.tanh(tf.matmul(h2, w3) + b3)
# `shape` is the special value -1, the size of that dimension is # computed so that the total size remains constant. Since we haven't # defined the batch dimension's shape yet, we use -1 to denote this # dimension should not change size. x_tensor = tf.reshape(x, [-1, 40, 40, 1]) x_tensor2 = tf.slice(x_tensor, [0, 10, 10, 0], [-1, 20, 20, -1]) with tf.name_scope('output'): y = tf.placeholder(tf.float32, [None, 10]) with tf.name_scope('loc1'): # %% We'll setup the two-layer localisation network to figure out the # %% parameters for an affine transformation of the input # %% Create variables for fully connected layer W_fc_loc1 = weight_variable([1600, 20]) b_fc_loc1 = bias_variable([20]) # %% Define the two layer localisation network h_fc_loc1 = tf.nn.tanh(tf.matmul(x, W_fc_loc1) + b_fc_loc1) # %% We can add dropout for regularizing and to reduce overfitting like so: keep_prob = tf.placeholder(tf.float32) h_fc_loc1_drop = tf.nn.dropout(h_fc_loc1, keep_prob) with tf.name_scope('loc2'): W_fc_loc2 = weight_variable([20, 9]) # Use identity transformation as starting point initial = np.array([[1., 0, 0], [0, 1., 0], [0, 0, 1.]]) initial = initial.astype('float32') initial = initial.flatten() b_fc_loc2 = tf.Variable(initial_value=initial, name='b_fc_loc2') # %% Second layer
def build_model(resolution, numIndiv): # %% Graph representation of our network # %% Placeholders for imsize = height*width resolution and labels x = tf.placeholder(tf.float32, [None, resolution]) y = tf.placeholder(tf.float32, [None, numIndiv]) # %% Since x is currently [batch, height*width], we need to reshape to a # 4-D tensor to use it in a convolutional graph. If one component of # `shape` is the special value -1, the size of that dimension is # computed so that the total size remains constant. Since we haven't # defined the batch dimension's shape yet, we use -1 to denote this # dimension should not change size. x_tensor = tf.reshape(x, [-1, imsize[1], imsize[2], imsize[0]]) # %% We'll setup the two-layer localisation network to figure out the # %% parameters for an affine transformation of the input # %% Create variables for fully connected layer W_fc_loc1 = weight_variable([resolution, 20]) b_fc_loc1 = bias_variable([20]) # 6 is the number of parameters needed for the affine transformer W_fc_loc2 = weight_variable([20, 6]) # Use identity transformation as starting point # the multiplication (2x2) matrix is set to be the identity and the # sum (translation) is [0,0] initial = np.array([[1., 0, 0], [0, 1., 0]]) initial = initial.astype('float32') initial = initial.flatten() b_fc_loc2 = tf.Variable(initial_value=initial, name='b_fc_loc2') # %% Define the two layer localisation network h_fc_loc1 = tf.nn.tanh(tf.matmul(x, W_fc_loc1) + b_fc_loc1) # %% We can add dropout for regularizing and to reduce overfitting like so: keep_prob = tf.placeholder(tf.float32) h_fc_loc1_drop = tf.nn.dropout(h_fc_loc1, keep_prob) # %% Second layer h_fc_loc2 = tf.nn.tanh(tf.matmul(h_fc_loc1_drop, W_fc_loc2) + b_fc_loc2) # %% We'll create a spatial transformer module to identify discriminative # %% patches out_size = (imsize[1], imsize[2]) h_trans = transformer(x_tensor, h_fc_loc2, out_size) # %% We'll setup the first convolutional layer filter_size = 3 n_filters_1 = 16 # Weight matrix is [height x width x input_channels x output_channels] W_conv1 = weight_variable([filter_size, filter_size, 1, n_filters_1]) # %% Bias is [output_channels] b_conv1 = bias_variable([n_filters_1]) # %% Now we can build a graph which does the first layer of convolution: # we define our stride as batch x height x width x channels # instead of pooling, we use strides of 2 and more layers # with smaller filters. h_conv1 = tf.nn.relu( tf.nn.conv2d(input=h_trans, filter=W_conv1, strides=[1, 2, 2, 1], padding='SAME') + b_conv1) # %% And just like the first layer, add additional layers to create # a deep net n_filters_2 = 64 W_conv2 = weight_variable([filter_size, filter_size, n_filters_1, n_filters_2]) b_conv2 = bias_variable([n_filters_2]) h_conv2 = tf.nn.relu( tf.nn.conv2d(input=h_conv1, filter=W_conv2, strides=[1, 2, 2, 1], padding='SAME') + b_conv2) # # # # %% And why not a third... n_filters_3 = 64 W_conv3 = weight_variable([filter_size, filter_size, n_filters_2, n_filters_3]) b_conv3 = bias_variable([n_filters_3]) h_conv3 = tf.nn.relu( tf.nn.conv2d(input=h_conv2, filter=W_conv3, strides=[1, 2, 2, 1], padding='SAME') + b_conv3) # # %% And why not a fourth... # n_filters_4 = 16 # W_conv4 = weight_variable([filter_size, filter_size, n_filters_3, n_filters_4]) # b_conv4 = bias_variable([n_filters_4]) # h_conv4 = tf.nn.relu( # tf.nn.conv2d(input=h_conv3, # filter=W_conv4, # strides=[1, 2, 2, 1], # padding='SAME') + # b_conv4) # %% We'll now reshape so we can connect to a fully-connected layer: # print resolution lastVolSize = 12*12*n_filters_3 # lastVolSize = 7*7*n_filters_3 h_conv4_flat = tf.reshape(h_conv3, [-1, lastVolSize]) # %% Create a fully-connected layer: n_fc = 4096 W_fc1 = weight_variable([lastVolSize, n_fc]) b_fc1 = bias_variable([n_fc]) h_fc1 = tf.nn.relu(tf.matmul(h_conv4_flat, W_fc1) + b_fc1) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) # %% second fc # n_fc2 = 256 # W_fc2 = weight_variable([n_fc, n_fc2]) # b_fc2 = bias_variable([n_fc2]) # h_fc2 = tf.nn.relu(tf.matmul(h_fc1, W_fc2) + b_fc2) # # h_fc2_drop = tf.nn.dropout(h_fc2, keep_prob) # %% And finally our softmax layer: W_fc3 = weight_variable([n_fc, numIndiv]) b_fc3 = bias_variable([numIndiv]) y_logits = tf.matmul(h_fc1_drop, W_fc3) + b_fc3 # %% Define loss/eval/training functions cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=y_logits,labels=y)) opt = tf.train.AdamOptimizer(learning_rate=0.001, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False, name='Adam') # opt = tf.train.FtrlOptimizer(0.01, learning_rate_power=-0.5, initial_accumulator_value=0.1, l1_regularization_strength=0.0, l2_regularization_strength=0.5, use_locking=False, name='Ftrl') # opt = tf.train.RMSPropOptimizer(learning_rate=0.1, decay=0.16, momentum=0.9, epsilon=1.0, use_locking=False, name='RMSProp') optimizer = opt.minimize(cross_entropy) grads = opt.compute_gradients(cross_entropy, [b_fc_loc2]) # %% Monitor accuracy correct_prediction = tf.equal(tf.argmax(y_logits, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
convs3 = [] dw_h_convs3 = OrderedDict() convs_comb = [] dw_h_convs_comb = OrderedDict() for layer in range(0, layers): stddev = np.sqrt(2 / (filter_size**2 * features)) if layer == 0: w1 = weight_variable( [filter_size, filter_size, n_channels // 3, features], stddev) else: w1 = weight_variable( [filter_size, filter_size, features, features], stddev) b1 = bias_variable([features]) conv1 = conv2d(in_node1, w1, keep_prob) dw_h_convs1[layer] = tf.nn.relu(conv1 + b1) conv2 = conv2d(in_node2, w1, keep_prob) dw_h_convs2[layer] = tf.nn.relu(conv2 + b1) conv3 = conv2d(in_node3, w1, keep_prob) dw_h_convs3[layer] = tf.nn.relu(conv3 + b1) weights.append(w1) biases.append(b1) convs1.append(conv1) convs2.append(conv2) convs3.append(conv3)
def create_fcn(placeholder, keep_prob, classes): """ Setup the main conv/deconv network """ with tf.variable_scope('inference'): vgg_net = create_vgg19(placeholder) conv_final = vgg_net['relu5_4'] output = tf_utils.max_pool_2x2(conv_final) conv_shapes = [[7, 7, 512, 4096], [1, 1, 4096, 4096], [1, 1, 4096, classes]] for i, conv_shape in enumerate(conv_shapes): name = 'conv%d' % (i + 6) with tf.variable_scope(name): W = tf_utils.weight_variable(conv_shape, name=name + '_w') b = tf_utils.bias_variable(conv_shape[-1:], name=name + '_b') output = tf_utils.conv2d(output, W, b) with tf.variable_scope('relu%d' % (i + 6)): if i < 2: output = tf.nn.relu(output) tf_utils.add_activation_summary(output, collections=['train']) output = tf.nn.dropout(output, keep_prob=keep_prob) pool4 = vgg_net['pool4'] pool3 = vgg_net['pool3'] deconv_shapes = [ tf.shape(pool4), tf.shape(pool3), tf.stack([ tf.shape(placeholder)[0], tf.shape(placeholder)[1], tf.shape(placeholder)[2], classes ]) ] W_shapes = [[4, 4, pool4.get_shape()[3].value, classes], [ 4, 4, pool3.get_shape()[3].value, pool4.get_shape()[3].value ], [16, 16, classes, pool3.get_shape()[3].value]] strides = [2, 2, 8] for i in range(3): name = 'deconv%d' % (i + 1) with tf.variable_scope(name): W = tf_utils.weight_variable(W_shapes[i], name=name + '_w') output = tf_utils.conv2d_transpose( output, W, None, output_shape=deconv_shapes[i], stride=strides[i]) with tf.variable_scope('skip%d' % (i + 1)): if i < 2: output = tf.add(output, vgg_net['pool%d' % (4 - i)]) prediction = tf.argmax(output, dimension=3, name='prediction') return tf.expand_dims(prediction, dim=3), output