ChristopherMarais commited on
Commit
f4453f1
·
1 Parent(s): c33581f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -45
app.py CHANGED
@@ -1,32 +1,16 @@
1
- import os
2
- import dill
3
- import timm
4
  import random
5
  import numpy as np
6
  import gradio as gr
7
- from fastai.tabular.all import *
8
- from fastai.vision.all import *
9
- from fastai.vision.utils import get_image_files
10
- from fastai.learner import load_learner
11
- from Ambrosia import pre_process_image
12
- from huggingface_hub import from_pretrained_fastai, push_to_hub_fastai, hf_hub_download
 
 
13
  from torchvision.transforms import GaussianBlur
14
 
15
- # Set the token
16
- huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
17
- if huggingface_token is None:
18
- raise ValueError("Hugging Face token not found. Please set the HUGGINGFACE_TOKEN environment variable.")
19
-
20
-
21
- # # Define a custom transform for Gaussian blur
22
- # def gaussian_blur(x, p=0.5, kernel_size_min=3, kernel_size_max=9, sigma_min=0.1, sigma_max=2):
23
- # if x.ndim == 4:
24
- # for i in range(x.shape[0]):
25
- # if random.random() < p:
26
- # kernel_size = random.randrange(kernel_size_min, kernel_size_max + 1, 2)
27
- # sigma = random.uniform(sigma_min, sigma_max)
28
- # x[i] = GaussianBlur(kernel_size=kernel_size, sigma=sigma)(x[i])
29
- # return x
30
 
31
  # Define a custom transform for Gaussian blur
32
  def gaussian_blur(x, p=0.5, kernel_size_min=3, kernel_size_max=20, sigma_min=0.1, sigma_max=3):
