|
| 1 | +import cv2 |
| 2 | +import torch |
| 3 | +import albumentations as A |
| 4 | +import segmentation_models_pytorch as smp |
| 5 | + |
| 6 | +MODEL_WEIGHTS_PATH = r"dpt_large-ade20k-b12dca68.pt" |
| 7 | +HF_HUB_PATH = "qubvel-hf/dpt-large-ade20k" |
| 8 | +PUSH_TO_HUB = False |
| 9 | + |
| 10 | + |
| 11 | +def get_transform(): |
| 12 | + return A.Compose( |
| 13 | + [ |
| 14 | + A.LongestMaxSize(max_size=480, interpolation=cv2.INTER_CUBIC), |
| 15 | + A.Normalize( |
| 16 | + mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5), max_pixel_value=255.0 |
| 17 | + ), |
| 18 | + # This is not correct transform, ideally image should resized without padding to multiple of 32, |
| 19 | + # but we take there is no such transform in albumentations, here is closest one |
| 20 | + A.PadIfNeeded( |
| 21 | + min_height=None, |
| 22 | + min_width=None, |
| 23 | + pad_height_divisor=32, |
| 24 | + pad_width_divisor=32, |
| 25 | + border_mode=cv2.BORDER_CONSTANT, |
| 26 | + value=0, |
| 27 | + p=1, |
| 28 | + ), |
| 29 | + ] |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +if __name__ == "__main__": |
| 34 | + # fmt: off |
| 35 | + smp_model = smp.DPT(encoder_name="tu-vit_large_patch16_384", classes=150, dynamic_img_size=True) |
| 36 | + dpt_model_dict = torch.load(MODEL_WEIGHTS_PATH, weights_only=True) |
| 37 | + |
| 38 | + for layer_index in range(0, 4): |
| 39 | + for param in ["running_mean", "running_var", "num_batches_tracked", "weight", "bias"]: |
| 40 | + for block_index in [1, 2]: |
| 41 | + for bn_index in [1, 2]: |
| 42 | + # Assigning weights of 4th fusion layer of original model to 1st layer of SMP DPT model, |
| 43 | + # Assigning weights of 3rd fusion layer of original model to 2nd layer of SMP DPT model ... |
| 44 | + # and so on ... |
| 45 | + # This is because order of calling fusion layers is reversed in original DPT implementation |
| 46 | + dpt_model_dict[f"decoder.fusion_blocks.{layer_index}.residual_conv_block{block_index}.batch_norm_{bn_index}.{param}"] = \ |
| 47 | + dpt_model_dict.pop(f"scratch.refinenet{4 - layer_index}.resConfUnit{block_index}.bn{bn_index}.{param}") |
| 48 | + |
| 49 | + if param in ["weight", "bias"]: |
| 50 | + if param == "weight": |
| 51 | + for block_index in [1, 2]: |
| 52 | + for conv_index in [1, 2]: |
| 53 | + dpt_model_dict[f"decoder.fusion_blocks.{layer_index}.residual_conv_block{block_index}.conv_{conv_index}.{param}"] = \ |
| 54 | + dpt_model_dict.pop(f"scratch.refinenet{4 - layer_index}.resConfUnit{block_index}.conv{conv_index}.{param}") |
| 55 | + |
| 56 | + dpt_model_dict[f"decoder.reassemble_blocks.{layer_index}.project_to_feature_dim.{param}"] = \ |
| 57 | + dpt_model_dict.pop(f"scratch.layer{layer_index + 1}_rn.{param}") |
| 58 | + |
| 59 | + dpt_model_dict[f"decoder.fusion_blocks.{layer_index}.project.{param}"] = \ |
| 60 | + dpt_model_dict.pop(f"scratch.refinenet{4 - layer_index}.out_conv.{param}") |
| 61 | + |
| 62 | + dpt_model_dict[f"decoder.projection_blocks.{layer_index}.project.0.{param}"] = \ |
| 63 | + dpt_model_dict.pop(f"pretrained.act_postprocess{layer_index + 1}.0.project.0.{param}") |
| 64 | + |
| 65 | + dpt_model_dict[f"decoder.reassemble_blocks.{layer_index}.project_to_out_channel.{param}"] = \ |
| 66 | + dpt_model_dict.pop(f"pretrained.act_postprocess{layer_index + 1}.3.{param}") |
| 67 | + |
| 68 | + if layer_index != 2: |
| 69 | + dpt_model_dict[f"decoder.reassemble_blocks.{layer_index}.upsample.{param}"] = \ |
| 70 | + dpt_model_dict.pop(f"pretrained.act_postprocess{layer_index + 1}.4.{param}") |
| 71 | + |
| 72 | + # Changing state dict keys for segmentation head |
| 73 | + dpt_model_dict = { |
| 74 | + name.replace("scratch.output_conv", "segmentation_head.head"): parameter |
| 75 | + for name, parameter in dpt_model_dict.items() |
| 76 | + } |
| 77 | + |
| 78 | + # Changing state dict keys for encoder layers |
| 79 | + dpt_model_dict = { |
| 80 | + name.replace("pretrained.model", "encoder.model"): parameter |
| 81 | + for name, parameter in dpt_model_dict.items() |
| 82 | + } |
| 83 | + |
| 84 | + # Removing keys, value pairs associated with auxiliary head |
| 85 | + dpt_model_dict = { |
| 86 | + name: parameter |
| 87 | + for name, parameter in dpt_model_dict.items() |
| 88 | + if not name.startswith("auxlayer") |
| 89 | + } |
| 90 | + # fmt: on |
| 91 | + |
| 92 | + smp_model.load_state_dict(dpt_model_dict, strict=True) |
| 93 | + |
| 94 | + # ------- DO NOT touch this section ------- |
| 95 | + smp_model.eval() |
| 96 | + |
| 97 | + input_tensor = torch.ones((1, 3, 384, 384)) |
| 98 | + output = smp_model(input_tensor) |
| 99 | + |
| 100 | + print(output.shape) |
| 101 | + print(output[0, 0, :3, :3]) |
| 102 | + |
| 103 | + expected_slice = torch.tensor( |
| 104 | + [ |
| 105 | + [3.4243, 3.4553, 3.4863], |
| 106 | + [3.3332, 3.2876, 3.2419], |
| 107 | + [3.2422, 3.1199, 2.9975], |
| 108 | + ] |
| 109 | + ) |
| 110 | + |
| 111 | + torch.testing.assert_close( |
| 112 | + output[0, 0, :3, :3], expected_slice, atol=1e-4, rtol=1e-4 |
| 113 | + ) |
| 114 | + |
| 115 | + # Saving |
| 116 | + transform = get_transform() |
| 117 | + |
| 118 | + transform.save_pretrained(HF_HUB_PATH) |
| 119 | + smp_model.save_pretrained(HF_HUB_PATH, push_to_hub=PUSH_TO_HUB) |
| 120 | + |
| 121 | + # Re-loading to make sure everything is saved correctly |
| 122 | + smp_model = smp.from_pretrained(HF_HUB_PATH) |
0 commit comments