Roll20 commited on
Commit
61c824d
β€’
1 Parent(s): 88b06a5

change app.py path structure

Browse files
app.py CHANGED
@@ -18,8 +18,8 @@ import pandas as pd
18
  import numpy as np
19
  from PIL import Image
20
  from glob import glob
21
- import gc # garbage collector, recycles memory usage
22
- import albumentations as A # library for image augmentation
23
  import gradio as gr
24
  #}}}
25
  # Gradio wrap {{{
@@ -36,11 +36,9 @@ def score(input_img):
36
  # 02. Model constants {{{
37
  device = torch.device('cuda')
38
  class Config:
39
- model_base_dir = '/weights/'
40
- model_file_ext = '/*.pth'
41
  im_size = 224
42
- batch_size = 1 # match total test images needed for inference to at least 1 batch_size
43
- num_workers = 0
44
  # }}}
45
  # 03. Define Dataset {{{
46
  class PetDataset(Dataset):
@@ -133,79 +131,47 @@ def score(input_img):
133
  torch.cuda.empty_cache()
134
  return fold_preds
135
  # }}}
136
- # 05. Inference exp53 {{{
137
- class Config_exp53(Config):
138
- model_dir = 'exp53'
139
- model_name = "swin_large_patch4_window7_224"
140
-
141
  test_preds = []
142
  test_preds_model = []
143
- modelfiles = glob(Config.model_base_dir + Config_exp53.model_dir + Config.model_file_ext) # get all model full file paths
144
-
145
- for model_index, model_path in enumerate(modelfiles): # loop through all indexes (total 10) and model full file paths
146
- print(f'inferencing with: {model_path}')
147
- test_preds_fold = []
148
- model = PetNet(model_name = Config_exp53.model_name, out_features = 1, inp_channels = 3, pretrained=False) # instantiate model
149
- model.load_state_dict(torch.load(model_path)) # load coorponding weights into the model
150
- model = model.to(device) # send to gpu
151
- model = model.float() # convert to float
152
- model.eval() # set to eval mode. (turn off BatchNorm, dropout etc.)
153
- test_preds_fold = tta_fn(thefile, model, Config.im_size, [1]) # returns a list of predictions
154
- test_preds_model.append(test_preds_fold) # append test.size # of predictions for each model
155
 
156
  final_predictions53 = np.mean(np.array(test_preds_model), axis=0)
157
- print(f'>>>exp53: ', final_predictions53)
158
  # }}}
159
- # 06. Inference exp55 {{{
160
- class Config_exp55(Config):
161
- model_dir = 'exp55'
162
- model_name = "beit_large_patch16_224"
163
-
164
  test_preds = []
165
  test_preds_model = []
166
- modelfiles = glob(Config.model_base_dir + Config_exp55.model_dir + Config.model_file_ext)
167
-
168
- for model_index, model_path in enumerate(modelfiles):
169
- print(f'inferencing with: {model_path}')
170
- test_preds_fold = []
171
- model = PetNet(model_name = Config_exp55.model_name, out_features = 1, inp_channels = 3, pretrained=False)
172
- model.load_state_dict(torch.load(model_path))
173
- model = model.to(device)
174
- model = model.float()
175
- model.eval()
176
- test_preds_fold = tta_fn(thefile, model, Config.im_size, [0])
177
- test_preds_model.append(test_preds_fold)
178
  final_predictions55 = np.mean(np.array(test_preds_model), axis=0)
179
- print(f'>>>exp55: ', final_predictions55)
180
  # }}}
181
- # 07. Inference exp66 {{{
182
- class Config_exp66(Config):
183
- model_dir = 'exp66'
184
- model_name = "swin_large_patch4_window12_384_in22k"
185
- im_size = 384
186
-
187
  test_preds = []
188
  test_preds_model = []
189
- modelfiles = glob(Config.model_base_dir + Config_exp66.model_dir + Config.model_file_ext)
190
-
191
- for model_index, model_path in enumerate(modelfiles):
192
- print(f'inferencing with: {model_path}')
193
- test_preds_fold = []
194
- model = PetNet(model_name = Config_exp66.model_name, out_features = 1, inp_channels = 3, pretrained=False)
195
- model.load_state_dict(torch.load(model_path))
196
- model = model.to(device)
197
- model = model.float()
198
- model.eval()
199
- test_preds_fold = tta_fn(thefile, model, Config_exp66.im_size, [0])
200
- test_preds_model.append(test_preds_fold)
201
  final_predictions66 = np.mean(np.array(test_preds_model), axis=0)
202
- print(f'>>>exp66: ', final_predictions66)
203
  #}}}
204
- # 08. Inference exp77 {{{
205
- class Config_exp77(Config):
206
- model_dir = 'exp77'
207
- model_name = "beit_large_patch16_224"
208
-
209
  class PetNet_exp77(nn.Module):
210
  def __init__(self, model_name, out_features = 1, inp_channels = 3, pretrained = False):
211
  super().__init__()
@@ -223,19 +189,15 @@ def score(input_img):
223
  test_preds = []
224
  test_preds_model = []
225
  modelfiles = glob(Config.model_base_dir + Config_exp77.model_dir + Config.model_file_ext)
