Spaces:
Build error
Build error
Commit ·
750f986
0
Parent(s):
Duplicate from remotewith/dented_final
Browse files- .gitattributes +34 -0
- README.md +14 -0
- app.py +249 -0
- car_model.pth +3 -0
- examples/0003.JPEG +0 -0
- examples/0008.jpg +0 -0
- model.py +36 -0
- requirements.txt +7 -0
.gitattributes
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Dented Final
|
| 3 |
+
emoji: ⚡
|
| 4 |
+
colorFrom: pink
|
| 5 |
+
colorTo: indigo
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.33.1
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
duplicated_from: remotewith/dented_final
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### 1. Imports and class names setup ###
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
import torch
|
| 6 |
+
import numpy as np
|
| 7 |
+
from roboflow import Roboflow
|
| 8 |
+
import cv2
|
| 9 |
+
|
| 10 |
+
rf = Roboflow(api_key="gjZE3lykkitagkxHplyJ")
|
| 11 |
+
project = rf.workspace().project("rideit")
|
| 12 |
+
model = project.version(1).model
|
| 13 |
+
|
| 14 |
+
from model import create_effnetb2_model
|
| 15 |
+
from timeit import default_timer as timer
|
| 16 |
+
from typing import Tuple, Dict
|
| 17 |
+
|
| 18 |
+
file_urls = [
|
| 19 |
+
'https://www.dropbox.com/s/7sjfwncffg8xej2/video_7.mp4?dl=1'
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
def download_file(url, save_name):
|
| 23 |
+
url = url
|
| 24 |
+
if not os.path.exists(save_name):
|
| 25 |
+
file = requests.get(url)
|
| 26 |
+
open(save_name, 'wb').write(file.content)
|
| 27 |
+
|
| 28 |
+
for i, url in enumerate(file_urls):
|
| 29 |
+
if 'mp4' in file_urls[i]:
|
| 30 |
+
download_file(
|
| 31 |
+
file_urls[i],
|
| 32 |
+
f"video.mp4"
|
| 33 |
+
)
|
| 34 |
+
else:
|
| 35 |
+
download_file(
|
| 36 |
+
file_urls[i],
|
| 37 |
+
f"image_{i}.jpg"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
video_path = [['video.mp4']]
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# Setup class names
|
| 45 |
+
class_names = ["dented","good"]
|
| 46 |
+
|
| 47 |
+
### 2. Model and transforms preparation ###
|
| 48 |
+
|
| 49 |
+
# Create EffNetB2 model
|
| 50 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(
|
| 51 |
+
num_classes=2, # len(class_names) would also work
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Load saved weights
|
| 55 |
+
effnetb2.load_state_dict(
|
| 56 |
+
torch.load(
|
| 57 |
+
f="car_model.pth",
|
| 58 |
+
map_location=torch.device("cpu"), # load to CPU
|
| 59 |
+
)
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
### 3. Predict function ###
|
| 63 |
+
|
| 64 |
+
def normalize_2d(matrix):
|
| 65 |
+
# Only this is changed to use 2-norm put 2 instead of 1
|
| 66 |
+
norm = np.linalg.norm(matrix)
|
| 67 |
+
# normalized matrix
|
| 68 |
+
matrix = matrix/norm
|
| 69 |
+
return matrix
|
| 70 |
+
|
| 71 |
+
# Create predict function
|
| 72 |
+
|
| 73 |
+
# Detect the image
|
| 74 |
+
|
| 75 |
+
def detect(imagepath):
|
| 76 |
+
|
| 77 |
+
pix=model.predict(imagepath, confidence=40, overlap=30)
|
| 78 |
+
pix=pix.json()
|
| 79 |
+
img=cv2.imread(imagepath)
|
| 80 |
+
|
| 81 |
+
x1,x2,y1,y2=[],[],[],[]
|
| 82 |
+
for i in pix.keys():
|
| 83 |
+
if i=="predictions":
|
| 84 |
+
for j in pix["predictions"]:
|
| 85 |
+
for a,b in j.items():
|
| 86 |
+
if a=="x":
|
| 87 |
+
x1.append(b)
|
| 88 |
+
if a=="y":
|
| 89 |
+
y1.append(b)
|
| 90 |
+
if a=="width":
|
| 91 |
+
x2.append(b)
|
| 92 |
+
if a=="height":
|
| 93 |
+
y2.append(b)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
for p in range(0,len(x1)):
|
| 98 |
+
x2[p]=x2[p]+x1[p]
|
| 99 |
+
|
| 100 |
+
for p in range(0,len(x1)):
|
| 101 |
+
y2[p]=y2[p]+x1[p]
|
| 102 |
+
|
| 103 |
+
for (x11,y11,x12,y12) in zip(x1,y1,x2,y2):
|
| 104 |
+
cv2.rectangle(
|
| 105 |
+
img,
|
| 106 |
+
(x11,y11),
|
| 107 |
+
(x12,y12),
|
| 108 |
+
color=(0, 0, 255),
|
| 109 |
+
thickness=2,
|
| 110 |
+
lineType=cv2.LINE_AA
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 114 |
+
#cv2.imshow("kamehamehaa",img)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def predict(img):
|
| 122 |
+
|
| 123 |
+
start_time = timer()
|
| 124 |
+
|
| 125 |
+
# Transform the target image and add a batch dimension
|
| 126 |
+
|
| 127 |
+
img1 = effnetb2_transforms(img).unsqueeze(0)
|
| 128 |
+
pix = normalize_2d(np.array(img))
|
| 129 |
+
|
| 130 |
+
#pix1=model.predict(str(image), confidence=40, overlap=30).numpy()
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
# Put model into evaluation mode and turn on inference mode
|
| 134 |
+
effnetb2.eval()
|
| 135 |
+
with torch.inference_mode():
|
| 136 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
| 137 |
+
pred_probs = torch.softmax(effnetb2(img1), dim=1)
|
| 138 |
+
|
| 139 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
| 140 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
| 141 |
+
|
| 142 |
+
# Calculate the prediction time
|
| 143 |
+
pred_time = round(timer() - start_time, 5)
|
| 144 |
+
|
| 145 |
+
# Return the prediction dictionary and prediction time
|
| 146 |
+
return pred_labels_and_probs, pred_time
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def show_preds_video(video_path):
|
| 150 |
+
cap = cv2.VideoCapture(video_path)
|
| 151 |
+
while(cap.isOpened()):
|
| 152 |
+
ret, frame = cap.read()
|
| 153 |
+
if ret:
|
| 154 |
+
frame_copy = frame.copy()
|
| 155 |
+
pix=model.predict(frame, confidence=40, overlap=30)
|
| 156 |
+
pix=pix.json()
|
| 157 |
+
x1,x2,y1,y2=[],[],[],[]
|
| 158 |
+
for i in pix.keys():
|
| 159 |
+
if i=="predictions":
|
| 160 |
+
for j in pix["predictions"]:
|
| 161 |
+
for a,b in j.items():
|
| 162 |
+
if a=="x":
|
| 163 |
+
x1.append(b)
|
| 164 |
+
if a=="y":
|
| 165 |
+
y1.append(b)
|
| 166 |
+
if a=="width":
|
| 167 |
+
x2.append(b)
|
| 168 |
+
if a=="height":
|
| 169 |
+
y2.append(b)
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
for p in range(0,len(x1)):
|
| 174 |
+
x2[p]=x2[p]+x1[p]
|
| 175 |
+
|
| 176 |
+
for p in range(0,len(x1)):
|
| 177 |
+
y2[p]=y2[p]+x1[p]
|
| 178 |
+
|
| 179 |
+
for (x11,y11,x12,y12) in zip(x1,y1,x2,y2):
|
| 180 |
+
cv2.rectangle(
|
| 181 |
+
img,
|
| 182 |
+
(x11,y11),
|
| 183 |
+
(x12,y12),
|
| 184 |
+
color=(0, 0, 255),
|
| 185 |
+
thickness=2,
|
| 186 |
+
lineType=cv2.LINE_AA
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
### 4. Gradio app ###
|
| 194 |
+
|
| 195 |
+
# Create title, description and article strings
|
| 196 |
+
title = "Dented car Detector"
|
| 197 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify images of dented or good cars."
|
| 198 |
+
article = "(https://www.learnpytorch.io/)."
|
| 199 |
+
|
| 200 |
+
# Create examples list from "examples/" directory
|
| 201 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
| 202 |
+
|
| 203 |
+
inputs_image = [
|
| 204 |
+
gr.components.Image(type="filepath", label="Input Image"),
|
| 205 |
+
]
|
| 206 |
+
|
| 207 |
+
outputs_image = [
|
| 208 |
+
gr.components.Image(type="numpy", label="Output Image"),
|
| 209 |
+
]
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
inputs_video = [
|
| 213 |
+
gr.components.Video(type="filepath", label="Input Video"),
|
| 214 |
+
|
| 215 |
+
]
|
| 216 |
+
outputs_video = [
|
| 217 |
+
gr.components.Image(type="numpy", label="Output Image"),
|
| 218 |
+
]
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
# Create the Gradio demo
|
| 222 |
+
app1 = gr.Interface(fn=predict, # mapping function from input to output
|
| 223 |
+
inputs=gr.Image(type="pil"), # what are the inputs?
|
| 224 |
+
outputs=[gr.Label(num_top_classes=2, label="Predictions"), # what are the outputs?
|
| 225 |
+
gr.Number(label="Prediction time (s)")
|
| 226 |
+
], # our fn has two outputs, therefore we have two outputs
|
| 227 |
+
# Create examples list from "examples/" directory
|
| 228 |
+
examples=example_list,
|
| 229 |
+
title=title,
|
| 230 |
+
description=description,
|
| 231 |
+
article=article)
|
| 232 |
+
|
| 233 |
+
app2=gr.Interface(fn=detect,
|
| 234 |
+
inputs=inputs_image,
|
| 235 |
+
outputs=outputs_image,
|
| 236 |
+
title=title)
|
| 237 |
+
app3=gr.Interface(
|
| 238 |
+
fn=show_preds_video,
|
| 239 |
+
inputs=inputs_video,
|
| 240 |
+
outputs=outputs_video,
|
| 241 |
+
examples=video_path,
|
| 242 |
+
cache_examples=False,
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
demo = gr.TabbedInterface([app1, app2,app3], ["Classify", "Detect","Video Interface"])
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
# Launch the demo!
|
| 249 |
+
demo.launch()
|
car_model.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6940f1cb63d97157edbd0ec03e4b9dea0d008e53f301771a7cc270a55de2fb3d
|
| 3 |
+
size 31261637
|
examples/0003.JPEG
ADDED
|
|
examples/0008.jpg
ADDED
|
model.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision
|
| 3 |
+
|
| 4 |
+
from torch import nn
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def create_effnetb2_model(num_classes:int=2,
|
| 8 |
+
seed:int=42):
|
| 9 |
+
"""Creates an EfficientNetB2 feature extractor model and transforms.
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
num_classes (int, optional): number of classes in the classifier head.
|
| 13 |
+
Defaults to 3.
|
| 14 |
+
seed (int, optional): random seed value. Defaults to 42.
|
| 15 |
+
|
| 16 |
+
Returns:
|
| 17 |
+
model (torch.nn.Module): EffNetB2 feature extractor model.
|
| 18 |
+
transforms (torchvision.transforms): EffNetB2 image transforms.
|
| 19 |
+
"""
|
| 20 |
+
# Create EffNetB2 pretrained weights, transforms and model
|
| 21 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
| 22 |
+
transforms = weights.transforms()
|
| 23 |
+
model = torchvision.models.efficientnet_b2(weights=weights)
|
| 24 |
+
|
| 25 |
+
# Freeze all layers in base model
|
| 26 |
+
for param in model.parameters():
|
| 27 |
+
param.requires_grad = False
|
| 28 |
+
|
| 29 |
+
# Change classifier head with random seed for reproducibility
|
| 30 |
+
torch.manual_seed(seed)
|
| 31 |
+
model.classifier = nn.Sequential(
|
| 32 |
+
nn.Dropout(p=0.3, inplace=True),
|
| 33 |
+
nn.Linear(in_features=1408, out_features=num_classes),
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
return model, transforms
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==1.12.0
|
| 2 |
+
torchvision==0.13.0
|
| 3 |
+
gradio==3.1.4
|
| 4 |
+
numpy==1.24.3
|
| 5 |
+
roboflow
|
| 6 |
+
opencv-python>=4.1.1
|
| 7 |
+
requests
|