Spaces:
Sleeping
Sleeping
Video feature
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +1 -0
- .gitignore +2 -0
- README.md +1 -1
- animals.png +3 -0
- app.py +257 -105
- requirements.txt +0 -0
- src/Nets.py +1 -1
- src/cache/val_df.csv +0 -0
- src/examples/AI_Generated/crow.png +3 -0
- src/examples/AI_Generated/donkey.png +3 -0
- src/examples/AI_Generated/eagle.png +3 -0
- src/examples/{dragonfly/353bd2bd65.jpg → AI_Generated/elephant (2).png} +2 -2
- src/examples/AI_Generated/elephant.png +3 -0
- src/examples/AI_Generated/fox.png +3 -0
- src/examples/AI_Generated/goat (2).png +3 -0
- src/examples/AI_Generated/goat.png +3 -0
- src/examples/AI_Generated/goldfish.png +3 -0
- src/examples/AI_Generated/jellyfish.png +3 -0
- src/examples/AI_Generated/koala.png +3 -0
- src/examples/AI_Generated/otter.png +3 -0
- src/examples/AI_Generated/panda.png +3 -0
- src/examples/AI_Generated/penguin.png +3 -0
- src/examples/AI_Generated/pigeon.png +3 -0
- src/examples/AI_Generated/rabbit.png +3 -0
- src/examples/AI_Generated/rhinoceros (2).png +3 -0
- src/examples/AI_Generated/rhinoceros.png +3 -0
- src/examples/AI_Generated/snake.png +3 -0
- src/examples/AI_Generated/swan.png +3 -0
- src/examples/AI_Generated/woodpecker.png +3 -0
- src/examples/antelope/1d556456dc.jpg +0 -3
- src/examples/badger/0836f4eb45.jpg +0 -3
- src/examples/badger/23bfad16a7.jpg +0 -3
- src/examples/badger/4c273d12a9.jpg +0 -3
- src/examples/badger/5bffbd51cf.jpg +0 -3
- src/examples/badger/87d1db4af3.jpg +0 -3
- src/examples/badger/89a8316cd4.jpg +0 -3
- src/examples/badger/99e296bf48.jpg +0 -3
- src/examples/bat/16f6af0091.jpg +0 -3
- src/examples/bat/1dd514de63.jpg +0 -3
- src/examples/bat/1fd53c0b98.jpg +0 -3
- src/examples/bat/2d028b789d.jpg +0 -3
- src/examples/bat/2f7c6c7cd5.jpg +0 -3
- src/examples/bat/330e4a8053.jpg +0 -3
- src/examples/bat/47d2c91d9b.jpg +0 -3
- src/examples/bat/513bb906a6.jpg +0 -3
- src/examples/bat/5e85312fa8.jpg +0 -3
- src/examples/bat/6b4b95f0c4.jpg +0 -3
- src/examples/bat/6da14f603d.jpg +0 -3
- src/examples/bat/741fa84ed0.jpg +0 -3
- src/examples/bear/116d9b7f88.jpg +0 -3
.gitattributes
CHANGED
@@ -35,3 +35,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
37 |
*.png filter=lfs diff=lfs merge=lfs -text
|
|
|
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
37 |
*.png filter=lfs diff=lfs merge=lfs -text
|
38 |
+
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
.gitignore
CHANGED
@@ -158,3 +158,5 @@ src/results/plots/
|
|
158 |
src/train_resnet.py
|
159 |
src/visualize_gradcam.ipynb
|
160 |
src/cache/data.csv
|
|
|
|
|
|
158 |
src/train_resnet.py
|
159 |
src/visualize_gradcam.ipynb
|
160 |
src/cache/data.csv
|
161 |
+
.vscode/settings.json
|
162 |
+
src/backup
|
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: Explain Animal CNN
|
3 |
-
emoji:
|
4 |
colorFrom: pink
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
|
|
1 |
---
|
2 |
title: Explain Animal CNN
|
3 |
+
emoji: 🐬
|
4 |
colorFrom: pink
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
animals.png
ADDED
![]() |
Git LFS Details
|
app.py
CHANGED
@@ -4,7 +4,6 @@ import sys
|
|
4 |
sys.path.append('src')
|
5 |
from collections import defaultdict
|
6 |
from functools import lru_cache
|
7 |
-
|
8 |
import gradio as gr
|
9 |
import matplotlib.pyplot as plt
|
10 |
import numpy as np
|
@@ -18,10 +17,18 @@ from torchcam.utils import overlay_mask
|
|
18 |
from torchvision.transforms.functional import to_pil_image
|
19 |
from tqdm import tqdm
|
20 |
from util import transform
|
|
|
|
|
|
|
|
|
21 |
|
22 |
IMAGE_PATH = os.path.join(os.getcwd(), 'src/examples')
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
25 |
|
26 |
CAM_METHODS = {
|
27 |
"GradCAM": GradCAM,
|
@@ -35,27 +42,24 @@ model.load_state_dict(torch.load('src/results/models/best_model.pth', map_locati
|
|
35 |
cam_model = copy.deepcopy(model)
|
36 |
data_df = pd.read_csv('src/cache/val_df.csv')
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
idx = np.random.randint(0, len(data_df))
|
42 |
-
p = os.path.join(IMAGE_PATH, data_df.iloc[idx]['path'])
|
43 |
-
p = p.replace('\\', '/')
|
44 |
-
p = p.replace('//', '/')
|
45 |
-
animal = data_df.iloc[idx]['target']
|
46 |
-
if os.path.exists(p):
|
47 |
-
random_images.append((animal, Image.open(p)))
|
48 |
-
return random_images
|
49 |
|
50 |
def get_class_name(idx):
|
51 |
-
return
|
|
|
|
|
|
|
52 |
|
53 |
@lru_cache(maxsize=100)
|
54 |
def get_translated(to_translate):
|
55 |
-
return
|
56 |
-
|
|
|
57 |
|
58 |
-
def infer_image(image):
|
|
|
59 |
image = transform(image)
|
60 |
image = image.unsqueeze(0)
|
61 |
with torch.no_grad():
|
@@ -67,7 +71,8 @@ def infer_image(image):
|
|
67 |
ret[animal] = prob.item()
|
68 |
return ret
|
69 |
|
70 |
-
def gradcam(image, alpha, cam_method, layer):
|
|
|
71 |
if layer == 'layer1': layers = [model.resnet.layer1]
|
72 |
elif layer == 'layer2': layers = [model.resnet.layer2]
|
73 |
elif layer == 'layer3': layers = [model.resnet.layer3]
|
@@ -78,105 +83,252 @@ def gradcam(image, alpha, cam_method, layer):
|
|
78 |
img_tensor = transform(image).unsqueeze(0)
|
79 |
cam = CAM_METHODS[cam_method](model, target_layer=layers)
|
80 |
output = model(img_tensor)
|
81 |
-
|
|
|
82 |
result = overlay_mask(image, to_pil_image(activation_map[0].squeeze(0), mode='F'), alpha=alpha)
|
83 |
cam.remove_hooks()
|
84 |
|
85 |
-
# height maximal 300px
|
86 |
-
if result.size[1] > 300:
|
87 |
-
|
88 |
-
|
89 |
return result
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
)
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
scale=2,
|
146 |
)
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
with gr.Column():
|
151 |
-
|
152 |
-
|
153 |
-
label="
|
154 |
-
info="
|
155 |
-
|
156 |
)
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
placeholder = gr.Markdown("## Example Images")
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
amount_rows = 0
|
170 |
-
for i in range(amount_rows):
|
171 |
-
with gr.Row():
|
172 |
-
for j in range(IMAGES_PER_ROW):
|
173 |
-
animal, image = loaded_images[i * IMAGES_PER_ROW + j]
|
174 |
-
showed_images.append(gr.Image(
|
175 |
-
value=image,
|
176 |
-
label=animal,
|
177 |
-
type="pil",
|
178 |
-
interactive=False,
|
179 |
-
))
|
180 |
|
181 |
|
182 |
|
|
|
4 |
sys.path.append('src')
|
5 |
from collections import defaultdict
|
6 |
from functools import lru_cache
|
|
|
7 |
import gradio as gr
|
8 |
import matplotlib.pyplot as plt
|
9 |
import numpy as np
|
|
|
17 |
from torchvision.transforms.functional import to_pil_image
|
18 |
from tqdm import tqdm
|
19 |
from util import transform
|
20 |
+
from gradio_blocks import build_video_to_camvideo
|
21 |
+
import cv2
|
22 |
+
import ffmpeg
|
23 |
+
|
24 |
|
25 |
IMAGE_PATH = os.path.join(os.getcwd(), 'src/examples')
|
26 |
+
IMAGES_PER_ROW = 7
|
27 |
+
|
28 |
+
MAXIMAL_FRAMES = 1000
|
29 |
+
BATCHES_TO_PROCESS = 10
|
30 |
+
OUTPUT_FPS = 15
|
31 |
+
MAX_OUT_FRAMES = 60
|
32 |
|
33 |
CAM_METHODS = {
|
34 |
"GradCAM": GradCAM,
|
|
|
42 |
cam_model = copy.deepcopy(model)
|
43 |
data_df = pd.read_csv('src/cache/val_df.csv')
|
44 |
|
45 |
+
C_NUM_TO_NAME = data_df[['encoded_target', 'target']].drop_duplicates().sort_values('encoded_target').set_index('encoded_target')['target'].to_dict()
|
46 |
+
C_NAME_TO_NUM = {v: k for k, v in C_NUM_TO_NAME.items()}
|
47 |
+
ALL_CLASSES = sorted(list(C_NUM_TO_NAME.values()), key=lambda x: x.lower())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
def get_class_name(idx):
|
50 |
+
return C_NUM_TO_NAME[idx]
|
51 |
+
|
52 |
+
def get_class_idx(name):
|
53 |
+
return C_NAME_TO_NUM[name]
|
54 |
|
55 |
@lru_cache(maxsize=100)
|
56 |
def get_translated(to_translate):
|
57 |
+
return "ssss"
|
58 |
+
# return GoogleTranslator(source="en", target="de").translate(to_translate)
|
59 |
+
# for idx in range(90): get_translated(get_class_name(idx))
|
60 |
|
61 |
+
def infer_image(image, image_sketch):
|
62 |
+
image = image if image is not None else image_sketch
|
63 |
image = transform(image)
|
64 |
image = image.unsqueeze(0)
|
65 |
with torch.no_grad():
|
|
|
71 |
ret[animal] = prob.item()
|
72 |
return ret
|
73 |
|
74 |
+
def gradcam(image, image_sketch=None, alpha=0.5, cam_method=GradCAM, layer=None, specific_class="Predicted Class"):
|
75 |
+
image = image if image is not None else image_sketch
|
76 |
if layer == 'layer1': layers = [model.resnet.layer1]
|
77 |
elif layer == 'layer2': layers = [model.resnet.layer2]
|
78 |
elif layer == 'layer3': layers = [model.resnet.layer3]
|
|
|
83 |
img_tensor = transform(image).unsqueeze(0)
|
84 |
cam = CAM_METHODS[cam_method](model, target_layer=layers)
|
85 |
output = model(img_tensor)
|
86 |
+
class_to_explain = output.squeeze(0).argmax().item() if specific_class == "Predicted Class" else get_class_idx(specific_class)
|
87 |
+
activation_map = cam(class_to_explain, output)
|
88 |
result = overlay_mask(image, to_pil_image(activation_map[0].squeeze(0), mode='F'), alpha=alpha)
|
89 |
cam.remove_hooks()
|
90 |
|
91 |
+
# # height maximal 300px
|
92 |
+
# if result.size[1] > 300:
|
93 |
+
# ratio = 300 / result.size[1]
|
94 |
+
# result = result.resize((int(result.size[0] * ratio), 300))
|
95 |
return result
|
96 |
|
97 |
+
|
98 |
+
def gradcam_video(video, alpha=0.5, cam_method=GradCAM, layer=None, specific_class="Predicted Class"):
|
99 |
+
global OUTPUT_FPS, MAXIMAL_FRAMES, BATCHES_TO_PROCESS, MAX_OUT_FRAMES
|
100 |
+
video = cv2.VideoCapture(video)
|
101 |
+
fps = int(video.get(cv2.CAP_PROP_FPS))
|
102 |
+
if OUTPUT_FPS == -1: OUTPUT_FPS = fps
|
103 |
+
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
|
104 |
+
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
105 |
+
if width > 3000 or height > 3000:
|
106 |
+
raise gr.Error("The video is too big. The maximal size is 3000x3000.")
|
107 |
+
print(f'FPS: {fps}, Width: {width}, Height: {height}')
|
108 |
+
|
109 |
+
frames = list()
|
110 |
+
success, image = video.read()
|
111 |
+
while success:
|
112 |
+
frames.append(image)
|
113 |
+
success, image = video.read()
|
114 |
+
print(f'Frames: {len(frames)}')
|
115 |
+
if len(frames) == 0:
|
116 |
+
raise gr.Error("The video is empty.")
|
117 |
+
if len(frames) >= MAXIMAL_FRAMES:
|
118 |
+
raise gr.Error(f"The video is too long. The maximal length is {MAXIMAL_FRAMES} frames.")
|
119 |
+
|
120 |
+
if len(frames) > MAX_OUT_FRAMES:
|
121 |
+
frames = frames[::len(frames) // MAX_OUT_FRAMES]
|
122 |
+
|
123 |
+
print(f'Frames to process: {len(frames)}')
|
124 |
+
|
125 |
+
processed = [Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in frames]
|
126 |
+
# generate lists in lists for the images for batch processing. 10 images per inner list..
|
127 |
+
batched = [processed[i:i + BATCHES_TO_PROCESS] for i in range(0, len(processed), BATCHES_TO_PROCESS)]
|
128 |
|
129 |
+
model.eval()
|
130 |
+
if layer == 'layer1': layers = [model.resnet.layer1]
|
131 |
+
elif layer == 'layer2': layers = [model.resnet.layer2]
|
132 |
+
elif layer == 'layer3': layers = [model.resnet.layer3]
|
133 |
+
elif layer == 'layer4': layers = [model.resnet.layer4]
|
134 |
+
else: layers = [model.resnet.layer1, model.resnet.layer2, model.resnet.layer3, model.resnet.layer4]
|
135 |
+
cam = CAM_METHODS[cam_method](model, target_layer=layers)
|
136 |
+
results = list()
|
137 |
+
for i, batch in enumerate(tqdm(batched)):
|
138 |
+
images_tensor = torch.stack([transform(image) for image in batch])
|
139 |
+
outputs = model(images_tensor)
|
140 |
+
out_classes = [output.argmax().item() for output in outputs]
|
141 |
+
classes_to_explain = out_classes if specific_class == "Predicted Class" else [get_class_idx(specific_class)] * len(out_classes)
|
142 |
+
activation_maps = cam(classes_to_explain, outputs)
|
143 |
+
for j, activation_map in enumerate(activation_maps[0]):
|
144 |
+
result = overlay_mask(batch[j], to_pil_image(activation_map, mode='F'), alpha=alpha)
|
145 |
+
results.append(cv2.cvtColor(np.array(result), cv2.COLOR_RGB2BGR))
|
146 |
+
cam.remove_hooks()
|
147 |
|
148 |
+
# save video
|
149 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
150 |
+
size = (results[0].shape[1], results[0].shape[0])
|
151 |
+
video = cv2.VideoWriter('src/results/gradcam_video.mp4', fourcc, OUTPUT_FPS, size)
|
152 |
+
for frame in results:
|
153 |
+
video.write(frame)
|
154 |
+
video.release()
|
155 |
+
return 'src/results/gradcam_video.mp4'
|
156 |
+
|
157 |
+
def load_examples():
|
158 |
+
folder_name_to_header = {
|
159 |
+
"AI_Generated": "AI Generated Images",
|
160 |
+
"true_predicted": "True Predicted Images (Validation Set)",
|
161 |
+
"false_predicted": "False Predicted Images (Validation Set)",
|
162 |
+
"others": "Other interesting images from the internet"
|
163 |
+
}
|
164 |
|
165 |
+
images_description = {
|
166 |
+
"AI_Generated": "These images are generated by Dalle3 and Stable Diffusion. All of them are not real images and because of that it is interesting to see how the model predicts them.",
|
167 |
+
"true_predicted": "These images are from the validation set and the model predicted them correctly.",
|
168 |
+
"false_predicted": "These images are from the validation set and the model predicted them incorrectly. Maybe you can see why the model predicted them incorrectly using the GradCAM visualization. :)",
|
169 |
+
"others": "These images are from the internet and are not part of the validation set. They are interesting because most of them show different animals."
|
170 |
+
}
|
171 |
+
|
172 |
+
loaded_images = defaultdict(list)
|
173 |
+
|
174 |
+
for image_type in ["AI_Generated", "true_predicted", "false_predicted", "others"]:
|
175 |
+
# for image_type in os.listdir(IMAGE_PATH):
|
176 |
+
full_path = os.path.join(IMAGE_PATH, image_type).replace('\\', '/').replace('//', '/')
|
177 |
+
gr.Markdown(f'## {folder_name_to_header[image_type]}')
|
178 |
+
gr.Markdown(images_description[image_type])
|
179 |
+
images_to_load = os.listdir(full_path)
|
180 |
+
rows = (len(images_to_load) // IMAGES_PER_ROW) + 1
|
181 |
+
for i in range(rows):
|
182 |
+
with gr.Row(elem_classes=["row-example-images"], equal_height=False):
|
183 |
+
for j in range(IMAGES_PER_ROW):
|
184 |
+
if i * IMAGES_PER_ROW + j >= len(images_to_load): break
|
185 |
+
image = images_to_load[i * IMAGES_PER_ROW + j]
|
186 |
+
loaded_images[image_type].append(
|
187 |
+
gr.Image(
|
188 |
+
value=os.path.join(full_path, image),
|
189 |
+
label=f"image ({get_translated(image.split('.')[0])})",
|
190 |
+
type="pil",
|
191 |
+
interactive=False,
|
192 |
+
elem_classes=["selectable_images"],
|
193 |
+
)
|
194 |
+
)
|
195 |
+
return loaded_images
|
196 |
+
|
197 |
+
css = """
|
198 |
+
#logo {text-align: right;}
|
199 |
+
p {text-align: justify; text-justify: inter-word; font-size: 1.1em; line-height: 1.2em;}
|
200 |
+
.svelte-1btp92j.selectable {cursor: pointer !important; }
|
201 |
+
"""
|
202 |
+
|
203 |
+
|
204 |
+
|
205 |
+
with gr.Blocks(theme='freddyaboulton/dracula_revamped', css=css) as demo:
|
206 |
+
# -------------------------------------------
|
207 |
+
# HEADER WITH LOGO
|
208 |
+
# -------------------------------------------
|
209 |
+
with gr.Row():
|
210 |
+
with open('src/header.md', 'r', encoding='utf-8') as f:
|
211 |
+
markdown_string = f.read()
|
212 |
+
with gr.Column(scale=10):
|
213 |
+
header = gr.Markdown(markdown_string)
|
214 |
+
with gr.Column(scale=1):
|
215 |
+
pil_logo = Image.open('animals.png')
|
216 |
+
logo = gr.Image(value=pil_logo, scale=2, interactive=False, show_download_button=False, show_label=False, container=False, elem_id="logo")
|
217 |
+
|
218 |
+
# -------------------------------------------
|
219 |
+
# INPUT IMAGE
|
220 |
+
# -------------------------------------------
|
221 |
+
with gr.Row():
|
222 |
+
with gr.Tab("Upload Image"):
|
223 |
+
with gr.Row(variant="panel", equal_height=True):
|
224 |
+
user_image = gr.Image(
|
225 |
+
type="pil",
|
226 |
+
label="Upload Your Own Image",
|
227 |
+
info="You can also upload your own image for prediction.",
|
228 |
)
|
229 |
+
with gr.Tab("Draw Image"):
|
230 |
+
with gr.Row(variant="panel", equal_height=True):
|
231 |
+
user_image_sketched = gr.Image(
|
232 |
+
type="pil",
|
233 |
+
source="canvas",
|
234 |
+
tool="color-sketch",
|
235 |
+
label="Draw Your Own Image",
|
236 |
+
info="You can also draw your own image for prediction.",
|
|
|
237 |
)
|
238 |
+
|
239 |
+
# -------------------------------------------
|
240 |
+
# TOOLS
|
241 |
+
# -------------------------------------------
|
242 |
+
with gr.Row():
|
243 |
+
# -------------------------------------------
|
244 |
+
# PREDICT
|
245 |
+
# -------------------------------------------
|
246 |
+
with gr.Tab("Predict"):
|
247 |
with gr.Column():
|
248 |
+
output = gr.Label(
|
249 |
+
num_top_classes=5,
|
250 |
+
label="Output",
|
251 |
+
info="Top three predicted classes and their confidences.",
|
252 |
+
scale=5,
|
253 |
)
|
254 |
+
predict_mode_button = gr.Button(value="Predict Animal", label="Predict", info="Click to make a prediction.", scale=1)
|
255 |
+
predict_mode_button.click(fn=infer_image, inputs=[user_image, user_image_sketched], outputs=output, queue=True)
|
256 |
+
|
257 |
+
# -------------------------------------------
|
258 |
+
# EXPLAIN
|
259 |
+
# -------------------------------------------
|
260 |
+
with gr.Tab("Explain"):
|
261 |
+
with gr.Row():
|
262 |
+
with gr.Column():
|
263 |
+
cam_method = gr.Radio(
|
264 |
+
list(CAM_METHODS.keys()),
|
265 |
+
label="GradCAM Method",
|
266 |
+
value="GradCAM",
|
267 |
+
interactive=True,
|
268 |
+
scale=2,
|
269 |
+
)
|
270 |
+
cam_method.description = "Here you can choose the GradCAM method."
|
271 |
+
cam_method.description_place = "left"
|
272 |
+
|
273 |
+
alpha = gr.Slider(
|
274 |
+
minimum=.1,
|
275 |
+
maximum=.9,
|
276 |
+
value=0.5,
|
277 |
+
interactive=True,
|
278 |
+
step=.1,
|
279 |
+
label="Alpha",
|
280 |
+
scale=1,
|
281 |
+
)
|
282 |
+
alpha.description = "Here you can choose the alpha value."
|
283 |
+
alpha.description_place = "left"
|
284 |
+
|
285 |
+
layer = gr.Radio(
|
286 |
+
["layer1", "layer2", "layer3", "layer4", "all"],
|
287 |
+
label="Layer",
|
288 |
+
value="layer4",
|
289 |
+
interactive=True,
|
290 |
+
scale=2,
|
291 |
+
)
|
292 |
+
layer.description = "Here you can choose the layer to visualize."
|
293 |
+
layer.description_place = "left"
|
294 |
+
|
295 |
+
animal_to_explain = gr.Dropdown(
|
296 |
+
choices=["Predicted Class"] + ALL_CLASSES,
|
297 |
+
label="Animal",
|
298 |
+
value="Predicted Class",
|
299 |
+
interactive=True,
|
300 |
+
scale=2,
|
301 |
+
)
|
302 |
+
animal_to_explain.description = "Here you can choose the animal to explain. If you choose 'Predicted Class' the method will explain the predicted class."
|
303 |
+
animal_to_explain.description_place = "center"
|
304 |
+
|
305 |
+
with gr.Column():
|
306 |
+
output_cam = gr.Image(
|
307 |
+
type="pil",
|
308 |
+
label="GradCAM",
|
309 |
+
info="GradCAM visualization"
|
310 |
+
|
311 |
+
)
|
312 |
+
|
313 |
+
gradcam_mode_button = gr.Button(value="Show GradCAM", label="GradCAM", info="Click to make a prediction.", scale=1)
|
314 |
+
gradcam_mode_button.click(fn=gradcam, inputs=[user_image, user_image_sketched, alpha, cam_method, layer, animal_to_explain], outputs=output_cam, queue=True)
|
315 |
+
|
316 |
+
# -------------------------------------------
|
317 |
+
# GIF CAM
|
318 |
+
# -------------------------------------------
|
319 |
+
with gr.Tab("Gif Cam"):
|
320 |
+
build_video_to_camvideo(CAM_METHODS, ALL_CLASSES, gradcam_video)
|
321 |
+
|
322 |
+
# -------------------------------------------
|
323 |
+
# EXAMPLES
|
324 |
+
# -------------------------------------------
|
325 |
+
with gr.Tab("Example Images"):
|
326 |
placeholder = gr.Markdown("## Example Images")
|
327 |
+
loaded_images = load_examples()
|
328 |
+
for k in loaded_images.keys():
|
329 |
+
for image in loaded_images[k]:
|
330 |
+
image.select(fn=lambda x: x, inputs=[image], outputs=[user_image])
|
331 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
332 |
|
333 |
|
334 |
|
requirements.txt
CHANGED
Binary files a/requirements.txt and b/requirements.txt differ
|
|
src/Nets.py
CHANGED
@@ -41,7 +41,7 @@ class SimpleCNN(nn.Module):
|
|
41 |
class CustomResNet18(nn.Module):
|
42 |
def __init__(self, num_classes=11):
|
43 |
super(CustomResNet18, self).__init__()
|
44 |
-
self.resnet = models.
|
45 |
num_features = self.resnet.fc.in_features
|
46 |
self.resnet.fc = nn.Linear(num_features, num_classes)
|
47 |
|
|
|
41 |
class CustomResNet18(nn.Module):
|
42 |
def __init__(self, num_classes=11):
|
43 |
super(CustomResNet18, self).__init__()
|
44 |
+
self.resnet = models.resnet50(pretrained=True)
|
45 |
num_features = self.resnet.fc.in_features
|
46 |
self.resnet.fc = nn.Linear(num_features, num_classes)
|
47 |
|
src/cache/val_df.csv
CHANGED
The diff for this file is too large to render.
See raw diff
|
|
src/examples/AI_Generated/crow.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/donkey.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/eagle.png
ADDED
![]() |
Git LFS Details
|
src/examples/{dragonfly/353bd2bd65.jpg → AI_Generated/elephant (2).png}
RENAMED
File without changes
|
src/examples/AI_Generated/elephant.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/fox.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/goat (2).png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/goat.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/goldfish.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/jellyfish.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/koala.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/otter.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/panda.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/penguin.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/pigeon.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/rabbit.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/rhinoceros (2).png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/rhinoceros.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/snake.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/swan.png
ADDED
![]() |
Git LFS Details
|
src/examples/AI_Generated/woodpecker.png
ADDED
![]() |
Git LFS Details
|
src/examples/antelope/1d556456dc.jpg
DELETED
Git LFS Details
|
src/examples/badger/0836f4eb45.jpg
DELETED
Git LFS Details
|
src/examples/badger/23bfad16a7.jpg
DELETED
Git LFS Details
|
src/examples/badger/4c273d12a9.jpg
DELETED
Git LFS Details
|
src/examples/badger/5bffbd51cf.jpg
DELETED
Git LFS Details
|
src/examples/badger/87d1db4af3.jpg
DELETED
Git LFS Details
|
src/examples/badger/89a8316cd4.jpg
DELETED
Git LFS Details
|
src/examples/badger/99e296bf48.jpg
DELETED
Git LFS Details
|
src/examples/bat/16f6af0091.jpg
DELETED
Git LFS Details
|
src/examples/bat/1dd514de63.jpg
DELETED
Git LFS Details
|
src/examples/bat/1fd53c0b98.jpg
DELETED
Git LFS Details
|
src/examples/bat/2d028b789d.jpg
DELETED
Git LFS Details
|
src/examples/bat/2f7c6c7cd5.jpg
DELETED
Git LFS Details
|
src/examples/bat/330e4a8053.jpg
DELETED
Git LFS Details
|
src/examples/bat/47d2c91d9b.jpg
DELETED
Git LFS Details
|
src/examples/bat/513bb906a6.jpg
DELETED
Git LFS Details
|
src/examples/bat/5e85312fa8.jpg
DELETED
Git LFS Details
|
src/examples/bat/6b4b95f0c4.jpg
DELETED
Git LFS Details
|
src/examples/bat/6da14f603d.jpg
DELETED
Git LFS Details
|
src/examples/bat/741fa84ed0.jpg
DELETED
Git LFS Details
|
src/examples/bear/116d9b7f88.jpg
DELETED
Git LFS Details
|