Ejemplo n.º 1
0
def main(FLAGS):
    (x_train, y_train, x_test, y_test) = mnist_util.load_mnist_data()

    x = Input(shape=(
        28,
        28,
        1,
    ), name="input")

    y = model.cryptonets_model(x)
    cryptonets_model = Model(inputs=x, outputs=y)
    print(cryptonets_model.summary())

    def loss(labels, logits):
        return keras.losses.categorical_crossentropy(labels,
                                                     logits,
                                                     from_logits=True)

    optimizer = SGD(learning_rate=0.008, momentum=0.9)
    cryptonets_model.compile(optimizer=optimizer,
                             loss=loss,
                             metrics=["accuracy"])

    cryptonets_model.fit(x_train,
                         y_train,
                         epochs=FLAGS.epochs,
                         batch_size=FLAGS.batch_size,
                         validation_data=(x_test, y_test),
                         verbose=1)

    test_loss, test_acc = cryptonets_model.evaluate(x_test, y_test, verbose=1)
    print("Test accuracy:", test_acc)

    # Squash weights and save model
    weights = squash_layers(cryptonets_model,
                            tf.compat.v1.keras.backend.get_session())
    (conv1_weights, squashed_weights, fc1_weights, fc_1half_weights,
     fc2_weights) = weights[0:5]

    tf.reset_default_graph()
    sess = tf.compat.v1.Session()

    x = Input(shape=(
        28,
        28,
        1,
    ), name="input")
    y = model.cryptonets_model_squashed(x, conv1_weights, squashed_weights,
                                        fc_1half_weights, fc2_weights)
    sess.run(tf.compat.v1.global_variables_initializer())
    mnist_util.save_model(
        sess,
        ["output/BiasAdd"],
        "./models",
        "testnets_1",
    )
Ejemplo n.º 2
0
def train_model():
    (x_train, y_train, x_test, y_test) = load_mnist_data()

    x = Input(
        shape=(
            28,
            28,
            1,
        ), name="input")
    y = mnist_mlp_model(x)

    mlp_model = Model(inputs=x, outputs=y)
    print(mlp_model.summary())

    def loss(labels, logits):
        return categorical_crossentropy(labels, logits, from_logits=True)

    optimizer = Adam()
    mlp_model.compile(optimizer=optimizer, loss=loss, metrics=["accuracy"])

    t0 = time.perf_counter()
    mlp_model.fit(
        x_train,
        y_train,
        epochs=10,
        batch_size=128,
        validation_data=(x_test, y_test),
        verbose=1)

    t1 = time.perf_counter()
    cur_times['t_training'] = delta_ms(t0, t1)

    test_loss, test_acc = mlp_model.evaluate(x_test, y_test, verbose=1)
    print("\nTest accuracy:", test_acc)

    save_model(
        tf.compat.v1.keras.backend.get_session(),
        ["output/BiasAdd"],
        "./models",
        "mlp",
    )

    # If we want to have training and testing in the same run, we must clean up after training
    tf.keras.backend.clear_session
    tf.reset_default_graph()
Ejemplo n.º 3
0
def main(FLAGS):
    (x_train, y_train, x_test, y_test) = mnist_util.load_mnist_data()

    x = Input(
        shape=(
            28,
            28,
            1,
        ), name="input")
    y = model.mnist_mlp_model(x)

    mlp_model = Model(inputs=x, outputs=y)
    print(mlp_model.summary())

    def loss(labels, logits):
        return categorical_crossentropy(labels, logits, from_logits=True)

    optimizer = SGD(learning_rate=0.008, momentum=0.9)
    mlp_model.compile(optimizer=optimizer, loss=loss, metrics=["accuracy"])

    mlp_model.fit(
        x_train,
        y_train,
        epochs=FLAGS.epochs,
        batch_size=FLAGS.batch_size,
        validation_data=(x_test, y_test),
        verbose=1)

    test_loss, test_acc = mlp_model.evaluate(x_test, y_test, verbose=1)
    print("\nTest accuracy:", test_acc)

    mnist_util.save_model(
        tf.compat.v1.keras.backend.get_session(),
        ["output/BiasAdd"],
        "./models",
        "mlp",
    )
