dmtschulz commited on
Commit
db76e3d
·
1 Parent(s): 7fd4b8e

set device for model

Browse files
Files changed (3) hide show
  1. app.py +1 -1
  2. app/main.py +1 -1
  3. src/model_config.py +6 -4
app.py CHANGED
@@ -18,7 +18,7 @@ def request_pydantic_schema(_: type, handler: GetCoreSchemaHandler):
18
  Request.__get_pydantic_core_schema__ = request_pydantic_schema
19
 
20
  device = set_device()
21
- model = load_model()
22
  model.eval()
23
 
24
  transform = T.Compose([
 
18
  Request.__get_pydantic_core_schema__ = request_pydantic_schema
19
 
20
  device = set_device()
21
+ model = load_model(device=device)
22
  model.eval()
23
 
24
  transform = T.Compose([
app/main.py CHANGED
@@ -22,7 +22,7 @@ app = FastAPI()
22
 
23
  device = set_device()
24
  log.info("Using device: %s", device)
25
- model = load_model()
26
  log.info("Model loaded. App is ready.")
27
 
28
  model.eval()
 
22
 
23
  device = set_device()
24
  log.info("Using device: %s", device)
25
+ model = load_model(device=device)
26
  log.info("Model loaded. App is ready.")
27
 
28
  model.eval()
src/model_config.py CHANGED
@@ -1,4 +1,6 @@
1
- from train import Autoencoder, DEVICE
 
 
2
  import torch
3
  from huggingface_hub import hf_hub_download
4
  import logging
@@ -12,15 +14,15 @@ def set_device():
12
  return device
13
 
14
  # Load model
15
- def load_model(h: int = 32):
16
  # model_path = "models/autoencoder_mnist.pth" # Use local model
17
  model_path = hf_hub_download(
18
  repo_id="dmtschulz/anomaly-detection-model",
19
  filename="autoencoder_mnist.pth"
20
  )
21
 
22
- model = Autoencoder(h).to(DEVICE)
23
- state_dict = torch.load(model_path, map_location=DEVICE)
24
  model.load_state_dict(state_dict)
25
  model.eval()
26
  log.info("Model loaded from %s", model_path)
 
1
+ # src/model_config.py
2
+
3
+ from train import Autoencoder
4
  import torch
5
  from huggingface_hub import hf_hub_download
6
  import logging
 
14
  return device
15
 
16
  # Load model
17
+ def load_model(device, h: int = 32):
18
  # model_path = "models/autoencoder_mnist.pth" # Use local model
19
  model_path = hf_hub_download(
20
  repo_id="dmtschulz/anomaly-detection-model",
21
  filename="autoencoder_mnist.pth"
22
  )
23
 
24
+ model = Autoencoder(h).to(device)
25
+ state_dict = torch.load(model_path, map_location=device)
26
  model.load_state_dict(state_dict)
27
  model.eval()
28
  log.info("Model loaded from %s", model_path)