Chesscorner commited on
Commit
5df0c51
·
verified ·
1 Parent(s): 4777031

Update caption.py

Browse files
Files changed (1) hide show
  1. caption.py +18 -11
caption.py CHANGED
@@ -6,21 +6,28 @@ from PIL import Image
6
  model = AutoModelForCausalLM.from_pretrained("Chesscorner/git-chess-v3")
7
  processor = AutoProcessor.from_pretrained("Chesscorner/git-chess-v3")
8
 
9
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 
10
  model.to(device)
11
 
12
- max_length = 60
13
- num_beams = 2
14
- gen_kwargs = {'max_length': max_length, 'num_beams': num_beams}
 
15
 
 
 
 
 
 
16
  def predict_step(image):
17
- pixel_values = processor(
18
- images=[image], return_tensors='pt').pixel_values
19
- pixel_values = pixel_values.to(device)
20
 
21
- output_ids = model.generate(pixel_values, **gen_kwargs)
 
 
22
 
 
23
  preds = processor.batch_decode(output_ids, skip_special_tokens=True)
24
- preds = [pred.strip() for pred in preds]
25
- return preds[0]
26
-
 
6
  model = AutoModelForCausalLM.from_pretrained("Chesscorner/git-chess-v3")
7
  processor = AutoProcessor.from_pretrained("Chesscorner/git-chess-v3")
8
 
9
+ # Set up device and move model to it
10
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
  model.to(device)
12
 
13
+ # Enable mixed precision if on GPU
14
+ use_fp16 = device.type == "cuda"
15
+ if use_fp16:
16
+ model.half()
17
 
18
+ # Set generation parameters
19
+ gen_kwargs = {'max_length': 10, 'num_beams': 2} # Adjust num_beams if needed
20
+
21
+
22
+ # Prediction function
23
  def predict_step(image):
24
+ # Preprocess the image
25
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values.to(device)
 
26
 
27
+ # Generate predictions with no_grad for efficiency
28
+ with torch.no_grad():
29
+ output_ids = model.generate(pixel_values=pixel_values, **gen_kwargs)
30
 
31
+ # Decode predictions
32
  preds = processor.batch_decode(output_ids, skip_special_tokens=True)
33
+ return preds[0].strip()