Views
No views yet
1import requests
2import torch
3from PIL import Image
4from transformers import AutoModel, AutoProcessor
5
6repo = "Bingsu/clip-vit-base-patch32-ko"
7model = AutoModel.from_pretrained(repo)
8processor = AutoProcessor.from_pretrained(repo)
9
10url = "http://images.cocodataset.org/val2017/000000039769.jpg"
11image = Image.open(requests.get(url, stream=True).raw)
12inputs = processor(text=["고양이 두 마리", "개 두 마리"], images=image, return_tensors="pt", padding=True)
13with torch.inference_mode():
14 outputs = model(**inputs)
15logits_per_image = outputs.logits_per_image
16probs = logits_per_image.softmax(dim=1)1>>> probs
2tensor([[0.9926, 0.0074]])1from transformers import pipeline
2
3repo = "Bingsu/clip-vit-base-patch32-ko"
4pipe = pipeline("zero-shot-image-classification", model=repo)
5
6url = "http://images.cocodataset.org/val2017/000000039769.jpg"
7result = pipe(images=url, candidate_labels=["고양이 한 마리", "고양이 두 마리", "분홍색 소파에 드러누운 고양이 친구들"], hypothesis_template="{}")1>>> result
2[{'score': 0.9456236958503723, 'label': '분홍색 소파에 드러누운 고양이 친구들'},
3 {'score': 0.05315302312374115, 'label': '고양이 두 마리'},
4 {'score': 0.0012233294546604156, 'label': '고양이 한 마리'}].train_new_from_iterator를 통해 학습되었습니다.1 # text_embeds.shape = [batch_size, sequence_length, transformer.width]
2 # take features from the eot embedding (eot_token is the highest number in each sequence)
3 # casting to torch.int for onnx compatibility: argmax doesn't support int64 inputs with opset 14
4 pooled_output = last_hidden_state[
5 torch.arange(last_hidden_state.shape[0]), input_ids.to(torch.int).argmax(dim=-1)
6 ]pooled_output을 구할때 id가 가장 큰 토큰을 사용하기 때문에, eos 토큰은 가장 마지막 토큰이 되어야 합니다.