Refactor
This commit is contained in:
parent
eec9ee0275
commit
294f2fab12
9 changed files with 65414 additions and 14814 deletions
9
config.py
Normal file
9
config.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
DATA = sorted(Path("/mnt/wsl/PHYSICALDRIVE1/data/unsplash").glob("*.jpg"))
|
||||||
|
|
||||||
|
CACHE_PATH = Path("/mnt/wsl/PHYSICALDRIVE1/data/cache2")
|
||||||
|
CACHE_PATH.mkdir(exist_ok=True, parents=True)
|
||||||
|
|
||||||
|
MODELS_PATH = Path("models")
|
||||||
|
MODELS_PATH.mkdir(exist_ok=True, parents=True)
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -1,2 +0,0 @@
|
||||||
from .display_images import display_images
|
|
||||||
from .plot_histograms import plot_histograms
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
from torch.utils.data import Dataset
|
from torch.utils.data import Dataset
|
||||||
from typing import List, Optional
|
from typing import List, Optional, Tuple
|
||||||
from editor.utils import compute_histogram
|
from editor.utils import compute_histogram
|
||||||
from .random_edit import random_edit
|
from .random_edit import random_edit
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
@ -45,7 +45,19 @@ class HistogramDataset(Dataset):
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self._paths) * self._edit_count
|
return len(self._paths) * self._edit_count
|
||||||
|
|
||||||
def __getitem__(self, idx):
|
def get_original_image(self, original_idx: int) -> Image.Image:
|
||||||
|
original_path = self._paths[original_idx]
|
||||||
|
original = Image.open(original_path)
|
||||||
|
original.thumbnail(
|
||||||
|
self._target_size, Image.Resampling.LANCZOS
|
||||||
|
) # size will be at most target_size, the aspect ratio is preserved
|
||||||
|
return original
|
||||||
|
|
||||||
|
def get_edited_image(self, original_idx: int, edit_idx: int) -> Image.Image:
|
||||||
|
original_image = self.get_original_image(original_idx)
|
||||||
|
return random_edit(original_image, seed=edit_idx)
|
||||||
|
|
||||||
|
def __getitem__(self, idx: int) -> Tuple[torch.Tensor, torch.Tensor]:
|
||||||
if self._cache_path is not None:
|
if self._cache_path is not None:
|
||||||
self._cached_data_path = self._cache_path / f"{idx}.pt"
|
self._cached_data_path = self._cache_path / f"{idx}.pt"
|
||||||
if self._cached_data_path.exists():
|
if self._cached_data_path.exists():
|
||||||
|
|
@ -55,10 +67,7 @@ class HistogramDataset(Dataset):
|
||||||
print(f"Failed to load {self._cached_data_path}, regenerating...")
|
print(f"Failed to load {self._cached_data_path}, regenerating...")
|
||||||
|
|
||||||
original_idx = idx // self._edit_count
|
original_idx = idx // self._edit_count
|
||||||
original_path = self._paths[original_idx]
|
original = self.get_original_image(original_idx)
|
||||||
original = Image.open(original_path)
|
|
||||||
original.thumbnail(self._target_size, Image.Resampling.LANCZOS)
|
|
||||||
|
|
||||||
edited = random_edit(original, seed=idx)
|
edited = random_edit(original, seed=idx)
|
||||||
|
|
||||||
edited_histogram = compute_histogram(
|
edited_histogram = compute_histogram(
|
||||||
|
|
|
||||||
3
editor/visualisation/__init__.py
Normal file
3
editor/visualisation/__init__.py
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
from .display_images import display_images
|
||||||
|
from .plot_histograms_in_3d import plot_histograms_in_3d
|
||||||
|
from .plot_histograms_in_2d import plot_histograms_in_2d
|
||||||
25
editor/visualisation/display_images.py
Normal file
25
editor/visualisation/display_images.py
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
from typing import Dict
|
||||||
|
from PIL.Image import Image
|
||||||
|
from math import ceil
|
||||||
|
|
||||||
|
|
||||||
|
def display_images(images: Dict[str, Image], images_per_row: int = 3):
|
||||||
|
fig, axes = plt.subplots(
|
||||||
|
nrows=ceil(len(images) / images_per_row),
|
||||||
|
ncols=min(images_per_row, len(images)),
|
||||||
|
figsize=(12, 8),
|
||||||
|
)
|
||||||
|
|
||||||
|
axes = axes.flatten()
|
||||||
|
|
||||||
|
for i, (title, image) in enumerate(images.items()):
|
||||||
|
axes[i].imshow(image)
|
||||||
|
axes[i].axis("off")
|
||||||
|
axes[i].set_title(title)
|
||||||
|
|
||||||
|
for i in range(len(images), len(axes)):
|
||||||
|
axes[i].axis("off")
|
||||||
|
|
||||||
|
plt.tight_layout()
|
||||||
|
plt.show()
|
||||||
32
editor/visualisation/plot_histograms_in_2d.py
Normal file
32
editor/visualisation/plot_histograms_in_2d.py
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
from typing import Dict
|
||||||
|
|
||||||
|
|
||||||
|
def plot_histograms_in_2d(histograms: Dict[str, np.ndarray]):
|
||||||
|
fig = plt.figure(figsize=(15, 5))
|
||||||
|
|
||||||
|
for i, (title, histogram) in enumerate(histograms.items(), 1):
|
||||||
|
ax = fig.add_subplot(1, 3, i, projection="3d")
|
||||||
|
|
||||||
|
size = histogram.shape[0]
|
||||||
|
|
||||||
|
x, y, z = np.indices(histogram.shape)
|
||||||
|
x = x.flatten()
|
||||||
|
y = y.flatten()
|
||||||
|
z = z.flatten()
|
||||||
|
values = histogram.flatten()
|
||||||
|
|
||||||
|
sizes = values * 5000
|
||||||
|
|
||||||
|
colors = np.vstack((x, y, z)).T / (size - 1)
|
||||||
|
|
||||||
|
sc = ax.scatter(x, y, z, c=colors, s=sizes, marker="o", alpha=0.5)
|
||||||
|
|
||||||
|
ax.set_xlim([0, (size - 1)])
|
||||||
|
ax.set_ylim([0, (size - 1)])
|
||||||
|
ax.set_zlim([0, (size - 1)])
|
||||||
|
ax.set_title(title)
|
||||||
|
|
||||||
|
return fig
|
||||||
54
editor/visualisation/plot_histograms_in_3d.py
Normal file
54
editor/visualisation/plot_histograms_in_3d.py
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
from plotly.subplots import make_subplots
|
||||||
|
import plotly.graph_objects as go
|
||||||
|
from math import ceil
|
||||||
|
from typing import Dict
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
def plot_histograms_in_3d(
|
||||||
|
histograms: Dict[str, np.ndarray], histogram_per_row: int = 3
|
||||||
|
):
|
||||||
|
cols = min(histogram_per_row, len(histograms))
|
||||||
|
rows = ceil(len(histograms) / histogram_per_row)
|
||||||
|
fig = make_subplots(
|
||||||
|
rows=rows,
|
||||||
|
cols=cols,
|
||||||
|
specs=[[{"type": "scatter3d"} for _ in range(cols)] for _ in range(rows)],
|
||||||
|
)
|
||||||
|
for i, (title, histogram) in enumerate(histograms.items()):
|
||||||
|
fig.add_trace(
|
||||||
|
_get_3d_scatter_plot_from_histogram(title, histogram),
|
||||||
|
row=(i // (histogram_per_row + 1)) + 1,
|
||||||
|
col=(i % histogram_per_row) + 1,
|
||||||
|
)
|
||||||
|
fig.show()
|
||||||
|
|
||||||
|
|
||||||
|
def _get_3d_scatter_plot_from_histogram(title, histogram):
|
||||||
|
x, y, z, marker_size = [], [], [], []
|
||||||
|
bins = len(histogram)
|
||||||
|
|
||||||
|
for i, row in enumerate(histogram):
|
||||||
|
for j, col in enumerate(row):
|
||||||
|
for k, value in enumerate(col):
|
||||||
|
if value > 0:
|
||||||
|
x.append(i)
|
||||||
|
y.append(j)
|
||||||
|
z.append(k)
|
||||||
|
marker_size.append(value)
|
||||||
|
|
||||||
|
return go.Scatter3d(
|
||||||
|
x=x,
|
||||||
|
y=y,
|
||||||
|
z=z,
|
||||||
|
mode="markers",
|
||||||
|
marker=dict(
|
||||||
|
size=[min(20, ms * 10000) for ms in marker_size],
|
||||||
|
color=[
|
||||||
|
f"rgb({xi*256/bins},{yi*256/bins},{zi*256/bins})"
|
||||||
|
for xi, yi, zi in zip(x, y, z)
|
||||||
|
],
|
||||||
|
opacity=0.8,
|
||||||
|
),
|
||||||
|
name=title,
|
||||||
|
)
|
||||||
73106
show_histograms.ipynb
73106
show_histograms.ipynb
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue