File size: 8,039 Bytes
2dd52ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python3
"""
Universal Model Converter for DeepDream-MLX.
Converts PyTorch (.pth) and Torch7 (.t7) models to MLX (.npz).
Also supports auto-downloading standard Places365 models.
Defaults to float16 for optimal performance on Apple Silicon.
"""

import os
import argparse
import glob
import numpy as np
import torch
import torchvision.models as models
from torch.hub import download_url_to_file

# Optional Torchfile for .t7 support
try:
    import torchfile
except ImportError:
    torchfile = None

# --- Configuration ---
PLACES365_URLS = {
    "alexnet": "http://places2.csail.mit.edu/models_places365/alexnet_places365.pth.tar",
    "resnet50": "http://places2.csail.mit.edu/models_places365/resnet50_places365.pth.tar",
    "vgg16": "http://places2.csail.mit.edu/models_places365/vgg16_places365.pth.tar",
    "googlenet": "http://places2.csail.mit.edu/models_places365/googlenet_places365.pth.tar"
}

# --- Helper Functions ---

def convert_tensor(tensor, target_dtype=np.float16):
    """Converts a tensor/array to the target numpy dtype."""
    if isinstance(tensor, torch.Tensor):
        return tensor.cpu().detach().numpy().astype(target_dtype)
    elif isinstance(tensor, np.ndarray):
        return tensor.astype(target_dtype)
    else:
        return np.array(tensor).astype(target_dtype)

def clean_state_dict(state_dict):
    """
    Flattens the state dictionary and removes common prefix artifacts 
    like 'module.' from DataParallel wrapping.
    """
    new_dict = {}
    for k, v in state_dict.items():
        # Remove 'module.' anywhere in the key
        name = k.replace("module.", "")
        new_dict[name] = convert_tensor(v)
    return new_dict

def get_places365_model_skeleton(arch):
    """Returns a standard PyTorch model structure for Places365."""
    if arch == "alexnet":
        return models.alexnet(num_classes=365)
    elif arch == "resnet50":
        return models.resnet50(num_classes=365)
    elif arch == "vgg16":
        return models.vgg16(num_classes=365)
    elif arch == "googlenet":
        return models.googlenet(num_classes=365, aux_logits=False)
    else:
        raise ValueError(f"Unknown architecture: {arch}")

# --- Conversion Logic ---

def convert_torch7(filepath, target_dir):
    if torchfile is None:
        print(f"⚠️  Skipping {filepath}: 'torchfile' not installed. Run `pip install torchfile`.")
        return

    print(f"Processing Torch7 file: {filepath}")
    try:
        model_obj = torchfile.load(filepath)
        converted_state = {}

        def extract_layers(layer, prefix=""):
            if hasattr(layer, 'weight') and layer.weight is not None:
                converted_state[f"{prefix}.weight"] = convert_tensor(layer.weight)
            if hasattr(layer, 'bias') and layer.bias is not None:
                converted_state[f"{prefix}.bias"] = convert_tensor(layer.bias)
            
            if hasattr(layer, 'modules') and layer.modules:
                for i, sublayer in enumerate(layer.modules):
                    # 0-based indexing for compatibility
                    next_prefix = f"{prefix}.{i}" if prefix else f"{i}"
                    extract_layers(sublayer, next_prefix)
        
        extract_layers(model_obj)
        
        if not converted_state:
            print(f"❌ No weights found in {filepath}.")
            return

        name_base = os.path.splitext(os.path.basename(filepath))[0]
        out_path = os.path.join(target_dir, f"{name_base}_t7_mlx.npz")
        np.savez(out_path, **converted_state)
        print(f"✅ Saved {out_path} ({len(converted_state)} tensors)")

    except Exception as e:
        print(f"❌ Failed to convert {filepath}: {e}")

