Skip to content

Commit

Permalink
remove commented out code
Browse files Browse the repository at this point in the history
  • Loading branch information
rachguo authored and rachguo committed Oct 26, 2023
1 parent e397e47 commit 71b2300
Showing 1 changed file with 60 additions and 60 deletions.
120 changes: 60 additions & 60 deletions test/test_tools_add_pre_post_processing_to_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# `pip install -e .` from the repo root.
from onnxruntime_extensions import get_library_path
from onnxruntime_extensions.tools import add_pre_post_processing_to_model as add_ppp
# from onnxruntime_extensions.tools import add_HuggingFace_CLIPImageProcessor_to_model as add_clip_feature
from onnxruntime_extensions.tools import add_HuggingFace_CLIPImageProcessor_to_model as add_clip_feature
from onnxruntime_extensions.tools import pre_post_processing as pre_post_processing
from onnxruntime_extensions.tools.pre_post_processing.steps import *

Expand Down Expand Up @@ -98,65 +98,65 @@ def new_output():
# check within 1%. probability values are in range 0..1
self.assertTrue(abs(orig_results[orig_idx] - new_results[new_idx]) < 0.01)

# def test_pytorch_mobilenet_using_clip_feature(self):
# input_model = os.path.join(test_data_dir, "pytorch_mobilenet_v2.onnx")
# output_model = os.path.join(test_data_dir, "pytorch_mobilenet_v2.updated.onnx")
# input_image_path = os.path.join(test_data_dir, "wolves.jpg")

# add_clip_feature.clip_image_processor(Path(input_model), Path(output_model), opset=16, do_resize=True,
# do_center_crop=True, do_normalize=True, do_rescale=True,
# do_convert_rgb=True, size=256, crop_size=224,
# rescale_factor=1/255, image_mean=[0.485, 0.456, 0.406],
# image_std=[0.229, 0.224, 0.225])

# def orig_output():
# from torchvision import transforms
# input_image = Image.open(input_image_path)
# preprocess = transforms.Compose([
# transforms.Resize(256),
# transforms.CenterCrop(224),
# transforms.ToTensor(),
# transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
# ])
# input_tensor = preprocess(input_image)
# input_batch = input_tensor.unsqueeze(
# 0).detach().cpu().numpy() # create a mini-batch as expected by the model

# s = ort.InferenceSession(input_model, providers=['CPUExecutionProvider'])
# scores = s.run(None, {"x": np.array(input_batch)})
# scores = np.squeeze(scores)

# def softmax(x):
# e_x = np.exp(x - np.max(x))
# return e_x / e_x.sum()

# probabilities = softmax(scores)
# return probabilities

# def new_output():
# input_bytes = np.fromfile(input_image_path, dtype=np.uint8)
# so = ort.SessionOptions()
# so.register_custom_ops_library(get_library_path())

# s = ort.InferenceSession(output_model, so, providers=['CPUExecutionProvider'])
# probabilities = s.run(None, {"image": np.array(input_bytes)})[0]
# scores = np.squeeze(probabilities) # remove batch dim

# def softmax(x):
# e_x = np.exp(x - np.max(x))
# return e_x / e_x.sum()

# probabilities = softmax(scores)
# return probabilities

# orig_results = orig_output()
# new_results = new_output()

# orig_idx = np.argmax(orig_results)
# new_idx = np.argmax(new_results)
# self.assertEqual(orig_idx, new_idx)
# # check within 1%. probability values are in range 0..1
# self.assertTrue(abs(orig_results[orig_idx] - new_results[new_idx]) < 0.01)
def test_pytorch_mobilenet_using_clip_feature(self):
input_model = os.path.join(test_data_dir, "pytorch_mobilenet_v2.onnx")
output_model = os.path.join(test_data_dir, "pytorch_mobilenet_v2.updated.onnx")
input_image_path = os.path.join(test_data_dir, "wolves.jpg")

add_clip_feature.clip_image_processor(Path(input_model), Path(output_model), opset=16, do_resize=True,
do_center_crop=True, do_normalize=True, do_rescale=True,
do_convert_rgb=True, size=256, crop_size=224,
rescale_factor=1/255, image_mean=[0.485, 0.456, 0.406],
image_std=[0.229, 0.224, 0.225])

def orig_output():
from torchvision import transforms
input_image = Image.open(input_image_path)
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(
0).detach().cpu().numpy() # create a mini-batch as expected by the model

s = ort.InferenceSession(input_model, providers=['CPUExecutionProvider'])
scores = s.run(None, {"x": np.array(input_batch)})
scores = np.squeeze(scores)

def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum()

probabilities = softmax(scores)
return probabilities

def new_output():
input_bytes = np.fromfile(input_image_path, dtype=np.uint8)
so = ort.SessionOptions()
so.register_custom_ops_library(get_library_path())

s = ort.InferenceSession(output_model, so, providers=['CPUExecutionProvider'])
probabilities = s.run(None, {"image": np.array(input_bytes)})[0]
scores = np.squeeze(probabilities) # remove batch dim

def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum()

probabilities = softmax(scores)
return probabilities

orig_results = orig_output()
new_results = new_output()

orig_idx = np.argmax(orig_results)
new_idx = np.argmax(new_results)
self.assertEqual(orig_idx, new_idx)
# check within 1%. probability values are in range 0..1
self.assertTrue(abs(orig_results[orig_idx] - new_results[new_idx]) < 0.01)

def test_tflite_mobilenet(self):
input_model = os.path.join(test_data_dir, "tflite_mobilenet_v2.onnx")
Expand Down

0 comments on commit 71b2300

Please sign in to comment.