MinxuanQin commited on
Commit
c1b68f7
1 Parent(s): a450d5c

add sample images and rewrite app.py

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
app.py CHANGED
@@ -3,6 +3,7 @@ sys.path.append(".")
3
 
4
  import streamlit as st
5
  import pandas as pd
 
6
 
7
  from model_loader import *
8
  from datasets import load_dataset
@@ -10,7 +11,8 @@ from datasets import load_dataset
10
 
11
  # load dataset
12
  #ds = load_dataset("test")
13
- ds = load_dataset("HuggingFaceM4/VQAv2", split="validation", cache_dir="cache", streaming=False)
 
14
 
15
  # define selector
16
  model_name = st.sidebar.selectbox(
@@ -20,17 +22,23 @@ model_name = st.sidebar.selectbox(
20
 
21
  image_selector_unspecific = st.number_input(
22
  "Select an image id: ",
23
- 0, len(ds)
24
  )
25
 
26
  # select and display
27
- sample = ds[image_selector_unspecific]
28
- image = sample['image']
29
- image
 
 
 
 
 
30
 
31
  # inference
32
  question = st.text_input(f"Ask the model a question related to the image: \n"
33
  f"(e.g. \"{sample['question']}\")")
34
  args = load_model(model_name) # TODO: cache
35
  answer = get_answer(args, image, question, model_name)
36
- st.write("answer")
 
 
3
 
4
  import streamlit as st
5
  import pandas as pd
6
+ from PIL import Image
7
 
8
  from model_loader import *
9
  from datasets import load_dataset
 
11
 
12
  # load dataset
13
  #ds = load_dataset("test")
14
+ # ds = load_dataset("HuggingFaceM4/VQAv2", split="validation", cache_dir="cache", streaming=False)
15
+ df = pd.read_json('vqa_samples.json', orient="columns")
16
 
17
  # define selector
18
  model_name = st.sidebar.selectbox(
 
22
 
23
  image_selector_unspecific = st.number_input(
24
  "Select an image id: ",
25
+ 0, len(df)
26
  )
27
 
28
  # select and display
29
+ #sample = ds[image_selector_unspecific]
30
+ sample = df.iloc[image_selector_unspecific]
31
+ img_path = sample['img_path']
32
+ image = Image.open(f'images/{img_path}.jpg')
33
+
34
+ st.image(image, channels="RGB")
35
+ question = sample['ques']
36
+ label = sample['label']
37
 
38
  # inference
39
  question = st.text_input(f"Ask the model a question related to the image: \n"
40
  f"(e.g. \"{sample['question']}\")")
41
  args = load_model(model_name) # TODO: cache
42
  answer = get_answer(args, image, question, model_name)
43
+ st.text(f"Answer by {model_name}: {answer}")
44
+ st.text(f"Ground truth: {label}")
extract_100_samples.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import torch
3
+ from datasets import load_dataset, get_dataset_split_names
4
+ import pandas as pd
5
+ dataset = load_dataset("HuggingFaceM4/VQAv2", split="validation", cache_dir="cache", streaming=False)
6
+
7
+ index_range = 1000
8
+
9
+ ## can see index here
10
+ df = pd.DataFrame(columns=['ques', 'label', 'q_id', 'img_path', 'question_type'])
11
+
12
+ for idx in range(index_range):
13
+ sample = dataset[idx]
14
+ ques = sample['question']
15
+ img = sample['image']
16
+ img_id = sample['image_id']
17
+ img.save(f'images/{img_id}.jpg')
18
+ label = sample['multiple_choice_answer']
19
+ q_id = sample['question_id']
20
+ q_type = sample['question_type']
21
+ df.loc[len(df.index)] = [ques, label, q_id, img_id, q_type]
22
+
23
+ df.to_json('vqa_samples.json', orient='columns')
images/131089.jpg ADDED
images/131108.jpg ADDED
images/131115.jpg ADDED
images/131131.jpg ADDED
images/131152.jpg ADDED
images/131159.jpg ADDED
images/131171.jpg ADDED
images/131207.jpg ADDED
images/131273.jpg ADDED
images/131276.jpg ADDED
images/131280.jpg ADDED
images/131282.jpg ADDED
images/131295.jpg ADDED
images/131335.jpg ADDED
images/131386.jpg ADDED
images/131390.jpg ADDED
images/131416.jpg ADDED
images/131418.jpg ADDED
images/131444.jpg ADDED
images/131453.jpg ADDED
images/131493.jpg ADDED
images/131494.jpg ADDED
images/131497.jpg ADDED
images/131516.jpg ADDED
images/131527.jpg ADDED
images/131539.jpg ADDED
images/133.jpg ADDED
images/136.jpg ADDED
images/139.jpg ADDED
images/143.jpg ADDED
images/164.jpg ADDED
images/192.jpg ADDED
images/196.jpg ADDED
images/21900.jpg ADDED
images/240301.jpg ADDED
images/240323.jpg ADDED
images/241.jpg ADDED
images/257.jpg ADDED
images/262148.jpg ADDED
images/262161.jpg ADDED
images/262162.jpg ADDED
images/262175.jpg ADDED
images/262189.jpg ADDED
images/262197.jpg ADDED
images/262200.jpg ADDED
images/262227.jpg ADDED
images/262228.jpg ADDED
images/262229.jpg ADDED