Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,11 @@ import gradio as gr
|
|
| 4 |
class Hook():
|
| 5 |
def hook_func(self, m, i, o): self.stored = o.detach().clone()
|
| 6 |
|
| 7 |
-
|
| 8 |
|
| 9 |
categories = ('arbanasi', 'filibe', 'gjirokoster', 'iskodra', 'kula', 'kuzguncuk', 'larissa_ampelakia', 'mardin', 'ohrid', 'pristina', 'safranbolu', 'selanik', 'sozopol_suzebolu', 'tiran', 'varna')
|
| 10 |
def classify_img(img):
|
| 11 |
-
pred,idx,probs=
|
| 12 |
return dict(zip(categories, map(float, probs)))
|
| 13 |
|
| 14 |
image=gr.inputs.Image(shape=(128,128))
|
|
@@ -16,7 +16,20 @@ label=gr.outputs.Label()
|
|
| 16 |
examples=["filibe-1-1.jpg",
|
| 17 |
"ohrid-3-1.jpg",
|
| 18 |
"varna-1-1.jpg"]
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
x,= first(dls_augmented.test_dl([img]))
|
| 21 |
hook_output = Hook()
|
| 22 |
hook = learn.model[0].register_forward_hook(hook_output.hook_func)
|
|
@@ -27,14 +40,13 @@ def gradcam(img_create, learn):
|
|
| 27 |
input_size=act.shape[0]
|
| 28 |
out_size=learn.model[1][-1].in_features
|
| 29 |
kernel_size=act.shape[1]
|
| 30 |
-
|
| 31 |
-
|
| 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)
|
| 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])
|
|
@@ -42,8 +54,10 @@ def gradcam(img_create, learn):
|
|
| 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)
|
|
|
|
| 4 |
class Hook():
|
| 5 |
def hook_func(self, m, i, o): self.stored = o.detach().clone()
|
| 6 |
|
| 7 |
+
learn = load_learner("resnet152_fit_one_cycle_freeze_91acc.pkl", cpu=True)
|
| 8 |
|
| 9 |
categories = ('arbanasi', 'filibe', 'gjirokoster', 'iskodra', 'kula', 'kuzguncuk', 'larissa_ampelakia', 'mardin', 'ohrid', 'pristina', 'safranbolu', 'selanik', 'sozopol_suzebolu', 'tiran', 'varna')
|
| 10 |
def classify_img(img):
|
| 11 |
+
pred,idx,probs=learn.predict(img)
|
| 12 |
return dict(zip(categories, map(float, probs)))
|
| 13 |
|
| 14 |
image=gr.inputs.Image(shape=(128,128))
|
|
|
|
| 16 |
examples=["filibe-1-1.jpg",
|
| 17 |
"ohrid-3-1.jpg",
|
| 18 |
"varna-1-1.jpg"]
|
| 19 |
+
|
| 20 |
+
#@title DataLoader
|
| 21 |
+
path = "train_val_cropped"
|
| 22 |
+
dblock = DataBlock(blocks = (ImageBlock, CategoryBlock),
|
| 23 |
+
get_items = get_image_files,
|
| 24 |
+
splitter = GrandparentSplitter(train_name="train",
|
| 25 |
+
valid_name="valid"),
|
| 26 |
+
get_y=parent_label,
|
| 27 |
+
item_tfms=RandomResizedCrop(128, min_scale=0.7),
|
| 28 |
+
batch_tfms=[*aug_transforms(),
|
| 29 |
+
Normalize.from_stats(*imagenet_stats)])
|
| 30 |
+
dls_augmented = dblock.dataloaders(path, shuffle=True)
|
| 31 |
+
|
| 32 |
+
def gradcam(img):
|
| 33 |
x,= first(dls_augmented.test_dl([img]))
|
| 34 |
hook_output = Hook()
|
| 35 |
hook = learn.model[0].register_forward_hook(hook_output.hook_func)
|
|
|
|
| 40 |
input_size=act.shape[0]
|
| 41 |
out_size=learn.model[1][-1].in_features
|
| 42 |
kernel_size=act.shape[1]
|
| 43 |
+
new_act = tensor(np.zeros((out_size, kernel_size, kernel_size)))
|
| 44 |
+
sum = tensor(np.zeros((1, kernel_size, kernel_size)))
|
|
|
|
| 45 |
for i in range(0,input_size,4):
|
| 46 |
+
sum=tensor(np.zeros((1, kernel_size, kernel_size)))
|
| 47 |
+
for j in range(i, i+4):
|
| 48 |
+
sum=sum+act[j, :, :]
|
| 49 |
+
new_act[int(i/4), :, :]=sum/4
|
| 50 |
cam_map = torch.einsum('ck,kij->cij', learn.model[1][-1].weight, new_act)
|
| 51 |
gcam=cam_map[1].detach().cpu()
|
| 52 |
x_dec = TensorImage(dls_augmented.train.decode((x,))[0][0])
|
|
|
|
| 54 |
x_dec.show(ctx=ax)
|
| 55 |
ax.imshow(cam_map[1].detach().cpu(), alpha=0.6, extent=(0,128,128,0), interpolation='bilinear', cmap='magma');
|
| 56 |
return gcam
|
| 57 |
+
|
| 58 |
gcam=gradcam(learn_inf,learn)
|
| 59 |
+
|
| 60 |
demo = gr.Interface(sepia, gr.Image(shape=(200, 200)), "image")
|
| 61 |
+
demo = gr.Interface(fn=classify_img, inputs=image, outputs=label, examples=examples)
|
| 62 |
|
|
|
|
| 63 |
demo.launch(inline=False)
|