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