Ejemplo n.º 4
0
def train_model():
    (x_train, y_train, x_test, y_test) = load_mnist_data()

    x = Input(shape=(
        28,
        28,
        1,
    ), name="input")

    y = cryptonets_model(x)
    cryptonets_model_var = Model(inputs=x, outputs=y)
    print(cryptonets_model_var.summary())

    def loss(labels, logits):
        return categorical_crossentropy(labels, logits, from_logits=True)

    optimizer = Adam()
    cryptonets_model_var.compile(optimizer=optimizer,
                                 loss=loss,
                                 metrics=["accuracy"])

    t0 = time.perf_counter()
    cryptonets_model_var.fit(x_train,
                             y_train,
                             epochs=10,
                             batch_size=128,
                             validation_data=(x_test, y_test),
                             verbose=1)
    t1 = time.perf_counter()
    cur_times['t_training'] = delta_ms(t0, t1)

    test_loss, test_acc = cryptonets_model_var.evaluate(x_test,
                                                        y_test,
                                                        verbose=1)
    print("Test accuracy:", test_acc)

    # Squash weights and save model
    weights = squash_layers(cryptonets_model_var,
                            tf.compat.v1.keras.backend.get_session())
    (conv1_weights, squashed_weights, fc1_weights, fc2_weights, act1_weights,
     act2_weights) = weights[0:6]

    tf.reset_default_graph()
    sess = tf.compat.v1.Session()

    x = Input(shape=(
        28,
        28,
        1,
    ), name="input")
    y = cryptonets_model_squashed(x, conv1_weights, squashed_weights,
                                  fc2_weights, act1_weights, act2_weights)
    sess.run(tf.compat.v1.global_variables_initializer())
    save_model(
        sess,
        ["output/BiasAdd"],
        "./models",
        "cryptonets",
    )

    # If we want to have training and testing in the same run, we must clean up after training
    tf.keras.backend.clear_session
    tf.reset_default_graph()
Ejemplo n.º 5
0
def main(FLAGS):

    y_train_logit_file = 'old_logit_out_train.h5'  # 'acc_train_logit_out.h5' #
    y_test_logit_file = 'old_logit_out_test.h5'  #'acc_test_logit_out.h5' #
    logit_scale = FLAGS.logit_scale  #[0.1, 1, 10, 100]

    (x_train, y_train, y_train_label, x_test, y_test,
     y_test_label) = mnist_util.load_mnist_logit_data(
         y_train_logit_file=y_train_logit_file,
         y_test_logit_file=y_test_logit_file,
         logit_scale=logit_scale)

    #generate valid architectures for a given security level and fixed layer level:
    #architectures = generate_architecture(4096) TODO
    max_levels = 4
    input_size = 784
    output_size = 10

    #architectures = generate_architectures(max_levels, input_size, output_size, include_bottleneck=True)
    experiments = [50, 100, 200, 400, 800]
    for exp in experiments:
        architectures = [[("dense", exp), ("activation", "square"),
                          ("dense", 10)]]

        #[]
        # [("dense", 100), ("dense", 800), ("activation", "square"), ("dense", 10)],
        # [("dense", 800), ("dense", 800), ("activation", "square"), ("dense", 10)],
        # [("dense", 800), ("dense", 800), ("dense", 10)]
        # [("dense", 800), ("activation", "square"), ("dense", 10)],
        # [("dense", 800), ("dense", 10), ("activation", "square")]]

        layer_list = architectures[0]

        x = Input(shape=(
            28,
            28,
            1,
        ), name="input")
        y = cryptonets_model_no_conv(x, layer_list)
        cryptonets_model = Model(inputs=x, outputs=y)
        print(cryptonets_model.summary())

        optimizer = SGD(learning_rate=0.008, momentum=0.9)
        cryptonets_model.compile(optimizer=optimizer,
                                 loss='mean_squared_error',
                                 metrics=[logit_accuracy])
        #cryptonets_model.compile(
        #    optimizer=optimizer, loss=loss, metrics=["accuracy"])

        cryptonets_model.fit(x_train,
                             y_train,
                             epochs=FLAGS.epochs,
                             batch_size=FLAGS.batch_size,
                             validation_data=(x_test, y_test_label),
                             verbose=1)

        test_loss, test_acc = cryptonets_model.evaluate(
            x_test, y_test_label, verbose=1
        )  #should this be y-test? No, evaluating against y_Test_label
        print("Test accuracy:", test_acc)

        # Squash weights and save model

        weights, compressed_layer_list = squash_layers_variable(
            cryptonets_model, tf.compat.v1.keras.backend.get_session(),
            layer_list)

        tf.reset_default_graph()
        sess = tf.compat.v1.Session()

        x = Input(shape=(
            28,
            28,
            1,
        ), name="input")
        y = cryptonets_model_no_conv_squashed(x, weights,
                                              compressed_layer_list)
        sess.run(tf.compat.v1.global_variables_initializer())

        save_name = FLAGS.save_file + str(exp)

        mnist_util.save_model(
            sess,
            ["output/BiasAdd"],
            "./models",
            save_name,
        )
        tf.reset_default_graph()
        sess = tf.compat.v1.Session()
