Update README.md
Browse files
README.md
CHANGED
@@ -9,190 +9,183 @@ tags:
|
|
9 |
---
|
10 |
# Model Card for Model ID
|
11 |
|
12 |
-
<!-- Provide a quick summary of what the model is/does. -->
|
13 |
-
|
14 |
-
This modelcard aims to be a base template for new models. It has been generated using [this raw template](https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/templates/modelcard_template.md?plain=1).
|
15 |
-
|
16 |
## Model Details
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
<!-- Provide a longer summary of what this model is. -->
|
21 |
-
|
22 |
|
23 |
|
24 |
-
|
25 |
-
- **Funded by [optional]:** [More Information Needed]
|
26 |
-
- **Shared by [optional]:** [More Information Needed]
|
27 |
-
- **Model type:** [More Information Needed]
|
28 |
-
- **Language(s) (NLP):** [More Information Needed]
|
29 |
-
- **License:** [More Information Needed]
|
30 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
31 |
-
|
32 |
-
### Model Sources [optional]
|
33 |
|
34 |
<!-- Provide the basic links for the model. -->
|
35 |
-
|
36 |
-
- **Repository:** [More Information Needed]
|
37 |
-
- **Paper [optional]:** [More Information Needed]
|
38 |
-
- **Demo [optional]:** [More Information Needed]
|
39 |
-
|
40 |
-
## Uses
|
41 |
-
|
42 |
-
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
43 |
-
|
44 |
-
### Direct Use
|
45 |
-
|
46 |
-
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
47 |
-
|
48 |
-
[More Information Needed]
|
49 |
-
|
50 |
-
### Downstream Use [optional]
|
51 |
-
|
52 |
-
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
53 |
-
|
54 |
-
[More Information Needed]
|
55 |
-
|
56 |
-
### Out-of-Scope Use
|
57 |
-
|
58 |
-
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
59 |
-
|
60 |
-
[More Information Needed]
|
61 |
-
|
62 |
-
## Bias, Risks, and Limitations
|
63 |
-
|
64 |
-
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
65 |
-
|
66 |
-
[More Information Needed]
|
67 |
-
|
68 |
-
### Recommendations
|
69 |
-
|
70 |
-
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
71 |
-
|
72 |
-
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
73 |
|
74 |
## How to Get Started with the Model
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
## Training Details
|
81 |
|
82 |
### Training Data
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
[More Information Needed]
|
87 |
|
88 |
### Training Procedure
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
#### Preprocessing [optional]
|
93 |
-
|
94 |
-
[More Information Needed]
|
95 |
-
|
96 |
-
|
97 |
-
#### Training Hyperparameters
|
98 |
-
|
99 |
-
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
100 |
-
|
101 |
-
#### Speeds, Sizes, Times [optional]
|
102 |
-
|
103 |
-
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
104 |
-
|
105 |
-
[More Information Needed]
|
106 |
-
|
107 |
-
## Evaluation
|
108 |
-
|
109 |
-
<!-- This section describes the evaluation protocols and provides the results. -->
|
110 |
-
|
111 |
-
### Testing Data, Factors & Metrics
|
112 |
-
|
113 |
-
#### Testing Data
|
114 |
-
|
115 |
-
<!-- This should link to a Dataset Card if possible. -->
|
116 |
-
|
117 |
-
[More Information Needed]
|
118 |
-
|
119 |
-
#### Factors
|
120 |
-
|
121 |
-
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
122 |
-
|
123 |
-
[More Information Needed]
|
124 |
-
|
125 |
-
#### Metrics
|
126 |
-
|
127 |
-
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
128 |
-
|
129 |
-
[More Information Needed]
|
130 |
-
|
131 |
-
### Results
|
132 |
-
|
133 |
-
[More Information Needed]
|
134 |
-
|
135 |
-
#### Summary
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
## Model Examination [optional]
|
140 |
-
|
141 |
-
<!-- Relevant interpretability work for the model goes here -->
|
142 |
-
|
143 |
-
[More Information Needed]
|
144 |
-
|
145 |
-
## Environmental Impact
|
146 |
-
|
147 |
-
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
148 |
-
|
149 |
-
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
150 |
-
|
151 |
-
- **Hardware Type:** [More Information Needed]
|
152 |
-
- **Hours used:** [More Information Needed]
|
153 |
-
- **Cloud Provider:** [More Information Needed]
|
154 |
-
- **Compute Region:** [More Information Needed]
|
155 |
-
- **Carbon Emitted:** [More Information Needed]
|
156 |
-
|
157 |
-
## Technical Specifications [optional]
|
158 |
-
|
159 |
-
### Model Architecture and Objective
|
160 |
-
|
161 |
-
[More Information Needed]
|
162 |
-
|
163 |
-
### Compute Infrastructure
|
164 |
-
|
165 |
-
[More Information Needed]
|
166 |
-
|
167 |
-
#### Hardware
|
168 |
-
|
169 |
-
[More Information Needed]
|
170 |
-
|
171 |
-
#### Software
|
172 |
-
|
173 |
-
[More Information Needed]
|
174 |
-
|
175 |
-
## Citation [optional]
|
176 |
-
|
177 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
178 |
-
|
179 |
-
**BibTeX:**
|
180 |
-
|
181 |
-
[More Information Needed]
|
182 |
-
|
183 |
-
**APA:**
|
184 |
-
|
185 |
-
[More Information Needed]
|
186 |
-
|
187 |
-
## Glossary [optional]
|
188 |
-
|
189 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
190 |
-
|
191 |
-
[More Information Needed]
|
192 |
-
|
193 |
-
## More Information [optional]
|
194 |
-
|
195 |
-
[More Information Needed]
|
196 |
|
197 |
## Model Card Authors [optional]
|
198 |
|
|
|
9 |
---
|
10 |
# Model Card for Model ID
|
11 |
|
|
|
|
|
|
|
|
|
12 |
## Model Details
|
13 |
|
14 |
+
The model is a VAE trained for encoding classical portraits in 128*128 resolution into a d=256 latent vector and decoding into original images.
|
|
|
|
|
|
|
15 |
|
16 |
|
17 |
+
## Model Sources [optional]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
<!-- Provide the basic links for the model. -->
|
20 |
+
- **Demo [optional]:** [https://huggingface.co/spaces/BioMike/ClassicalPortraitsVAE]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
## How to Get Started with the Model
|
23 |
|
24 |
+
```python
|
25 |
+
import json
|
26 |
+
import torch
|
27 |
+
import torch.nn as nn
|
28 |
+
import os
|
29 |
+
from pathlib import Path
|
30 |
+
from typing import Optional, Union, Dict
|
31 |
+
from huggingface_hub import snapshot_download
|
32 |
+
import warnings
|
33 |
+
|
34 |
+
class ConvVAE(nn.Module):
|
35 |
+
def __init__(self, latent_size):
|
36 |
+
super(ConvVAE, self).__init__()
|
37 |
+
|
38 |
+
# Encoder
|
39 |
+
self.encoder = nn.Sequential(
|
40 |
+
nn.Conv2d(3, 64, 3, stride=2, padding=1), # (batch, 64, 64, 64)
|
41 |
+
nn.BatchNorm2d(64),
|
42 |
+
nn.ReLU(),
|
43 |
+
nn.Conv2d(64, 128, 3, stride=2, padding=1), # (batch, 128, 32, 32)
|
44 |
+
nn.BatchNorm2d(128),
|
45 |
+
nn.ReLU(),
|
46 |
+
nn.Conv2d(128, 256, 3, stride=2, padding=1), # (batch, 256, 16, 16)
|
47 |
+
nn.BatchNorm2d(256),
|
48 |
+
nn.ReLU(),
|
49 |
+
nn.Conv2d(256, 512, 3, stride=2, padding=1), # (batch, 512, 8, 8)
|
50 |
+
nn.BatchNorm2d(512),
|
51 |
+
nn.ReLU()
|
52 |
+
)
|
53 |
+
|
54 |
+
self.fc_mu = nn.Linear(512 * 8 * 8, latent_size)
|
55 |
+
self.fc_logvar = nn.Linear(512 * 8 * 8, latent_size)
|
56 |
+
|
57 |
+
self.fc2 = nn.Linear(latent_size, 512 * 8 * 8)
|
58 |
+
|
59 |
+
self.decoder = nn.Sequential(
|
60 |
+
nn.ConvTranspose2d(512, 256, 4, stride=2, padding=1), # (batch, 256, 16, 16)
|
61 |
+
nn.BatchNorm2d(256),
|
62 |
+
nn.ReLU(),
|
63 |
+
nn.ConvTranspose2d(256, 128, 4, stride=2, padding=1), # (batch, 128, 32, 32)
|
64 |
+
nn.BatchNorm2d(128),
|
65 |
+
nn.ReLU(),
|
66 |
+
nn.ConvTranspose2d(128, 64, 4, stride=2, padding=1), # (batch, 64, 64, 64)
|
67 |
+
nn.BatchNorm2d(64),
|
68 |
+
nn.ReLU(),
|
69 |
+
nn.ConvTranspose2d(64, 3, 4, stride=2, padding=1), # (batch, 3, 128, 128)
|
70 |
+
nn.Tanh()
|
71 |
+
)
|
72 |
+
|
73 |
+
def forward(self, x):
|
74 |
+
mu, logvar = self.encode(x)
|
75 |
+
z = self.reparameterize(mu, logvar)
|
76 |
+
decoded = self.decode(z)
|
77 |
+
return decoded, mu, logvar
|
78 |
+
|
79 |
+
def encode(self, x):
|
80 |
+
x = self.encoder(x)
|
81 |
+
x = x.view(x.size(0), -1)
|
82 |
+
mu = self.fc_mu(x)
|
83 |
+
logvar = self.fc_logvar(x)
|
84 |
+
return mu, logvar
|
85 |
+
|
86 |
+
def reparameterize(self, mu, logvar):
|
87 |
+
std = torch.exp(0.5 * logvar)
|
88 |
+
eps = torch.randn_like(std)
|
89 |
+
return mu + eps * std
|
90 |
+
|
91 |
+
def decode(self, z):
|
92 |
+
x = self.fc2(z)
|
93 |
+
x = x.view(-1, 512, 8, 8)
|
94 |
+
decoded = self.decoder(x)
|
95 |
+
return decoded
|
96 |
+
|
97 |
+
@classmethod
|
98 |
+
def from_pretrained(
|
99 |
+
cls,
|
100 |
+
model_id: str,
|
101 |
+
revision: Optional[str] = None,
|
102 |
+
cache_dir: Optional[Union[str, Path]] = None,
|
103 |
+
force_download: bool = False,
|
104 |
+
proxies: Optional[Dict] = None,
|
105 |
+
resume_download: bool = False,
|
106 |
+
local_files_only: bool = False,
|
107 |
+
token: Union[str, bool, None] = None,
|
108 |
+
map_location: str = "cpu",
|
109 |
+
strict: bool = False,
|
110 |
+
**model_kwargs,
|
111 |
+
):
|
112 |
+
"""
|
113 |
+
Load a pretrained model from a given model ID.
|
114 |
+
Args:
|
115 |
+
model_id (str): Identifier of the model to load.
|
116 |
+
revision (Optional[str]): Specific model revision to use.
|
117 |
+
cache_dir (Optional[Union[str, Path]]): Directory to store downloaded models.
|
118 |
+
force_download (bool): Force re-download even if the model exists.
|
119 |
+
proxies (Optional[Dict]): Proxy configuration for downloads.
|
120 |
+
resume_download (bool): Resume interrupted downloads.
|
121 |
+
local_files_only (bool): Use only local files, don't download.
|
122 |
+
token (Union[str, bool, None]): Token for API authentication.
|
123 |
+
map_location (str): Device to map model to. Defaults to "cpu".
|
124 |
+
strict (bool): Enforce strict state_dict loading.
|
125 |
+
**model_kwargs: Additional keyword arguments for model initialization.
|
126 |
+
Returns:
|
127 |
+
An instance of the model loaded from the pretrained weights.
|
128 |
+
"""
|
129 |
+
model_dir = Path(model_id)
|
130 |
+
if not model_dir.exists():
|
131 |
+
model_dir = Path(
|
132 |
+
snapshot_download(
|
133 |
+
repo_id=model_id,
|
134 |
+
revision=revision,
|
135 |
+
cache_dir=cache_dir,
|
136 |
+
force_download=force_download,
|
137 |
+
proxies=proxies,
|
138 |
+
resume_download=resume_download,
|
139 |
+
token=token,
|
140 |
+
local_files_only=local_files_only,
|
141 |
+
)
|
142 |
+
)
|
143 |
+
|
144 |
+
config_file = model_dir / "config.json"
|
145 |
+
with open(config_file, 'r') as f:
|
146 |
+
config = json.load(f)
|
147 |
+
|
148 |
+
latent_size = config.get('latent_size')
|
149 |
+
if latent_size is None:
|
150 |
+
raise ValueError("The configuration file is missing the 'latent_size' key.")
|
151 |
+
|
152 |
+
model = cls(latent_size, **model_kwargs)
|
153 |
+
|
154 |
+
model_file = model_dir / "model_conv_vae_256_epoch_304.pth"
|
155 |
+
if not model_file.exists():
|
156 |
+
raise FileNotFoundError(f"The model checkpoint '{model_file}' does not exist.")
|
157 |
+
|
158 |
+
state_dict = torch.load(model_file, map_location=map_location)
|
159 |
+
|
160 |
+
new_state_dict = {}
|
161 |
+
for k, v in state_dict.items():
|
162 |
+
if k.startswith('_orig_mod.'):
|
163 |
+
new_state_dict[k[len('_orig_mod.'):]] = v
|
164 |
+
else:
|
165 |
+
new_state_dict[k] = v
|
166 |
+
|
167 |
+
model.load_state_dict(new_state_dict, strict=strict)
|
168 |
+
model.to(map_location)
|
169 |
+
|
170 |
+
return model
|
171 |
+
|
172 |
+
|
173 |
+
model = ConvVAE.from_pretrained(
|
174 |
+
model_id="BioMike/classical_portrait_vae",
|
175 |
+
cache_dir="./model_cache",
|
176 |
+
map_location="cpu",
|
177 |
+
strict=True).eval()
|
178 |
+
```
|
179 |
|
180 |
## Training Details
|
181 |
|
182 |
### Training Data
|
183 |
|
184 |
+
The model was trained on the [Portrait Dataset](https://www.kaggle.com/datasets/karnikakapoor/art-portraits)
|
|
|
|
|
185 |
|
186 |
### Training Procedure
|
187 |
|
188 |
+
The model was trained into two steps, in the first the model vgg16 was employed in the perceptual loss, to train our model to extract general features, and the model vggface2 was used to train VAE to decode faces accurately.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
|
190 |
## Model Card Authors [optional]
|
191 |
|