CIS4190/5190 Staff commited on
Commit
bb2de70
·
1 Parent(s): 3a5ead0

fix: app.py to accept model.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
app.py CHANGED
@@ -6,7 +6,6 @@ import pandas as pd
6
  import importlib.util
7
  import spaces
8
 
9
-
10
  # 1. Load leaderboard data
11
  dataset = load_dataset("gydou/5190_Spring_Final_Hidden_Data_Released")["test"]
12
  images = dataset["image"]
@@ -20,17 +19,25 @@ def get_student_transform(preprocess_file):
20
  spec.loader.exec_module(preprocess_mod)
21
  return preprocess_mod.get_transform()
22
 
23
- # 3. Model testing/evaluation function
 
 
 
 
 
 
 
 
24
  @spaces.GPU
25
- def test_model(model_file, preprocess_file):
26
  device = "cuda" if torch.cuda.is_available() else "cpu"
27
 
28
- # Load full student model (assumes torch.save(model, ...))
29
- model = torch.load(model_file.name, map_location=device)
 
 
30
  model.eval()
31
- model.to(device)
32
 
33
- # Dynamically get the transform
34
  transform = get_student_transform(preprocess_file)
35
 
36
  preds = []
@@ -48,8 +55,8 @@ def test_model(model_file, preprocess_file):
48
 
49
  leaderboard = pd.DataFrame(columns=["Name", "Score"])
50
 
51
- def submit(model_file, preprocess_file, name):
52
- score = test_model(model_file, preprocess_file)
53
  global leaderboard
54
  leaderboard.loc[len(leaderboard)] = [name, score]
55
  leaderboard_sorted = leaderboard.sort_values("Score")
@@ -60,20 +67,21 @@ with gr.Blocks() as demo:
60
  gr.Markdown(
61
  """
62
  # IMG2GPS Leaderboard
63
- Upload your trained PyTorch model (`student_model.pt`)
64
- and the preprocessing module (`preprocess.py`) with a `get_transform()` function.
65
  """
66
  )
67
  with gr.Row():
68
  name = gr.Text(label="Name/Alias")
69
- model_file = gr.File(label="PyTorch Model (.pt)")
70
- preprocess_file = gr.File(label="Preprocessing (.py)")
 
71
  outputs = [
72
  gr.Number(label="Mean Coordinate Error"),
73
  gr.Dataframe(headers=["Name", "Score"], label="Leaderboard")
74
  ]
75
  submit_btn = gr.Button("Submit")
76
- submit_btn.click(fn=submit, inputs=[model_file, preprocess_file, name], outputs=outputs)
77
 
78
  if __name__ == "__main__":
79
  demo.launch()
 
6
  import importlib.util
7
  import spaces
8
 
 
9
  # 1. Load leaderboard data
10
  dataset = load_dataset("gydou/5190_Spring_Final_Hidden_Data_Released")["test"]
11
  images = dataset["image"]
 
19
  spec.loader.exec_module(preprocess_mod)
20
  return preprocess_mod.get_transform()
21
 
22
+ # 3. Function to dynamically import student's model class
23
+ def get_student_model_class(model_class_file):
24
+ spec = importlib.util.spec_from_file_location("student_model", model_class_file.name)
25
+ model_mod = importlib.util.module_from_spec(spec)
26
+ spec.loader.exec_module(model_mod)
27
+ # Expect the class to be named "StudentModel"
28
+ return model_mod.StudentModel
29
+
30
+ # 4. Model testing/evaluation function
31
  @spaces.GPU
32
+ def test_model(state_dict_file, model_class_file, preprocess_file):
33
  device = "cuda" if torch.cuda.is_available() else "cpu"
34
 
35
+ # Dynamically import model class and preprocessing
36
+ StudentModel = get_student_model_class(model_class_file)
37
+ model = StudentModel().to(device)
38
+ model.load_state_dict(torch.load(state_dict_file.name, map_location=device))
39
  model.eval()
 
40
 
 
41
  transform = get_student_transform(preprocess_file)
42
 
43
  preds = []
 
55
 
56
  leaderboard = pd.DataFrame(columns=["Name", "Score"])
57
 
58
+ def submit(state_dict_file, model_class_file, preprocess_file, name):
59
+ score = test_model(state_dict_file, model_class_file, preprocess_file)
60
  global leaderboard
61
  leaderboard.loc[len(leaderboard)] = [name, score]
62
  leaderboard_sorted = leaderboard.sort_values("Score")
 
67
  gr.Markdown(
68
  """
69
  # IMG2GPS Leaderboard
70
+ Upload your model weights (`model_state_dict.pt`), the model class definition (`model.py`)
71
+ (with class named StudentModel), and preprocessing (`preprocess.py` with get_transform).
72
  """
73
  )
74
  with gr.Row():
75
  name = gr.Text(label="Name/Alias")
76
+ state_dict_file = gr.File(label="PyTorch Model State Dict (.pt)")
77
+ model_class_file = gr.File(label="Model Class (`model.py`)")
78
+ preprocess_file = gr.File(label="Preprocessing (`preprocess.py`)")
79
  outputs = [
80
  gr.Number(label="Mean Coordinate Error"),
81
  gr.Dataframe(headers=["Name", "Score"], label="Leaderboard")
82
  ]
83
  submit_btn = gr.Button("Submit")
84
+ submit_btn.click(fn=submit, inputs=[state_dict_file, model_class_file, preprocess_file, name], outputs=outputs)
85
 
86
  if __name__ == "__main__":
87
  demo.launch()