Views
No views yet
combined_size_vs_performance_xkcd.png:
images/combined_size_vs_performance_xkcd.png)images/ folder of this repository..tflite model from this repository. You can download it using curl or directly from the "Files and versions" tab.curl -L -O https://huggingface.co/anton96vice/mobileclip2_tflite/resolve/main/mobileclip_s2_datacompdr_last.tflite.tflite model file.1import tensorflow as tf
2import numpy as np
3# from PIL import Image # For image preprocessing
4
5# Load the TFLite model and allocate tensors.
6interpreter = tf.lite.Interpreter(model_path="mobileclip_s2_datacompdr_last.tflite") # Or your chosen model file
7interpreter.allocate_tensors()
8
9# Get input and output tensor details.
10input_details = interpreter.get_input_details()
11output_details = interpreter.get_output_details()
12
13print("Input Details:", input_details)
14print("Output Details:", output_details)input_details and output_details carefully! This will tell you the expected shape, data type (e.g., float32, int32), and names of the input and output tensors. MobileCLIP models typically have separate inputs/outputs for the image tower and the text tower.input_details).[0, 1] and then normalize using ImageNet mean and standard deviation.[1, height, width, 3]) and data type (float32).1# Example Image Preprocessing (conceptual - adapt to your model's specifics)
2# from PIL import Image
3#
4# def preprocess_image(image_path, input_shape):
5# img = Image.open(image_path).convert('RGB')
6# img = img.resize((input_shape[1], input_shape[2])) # Assuming HWC format for shape
7# img_array = np.array(img, dtype=np.float32) / 255.0 # Scale to [0,1]
8#
9# # Example normalization (adjust if different for your model)
10# mean = np.array([0.485, 0.456, 0.406], dtype=np.float32)
11# std = np.array([0.229, 0.224, 0.225], dtype=np.float32)
12# img_array = (img_array - mean) / std
13#
14# return np.expand_dims(img_array, axis=0) # Add batch dimension
15
16# Assuming image_input_index is the index for image tensor from input_details
17# image_input_tensor_index = input_details[image_input_index]['index']
18# image_input_shape = input_details[image_input_index]['shape']
19# preprocessed_image = preprocess_image("your_image.jpg", image_input_shape)
20# interpreter.set_tensor(image_input_tensor_index, preprocessed_image)input_details). Pad shorter sequences or truncate longer ones.[1, sequence_length]) and data type (int32).1# Example Text Preprocessing (conceptual - you'll need the correct tokenizer)
2#
3# def tokenize_text(text, tokenizer, max_length):
4# # This is highly dependent on the actual tokenizer used by MobileCLIP
5# # For example, if using a Hugging Face tokenizer:
6# # inputs = tokenizer(text, return_tensors="np", padding="max_length", truncation=True, max_length=max_length)
7# # return inputs['input_ids'] # Or other relevant tokenizer output
8# # For a simple BPE or SentencePiece tokenizer, it would be different.
9# # Placeholder for conceptual demonstration:
10# token_ids = np.random.randint(0, 30000, size=(1, max_length), dtype=np.int32) # Replace with actual tokenization
11# return token_ids
12
13# Assuming text_input_index is the index for text tensor from input_details
14# text_input_tensor_index = input_details[text_input_index]['index']
15# text_input_shape = input_details[text_input_index]['shape']
16# max_seq_len = text_input_shape[1] # Assuming shape is [batch, seq_len]
17# texts = ["a photo of a cat", "a drawing of a dog"]
18# your_tokenizer = None # Load/initialize your specific tokenizer here
19#
20# for i, text_prompt in enumerate(texts):
21# tokenized_prompt = tokenize_text(text_prompt, your_tokenizer, max_seq_len)
22# # If model processes one text at a time, or if it batches texts (adapt accordingly)
23# interpreter.set_tensor(text_input_tensor_index, tokenized_prompt)
24# # Run inference for this text (or batch later)interpreter.invoke()1# Assuming image_output_index and text_output_index from output_details
2# image_output_tensor_index = output_details[image_output_index]['index']
3# text_output_tensor_index = output_details[text_output_index]['index']
4#
5# image_embedding = interpreter.get_tensor(image_output_tensor_index)
6# text_embedding = interpreter.get_tensor(text_output_tensor_index)
7#
8# print("Image Embedding Shape:", image_embedding.shape)
9# print("Text Embedding Shape:", text_embedding.shape)1# from sklearn.metrics.pairwise import cosine_similarity
2#
3# # Example: image_embedding from step 5, and multiple text_embeddings for labels
4# # text_embeddings_for_labels = np.array([...]) # Shape: (num_labels, embed_dim)
5# # similarities = cosine_similarity(image_embedding, text_embeddings_for_labels)
6# # predicted_label_index = np.argmax(similarities)
7# # your_labels = ["label1", "label2", ...]
8# # predicted_label = your_labels[predicted_label_index]
9# # print(f"Predicted label: {predicted_label}")1# # Get the runner for a specific signature
2# # Ensure you know the correct signature names for your model.
3# # image_runner = interpreter.get_signature_runner('serving_default_image_tower_signature_name') # Replace with actual name
4# # text_runner = interpreter.get_signature_runner('serving_default_text_tower_signature_name') # Replace with actual name
5
6# # For image feature extraction
7# # output_image = image_runner(name_of_input_image_tensor=preprocessed_image)
8# # image_embedding = output_image['name_of_output_image_feature_tensor']
9
10# # For text feature extraction
11# # output_text = text_runner(name_of_input_text_tensor=tokenized_prompt)
12# # text_embedding = output_text['name_of_output_text_feature_tensor']input_details, output_details, or any metadata associated with the TFLite model (e.g., using Netron app) to understand its specific input/output structure and signature names.
Refer to the original MobileCLIP project documentation for more precise details on preprocessing, tokenization, and model architecture if available.