File size: 2,230 Bytes
e10e3fa
 
 
99f86a4
 
e10e3fa
 
 
e828d81
 
f4142af
99f86a4
 
 
 
 
 
 
 
 
5ddf05e
 
99f86a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e10e3fa
a4d339b
e10e3fa
462216f
99f86a4
 
f4142af
e10e3fa
0fef32f
f91810f
984367f
f4142af
e10e3fa
99f86a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
import gradio as gr
from huggingface_hub import from_pretrained_fastai
from lime import lime_image
from skimage.segmentation import mark_boundaries

learn = from_pretrained_fastai('mindwrapped/pokemon-card-checker')

def check_card(img):
    pred_label, _, scores = learn.predict(img)
    scores = scores.detach().numpy()
    scores = {'real': float(scores[1]), 'fake': float(scores[0])}

    print(np.array(img).shape)

    # Lime Explanation
    explainer = lime_image.LimeImageExplainer()
    explanation = explainer.explain_instance(
      np.array(img), 
      classifier_fn=classify_cards,
      labels=['0', '1'],
      num_samples=1000,
      random_seed=42,
    )

    temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=False, num_features=10, hide_rest=False)
    img_boundry = mark_boundaries(temp/255.0, mask)
    return scores, img_boundry

def classify_cards(imgs):
  print(imgs.shape)
  scores = []

  for i in range(imgs.shape[0]):
    pred_label, _, score = learn.predict(imgs[i])
    scores.append(score.detach().numpy())

  scores = np.array(scores)
  print(scores.shape)

  return scores


demo = gr.Interface(
  fn=check_card, 
  inputs='image',
  outputs=["label", "image"],
  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'],
  title='Pokemon Card Checker',
  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).',
  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)',
  live=False,
  )

demo.launch(debug=True)