Skip to content

Commit

Permalink
fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Anas committed May 31, 2024
1 parent 259ce86 commit df0b49b
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 135 deletions.
Binary file modified data/utils/__pycache__/datasets.cpython-311.pyc
Binary file not shown.
165 changes: 70 additions & 95 deletions data/utils/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,61 +693,40 @@ def C2FPL_client(train_data,args, client_partition,client_video_num_partition,
new_repr = []
n_num = 0
a_num = 0
# for i in range(len(client_partition['train'])):
# if client_video_num_partition['train'][i] > 809:
# n_num += 1
# else:
# a_num += 1
# new_repr.append(train_data[client_partition['train'][i]])

for i in range(len(client_partition['train'])):
if client_video_num_partition['train'][i] > 809:
n_num += 1
else:
a_num += 1
new_repr.append(train_data[client_partition['train'][i]])
if load == 0:

# for i in range(len(client_partition['train'])):
# new_repr.append(train_data[client_partition['train'][i]])
if load == 0:
print("Creating Clusters for Client ", client_id)
# break
# print(f"Client {client_id} has {n_num} normal videon and {a_num} abnormal videos \n Total number of videos of {n_num + a_num}")

# params = []
# top_k = 30
# for i in range(len(new_repr)): # entropy and var diff
# entropy = 0

# param_1 = get_matrix(new_repr[i]) # l2 norm
# mu, var = estimate_gauss(param_1) # mean and variance of l2 norm

# l2_diff = np.diff(param_1, n=1) # max diff
# var_diff = np.var(l2_diff) # variance of max diff
# max_diff = np.max(np.diff(param_1, n=1))
# param_2 = covariance_mat(new_repr[i])
# param_2 = np.where(param_2 == 0, 0.000000001,param_2)
# for i in np.diagonal(param_2):
# entropy += -(i * np.log(i))


# params.append((max_diff, entropy))

params = []
top_k = 30
for i in range(len(new_repr)): # entropy and var diff
entropy = 0

# params = []
# top_k = 15
# for i in range(len(new_repr)):
# entropy = 0
param_1 = get_matrix(new_repr[i]) # l2 norm
mu, var = estimate_gauss(param_1) # mean and variance of l2 norm

# param_1 = get_matrix(new_repr[i]) # l2 norm
# mu, var = estimate_gauss(param_1) # mean and variance of l2 norm
l2_diff = np.diff(param_1, n=1) # max diff
var_diff = np.var(l2_diff) # variance of max diff
max_diff = np.max(np.diff(param_1, n=1))
param_2 = covariance_mat(new_repr[i])
param_2 = np.where(param_2 == 0, 0.000000001,param_2)
for i in np.diagonal(param_2):
entropy += -(i * np.log(i))

# l2_diff = np.diff(param_1, n=1) # max diff
# var_diff = np.var(l2_diff) # variance of max diff

# param_2 = covariance_mat(new_repr[i]) # covariance matrix
# cov_var_sum = np.sum(np.diag(param_2)) # sum of diagonal elements
# param_2 = np.where(param_2 == 0, 0.000000001,param_2)
# # param_2 = param_2[~np.isnan(param_2)]
# s= np.linalg.eigvals(param_2) # singular values
# for i in s[:top_k]:
# entropy += (i * np.log(i)) # entropy of covariance matrix
params.append((max_diff, entropy))


# params.append((var_diff, entropy))

# params = []
# top_k = 30
Expand All @@ -758,23 +737,20 @@ def C2FPL_client(train_data,args, client_partition,client_video_num_partition,

# params.append(np.diagonal(param_2[:top_k, :top_k]))

# params = []

params = []
# for i in range(len(new_repr)):

for i in range(len(new_repr)):
# param = get_matrix(new_repr[i])
# mu, var = estimate_gauss(param)

param = get_matrix(new_repr[i])
mu, var = estimate_gauss(param)

# params.append((mu,var,))

params.append((mu,var,))




# from sklearn.decomposition import PCA
# pca = PCA(n_components=2)
# sample = pca.fit(params).transform(params)



Expand All @@ -789,9 +765,7 @@ def C2FPL_client(train_data,args, client_partition,client_video_num_partition,

import matplotlib.pyplot as plt

# plt.scatter(sample[:, 0], sample[:, 1], c= labels)
# plt.title(f"Client {client_id} Clusters")
# wandb.log({f"Client {client_id} Clusters": wandb.Image(plt)})



import pandas as pd
Expand Down Expand Up @@ -855,6 +829,7 @@ def C2FPL_client(train_data,args, client_partition,client_video_num_partition,




else:
print("Loading Clusters")
# all_abnormal = {}
Expand Down Expand Up @@ -882,41 +857,41 @@ def C2FPL_client(train_data,args, client_partition,client_video_num_partition,



# n = 0
# a = 0
# for i in range(len(new_repr)):
n = 0
a = 0
for i in range(len(new_repr)):

# if client_video_num_partition['train'][i] in normal_set.keys():
# if client_video_num_partition['train'][i] > 809:
# # plt.scatter(params[i][0], params[i][1], c= "blue", marker="o")
# n += 1
if client_video_num_partition['train'][i] in normal_set.keys():
if client_video_num_partition['train'][i] > 809:
# plt.scatter(params[i][0], params[i][1], c= "blue", marker="o")
n += 1

# # else:
# # # plt.scatter(params[i][0], params[i][1], c= "red", marker="o")
# else:
# if client_video_num_partition['train'][i] <= 809:
# # plt.scatter(params[i][0], params[i][1], c= "blue", marker="x")
# a += 1
# else:
# # plt.scatter(params[i][0], params[i][1], c= "red", marker="x")
# # plt.scatter(params[i][0], params[i][1], c= "red", marker="o")
else:
if client_video_num_partition['train'][i] <= 809:
# plt.scatter(params[i][0], params[i][1], c= "blue", marker="x")
a += 1
# else:
# plt.scatter(params[i][0], params[i][1], c= "red", marker="x")

# plt.title(f"Client {client_id} Clusters")
# wandb.log({f"Client {client_id} Clusters": wandb.Image(plt)})




# df.loc[client_id, 'num_of_GT_normal'] = n_num
# df.loc[client_id , 'num_of_GT_abnormal'] = a_num
# df.loc[client_id, 'num_of_P_normal'] = len(normal_set.keys())
# df.loc[client_id , 'num_of_P_abnormal'] = len(abnormal_set.keys())
# df.loc[client_id , 'Total_vids'] = n_num + a_num
# df.loc[client_id, 'correct_normal'] = n
# df.loc[client_id , 'correct_abnormal'] = a
# df.loc[client_id, 'normal_acc %'] = n/len(normal_set.keys())
# df.loc[client_id, 'abnormal_acc %'] = a/len(abnormal_set.keys())
df.loc[client_id, 'num_of_GT_normal'] = n_num
df.loc[client_id , 'num_of_GT_abnormal'] = a_num
df.loc[client_id, 'num_of_P_normal'] = len(normal_set.keys())
df.loc[client_id , 'num_of_P_abnormal'] = len(abnormal_set.keys())
df.loc[client_id , 'Total_vids'] = n_num + a_num
df.loc[client_id, 'correct_normal'] = n
df.loc[client_id , 'correct_abnormal'] = a
df.loc[client_id, 'normal_acc %'] = n/len(normal_set.keys())
df.loc[client_id, 'abnormal_acc %'] = a/len(abnormal_set.keys())

# print(df)
print(df)

# wandb.log({f"Client {client_id} Clustering ACC": wandb.Table(dataframe=df)})

Expand Down Expand Up @@ -990,34 +965,34 @@ def C2FPL_client_eta(train_data,args, client_partition,client_video_num_partitio
if load == 0:


# params = []
# top_k = 30
# for i in range(len(new_repr)):
# entropy = 0
params = []
top_k = 30
for i in range(len(new_repr)):
entropy = 0

# param_1 = get_matrix(new_repr[i])
# mu, var = estimate_gauss(param_1)
# max_diff = np.max(np.diff(param_1, n=1))
# param_2 = covariance_mat(new_repr[i])
# param_2 = np.where(param_2 == 0, 0.000000001,param_2)
# for i in np.diagonal(param_2):
# entropy += -(i * np.log(i))
param_1 = get_matrix(new_repr[i])
mu, var = estimate_gauss(param_1)
max_diff = np.max(np.diff(param_1, n=1))
param_2 = covariance_mat(new_repr[i])
param_2 = np.where(param_2 == 0, 0.000000001,param_2)
for i in np.diagonal(param_2):
entropy += -(i * np.log(i))


# params.append((max_diff, entropy))
params.append((max_diff, entropy))




params = []
# params = []

for i in range(len(new_repr)):
# for i in range(len(new_repr)):

param = get_matrix(new_repr[i])
mu, var = estimate_gauss(param)
# param = get_matrix(new_repr[i])
# mu, var = estimate_gauss(param)


params.append((mu,var,))
# params.append((mu,var,))



Expand Down
27 changes: 25 additions & 2 deletions out/FedAvg/ucf_log.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'seed': 30,
'mu': 1.0,
'join_ratio': 1.0,
'global_epoch': 5,
'global_epoch': 20,
'local_epoch': 1,
'finetune_epoch': 0,
'test_gap': 1,
Expand All @@ -33,8 +33,31 @@
'train_mode': 'US',
'gmm_pl': 1,
'eta_clustering': 0,
'load': 0,
'load': 1,
'refine': 0,
'ws_percentage': 1,
'dataset_args': {'dataset': 'ucf', 'client_num': 11, 'seed': 42, 'split': 'sample'}
}
==================== FedAvg TEST RESULTS: ====================
{
1: {'AUC': '0.6845 -> 0.0000'},
2: {'AUC': '0.7152 -> 0.0000'},
3: {'AUC': '0.7280 -> 0.0000'},
4: {'AUC': '0.7327 -> 0.0000'},
5: {'AUC': '0.7351 -> 0.0000'},
6: {'AUC': '0.7359 -> 0.0000'},
7: {'AUC': '0.7365 -> 0.0000'},
8: {'AUC': '0.7356 -> 0.0000'},
9: {'AUC': '0.7355 -> 0.0000'},
10: {'AUC': '0.7341 -> 0.0000'},
11: {'AUC': '0.7314 -> 0.0000'},
12: {'AUC': '0.7297 -> 0.0000'},
13: {'AUC': '0.7274 -> 0.0000'},
14: {'AUC': '0.7258 -> 0.0000'},
15: {'AUC': '0.7222 -> 0.0000'},
16: {'AUC': '0.7211 -> 0.0000'},
17: {'AUC': '0.7187 -> 0.0000'},
18: {'AUC': '0.7172 -> 0.0000'},
19: {'AUC': '0.7152 -> 0.0000'},
20: {'AUC': '0.7131 -> 0.0000'}
}
Binary file modified src/client/__pycache__/fedavg.cpython-311.pyc
Binary file not shown.
Loading

0 comments on commit df0b49b

Please sign in to comment.