FelixOtt's picture
Upload folder using huggingface_hub
29d330e verified
Raw History Blame Contribute Delete
4.39 kB
import os
import torch
from utils.create_dataset import create_data_set
from torchvision import models
import numpy as np
from utils.Dataloader import create_datasets_and_loaders
from easyfsl.methods import PrototypicalNetworks
def setup_client(station_properties,
station_number,
fed_method,
run_name,
station_train_loaders,
station_valid_loaders,
global_model_stations,
globel_model_second,
log_files,
local_model_path,
log_emb_diff,
env_list):
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
n_way=station_properties["n_way"]
n_shot = station_properties["n_shot"]
n_query =station_properties["n_query"]
n_tasks_per_epoch =station_properties["n_tasks_per_epoch"]
n_validation_tasks = station_properties["n_validation_tasks"]
n_workers = station_properties["n_workers"]
norm_vals = station_properties["norm_vals"]
train_test_spl=station_properties["train_test_spl"]
permutation = station_properties["permutatiion"][station_number]
root_dir= station_properties["root_dir"]
global_model_path = station_properties["global_model_path"]
root_folder = fed_method + run_name
root_folder = os.path.join("../Output", root_folder)
os.makedirs(root_folder, exist_ok=True)
local_model_dir = os.path.join(root_folder,"local_models")
os.makedirs(root_folder, exist_ok=True)
local_model = os.path.join(local_model_dir,f"station_{station_number}_models")
os.makedirs(local_model, exist_ok=True)
label_list = station_properties["label_list"][station_number]
env_list = env_list
file_name = f"train_data_{label_list}.csv"
print(file_name)
if not os.path.exists(file_name):
create_data_set(
root_dir=root_dir,
train_test_split_dir=train_test_spl,
label_list=permutation,
n_way=n_way,
n_shot=n_shot,
n_query=n_query,
env_no = env_list
)
else:
pass
train_loaders, valid_loaders = create_datasets_and_loaders(
root_dir,
train_test_spl,
permutation,
n_way,
n_shot,
n_query,
n_tasks_per_epoch,
n_validation_tasks,
n_workers,
env_no = env_list
)
station_train_loaders[f"station_{station_number}_train_loaders"] = train_loaders
station_valid_loaders[f"station_{station_number}_valid_loaders"] = valid_loaders
local_model = os.path.join(local_model_dir,f"station_{station_number}_models")
#loss_fn = torch.nn.CrossEntropyLoss(weight=class_weights)
loss_fn = torch.nn.CrossEntropyLoss()
LOSS_FUNCTION = loss_fn
# Define the path for the log file
print(f"loss function is -------------------------------------------------{loss_fn}")
if isinstance(loss_fn, torch.nn.Module):
print("loss_fn is an instance of torch.nn.Module.")
else:
print("loss_fn is not an instance of torch.nn.Module.")
os.makedirs(root_folder, exist_ok=True)
LOG_DIR = os.path.join(root_folder,"logs")
os.makedirs(LOG_DIR, exist_ok=True)
LOG_FILE = os.path.join(LOG_DIR, f"training_eval_logs_station_{station_number}.txt")
LOG_DIR_emb = os.path.join(LOG_DIR,"embeddings")
os.makedirs(LOG_DIR_emb, exist_ok=True)
LOG_FILE_emb = os.path.join(LOG_DIR_emb, f"embedding_loss_station_{station_number}")
#global model
model = models.resnet18(pretrained=False)
model.fc = torch.nn.Identity()
globel_model_1 = PrototypicalNetworks(model).to(DEVICE)
weights = torch.load(global_model_path,map_location=DEVICE)
globel_model_1.load_state_dict(weights)
global_model_stations[f"station_{station_number}_globalM"] = globel_model_1
##reference model
model_1_2 = models.resnet18(pretrained=False)
model_1_2.fc = torch.nn.Identity()
globel_model_1_2 = PrototypicalNetworks(model_1_2).to(DEVICE)
weights_1_2 = torch.load(global_model_path,map_location=DEVICE)
globel_model_1_2.load_state_dict(weights_1_2)
globel_model_second[f"station_{station_number}_globalsecond"] = globel_model_1_2
log_files.append(LOG_FILE)
local_model_path.append(local_model)
log_emb_diff.append(LOG_FILE_emb)