Bingsu commited on
Commit
6898330
1 Parent(s): 50e6cd6

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +66 -0
README.md ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - clip
4
+ language: ko
5
+ license: mit
6
+ ---
7
+
8
+ # vitB32_bert_ko_small_clip
9
+
10
+ [openai/clip-vit-base-patch32](https://huggingface.co/openai/clip-vit-base-patch32) + [lassl/bert-ko-small](https://huggingface.co/lassl/bert-ko-small) CLIP Model
11
+
12
+ [training code(github)](https://github.com/Bing-su/KoCLIP_training_code)
13
+
14
+ ## Train
15
+
16
+ SBERT의 [Making Monolingual Sentence Embeddings Multilingual using Knowledge Distillation](https://arxiv.org/abs/2004.09813)를 참고하여, `openai/clip-vit-base-patch32` 텍스트 모델의 가중치를 `lassl/bert-ko-small`로 복제하였습니다. 논문과는 달리 mean pooling을 사용하지 않고, huggingface모델의 기본 pooling을 그대로 사용하였습니다.
17
+
18
+ 사용한 데이터: [Aihub 한국어-영어 번역(병렬) 말뭉치](https://aihub.or.kr/aidata/87)
19
+
20
+
21
+ ## How to Use
22
+
23
+ #### 1.
24
+
25
+ ```python
26
+ import requests
27
+ from PIL import Image
28
+ from transformers import VisionTextDualEncoderProcessor, VisionTextDualEncoderModel # or Auto...
29
+
30
+ model = VisionTextDualEncoderModel.from_pretrained("Bingsu/vitB32_bert_ko_small_clip")
31
+ processor = VisionTextDualEncoderProcessor.from_pretrained("Bingsu/vitB32_bert_ko_small_clip")
32
+
33
+ url = "http://images.cocodataset.org/val2017/000000039769.jpg"
34
+ image = Image.open(requests.get(url, stream=True).raw)
35
+
36
+ inputs = processor(text=["고양이 두 마리", "개 두 마리"], images=image, return_tensors="pt", padding=True)
37
+
38
+ outputs = model(**inputs)
39
+ logits_per_image = outputs.logits_per_image
40
+ probs = logits_per_image.softmax(dim=1)
41
+ ```
42
+
43
+ ```pycon
44
+ >>> probs
45
+ tensor([[0.9756, 0.0244]], grad_fn=<SoftmaxBackward0>)
46
+ ```
47
+
48
+ #### 2.
49
+
50
+ ```python
51
+ from transformers import AutoModel, AutoProcessor, pipeline
52
+
53
+ model = AutoModel.from_pretrained("Bingsu/vitB32_bert_ko_small_clip")
54
+ processor = AutoProcessor.from_pretrained("Bingsu/vitB32_bert_ko_small_clip")
55
+ pipe = pipeline("zero-shot-image-classification", model=model, feature_extractor=processor.feature_extractor, tokenizer=processor.tokenizer)
56
+
57
+ url = "http://images.cocodataset.org/val2017/000000039769.jpg"
58
+ result = pipe(images=url, candidate_labels=["고양이 한 마리", "고양이 두 마리", "고양이 두 마리와 리모컨 두 개"], hypothesis_template="{}")
59
+ ```
60
+
61
+ ```pycon
62
+ >>> result
63
+ [{'score': 0.871887743473053, 'label': '고양이 두 마리와 리모컨 두 개'},
64
+ {'score': 0.12316706776618958, 'label': '고양이 두 마리'},
65
+ {'score': 0.004945191089063883, 'label': '고양이 한 마리'}]
66
+ ```