Image-Text-to-Text
Transformers
Safetensors
English
moondream1
text-generation
moondream2
VLM
custom_code
Instructions to use Subh775/Perception-moondream2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Subh775/Perception-moondream2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="Subh775/Perception-moondream2", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("Subh775/Perception-moondream2", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Subh775/Perception-moondream2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Subh775/Perception-moondream2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Subh775/Perception-moondream2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/Subh775/Perception-moondream2
- SGLang
How to use Subh775/Perception-moondream2 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "Subh775/Perception-moondream2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Subh775/Perception-moondream2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "Subh775/Perception-moondream2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Subh775/Perception-moondream2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use Subh775/Perception-moondream2 with Docker Model Runner:
docker model run hf.co/Subh775/Perception-moondream2
Add region_model.py for self-contained custom code
Browse files- region_model.py +43 -0
region_model.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from .fourier_features import FourierFeatures
|
| 4 |
+
|
| 5 |
+
class RegionModel(nn.Module):
|
| 6 |
+
def __init__(self):
|
| 7 |
+
super().__init__()
|
| 8 |
+
|
| 9 |
+
self.position_features = FourierFeatures(2, 256)
|
| 10 |
+
self.position_encoder = nn.Linear(256, 2048)
|
| 11 |
+
self.size_features = FourierFeatures(2, 256)
|
| 12 |
+
self.size_encoder = nn.Linear(256, 2048)
|
| 13 |
+
|
| 14 |
+
self.position_decoder = nn.Linear(2048, 2)
|
| 15 |
+
self.size_decoder = nn.Linear(2048, 2)
|
| 16 |
+
self.confidence_decoder = nn.Linear(2048, 1)
|
| 17 |
+
|
| 18 |
+
def encode_position(self, position):
|
| 19 |
+
return self.position_encoder(self.position_features(position))
|
| 20 |
+
|
| 21 |
+
def encode_size(self, size):
|
| 22 |
+
return self.size_encoder(self.size_features(size))
|
| 23 |
+
|
| 24 |
+
def decode_position(self, x):
|
| 25 |
+
return self.position_decoder(x)
|
| 26 |
+
|
| 27 |
+
def decode_size(self, x):
|
| 28 |
+
return self.size_decoder(x)
|
| 29 |
+
|
| 30 |
+
def decode_confidence(self, x):
|
| 31 |
+
return self.confidence_decoder(x)
|
| 32 |
+
|
| 33 |
+
def encode(self, position, size):
|
| 34 |
+
return torch.stack(
|
| 35 |
+
[self.encode_position(position), self.encode_size(size)], dim=0
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
def decode(self, position_logits, size_logits):
|
| 39 |
+
return (
|
| 40 |
+
self.decode_position(position_logits),
|
| 41 |
+
self.decode_size(size_logits),
|
| 42 |
+
self.decode_confidence(size_logits),
|
| 43 |
+
)
|