skgreenstar15 commited on
Commit
e613065
ยท
1 Parent(s): 4d9b49c

first commit

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