|
| 1 | +from transformers import TFAutoModelForImageClassification |
| 2 | +from transformers import ConvNextFeatureExtractor, ViTFeatureExtractor |
| 3 | +from transformers import BeitFeatureExtractor, AutoFeatureExtractor |
| 4 | +import tensorflow as tf |
| 5 | +from PIL import Image |
| 6 | +import requests |
| 7 | +from shark.shark_inference import SharkInference |
| 8 | +from shark.shark_downloader import download_tf_model |
| 9 | + |
| 10 | +# Create a set of input signature. |
| 11 | +inputs_signature = [ |
| 12 | + tf.TensorSpec(shape=[1, 3, 224, 224], dtype=tf.float32), |
| 13 | +] |
| 14 | + |
| 15 | + |
| 16 | +class AutoModelImageClassfication(tf.Module): |
| 17 | + def __init__(self, model_name): |
| 18 | + super(AutoModelImageClassfication, self).__init__() |
| 19 | + self.m = TFAutoModelForImageClassification.from_pretrained( |
| 20 | + model_name, output_attentions=False |
| 21 | + ) |
| 22 | + self.m.predict = lambda x: self.m(x) |
| 23 | + |
| 24 | + @tf.function(input_signature=inputs_signature) |
| 25 | + def forward(self, inputs): |
| 26 | + return self.m.predict(inputs) |
| 27 | + |
| 28 | + |
| 29 | +fail_models = [ |
| 30 | + "facebook/data2vec-vision-base-ft1k", |
| 31 | + "microsoft/swin-tiny-patch4-window7-224", |
| 32 | +] |
| 33 | + |
| 34 | +supported_models = [ |
| 35 | + # "facebook/convnext-tiny-224", |
| 36 | + "google/vit-base-patch16-224", |
| 37 | +] |
| 38 | + |
| 39 | +img_models_fe_dict = { |
| 40 | + "facebook/convnext-tiny-224": ConvNextFeatureExtractor, |
| 41 | + "facebook/data2vec-vision-base-ft1k": BeitFeatureExtractor, |
| 42 | + "microsoft/swin-tiny-patch4-window7-224": AutoFeatureExtractor, |
| 43 | + "google/vit-base-patch16-224": ViTFeatureExtractor, |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +def preprocess_input_image(model_name): |
| 48 | + # from datasets import load_dataset |
| 49 | + # dataset = load_dataset("huggingface/cats-image") |
| 50 | + # image1 = dataset["test"]["image"][0] |
| 51 | + # # print("image1: ", image1) # <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=640x480 at 0x7FA0B86BB6D0> |
| 52 | + url = "http://images.cocodataset.org/val2017/000000039769.jpg" |
| 53 | + # <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=640x480 at 0x7FA0B86BB6D0> |
| 54 | + image = Image.open(requests.get(url, stream=True).raw) |
| 55 | + feature_extractor = img_models_fe_dict[model_name].from_pretrained( |
| 56 | + model_name |
| 57 | + ) |
| 58 | + # inputs: {'pixel_values': <tf.Tensor: shape=(1, 3, 224, 224), dtype=float32, numpy=array([[[[]]]], dtype=float32)>} |
| 59 | + inputs = feature_extractor(images=image, return_tensors="tf") |
| 60 | + |
| 61 | + return [inputs[str(*inputs)]] |
| 62 | + |
| 63 | + |
| 64 | +def get_causal_image_model(hf_name): |
| 65 | + model = AutoModelImageClassfication(hf_name) |
| 66 | + test_input = preprocess_input_image(hf_name) |
| 67 | + # TFSequenceClassifierOutput(loss=None, logits=<tf.Tensor: shape=(1, 1000), dtype=float32, numpy= |
| 68 | + # array([[]], dtype=float32)>, hidden_states=None, attentions=None) |
| 69 | + actual_out = model.forward(*test_input) |
| 70 | + return model, test_input, actual_out |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + for model_name in supported_models: |
| 75 | + print(f"Running model: {model_name}") |
| 76 | + inputs = preprocess_input_image(model_name) |
| 77 | + model = AutoModelImageClassfication(model_name) |
| 78 | + |
| 79 | + # 1. USE SharkImporter to get the mlir |
| 80 | + # from shark.shark_importer import SharkImporter |
| 81 | + # mlir_importer = SharkImporter( |
| 82 | + # model, |
| 83 | + # inputs, |
| 84 | + # frontend="tf", |
| 85 | + # ) |
| 86 | + # imported_mlir, func_name = mlir_importer.import_mlir() |
| 87 | + |
| 88 | + # 2. USE SharkDownloader to get the mlir |
| 89 | + imported_mlir, func_name, inputs, golden_out = download_tf_model( |
| 90 | + model_name |
| 91 | + ) |
| 92 | + |
| 93 | + shark_module = SharkInference( |
| 94 | + imported_mlir, func_name, device="cpu", mlir_dialect="mhlo" |
| 95 | + ) |
| 96 | + shark_module.compile() |
| 97 | + shark_module.forward(inputs) |
0 commit comments