Spaces:
Runtime error
Runtime error
mindwrapped
commited on
Commit
•
99f86a4
1
Parent(s):
4cb9e3b
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,55 @@
|
|
1 |
import numpy as np
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import from_pretrained_fastai
|
|
|
|
|
4 |
|
5 |
learn = from_pretrained_fastai('mindwrapped/pokemon-card-checker')
|
6 |
|
7 |
def check_card(img):
|
8 |
pred_label, _, scores = learn.predict(img)
|
9 |
scores = scores.detach().numpy()
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
|
13 |
demo = gr.Interface(
|
14 |
fn=check_card,
|
15 |
-
inputs=
|
16 |
-
outputs="label",
|
17 |
examples=['real-1.jpeg','real-2.jpeg','fake-1.jpeg','fake-2.jpeg','real-3.jpeg','real-4.jpeg','fake-3.jpeg','fake-4.jpeg'],
|
18 |
title='Pokemon Card Checker',
|
19 |
-
description='A resnet34 model fine-tuned to determine whether Pokemon cards are real or fake. \n\n[Dataset](https://www.kaggle.com/datasets/ongshujian/real-and-fake-pokemon-cards) created by [Shujian Ong](https://www.kaggle.com/ongshujian).',
|
20 |
-
article='Can you guess which cards are real and fake? \n\nI can\'t
|
21 |
live=False,
|
22 |
)
|
23 |
|
24 |
-
demo.launch()
|
|
|
1 |
import numpy as np
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import from_pretrained_fastai
|
4 |
+
from lime import lime_image
|
5 |
+
from skimage.segmentation import mark_boundaries
|
6 |
|
7 |
learn = from_pretrained_fastai('mindwrapped/pokemon-card-checker')
|
8 |
|
9 |
def check_card(img):
|
10 |
pred_label, _, scores = learn.predict(img)
|
11 |
scores = scores.detach().numpy()
|
12 |
+
scores = {'real': float(scores[1]), 'fake': float(scores[0])}
|
13 |
+
|
14 |
+
print(np.array(img).shape)
|
15 |
+
|
16 |
+
# Lime Explanation
|
17 |
+
explainer = lime_image.LimeImageExplainer()
|
18 |
+
explanation = explainer.explain_instance(
|
19 |
+
np.array(img),
|
20 |
+
classifier_fn=classify_cards,
|
21 |
+
labels=['fake', 'real'],
|
22 |
+
num_samples=100,
|
23 |
+
random_seed=42,
|
24 |
+
)
|
25 |
+
|
26 |
+
temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=False, num_features=10, hide_rest=False)
|
27 |
+
img_boundry = mark_boundaries(temp/255.0, mask)
|
28 |
+
return scores, img_boundry
|
29 |
+
|
30 |
+
def classify_cards(imgs):
|
31 |
+
print(imgs.shape)
|
32 |
+
scores = []
|
33 |
+
|
34 |
+
for i in range(imgs.shape[0]):
|
35 |
+
pred_label, _, score = learn.predict(imgs[i])
|
36 |
+
scores.append(score.detach().numpy())
|
37 |
+
|
38 |
+
scores = np.array(scores)
|
39 |
+
print(scores.shape)
|
40 |
+
|
41 |
+
return scores
|
42 |
|
43 |
|
44 |
demo = gr.Interface(
|
45 |
fn=check_card,
|
46 |
+
inputs='image',
|
47 |
+
outputs=["label", "image"],
|
48 |
examples=['real-1.jpeg','real-2.jpeg','fake-1.jpeg','fake-2.jpeg','real-3.jpeg','real-4.jpeg','fake-3.jpeg','fake-4.jpeg'],
|
49 |
title='Pokemon Card Checker',
|
50 |
+
description='A resnet34 model fine-tuned to determine whether Pokemon cards are real or fake. \n\nAdded [LIME](https://github.com/marcotcr/lime) to show what contributed to the predicted label. \n\n[Dataset](https://www.kaggle.com/datasets/ongshujian/real-and-fake-pokemon-cards) created by [Shujian Ong](https://www.kaggle.com/ongshujian).',
|
51 |
+
article='Can you guess which cards are real and fake? \n\nI can\'t :D \n\n([View Labels](https://gist.github.com/mindwrapped/e5aad747757ef006037a1a1982be34fc)) \n\n![visitor badge](https://visitor-badge.glitch.me/badge?page_id=mindwrapped.pokemon-card-checker-space)',
|
52 |
live=False,
|
53 |
)
|
54 |
|
55 |
+
demo.launch(debug=True)
|