RyanTietjen commited on
Commit
9393e64
·
verified ·
1 Parent(s): 6ffc1e4

Upload 4 files

Browse files
class_names.txt ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ apple_pie
2
+ baby_back_ribs
3
+ baklava
4
+ beef_carpaccio
5
+ beef_tartare
6
+ beet_salad
7
+ beignets
8
+ bibimbap
9
+ bread_pudding
10
+ breakfast_burrito
11
+ bruschetta
12
+ caesar_salad
13
+ cannoli
14
+ caprese_salad
15
+ carrot_cake
16
+ ceviche
17
+ cheese_plate
18
+ cheesecake
19
+ chicken_curry
20
+ chicken_quesadilla
21
+ chicken_wings
22
+ chocolate_cake
23
+ chocolate_mousse
24
+ churros
25
+ clam_chowder
26
+ club_sandwich
27
+ crab_cakes
28
+ creme_brulee
29
+ croque_madame
30
+ cup_cakes
31
+ deviled_eggs
32
+ donuts
33
+ dumplings
34
+ edamame
35
+ eggs_benedict
36
+ escargots
37
+ falafel
38
+ filet_mignon
39
+ fish_and_chips
40
+ foie_gras
41
+ french_fries
42
+ french_onion_soup
43
+ french_toast
44
+ fried_calamari
45
+ fried_rice
46
+ frozen_yogurt
47
+ garlic_bread
48
+ gnocchi
49
+ greek_salad
50
+ grilled_cheese_sandwich
51
+ grilled_salmon
52
+ guacamole
53
+ gyoza
54
+ hamburger
55
+ hot_and_sour_soup
56
+ hot_dog
57
+ huevos_rancheros
58
+ hummus
59
+ ice_cream
60
+ lasagna
61
+ lobster_bisque
62
+ lobster_roll_sandwich
63
+ macaroni_and_cheese
64
+ macarons
65
+ miso_soup
66
+ mussels
67
+ nachos
68
+ omelette
69
+ onion_rings
70
+ oysters
71
+ pad_thai
72
+ paella
73
+ pancakes
74
+ panna_cotta
75
+ peking_duck
76
+ pho
77
+ pizza
78
+ pork_chop
79
+ poutine
80
+ prime_rib
81
+ pulled_pork_sandwich
82
+ ramen
83
+ ravioli
84
+ red_velvet_cake
85
+ risotto
86
+ samosa
87
+ sashimi
88
+ scallops
89
+ seaweed_salad
90
+ shrimp_and_grits
91
+ spaghetti_bolognese
92
+ spaghetti_carbonara
93
+ spring_rolls
94
+ steak
95
+ strawberry_shortcake
96
+ sushi
97
+ tacos
98
+ takoyaki
99
+ tiramisu
100
+ tuna_tartare
101
+ waffles
model.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Ryan Tietjen
3
+ Aug 2024
4
+ Creates a vit base 16 model for the demo
5
+ """
6
+
7
+ import torch
8
+ import torchvision
9
+ from torch import nn
10
+
11
+ def vit_b_16(num_classes:int=101,
12
+ seed:int=31,
13
+ freeze_gradients:bool=True,
14
+ unfreeze_blocks=0):
15
+ """
16
+ Initializes and configures a Vision Transformer (ViT-B/16) model with options for freezing gradients
17
+ and adjusting the number of trainable blocks.
18
+
19
+ This function sets up a ViT-B/16 model pre-trained on the ImageNet-1K dataset, modifies the classification
20
+ head to accommodate a specified number of classes, and optionally freezes the gradients of certain blocks
21
+ to prevent them from being updated during training.
22
+
23
+ Parameters:
24
+ num_classes (int): The number of output classes for the new classification head. Default is 101.
25
+ seed (int): Random seed for reproducibility. Default is 31.
26
+ freeze_gradients (bool): If True, freezes the gradients of the model's parameters, except for the last few
27
+ blocks specified by `unfreeze_blocks`. Default is True.
28
+ unfreeze_blocks (int): Number of transformer blocks from the end whose parameters will have trainable gradients.
29
+ Default is 0, implying all are frozen except the new classification head.
30
+
31
+ Returns:
32
+ tuple: A tuple containing:
33
+ - model (torch.nn.Module): The modified ViT-B/16 model with a new classifier head.
34
+ - transforms (callable): The transformation function required for input images, as recommended by the
35
+ pre-trained weights.
36
+
37
+ Example:
38
+ ```python
39
+ model, transform = vit_b_16(num_classes=101, seed=31, freeze_gradients=True, unfreeze_blocks=2)
40
+ ```
41
+
42
+ Notes:
43
+ - The total number of parameters in the model is calculated and used to determine which parameters to freeze.
44
+ - The classifier head of the model is replaced with a new linear layer that outputs to the specified number of classes.
45
+ """
46
+
47
+ torch.manual_seed(seed)
48
+
49
+ #Create model and extract weights/transforms
50
+ weights = torchvision.models.ViT_B_16_Weights.IMAGENET1K_SWAG_E2E_V1
51
+ transforms = weights.transforms()
52
+ model = torchvision.models.vit_b_16(weights=weights)
53
+
54
+ params = list(model.parameters())
55
+ params_to_unfreeze = 4 + (12 * unfreeze_blocks)
56
+ # Total number of parameters
57
+ total_params = len(params)
58
+
59
+
60
+ #Freeze gradients to avoid modifying the original model
61
+ if freeze_gradients:
62
+ for i, param in enumerate(params):
63
+ # Set requires_grad to False for all but the last n encoder blocks
64
+ if i < total_params - params_to_unfreeze:
65
+ param.requires_grad = False
66
+
67
+ #modify classifier model to fit our
68
+ model.heads = nn.Sequential(
69
+ nn.Linear(in_features=768,
70
+ out_features=num_classes))
71
+
72
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch==1.12.0
2
+ torchvision==0.13.0
3
+ gradio==4.40.0
4
+ numpy==1.26.4
vit_b_16_unfreeze_one_encoder_block_10_total_epochs.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a91f23cd7a2b19958a8fb041786f083180d0dbac3242867ae900f4e0db185d45
3
+ size 344740050