Skip to content

Commit

Permalink
remove(example/cifar100) remove the annotation code and chinese annot…
Browse files Browse the repository at this point in the history
…ation

Signed-off-by: Marchons <[email protected]>
  • Loading branch information
Yoda-wu committed Oct 29, 2024
1 parent b9199de commit ce84a2e
Show file tree
Hide file tree
Showing 11 changed files with 4 additions and 53 deletions.
25 changes: 1 addition & 24 deletions examples/cifar100/fci_ssl/fed_ci_match/algorithm/FedCiMatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@


def get_one_hot(target, num_classes):
# print(f'in get one hot, target shape is {target.shape}')
y = tf.one_hot(target, depth=num_classes)
# print(f'in get one hot, y shape is {y.shape}')
if len(y.shape) == 3:
y = tf.squeeze(y, axis=1)
# print(f'in get one hot, after tf.squeeze y shape is {y.shape}')
return y


Expand Down Expand Up @@ -72,16 +69,12 @@ def build_feature_extractor(self):

feature_extractor.build(input_shape=(None, 32, 32, 3))
feature_extractor.call(keras.Input(shape=(32, 32, 3)))
# feature_extractor.load_weights(
# "examples/cifar100/fci_ssl/fed_ci_match/algorithm/feature_extractor.weights.h5"
# )
return feature_extractor