@@ -38,6 +22,12 @@ def gaussian_blur(x, p=0.5, kernel_size_min=3, kernel_size_max=20, sigma_min=0.1
38
  x[i] = GaussianBlur(kernel_size=kernel_size, sigma=sigma)(x[i])
39
  return x
40
 
 
 
 
 
 
 
41
  # this function only describes how much a singular value in al ist stands out.
42
  # if all values in the lsit are high or low this is 1
43
  # the smaller the proportiopn of number of disimilar vlaues are to other more similar values the lower this number
@@ -65,26 +55,78 @@ def unkown_prob_calc(probs, wedge_threshold, wedge_magnitude=1, wedge='strict'):
65
  unknown_prob = 1-kown_prob
66
  return(unknown_prob)
67
 
68
- # load model
69
- learn = from_pretrained_fastai("ChristopherMarais/beetle-model")
70
- # learn = load_learner(
71
- # hf_hub_download('ChristopherMarais/Andrew_Alpha_model', filename="model.pkl")
72
- # )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  # get class names
75
- labels = np.append(np.array(learn.dls.vocab), "Unknown")
76
-
77
- def predict(img):
78
- # Segment image into smaller images
79
- pre_process = pre_process_image(manual_thresh_buffer=0.15, image = img) # use image_dir if directory of image used
80
- pre_process.segment(cluster_num=2,
81
- image_edge_buffer=50)
82
- # get predictions for all segments
83
  conf_dict_lst = []
84
  output_lst = []
85
- img_cnt = len(pre_process.col_image_lst)
86
  for i in range(0,img_cnt):
87
- prob_ar = np.array(learn.predict(pre_process.col_image_lst[i])[2])
88
  unkown_prob = unkown_prob_calc(probs=prob_ar, wedge_threshold=0.85, wedge_magnitude=5, wedge='dynamic')
89
  prob_ar = np.append(prob_ar, unkown_prob)
90
  prob_ar = np.around(prob_ar*100, decimals=1)
@@ -92,19 +134,20 @@ def predict(img):
92
  conf_dict = {labels[i]: float(prob_ar[i]) for i in range(len(prob_ar))}
93
  conf_dict = dict(sorted(conf_dict.items(), key=lambda item: item[1], reverse=True))
94
  conf_dict_lst.append(str(conf_dict))
95
- result = list(zip(pre_process.col_image_lst, conf_dict_lst))
96
-
97
  return(result)
98
 
 
 
99
  with gr.Blocks() as demo:
100
  with gr.Column(variant="panel"):
101
  with gr.Row(variant="compact"):
102
  inputs = gr.Image()
103
- btn = gr.Button("Classify").style(full_width=False)
104
 
105
  gallery = gr.Gallery(
106
  label="Show images", show_label=True, elem_id="gallery"
107
- ).style(grid=[8], height="auto")
108
 
109
- btn.click(predict, inputs, gallery)
110
- demo.launch()
 
 
 
 
1
  import random
2
  import numpy as np
3
  import gradio as gr
4
+ from huggingface_hub import from_pretrained_fastai
5
+ from PIL import Image
6
+ from groundingdino.util.inference import load_model
7
+ from groundingdino.util.inference import predict as grounding_dino_predict
8
+ import groundingdino.datasets.transforms as T
9
+ import torch
10
+ from torchvision.ops import box_convert
11
+ from torchvision.transforms.functional import to_tensor
12
  from torchvision.transforms import GaussianBlur
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  # Define a custom transform for Gaussian blur
16
  def gaussian_blur(x, p=0.5, kernel_size_min=3, kernel_size_max=20, sigma_min=0.1, sigma_max=3):
 
22
  x[i] = GaussianBlur(kernel_size=kernel_size, sigma=sigma)(x[i])
23
  return x
24
 
25
+ # Custom Label Function
26
+ def custom_label_func(fpath):
27
+ # this directs the labels to be 2 levels up from the image folder
28
+ label = fpath.parents[2].name
29
+ return label
30
+
31
  # this function only describes how much a singular value in al ist stands out.
32
  # if all values in the lsit are high or low this is 1
33
  # the smaller the proportiopn of number of disimilar vlaues are to other more similar values the lower this number
 
55
  unknown_prob = 1-kown_prob
56
  return(unknown_prob)
57
 
58
+ def load_image(image_source):
59
+ transform = T.Compose(
60
+ [
61
+ T.RandomResize([800], max_size=1333),
62
+ T.ToTensor(),
63
+ T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
64
+ ]
65
+ )
66
+ image_source = image_source.convert("RGB")
67
+
68
+ image_transformed, _ = transform(image_source, None)
69
+ return image_transformed
70
+
71
+ # load object detection model
72
+ od_model = load_model(
73
+ model_checkpoint_path="groundingdino_swint_ogc.pth",
74
+ model_config_path="GroundingDINO_SwinT_OGC.cfg.py",
75
+ device="cpu")
76
+
77
+ def detect_objects(og_image, model=od_model, prompt="bug . insect", device="cpu"):
78
+ TEXT_PROMPT = prompt
79
+ BOX_TRESHOLD = 0.35
80
+ TEXT_TRESHOLD = 0.25
81
+ DEVICE = device # cuda or cpu
82
+
83
+ # Convert numpy array to PIL Image if needed
84
+ if isinstance(og_image, np.ndarray):
85
+ og_image_obj = Image.fromarray(og_image)
86
+ else:
87
+ og_image_obj = og_image # Assuming og_image is already a PIL Image
88
+
89
+ # Transform the image
90
+ image_transformed = load_image(image_source = og_image_obj)
91
 
92
+ # Your model prediction code here...
93
+ boxes, logits, phrases = grounding_dino_predict(
94
+ model=model,
95
+ image=image_transformed,
96
+ caption=TEXT_PROMPT,
97
+ box_threshold=BOX_TRESHOLD,
98
+ text_threshold=TEXT_TRESHOLD,
99
+ device=DEVICE)
100
+
101
+ # Use og_image_obj directly for further processing
102
+ height, width = og_image_obj.size
103
+ boxes_norm = boxes * torch.Tensor([height, width, height, width])
104
+ xyxy = box_convert(
105
+ boxes=boxes_norm,
106
+ in_fmt="cxcywh",
107
+ out_fmt="xyxy").numpy()
108
+ img_lst = []
109
+ for i in range(len(boxes_norm)):
110
+ crop_img = og_image_obj.crop((xyxy[i]))
111
+ img_lst.append(crop_img)
112
+ return (img_lst)
113
+
114
+
115
+ # load beetle classifier model
116
+ repo_id="ChristopherMarais/beetle-model"
117
+ bc_model = from_pretrained_fastai(repo_id)
118
  # get class names
119
+ labels = np.append(np.array(bc_model.dls.vocab), "Unknown")
120
+
121
+ def predict_beetle(img):
122
+ # Split image into smaller images of detected objects
123
+ image_lst = detect_objects(og_image=img, model=od_model, prompt="bug . insect", device="cpu")
124
+ # get predictions for all segments
 
 
125
  conf_dict_lst = []
126
  output_lst = []
127
+ img_cnt = len(image_lst)
128
  for i in range(0,img_cnt):
129
+ prob_ar = np.array(bc_model.predict(image_lst[i])[2])
130
  unkown_prob = unkown_prob_calc(probs=prob_ar, wedge_threshold=0.85, wedge_magnitude=5, wedge='dynamic')
131
  prob_ar = np.append(prob_ar, unkown_prob)
132
  prob_ar = np.around(prob_ar*100, decimals=1)
 
134
  conf_dict = {labels[i]: float(prob_ar[i]) for i in range(len(prob_ar))}
135
  conf_dict = dict(sorted(conf_dict.items(), key=lambda item: item[1], reverse=True))
136
  conf_dict_lst.append(str(conf_dict))
137
+ result = list(zip(image_lst, conf_dict_lst))
 
138
  return(result)
139
 
140
+
141
+ # gradio app
142
  with gr.Blocks() as demo:
143
  with gr.Column(variant="panel"):
144
  with gr.Row(variant="compact"):
145
  inputs = gr.Image()
146
+ btn = gr.Button("Classify")
147
 
148
  gallery = gr.Gallery(
149
  label="Show images", show_label=True, elem_id="gallery"
150
+ )
151
 
152
+ btn.click(predict_beetle, inputs, gallery)
153
+ demo.launch()