Views
No views yet
transformers1from transformers import CLIPProcessor, CLIPModel
2from PIL import Image
3import torch
4
5# Load model and processor
6model = CLIPModel.from_pretrained("BiliSakura/Remote-CLIP-ViT-L-14")
7processor = CLIPProcessor.from_pretrained("BiliSakura/Remote-CLIP-ViT-L-14")
8
9# Load and process image
10image = Image.open("path/to/your/image.jpg")
11inputs = processor(
12 text=["a photo of a building", "a photo of vegetation", "a photo of water"],
13 images=image,
14 return_tensors="pt",
15 padding=True
16)
17
18# Get image-text similarity scores
19with torch.inference_mode():
20 outputs = model(**inputs)
21 logits_per_image = outputs.logits_per_image
22 probs = logits_per_image.softmax(dim=1)
23
24print(f"Similarity scores: {probs}")1from transformers import CLIPProcessor, CLIPModel
2from PIL import Image
3import torch
4
5model = CLIPModel.from_pretrained("BiliSakura/Remote-CLIP-ViT-L-14")
6processor = CLIPProcessor.from_pretrained("BiliSakura/Remote-CLIP-ViT-L-14")
7
8# Define candidate labels
9candidate_labels = [
10 "a satellite image of urban area",
11 "a satellite image of forest",
12 "a satellite image of agricultural land",
13 "a satellite image of water body"
14]
15
16image = Image.open("path/to/your/image.jpg")
17inputs = processor(
18 text=candidate_labels,
19 images=image,
20 return_tensors="pt",
21 padding=True
22)
23
24with torch.inference_mode():
25 outputs = model(**inputs)
26 probs = outputs.logits_per_image.softmax(dim=1)
27
28# Get the predicted label
29predicted_idx = probs.argmax().item()
30print(f"Predicted label: {candidate_labels[predicted_idx]}")
31print(f"Confidence: {probs[0][predicted_idx]:.4f}")1from transformers import CLIPProcessor, CLIPModel
2from PIL import Image
3import torch
4
5model = CLIPModel.from_pretrained("BiliSakura/Remote-CLIP-ViT-L-14")
6processor = CLIPProcessor.from_pretrained("BiliSakura/Remote-CLIP-ViT-L-14")
7
8# Get image features only
9image = Image.open("path/to/your/image.jpg")
10image_inputs = processor(images=image, return_tensors="pt")
11
12with torch.inference_mode():
13 image_features = model.get_image_features(**image_inputs)
14
15# Get text features only
16text_inputs = processor(
17 text=["a satellite image of urban area"],
18 return_tensors="pt",
19 padding=True,
20 truncation=True
21)
22
23with torch.inference_mode():
24 text_features = model.get_text_features(**text_inputs)
25
26print(f"Image features shape: {image_features.shape}")
27print(f"Text features shape: {text_features.shape}")diffusers1from diffusers import StableDiffusionPipeline
2from transformers import CLIPTextModel, CLIPTokenizer
3import torch
4
5# Load the text encoder and tokenizer
6text_encoder = CLIPTextModel.from_pretrained(
7 "BiliSakura/Remote-CLIP-ViT-L-14/diffusers",
8 subfolder="text_encoder",
9 torch_dtype=torch.float16
10)
11tokenizer = CLIPTokenizer.from_pretrained(
12 "BiliSakura/Remote-CLIP-ViT-L-14"
13)
14
15# Encode text prompt
16prompt = "a satellite image of a city with buildings and roads"
17text_inputs = tokenizer(
18 prompt,
19 padding="max_length",
20 max_length=77,
21 truncation=True,
22 return_tensors="pt"
23)
24
25with torch.inference_mode():
26 text_outputs = text_encoder(text_inputs.input_ids)
27 text_embeddings = text_outputs.last_hidden_state
28
29print(f"Text embeddings shape: {text_embeddings.shape}")1from diffusers import StableDiffusionPipeline
2import torch
3
4# Load pipeline with custom text encoder
5pipe = StableDiffusionPipeline.from_pretrained(
6 "runwayml/stable-diffusion-v1-5",
7 text_encoder=text_encoder,
8 tokenizer=tokenizer,
9 torch_dtype=torch.float16
10)
11pipe = pipe.to("cuda")
12
13# Generate image
14prompt = "a high-resolution satellite image of urban area"
15image = pipe(prompt, num_inference_steps=50, guidance_scale=7.5).images[0]
16image.save("generated_image.png")1@article{remoteclip,
2 author = {Fan Liu and
3 Delong Chen and
4 Zhangqingyun Guan and
5 Xiaocong Zhou and
6 Jiale Zhu and
7 Qiaolin Ye and
8 Liyong Fu and
9 Jun Zhou},
10 title = {RemoteCLIP: {A} Vision Language Foundation Model for Remote Sensing},
11 journal = {{IEEE} Transactions on Geoscience and Remote Sensing},
12 volume = {62},
13 pages = {1--16},
14 year = {2024},
15 url = {https://doi.org/10.1109/TGRS.2024.3390838},
16 doi = {10.1109/TGRS.2024.3390838},
17}