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.

oneflow_convert.py 3.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """
  2. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. """
  13. # oneflow checkpoint 到savedmodel 转换脚本
  14. # 转换出来job_name是'inference',也就是 make_infer_func 中定义的同名函数
  15. import oneflow as flow
  16. import os
  17. import shutil
  18. os.environ['CUDA_VISIBLE_DEVICES'] = '3'
  19. # 输入参数(需要根据模型修改)
  20. image_size = (3, 299, 299)
  21. model_name = "inceptionv3"
  22. saved_model_path = "inceptionv3"
  23. batch_size = 8
  24. # 导入模型网络结构
  25. # from of_models.resnet_model import resnet50
  26. # from of_models.alexnet_model import alexnet
  27. # from of_models.vgg_model import vgg16bn
  28. from models.of_models import inceptionv3
  29. model = inceptionv3
  30. # 模型checkpoint文件路径
  31. # DEFAULT_CHECKPOINT_DIR = "./checkpoint_of/resnet_v15_of_best_model_val_top1_77318"
  32. # DEFAULT_CHECKPOINT_DIR = "./checkpoint_of/alexnet_of_best_model_val_top1_54762"
  33. # DEFAULT_CHECKPOINT_DIR = "./checkpoint_of/vgg16_of_best_model_val_top1_721"
  34. DEFAULT_CHECKPOINT_DIR = "./checkpoint_of/snapshot_epoch_75"
  35. def init_env():
  36. flow.env.init()
  37. flow.config.machine_num(1)
  38. flow.config.cpu_device_num(1)
  39. flow.config.gpu_device_num(1)
  40. flow.config.enable_debug_mode(True)
  41. def make_infer_func(batch_size, image_size):
  42. input_lbns = {}
  43. output_lbns = {}
  44. image_shape = (batch_size,) + tuple(image_size)
  45. @flow.global_function(type="predict")
  46. def inference(
  47. image: flow.typing.Numpy.Placeholder(image_shape, dtype=flow.float32)
  48. ) -> flow.typing.Numpy:
  49. input_lbns["image"] = image.logical_blob_name
  50. output = model(image)
  51. output = flow.nn.softmax(output)
  52. output_lbns["output"] = output.logical_blob_name
  53. return output
  54. return inference, input_lbns, output_lbns
  55. def ckpt_to_savedmodel():
  56. init_env()
  57. resnet_infer, input_lbns, output_lbns = make_infer_func(
  58. batch_size, image_size
  59. )
  60. # origin resnet inference model
  61. checkpoint = flow.train.CheckPoint()
  62. checkpoint.load(DEFAULT_CHECKPOINT_DIR)
  63. # save model
  64. # 修改路径
  65. if os.path.exists(saved_model_path) and os.path.isdir(saved_model_path):
  66. shutil.rmtree(saved_model_path)
  67. model_version = 1
  68. saved_model_builder = flow.SavedModelBuilderV2(saved_model_path)
  69. job_builder = (
  70. saved_model_builder.ModelName(model_name)
  71. .Version(model_version)
  72. .Job(resnet_infer)
  73. )
  74. for input_name, lbn in input_lbns.items():
  75. job_builder.Input(input_name, lbn)
  76. for output_name, lbn in output_lbns.items():
  77. job_builder.Output(output_name, lbn)
  78. job_builder.Complete().Save()
  79. if __name__ == "__main__":
  80. ckpt_to_savedmodel()

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能