kaab4321 commited on
Commit
03c3d8d
·
verified ·
1 Parent(s): aaccce8

Update flask_app.py

Browse files
Files changed (1) hide show
  1. flask_app.py +10 -7
flask_app.py CHANGED
@@ -147,26 +147,29 @@ def img_text():
147
 
148
  sentences = data['sentences']
149
  print(sentences)
150
- device = torch.device('cpu')
151
 
152
  image_size = 384
153
- image = load_demo_image(image_size=image_size, device=device, img_url = image_url)
154
 
155
 
156
  model = blip_itm(pretrained=model_url, image_size=image_size, vit='base')
157
  model.eval()
 
158
  #model = model.to(device='cpu')
159
  #model = model.to('cuda')
160
 
161
  #caption = 'a cute kitten with orange color'
162
  #print('text: %s' %sentences)
163
  score = []
 
164
  for sentence in sentences:
165
- itm_output = model(image,sentence,match_head='itm')
166
- #itm_score = torch.nn.functional.softmax(itm_output,dim=1)[:,1]
167
- itm_score = torch.nn.functional.softmax(itm_output, dim=1)[:, 1].item() # Convert tensor to Python float
168
- print('The image and text is matched with a probability of %.4f'%itm_score)
169
- score.append(itm_score)
 
 
170
 
171
  del model, image
172
  return jsonify(scores=score)
 
147
 
148
  sentences = data['sentences']
149
  print(sentences)
 
150
 
151
  image_size = 384
152
+ image = load_demo_image(image_size=image_size,device=device, img_url = image_url)
153
 
154
 
155
  model = blip_itm(pretrained=model_url, image_size=image_size, vit='base')
156
  model.eval()
157
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
158
  #model = model.to(device='cpu')
159
  #model = model.to('cuda')
160
 
161
  #caption = 'a cute kitten with orange color'
162
  #print('text: %s' %sentences)
163
  score = []
164
+
165
  for sentence in sentences:
166
+ with torch.no_grad():
167
+ itm_output = model(image, sentence, match_head='itm')
168
+ itm_score = torch.nn.functional.softmax(itm_output, dim=1)[:, 1].item()
169
+ score.append(itm_score)
170
+
171
+ del itm_output
172
+ torch.cuda.empty_cache() # Only needed if running on CUDA
173
 
174
  del model, image
175
  return jsonify(scores=score)