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.

train.py 2.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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.keras.preprocessing.image as img_preprocessing
  17. from interface import fedavg, s3_transmitter
  18. from interface import Dataset, Estimator
  19. from sedna.core.federated_learning import FederatedLearningV2
  20. from sedna.datasources import TxtDataParse
  21. from sedna.common.config import BaseConfig
  22. def image_process(line):
  23. file_path, label = line.split(',')
  24. original_dataset_url = (
  25. BaseConfig.original_dataset_url or BaseConfig.train_dataset_url
  26. )
  27. root_path = os.path.dirname(original_dataset_url)
  28. file_path = os.path.join(root_path, file_path)
  29. img = img_preprocessing.load_img(file_path).resize((128, 128))
  30. data = img_preprocessing.img_to_array(img) / 255.0
  31. label = [0, 1] if int(label) == 0 else [1, 0]
  32. data = np.array(data)
  33. label = np.array(label)
  34. return [data, label]
  35. def main():
  36. train_dataset_url = BaseConfig.train_dataset_url
  37. # we have same data in the trainset and testset
  38. test_dataset_url = BaseConfig.train_dataset_url
  39. train_data = TxtDataParse(data_type="train", func=image_process)
  40. train_data.parse(train_dataset_url)
  41. data = Dataset(trainset=train_data, testset=train_data)
  42. estimator = Estimator()
  43. fl_model = FederatedLearningV2(
  44. data=data,
  45. estimator=estimator,
  46. aggregation=fedavg,
  47. transmitter=s3_transmitter)
  48. fl_model.train()
  49. if __name__ == '__main__':
  50. main()