def build_classifier(self):
if self.classifier != None:
new_classifier = keras.Sequential(
[
# tf.keras.Input(shape=(None, self.feature_extractor.layers[-2].output_shape[-1])),
keras.layers.Dense(
self.num_classes, kernel_initializer="lecun_normal"
)
Expand All @@ -106,7 +99,6 @@ def build_classifier(self):
)
self.classifier = keras.Sequential(
[
# tf.keras.Input(shape=(None, self.feature_extractor.layers[-2].output_shape[-1])),
keras.layers.Dense(
self.num_classes, kernel_initializer="lecun_normal"
)
Expand Down Expand Up @@ -192,10 +184,8 @@ def get_train_loader(self):
)
if len(self.exemplar_set) != 0:
for exm_set in self.exemplar_set:
# print('in get train loader' , exm_set[0].shape)
train_x = np.concatenate((train_x, exm_set[0]), axis=0)
label = np.array(exm_set[1])
# label = label.reshape(-1, 1)
train_y = np.concatenate((train_y, label), axis=0)
logging.info(
f"train_x shape: {train_x.shape} and train_y shape: {train_y.shape}"
Expand Down Expand Up @@ -250,8 +240,6 @@ def construct_exemplar_set(self, images, class_id, m):
exemplar_data = []
exemplar_label = []
class_mean, fe_ouput = self.compute_exemplar_mean(images)
# cl_mean = tf.reduce_mean(class_mean, axis=0)
# now_class_mean = np.zeros((1, fe_ouput.shape[1]))
diff = tf.abs(fe_ouput - class_mean)
distance = [float(tf.reduce_sum(dis).numpy()) for dis in diff]

Expand All @@ -261,14 +249,7 @@ def construct_exemplar_set(self, images, class_id, m):
exemplar_data = [images[i] for i in sorted_index]
exemplar_label = [class_id] * len(exemplar_data)
self.exemplar_set.append((exemplar_data, exemplar_label))
# for i in range(m):
# x = class_mean - (now_class_mean + fe_ouput) / (i + 1)
# x = np.linalg.norm(x)
# index = np.argmin(x)
# now_class_mean += fe_ouput[index]
# exemplar_data.append(images[index])
# exemplar_label.append(class_id)
# self.exemplar_set.append((exemplar_data, exemplar_label))


def compute_exemplar_mean(self, images):
images_data = (
Expand All @@ -279,7 +260,6 @@ def compute_exemplar_mean(self, images):
fe_output = self.feature_extractor.predict(images_data)
print("fe_output shape:", fe_output.shape)
class_mean = tf.reduce_mean(fe_output, axis=0)
# class_mean = np.mean(fe_output, axis=0)
return class_mean, fe_output

def train(self, round):
Expand All @@ -291,11 +271,8 @@ def train(self, round):
)
q = []
logging.info(f"is new task: {self.is_new_task}")
# if self.classifier is not None:
# q = self.caculate_pre_update()
if self.is_new_task:
self.build_classifier()
# self.is_new_task = False
all_params = []
all_params.extend(self.feature_extractor.trainable_variables)
all_params.extend(self.classifier.trainable_variables)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def __init__(self, **kwargs) -> None:
self.batch_size = kwargs.get("batch_size", 32)
self.task_size = kwargs.get("task_size", 2)
self.memory_size = kwargs.get("memory_size", 2000)
# self.fe = self.build_feature_extractor()
self.num_classes = 50 # the number of class for the first task
self.FedCiMatch = FedCiMatch(
self.num_classes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def _build_classifier(self):
if self.classifier != None:
new_classifier = keras.Sequential(
[
# tf.keras.Input(shape=(None, self.feature_extractor.layers[-2].output_shape[-1])),
keras.layers.Dense(
len(self.class_mapping), kernel_initializer="lecun_normal"
)
Expand All @@ -100,7 +99,6 @@ def _build_classifier(self):
)
self.classifier = keras.Sequential(
[
# tf.keras.Input(shape=(None, self.feature_extractor.layers[-2].output_shape[-1])),
keras.layers.Dense(
len(self.class_mapping), kernel_initializer="lecun_normal"
)
Expand All @@ -126,7 +124,6 @@ def get_data_size(self):
def model_call(self, x, training=False):
x = self.feature_extractor(x, training=training)
x = self.classifier(x, training=training)
# x = tf.nn.softmax(x)
return x

def _build_class_mapping(self):
Expand Down Expand Up @@ -181,7 +178,6 @@ def train(self, task_id, round):

for epoch in range(self.epochs):
for x, y in self.labeled_train_loader:
# logging.info(f' y is {y.numpy()}')
y = np.array([self.class_mapping[i] for i in y.numpy()])
tasks = self._split_tasks(x, y)
base_model_weights = self.feature_extractor.get_weights()
Expand Down
4 changes: 1 addition & 3 deletions examples/cifar100/fci_ssl/fedavg/algorithm/basemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ def build_classifier(self):
if self.classifier != None:
new_classifier = keras.Sequential(
[
# tf.keras.Input(shape=(None, self.feature_extractor.layers[-2].output_shape[-1])),
keras.layers.Dense(
self.num_classes, kernel_initializer="lecun_normal"
)
Expand All @@ -109,7 +108,6 @@ def build_classifier(self):
logging.info(f"input shape is {self.fe.layers[-2].output_shape[-1]}")
self.classifier = keras.Sequential(
[
# tf.keras.Input(shape=(None, self.feature_extractor.layers[-2].output_shape[-1])),
keras.layers.Dense(
self.num_classes, kernel_initializer="lecun_normal"
)
Expand Down Expand Up @@ -174,7 +172,7 @@ def predict(self, data_files, **kwargs):
pred = tf.argmax(prob, axis=1)
pred = tf.cast(pred, dtype=tf.int32)
result[data] = pred.numpy()
print("finish predict")
logging.info("finish predict")
return result

def eval(self, data, round, **kwargs):
Expand Down
3 changes: 0 additions & 3 deletions examples/cifar100/fci_ssl/fedavg/algorithm/resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def call(self, inputs, training=None):
return output


# 残差神经网络
class ResNet(keras.Model):
def __init__(self, layer_dims, num_classes=10): # [2, 2, 2, 2]
super(ResNet, self).__init__()
Expand Down Expand Up @@ -92,8 +91,6 @@ def call(self, inputs, training=None):

# [b, c]
x = self.avgpool(x)
# [b, 100]
# x = self.fc(x)
return x

def build_resblock(self, filter_num, blocks, stride=1):
Expand Down
10 changes: 1 addition & 9 deletions examples/cifar100/fci_ssl/glfc/algorithm/GLFC.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@


def get_one_hot(target, num_classes):
# print(f'in get one hot, target shape is {target.shape}')
y = tf.one_hot(target, depth=num_classes)
# print(f'in get one hot, y shape is {y.shape}')
if len(y.shape) == 3:
y = tf.squeeze(y, axis=1)
# print(f'in get one hot, after tf.squeeze y shape is {y.shape}')
return y


Expand Down Expand Up @@ -87,7 +84,6 @@ def _initialize_classifier(self):
if self.classifier != None:
new_classifier = tf.keras.Sequential(
[
# tf.keras.Input(shape=(None, self.feature_extractor.layers[-2].output_shape[-1])),
tf.keras.layers.Dense(
self.num_classes, kernel_initializer="lecun_normal"
)
Expand All @@ -112,7 +108,6 @@ def _initialize_classifier(self):
)
self.classifier = tf.keras.Sequential(
[
# tf.keras.Input(shape=(None, self.feature_extractor.layers[-2].output_shape[-1])),
tf.keras.layers.Dense(
self.num_classes, kernel_initializer="lecun_normal"
)
Expand Down Expand Up @@ -163,7 +158,6 @@ def update_new_set(self, need_update):
self._reduce_exemplar_set(m)
for i in self.last_class:
images = self.get_train_set_data(i)
# print(f'process class {i} with {len(images)} images')
self._construct_exemplar_set(images, i, m)

def _get_train_loader(self, mix):
Expand Down Expand Up @@ -196,7 +190,6 @@ def train(self, round):
opt = keras.optimizers.Adam(
learning_rate=self.learning_rate, weight_decay=0.00001
)
# print(self.train_loader is None)
feature_extractor_params = self.feature_extractor.trainable_variables
classifier_params = self.classifier.trainable_variables
all_params = []
Expand Down Expand Up @@ -439,11 +432,10 @@ def evaluate(self):
total_correct = 0
for x, y in self.train_loader:
logits = self.model_call(x, training=False)
# prob = tf.nn.softmax(logits, axis=1)
pred = tf.argmax(logits, axis=1)
pred = tf.cast(pred, dtype=tf.int32)
pred = tf.reshape(pred, y.shape)
print(pred)
logging.info(pred)
y = tf.cast(y, dtype=tf.int32)
correct = tf.cast(tf.equal(pred, y), dtype=tf.int32)
correct = tf.reduce_sum(correct)
Expand Down
2 changes: 0 additions & 2 deletions examples/cifar100/fci_ssl/glfc/algorithm/data_prepocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def __init__(
print(f"mean: {self.mean}, std: {self.std}")

def preprocess_labeled_dataset(self, x, y, batch_size):
# wx = self.weak_augment_helper(x)
return (
tf.data.Dataset.from_tensor_slices((x, y))
.shuffle(100000)
Expand All @@ -48,7 +47,6 @@ def preprocess_labeled_dataset(self, x, y, batch_size):
)

def preprocess_unlabeled_dataset(self, ux, uy, batch_size):
# unlabeled_train_db = tf.data.Dataset.from_tensor_slices((ux, ux, ux, uy))

wux = self.weak_augment_helper(ux)
sux = self.strong_augment_helper(ux)
Expand Down
1 change: 0 additions & 1 deletion examples/cifar100/fci_ssl/glfc/algorithm/proxy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ def reconstruction(self):

for label_i in range(100):
if class_ratio[0][label_i] > 0:
# num_agumentation = self.num_image
agumentation = []

grad_index = np.where(proto_label == label_i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def __init__(self, num_classes, feature_extractor):
self.fc = Dense(num_classes, activation="softmax")

def call(self, inputs):
# print(type(self.feature))
x = self.feature(inputs)
x = self.fc(x)
return x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def __init__(self, layer_dims, num_classes=10): # [2, 2, 2, 2]

# output: [b, 512, h, w],
self.avgpool = keras.layers.GlobalAveragePooling2D()
# self.fc = keras.layers.Dense(num_classes)

def call(self, inputs, training=None):
x = self.stem(inputs, training=training)
Expand All @@ -91,8 +90,6 @@ def call(self, inputs, training=None):

# [b, c]
x = self.avgpool(x)
# [b, 100]
# x = self.fc(x)
return x

def build_resblock(self, filter_num, blocks, stride=1):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import abc
from copy import deepcopy
from typing import List

import numpy as np
from sedna.algorithms.aggregation.aggregation import BaseAggregation
Expand All @@ -32,7 +31,7 @@ def aggregate(self, clients):
Parameters
----------
clients: List
clients:
All clients in federated learning job
Returns
Expand Down

0 comments on commit ce84a2e

Please sign in to comment.