@@ -161,11 +161,15 @@ def group_qkv_layers(state_dict: dict) -> dict:
161
161
162
162
def convert_model (model_name : str , push_to_hub : bool = False ):
163
163
params = PRETRAINED_CHECKPOINTS [model_name ]
164
+
165
+ print (f"Converting model: { model_name } " )
166
+ print (f"Downloading weights from: { params ['repo_id' ]} " )
164
167
165
168
hf_weights_path = hf_hub_download (
166
169
repo_id = params ["repo_id" ], filename = "pytorch_model.bin"
167
170
)
168
171
hf_state_dict = torch .load (hf_weights_path , weights_only = True )
172
+ print (f"Loaded HuggingFace state dict with { len (hf_state_dict )} keys" )
169
173
170
174
# Rename keys
171
175
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):
186
190
}
187
191
188
192
# Create model
193
+ print (f"Creating SMP UPerNet model with encoder: { params ['encoder_name' ]} " )
189
194
extra_kwargs = params .get ("extra_kwargs" , {})
190
195
smp_model = smp .UPerNet (
191
196
encoder_name = params ["encoder_name" ],
@@ -195,18 +200,21 @@ def convert_model(model_name: str, push_to_hub: bool = False):
195
200
** extra_kwargs ,
196
201
)
197
202
203
+ print ("Loading weights into SMP model..." )
198
204
smp_model .load_state_dict (smp_state_dict , strict = True )
199
205
200
206
# Check we can run the model
207
+ print ("Verifying model with test inference..." )
201
208
smp_model .eval ()
202
209
sample = torch .ones (1 , 3 , 512 , 512 )
203
210
with torch .no_grad ():
204
211
output = smp_model (sample )
205
-
212
+ print ( f"Test inference successful. Output shape: { output . shape } " )
206
213
207
214
# Save model with preprocessing
208
215
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 )
210
218
211
219
transform = A .Compose (
212
220
[
@@ -221,14 +229,21 @@ def convert_model(model_name: str, push_to_hub: bool = False):
221
229
transform .save_pretrained (save_directory = smp_repo_id )
222
230
223
231
if push_to_hub :
232
+ print (f"Pushing model to HuggingFace Hub: { smp_repo_id } " )
224
233
api = HfApi ()
234
+ if not api .repo_exists (smp_repo_id ):
235
+ api .create_repo (repo_id = smp_repo_id , repo_type = "model" )
225
236
api .upload_folder (
226
237
repo_id = smp_repo_id ,
227
238
folder_path = smp_repo_id ,
228
239
repo_type = "model" ,
229
240
)
241
+
242
+ print (f"Conversion of { model_name } completed successfully!" )
230
243
231
244
232
245
if __name__ == "__main__" :
246
+ print (f"Starting conversion of { len (PRETRAINED_CHECKPOINTS )} UPerNet models" )
233
247
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