Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
@@ -1,9 +1,43 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
To load and initialize the `Generator` model from the repository, follow these steps:
|
2 |
+
|
3 |
+
1. **Install Required Packages**: Ensure you have the necessary Python packages installed:
|
4 |
+
|
5 |
+
```python
|
6 |
+
pip install torch omegaconf huggingface_hub
|
7 |
+
```
|
8 |
+
|
9 |
+
2. **Download Model Files**: Retrieve the `generator.pth`, `config.json`, and `model.py` files from the Hugging Face repository. You can use the `huggingface_hub` library for this:
|
10 |
+
|
11 |
+
```python
|
12 |
+
from huggingface_hub import hf_hub_download
|
13 |
+
|
14 |
+
repo_id = "Kiwinicki/sat2map-generator"
|
15 |
+
generator_path = hf_hub_download(repo_id=repo_id, filename="generator.pth")
|
16 |
+
config_path = hf_hub_download(repo_id=repo_id, filename="config.json")
|
17 |
+
model_path = hf_hub_download(repo_id=repo_id, filename="model.py")
|
18 |
+
```
|
19 |
+
|
20 |
+
3. **Load the Model**: Incorporate the downloaded `model.py` to define the `Generator` class, then load the model's state dictionary and configuration:
|
21 |
+
|
22 |
+
```python
|
23 |
+
import torch
|
24 |
+
import json
|
25 |
+
from omegaconf import OmegaConf
|
26 |
+
import sys
|
27 |
+
from pathlib import Path
|
28 |
+
from model import Generator
|
29 |
+
|
30 |
+
# Load configuration
|
31 |
+
with open(config_path, "r") as f:
|
32 |
+
config_dict = json.load(f)
|
33 |
+
cfg = OmegaConf.create(config_dict)
|
34 |
+
|
35 |
+
# Initialize and load the generator model
|
36 |
+
generator = Generator(cfg)
|
37 |
+
generator.load_state_dict(torch.load(generator_path))
|
38 |
+
generator.eval()
|
39 |
+
x = torch.randn([1, cfg['channels'], 256, 256])
|
40 |
+
out = generator(x)
|
41 |
+
```
|
42 |
+
|
43 |
+
Here, `generator` is the initialized model ready for inference.
|