Spaces:
Build error
Build error
ekenkel
commited on
Commit
·
43d44f1
1
Parent(s):
0754a04
added potential fix to GELU error
Browse files- app.py +6 -0
- modelCreation.py +79 -79
- requirements.txt +1 -0
app.py
CHANGED
@@ -2,6 +2,12 @@ from fastai.vision.all import *
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
URL = 'https://dog.ceo/api/breeds/list/all'
|
7 |
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import json
|
5 |
+
from torch import Tensor
|
6 |
+
import torch.nn.functional as F
|
7 |
+
|
8 |
+
class GELU(nn.Module):
|
9 |
+
def forward(self, input: Tensor) -> Tensor:
|
10 |
+
return F.gelu(input)
|
11 |
|
12 |
URL = 'https://dog.ceo/api/breeds/list/all'
|
13 |
|
modelCreation.py
CHANGED
@@ -1,79 +1,79 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
1 |
+
import timm
|
2 |
+
from fastcore.all import *
|
3 |
+
from fastdownload import download_url
|
4 |
+
from fastai.vision.widgets import *
|
5 |
+
from fastai.vision.all import *
|
6 |
+
import os, shutil
|
7 |
+
import time
|
8 |
+
import requests
|
9 |
+
import json
|
10 |
+
from azure.cognitiveservices.search.imagesearch import ImageSearchClient as api
|
11 |
+
from msrest.authentication import CognitiveServicesCredentials as auth
|
12 |
+
|
13 |
+
|
14 |
+
def search_images_bing(key, term, min_sz=128, max_images=110):
|
15 |
+
params = {'q': term, 'count': max_images, 'min_height': min_sz, 'min_width': min_sz}
|
16 |
+
headers = {"Ocp-Apim-Subscription-Key": key}
|
17 |
+
search_url = "https://api.bing.microsoft.com/v7.0/images/search"
|
18 |
+
response = requests.get(search_url, headers=headers, params=params)
|
19 |
+
response.raise_for_status()
|
20 |
+
search_results = response.json()
|
21 |
+
return L(search_results['value'])
|
22 |
+
|
23 |
+
|
24 |
+
URL = 'https://dog.ceo/api/breeds/list/all'
|
25 |
+
|
26 |
+
result = requests.get(url = URL).json()
|
27 |
+
searchText = []
|
28 |
+
for val in result['message'].items():
|
29 |
+
if len(val[1]) > 0:
|
30 |
+
for type in val[1]:
|
31 |
+
searchText.append(f'{type} {val[0]}')
|
32 |
+
else:
|
33 |
+
searchText.append(val[0])
|
34 |
+
|
35 |
+
path = Path('Dog_Types')
|
36 |
+
|
37 |
+
key = os.environ.get('AZURE_SEARCH_KEY', 'INSERT KEY HERE')
|
38 |
+
|
39 |
+
if not path.exists():
|
40 |
+
path.mkdir()
|
41 |
+
else:
|
42 |
+
folder = 'Dog_Types'
|
43 |
+
for filename in os.listdir(folder):
|
44 |
+
file_path = os.path.join(folder, filename)
|
45 |
+
try:
|
46 |
+
if os.path.isfile(file_path) or os.path.islink(file_path):
|
47 |
+
os.unlink(file_path)
|
48 |
+
elif os.path.isdir(file_path):
|
49 |
+
shutil.rmtree(file_path)
|
50 |
+
except Exception as e:
|
51 |
+
print('Failed to delete %s. Reason: %s' % (file_path, e))
|
52 |
+
|
53 |
+
for o in searchText:
|
54 |
+
try:
|
55 |
+
dest = (path / o)
|
56 |
+
dest.mkdir(exist_ok=True, parents=True)
|
57 |
+
results = search_images_bing(key, f'{o} dog photo')
|
58 |
+
download_images(dest, urls=results.attrgot('contentUrl'))
|
59 |
+
except shutil.SameFileError:
|
60 |
+
pass
|
61 |
+
|
62 |
+
for breed in searchText:
|
63 |
+
failed = verify_images(get_image_files(f'{path}/{breed}'))
|
64 |
+
failed.map(Path.unlink)
|
65 |
+
|
66 |
+
|
67 |
+
dataloaders = DataBlock(
|
68 |
+
blocks=(ImageBlock, CategoryBlock),
|
69 |
+
get_items=get_image_files,
|
70 |
+
splitter=RandomSplitter(valid_pct=0.2, seed=42),
|
71 |
+
get_y=parent_label,
|
72 |
+
item_tfms=Resize(128)
|
73 |
+
).dataloaders(path)
|
74 |
+
|
75 |
+
|
76 |
+
learn = vision_learner(dataloaders, 'convnext_tiny_in22k', metrics=error_rate)
|
77 |
+
learn.fine_tune(18)
|
78 |
+
|
79 |
+
learn.export('dogIdentifierModel.pkl')
|
requirements.txt
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
timm
|
|
|
2 |
fastai
|
3 |
torch
|
4 |
gradio
|
|
|
1 |
timm
|
2 |
+
json
|
3 |
fastai
|
4 |
torch
|
5 |
gradio
|