From ce84a2ebba876cf67778511aed494bcf6a15c300 Mon Sep 17 00:00:00 2001 From: Marchons <1286360646@qq.com> Date: Tue, 29 Oct 2024 09:57:03 +0800 Subject: [PATCH] remove(example/cifar100) remove the annotation code and chinese annotation Signed-off-by: Marchons <1286360646@qq.com> --- .../fed_ci_match/algorithm/FedCiMatch.py | 25 +------------------ .../fed_ci_match/algorithm/basemodel.py | 1 - .../fed_ci_match_v2/algorithm/FedCiMatch.py | 4 --- .../fci_ssl/fedavg/algorithm/basemodel.py | 4 +-- .../fci_ssl/fedavg/algorithm/resnet.py | 3 --- .../cifar100/fci_ssl/glfc/algorithm/GLFC.py | 10 +------- .../glfc/algorithm/data_prepocessor.py | 2 -- .../fci_ssl/glfc/algorithm/proxy_server.py | 1 - .../fedavg/algorithm/network.py | 1 - .../fedavg/algorithm/resnet.py | 3 --- .../fedavg/algorithm/aggregation.py | 3 +-- 11 files changed, 4 insertions(+), 53 deletions(-) diff --git a/examples/cifar100/fci_ssl/fed_ci_match/algorithm/FedCiMatch.py b/examples/cifar100/fci_ssl/fed_ci_match/algorithm/FedCiMatch.py index aa11fdea..cc7b1ffc 100644 --- a/examples/cifar100/fci_ssl/fed_ci_match/algorithm/FedCiMatch.py +++ b/examples/cifar100/fci_ssl/fed_ci_match/algorithm/FedCiMatch.py @@ -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 @@ -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" ) @@ -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" ) @@ -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}" @@ -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] @@ -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 = ( @@ -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): @@ -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) diff --git a/examples/cifar100/fci_ssl/fed_ci_match/algorithm/basemodel.py b/examples/cifar100/fci_ssl/fed_ci_match/algorithm/basemodel.py index b4201a57..e351ddf0 100644 --- a/examples/cifar100/fci_ssl/fed_ci_match/algorithm/basemodel.py +++ b/examples/cifar100/fci_ssl/fed_ci_match/algorithm/basemodel.py @@ -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, diff --git a/examples/cifar100/fci_ssl/fed_ci_match_v2/algorithm/FedCiMatch.py b/examples/cifar100/fci_ssl/fed_ci_match_v2/algorithm/FedCiMatch.py index bab2bf4b..77513380 100644 --- a/examples/cifar100/fci_ssl/fed_ci_match_v2/algorithm/FedCiMatch.py +++ b/examples/cifar100/fci_ssl/fed_ci_match_v2/algorithm/FedCiMatch.py @@ -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" ) @@ -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" ) @@ -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): @@ -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() diff --git a/examples/cifar100/fci_ssl/fedavg/algorithm/basemodel.py b/examples/cifar100/fci_ssl/fedavg/algorithm/basemodel.py index aaea5639..7fdc725b 100644 --- a/examples/cifar100/fci_ssl/fedavg/algorithm/basemodel.py +++ b/examples/cifar100/fci_ssl/fedavg/algorithm/basemodel.py @@ -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" ) @@ -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" ) @@ -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): diff --git a/examples/cifar100/fci_ssl/fedavg/algorithm/resnet.py b/examples/cifar100/fci_ssl/fedavg/algorithm/resnet.py index 1aa331cd..cbc4de72 100644 --- a/examples/cifar100/fci_ssl/fedavg/algorithm/resnet.py +++ b/examples/cifar100/fci_ssl/fedavg/algorithm/resnet.py @@ -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__() @@ -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): diff --git a/examples/cifar100/fci_ssl/glfc/algorithm/GLFC.py b/examples/cifar100/fci_ssl/glfc/algorithm/GLFC.py index 92c99330..469ae720 100644 --- a/examples/cifar100/fci_ssl/glfc/algorithm/GLFC.py +++ b/examples/cifar100/fci_ssl/glfc/algorithm/GLFC.py @@ -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 @@ -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" ) @@ -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" ) @@ -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): @@ -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 = [] @@ -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) diff --git a/examples/cifar100/fci_ssl/glfc/algorithm/data_prepocessor.py b/examples/cifar100/fci_ssl/glfc/algorithm/data_prepocessor.py index 8e9b0d29..449e09f2 100644 --- a/examples/cifar100/fci_ssl/glfc/algorithm/data_prepocessor.py +++ b/examples/cifar100/fci_ssl/glfc/algorithm/data_prepocessor.py @@ -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) @@ -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) diff --git a/examples/cifar100/fci_ssl/glfc/algorithm/proxy_server.py b/examples/cifar100/fci_ssl/glfc/algorithm/proxy_server.py index 49f456db..69e03901 100644 --- a/examples/cifar100/fci_ssl/glfc/algorithm/proxy_server.py +++ b/examples/cifar100/fci_ssl/glfc/algorithm/proxy_server.py @@ -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) diff --git a/examples/cifar100/federated_class_incremental_learning/fedavg/algorithm/network.py b/examples/cifar100/federated_class_incremental_learning/fedavg/algorithm/network.py index 2c32338f..20630623 100644 --- a/examples/cifar100/federated_class_incremental_learning/fedavg/algorithm/network.py +++ b/examples/cifar100/federated_class_incremental_learning/fedavg/algorithm/network.py @@ -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 diff --git a/examples/cifar100/federated_class_incremental_learning/fedavg/algorithm/resnet.py b/examples/cifar100/federated_class_incremental_learning/fedavg/algorithm/resnet.py index e1dbec29..48db9122 100644 --- a/examples/cifar100/federated_class_incremental_learning/fedavg/algorithm/resnet.py +++ b/examples/cifar100/federated_class_incremental_learning/fedavg/algorithm/resnet.py @@ -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) @@ -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): diff --git a/examples/cifar100/federated_learning/fedavg/algorithm/aggregation.py b/examples/cifar100/federated_learning/fedavg/algorithm/aggregation.py index 52a9a140..df1de505 100644 --- a/examples/cifar100/federated_learning/fedavg/algorithm/aggregation.py +++ b/examples/cifar100/federated_learning/fedavg/algorithm/aggregation.py @@ -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 @@ -32,7 +31,7 @@ def aggregate(self, clients): Parameters ---------- - clients: List + clients: All clients in federated learning job Returns