BK-Lee commited on
Commit
1bd231e
1 Parent(s): 065bb14

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +62 -3
README.md CHANGED
@@ -1,3 +1,62 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ ---
4
+ # Two steps only need.
5
+
6
+ First step.
7
+ ```shell
8
+ git clone https://github.com/ByungKwanLee/TroL
9
+ bash install
10
+ ```
11
+
12
+ Second step.
13
+ ```python
14
+ import torch
15
+ from config import *
16
+ from PIL import Image
17
+ from utils.utils import *
18
+ import torch.nn.functional as F
19
+ from trol.load_trol import load_trol
20
+ from torchvision.transforms.functional import pil_to_tensor
21
+
22
+ # model selection
23
+ link = "TroL-3.8B" # [Select One] 'TroL-1.8B' | 'TroL-3.8B' | 'TroL-7B'
24
+
25
+ # User prompt
26
+ prompt_type="with_image" # Select one option "text_only", "with_image"
27
+ img_path='figures/demo.png'
28
+ question="What is the troll doing? Provide the detail in the image and imagine what the event happens."
29
+
30
+ # loading model
31
+ model, tokenizer = load_trol(link=link)
32
+
33
+ # cpu -> gpu
34
+ for param in model.parameters():
35
+ if not param.is_cuda:
36
+ param.data = param.to('cuda:0')
37
+
38
+ # prompt type -> input prompt
39
+ image_token_number = None
40
+ if prompt_type == 'with_image':
41
+ # Image Load
42
+ image = pil_to_tensor(Image.open(img_path).convert("RGB"))
43
+ if not "3.8B" in link:
44
+ image_token_number = 1225
45
+ image = F.interpolate(image.unsqueeze(0), size=(490, 490), mode='bicubic').squeeze(0)
46
+ inputs = [{'image': image, 'question': question}]
47
+ elif prompt_type=='text_only':
48
+ inputs = [{'question': question}]
49
+
50
+ # Generate
51
+ with torch.inference_mode():
52
+ _inputs = model.eval_process(inputs=inputs,
53
+ data='demo',
54
+ tokenizer=tokenizer,
55
+ device='cuda:0',
56
+ img_token_number=image_token_number)
57
+ generate_ids = model.generate(**_inputs, max_new_tokens=256, use_cache=True)
58
+ response = output_filtering(tokenizer.batch_decode(generate_ids, skip_special_tokens=False)[0], model)
59
+ print(response)
60
+ ```
61
+
62
+ So easy Let's say TroL!