Downloads · 30 days
0
yashm/clip-insect-sex
clip-insect-sex is a image classification model from yashm. Use it when you need a label for an image. It is set up for transformers. The card lists the license as other.
A custom CLIP-based binary classifier for insect sex detection (female vs male) trained on an imagefolder dataset with augmentation.
Downloads · 30 days
0
Access
Public
Updated Apr 11, 2026
Parameters
87.9M
351 MB on disk
Likes
1
Public
Click a slice to open those files.
.safetensors351 MB · 99%
From the Hugging Face model README
A custom CLIP-based binary classifier for insect sex detection (female vs male) trained on an imagefolder dataset with augmentation.
openai/clip-vit-base-patch32female, malepip install torch torchvision transformers huggingface_hub safetensors pillow numpy
import torch
import numpy as np
from PIL import Image
from torchvision import transforms
from torch import nn
from safetensors.torch import load_file
from huggingface_hub import hf_hub_download
from transformers import CLIPVisionModelWithProjection
class CLIPBinaryClassifier(nn.Module):
def __init__(self, clip_name: str, n_classes: int):
super().__init__()
self.vision = CLIPVisionModelWithProjection.from_pretrained(clip_name)
self.logit_scale = nn.Parameter(torch.tensor(np.log(100.0)))
self.prompt_vecs = nn.Parameter(
torch.empty(n_classes, self.vision.config.projection_dim)
)
def forward(self, pixel_values):
img_embeds = self.vision(pixel_values).image_embeds
txt_embeds = self.prompt_vecs
logits = torch.cosine_similarity(
img_embeds.unsqueeze(1), txt_embeds.unsqueeze(0), dim=-1
) * self.logit_scale.exp()
return {"logits": logits}
repo_id = "yashm/clip-insect-sex"
model = CLIPBinaryClassifier("openai/clip-vit-base-patch32", n_classes=2)
state_dict = load_file(
hf_hub_download(repo_id=repo_id, filename="model.safetensors", repo_type="model")
)
model.load_state_dict(state_dict, strict=True)
model.eval()
label_names = ["female", "male"]
img_size = 224
preprocess = transforms.Compose([
transforms.Resize((img_size, img_size)),
transforms.ToTensor(),
])
image = Image.open("path/to/image.jpg").convert("RGB")
x = preprocess(image).unsqueeze(0)
with torch.no_grad():
logits = model(x)["logits"]
probs = logits.softmax(-1).squeeze(0).cpu().numpy()
pred_idx = int(np.argmax(probs))
print("Prediction:", label_names[pred_idx])
print("Probabilities:", dict(zip(label_names, probs.round(4))))
data/ and augmented variants in augmented_data/.Gryllus bimaculatus.100.0%Train/Test = 192/48 (seed=42, test_size=0.2)This model card uses other. Confirm compatibility with base model and your intended redistribution.
If this model supports your work, cite your project/repository here.
240 images