def convert_pytorch(filepath, target_dir):
    print(f"Processing PyTorch file: {filepath}")
    try:
        checkpoint = torch.load(filepath, map_location="cpu")
        
        if isinstance(checkpoint, dict) and 'state_dict' in checkpoint:
            state_dict = checkpoint['state_dict']
        elif isinstance(checkpoint, dict):
            state_dict = checkpoint
        else:
            print(f"❌ Unknown checkpoint format in {filepath}")
            return

        clean_dict = clean_state_dict(state_dict)
        
        name_base = os.path.splitext(os.path.basename(filepath))[0]
        # Avoid double extension if file was .pth.tar
        if name_base.endswith(".pth"):
            name_base = os.path.splitext(name_base)[0]
            
        out_path = os.path.join(target_dir, f"{name_base}_mlx.npz")
        np.savez(out_path, **clean_dict)
        
        size_mb = os.path.getsize(out_path) / (1024*1024)
        print(f"✅ Saved {out_path} ({size_mb:.1f} MB)")

    except Exception as e:
        print(f"❌ Failed to convert {filepath}: {e}")

def download_and_convert_places365(arch, download_dir, target_dir):
    url = PLACES365_URLS.get(arch)
    if not url:
        print(f"No URL for {arch}")
        return

    filename = os.path.join(download_dir, os.path.basename(url))
    
    # 1. Download
    if not os.path.exists(filename):
        print(f"Downloading {arch} from {url}...")
        try:
            download_url_to_file(url, filename)
        except Exception as e:
            print(f"Download failed: {e}")
            return
    else:
        print(f"Found cached {filename}")

    # 2. Load into standard Skeleton (ensures structural correctness)
    print(f"Loading {arch} into PyTorch structure...")
    try:
        model = get_places365_model_skeleton(arch)
        checkpoint = torch.load(filename, map_location="cpu")
        state_dict = checkpoint['state_dict'] if 'state_dict' in checkpoint else checkpoint
        
        # Robust Load
        new_state_dict = {k.replace("module.", ""): v for k, v in state_dict.items()}
        try:
            model.load_state_dict(new_state_dict, strict=True)
        except:
            model.load_state_dict(new_state_dict, strict=False)
            
        # 3. Export
        model.eval()
        final_dict = clean_state_dict(model.state_dict())
        out_path = os.path.join(target_dir, f"{arch}_places365_mlx.npz")
        np.savez(out_path, **final_dict)
        print(f"✅ Saved {out_path}")

    except Exception as e:
        print(f"Failed to process {arch}: {e}")

# --- Main CLI ---

def main():
    parser = argparse.ArgumentParser(description="DeepDream-MLX Model Converter")
    parser.add_argument("--scan", default="toConvert", help="Directory to scan for local files")
    parser.add_argument("--download", choices=["alexnet", "resnet50", "vgg16", "googlenet", "all"], 
                        help="Download and convert specific Places365 models")
    parser.add_argument("--dest", default=".", help="Output directory for .npz files")
    args = parser.parse_args()

    if not os.path.exists(args.dest):
        os.makedirs(args.dest)

    # 1. Handle Downloads
    if args.download:
        if not os.path.exists(args.scan):
            os.makedirs(args.scan)
            
        targets = ["alexnet", "resnet50", "vgg16", "googlenet"] if args.download == "all" else [args.download]
        for t in targets:
            download_and_convert_places365(t, args.scan, args.dest)

    # 2. Handle Local Scan
    if os.path.exists(args.scan):
        print(f"\nScanning '{args.scan}' for local models...")
        files = glob.glob(os.path.join(args.scan, "*"))
        for f in files:
            if os.path.isdir(f): continue
            ext = os.path.splitext(f)[1].lower()
            
            if ext == ".t7":
                convert_torch7(f, args.dest)
            elif ext in [".pth", ".pt", ".tar", ".pkl"]:
                # If it looks like a downloaded places file we already processed, skip to avoid duplication
                # heuristic: if we just downloaded it. 
                convert_pytorch(f, args.dest)
            elif ext in [".caffemodel"]:
                print(f"⚠️  Skipping Caffe model {os.path.basename(f)} (Not supported)")

if __name__ == "__main__":
    main()