pollitoconpapass commited on
Commit
3378873
·
verified ·
1 Parent(s): 5bbc309

Added Implementation Script

Browse files
Files changed (1) hide show
  1. README.md +76 -0
README.md CHANGED
@@ -19,3 +19,79 @@ language:
19
  This mllama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
20
 
21
  [<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  This mllama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
20
 
21
  [<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
22
+
23
+
24
+ # Implementation
25
+ ```py
26
+ from datasets import load_dataset
27
+ from unsloth import FastVisionModel
28
+
29
+ model, tokenizer = FastVisionModel.from_pretrained(
30
+ # "unsloth/Llama-3.2-11B-Vision-Instruct",
31
+ "pollitoconpapass/Llama-3.2-11B-Vision-Radiology-mini",
32
+ load_in_4bit = True, # Use 4bit to reduce memory use. False for 16bit LoRA.
33
+ use_gradient_checkpointing = "unsloth", # True or "unsloth" for long context
34
+ )
35
+
36
+ model = FastVisionModel.get_peft_model(
37
+ model,
38
+ finetune_vision_layers = True, # False if not finetuning vision layers
39
+ finetune_language_layers = True, # False if not finetuning language layers
40
+ finetune_attention_modules = True, # False if not finetuning attention layers
41
+ finetune_mlp_modules = True, # False if not finetuning MLP layers
42
+
43
+ r = 16, # The larger, the higher the accuracy, but might overfit
44
+ lora_alpha = 16, # Recommended alpha == r at least
45
+ lora_dropout = 0,
46
+ bias = "none",
47
+ random_state = 3407,
48
+ use_rslora = False, # We support rank stabilized LoRA
49
+ loftq_config = None, # And LoftQ
50
+ # target_modules = "all-linear", # Optional now! Can specify a list if needed
51
+ )
52
+
53
+ dataset = load_dataset("unsloth/Radiology_mini", split = "train")
54
+ instruction = "You are an expert radiographer. Describe accurately what you see in this image."
55
+
56
+ def convert_to_conversation(sample):
57
+ conversation = [
58
+ { "role": "user",
59
+ "content" : [
60
+ {"type" : "text", "text" : instruction},
61
+ {"type" : "image", "image" : sample["image"]} ]
62
+ },
63
+ { "role" : "assistant",
64
+ "content" : [
65
+ {"type" : "text", "text" : sample["caption"]} ]
66
+ },
67
+ ]
68
+ return { "messages" : conversation }
69
+ pass
70
+
71
+ converted_dataset = [convert_to_conversation(sample) for sample in dataset]
72
+
73
+ FastVisionModel.for_inference(model) # Enable for inference!
74
+
75
+ image = dataset[0]["image"]
76
+ instruction = "You are an expert radiographer. Describe accurately what you see in this image."
77
+
78
+ messages = [
79
+ {"role": "user", "content": [
80
+ {"type": "image"},
81
+ {"type": "text", "text": instruction}
82
+ ]}
83
+ ]
84
+ input_text = tokenizer.apply_chat_template(messages, add_generation_prompt = True)
85
+ inputs = tokenizer(
86
+ image,
87
+ input_text,
88
+ add_special_tokens = False,
89
+ return_tensors = "pt",
90
+ ).to("cuda")
91
+
92
+ from transformers import TextStreamer
93
+ text_streamer = TextStreamer(tokenizer, skip_prompt = True)
94
+ _ = model.generate(**inputs, streamer = text_streamer, max_new_tokens = 500,
95
+ use_cache = True, temperature = 1.5, min_p = 0.1)
96
+
97
+ ```