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.

of_cnn_resnet.py 2.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # !/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. """
  4. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  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. =============================================================
  15. """
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import sys
  20. import codecs
  21. import os
  22. import numpy as np
  23. from PIL import Image
  24. import oneflow as flow
  25. from resnet_model import resnet50
  26. sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
  27. current_dir = os.path.dirname(os.path.abspath(__file__))
  28. def init_resnet():
  29. """Initialize ResNet with pretrained weights"""
  30. model_load_dir = current_dir + os.sep + "model" + os.sep + 'resnet_v15_of_best_model_val_top1_773/'
  31. assert os.path.isdir(model_load_dir)
  32. check_point = flow.train.CheckPoint()
  33. check_point.load(model_load_dir)
  34. def load_image(image_path):
  35. """Load and preprocess the image"""
  36. rgb_mean = [123.68, 116.779, 103.939]
  37. rgb_std = [58.393, 57.12, 57.375]
  38. im = Image.open(image_path).convert('RGB')
  39. im = im.resize((224, 224))
  40. im = np.array(im).astype('float32')
  41. im = (im - rgb_mean) / rgb_std
  42. im = np.transpose(im, (2, 0, 1))
  43. im = np.expand_dims(im, axis=0)
  44. return np.ascontiguousarray(im, 'float32')
  45. @flow.global_function(flow.function_config())
  46. def InferenceNet(images=flow.FixedTensorDef(
  47. (1, 3, 224, 224), dtype=flow.float)):
  48. """Run the inference of ResNet"""
  49. logits = resnet50(images, training=False)
  50. predictions = flow.nn.softmax(logits)
  51. return predictions
  52. def resnet_inf(image_path):
  53. """The whole procedure of inference of ResNet and return the category_id and the corresponding score"""
  54. image = load_image(image_path.encode('utf-8'))
  55. predictions = InferenceNet(image).get()
  56. clsidx = predictions.ndarray().argmax()
  57. return predictions.ndarray().max(), clsidx

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