vapit commited on
Commit
37632e1
·
1 Parent(s): d7eaebd

add final code

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ convnext_model.pth filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: DogVision
3
- emoji: 🏢
4
  colorFrom: gray
5
  colorTo: indigo
6
  sdk: gradio
 
1
  ---
2
  title: DogVision
3
+ emoji: 🐶
4
  colorFrom: gray
5
  colorTo: indigo
6
  sdk: gradio
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 1. Imports and class names setup ###
2
+ from model import create_model
3
+ import pandas as pd
4
+ import torch
5
+ from typing import Tuple, Dict
6
+ from timeit import default_timer as timer
7
+ import gradio as gr
8
+ import os
9
+ import numpy as np
10
+
11
+ ### 1. Create a Dictionary for Dog Breeds
12
+ labels_csv = pd.read_csv('./labels.csv')
13
+ labels = labels_csv['breed']
14
+ labels = np.array(labels)
15
+ unique_labels = np.unique(labels)
16
+
17
+ ### 2. Model and transforms preparation ###
18
+ model, model_transforms = create_model(num_classes=len(unique_labels))
19
+ model = torch.compile(model)
20
+
21
+
22
+ # Load save weights
23
+ model.load_state_dict(torch.load(f='./convnext_model.pth', map_location='cpu',weights_only=True))
24
+
25
+ # 3. Predict Function
26
+
27
+ def predict(img) -> Tuple[Dict[str, float], str]:
28
+ """
29
+ Predicts the class probabilities for a given image using a pre-trained model.
30
+
31
+ Args:
32
+ img: A PIL image to be predicted.
33
+
34
+ Returns:
35
+ A tuple containing:
36
+ - A formatted string displaying class labels and their respective probabilities.
37
+ - The time taken for inference in seconds as a string.
38
+ """
39
+ # Start a timer
40
+ start_time = timer()
41
+
42
+ # Put the model into evaluation mode and disable gradient computation
43
+ model.eval()
44
+ with torch.inference_mode():
45
+
46
+ # Transform the input image for use with the model
47
+ img = model_transforms(img).unsqueeze(dim=0)
48
+
49
+ # Pass transformed image through the model
50
+ pred_logit = model(img)
51
+
52
+ # Turn prediction logits into probabilities
53
+ pred_prob = torch.softmax(pred_logit, dim=1)
54
+
55
+ pred_label = torch.argmax(pred_prob, dim=1)
56
+
57
+ # Map probabilities to class labels
58
+ prediction = unique_labels[pred_label]
59
+ probabilities = {unique_labels[i]: pred_prob[0, i].item() for i in range(len(unique_labels))}
60
+
61
+ # Calculate the time taken
62
+ end_time = timer()
63
+ inference_time = end_time - start_time
64
+
65
+ # Return predictions as a dictionary and inference time
66
+ return probabilities, f"{inference_time:.4f} seconds"
67
+ ### 4. Gradio app ###
68
+
69
+
70
+ # Create title, description and article
71
+ title = "Dogvision 🐶"
72
+ description = "A [ConvNeXt Tiny](https://pytorch.org/vision/stable/models/generated/torchvision.models.convnext_tiny.html#torchvision.models.convnext_tiny) Computer Vision Model To Classify 120 Dog Breeds 🐩 Ranging fro A Labrador 🐕 to A German Shepherd! 🐕‍🦺"
73
+ article = "Created with 🤎 (and a mixture of mathematics, statistics, and tons of calculations 👩🏽‍🔬) by Arpit Vaghela [GitHub](https://github.com/magnifiques)"
74
+
75
+ # Create example list
76
+ example_list = [["./examples/" + example] for example in os.listdir("examples")]
77
+
78
+ demo = gr.Interface(fn=predict,
79
+ inputs=gr.Image(type='pil'),
80
+ outputs=[
81
+ gr.Label(num_top_classes=3, label="Top Predictions"), # Display top predictions with probabilities
82
+ gr.Textbox(label="Prediction Time (s)") # Display inference time
83
+ ],
84
+ examples=example_list,
85
+ title=title,
86
+ description=description,
87
+ article=article)
88
+
89
+ # Launch the demo!
90
+ demo.launch(debug=False, # print errors locally?
91
+ share=True) # generate a publicly shareable URL
breeds.txt ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ boston_bull
2
+ dingo
3
+ pekinese
4
+ bluetick
5
+ golden_retriever
6
+ bedlington_terrier
7
+ borzoi
8
+ basenji
9
+ scottish_deerhound
10
+ shetland_sheepdog
11
+ walker_hound
12
+ maltese_dog
13
+ norfolk_terrier
14
+ african_hunting_dog
15
+ wire-haired_fox_terrier
16
+ redbone
17
+ lakeland_terrier
18
+ boxer
19
+ doberman
20
+ otterhound
21
+ standard_schnauzer
22
+ irish_water_spaniel
23
+ black-and-tan_coonhound
24
+ cairn
25
+ affenpinscher
26
+ labrador_retriever
27
+ ibizan_hound
28
+ english_setter
29
+ weimaraner
30
+ giant_schnauzer
31
+ groenendael
32
+ dhole
33
+ toy_poodle
34
+ border_terrier
35
+ tibetan_terrier
36
+ norwegian_elkhound
37
+ shih-tzu
38
+ irish_terrier
39
+ kuvasz
40
+ german_shepherd
41
+ greater_swiss_mountain_dog
42
+ basset
43
+ australian_terrier
44
+ schipperke
45
+ rhodesian_ridgeback
46
+ irish_setter
47
+ appenzeller
48
+ bloodhound
49
+ samoyed
50
+ miniature_schnauzer
51
+ brittany_spaniel
52
+ kelpie
53
+ papillon
54
+ border_collie
55
+ entlebucher
56
+ collie
57
+ malamute
58
+ welsh_springer_spaniel
59
+ chihuahua
60
+ saluki
61
+ pug
62
+ malinois
63
+ komondor
64
+ airedale
65
+ leonberg
66
+ mexican_hairless
67
+ bull_mastiff
68
+ bernese_mountain_dog
69
+ american_staffordshire_terrier
70
+ lhasa
71
+ cardigan
72
+ italian_greyhound
73
+ clumber
74
+ scotch_terrier
75
+ afghan_hound
76
+ old_english_sheepdog
77
+ saint_bernard
78
+ miniature_pinscher
79
+ eskimo_dog
80
+ irish_wolfhound
81
+ brabancon_griffon
82
+ toy_terrier
83
+ chow
84
+ flat-coated_retriever
85
+ norwich_terrier
86
+ soft-coated_wheaten_terrier
87
+ staffordshire_bullterrier
88
+ english_foxhound
89
+ gordon_setter
90
+ siberian_husky
91
+ newfoundland
92
+ briard
93
+ chesapeake_bay_retriever
94
+ dandie_dinmont
95
+ great_pyrenees
96
+ beagle
97
+ vizsla
98
+ west_highland_white_terrier
99
+ kerry_blue_terrier
100
+ whippet
101
+ sealyham_terrier
102
+ standard_poodle
103
+ keeshond
104
+ japanese_spaniel
105
+ miniature_poodle
106
+ pomeranian
107
+ curly-coated_retriever
108
+ yorkshire_terrier
109
+ pembroke
110
+ great_dane
111
+ blenheim_spaniel
112
+ silky_terrier
113
+ sussex_spaniel
114
+ german_short-haired_pointer
115
+ french_bulldog
116
+ bouvier_des_flandres
117
+ tibetan_mastiff
118
+ english_springer
119
+ cocker_spaniel
120
+ rottweiler
convnext_model.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1f99258fdd9b2f3a2e137dff009a5b1bc3aaea5e2b349f644fd3d03e5ec8c27c
3
+ size 111721116
examples/example1.jpg ADDED
examples/example2.jpg ADDED
examples/example3.jpg ADDED
labels.csv ADDED
The diff for this file is too large to render. See raw diff
 
