Update app.py
Browse files
app.py
CHANGED
|
@@ -16,6 +16,34 @@ label=gr.outputs.Label()
|
|
| 16 |
examples=["filibe-1-1.jpg",
|
| 17 |
"ohrid-3-1.jpg",
|
| 18 |
"varna-1-1.jpg"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
examples=["filibe-1-1.jpg",
|
| 17 |
"ohrid-3-1.jpg",
|
| 18 |
"varna-1-1.jpg"]
|
| 19 |
+
def gradcam(img_create, learn):
|
| 20 |
+
x,= first(dls_augmented.test_dl([img]))
|
| 21 |
+
hook_output = Hook()
|
| 22 |
+
hook = learn.model[0].register_forward_hook(hook_output.hook_func)
|
| 23 |
+
with torch.no_grad(): output = learn.model.eval()(x)
|
| 24 |
+
act = hook_output.stored[0]
|
| 25 |
+
hook.remove()
|
| 26 |
|
| 27 |
+
input_size=act.shape[0]
|
| 28 |
+
out_size=learn.model[1][-1].in_features
|
| 29 |
+
kernel_size=act.shape[1]
|
| 30 |
+
a=act
|
| 31 |
+
new_act=tensor(np.zeros((out_size,kernel_size,kernel_size)))
|
| 32 |
+
sum=tensor(np.zeros((1,kernel_size,kernel_size)))
|
| 33 |
+
for i in range(0,input_size,4):
|
| 34 |
+
sum=tensor(np.zeros((1,kernel_size,kernel_size)))
|
| 35 |
+
for j in range(i,i+4):
|
| 36 |
+
sum=sum+act[j,:,:]
|
| 37 |
+
new_act[int(i/4),:,:]=sum/4
|
| 38 |
+
cam_map = torch.einsum('ck,kij->cij', learn.model[1][-1].weight, new_act)
|
| 39 |
+
gcam=cam_map[1].detach().cpu()
|
| 40 |
+
x_dec = TensorImage(dls_augmented.train.decode((x,))[0][0])
|
| 41 |
+
_,ax = plt.subplots()
|
| 42 |
+
x_dec.show(ctx=ax)
|
| 43 |
+
ax.imshow(cam_map[1].detach().cpu(), alpha=0.6, extent=(0,128,128,0), interpolation='bilinear', cmap='magma');
|
| 44 |
+
return gcam
|
| 45 |
+
gcam=gradcam(learn_inf,learn)
|
| 46 |
+
demo = gr.Interface(sepia, gr.Image(shape=(200, 200)), "image")
|
| 47 |
+
|
| 48 |
+
demo=gr.Interface(fn=classify_img, inputs=image, outputs=label, examples=examples)
|
| 49 |
+
demo.launch(inline=False)
|