<jupyter_start><jupyter_text>Setup env<jupyter_code>import torch
import torch.nn as nn
from torchvision import transforms
from torch.utils.data import DataLoader, TensorDataset, Dataset
from torch.utils.data.sampler import SubsetRandomSampler
from torch import optim
import pandas as pd
import sys


import time
import numpy as np
from concurrent import futures
import logging
import grpc
import argparse
import base64
import io
import matplotlib.pyplot as plt 
from tqdm import tqdm
def fullmodel2base64(model):
    buffer = io.BytesIO()
    torch.save(model, buffer)
    bg = buffer.getvalue()
    return base64.b64encode(bg).decode()

def base642fullmodel(modbase64):
    inputrpc = bytes(modbase64.encode())
    inputrpc_ = base64.b64decode(inputrpc)
    loadmodel = torch.load(io.BytesIO(inputrpc_))
    return loadmodel
class MNISTDataset(Dataset):
    """MNIST dataset"""
    
    def __init__(self, feature, target=None, transform=None):
        
        self.X = feature
        self.y = target
            
        self.transform = transform
    
    def __len__(self):
        return len(self.X)

    def __getitem__(self, idx):
        # training
        if self.transform is not None:
            return self.transform(self.X[idx]), self.y[idx]
        # testing
        elif self.y is None:
            return [self.X[idx]]
        # validation
        return self.X[idx], self.y[idx]
class Model(nn.Module):
    def __init__(self):
        super().__init__()
        
        self.cnn = nn.Sequential(nn.Conv2d(in_channels=1, out_channels=32, kernel_size=5),
                                     nn.ReLU(inplace=True),
                                     nn.Conv2d(in_channels=32, out_channels=32, kernel_size=5),
                                     nn.ReLU(inplace=True),
                                     nn.MaxPool2d(kernel_size=2),
                                     nn.Dropout(0.25),
                                     nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3),
                                     nn.ReLU(inplace=True),
                                     nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3),
                                     nn.ReLU(inplace=True),
                                     nn.MaxPool2d(kernel_size=2, stride=2),
                                     nn.Dropout(0.25))
        
        self.classifier = nn.Sequential(nn.Linear(576, 256),
                                       nn.Dropout(0.5),
                                       nn.Linear(256, 10))

        
    def forward(self, x):
        x = self.cnn(x)
        x = x.view(x.size(0), -1) # flatten layer
        x = self.classifier(x)
        
        return x
def getdataloader(dset = './mnist_test.csv'):
    #print(dset)
    train = pd.read_csv(dset)

    train_labels = train['label'].values
    train_data = train.drop(labels = ['label'], axis = 1)
    train_data = train_data.values.reshape(-1,28, 28)
    
    featuresTrain = torch.from_numpy(train_data)
    targetsTrain = torch.from_numpy(train_labels)
    
    data_transform = transforms.Compose([
    transforms.ToPILImage(),
    transforms.RandomAffine(degrees=45, translate=(0.1, 0.1), scale=(0.8, 1.2)),
    transforms.ToTensor()])

    train_set = MNISTDataset(featuresTrain.float(), targetsTrain, transform=data_transform)
    
    trainloader = torch.utils.data.DataLoader(train_set, batch_size = 256, shuffle = True)
    return trainloader
def read_bcfl_model(txtpath,inipfs=False):
    file = open(txtpath,'r')
    context = file.readline()
    file.close()
    lcontext = context.split(',')
    models = []
    if not inipfs:
        for i in lcontext:
            models.append(base642fullmodel(i))
    else:
        print("Download...")
        import ipfshttpclient
        client = ipfshttpclient.connect("/ip4/172.16.58.3/tcp/5001/")
        for i in lcontext:
            m = client.cat(i).decode()
            models.append(base642fullmodel(m))
    return models
def acc_plot(models, dataloder, device="cpu"):
    
    accd = []
    for m in tqdm(models):
        ans = np.array([])
        res = np.array([])
        model = m.to(device)
        
        model.eval()
        for data, target in dataloder:
            #data = data.view(data.size(0),-1)

            output = model(data.float().to(device))

            _, preds_tensor = torch.max(output, 1)
            preds = np.squeeze(preds_tensor.cpu().numpy())
            ans = np.append(ans, np.array(target))
            res = np.append(res, np.array(preds))

        #getit = 0
        #for i in range(len(ans)):
        #    if ans[i]==res[i]:
        #        getit+=1
        #acc = getit/len(ans)
        acc = (ans==res).sum()/len(ans)
        accd.append(acc)
        
    return accd
# Create first base-model
# fullmodel2base64(Model())<jupyter_output><empty_output><jupyter_text>BCFL result<jupyter_code>dev=torch.device("cuda")
test_dloader = getdataloader('./mnist_test.csv')

bcfl_acc = acc_plot(read_bcfl_model('./new/100_round_model_id_0.txt',inipfs=True), test_dloader, device=dev)
plt.grid(True)
plt.ylabel("Accuracy")
plt.xlabel("Round")
plt.plot(bcfl_acc)<jupyter_output><empty_output><jupyter_text>Local training<jupyter_code>dev=torch.device("cuda")
dloader = getdataloader('./mnist_train.csv')
test_dloader = getdataloader('./mnist_test.csv')
model = Model()

optimizer = optim.RMSprop(model.parameters(), lr=0.001)

loss_function = nn.CrossEntropyLoss()
model.cuda()
model.train()

models = []

for i in tqdm(range(100)):
    #print("E : ", i)
    running_loss = 0
    for data, target in dloader:
        
        data = data.cuda()
        target = target.cuda()
        optimizer.zero_grad()
        #data = data.view(data.size(0),-1)

        output = model(data.float())

        loss = loss_function(output, target)

        loss.backward()

        optimizer.step()
        
        running_loss += loss.item()
    models.append(model)
    #print("loss: ",running_loss/len(dloader))
local_acc = acc_plot(models, test_dloader,device=dev)
plt.grid(True)
plt.ylabel("Accuracy")
plt.xlabel("Round")
plt.plot(local_acc)<jupyter_output><empty_output><jupyter_text>Compare<jupyter_code>plt.grid(True)
plt.ylabel("Accuracy")
plt.xlabel("Round")
#plt.plot(range(10), bcfl_acc[:10], range(10), local_acc[:10])
plt.plot(range(100), bcfl_acc[:100], color='red', label='BCFL')
plt.plot(range(100), local_acc[:100], color='green', label='LOCAL')
plt.legend()

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12892307692307692, 'fraction_numerical': 0.022923076923076924, 'mean_word_length': 2.3912363067292643, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '328908', 'license': 'Apache-2.0', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2124, 'n_words': 379, 'path': 'evaluation/evaluate1.ipynb', 'repo_name': 'Intelligent-Systems-Lab/ISL-BCFL', 'size': '6504'}