Skip to content

Commit 797196d

Browse files
committed
Update conversion
1 parent fbd2d3f commit 797196d

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

scripts/models-conversions/upernet-hf-to-smp.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,15 @@ def group_qkv_layers(state_dict: dict) -> dict:
161161

162162
def convert_model(model_name: str, push_to_hub: bool = False):
163163
params = PRETRAINED_CHECKPOINTS[model_name]
164+
165+
print(f"Converting model: {model_name}")
166+
print(f"Downloading weights from: {params['repo_id']}")
164167

165168
hf_weights_path = hf_hub_download(
166169
repo_id=params["repo_id"], filename="pytorch_model.bin"
167170
)
168171
hf_state_dict = torch.load(hf_weights_path, weights_only=True)
172+
print(f"Loaded HuggingFace state dict with {len(hf_state_dict)} keys")
169173

170174
# Rename keys
171175
keys_mapping = convert_old_keys_to_new_keys(hf_state_dict.keys(), params["mapping"])
@@ -186,6 +190,7 @@ def convert_model(model_name: str, push_to_hub: bool = False):
186190
}
187191

188192
# Create model
193+
print(f"Creating SMP UPerNet model with encoder: {params['encoder_name']}")
189194
extra_kwargs = params.get("extra_kwargs", {})
190195
smp_model = smp.UPerNet(
191196
encoder_name=params["encoder_name"],
@@ -195,18 +200,21 @@ def convert_model(model_name: str, push_to_hub: bool = False):
195200
**extra_kwargs,
196201
)
197202

203+
print("Loading weights into SMP model...")
198204
smp_model.load_state_dict(smp_state_dict, strict=True)
199205

200206
# Check we can run the model
207+
print("Verifying model with test inference...")
201208
smp_model.eval()
202209
sample = torch.ones(1, 3, 512, 512)
203210
with torch.no_grad():
204211
output = smp_model(sample)
205-
212+
print(f"Test inference successful. Output shape: {output.shape}")
206213

207214
# Save model with preprocessing
208215
smp_repo_id = f"smp-hub/upernet-{model_name}"
209-
smp_model.save_pretrained(smp_repo_id)
216+
print(f"Saving model to: {smp_repo_id}")
217+
smp_model.save_pretrained(save_directory=smp_repo_id)
210218

211219
transform = A.Compose(
212220
[
@@ -221,14 +229,21 @@ def convert_model(model_name: str, push_to_hub: bool = False):
221229
transform.save_pretrained(save_directory=smp_repo_id)
222230

223231
if push_to_hub:
232+
print(f"Pushing model to HuggingFace Hub: {smp_repo_id}")
224233
api = HfApi()
234+
if not api.repo_exists(smp_repo_id):
235+
api.create_repo(repo_id=smp_repo_id, repo_type="model")
225236
api.upload_folder(
226237
repo_id=smp_repo_id,
227238
folder_path=smp_repo_id,
228239
repo_type="model",
229240
)
241+
242+
print(f"Conversion of {model_name} completed successfully!")
230243

231244

232245
if __name__ == "__main__":
246+
print(f"Starting conversion of {len(PRETRAINED_CHECKPOINTS)} UPerNet models")
233247
for model_name in PRETRAINED_CHECKPOINTS.keys():
234-
convert_model(model_name, push_to_hub=False)
248+
convert_model(model_name, push_to_hub=True)
249+
print("All conversions completed!")

0 commit comments

Comments
 (0)