yellowdolphin commited on
Commit
fa37760
·
1 Parent(s): ebf1075

initial version

Browse files
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from pathlib import Path
3
+ from subprocess import run
4
+
5
+ import gradio as gr
6
+
7
+
8
+ yolo_input_size = 384
9
+ versions = ('2_v108', '4_v109', '0_int6', '1_v110', '3_v111')
10
+
11
+ score_thr = 0.025
12
+ zoom_score_thr = 0.35
13
+ iou_thr = 0.6
14
+ max_det = 1
15
+ yolo_ens = 'fast' # fast, val, detect, detect_internal, all
16
+ output_size = (512, 512)
17
+ bs = 1 #128 if 'CUDA_VERSION' in os.environ else 16
18
+
19
+ project_dir = None
20
+ working = Path(os.getcwd())
21
+ modelbox = working / 'models'
22
+ checkpoint_files = [modelbox / f'yolov5_l6_{yolo_input_size}_fold{x}.pt' for x in versions]
23
+ image_root = working / 'images' / 'subdir'
24
+
25
+ image_urls = [
26
+ # Negatives
27
+ 'https://upload.wikimedia.org/wikipedia/commons/c/c5/Common_Dolphin.jpg',
28
+ 'https://upload.wikimedia.org/wikipedia/commons/b/b8/Beluga847.jpg',
29
+ 'https://upload.wikimedia.org/wikipedia/commons/e/ea/Beluga_1_1999-07-03.jpg',
30
+ 'https://upload.wikimedia.org/wikipedia/commons/2/2b/Whale_Watching_in_Gloucester%2C_Massachusetts_5.jpg',
31
+
32
+ # Positives
33
+ '/kaggle/input/happy-whale-and-dolphin/test_images/00098d1376dab2.jpg',
34
+ ]
35
+ yolo_source = f'{image_root}/negative001.jpg'
36
+
37
+
38
+ def pred_fn(image, fake=True):
39
+ if fake:
40
+ x0, x1 = (int(f * image.shape[0]) for f in (0.2, 0.8))
41
+ y0, y1 = (int(f * image.shape[1]) for f in (0.2, 0.8))
42
+ cropped_image = image[x0:x1, y0:y1, :]
43
+ response_str = f"This looks like a common dolphin, but I have not seen this individual before (0.834 confidence).\n" \
44
+ "Go submit your photo on www.happywhale.com!"
45
+ return cropped_image, response_str
46
+
47
+ cropped_image, species = fast_yolo_crop(image)
48
+ test_embedding = get_test_embedding(embed_models, sizes)
49
+
50
+ cosine_similarity = np.dot(comp_embeddings, test_embedding[0]) / n_models
51
+ cosine_distances = 1 - cosine_similarity
52
+ normalized_distances = cosine_distances / max_distance
53
+ normalized_similarities = 1 - normalized_distances
54
+
55
+ min_similarity = normalized_similarities.min()
56
+ max_similarity = normalized_similarities.max()
57
+ confidence = get_confidence(max_similarity, threshold)
58
+
59
+ print(f"Similarities: {min_similarity:.4f} ... {max_similarity:.4f}")
60
+ print(f"Threshold:", threshold)
61
+
62
+ if max_similarity > threshold:
63
+ response_str = f"This looks like a {species} I have seen before ({confidence:.3f} confidence).\n" \
64
+ "You might find its previous encounters on www.happywhale.com"
65
+ else:
66
+ response_str = f"This looks like a {species}, but I have not seen this individual before ({confidence:.3f} confidence).\n" \
67
+ "Go submit your photo on www.happywhale.com!"
68
+
69
+ return cropped_image, response_str
70
+
71
+ examples = [str(image_root / f'negative{i:03d}') for i in range(3)]
72
+
73
+ demo = gr.Interface(fn=pred_fn, inputs="image", outputs=["image", "text"],
74
+ examples=examples)
75
+ demo.launch()
images/00021adfb725ed.jpg ADDED
images/000be9acf46619.jpg ADDED
images/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ Images in this folder are licensed under the Creative Commons Attribution-Share Alike 4.0 International license:
2
+ https://creativecommons.org/licenses/by-sa/4.0/deed.en
3
+
4
+ Attribution/Origin:
5
+ negative001.jpg https://upload.wikimedia.org/wikipedia/commons/2/2b/Whale_Watching_in_Gloucester%2C_Massachusetts_5.jpg
6
+ negative002.jpg https://upload.wikimedia.org/wikipedia/commons/c/c5/Common_Dolphin.jpg
7
+
images/negative000.jpg ADDED
images/negative001.jpg ADDED
images/negative002.jpg ADDED