Esempio n. 1
0
def mnist_mlp_model(x):
    paddings = tf.constant([[0, 0], [0, 1], [0, 1], [0, 0]], name='pad_const')
    x = tf.pad(x, paddings)

    W_conv1 = tf.compat.v1.get_variable('W_conv1', [5, 5, 1, 5])
    y = conv2d_stride_2_valid(x, W_conv1)
    W_bc1 = tf.compat.v1.get_variable('W_conv1_bias', [1, 13, 13, 5])
    y = y + W_bc1
    y = tf.nn.relu(y)

    y = max_pool_3x3_same_size(y)
    W_conv2 = tf.compat.v1.get_variable('W_conv2', [5, 5, 5, 50])
    y = conv2d_stride_2_valid(y, W_conv2)
    y = max_pool_3x3_same_size(y)

    y = tf.reshape(y, [-1, 5 * 5 * 50])
    W_fc1 = tf.compat.v1.get_variable('W_fc1', [5 * 5 * 50, 100])
    W_b1 = tf.compat.v1.get_variable('W_fc1_bias', [100])
    y = tf.matmul(y, W_fc1)
    y = y + W_b1
    y = tf.nn.relu(y)

    W_fc2 = tf.compat.v1.get_variable('W_fc2', [100, 10])
    W_b2 = tf.compat.v1.get_variable('W_fc2_bias', [10])
    y = tf.matmul(y, W_fc2)
    y = tf.add(y, W_b2, name='output')

    return y
def cryptonets_relu_model(x, mode):
    if mode not in set(['train', 'test']):
        print('mode should be train or test')
        raise Exception()

    paddings = tf.constant([[0, 0], [0, 1], [0, 1], [0, 0]], name='pad_const')
    x = tf.pad(x, paddings)

    W_conv1 = get_variable('W_conv1', [5, 5, 1, 5], mode)
    y = conv2d_stride_2_valid(x, W_conv1)
    W_bc1 = get_variable('W_conv1_bias', [1, 13, 13, 5], mode)
    y = y + W_bc1
    y = tf.nn.relu(y)

    y = avg_pool_3x3_same_size(y)
    W_conv2 = get_variable('W_conv2', [5, 5, 5, 50], mode)
    y = conv2d_stride_2_valid(y, W_conv2)
    y = avg_pool_3x3_same_size(y)

    y = tf.reshape(y, [-1, 5 * 5 * 50])
    W_fc1 = get_variable('W_fc1', [5 * 5 * 50, 100], mode)
    W_b1 = get_variable('W_fc1_bias', [100], mode)
    y = tf.matmul(y, W_fc1)
    y = y + W_b1
    y = tf.nn.relu(y)

    W_fc2 = get_variable('W_fc2', [100, 10], mode)
    W_b2 = get_variable('W_fc2_bias', [10], mode)
    y = tf.matmul(y, W_fc2)
    y = y + W_b2
    return y
