mirror of
https://github.com/data61/MP-SPDZ.git
synced 2026-01-10 14:08:09 -05:00
Maintenance.
This commit is contained in:
Submodule Programs/Circuits updated: 908452826c...cdd5927692
@@ -24,8 +24,10 @@ decision_tree.max_leaves = 2000
|
||||
if 'nearest' in program.args:
|
||||
sfix.round_nearest = True
|
||||
|
||||
layers = decision_tree.TreeTrainer(
|
||||
train[1], train[0], n_levels, binary=binary, n_threads=n_threads).train()
|
||||
trainer = decision_tree.TreeTrainer(
|
||||
train[1], train[0], n_levels, binary=binary, n_threads=n_threads)
|
||||
trainer.time = 'time' in program.args
|
||||
layers = trainer.train()
|
||||
|
||||
#decision_tree.output_decision_tree(layers)
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ model = tf.keras.models.Sequential(AlexNet)
|
||||
|
||||
model.compile_by_args(program)
|
||||
|
||||
model.build(training_samples.sizes)
|
||||
model.build(training_samples.sizes, program=program)
|
||||
model.summary()
|
||||
|
||||
opt = model.fit(
|
||||
|
||||
@@ -38,9 +38,9 @@ from random import randint
|
||||
import sys
|
||||
program.bit_length = 128
|
||||
|
||||
n_parallel = int(sys.argv[2])
|
||||
n_total = int(sys.argv[3])
|
||||
nmessages = int(sys.argv[4])
|
||||
n_parallel = int(program.args[1])
|
||||
n_total = int(program.args[2])
|
||||
nmessages = int(program.args[3])
|
||||
|
||||
use_mimc_prf = True
|
||||
# Use just one PRF
|
||||
|
||||
@@ -36,6 +36,7 @@ model.build(test_samples.sizes)
|
||||
start = 0
|
||||
for var in model.trainable_variables:
|
||||
var.assign_all(0)
|
||||
# activate to use the model output by keras_mnist_lenet
|
||||
# start = var.read_from_file(start)
|
||||
|
||||
guesses = model.predict(test_samples)
|
||||
|
||||
@@ -82,22 +82,8 @@ def _(i):
|
||||
sgd.run(batch_size)
|
||||
stop_timer(1)
|
||||
|
||||
def get_correct(Y, n):
|
||||
n_correct = regint(0)
|
||||
for i in range(n):
|
||||
n_correct += (Y[i].reveal() > 0).bit_xor(
|
||||
layers[-2].Y[i][0][0][0].reveal() < 0)
|
||||
return n_correct
|
||||
|
||||
sgd.forward(N)
|
||||
|
||||
n_correct = get_correct(layers[-1].Y, N)
|
||||
n_correct, loss = sgd.reveal_correctness(layers[0].X, layers[-1].Y)
|
||||
print_ln('train_acc: %s (%s/%s)', cfix(n_correct) / N, n_correct, N)
|
||||
|
||||
training_address = layers[0].X.address
|
||||
layers[0].X.address = X.address
|
||||
sgd.forward(n_test)
|
||||
layers[0].X.address = training_address
|
||||
|
||||
n_correct = get_correct(Y, n_test)
|
||||
n_correct, loss = sgd.reveal_correctness(X, Y)
|
||||
print_ln('acc: %s (%s/%s)', cfix(n_correct) / n_test, n_correct, n_test)
|
||||
|
||||
11
Programs/Source/personal_client_example.py
Normal file
11
Programs/Source/personal_client_example.py
Normal file
@@ -0,0 +1,11 @@
|
||||
listen_for_clients(15000)
|
||||
socket = accept_client_connection(15000)
|
||||
|
||||
n = 1000
|
||||
|
||||
for i in range(2):
|
||||
x = personal.read_fix_from_socket(i, socket, n)
|
||||
sfix(x).write_fully_to_socket(socket)
|
||||
|
||||
res = sum(sfix.read_from_socket(socket, n))
|
||||
print_ln('%s', res.reveal())
|
||||
@@ -2,7 +2,7 @@ from Compiler import instructions_base
|
||||
import sys
|
||||
|
||||
program.bit_length = 128
|
||||
nparallel = int(sys.argv[2])
|
||||
nparallel = int(program.args[1])
|
||||
|
||||
instructions_base.set_global_vector_size(nparallel)
|
||||
use_cubes = True
|
||||
|
||||
53
Programs/Source/torch_densenet.py
Normal file
53
Programs/Source/torch_densenet.py
Normal file
@@ -0,0 +1,53 @@
|
||||
# this tests the pretrained DenseNet in secure computation
|
||||
|
||||
program.options_from_args()
|
||||
sfix.set_precision_from_args(program, adapt_ring=True)
|
||||
MultiArray.disable_index_checks()
|
||||
Array.check_indices = False
|
||||
|
||||
from Compiler import ml
|
||||
|
||||
try:
|
||||
ml.set_n_threads(int(program.args[2]))
|
||||
except:
|
||||
pass
|
||||
|
||||
import torchvision
|
||||
import torch
|
||||
import numpy
|
||||
import requests
|
||||
import io
|
||||
import PIL
|
||||
|
||||
from torchvision import transforms
|
||||
|
||||
model = getattr(torchvision.models.densenet, 'densenet' + program.args[1])(
|
||||
weights='DEFAULT')
|
||||
|
||||
r = requests.get('https://github.com/pytorch/hub/raw/master/images/dog.jpg')
|
||||
input_image = PIL.Image.open(io.BytesIO(r.content))
|
||||
preprocess = transforms.Compose([
|
||||
transforms.Resize(256),
|
||||
transforms.CenterCrop(224),
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
||||
])
|
||||
input_tensor = preprocess(input_image)
|
||||
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
||||
|
||||
with torch.no_grad():
|
||||
output = int(model(input_batch).argmax())
|
||||
print('Model says %d' % output)
|
||||
|
||||
secret_input = sfix.input_tensor_via(
|
||||
0, numpy.moveaxis(input_batch.numpy(), 1, -1))
|
||||
|
||||
layers = ml.layers_from_torch(
|
||||
model, secret_input.shape, 1, input_via=0,
|
||||
layer_args={model.features.conv0: {'weight_type': sfix.get_prec_type(32)}})
|
||||
|
||||
optimizer = ml.Optimizer(layers)
|
||||
optimizer.output_stats = 'output_stats' in program.args
|
||||
|
||||
print_ln('Secure computation says %s',
|
||||
optimizer.eval(secret_input, top=True)[0].reveal())
|
||||
@@ -40,7 +40,7 @@ from Compiler import ml
|
||||
|
||||
ml.set_n_threads(int(program.args[2]))
|
||||
|
||||
layers = ml.layers_from_torch(net, data[0][1].shape, 128)
|
||||
layers = ml.layers_from_torch(net, data[0][1].shape, 128, program=program)
|
||||
layers[0].X = data[0][1]
|
||||
layers[-1].Y = data[0][0]
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ for train in True, False:
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
|
||||
net = nn.Sequential(
|
||||
layers = [
|
||||
nn.Conv2d(1, 20, 5),
|
||||
nn.ReLU(),
|
||||
nn.MaxPool2d(2),
|
||||
@@ -29,7 +29,12 @@ net = nn.Sequential(
|
||||
nn.Linear(800, 500),
|
||||
nn.ReLU(),
|
||||
nn.Linear(500, 10)
|
||||
)
|
||||
]
|
||||
|
||||
if 'bn' in program.args:
|
||||
layers.insert(3, nn.BatchNorm2d(20))
|
||||
|
||||
net = nn.Sequential(*layers)
|
||||
|
||||
# train for a bit
|
||||
transform = torchvision.transforms.Compose(
|
||||
|
||||
47
Programs/Source/torch_resnet.py
Normal file
47
Programs/Source/torch_resnet.py
Normal file
@@ -0,0 +1,47 @@
|
||||
# this tests the pretrained ResNet in secure computation
|
||||
|
||||
program.options_from_args()
|
||||
|
||||
from Compiler import ml
|
||||
|
||||
try:
|
||||
ml.set_n_threads(int(program.args[2]))
|
||||
except:
|
||||
pass
|
||||
|
||||
import torchvision
|
||||
import torch
|
||||
import numpy
|
||||
import requests
|
||||
import io
|
||||
import PIL
|
||||
|
||||
from torchvision import transforms
|
||||
|
||||
model = getattr(torchvision.models.resnet, 'resnet' + program.args[1])(
|
||||
weights='DEFAULT')
|
||||
|
||||
r = requests.get('https://github.com/pytorch/hub/raw/master/images/dog.jpg')
|
||||
input_image = PIL.Image.open(io.BytesIO(r.content))
|
||||
preprocess = transforms.Compose([
|
||||
transforms.Resize(256),
|
||||
transforms.CenterCrop(224),
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
||||
])
|
||||
input_tensor = preprocess(input_image)
|
||||
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
||||
|
||||
with torch.no_grad():
|
||||
output = int(model(input_batch).argmax())
|
||||
print('Model says %d' % output)
|
||||
|
||||
secret_input = sfix.input_tensor_via(
|
||||
0, numpy.moveaxis(input_batch.numpy(), 1, -1))
|
||||
|
||||
layers = ml.layers_from_torch(model, secret_input.shape, 1, input_via=0)
|
||||
|
||||
optimizer = ml.Optimizer(layers)
|
||||
|
||||
print_ln('Secure computation says %s',
|
||||
optimizer.eval(secret_input, top=True)[0].reveal())
|
||||
53
Programs/Source/torch_squeeze.py
Normal file
53
Programs/Source/torch_squeeze.py
Normal file
@@ -0,0 +1,53 @@
|
||||
# this tests the pretrained SqueezeNet in secure computation
|
||||
|
||||
program.options_from_args()
|
||||
|
||||
from Compiler import ml
|
||||
|
||||
try:
|
||||
ml.set_n_threads(int(program.args[1]))
|
||||
except:
|
||||
pass
|
||||
|
||||
import torchvision
|
||||
import torch
|
||||
import numpy
|
||||
import requests
|
||||
import io
|
||||
import PIL
|
||||
|
||||
from torchvision import transforms
|
||||
|
||||
model = torchvision.models.get_model('SqueezeNet1_1', weights='DEFAULT')
|
||||
|
||||
r = requests.get('https://github.com/pytorch/hub/raw/master/images/dog.jpg')
|
||||
input_image = PIL.Image.open(io.BytesIO(r.content))
|
||||
preprocess = transforms.Compose([
|
||||
transforms.Resize(256),
|
||||
transforms.CenterCrop(224),
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
||||
])
|
||||
input_tensor = preprocess(input_image)
|
||||
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
||||
|
||||
with torch.no_grad():
|
||||
output = int(model(input_batch).argmax())
|
||||
print('Model says %d' % output)
|
||||
|
||||
secret_input = sfix.input_tensor_via(
|
||||
0, numpy.moveaxis(input_batch.numpy(), 1, -1))
|
||||
|
||||
layer_args = {}
|
||||
if 'first_conv_high' in program.args:
|
||||
layer_args[getattr(model.features, '0')] = \
|
||||
{'weight_type': sfix.get_prec_type(32)}
|
||||
|
||||
layers = ml.layers_from_torch(model, secret_input.shape, 1, input_via=0,
|
||||
layer_args=layer_args)
|
||||
|
||||
optimizer = ml.Optimizer(layers)
|
||||
optimizer.output_stats = 'output_stats' in program.args
|
||||
|
||||
print_ln('Secure computation says %s',
|
||||
optimizer.eval(secret_input, top=True)[0].reveal())
|
||||
Reference in New Issue
Block a user