Add all the needed files
Browse files- .gitattributes +35 -35
- best_pattern_reconstructor.pth +3 -0
- markov_chains.pkl +3 -0
- pattern_reconstructor_final.pth +3 -0
- requirements.txt +6 -0
- visual_markov_patterns_generator.py +418 -0
.gitattributes
CHANGED
|
@@ -1,35 +1,35 @@
|
|
| 1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
best_pattern_reconstructor.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:38759525a44ba192967008c883f82debfb1db75695b45d9c08a027d7a9f12396
|
| 3 |
+
size 2766709
|
markov_chains.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0198d94f3071fbb19a25bfa10711d531277c66186403c5dd728e6345ff3daf8b
|
| 3 |
+
size 158175882
|
pattern_reconstructor_final.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9b191f1812f54342dde3e058806937535cfd964f5d0b2542e970b23101519803
|
| 3 |
+
size 2766729
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==5.33.0
|
| 2 |
+
numpy>2.1.0
|
| 3 |
+
opencv_contrib_python==4.11.0.86
|
| 4 |
+
opencv_python==4.11.0.86
|
| 5 |
+
Pillow==11.2.1
|
| 6 |
+
torch==2.7.0
|
visual_markov_patterns_generator.py
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import random
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
from collections import defaultdict
|
| 6 |
+
import cv2
|
| 7 |
+
import pickle
|
| 8 |
+
import os
|
| 9 |
+
from PIL import Image
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class Config:
|
| 13 |
+
"""Generation system configuration"""
|
| 14 |
+
|
| 15 |
+
IMG_SIZE = 128
|
| 16 |
+
PATCH_SIZE = 16
|
| 17 |
+
GRID_SIZE = 8
|
| 18 |
+
PATTERN_DIM = 32
|
| 19 |
+
|
| 20 |
+
PATTERN_TYPES = ["sobel", "haar", "gradient", "texture", "color", "frequency"]
|
| 21 |
+
|
| 22 |
+
@property
|
| 23 |
+
def active_pattern_types(self):
|
| 24 |
+
return self.PATTERN_TYPES
|
| 25 |
+
|
| 26 |
+
@property
|
| 27 |
+
def n_active_patterns(self):
|
| 28 |
+
return len(self.active_pattern_types)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class PatternExtractor:
|
| 32 |
+
"""Extract various pattern types from images"""
|
| 33 |
+
|
| 34 |
+
def __init__(self):
|
| 35 |
+
self.pattern_dim = Config.PATTERN_DIM
|
| 36 |
+
|
| 37 |
+
def sobel_filter(self, patch):
|
| 38 |
+
"""Sobel filters for edge detection"""
|
| 39 |
+
gray = torch.mean(patch, dim=0).numpy()
|
| 40 |
+
sobel_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
|
| 41 |
+
sobel_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
|
| 42 |
+
|
| 43 |
+
sobel_x = self._normalize_and_reduce(sobel_x)
|
| 44 |
+
sobel_y = self._normalize_and_reduce(sobel_y)
|
| 45 |
+
|
| 46 |
+
return np.concatenate([sobel_x, sobel_y])
|
| 47 |
+
|
| 48 |
+
def haar_features(self, patch):
|
| 49 |
+
"""Haar-like features"""
|
| 50 |
+
gray = torch.mean(patch, dim=0).numpy()
|
| 51 |
+
h, w = gray.shape
|
| 52 |
+
|
| 53 |
+
left_half = gray[:, : w // 2]
|
| 54 |
+
right_half = gray[:, w // 2 :]
|
| 55 |
+
haar_vertical = np.mean(left_half) - np.mean(right_half)
|
| 56 |
+
|
| 57 |
+
top_half = gray[: h // 2, :]
|
| 58 |
+
bottom_half = gray[h // 2 :, :]
|
| 59 |
+
haar_horizontal = np.mean(top_half) - np.mean(bottom_half)
|
| 60 |
+
|
| 61 |
+
haar_features = np.array([haar_vertical, haar_horizontal])
|
| 62 |
+
return self._expand_to_dim(haar_features)
|
| 63 |
+
|
| 64 |
+
def gradient_features(self, patch):
|
| 65 |
+
"""Gradient features"""
|
| 66 |
+
gradients = []
|
| 67 |
+
for c in range(patch.shape[0]):
|
| 68 |
+
channel = patch[c].numpy()
|
| 69 |
+
grad_x = np.gradient(channel, axis=1)
|
| 70 |
+
grad_y = np.gradient(channel, axis=0)
|
| 71 |
+
|
| 72 |
+
grad_stats = np.array(
|
| 73 |
+
[
|
| 74 |
+
np.mean(np.abs(grad_x)),
|
| 75 |
+
np.mean(np.abs(grad_y)),
|
| 76 |
+
np.std(grad_x),
|
| 77 |
+
np.std(grad_y),
|
| 78 |
+
]
|
| 79 |
+
)
|
| 80 |
+
gradients.extend(grad_stats)
|
| 81 |
+
|
| 82 |
+
return self._expand_to_dim(np.array(gradients))
|
| 83 |
+
|
| 84 |
+
def texture_features(self, patch):
|
| 85 |
+
"""Texture features"""
|
| 86 |
+
gray = torch.mean(patch, dim=0).numpy()
|
| 87 |
+
h, w = gray.shape
|
| 88 |
+
lbp_features = []
|
| 89 |
+
|
| 90 |
+
for i in range(1, h - 1):
|
| 91 |
+
for j in range(1, w - 1):
|
| 92 |
+
center = gray[i, j]
|
| 93 |
+
pattern = 0
|
| 94 |
+
for di in [-1, 0, 1]:
|
| 95 |
+
for dj in [-1, 0, 1]:
|
| 96 |
+
if di == 0 and dj == 0:
|
| 97 |
+
continue
|
| 98 |
+
if gray[i + di, j + dj] > center:
|
| 99 |
+
pattern += 1
|
| 100 |
+
lbp_features.append(pattern)
|
| 101 |
+
|
| 102 |
+
lbp_stats = np.array(
|
| 103 |
+
[
|
| 104 |
+
np.mean(lbp_features),
|
| 105 |
+
np.std(lbp_features),
|
| 106 |
+
np.max(lbp_features),
|
| 107 |
+
np.min(lbp_features),
|
| 108 |
+
]
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
return self._expand_to_dim(lbp_stats)
|
| 112 |
+
|
| 113 |
+
def color_features(self, patch):
|
| 114 |
+
"""Color features"""
|
| 115 |
+
color_stats = []
|
| 116 |
+
for c in range(patch.shape[0]):
|
| 117 |
+
channel = patch[c]
|
| 118 |
+
stats = torch.tensor(
|
| 119 |
+
[
|
| 120 |
+
torch.mean(channel),
|
| 121 |
+
torch.std(channel),
|
| 122 |
+
torch.max(channel),
|
| 123 |
+
torch.min(channel),
|
| 124 |
+
]
|
| 125 |
+
)
|
| 126 |
+
color_stats.extend(stats.numpy())
|
| 127 |
+
|
| 128 |
+
return self._expand_to_dim(np.array(color_stats))
|
| 129 |
+
|
| 130 |
+
def frequency_features(self, patch):
|
| 131 |
+
"""Frequency features via FFT"""
|
| 132 |
+
gray = torch.mean(patch, dim=0).numpy()
|
| 133 |
+
fft = np.fft.fft2(gray)
|
| 134 |
+
fft_magnitude = np.abs(fft)
|
| 135 |
+
|
| 136 |
+
freq_stats = np.array(
|
| 137 |
+
[
|
| 138 |
+
np.mean(fft_magnitude),
|
| 139 |
+
np.std(fft_magnitude),
|
| 140 |
+
np.sum(fft_magnitude[: len(fft_magnitude) // 4, :]),
|
| 141 |
+
np.sum(fft_magnitude[3 * len(fft_magnitude) // 4 :, :]),
|
| 142 |
+
]
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
return self._expand_to_dim(freq_stats)
|
| 146 |
+
|
| 147 |
+
def _normalize_and_reduce(self, array):
|
| 148 |
+
"""Normalize and reduce to smaller dimension"""
|
| 149 |
+
array = (array - np.min(array)) / (np.max(array) - np.min(array) + 1e-8)
|
| 150 |
+
return array.flatten()[: self.pattern_dim // 2]
|
| 151 |
+
|
| 152 |
+
def _expand_to_dim(self, features):
|
| 153 |
+
"""Expand features to required dimension"""
|
| 154 |
+
if len(features) >= self.pattern_dim:
|
| 155 |
+
return features[: self.pattern_dim]
|
| 156 |
+
else:
|
| 157 |
+
expanded = np.zeros(self.pattern_dim)
|
| 158 |
+
expanded[: len(features)] = features
|
| 159 |
+
return expanded
|
| 160 |
+
|
| 161 |
+
def extract_all_patterns(self, patch):
|
| 162 |
+
"""Extract all pattern types"""
|
| 163 |
+
patterns = {}
|
| 164 |
+
patterns["sobel"] = self.sobel_filter(patch)
|
| 165 |
+
patterns["haar"] = self.haar_features(patch)
|
| 166 |
+
patterns["gradient"] = self.gradient_features(patch)
|
| 167 |
+
patterns["texture"] = self.texture_features(patch)
|
| 168 |
+
patterns["color"] = self.color_features(patch)
|
| 169 |
+
patterns["frequency"] = self.frequency_features(patch)
|
| 170 |
+
|
| 171 |
+
return patterns
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
class PatternReconstructor(nn.Module):
|
| 175 |
+
"""Neural network for reconstructing RGB patches from patterns"""
|
| 176 |
+
|
| 177 |
+
def __init__(self, config):
|
| 178 |
+
super(PatternReconstructor, self).__init__()
|
| 179 |
+
|
| 180 |
+
input_dim = config.n_active_patterns * config.PATTERN_DIM
|
| 181 |
+
hidden_dim = 512
|
| 182 |
+
|
| 183 |
+
self.encoder = nn.Sequential(
|
| 184 |
+
nn.Linear(input_dim, hidden_dim),
|
| 185 |
+
nn.ReLU(),
|
| 186 |
+
nn.Dropout(0.2),
|
| 187 |
+
nn.Linear(hidden_dim, hidden_dim // 2),
|
| 188 |
+
nn.ReLU(),
|
| 189 |
+
nn.Dropout(0.2),
|
| 190 |
+
nn.Linear(hidden_dim // 2, hidden_dim // 4),
|
| 191 |
+
nn.ReLU(),
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
self.decoder_conv = nn.Sequential(
|
| 195 |
+
nn.ConvTranspose2d(128, 128, 4, 1, 0),
|
| 196 |
+
nn.ReLU(),
|
| 197 |
+
nn.ConvTranspose2d(128, 64, 4, 2, 1),
|
| 198 |
+
nn.ReLU(),
|
| 199 |
+
nn.ConvTranspose2d(64, 32, 4, 2, 1),
|
| 200 |
+
nn.ReLU(),
|
| 201 |
+
nn.ConvTranspose2d(32, 3, 3, 1, 1),
|
| 202 |
+
nn.Tanh(),
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
def forward(self, patterns):
|
| 206 |
+
encoded = self.encoder(patterns)
|
| 207 |
+
encoded = encoded.view(-1, 128, 1, 1)
|
| 208 |
+
reconstructed = self.decoder_conv(encoded)
|
| 209 |
+
return reconstructed
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
class MultiPatternMarkovChain:
|
| 213 |
+
"""Multi-dimensional Markov chain for patterns"""
|
| 214 |
+
|
| 215 |
+
def __init__(self, config):
|
| 216 |
+
self.config = config
|
| 217 |
+
self.chains = {}
|
| 218 |
+
self.pattern_store = {}
|
| 219 |
+
|
| 220 |
+
for pattern_type in config.active_pattern_types:
|
| 221 |
+
self.chains[pattern_type] = defaultdict(lambda: defaultdict(list))
|
| 222 |
+
|
| 223 |
+
def generate_patterns(self, grid_size, temperature=1.0):
|
| 224 |
+
"""Generate patterns for all active types"""
|
| 225 |
+
result = {}
|
| 226 |
+
|
| 227 |
+
for pattern_type in self.config.active_pattern_types:
|
| 228 |
+
if pattern_type in self.chains:
|
| 229 |
+
result[pattern_type] = self._generate_single_pattern_grid(
|
| 230 |
+
pattern_type, grid_size, temperature
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
return result
|
| 234 |
+
|
| 235 |
+
def _generate_single_pattern_grid(self, pattern_type, grid_size, temperature):
|
| 236 |
+
"""Generate grid for single pattern type"""
|
| 237 |
+
chain = self.chains[pattern_type]
|
| 238 |
+
grid = [[None for _ in range(grid_size)] for _ in range(grid_size)]
|
| 239 |
+
|
| 240 |
+
if not chain:
|
| 241 |
+
return grid
|
| 242 |
+
|
| 243 |
+
start_hash = random.choice(list(chain.keys()))
|
| 244 |
+
grid[0][0] = start_hash
|
| 245 |
+
|
| 246 |
+
for i in range(grid_size):
|
| 247 |
+
for j in range(grid_size):
|
| 248 |
+
if i == 0 and j == 0:
|
| 249 |
+
continue
|
| 250 |
+
|
| 251 |
+
candidates = []
|
| 252 |
+
|
| 253 |
+
if i > 0 and grid[i - 1][j] is not None:
|
| 254 |
+
candidates.extend(chain[grid[i - 1][j]].get("down", []))
|
| 255 |
+
|
| 256 |
+
if j > 0 and grid[i][j - 1] is not None:
|
| 257 |
+
candidates.extend(chain[grid[i][j - 1]].get("right", []))
|
| 258 |
+
|
| 259 |
+
if candidates:
|
| 260 |
+
grid[i][j] = self._softmax_selection(candidates, temperature)
|
| 261 |
+
else:
|
| 262 |
+
grid[i][j] = random.choice(list(chain.keys()))
|
| 263 |
+
|
| 264 |
+
return grid
|
| 265 |
+
|
| 266 |
+
def _softmax_selection(self, candidates, temperature):
|
| 267 |
+
"""Selection with temperature consideration"""
|
| 268 |
+
if not candidates:
|
| 269 |
+
return None
|
| 270 |
+
|
| 271 |
+
if len(candidates) == 1:
|
| 272 |
+
return candidates[0][0]
|
| 273 |
+
|
| 274 |
+
probs = np.array([c[1] for c in candidates])
|
| 275 |
+
|
| 276 |
+
if temperature == 0:
|
| 277 |
+
max_idx = np.argmax(probs)
|
| 278 |
+
return candidates[max_idx][0]
|
| 279 |
+
else:
|
| 280 |
+
probs = probs / temperature
|
| 281 |
+
exp_probs = np.exp(probs - np.max(probs))
|
| 282 |
+
softmax_probs = exp_probs / np.sum(exp_probs)
|
| 283 |
+
|
| 284 |
+
idx = np.random.choice(len(candidates), p=softmax_probs)
|
| 285 |
+
return candidates[idx][0]
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
class VisualMarkovGenerator:
|
| 289 |
+
"""Main image generator class"""
|
| 290 |
+
|
| 291 |
+
def __init__(
|
| 292 |
+
self,
|
| 293 |
+
model_path="pattern_reconstructor_final.pth",
|
| 294 |
+
markov_path="markov_chains.pkl",
|
| 295 |
+
):
|
| 296 |
+
self.config = Config()
|
| 297 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 298 |
+
self.model = PatternReconstructor(self.config).to(self.device)
|
| 299 |
+
|
| 300 |
+
if os.path.exists(model_path):
|
| 301 |
+
self.model.load_state_dict(torch.load(model_path, map_location=self.device))
|
| 302 |
+
print(f"Model loaded from {model_path}")
|
| 303 |
+
else:
|
| 304 |
+
print(f"Model file {model_path} not found. Using random weights.")
|
| 305 |
+
|
| 306 |
+
if os.path.exists(markov_path):
|
| 307 |
+
with open(markov_path, "rb") as f:
|
| 308 |
+
save_data = pickle.load(f)
|
| 309 |
+
|
| 310 |
+
self.markov_chain = MultiPatternMarkovChain(self.config)
|
| 311 |
+
self.markov_chain.pattern_store = save_data["pattern_store"]
|
| 312 |
+
|
| 313 |
+
for pattern_type in self.config.active_pattern_types:
|
| 314 |
+
if pattern_type in save_data["chains"]:
|
| 315 |
+
chain_data = save_data["chains"][pattern_type]
|
| 316 |
+
for state, directions in chain_data.items():
|
| 317 |
+
for direction, candidates in directions.items():
|
| 318 |
+
self.markov_chain.chains[pattern_type][state][
|
| 319 |
+
direction
|
| 320 |
+
] = candidates
|
| 321 |
+
|
| 322 |
+
print(f"Markov chains loaded from {markov_path}")
|
| 323 |
+
else:
|
| 324 |
+
print(f"Markov chains file {markov_path} not found. Creating empty chain.")
|
| 325 |
+
self.markov_chain = MultiPatternMarkovChain(self.config)
|
| 326 |
+
|
| 327 |
+
self.model.eval()
|
| 328 |
+
|
| 329 |
+
def generate_image(self, temperature=1.0, seed=None):
|
| 330 |
+
"""Generate image with specified temperature"""
|
| 331 |
+
if seed is not None:
|
| 332 |
+
random.seed(seed)
|
| 333 |
+
np.random.seed(seed)
|
| 334 |
+
torch.manual_seed(seed)
|
| 335 |
+
|
| 336 |
+
pattern_grids = self.markov_chain.generate_patterns(
|
| 337 |
+
self.config.GRID_SIZE, temperature=temperature
|
| 338 |
+
)
|
| 339 |
+
|
| 340 |
+
if not pattern_grids:
|
| 341 |
+
print("Warning: empty patterns for generation")
|
| 342 |
+
return self._create_random_image()
|
| 343 |
+
|
| 344 |
+
result_image = self._generate_image_from_patterns(pattern_grids)
|
| 345 |
+
|
| 346 |
+
img_np = torch.clamp(result_image, 0, 1).permute(1, 2, 0).numpy()
|
| 347 |
+
img_np = (img_np * 255).astype(np.uint8)
|
| 348 |
+
|
| 349 |
+
return Image.fromarray(img_np)
|
| 350 |
+
|
| 351 |
+
def _generate_image_from_patterns(self, pattern_grids):
|
| 352 |
+
"""Generate image from patterns"""
|
| 353 |
+
first_pattern_type = list(pattern_grids.keys())[0]
|
| 354 |
+
grid_size = len(pattern_grids[first_pattern_type])
|
| 355 |
+
result_image = torch.zeros(
|
| 356 |
+
(3, grid_size * self.config.PATCH_SIZE, grid_size * self.config.PATCH_SIZE)
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
+
with torch.no_grad():
|
| 360 |
+
for i in range(grid_size):
|
| 361 |
+
for j in range(grid_size):
|
| 362 |
+
pattern_vector = []
|
| 363 |
+
|
| 364 |
+
for pattern_type in self.config.active_pattern_types:
|
| 365 |
+
if pattern_type in pattern_grids:
|
| 366 |
+
pattern_hash = pattern_grids[pattern_type][i][j]
|
| 367 |
+
if pattern_hash in self.markov_chain.pattern_store:
|
| 368 |
+
pattern = self.markov_chain.pattern_store[pattern_hash]
|
| 369 |
+
pattern_vector.extend(pattern)
|
| 370 |
+
else:
|
| 371 |
+
pattern_vector.extend(np.zeros(self.config.PATTERN_DIM))
|
| 372 |
+
else:
|
| 373 |
+
pattern_vector.extend(np.zeros(self.config.PATTERN_DIM))
|
| 374 |
+
|
| 375 |
+
if len(pattern_vector) > 0:
|
| 376 |
+
pattern_tensor = (
|
| 377 |
+
torch.FloatTensor(pattern_vector)
|
| 378 |
+
.unsqueeze(0)
|
| 379 |
+
.to(self.device)
|
| 380 |
+
)
|
| 381 |
+
reconstructed_patch = (
|
| 382 |
+
self.model(pattern_tensor).cpu().squeeze(0)
|
| 383 |
+
)
|
| 384 |
+
|
| 385 |
+
start_i = i * self.config.PATCH_SIZE
|
| 386 |
+
end_i = start_i + self.config.PATCH_SIZE
|
| 387 |
+
start_j = j * self.config.PATCH_SIZE
|
| 388 |
+
end_j = start_j + self.config.PATCH_SIZE
|
| 389 |
+
|
| 390 |
+
result_image[:, start_i:end_i, start_j:end_j] = (
|
| 391 |
+
reconstructed_patch
|
| 392 |
+
)
|
| 393 |
+
|
| 394 |
+
return result_image
|
| 395 |
+
|
| 396 |
+
def _create_random_image(self):
|
| 397 |
+
"""Create random image in case of error"""
|
| 398 |
+
random_img = torch.rand(3, self.config.IMG_SIZE, self.config.IMG_SIZE)
|
| 399 |
+
img_np = (random_img.permute(1, 2, 0).numpy() * 255).astype(np.uint8)
|
| 400 |
+
return Image.fromarray(img_np)
|
| 401 |
+
|
| 402 |
+
|
| 403 |
+
def create_generator(
|
| 404 |
+
model_path="pattern_reconstructor_final.pth", markov_path="markov_chains.pkl"
|
| 405 |
+
):
|
| 406 |
+
"""Create generator instance"""
|
| 407 |
+
return VisualMarkovGenerator(model_path, markov_path)
|
| 408 |
+
|
| 409 |
+
|
| 410 |
+
if __name__ == "__main__":
|
| 411 |
+
generator = create_generator()
|
| 412 |
+
stats = generator.get_stats()
|
| 413 |
+
print("Generator statistics:")
|
| 414 |
+
print(f"Total patterns: {stats['total_patterns']}")
|
| 415 |
+
print(f"Model loaded: {stats['model_loaded']}")
|
| 416 |
+
|
| 417 |
+
img = generator.generate_image(temperature=1.0, seed=42)
|
| 418 |
+
print(f"Generated image size: {img.size}")
|