mindwrapped commited on
Commit
09883a8
1 Parent(s): 9c8a7bc

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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=['0', '1'],
22
+ num_samples=1000,
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='This space uses 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 (green shows what contributed towards that label and red shows what contributed against the label predicted).\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 🤔 \n\n([View Labels](https://gist.github.com/mindwrapped/e5aad747757ef006037a1a1982be34fc)) \n\nFeel free to like if you like it. \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)