226
-
227
- for model_index, model_path in enumerate(modelfiles):
228
- print(f'inferencing with: {model_path}')
229
- test_preds_fold = []
230
- model = PetNet_exp77(model_name = Config_exp77.model_name, out_features = 1, inp_channels = 3, pretrained=False)
231
- model.load_state_dict(torch.load(model_path))
232
- model = model.to(device)
233
- model = model.float()
234
- model.eval()
235
- test_preds_fold = tta_fn(thefile, model, Config.im_size, [0])
236
- test_preds_model.append(test_preds_fold)
237
  final_predictions77 = np.mean(np.array(test_preds_model), axis=0)
238
- print(f'>>>exp77: ', final_predictions77)
239
  #}}}
240
  # 09. Final predicted scores {{{
241
  final_predictions = (3*final_predictions53 +
 
18
  import numpy as np
19
  from PIL import Image
20
  from glob import glob
21
+ import gc
22
+ import albumentations as A
23
  import gradio as gr
24
  #}}}
25
  # Gradio wrap {{{
 
36
  # 02. Model constants {{{
37
  device = torch.device('cuda')
38
  class Config:
 
 
39
  im_size = 224
40
+ batch_size = 1
41
+ num_workers = 8
42
  # }}}
43
  # 03. Define Dataset {{{
44
  class PetDataset(Dataset):
 
131
  torch.cuda.empty_cache()
132
  return fold_preds
133
  # }}}
134
+ # 05. Inference 1 {{{
 
 
 
 
135
  test_preds = []
136
  test_preds_model = []
137
+ test_preds_fold = []
138
+ model = PetNet(model_name = 'swin_large_patch4_window7_224', out_features = 1, inp_channels = 3, pretrained=False)
139
+ model.load_state_dict(torch.load('swin_large_patch4_window7_224_fold0_half.pth'))
140
+ model = model.to(device)
141
+ model = model.float()
142
+ model.eval()
143
+ test_preds_fold = tta_fn(thefile, model, Config.im_size, [1])
144
+ test_preds_model.append(test_preds_fold)
 
 
 
 
145
 
146
  final_predictions53 = np.mean(np.array(test_preds_model), axis=0)
 
147
  # }}}
148
+ # 06. Inference 2 {{{
 
 
 
 
149
  test_preds = []
150
  test_preds_model = []
151
+ test_preds_fold = []
152
+ model = PetNet(model_name = 'beit_large_patch16_224', out_features = 1, inp_channels = 3, pretrained=False)
153
+ model.load_state_dict(torch.load('beit_large_patch16_224_fold0_half.pth'))
154
+ model = model.to(device)
155
+ model = model.float()
156
+ model.eval()
157
+ test_preds_fold = tta_fn(thefile, model, Config.im_size, [0])
158
+ test_preds_model.append(test_preds_fold)
 
 
 
 
159
  final_predictions55 = np.mean(np.array(test_preds_model), axis=0)
 
160
  # }}}
161
+ # 07. Inference 3 {{{
 
 
 
 
 
162
  test_preds = []
163
  test_preds_model = []
164
+ test_preds_fold = []
165
+ model = PetNet(model_name = 'swin_large_patch4_window12_384_in22k', out_features = 1, inp_channels = 3, pretrained=False)
166
+ model.load_state_dict(torch.load('swin_large_patch4_window12_384_in22k_fold0_half.pth'))
167
+ model = model.to(device)
168
+ model = model.float()
169
+ model.eval()
170
+ test_preds_fold = tta_fn(thefile, model, 384, [0])
171
+ test_preds_model.append(test_preds_fold)
 
 
 
 
172
  final_predictions66 = np.mean(np.array(test_preds_model), axis=0)
 
173
  #}}}
174
+ # 08. Inference 4 {{{
 
 
 
 
175
  class PetNet_exp77(nn.Module):
176
  def __init__(self, model_name, out_features = 1, inp_channels = 3, pretrained = False):
177
  super().__init__()
 
189
  test_preds = []
190
  test_preds_model = []
191
  modelfiles = glob(Config.model_base_dir + Config_exp77.model_dir + Config.model_file_ext)
192
+ test_preds_fold = []
193
+ model = PetNet_exp77(model_name = 'beit_large_patch16_224', out_features = 1, inp_channels = 3, pretrained=False)
194
+ model.load_state_dict(torch.load('beit_large_patch16_224_fold1_half.pth'))
195
+ model = model.to(device)
196
+ model = model.float()
197
+ model.eval()
198
+ test_preds_fold = tta_fn(thefile, model, Config.im_size, [0])
199
+ test_preds_model.append(test_preds_fold)
 
 
 
200
  final_predictions77 = np.mean(np.array(test_preds_model), axis=0)
 
201
  #}}}
202
  # 09. Final predicted scores {{{
203
  final_predictions = (3*final_predictions53 +
weights/exp55/beit_large_patch16_224_fold0_half.pth β†’ beit_large_patch16_224_fold0_half.pth RENAMED
File without changes
weights/exp77/beit_large_patch16_224_fold0_half.pth β†’ beit_large_patch16_224_fold1_half.pth RENAMED
File without changes
weights/exp66/swin_large_patch4_window12_384_in22k_fold0_half.pth β†’ swin_large_patch4_window12_384_in22k_fold0_half.pth RENAMED
File without changes
weights/exp53/swin_large_patch4_window7_224_fold0_half.pth β†’ swin_large_patch4_window7_224_fold0_half.pth RENAMED
File without changes