def all_classifier(feature1, feature2, keep_prob): with tf.name_scope('all_classifier'): W1 = utils.get_weights_variable(4096, 2048, name='W1') b1 = utils.get_bias_variable(2048, name='b1') matmul1 = tf.nn.bias_add(tf.matmul(feature1, W1), b1) matmul1 = tf.nn.relu(matmul1) W2 = utils.get_weights_variable(4096, 2048, name='W2') b2 = utils.get_bias_variable(2048, name='b2') matmul2 = tf.nn.bias_add(tf.matmul(feature2, W2), b2) matmul2 = tf.nn.relu(matmul2) feature = tf.concat([matmul1, matmul2], axis=1) feature = tf.nn.dropout(feature, keep_prob) W3 = utils.get_weights_variable(4096, 4096, name='W3') b3 = utils.get_bias_variable(4096, name='b3') matmul3 = tf.nn.bias_add(tf.matmul(feature, W3), b3) matmul3 = tf.nn.relu(matmul3) W5 = utils.get_weights_variable(4096, CLASS_COUNTS, name='W5') b5 = utils.get_bias_variable(CLASS_COUNTS, name='b5') matmul5 = tf.nn.bias_add(tf.matmul(matmul3, W5), b5) return matmul5
def all_classifier(feature1, feature2, keep_prob): with tf.name_scope('all_classifier'): W1 = utils.get_weights_variable(4096, 3072, name='W1') b1 = utils.get_bias_variable(3072, name='b1') matmul1 = tf.nn.bias_add(tf.matmul(feature1, W1), b1) matmul1 = tf.nn.relu(matmul1) W2 = utils.get_weights_variable(4096, 1024, name='W2') b2 = utils.get_bias_variable(1024, name='b2') matmul2 = tf.nn.bias_add(tf.matmul(feature2, W2), b2) matmul2 = tf.nn.relu(matmul2) feature = tf.concat([matmul1, matmul2], axis=1) feature = tf.nn.dropout(feature, keep_prob) W3 = utils.get_weights_variable(4096, 3072, name='W3') b3 = utils.get_bias_variable(3072, name='b3') matmul3 = tf.nn.bias_add(tf.matmul(feature, W3), b3) matmul3 = tf.nn.relu(matmul3) matmul3 = tf.concat([matmul3, matmul2], axis=1) matmul3 = tf.nn.dropout(matmul3, keep_prob) W4 = utils.get_weights_variable(4096, CLASS_COUNTS, name='W4') b4 = utils.get_bias_variable(CLASS_COUNTS, name='b4') matmul4 = tf.nn.bias_add(tf.matmul(matmul3, W4), b4) return matmul4
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 classifier1(feature, keep_prob): with tf.name_scope('classifier1'): W1 = utils.get_weights_variable(4096, 1024, name='W1') b1 = utils.get_bias_variable(1024, name='b1') matmul1 = tf.nn.relu(tf.nn.bias_add(tf.matmul(feature, W1), b1)) matmul1 = tf.nn.dropout(matmul1, keep_prob) W2 = utils.get_weights_variable(1024, CLASS_COUNTS, name='W2') b2 = utils.get_bias_variable(CLASS_COUNTS, name='b2') matmul2 = tf.nn.bias_add(tf.matmul(matmul1, W2), b2) return matmul2
def Domain1(features, grad_scale, keep_prob): with tf.name_scope('Domain1'): features = flip_gradient(features, grad_scale) W1 = utils.get_weights_variable(4096, 1024, name='W1') b1 = utils.get_bias_variable(1024, name='b1') matmul1 = tf.nn.relu(tf.nn.bias_add(tf.matmul(features, W1), b1)) matmul1 = tf.nn.dropout(matmul1, keep_prob) W2 = utils.get_weights_variable(1024, 2, name='W2') b2 = utils.get_bias_variable(2, name='b2') matmul2 = tf.nn.bias_add(tf.matmul(matmul1, W2), b2) return matmul2
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
x_image = tf.reshape(x, [-1, img_size, img_size, num_channels]) y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true') y_true_cls = tf.argmax(y_true, dimension=1) x_pretty = pt.wrap(x_image) with pt.defaults_scope(activation_fn=tf.nn.relu): y_pred, loss = x_pretty.\ conv2d(kernel=5, depth=16, name='layer_conv1').\ max_pool(kernel=2, stride=2).\ conv2d(kernel=5, depth=36, name='layer_conv2').\ max_pool(kernel=2, stride=2).\ flatten().\ fully_connected(size=128, name='layer_fc1').\ softmax_classifier(num_classes=num_classes, labels=y_true) weights_conv1 = utils.get_weights_variable(layer_name='layer_conv1') weights_conv2 = utils.get_weights_variable(layer_name='layer_conv2') optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(loss) y_pred_cls = tf.argmax(y_pred, dimension=1) correct_prediction = tf.equal(y_pred_cls, y_true_cls) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) session = tf.Session() session.run(tf.global_variables_initializer()) utils.print_test_accuracy(session=session, x=x, y_true=y_true, y_pred_cls=y_pred_cls, show_example_errors=False,