Spaces:
Sleeping
Sleeping
Updated random_forest.py
Browse files- app.py +1 -1
- models/random_forest_sr.py +19 -8
- models/random_forest_sr1.py +68 -0
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from models.espcn import espcn_upscale
|
|
| 10 |
from models.sr_gan import srgan_upscale
|
| 11 |
|
| 12 |
# ✅ New import for Random Forest Super Resolution
|
| 13 |
-
from models.random_forest_sr import random_forest_upscale
|
| 14 |
|
| 15 |
lancros_page = gr.Interface(
|
| 16 |
fn=upsample_lancros,
|
|
|
|
| 10 |
from models.sr_gan import srgan_upscale
|
| 11 |
|
| 12 |
# ✅ New import for Random Forest Super Resolution
|
| 13 |
+
from CV_Project.models.random_forest_sr import random_forest_upscale
|
| 14 |
|
| 15 |
lancros_page = gr.Interface(
|
| 16 |
fn=upsample_lancros,
|
models/random_forest_sr.py
CHANGED
|
@@ -1,15 +1,16 @@
|
|
| 1 |
import numpy as np
|
| 2 |
-
from skimage import transform, util
|
| 3 |
from sklearn.ensemble import RandomForestRegressor
|
| 4 |
from skimage.util import view_as_windows
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
# CONFIGURATION
|
| 8 |
-
PATCH_SIZE = (
|
| 9 |
STEP = 1
|
| 10 |
-
N_ESTIMATORS =
|
| 11 |
-
MAX_DEPTH =
|
| 12 |
SCALE_FACTOR = 2
|
|
|
|
| 13 |
|
| 14 |
def extract_patches(img, patch_size, step):
|
| 15 |
patches = view_as_windows(img, patch_size, step)
|
|
@@ -17,7 +18,11 @@ def extract_patches(img, patch_size, step):
|
|
| 17 |
return patches.reshape(h * w, -1)
|
| 18 |
|
| 19 |
def train_rf(X, y):
|
| 20 |
-
rf = RandomForestRegressor(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
rf.fit(X, y)
|
| 22 |
return rf
|
| 23 |
|
|
@@ -53,12 +58,18 @@ def random_forest_upscale(pil_img: Image.Image) -> Image.Image:
|
|
| 53 |
|
| 54 |
for c in range(img.shape[2]):
|
| 55 |
channel = img[:, :, c]
|
| 56 |
-
hr_channel = transform.resize(channel, hr_shape)
|
| 57 |
-
lr_channel = transform.resize(hr_channel, (hr_shape[0] // SCALE_FACTOR, hr_shape[1] // SCALE_FACTOR))
|
| 58 |
-
lr_channel_up = transform.resize(lr_channel, hr_shape)
|
| 59 |
|
| 60 |
X = extract_patches(lr_channel_up, PATCH_SIZE, STEP)
|
| 61 |
y = extract_patches(hr_channel, PATCH_SIZE, STEP)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
rf_model = train_rf(X, y)
|
| 63 |
sr = predict_and_reconstruct(rf_model, lr_channel_up, PATCH_SIZE, STEP, hr_shape)
|
| 64 |
sr_channels.append(sr)
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
+
from skimage import transform, util, color
|
| 3 |
from sklearn.ensemble import RandomForestRegressor
|
| 4 |
from skimage.util import view_as_windows
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
# CONFIGURATION
|
| 8 |
+
PATCH_SIZE = (3, 3)
|
| 9 |
STEP = 1
|
| 10 |
+
N_ESTIMATORS = 10
|
| 11 |
+
MAX_DEPTH = 10
|
| 12 |
SCALE_FACTOR = 2
|
| 13 |
+
SAMPLE_PATCHES = 10000 # Controls speed/accuracy trade-off
|
| 14 |
|
| 15 |
def extract_patches(img, patch_size, step):
|
| 16 |
patches = view_as_windows(img, patch_size, step)
|
|
|
|
| 18 |
return patches.reshape(h * w, -1)
|
| 19 |
|
| 20 |
def train_rf(X, y):
|
| 21 |
+
rf = RandomForestRegressor(
|
| 22 |
+
n_estimators=N_ESTIMATORS,
|
| 23 |
+
max_depth=MAX_DEPTH,
|
| 24 |
+
n_jobs=-1
|
| 25 |
+
)
|
| 26 |
rf.fit(X, y)
|
| 27 |
return rf
|
| 28 |
|
|
|
|
| 58 |
|
| 59 |
for c in range(img.shape[2]):
|
| 60 |
channel = img[:, :, c]
|
| 61 |
+
hr_channel = transform.resize(channel, hr_shape, anti_aliasing=True)
|
| 62 |
+
lr_channel = transform.resize(hr_channel, (hr_shape[0] // SCALE_FACTOR, hr_shape[1] // SCALE_FACTOR), anti_aliasing=True)
|
| 63 |
+
lr_channel_up = transform.resize(lr_channel, hr_shape, anti_aliasing=True)
|
| 64 |
|
| 65 |
X = extract_patches(lr_channel_up, PATCH_SIZE, STEP)
|
| 66 |
y = extract_patches(hr_channel, PATCH_SIZE, STEP)
|
| 67 |
+
|
| 68 |
+
if X.shape[0] > SAMPLE_PATCHES:
|
| 69 |
+
idx = np.random.choice(X.shape[0], SAMPLE_PATCHES, replace=False)
|
| 70 |
+
X = X[idx]
|
| 71 |
+
y = y[idx]
|
| 72 |
+
|
| 73 |
rf_model = train_rf(X, y)
|
| 74 |
sr = predict_and_reconstruct(rf_model, lr_channel_up, PATCH_SIZE, STEP, hr_shape)
|
| 75 |
sr_channels.append(sr)
|
models/random_forest_sr1.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from skimage import transform, util
|
| 3 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 4 |
+
from skimage.util import view_as_windows
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# CONFIGURATION
|
| 8 |
+
PATCH_SIZE = (5, 5)
|
| 9 |
+
STEP = 1
|
| 10 |
+
N_ESTIMATORS = 50
|
| 11 |
+
MAX_DEPTH = 20
|
| 12 |
+
SCALE_FACTOR = 2
|
| 13 |
+
|
| 14 |
+
def extract_patches(img, patch_size, step):
|
| 15 |
+
patches = view_as_windows(img, patch_size, step)
|
| 16 |
+
h, w = patches.shape[:2]
|
| 17 |
+
return patches.reshape(h * w, -1)
|
| 18 |
+
|
| 19 |
+
def train_rf(X, y):
|
| 20 |
+
rf = RandomForestRegressor(n_estimators=N_ESTIMATORS, max_depth=MAX_DEPTH, n_jobs=-1)
|
| 21 |
+
rf.fit(X, y)
|
| 22 |
+
return rf
|
| 23 |
+
|
| 24 |
+
def predict_and_reconstruct(model, lr_img, patch_size, step, out_shape):
|
| 25 |
+
lr_patches = extract_patches(lr_img, patch_size, step)
|
| 26 |
+
preds = model.predict(lr_patches)
|
| 27 |
+
|
| 28 |
+
patch_h, patch_w = patch_size
|
| 29 |
+
img_h = (lr_img.shape[0] - patch_h) // step + 1
|
| 30 |
+
img_w = (lr_img.shape[1] - patch_w) // step + 1
|
| 31 |
+
|
| 32 |
+
result = np.zeros(out_shape)
|
| 33 |
+
weight = np.zeros(out_shape)
|
| 34 |
+
|
| 35 |
+
idx = 0
|
| 36 |
+
for i in range(img_h):
|
| 37 |
+
for j in range(img_w):
|
| 38 |
+
patch = preds[idx].reshape(patch_h, patch_w)
|
| 39 |
+
result[i*step:i*step+patch_h, j*step:j*step+patch_w] += patch
|
| 40 |
+
weight[i*step:i*step+patch_h, j*step:j*step+patch_w] += 1
|
| 41 |
+
idx += 1
|
| 42 |
+
|
| 43 |
+
weight[weight == 0] = 1
|
| 44 |
+
return result / weight
|
| 45 |
+
|
| 46 |
+
def random_forest_upscale(pil_img: Image.Image) -> Image.Image:
|
| 47 |
+
img = np.array(pil_img) / 255.0 # Normalize
|
| 48 |
+
if img.ndim == 2:
|
| 49 |
+
img = np.expand_dims(img, axis=-1)
|
| 50 |
+
|
| 51 |
+
hr_shape = (img.shape[0] * SCALE_FACTOR, img.shape[1] * SCALE_FACTOR)
|
| 52 |
+
sr_channels = []
|
| 53 |
+
|
| 54 |
+
for c in range(img.shape[2]):
|
| 55 |
+
channel = img[:, :, c]
|
| 56 |
+
hr_channel = transform.resize(channel, hr_shape)
|
| 57 |
+
lr_channel = transform.resize(hr_channel, (hr_shape[0] // SCALE_FACTOR, hr_shape[1] // SCALE_FACTOR))
|
| 58 |
+
lr_channel_up = transform.resize(lr_channel, hr_shape)
|
| 59 |
+
|
| 60 |
+
X = extract_patches(lr_channel_up, PATCH_SIZE, STEP)
|
| 61 |
+
y = extract_patches(hr_channel, PATCH_SIZE, STEP)
|
| 62 |
+
rf_model = train_rf(X, y)
|
| 63 |
+
sr = predict_and_reconstruct(rf_model, lr_channel_up, PATCH_SIZE, STEP, hr_shape)
|
| 64 |
+
sr_channels.append(sr)
|
| 65 |
+
|
| 66 |
+
sr_image = np.stack(sr_channels, axis=-1)
|
| 67 |
+
sr_image = np.clip(sr_image * 255, 0, 255).astype(np.uint8)
|
| 68 |
+
return Image.fromarray(sr_image)
|