ahsanMah commited on
Commit
e71c8e0
·
1 Parent(s): 98f9427

resizing back to orignal image size

Browse files
Files changed (2) hide show
  1. README.md +1 -0
  2. app.py +3 -6
README.md CHANGED
@@ -11,6 +11,7 @@ pinned: false
11
  <img src="samples/duckelephant.jpeg" alt="input elephant" width="32%"/>
12
  <img src="assets/heatmap.webp" alt="heatmap image" width="32%"/>
13
  </p>
 
14
  ## Introduction
15
 
16
  This project aims to add anomaly detection capabilities to score-based diffusion models, enabling the detection of anomalies in natural images. The core idea is to learn the distribution of typical score vectors for each patch position in an image. By training a position-conditioned normalizing flow model, we can estimate the likelihood of the score vectors' outputs. This allows for visualizing per-patch likelihoods and generating heatmaps of anomalies.
 
11
  <img src="samples/duckelephant.jpeg" alt="input elephant" width="32%"/>
12
  <img src="assets/heatmap.webp" alt="heatmap image" width="32%"/>
13
  </p>
14
+
15
  ## Introduction
16
 
17
  This project aims to add anomaly detection capabilities to score-based diffusion models, enabling the detection of anomalies in natural images. The core idea is to learn the distribution of typical score vectors for each patch position in an image. By training a position-conditioned normalizing flow model, we can estimate the likelihood of the score vectors' outputs. This allows for visualizing per-patch likelihoods and generating heatmaps of anomalies.
app.py CHANGED
@@ -88,11 +88,6 @@ def plot_heatmap(img: Image, heatmap: np.array):
88
  h = cmap(h, bytes=True)[:, :, :3]
89
  h = Image.fromarray(h).resize(img.size, resample=Image.Resampling.BILINEAR)
90
  im = Image.blend(img, h, alpha=0.6)
91
- # im = ax.imshow(np.array(im))
92
- # # fig.colorbar(im)
93
- # # plt.grid(False)
94
- # # plt.axis("off")
95
- # fig.tight_layout()
96
  return im
97
 
98
  @torch.no_grad
@@ -106,6 +101,7 @@ def run_inference(model, img):
106
 
107
  def localize_anomalies(input_img, preset="edm2-img64-s-fid", load_from_hub=False):
108
 
 
109
  device = "cuda" if torch.cuda.is_available() else "cpu"
110
  # img = center_crop_imagenet(64, img)
111
  input_img = input_img.resize(size=(64, 64), resample=Image.Resampling.LANCZOS)
@@ -127,6 +123,7 @@ def localize_anomalies(input_img, preset="edm2-img64-s-fid", load_from_hub=False
127
  outstr = f"Anomaly score: {nll:.3f} / {pct:.2f} percentile"
128
  histplot = plot_against_reference(nll, ref_nll)
129
  heatmapplot = plot_heatmap(input_img, img_likelihood)
 
130
 
131
  return outstr, heatmapplot, histplot
132
 
@@ -151,7 +148,7 @@ def build_demo(inference_fn):
151
  gr.Text(
152
  label="Estimated global outlier scores - Percentiles with respect to Imagenette Scores"
153
  ),
154
- gr.Image(label="Anomaly Heatmap", min_width=64),
155
  gr.Plot(label="Comparing to Imagenette"),
156
  ],
157
  examples=[
 
88
  h = cmap(h, bytes=True)[:, :, :3]
89
  h = Image.fromarray(h).resize(img.size, resample=Image.Resampling.BILINEAR)
90
  im = Image.blend(img, h, alpha=0.6)
 
 
 
 
 
91
  return im
92
 
93
  @torch.no_grad
 
101
 
102
  def localize_anomalies(input_img, preset="edm2-img64-s-fid", load_from_hub=False):
103
 
104
+ orig_size = input_img.size
105
  device = "cuda" if torch.cuda.is_available() else "cpu"
106
  # img = center_crop_imagenet(64, img)
107
  input_img = input_img.resize(size=(64, 64), resample=Image.Resampling.LANCZOS)
 
123
  outstr = f"Anomaly score: {nll:.3f} / {pct:.2f} percentile"
124
  histplot = plot_against_reference(nll, ref_nll)
125
  heatmapplot = plot_heatmap(input_img, img_likelihood)
126
+ heatmapplot = heatmapplot.resize(orig_size)
127
 
128
  return outstr, heatmapplot, histplot
129
 
 
148
  gr.Text(
149
  label="Estimated global outlier scores - Percentiles with respect to Imagenette Scores"
150
  ),
151
+ gr.Image(label="Anomaly Heatmap", min_width=160),
152
  gr.Plot(label="Comparing to Imagenette"),
153
  ],
154
  examples=[