Esempio n. 3
0
def cryptonets_test_averaged_squashed(x):
    """Constructs test network for Cryptonets using saved weights.
       Assumes linear layers have been squashed."""
    paddings = [[0, 0], [0, 1], [0, 1], [0, 0]]
    x = tf.pad(x, paddings)

    W_conv1_Part1 = np.loadtxt('W_conv1_Part1.txt',dtype=np.float32).reshape([5,5,1,5])
    W_conv1_Part2 = np.loadtxt('W_conv1_Part2.txt',dtype=np.float32).reshape([5,5,1,5])
    W_conv1_Part3 = np.loadtxt('W_conv1_Part3.txt',dtype=np.float32).reshape([5,5,1,5])
    W_conv1_Part4 = np.loadtxt('W_conv1_Part4.txt',dtype=np.float32).reshape([5,5,1,5])
    W_conv1 = (W_conv1_Part1 + W_conv1_Part2 + W_conv1_Part3 + W_conv1_Part4) *0.25
	
    y = conv2d_stride_2_valid(x, W_conv1)
    y = tf.square(y)
	
    W_squash_Part1 = np.loadtxt('W_squash_Part1.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
    W_squash_Part2 = np.loadtxt('W_squash_Part2.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
    W_squash_Part3 = np.loadtxt('W_squash_Part3.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
    W_squash_Part4 = np.loadtxt('W_squash_Part4.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
    W_squash = (W_squash_Part1+W_squash_Part2+W_squash_Part3+W_squash_Part4) *0.25
    
    y = tf.reshape(y, [-1, 5 * 13 * 13])
    y = tf.matmul(y, W_squash)
    y = tf.square(y)
	
    W_fc2_Part1 = np.loadtxt('W_fc2_Part1.txt',dtype=np.float32).reshape([100, 10])
    W_fc2_Part2 = np.loadtxt('W_fc2_Part2.txt',dtype=np.float32).reshape([100, 10])
    W_fc2_Part3 = np.loadtxt('W_fc2_Part3.txt',dtype=np.float32).reshape([100, 10])
    W_fc2_Part4 = np.loadtxt('W_fc2_Part4.txt',dtype=np.float32).reshape([100, 10])
    W_fc2 = (W_fc2_Part1+W_fc2_Part2+W_fc2_Part3+W_fc2_Part4)*0.25
    y = tf.matmul(y, W_fc2)
    return y
Esempio n. 4
0
def cryptonets_relu_test_squashed(x):
    """Constructs test network for Cryptonets Relu using saved weights.
       Assumes linear layers have been squashed."""
    paddings = [[0, 0], [0, 1], [0, 1], [0, 0]]
    x = tf.pad(x, paddings)

    W_conv1 = get_variable('W_conv1', [5, 5, 1, 5], 'test')
    y = conv2d_stride_2_valid(x, W_conv1)
    W_bc1 = get_variable('W_conv1_bias', [1, 13, 13, 5], 'test')
    y = tf.nn.relu(y)

    W_squash = get_variable('W_squash', [5 * 13 * 13, 100], 'test')
    y = tf.reshape(y, [-1, 5 * 13 * 13])
    y = tf.matmul(y, W_squash)
    W_b1 = get_variable('W_fc1_bias', [100], 'test')
    y = y + W_b1

    y = tf.nn.relu(y)
    W_fc2 = get_variable('W_fc2', [100, 10], 'test')
    y = tf.matmul(y, W_fc2)

    W_b2 = get_variable('W_fc2_bias', [10], 'test')
    y = y + W_b2

    return y
Esempio n. 5
0
def cryptonets_HE_avg(x):
    paddings = [[0, 0], [0, 1], [0, 1], [0, 0]]
    x = tf.pad(x, paddings)

    W_conv1 = np.float32(np.load("conv1_avg_dec.npy",allow_pickle=True))
    y = conv2d_stride_2_valid(x, W_conv1)
    y = tf.square(y)
    
    W_squash_Part1 = np.loadtxt('W_squash_repeatedSampling_Part1.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
    W_squash_Part2 = np.loadtxt('W_squash_repeatedSampling_Part2.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
    W_squash_Part3 = np.loadtxt('W_squash_repeatedSampling_Part3.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
    W_squash_Part4 = np.loadtxt('W_squash_repeatedSampling_Part4.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
    W_squash = (W_squash_Part1+W_squash_Part2+W_squash_Part3+W_squash_Part4) *0.25
    
    y = tf.reshape(y, [-1, 5 * 13 * 13])
    y = tf.matmul(y, W_squash)
    y = tf.square(y)
    W_fc2 = np.float32(np.load("fc2_av_dec.npy",allow_pickle=True))
    y = tf.matmul(y, W_fc2)
    return y
Esempio n. 6
0
def squash_layers():
    print("Squashing layers")
    tf.compat.v1.reset_default_graph()

    # Input from h_conv1 squaring
    x = tf.compat.v1.placeholder(tf.float32, [None, 13, 13, 5])

    # Pooling layer
    h_pool1 = avg_pool_3x3_same_size(x)  # To N x 13 x 13 x 5

    # Second convolution
    W_conv2 = np.loadtxt('W_conv2.txt',
                         dtype=np.float32).reshape([5, 5, 5, 50])
    h_conv2 = conv2d_stride_2_valid(h_pool1, W_conv2)

    # Second pooling layer.
    h_pool2 = avg_pool_3x3_same_size(h_conv2)

    # Fully connected layer 1
    # Input: N x 5 x 5 x 50
    # Output: N x 100
    W_fc1 = np.loadtxt('W_fc1.txt',
                       dtype=np.float32).reshape([5 * 5 * 50, 100])
    h_pool2_flat = tf.reshape(h_pool2, [-1, 5 * 5 * 50])
    pre_square = tf.matmul(h_pool2_flat, W_fc1)

    with tf.compat.v1.Session() as sess:
        x_in = np.eye(13 * 13 * 5)
        x_in = x_in.reshape([13 * 13 * 5, 13, 13, 5])
        W = (sess.run([pre_square], feed_dict={x: x_in}))[0]
        squashed_file_name = "W_squash.txt"
        np.savetxt(squashed_file_name, W)
        print("Saved to", squashed_file_name)

        # Sanity check
        x_in = np.random.rand(100, 13, 13, 5)
        network_out = (sess.run([pre_square], feed_dict={x: x_in}))[0]
        linear_out = x_in.reshape(100, 13 * 13 * 5).dot(W)
        assert (np.max(np.abs(linear_out - network_out)) < 1e-5)

    print("Squashed layers")
Esempio n. 7
0
def cryptonets_test_squashed_mode(x,mode):
    """Constructs test network for Cryptonets using saved weights.
       Assumes linear layers have been squashed."""
    paddings = [[0, 0], [0, 1], [0, 1], [0, 0]]
    x = tf.pad(x, paddings)
    if mode==1:
        W_conv1_Part1 = np.loadtxt('W_conv1_InOrderSampling_Part1.txt',dtype=np.float32).reshape([5,5,1,5])
        W_conv1_Part2 = np.loadtxt('W_conv1_InOrderSampling_Part2.txt',dtype=np.float32).reshape([5,5,1,5])
        W_conv1_Part3 = np.loadtxt('W_conv1_InOrderSampling_Part3.txt',dtype=np.float32).reshape([5,5,1,5])
        W_conv1_Part4 = np.loadtxt('W_conv1_InOrderSampling_Part4.txt',dtype=np.float32).reshape([5,5,1,5])
        W_conv1 = (W_conv1_Part1 + W_conv1_Part2 + W_conv1_Part3 + W_conv1_Part4) *0.25  
        
        W_squash_Part1 = np.loadtxt('W_squash_InOrderSampling_Part1.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
        W_squash_Part2 = np.loadtxt('W_squash_InOrderSampling_Part2.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
        W_squash_Part3 = np.loadtxt('W_squash_InOrderSampling_Part3.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
        W_squash_Part4 = np.loadtxt('W_squash_InOrderSampling_Part4.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
        W_squash = (W_squash_Part1+W_squash_Part2+W_squash_Part3+W_squash_Part4) *0.25  

        W_fc2_Part1 = np.loadtxt('W_fc2_InOrderSampling_Part1.txt',dtype=np.float32).reshape([100, 10])
        W_fc2_Part2 = np.loadtxt('W_fc2_InOrderSampling_Part2.txt',dtype=np.float32).reshape([100, 10])
        W_fc2_Part3 = np.loadtxt('W_fc2_InOrderSampling_Part3.txt',dtype=np.float32).reshape([100, 10])
        W_fc2_Part4 = np.loadtxt('W_fc2_InOrderSampling_Part4.txt',dtype=np.float32).reshape([100, 10])
        W_fc2 = (W_fc2_Part1+W_fc2_Part2+W_fc2_Part3+W_fc2_Part4)*0.25

    elif mode==2:
        W_conv1_Part1 = np.loadtxt('W_conv1_nonRepeatedSampling_Part1.txt',dtype=np.float32).reshape([5,5,1,5])
        W_conv1_Part2 = np.loadtxt('W_conv1_nonRepeatedSampling_Part2.txt',dtype=np.float32).reshape([5,5,1,5])
        W_conv1_Part3 = np.loadtxt('W_conv1_nonRepeatedSampling_Part3.txt',dtype=np.float32).reshape([5,5,1,5])
        W_conv1_Part4 = np.loadtxt('W_conv1_nonRepeatedSampling_Part4.txt',dtype=np.float32).reshape([5,5,1,5])
        W_conv1 = (W_conv1_Part1 + W_conv1_Part2 + W_conv1_Part3 + W_conv1_Part4) *0.25  
        
        W_squash_Part1 = np.loadtxt('W_squash_nonRepeatedSampling_Part1.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
        W_squash_Part2 = np.loadtxt('W_squash_nonRepeatedSampling_Part2.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
        W_squash_Part3 = np.loadtxt('W_squash_nonRepeatedSampling_Part3.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
        W_squash_Part4 = np.loadtxt('W_squash_nonRepeatedSampling_Part4.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
        W_squash = (W_squash_Part1+W_squash_Part2+W_squash_Part3+W_squash_Part4) *0.25  

        W_fc2_Part1 = np.loadtxt('W_fc2_nonRepeatedSampling_Part1.txt',dtype=np.float32).reshape([100, 10])
        W_fc2_Part2 = np.loadtxt('W_fc2_nonRepeatedSampling_Part2.txt',dtype=np.float32).reshape([100, 10])
        W_fc2_Part3 = np.loadtxt('W_fc2_nonRepeatedSampling_Part3.txt',dtype=np.float32).reshape([100, 10])
        W_fc2_Part4 = np.loadtxt('W_fc2_nonRepeatedSampling_Part4.txt',dtype=np.float32).reshape([100, 10])
        W_fc2 = (W_fc2_Part1+W_fc2_Part2+W_fc2_Part3+W_fc2_Part4)*0.25

    else:
        W_conv1_Part1 = np.loadtxt('W_conv1_repeatedSampling_Part1.txt',dtype=np.float32).reshape([5,5,1,5])
        W_conv1_Part2 = np.loadtxt('W_conv1_repeatedSampling_Part2.txt',dtype=np.float32).reshape([5,5,1,5])
        W_conv1_Part3 = np.loadtxt('W_conv1_repeatedSampling_Part3.txt',dtype=np.float32).reshape([5,5,1,5])
        W_conv1_Part4 = np.loadtxt('W_conv1_repeatedSampling_Part4.txt',dtype=np.float32).reshape([5,5,1,5])
        W_conv1 = (W_conv1_Part1 + W_conv1_Part2 + W_conv1_Part3 + W_conv1_Part4) *0.25  
        
        W_squash_Part1 = np.loadtxt('W_squash_repeatedSampling_Part1.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
        W_squash_Part2 = np.loadtxt('W_squash_repeatedSampling_Part2.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
        W_squash_Part3 = np.loadtxt('W_squash_repeatedSampling_Part3.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
        W_squash_Part4 = np.loadtxt('W_squash_repeatedSampling_Part4.txt',dtype=np.float32).reshape([5 * 13 * 13, 100])
        W_squash = (W_squash_Part1+W_squash_Part2+W_squash_Part3+W_squash_Part4) *0.25  

        W_fc2_Part1 = np.loadtxt('W_fc2_repeatedSampling_Part1.txt',dtype=np.float32).reshape([100, 10])
        W_fc2_Part2 = np.loadtxt('W_fc2_repeatedSampling_Part2.txt',dtype=np.float32).reshape([100, 10])
        W_fc2_Part3 = np.loadtxt('W_fc2_repeatedSampling_Part3.txt',dtype=np.float32).reshape([100, 10])
        W_fc2_Part4 = np.loadtxt('W_fc2_repeatedSampling_Part4.txt',dtype=np.float32).reshape([100, 10])
        W_fc2 = (W_fc2_Part1+W_fc2_Part2+W_fc2_Part3+W_fc2_Part4)*0.25
    y = conv2d_stride_2_valid(x, W_conv1)
    y = tf.square(y)
    
    y = tf.reshape(y, [-1, 5 * 13 * 13])
    y = tf.matmul(y, W_squash)
    y = tf.square(y)
    
    y = tf.matmul(y, W_fc2)
    return y
Esempio n. 8
0
def cryptonets_model(x, mode):
    """Builds the graph for classifying digits based on Cryptonets

    Args:
        x: an input tensor with the dimensions (N_examples, 28, 28)

    Returns:
        A tuple (y, a scalar placeholder). y is a tensor of shape
        (N_examples, 10), with values equal to the logits of classifying the
        digit into one of 10 classes (the digits 0-9).
    """
    if mode not in set(['train', 'test']):
        print('mode should be train or test')
        raise Exception()

    # Reshape to use within a conv neural net.
    # Last dimension is for "features" - there is only one here, since images
    # are grayscale -- it would be 3 for an RGB image, 4 for RGBA, etc.
    # CryptoNets's output of the first conv layer has feature map size 13 x 13,
    # therefore, we manually add paddings.
    with tf.name_scope('reshape'):
        print('padding')
        paddings = [[0, 0], [0, 1], [0, 1], [0, 0]]
        x = tf.pad(x, paddings)
        print('padded')

    # First conv layer
    # Input: N x 28 x 28 x 1
    # Filter: 5 x 5 x 1 x 5
    # Output: N x 13 x 13 x 5
    with tf.name_scope('conv1'):
        W_conv1 = get_variable("W_conv1", [5, 5, 1, 5], mode)
        h_conv1 = tf.square(conv2d_stride_2_valid(x, W_conv1))

    # Pooling layer
    # Input: N x 13 x 13 x 5
    # Output: N x 13 x 13 x 5
    with tf.name_scope('pool1'):
        h_pool1 = avg_pool_3x3_same_size(h_conv1)

    # Second convolution
    # Input: N x 13 x 13 x 5
    # Filter: 5 x 5 x 5 x 50
    # Output: N x 5 x 5 x 50
    with tf.name_scope('conv2'):
        W_conv2 = get_variable("W_conv2", [5, 5, 5, 50], mode)
        h_conv2 = conv2d_stride_2_valid(h_pool1, W_conv2)

    # Second pooling layer
    # Input: N x 5 x 5 x 50
    # Output: N x 5 x 5 x 50
    with tf.name_scope('pool2'):
        h_pool2 = avg_pool_3x3_same_size(h_conv2)

    # Fully connected layer 1
    # Input: N x 5 x 5 x 50
    # Input flattened: N x 1250
    # Weight: 1250 x 100
    # Output: N x 100
    with tf.name_scope('fc1'):
        h_pool2_flat = tf.reshape(h_pool2, [-1, 5 * 5 * 50])
        W_fc1 = get_variable("W_fc1", [5 * 5 * 50, 100], mode)
        h_fc1 = tf.square(tf.matmul(h_pool2_flat, W_fc1))

    # Map the 100 features to 10 classes, one for each digit
    # Input: N x 100
    # Weight: 100 x 10
    # Output: N x 10
    with tf.name_scope('fc2'):
        W_fc2 = get_variable("W_fc2", [100, 10], mode)
        y_conv = tf.matmul(h_fc1, W_fc2)
    return y_conv
Esempio n. 9
0
def cryptonets_train(x):
    """Builds the graph for classifying digits based on Cryptonets

    Args:
        x: an input tensor with the dimensions (N_examples, 784), where 784 is
        the number of pixels in a standard MNIST image.

    Returns:
        A tuple (y, a scalar placeholder). y is a tensor of shape
        (N_examples, 10), with values equal to the logits of classifying the
        digit into one of 10 classes (the digits 0-9).
    """
    # Reshape to use within a conv neural net.
    # Last dimension is for "features" - there is only one here, since images
    # are grayscale -- it would be 3 for an RGB image, 4 for RGBA, etc.
    with tf.name_scope('reshape'):
        paddings = [[0, 0], [0, 1], [0, 1], [0, 0]]
        x = tf.pad(x, paddings)

    # First conv layer
    # CryptoNets's output of the first conv layer has feature map size 13 x 13,
    # therefore, we manually add paddings.
    # Input: N x 28 x 28 x 1
    # Filter: 5 x 5 x 1 x 5
    # Output: N x 12 x 12 x 5
    # Output after padding: N x 13 x 13 x 5
    with tf.name_scope('conv1'):
        W_conv1 = tf.get_variable("W_conv1", [5, 5, 1, 5])
        h_conv1_no_pad = tf.square(conv2d_stride_2_valid(x, W_conv1))
        paddings = tf.constant([[0, 0], [0, 1], [0, 1], [0, 0]],
                               name='pad_const')
        h_conv1 = tf.pad(h_conv1_no_pad, paddings)

    # Pooling layer
    # Input: N x 13 x 13 x 5
    # Output: N x 13 x 13 x 5
    with tf.name_scope('pool1'):
        h_pool1 = avg_pool_3x3_same_size(h_conv1)

    # Second convolution
    # Input: N x 13 x 13 x 5
    # Filter: 5 x 5 x 5 x 50
    # Output: N x 5 x 5 x 50
    with tf.name_scope('conv2'):
        W_conv2 = tf.get_variable("W_conv2", [5, 5, 5, 50])
        h_conv2 = conv2d_stride_2_valid(h_pool1, W_conv2)

    # Second pooling layer
    # Input: N x 5 x 5 x 50
    # Output: N x 5 x 5 x 50
    with tf.name_scope('pool2'):
        h_pool2 = avg_pool_3x3_same_size(h_conv2)

    # Fully connected layer 1
    # Input: N x 5 x 5 x 50
    # Input flattened: N x 1250
    # Weight: 1250 x 100
    # Output: N x 100
    with tf.name_scope('fc1'):
        h_pool2_flat = tf.reshape(h_pool2, [-1, 5 * 5 * 50])
        W_fc1 = tf.get_variable("W_fc1", [5 * 5 * 50, 100])
        h_fc1 = tf.square(tf.matmul(h_pool2_flat, W_fc1))

    # Map the 100 features to 10 classes, one for each digit
    # Input: N x 100
    # Weight: 100 x 10
    # Output: N x 10
    with tf.name_scope('fc2'):
        W_fc2 = tf.get_variable("W_fc2", [100, 10])
        y_conv = tf.matmul(h_fc1, W_fc2)
    return y_conv