jieun commited on
Commit
215e205
ยท
1 Parent(s): 1cdaf87
Files changed (4) hide show
  1. .gitignore +1 -0
  2. get-age.py +49 -0
  3. requirements.txt +8 -0
  4. sample.py +28 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .venv*
get-age.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import torch
3
+ from io import BytesIO
4
+ from PIL import Image
5
+ from transformers import ViTImageProcessor, ViTForImageClassification
6
+ import streamlit as st
7
+
8
+ # @st.cache_data
9
+ def get_model_transformers():
10
+ # Init model, transforms
11
+ model = ViTForImageClassification.from_pretrained('nateraw/vit-age-classifier')
12
+ transforms = ViTImageProcessor.from_pretrained('nateraw/vit-age-classifier')
13
+
14
+ return model, transforms
15
+
16
+ st.title("๋‚˜์ด๋ฅผ ์˜ˆ์ธกํ•ด๋ด…์‹œ๋‹ค!")
17
+ uploaded_file = st.file_uploader("๋‚˜์ด๋ฅผ ์˜ˆ์ธกํ•  ์‚ฌ๋žŒ์˜ ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜์„ธ์š”.", type=["jpg"])
18
+ if uploaded_file:
19
+ # print(f"uploaded file: {uploaded_file}")
20
+ st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
21
+
22
+ # Get example image from official fairface repo + read it in as an image
23
+ # r = requests.get('https://github.com/dchen236/FairFace/blob/master/detected_faces/race_Asian_face0.jpg?raw=true')
24
+ # im = Image.open(BytesIO(r.content))
25
+
26
+ im = Image.open(uploaded_file)
27
+
28
+ model, transforms = get_model_transformers()
29
+
30
+ # Transform our image and pass it through the model
31
+ inputs = transforms(im, return_tensors='pt')
32
+ output = model(**inputs)
33
+
34
+ # Predicted Class probabilities
35
+ proba = output.logits.softmax(1)
36
+
37
+ values, indices = torch.topk(proba, k=5)
38
+
39
+ result_dict = {model.config.id2label[i.item()]: v.item() for i, v in zip(indices.numpy()[0], values.detach().numpy()[0])}
40
+ first_result = list(result_dict.keys())[0]
41
+
42
+ print(f'predicted result:{result_dict}')
43
+ print(f'first_result: {first_result}')
44
+
45
+ st.header('๊ฒฐ๊ณผ')
46
+ st.subheader(f'์˜ˆ์ธก๋œ ๋‚˜์ด๋Š” {first_result} ์ž…๋‹ˆ๋‹ค')
47
+
48
+ for key, value in result_dict.items():
49
+ st.write(f'{key}: {value * 100:.2f}%')
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ transformers
2
+ pillow
3
+ streamlit
4
+
5
+ # https://pytorch.org/
6
+ torch
7
+ torchvision
8
+ torchaudio
sample.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import torch
3
+ from io import BytesIO
4
+ from PIL import Image
5
+ from transformers import ViTImageProcessor, ViTForImageClassification
6
+
7
+ # Get example image from official fairface repo + read it in as an image
8
+ r = requests.get('https://github.com/dchen236/FairFace/blob/master/detected_faces/race_Asian_face0.jpg?raw=true')
9
+ im = Image.open(BytesIO(r.content))
10
+
11
+ # Init model, transforms
12
+ model = ViTForImageClassification.from_pretrained('nateraw/vit-age-classifier')
13
+ transforms = ViTImageProcessor.from_pretrained('nateraw/vit-age-classifier')
14
+
15
+ # Transform our image and pass it through the model
16
+ inputs = transforms(im, return_tensors='pt')
17
+ output = model(**inputs)
18
+
19
+ # Predicted Class probabilities
20
+ proba = output.logits.softmax(1)
21
+
22
+ values, indices = torch.topk(proba, k=5)
23
+
24
+ result_dict = {model.config.id2label[i.item()]: v.item() for i, v in zip(indices.numpy()[0], values.detach().numpy()[0])}
25
+ first_result = list(result_dict.keys())[0]
26
+
27
+ print(f'predicted result:{result_dict}')
28
+ print(f'first_result: {first_result}')