|
| 1 | +import sys |
| 2 | +from types import ModuleType |
| 3 | + |
| 4 | +# --- STEP 1: FIX FOR PYTHON 3.12 --- |
| 5 | +pkg_mock = ModuleType("pkg_resources") |
| 6 | +try: |
| 7 | + from packaging.version import parse as parse_version |
| 8 | + pkg_mock.parse_version = parse_version |
| 9 | +except ImportError: |
| 10 | + pkg_mock.parse_version = lambda v: [int(x) for x in v.split('.') if x.isdigit()] |
| 11 | +sys.modules["pkg_resources"] = pkg_mock |
| 12 | + |
| 13 | +# --- STEP 2: IMPORTS --- |
| 14 | +import os |
| 15 | +import numpy as np |
| 16 | +import tensorflow as tf |
| 17 | +from PIL import Image |
| 18 | + |
| 19 | +# --- STEP 3: PREPARE MODEL --- |
| 20 | +# Using InceptionV3 - the classic "Dream" model |
| 21 | +base_model = tf.keras.applications.InceptionV3(include_top=False, weights='imagenet') |
| 22 | + |
| 23 | +# Maximize the activations of these specific layers |
| 24 | +# Mixed3/4/5 are the "trippy" layers with eyes and patterns |
| 25 | +names = ['mixed3', 'mixed5'] |
| 26 | +layers = [base_model.get_layer(name).output for name in names] |
| 27 | +dream_model = tf.keras.Model(inputs=base_model.input, outputs=layers) |
| 28 | + |
| 29 | +def deprocess(img): |
| 30 | + img = 255 * (img + 1.0) / 2.0 |
| 31 | + return tf.cast(img, tf.uint8) |
| 32 | + |
| 33 | +def calc_loss(img, model): |
| 34 | + img_batch = tf.expand_dims(img, axis=0) |
| 35 | + layer_activations = model(img_batch) |
| 36 | + if len(layer_activations) == 1: |
| 37 | + layer_activations = [layer_activations] |
| 38 | + |
| 39 | + losses = [] |
| 40 | + for act in layer_activations: |
| 41 | + loss = tf.math.reduce_mean(act) |
| 42 | + losses.append(loss) |
| 43 | + return tf.reduce_sum(losses) |
| 44 | + |
| 45 | +@tf.function |
| 46 | +def deepdream_step(img, model, step_size): |
| 47 | + with tf.GradientTape() as tape: |
| 48 | + tape.watch(img) |
| 49 | + loss = calc_loss(img, model) |
| 50 | + gradients = tape.gradient(loss, img) |
| 51 | + gradients /= tf.math.reduce_std(gradients) + 1e-8 |
| 52 | + img = img + gradients * step_size |
| 53 | + img = tf.clip_by_value(img, -1, 1) |
| 54 | + return loss, img |
| 55 | + |
| 56 | +# --- STEP 4: MAIN PROCESS --- |
| 57 | +def run_dream(input_path, output_path, steps=100, step_size=0.01): |
| 58 | + # Load Image |
| 59 | + if not os.path.exists(input_path): |
| 60 | + print(f"Error: {input_path} not found!") |
| 61 | + return |
| 62 | + |
| 63 | + img = Image.open(input_path).convert('RGB') |
| 64 | + img = img.resize((500, 500)) # Resizing to save your RAM |
| 65 | + img = np.array(img) |
| 66 | + img = tf.keras.applications.inception_v3.preprocess_input(img) |
| 67 | + img = tf.convert_to_tensor(img) |
| 68 | + |
| 69 | + print(f"Dreaming on {input_path}...") |
| 70 | + for step in range(steps): |
| 71 | + loss, img = deepdream_step(img, dream_model, step_size) |
| 72 | + if step % 20 == 0: |
| 73 | + print(f"Step {step}, Loss {loss.numpy()}") |
| 74 | + |
| 75 | + # Save Output |
| 76 | + result = deprocess(img) |
| 77 | + final_img = Image.fromarray(result.numpy()) |
| 78 | + |
| 79 | + # Ensure output dir exists |
| 80 | + os.makedirs(os.path.dirname(output_path), exist_ok=True) |
| 81 | + final_img.save(output_path) |
| 82 | + print(f"Success! Dream saved to {output_path}") |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + run_dream('input/input.jpg', 'output/output.jpg') |
0 commit comments