<jupyter_start><jupyter_text>Federated FastEstimator FGSM Tutorial<jupyter_code>#Install dependencies if not already installed
!pip install tensorflow'>=2.3' torch'>=1.6' fastestimator
import fastestimator as fe
import tempfile
from fastestimator.architecture.pytorch import LeNet
from fastestimator.backend import to_tensor, argmax
from fastestimator.dataset.data import cifar10
from fastestimator.op.numpyop.meta import Sometimes
from fastestimator.op.numpyop.multivariate import HorizontalFlip
from fastestimator.op.numpyop.univariate import CoarseDropout, Normalize, Onehot
from fastestimator.op.tensorop import Average
from fastestimator.op.tensorop.gradient import Watch, FGSM
from fastestimator.op.tensorop.loss import CrossEntropy
from fastestimator.op.tensorop.model import ModelOp, UpdateOp
from fastestimator.trace.io import BestModelSaver
from fastestimator.trace.metric import Accuracy
from fastestimator.util import ImgData, to_number
from openfl.native.fastestimator import FederatedFastEstimator
from fastestimator.dataset.data import cifar10
from fastestimator.trace.adapt import LRScheduler,ReduceLROnPlateau
from fastestimator.op.numpyop.univariate import Normalize, ChannelTranspose

batch_size=128

train_data, eval_data = cifar10.load_data()
test_data = eval_data.split(0.5)

pipeline = fe.Pipeline(train_data=train_data,
                     eval_data=eval_data,
                     test_data=test_data,
                     batch_size=batch_size,
                     ops=[Normalize(inputs="x", outputs="x",
                             mean=(0.4914, 0.4822, 0.4465), 
                             std=(0.2471, 0.2435, 0.2616)),
                          ChannelTranspose(inputs="x", outputs="x")])
model = fe.build(model_fn=lambda: LeNet(input_shape=(3, 32, 32)), \
                         optimizer_fn="adam", model_name="adv_model")

network = fe.Network(ops=[
                Watch(inputs="x"),
                ModelOp(model=model, inputs="x", outputs="y_pred"),
                CrossEntropy(inputs=("y_pred", "y"), outputs="base_ce"),
                FGSM(data="x", loss="base_ce", outputs="x_adverse", epsilon=0.04),
                ModelOp(model=model, inputs="x_adverse", outputs="y_pred_adv"),
                CrossEntropy(inputs=("y_pred_adv", "y"), outputs="adv_ce"),
                Average(inputs=("base_ce", "adv_ce"), outputs="avg_ce"),
                UpdateOp(model=model, loss_name="avg_ce")
            ])

estimator = fe.Estimator(pipeline=pipeline,
                         network=network,
                         epochs=1,
                         traces=[Accuracy(true_key="y", pred_key="y_pred", output_name="clean_accuracy"),
                                Accuracy(true_key="y", pred_key="y_pred_adv", output_name="adversarial_accuracy"),
                                ReduceLROnPlateau(model=model,metric='base_ce',patience=2),
                                BestModelSaver(model=model, save_dir=tempfile.mkdtemp(), metric="base_ce", save_best_mode="min",load_best_final=True),],
                         max_train_steps_per_epoch=None,
                         max_eval_steps_per_epoch=None,
                         monitor_names=["base_ce", "adv_ce"],
                         log_steps=50)
openfl_estimator = FederatedFastEstimator(estimator, override_config={'aggregator.settings.rounds_to_train':5})
model=openfl_estimator.fit()
model.state_dict()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10575246949447996, 'fraction_numerical': 0.0168506682161534, 'mean_word_length': 3.041079812206573, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '48094', 'license': 'Apache-2.0', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1020, 'n_words': 172, 'path': 'openfl-tutorials/Federated_FastEstimator_FGSM_Tutorial.ipynb', 'repo_name': 'karol-brejna-i/openfl', 'size': '3442'}