You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

interface.py 3.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright 2021 The KubeEdge Authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import numpy as np
  16. import tensorflow as tf
  17. from tensorflow import keras
  18. from tensorflow.keras.layers import Dense, MaxPooling2D, Conv2D, Flatten, Dropout
  19. from tensorflow.keras.models import Sequential
  20. from sedna.algorithms.aggregation import FedAvgV2
  21. from sedna.algorithms.client_choose import SimpleClientChoose
  22. from sedna.common.config import Context
  23. from sedna.core.federated_learning import FederatedLearningV2
  24. os.environ['BACKEND_TYPE'] = 'KERAS'
  25. simple_chooser = SimpleClientChoose(per_round=2)
  26. # It has been determined that mistnet is required here.
  27. fedavg = FedAvgV2()
  28. # The function `get_transmitter_from_config()` returns an object instance.
  29. s3_transmitter = FederatedLearningV2.get_transmitter_from_config()
  30. class Dataset:
  31. def __init__(self, trainset=None, testset=None) -> None:
  32. self.customized = True
  33. self.trainset = tf.data.Dataset.from_tensor_slices((trainset.x, trainset.y))
  34. self.trainset = self.trainset.batch(int(Context.get_parameters("batch_size", 32)))
  35. self.testset = tf.data.Dataset.from_tensor_slices((testset.x, testset.y))
  36. self.testset = self.testset.batch(int(Context.get_parameters("batch_size", 32)))
  37. class Estimator:
  38. def __init__(self) -> None:
  39. self.model = self.build()
  40. self.pretrained = None
  41. self.saved = None
  42. self.hyperparameters = {
  43. "use_tensorflow": True,
  44. "is_compiled": True,
  45. "type": "basic",
  46. "rounds": int(Context.get_parameters("exit_round", 5)),
  47. "target_accuracy": 0.97,
  48. "epochs": int(Context.get_parameters("epochs", 5)),
  49. "batch_size": int(Context.get_parameters("batch_size", 32)),
  50. "optimizer": "SGD",
  51. "learning_rate": float(Context.get_parameters("learning_rate", 0.01)),
  52. # The machine learning model
  53. "model_name": "sdd_model",
  54. "momentum": 0.9,
  55. "weight_decay": 0.0
  56. }
  57. @staticmethod
  58. def build():
  59. model = Sequential()
  60. model.add(Conv2D(64, kernel_size=(3, 3),
  61. activation="relu", strides=(2, 2),
  62. input_shape=(128, 128, 3)))
  63. model.add(MaxPooling2D(pool_size=(2, 2)))
  64. model.add(Conv2D(32, kernel_size=(3, 3), activation="relu"))
  65. model.add(MaxPooling2D(pool_size=(2, 2)))
  66. model.add(Flatten())
  67. model.add(Dropout(0.25))
  68. model.add(Dense(64, activation="relu"))
  69. model.add(Dense(32, activation="relu"))
  70. model.add(Dropout(0.5))
  71. model.add(Dense(2, activation="softmax"))
  72. model.compile(loss="categorical_crossentropy",
  73. optimizer="adam",
  74. metrics=["accuracy"])
  75. return model