Hali5 commited on
Commit
fadf1fb
·
1 Parent(s): 072dec6

added validation set and improve the prediction function

Browse files
Files changed (2) hide show
  1. app.py +44 -10
  2. val_samples.npz +3 -0
app.py CHANGED
@@ -44,30 +44,47 @@ dataset = numpy.load("test_samples.npz")
44
  images = dataset["images"]
45
  labels = dataset["labels"]
46
 
47
- example_rows = []
 
 
 
 
 
 
48
  number_of_examples = len(labels)
49
 
50
  os.makedirs("ui_examples", exist_ok=True)
51
 
52
  for i in range(number_of_examples):
53
  img_array = images[i]
 
54
  label_index = int(labels[i].item() if hasattr(labels[i], 'item') else labels[i])
 
55
 
56
  truth_label_text = LABELS[label_index] if label_index < len(LABELS) else f"Class {label_index}"
 
57
 
58
  file_path = f"ui_examples/sample_{i}.jpg"
 
 
59
  Image.fromarray(img_array.astype("uint8"), "RGB").save(file_path)
 
60
 
61
- example_rows.append([file_path, truth_label_text])
 
62
 
63
  @spaces.GPU
64
  def predict(image,truth_labels=None):
65
  if image is None:
66
  return None
67
 
68
- pil_img = Image.fromarray(image.astype('uint8'), 'RGB')
69
-
70
- img_tensor = tf(pil_img).unsqueeze(0).to(device)
 
 
 
 
71
 
72
  with torch.no_grad():
73
  outputs = model(img_tensor)
@@ -85,11 +102,11 @@ custom_css = """
85
  """
86
 
87
 
88
- with gradio.Blocks() as demo:
89
  gradio.Markdown("# PathMNIST Image Classification")
90
 
91
  with gradio.Tab("Predict"):
92
- gradio.Markdown("Upload a tissue patch image for classification.")
93
  with gradio.Row():
94
  input_img = gradio.Image(height=512, width=512)
95
  with gradio.Column():
@@ -97,8 +114,25 @@ with gradio.Blocks() as demo:
97
  btn = gradio.Button("Predict")
98
  btn.click(fn=predict, inputs=input_img, outputs=output_lbl)
99
 
100
- with gradio.Tab("Examples"):
101
- gradio.Markdown("Click an example below to test the model against the PathMNIST test dataset.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  with gradio.Row():
104
  with gradio.Column():
@@ -107,7 +141,7 @@ with gradio.Blocks() as demo:
107
  output_lbl_ex = gradio.Label(num_top_classes=3, label="Model Prediction")
108
 
109
  gradio.Examples(
110
- examples=example_rows,
111
  inputs=[input_img_ex, truth_box],
112
  outputs=output_lbl_ex,
113
  fn=predict,
 
44
  images = dataset["images"]
45
  labels = dataset["labels"]
46
 
47
+ val_dataset = numpy.load("val_samples.npz")
48
+ val_images = val_dataset["images"]
49
+ val_labels = val_dataset["labels"]
50
+
51
+ example_rows_test = []
52
+ example_rows_val = []
53
+
54
  number_of_examples = len(labels)
55
 
56
  os.makedirs("ui_examples", exist_ok=True)
57
 
58
  for i in range(number_of_examples):
59
  img_array = images[i]
60
+ val_img_arr = val_images[i]
61
  label_index = int(labels[i].item() if hasattr(labels[i], 'item') else labels[i])
62
+ val_label_index = int(val_labels[i].item() if hasattr(val_labels[i], 'item') else val_labels[i])
63
 
64
  truth_label_text = LABELS[label_index] if label_index < len(LABELS) else f"Class {label_index}"
65
+ val_truth_label_text = LABELS[val_label_index] if val_label_index < len(LABELS) else f"Class {val_label_index}"
66
 
67
  file_path = f"ui_examples/sample_{i}.jpg"
68
+ val_file_path = f"ui_examples/val_sample_{i}.jpg"
69
+
70
  Image.fromarray(img_array.astype("uint8"), "RGB").save(file_path)
71
+ Image.fromarray(val_img_arr.astype("uint8"), "RGB").save(val_file_path)
72
 
73
+ example_rows_test.append([file_path, truth_label_text])
74
+ example_rows_val.append([val_file_path, val_truth_label_text])
75
 
76
  @spaces.GPU
77
  def predict(image,truth_labels=None):
78
  if image is None:
79
  return None
80
 
81
+ if truth_labels:
82
+ # already preprocessed test/val_samples
83
+ img_tensor = torch.from_numpy(image).unsqueeze(0).to(device)
84
+ else:
85
+ # uplouded image
86
+ pil_img = Image.fromarray(image.astype('uint8'), 'RGB')
87
+ img_tensor = tf(pil_img).unsqueeze(0).to(device)
88
 
89
  with torch.no_grad():
90
  outputs = model(img_tensor)
 
102
  """
103
 
104
 
105
+ with gradio.Blocks(css=custom_css) as demo:
106
  gradio.Markdown("# PathMNIST Image Classification")
107
 
108
  with gradio.Tab("Predict"):
109
+ gradio.Markdown("## Upload a tissue image for classification")
110
  with gradio.Row():
111
  input_img = gradio.Image(height=512, width=512)
112
  with gradio.Column():
 
114
  btn = gradio.Button("Predict")
115
  btn.click(fn=predict, inputs=input_img, outputs=output_lbl)
116
 
117
+ with gradio.Tab("Examples (Validation)"):
118
+ gradio.Markdown("## Select an example below to test the model against the PathMNIST validation dataset")
119
+
120
+ with gradio.Row():
121
+ with gradio.Column():
122
+ input_img_ex = gradio.Image(label="Selected Test Image", height=512, width=512)
123
+ truth_box = gradio.Textbox(label="Ground Truth Label", interactive=False)
124
+ output_lbl_ex = gradio.Label(num_top_classes=3, label="Model Prediction")
125
+
126
+ gradio.Examples(
127
+ examples=example_rows_val,
128
+ inputs=[input_img_ex, truth_box],
129
+ outputs=output_lbl_ex,
130
+ fn=predict,
131
+ cache_examples=True,
132
+ )
133
+
134
+ with gradio.Tab("Examples (Test)"):
135
+ gradio.Markdown("## Select an example below to test the model against the PathMNIST test dataset")
136
 
137
  with gradio.Row():
138
  with gradio.Column():
 
141
  output_lbl_ex = gradio.Label(num_top_classes=3, label="Model Prediction")
142
 
143
  gradio.Examples(
144
+ examples=example_rows_test,
145
  inputs=[input_img_ex, truth_box],
146
  outputs=output_lbl_ex,
147
  fn=predict,
val_samples.npz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3085e9bbff8d470ed1375d618232b580ebb7fe8dbd0fd02c8a584e115936a5d6
3
+ size 2618925