import torch.nn as nn
import crypten
import torch
from torchvision.models import resnet18
import crypten.communicator as comm
import logging
import crypten.mpc as mpc
import os
import time
crypten.init()
torch.set_num_threads(1)
logging.getLogger().setLevel(logging.INFO)
ALICE = 0
BOB = 1
def compute_accuracy(output, labels):
pred = output.argmax(1)
correct = pred.eq(labels)
correct_count = correct.sum(0, keepdim=True).float()
accuracy = correct_count.mul_(100.0 / output.size(0))
return accuracy
labels = torch.load('label-cpu.pth')
print(labels)
@mpc.run_multiprocess(world_size=2)
def encrypt_model_and_data():
# Load pre-trained model to Alice
# comm.get().set_verbosity(True)
model = crypten.load_from_party('finalmodel-cpu.pth', src=ALICE)
start_time = time.time()
# Encrypt model from Alice
# dummy_input = torch.empty((1, 3, 224, 224))
dummy_input = torch.empty((1, 256, 14, 14))
private_model = crypten.nn.from_pytorch(model, dummy_input)
private_model.encrypt(src=ALICE)
# 将模型移动到CUDA设备
private_model = private_model.to(device)
# Load data to Bob
data_enc = crypten.load_from_party('data-cpu.pth', src=BOB)
data_enc2 = data_enc[0:1]
# data_flatten = data_enc2.view(-1, 3, 224, 224)
data_flatten = data_enc2.view(-1, 256, 14, 14)
# Classify the encrypted data
private_model.eval()
output_enc = private_model(data_flatten)
# Compute the accuracy
output = output_enc.get_plain_text()
accuracy = compute_accuracy(output, labels[0:1])
end_time = time.time()
elapsed_time = end_time - start_time
crypten.print("\tTotal Time Elapsed: {0:.2f} seconds".format(elapsed_time))
crypten.print("\tAccuracy: {0:.4f}".format(accuracy.item()))
print(comm.get().get_communication_stats())
comm.get().print_communication_stats()
encrypt_model_and_data()
This is my code. When I run the communication test, I see that it's not communicating.But the program can be run normally to get the result:
INFO:root:====Communication Stats====
INFO:root:Rounds: 0
INFO:root:Bytes: 0
INFO:root:Communication time: 0
Total Time Elapsed: 4.10 seconds
Accuracy: 0.0000
{'rounds': 0, 'bytes': 0, 'time': 0}
{'rounds': 0, 'bytes': 0, 'time': 0}
INFO:root:==================
INFO:root:DistributedCommunicator with rank 0
INFO:root:==================
INFO:root:Added key: store_based_barrier_key:1 to store for rank: 0
INFO:root:Added key: store_based_barrier_key:2 to store for rank: 0
INFO:root:Added key: store_based_barrier_key:3 to store for rank: 0
INFO:root:World size = 1
import torch.nn as nn
import crypten
import torch
from torchvision.models import resnet18
import crypten.communicator as comm
import logging
import crypten.mpc as mpc
import os
import time
crypten.init()
torch.set_num_threads(1)
logging.getLogger().setLevel(logging.INFO)
ALICE = 0
BOB = 1
def compute_accuracy(output, labels):
pred = output.argmax(1)
correct = pred.eq(labels)
correct_count = correct.sum(0, keepdim=True).float()
accuracy = correct_count.mul_(100.0 / output.size(0))
return accuracy
labels = torch.load('label-cpu.pth')
print(labels)
@mpc.run_multiprocess(world_size=2)
def encrypt_model_and_data():
# Load pre-trained model to Alice
# comm.get().set_verbosity(True)
model = crypten.load_from_party('finalmodel-cpu.pth', src=ALICE)
start_time = time.time()
# Encrypt model from Alice
# dummy_input = torch.empty((1, 3, 224, 224))
dummy_input = torch.empty((1, 256, 14, 14))
private_model = crypten.nn.from_pytorch(model, dummy_input)
private_model.encrypt(src=ALICE)
encrypt_model_and_data()
This is my code. When I run the communication test, I see that it's not communicating.But the program can be run normally to get the result:
INFO:root:====Communication Stats====
INFO:root:Rounds: 0
INFO:root:Bytes: 0
INFO:root:Communication time: 0
Total Time Elapsed: 4.10 seconds
Accuracy: 0.0000
{'rounds': 0, 'bytes': 0, 'time': 0}
{'rounds': 0, 'bytes': 0, 'time': 0}
INFO:root:==================
INFO:root:DistributedCommunicator with rank 0
INFO:root:==================
INFO:root:Added key: store_based_barrier_key:1 to store for rank: 0
INFO:root:Added key: store_based_barrier_key:2 to store for rank: 0
INFO:root:Added key: store_based_barrier_key:3 to store for rank: 0
INFO:root:World size = 1