CLEANUP: Removing obsolete/untracked files from remote
Browse files- comparisons/torch_dream.py +0 -144
- toConvert/.gitkeep +0 -0
comparisons/torch_dream.py
DELETED
|
@@ -1,144 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
import argparse
|
| 3 |
-
import time
|
| 4 |
-
import torch
|
| 5 |
-
import torch.nn as nn
|
| 6 |
-
from torchvision import models, transforms
|
| 7 |
-
from PIL import Image
|
| 8 |
-
import numpy as np
|
| 9 |
-
import scipy.ndimage as nd
|
| 10 |
-
|
| 11 |
-
# device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
|
| 12 |
-
# MPS support for some ops (like rolling) might be tricky or just fall back to CPU.
|
| 13 |
-
# For fairness, we try to use MPS where possible.
|
| 14 |
-
DEVICE = torch.device("mps") if torch.backends.mps.is_available() else torch.device("cpu")
|
| 15 |
-
|
| 16 |
-
IMAGENET_MEAN = torch.tensor([0.485, 0.456, 0.406]).to(DEVICE).view(1, 3, 1, 1)
|
| 17 |
-
IMAGENET_STD = torch.tensor([0.229, 0.224, 0.225]).to(DEVICE).view(1, 3, 1, 1)
|
| 18 |
-
|
| 19 |
-
def preprocess(img_np):
|
| 20 |
-
# HWC -> CHW, Add batch dim
|
| 21 |
-
x = torch.from_numpy(img_np).float().permute(2, 0, 1).unsqueeze(0) / 255.0
|
| 22 |
-
x = x.to(DEVICE)
|
| 23 |
-
x = (x - IMAGENET_MEAN) / IMAGENET_STD
|
| 24 |
-
return x
|
| 25 |
-
|
| 26 |
-
def deprocess(x):
|
| 27 |
-
x = x * IMAGENET_STD + IMAGENET_MEAN
|
| 28 |
-
x = torch.clamp(x, 0, 1)
|
| 29 |
-
x = x.squeeze(0).permute(1, 2, 0).cpu().detach().numpy()
|
| 30 |
-
return (x * 255).astype(np.uint8)
|
| 31 |
-
|
| 32 |
-
def get_model(name):
|
| 33 |
-
if name == "googlenet":
|
| 34 |
-
model = models.googlenet(weights='DEFAULT')
|
| 35 |
-
layers = ["inception4c"] # Default roughly
|
| 36 |
-
elif name == "vgg16":
|
| 37 |
-
model = models.vgg16(weights='DEFAULT')
|
| 38 |
-
layers = ["features.20"] # relu4_2 roughly
|
| 39 |
-
elif name == "resnet50":
|
| 40 |
-
model = models.resnet50(weights='DEFAULT')
|
| 41 |
-
layers = ["layer4"]
|
| 42 |
-
else:
|
| 43 |
-
raise ValueError(name)
|
| 44 |
-
|
| 45 |
-
model.to(DEVICE)
|
| 46 |
-
model.eval()
|
| 47 |
-
for param in model.parameters():
|
| 48 |
-
param.requires_grad = False
|
| 49 |
-
return model, layers
|
| 50 |
-
|
| 51 |
-
class Hook:
|
| 52 |
-
def __init__(self, module):
|
| 53 |
-
self.hook = module.register_forward_hook(self.hook_fn)
|
| 54 |
-
self.activation = None
|
| 55 |
-
def hook_fn(self, module, input, output):
|
| 56 |
-
self.activation = output
|
| 57 |
-
def close(self):
|
| 58 |
-
self.hook.remove()
|
| 59 |
-
|
| 60 |
-
def deepdream(args):
|
| 61 |
-
img = Image.open(args.input).convert('RGB')
|
| 62 |
-
if args.width:
|
| 63 |
-
w, h = img.size
|
| 64 |
-
scale = args.width / w
|
| 65 |
-
img = img.resize((args.width, int(h*scale)), Image.LANCZOS)
|
| 66 |
-
|
| 67 |
-
img_np = np.array(img)
|
| 68 |
-
model, default_layer_names = get_model(args.model)
|
| 69 |
-
|
| 70 |
-
# Hooks
|
| 71 |
-
hooks = []
|
| 72 |
-
# Simplified layer selection for benchmark: just use leaf modules if possible
|
| 73 |
-
# or get by name. For torchvision models, names are tricky.
|
| 74 |
-
# We'll stick to a simple hardcoded layer for the benchmark comparison.
|
| 75 |
-
# GoogLeNet inception4c is usually 'inception4c' submodule.
|
| 76 |
-
|
| 77 |
-
target_modules = []
|
| 78 |
-
if args.model == "googlenet":
|
| 79 |
-
target_modules = [model.inception4c]
|
| 80 |
-
elif args.model == "vgg16":
|
| 81 |
-
target_modules = [model.features[20]] # relu4_2
|
| 82 |
-
elif args.model == "resnet50":
|
| 83 |
-
target_modules = [model.layer4]
|
| 84 |
-
|
| 85 |
-
for m in target_modules:
|
| 86 |
-
hooks.append(Hook(m))
|
| 87 |
-
|
| 88 |
-
input_tensor = preprocess(img_np).requires_grad_(True)
|
| 89 |
-
|
| 90 |
-
print(f"Running Torch ({DEVICE}) Dream on {args.model}...")
|
| 91 |
-
start_t = time.time()
|
| 92 |
-
|
| 93 |
-
# Octave handling is complex to replicate exactly pixel-perfect with MLX version
|
| 94 |
-
# due to resize implementation differences.
|
| 95 |
-
# We will implement a Single Scale run for benchmarking pure iteration speed.
|
| 96 |
-
# Multi-scale introduces resize overhead which is CPU bound mostly.
|
| 97 |
-
|
| 98 |
-
optimizer = torch.optim.SGD([input_tensor], lr=args.lr)
|
| 99 |
-
|
| 100 |
-
for i in range(args.steps):
|
| 101 |
-
optimizer.zero_grad()
|
| 102 |
-
model(input_tensor)
|
| 103 |
-
|
| 104 |
-
loss = 0
|
| 105 |
-
for h in hooks:
|
| 106 |
-
act = h.activation
|
| 107 |
-
loss += act.pow(2).mean()
|
| 108 |
-
|
| 109 |
-
loss.backward()
|
| 110 |
-
|
| 111 |
-
# Gradient Smoothing (Gaussian Blur) would go here.
|
| 112 |
-
# For benchmark simplicity, we skip explicit smoothing to test raw backprop speed,
|
| 113 |
-
# or we could add a simple avg pool.
|
| 114 |
-
|
| 115 |
-
# Normalize grad
|
| 116 |
-
g = input_tensor.grad
|
| 117 |
-
g /= (torch.std(g) + 1e-8)
|
| 118 |
-
input_tensor.grad = g
|
| 119 |
-
|
| 120 |
-
optimizer.step()
|
| 121 |
-
|
| 122 |
-
# Clip
|
| 123 |
-
# (Manual clip to bounds omitted for speed, standard clamp at end)
|
| 124 |
-
|
| 125 |
-
torch.cuda.synchronize() if str(DEVICE) == 'cuda' else None
|
| 126 |
-
# MPS sync?
|
| 127 |
-
|
| 128 |
-
duration = time.time() - start_t
|
| 129 |
-
print(f"Time: {duration:.4f}s")
|
| 130 |
-
|
| 131 |
-
out = deprocess(input_tensor)
|
| 132 |
-
Image.fromarray(out).save(args.output)
|
| 133 |
-
|
| 134 |
-
if __name__ == "__main__":
|
| 135 |
-
parser = argparse.ArgumentParser()
|
| 136 |
-
parser.add_argument("--input", required=True)
|
| 137 |
-
parser.add_argument("--output", default="torch_out.jpg")
|
| 138 |
-
parser.add_argument("--model", default="googlenet")
|
| 139 |
-
parser.add_argument("--steps", type=int, default=10)
|
| 140 |
-
parser.add_argument("--lr", type=float, default=0.05)
|
| 141 |
-
parser.add_argument("--width", type=int, default=400)
|
| 142 |
-
args = parser.parse_args()
|
| 143 |
-
|
| 144 |
-
deepdream(args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
toConvert/.gitkeep
DELETED
|
File without changes
|