Ejemplo n.º 6
0
def main(FLAGS):

    logit_scale = FLAGS.logit_scale  #[0.1, 1, 10, 100]

    (x_train, y_train, y_train_label, x_test, y_test,
     y_test_label) = mnist_util.load_mnist_logit_data_acc(
         logit_scale=logit_scale)

    #generate valid architectures for a given security level and fixed layer level:
    #architectures = generate_architecture(4096) TODO
    max_levels = 4
    input_size = 784
    output_size = 10

    print("TESTING A LOT OF SHIT")
    print(x_train.shape, y_train.shape, y_train_label.shape, x_test.shape,
          y_test.shape, y_test_label.shape)
    print("log1", y_train[0])
    print("label", y_train_label[0])

    print(np.argmax(y_test[0:5], axis=1), np.argmax(y_test_label[0:5], axis=1))

    #architectures = generate_architectures(max_levels, input_size, output_size, include_bottleneck=True)
    architectures = [[("dense", 100), ("activation", "square"), ("dense", 10)]]

    #[]
    # [("dense", 100), ("dense", 800), ("activation", "square"), ("dense", 10)],
    # [("dense", 800), ("dense", 800), ("activation", "square"), ("dense", 10)],
    # [("dense", 800), ("dense", 800), ("dense", 10)]
    # [("dense", 800), ("activation", "square"), ("dense", 10)],
    # [("dense", 800), ("dense", 10), ("activation", "square")]]
    print("Architectures to test:")
    for i in range(len(architectures)):
        print(f"### ARCHITECTURE {i} ### ")
        print(architectures[i])

    accuracies = []
    for layer_list in architectures:
        x = Input(shape=(
            28,
            28,
            1,
        ), name="input")
        y = cryptonets_model_no_conv(x, layer_list)
        cryptonets_model = Model(inputs=x, outputs=y)
        print(cryptonets_model.summary())

        optimizer = SGD(learning_rate=0.008, momentum=0.9)
        #optimizer = Adam(learning_rate=0.001)
        cryptonets_model.compile(optimizer=optimizer,
                                 loss='mean_squared_error',
                                 metrics=[logit_accuracy])
        #cryptonets_model.compile(
        #    optimizer=optimizer, loss=loss, metrics=["accuracy"])

        cryptonets_model.fit(
            x_train,
            y_train,  #y_train_label 
            epochs=FLAGS.epochs,
            batch_size=FLAGS.batch_size,
            validation_data=(x_test, y_test_label),  #y_test?? 
            verbose=1)

        test_loss, test_acc = cryptonets_model.evaluate(
            x_test, y_test_label, verbose=1
        )  #should this be y-test? No, evaluating against y_Test_label
        print("Test accuracy:", test_acc)
        accuracies.append(test_acc)

        #reset graph
        tf.reset_default_graph()
        sess = tf.compat.v1.Session()

        best_model = np.argmax(accuracies)
        layer_list = architectures[best_model]

    print("architecture, test_acc")
    for i in range(len(architectures)):
        print(architectures[i][0:2], accuracies[i])

    print("best model", best_model, layer_list, accuracies[best_model])

    x = Input(shape=(
        28,
        28,
        1,
    ), name="input")
    y = cryptonets_model_no_conv(x, layer_list)
    cryptonets_model = Model(inputs=x, outputs=y)
    print(cryptonets_model.summary())

    optimizer = SGD(learning_rate=0.008, momentum=0.9)
    cryptonets_model.compile(optimizer=optimizer,
                             loss='mean_squared_error',
                             metrics=[logit_accuracy])
    #cryptonets_model.compile(
    #    optimizer=optimizer, loss=loss, metrics=["accuracy"])

    cryptonets_model.fit(x_train,
                         y_train,
                         epochs=FLAGS.epochs,
                         batch_size=FLAGS.batch_size,
                         validation_data=(x_test, y_test_label),
                         verbose=1)

    test_loss, test_acc = cryptonets_model.evaluate(
        x_test, y_test_label,
        verbose=1)  #should this be y-test? No, evaluating against y_Test_label
    print("Test accuracy:", test_acc)

    # Squash weights and save model

    weights, compressed_layer_list = squash_layers_variable(
        cryptonets_model, tf.compat.v1.keras.backend.get_session(), layer_list)

    tf.reset_default_graph()
    sess = tf.compat.v1.Session()

    x = Input(shape=(
        28,
        28,
        1,
    ), name="input")
    y = cryptonets_model_no_conv_squashed(x, weights, compressed_layer_list)
    sess.run(tf.compat.v1.global_variables_initializer())

    mnist_util.save_model(
        sess,
        ["output/BiasAdd"],
        "./models",
        FLAGS.save_file,
    )