model.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torchvision
4
+ from torchvision import transforms
5
+
6
+ # Custom transformation to handle palette images
7
+ def convert_to_rgba(image):
8
+ # Check if the image mode is 'P' (palette mode)
9
+ if image.mode == 'P':
10
+ image = image.convert('RGBA')
11
+ return image
12
+
13
+ def create_model(num_classes: int = 120, seed: int = 42):
14
+
15
+
16
+ # 1. Download the default weights
17
+ weights = torchvision.models.ConvNeXt_Tiny_Weights.IMAGENET1K_V1
18
+
19
+ # 2. Setup transforms
20
+ default_transforms = weights.transforms()
21
+
22
+ custom_transforms = transforms.Compose([
23
+ # transforms.RandomHorizontalFlip(p=0.5), # Randomly flip images horizontally
24
+ # transforms.Lambda(convert_to_rgba), # Apply RGBA conversion if necessary
25
+ # transforms.RandomRotation(degrees=10), # Randomly rotate images by up to 10 degrees
26
+ # transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1), # Color jitter
27
+ ])
28
+
29
+ # 3. Combine custom and ViT's default transforms
30
+ combined_transforms = transforms.Compose([
31
+ custom_transforms, # First, apply your custom augmentations
32
+ transforms.Resize((224, 224)), # Resize to ConvNext's input size if needed (ConvNext expects 224x224)
33
+ transforms.ToTensor(), # Convert image to Tensor
34
+ default_transforms, # Apply default normalization (mean, std)
35
+ ])
36
+
37
+ # 4. Create a model and apply the default weights
38
+ model = torchvision.models.convnext_tiny(weights=weights)
39
+
40
+ # 5. Freeze the base layers in the model (this will stop all layers from training)
41
+ for parameters in model.parameters():
42
+ parameters.requires_grad = False
43
+
44
+ # 6. Set seeds for reproducibility
45
+ torch.manual_seed(seed)
46
+
47
+ # 7. Modify the number of output layers (add a dropout layer for regularization)
48
+ model.classifier = nn.Sequential(
49
+ nn.LayerNorm([768, 1, 1], eps=1e-06, elementwise_affine=True), # Apply LayerNorm on the channel dimension (768)
50
+ nn.Flatten(start_dim=1), # Flatten the tensor from dimension 1 onwards (batch size remains intact)
51
+ nn.Linear(in_features=768, out_features=num_classes, bias=True)
52
+ )
53
+
54
+
55
+ return model, combined_transforms
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch==2.4.0
2
+ torchvision==0.19.0
3
+ gradio==4.44.0
4
+ triton==3.1.0