Spaces:
Sleeping
Sleeping
feat: initial commit
Browse files- .gitignore +1 -0
- app.py +68 -0
- best_cpu.pt +3 -0
- examples/back.jpg +0 -0
- examples/front.jpg +0 -0
- examples/other.jpg +0 -0
- examples/page.jpg +0 -0
- examples/page2.jpg +0 -0
- labels.txt +3 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
__pycache__
|
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torchvision import transforms
|
3 |
+
import gradio as gr
|
4 |
+
import timm
|
5 |
+
|
6 |
+
# Read the categories
|
7 |
+
with open("labels.txt", "r") as f:
|
8 |
+
categories = [s.strip() for s in f.readlines()]
|
9 |
+
|
10 |
+
model_ft = timm.create_model('vit_base_patch16_224_in21k', pretrained=True, num_classes=len(categories))
|
11 |
+
model_path = 'best_cpu.pt'
|
12 |
+
model_ft.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
13 |
+
model_ft.eval()
|
14 |
+
|
15 |
+
|
16 |
+
# Download an example image from the pytorch website
|
17 |
+
# torch.hub.download_url_to_file("https://iiif.dl.itc.u-tokyo.ac.jp/iiif/genji/TIFF/A00_6587/01/01_0001.tif/full/,400/0/default.jpg", "examples/other.jpg")
|
18 |
+
# torch.hub.download_url_to_file("https://iiif.dl.itc.u-tokyo.ac.jp/iiif/genji/TIFF/A00_6587/01/01_0002.tif/full/,400/0/default.jpg", "examples/front.jpg")
|
19 |
+
# torch.hub.download_url_to_file("https://iiif.dl.itc.u-tokyo.ac.jp/iiif/genji/TIFF/A00_6587/01/01_0003.tif/full/,400/0/default.jpg", "examples/page.jpg")
|
20 |
+
# torch.hub.download_url_to_file("https://iiif.dl.itc.u-tokyo.ac.jp/iiif/genji/TIFF/A00_6587/01/01_0009.tif/full/,400/0/default.jpg", "examples/page2.jpg")
|
21 |
+
# torch.hub.download_url_to_file("https://iiif.dl.itc.u-tokyo.ac.jp/iiif/genji/TIFF/A00_6587/01/01_0032.tif/full/,400/0/default.jpg", "examples/back.jpg")
|
22 |
+
|
23 |
+
|
24 |
+
def inference(input_image):
|
25 |
+
preprocess = transforms.Compose([
|
26 |
+
transforms.Resize(224),
|
27 |
+
transforms.CenterCrop(224),
|
28 |
+
transforms.ToTensor(),
|
29 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
30 |
+
])
|
31 |
+
input_tensor = preprocess(input_image)
|
32 |
+
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
33 |
+
|
34 |
+
# move the input and model to GPU for speed if available
|
35 |
+
if torch.cuda.is_available():
|
36 |
+
input_batch = input_batch.to('cuda')
|
37 |
+
model_ft.to('cuda')
|
38 |
+
|
39 |
+
with torch.no_grad():
|
40 |
+
output = model_ft(input_batch)
|
41 |
+
# The output has unnormalized scores. To get probabilities, you can run a softmax on it.
|
42 |
+
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
43 |
+
|
44 |
+
|
45 |
+
# Show top categories per image
|
46 |
+
top5_prob, top5_catid = torch.topk(probabilities, len(categories))
|
47 |
+
result = {}
|
48 |
+
for i in range(top5_prob.size(0)):
|
49 |
+
result[categories[top5_catid[i]]] = top5_prob[i].item()
|
50 |
+
return result
|
51 |
+
|
52 |
+
inputs = gr.inputs.Image(type='pil')
|
53 |
+
outputs = gr.outputs.Label(type="confidences",num_top_classes=len(categories))
|
54 |
+
|
55 |
+
title = "表紙・裏表紙・その他のページの分類"
|
56 |
+
description = "Vision Transformerを用いた表紙・裏表紙・その他のページの分類モデルです。"
|
57 |
+
|
58 |
+
article = "<p style='text-align: center'>次のデータセットを使用しました。<a href='' target='_blank'>あああ</a></p>"
|
59 |
+
|
60 |
+
examples = [
|
61 |
+
['examples/other.jpg'],
|
62 |
+
['examples/front.jpg'],
|
63 |
+
["examples/page.jpg"],
|
64 |
+
["examples/page2.jpg"],
|
65 |
+
["examples/back.jpg"]
|
66 |
+
]
|
67 |
+
|
68 |
+
gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch()
|
best_cpu.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f3ab2e5acd80138398b765a0ab0f68cdda324e25fb311af590c6c21fe788c3b3
|
3 |
+
size 343257169
|
examples/back.jpg
ADDED
![]() |
examples/front.jpg
ADDED
![]() |
examples/other.jpg
ADDED
![]() |
examples/page.jpg
ADDED
![]() |
examples/page2.jpg
ADDED
![]() |
labels.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
back
|
2 |
+
front
|
3 |
+
page
|