Add source code
Browse files- .gitignore +35 -0
- LICENSE +159 -0
- convert.py +308 -0
- model.py +227 -0
- requirements.txt +25 -0
- run.py +132 -0
- swin_ir.py +286 -0
- ui.py +1435 -0
- upscale.py +186 -0
.gitignore
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Model weights (large files — download via `python run.py convert` or get from HuggingFace)
|
| 2 |
+
weights/
|
| 3 |
+
|
| 4 |
+
# Python
|
| 5 |
+
__pycache__/
|
| 6 |
+
*.py[cod]
|
| 7 |
+
*.pyo
|
| 8 |
+
*.pyd
|
| 9 |
+
.Python
|
| 10 |
+
*.egg-info/
|
| 11 |
+
dist/
|
| 12 |
+
build/
|
| 13 |
+
.eggs/
|
| 14 |
+
*.egg
|
| 15 |
+
|
| 16 |
+
# Virtual environments
|
| 17 |
+
.venv/
|
| 18 |
+
venv/
|
| 19 |
+
env/
|
| 20 |
+
.env
|
| 21 |
+
|
| 22 |
+
# OS
|
| 23 |
+
.DS_Store
|
| 24 |
+
Thumbs.db
|
| 25 |
+
|
| 26 |
+
# Editor
|
| 27 |
+
.vscode/
|
| 28 |
+
.idea/
|
| 29 |
+
*.swp
|
| 30 |
+
*.swo
|
| 31 |
+
|
| 32 |
+
# Output files
|
| 33 |
+
output/
|
| 34 |
+
outputs/
|
| 35 |
+
*.tmp
|
LICENSE
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 10 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 11 |
+
|
| 12 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 13 |
+
the copyright owner that is granting the License.
|
| 14 |
+
|
| 15 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 16 |
+
other entities that control, are controlled by, or are under common
|
| 17 |
+
control with that entity. For the purposes of this definition,
|
| 18 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 19 |
+
direction or management of such entity, whether by contract or
|
| 20 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 21 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 22 |
+
|
| 23 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 24 |
+
exercising permissions granted by this License.
|
| 25 |
+
|
| 26 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 27 |
+
including but not limited to software source code, documentation
|
| 28 |
+
source, and configuration files.
|
| 29 |
+
|
| 30 |
+
"Object" form shall mean any form resulting from mechanical
|
| 31 |
+
transformation or translation of a Source form, including but
|
| 32 |
+
not limited to compiled object code, generated documentation,
|
| 33 |
+
and conversions to other media types.
|
| 34 |
+
|
| 35 |
+
"Work" shall mean the work of authorship made available under
|
| 36 |
+
the License, as indicated by a copyright notice that is included in
|
| 37 |
+
or attached to the work (an example is provided in the Appendix below).
|
| 38 |
+
|
| 39 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 40 |
+
form, that is based on (or derived from) the Work and for which the
|
| 41 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 42 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 43 |
+
of this License, Derivative Works shall not include works that remain
|
| 44 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 45 |
+
the Work and Derivative Works thereof.
|
| 46 |
+
|
| 47 |
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
| 48 |
+
in the Work by the copyright owner or by an individual or Legal Entity
|
| 49 |
+
authorized to submit on behalf of the copyright owner. For the purposes
|
| 50 |
+
of this definition, "submit" means any form of electronic, verbal, or
|
| 51 |
+
written communication sent to the Licensor or its representatives,
|
| 52 |
+
including but not limited to communication on electronic mailing lists,
|
| 53 |
+
source code control systems, and issue tracking systems that are managed
|
| 54 |
+
by, or on behalf of, the Licensor for the purpose of developing the Work,
|
| 55 |
+
but excluding communication that is conspicuously marked or designated in
|
| 56 |
+
writing by the copyright owner as "Not a Contribution."
|
| 57 |
+
|
| 58 |
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
| 59 |
+
whom a Contribution has been received by the Licensor and included
|
| 60 |
+
within the Work.
|
| 61 |
+
|
| 62 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 63 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 64 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 65 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 66 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 67 |
+
Work and such Derivative Works in Source or Object form.
|
| 68 |
+
|
| 69 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 70 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 71 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 72 |
+
(except as stated in this section) patent license to make, have made,
|
| 73 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 74 |
+
where such license applies only to those patent claims licensable
|
| 75 |
+
by such Contributor that are necessarily infringed by their
|
| 76 |
+
Contribution(s) alone or by the combination of their Contribution(s)
|
| 77 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 78 |
+
institute patent litigation against any entity (including a cross-claim
|
| 79 |
+
or counterfault in a lawsuit) alleging that the Work or any
|
| 80 |
+
Contribution embodied within the Work constitutes direct or contributory
|
| 81 |
+
patent infringement, then any patent licenses granted to You under
|
| 82 |
+
this License for that Work shall terminate as of the date such
|
| 83 |
+
litigation is filed.
|
| 84 |
+
|
| 85 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 86 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 87 |
+
modifications, and in Source or Object form, provided that You
|
| 88 |
+
meet the following conditions:
|
| 89 |
+
|
| 90 |
+
(a) You must give any other recipients of the Work or Derivative
|
| 91 |
+
Works a copy of this License; and
|
| 92 |
+
|
| 93 |
+
(b) You must cause any modified files to carry prominent notices
|
| 94 |
+
stating that You changed the files; and
|
| 95 |
+
|
| 96 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 97 |
+
that You distribute, all copyright, patent, trademark, and
|
| 98 |
+
attribution notices from the Source form of the Work,
|
| 99 |
+
excluding those notices that do not pertain to any part of
|
| 100 |
+
the Derivative Works; and
|
| 101 |
+
|
| 102 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 103 |
+
distribution, You must include a readable copy of the
|
| 104 |
+
attribution notices contained within such NOTICE file, in
|
| 105 |
+
at least one of the following places: within a NOTICE text
|
| 106 |
+
file distributed as part of the Derivative Works; within
|
| 107 |
+
the Source form or documentation, if provided along with the
|
| 108 |
+
Derivative Works; or, within a display generated by the
|
| 109 |
+
Derivative Works, if and wherever such third-party notices
|
| 110 |
+
normally appear. The contents of the NOTICE file are for
|
| 111 |
+
informational purposes only and do not modify the License.
|
| 112 |
+
You may add Your own attribution notices within Derivative
|
| 113 |
+
Works that You distribute, alongside or in addition to the
|
| 114 |
+
NOTICE text from the Work, provided that such additional
|
| 115 |
+
attribution notices cannot be construed as modifying the License.
|
| 116 |
+
|
| 117 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 118 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 119 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 120 |
+
this License, without any additional terms or conditions.
|
| 121 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 122 |
+
the terms of any separate license agreement you may have executed
|
| 123 |
+
with Licensor regarding such Contributions.
|
| 124 |
+
|
| 125 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 126 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 127 |
+
except as required for reasonable and customary use in describing the
|
| 128 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 129 |
+
|
| 130 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 131 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 132 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 133 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 134 |
+
implied, including, without limitation, any warranties or conditions
|
| 135 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 136 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 137 |
+
appropriateness of using or reproducing the Work and assume any
|
| 138 |
+
risks associated with Your exercise of permissions under this License.
|
| 139 |
+
|
| 140 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 141 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 142 |
+
unless required by applicable law (such as deliberate and grossly
|
| 143 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 144 |
+
liable to You for damages, including any direct, indirect, special,
|
| 145 |
+
incidental, or exemplary damages of any character arising as a
|
| 146 |
+
result of this License or out of the use or inability to use the
|
| 147 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 148 |
+
work stoppage, computer failure or malfunction, or all other
|
| 149 |
+
commercial damages or losses), even if such Contributor has been
|
| 150 |
+
advised of the possibility of such damages.
|
| 151 |
+
|
| 152 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 153 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 154 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 155 |
+
or other liability obligations and/or rights consistent with this
|
| 156 |
+
License. However, in accepting such obligations, You may offer only
|
| 157 |
+
conditions consistent with this License.
|
| 158 |
+
|
| 159 |
+
END OF TERMS AND CONDITIONS
|
convert.py
ADDED
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Convert Thera RDN weights (air/pro) from Flax pickle to MLX safetensors format.
|
| 4 |
+
|
| 5 |
+
Requires: jax, flax, numpy, safetensors, huggingface_hub
|
| 6 |
+
These are only needed for conversion, not for inference.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import argparse
|
| 10 |
+
import os
|
| 11 |
+
import pickle
|
| 12 |
+
import sys
|
| 13 |
+
|
| 14 |
+
import numpy as np
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def conv_weight(kernel):
|
| 18 |
+
"""Flax Conv (H, W, C_in, C_out) → MLX Conv2d (C_out, H, W, C_in)"""
|
| 19 |
+
return np.transpose(np.asarray(kernel), (3, 0, 1, 2))
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def dense_weight(kernel):
|
| 23 |
+
"""Flax Dense (in, out) → MLX Linear (out, in)"""
|
| 24 |
+
return np.transpose(np.asarray(kernel), (1, 0))
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def layernorm_params(flax_ln, prefix):
|
| 28 |
+
"""Map Flax LayerNorm scale/bias to MLX weight/bias."""
|
| 29 |
+
w = {}
|
| 30 |
+
w[f'{prefix}.weight'] = np.asarray(flax_ln['scale'])
|
| 31 |
+
if 'bias' in flax_ln:
|
| 32 |
+
w[f'{prefix}.bias'] = np.asarray(flax_ln['bias'])
|
| 33 |
+
return w
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def convert_rdn_encoder(enc):
|
| 37 |
+
"""Convert RDN backbone weights."""
|
| 38 |
+
weights = {}
|
| 39 |
+
|
| 40 |
+
# SFE1 (Conv_0) and SFE2 (Conv_1)
|
| 41 |
+
weights['encoder.sfe1.weight'] = conv_weight(enc['Conv_0']['kernel'])
|
| 42 |
+
weights['encoder.sfe1.bias'] = np.asarray(enc['Conv_0']['bias'])
|
| 43 |
+
weights['encoder.sfe2.weight'] = conv_weight(enc['Conv_1']['kernel'])
|
| 44 |
+
weights['encoder.sfe2.bias'] = np.asarray(enc['Conv_1']['bias'])
|
| 45 |
+
|
| 46 |
+
# 16 Residual Dense Blocks
|
| 47 |
+
for i in range(16):
|
| 48 |
+
rdb = enc[f'RDB_{i}']
|
| 49 |
+
# 8 RDB_Conv layers per block
|
| 50 |
+
for j in range(8):
|
| 51 |
+
rc = rdb[f'RDB_Conv_{j}']['Conv_0']
|
| 52 |
+
prefix = f'encoder.rdbs.{i}.convs.{j}.conv'
|
| 53 |
+
weights[f'{prefix}.weight'] = conv_weight(rc['kernel'])
|
| 54 |
+
weights[f'{prefix}.bias'] = np.asarray(rc['bias'])
|
| 55 |
+
# Local fusion 1x1 conv (Conv_0 at RDB level)
|
| 56 |
+
lf = rdb['Conv_0']
|
| 57 |
+
prefix = f'encoder.rdbs.{i}.local_fusion'
|
| 58 |
+
weights[f'{prefix}.weight'] = conv_weight(lf['kernel'])
|
| 59 |
+
weights[f'{prefix}.bias'] = np.asarray(lf['bias'])
|
| 60 |
+
|
| 61 |
+
# Global Feature Fusion (Conv_2 = 1x1, Conv_3 = 3x3)
|
| 62 |
+
weights['encoder.gff_1x1.weight'] = conv_weight(enc['Conv_2']['kernel'])
|
| 63 |
+
weights['encoder.gff_1x1.bias'] = np.asarray(enc['Conv_2']['bias'])
|
| 64 |
+
weights['encoder.gff_3x3.weight'] = conv_weight(enc['Conv_3']['kernel'])
|
| 65 |
+
weights['encoder.gff_3x3.bias'] = np.asarray(enc['Conv_3']['bias'])
|
| 66 |
+
|
| 67 |
+
return weights
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def convert_swinir_tail(ref):
|
| 71 |
+
"""Convert SwinIR tail (refine) weights for rdn-pro."""
|
| 72 |
+
weights = {}
|
| 73 |
+
|
| 74 |
+
# conv_first: refine/Conv_0
|
| 75 |
+
weights['refine.conv_first.weight'] = conv_weight(ref['Conv_0']['kernel'])
|
| 76 |
+
weights['refine.conv_first.bias'] = np.asarray(ref['Conv_0']['bias'])
|
| 77 |
+
|
| 78 |
+
# patch_embed_norm: refine/PatchEmbed_0/LayerNorm_0
|
| 79 |
+
weights.update(layernorm_params(
|
| 80 |
+
ref['PatchEmbed_0']['LayerNorm_0'], 'refine.patch_embed_norm'))
|
| 81 |
+
|
| 82 |
+
# RSTB layers
|
| 83 |
+
rstb_depths = [7, 6] # number of SwinTransformerBlocks per RSTB
|
| 84 |
+
for i, depth in enumerate(rstb_depths):
|
| 85 |
+
rstb = ref[f'RSTB_{i}']
|
| 86 |
+
basic = rstb['BasicLayer_0']
|
| 87 |
+
|
| 88 |
+
for j in range(depth):
|
| 89 |
+
stb = basic[f'SwinTransformerBlock_{j}']
|
| 90 |
+
mlx_prefix = f'refine.layers.{i}.blocks.{j}'
|
| 91 |
+
|
| 92 |
+
# LayerNorm_0 → norm1
|
| 93 |
+
weights.update(layernorm_params(
|
| 94 |
+
stb['LayerNorm_0'], f'{mlx_prefix}.norm1'))
|
| 95 |
+
|
| 96 |
+
# WindowAttention_0
|
| 97 |
+
wa = stb['WindowAttention_0']
|
| 98 |
+
# qkv Dense → Linear
|
| 99 |
+
weights[f'{mlx_prefix}.attn.qkv.weight'] = dense_weight(wa['qkv']['kernel'])
|
| 100 |
+
weights[f'{mlx_prefix}.attn.qkv.bias'] = np.asarray(wa['qkv']['bias'])
|
| 101 |
+
# proj Dense → Linear
|
| 102 |
+
weights[f'{mlx_prefix}.attn.proj.weight'] = dense_weight(wa['proj']['kernel'])
|
| 103 |
+
weights[f'{mlx_prefix}.attn.proj.bias'] = np.asarray(wa['proj']['bias'])
|
| 104 |
+
# relative_position_bias_table (no transform needed)
|
| 105 |
+
weights[f'{mlx_prefix}.attn.relative_position_bias_table'] = \
|
| 106 |
+
np.asarray(wa['relative_position_bias_table'])
|
| 107 |
+
|
| 108 |
+
# LayerNorm_1 → norm2
|
| 109 |
+
weights.update(layernorm_params(
|
| 110 |
+
stb['LayerNorm_1'], f'{mlx_prefix}.norm2'))
|
| 111 |
+
|
| 112 |
+
# Mlp_0 → mlp
|
| 113 |
+
mlp = stb['Mlp_0']
|
| 114 |
+
weights[f'{mlx_prefix}.mlp.fc1.weight'] = dense_weight(mlp['Dense_0']['kernel'])
|
| 115 |
+
weights[f'{mlx_prefix}.mlp.fc1.bias'] = np.asarray(mlp['Dense_0']['bias'])
|
| 116 |
+
weights[f'{mlx_prefix}.mlp.fc2.weight'] = dense_weight(mlp['Dense_1']['kernel'])
|
| 117 |
+
weights[f'{mlx_prefix}.mlp.fc2.bias'] = np.asarray(mlp['Dense_1']['bias'])
|
| 118 |
+
|
| 119 |
+
# RSTB conv: RSTB_{i}/Conv_0
|
| 120 |
+
weights[f'refine.layers.{i}.conv.weight'] = conv_weight(rstb['Conv_0']['kernel'])
|
| 121 |
+
weights[f'refine.layers.{i}.conv.bias'] = np.asarray(rstb['Conv_0']['bias'])
|
| 122 |
+
|
| 123 |
+
# Final norm: refine/LayerNorm_0
|
| 124 |
+
weights.update(layernorm_params(ref['LayerNorm_0'], 'refine.norm'))
|
| 125 |
+
|
| 126 |
+
# conv_after_body: refine/Conv_1
|
| 127 |
+
weights['refine.conv_after_body.weight'] = conv_weight(ref['Conv_1']['kernel'])
|
| 128 |
+
weights['refine.conv_after_body.bias'] = np.asarray(ref['Conv_1']['bias'])
|
| 129 |
+
|
| 130 |
+
# conv_last: refine/Conv_2
|
| 131 |
+
weights['refine.conv_last.weight'] = conv_weight(ref['Conv_2']['kernel'])
|
| 132 |
+
weights['refine.conv_last.bias'] = np.asarray(ref['Conv_2']['bias'])
|
| 133 |
+
|
| 134 |
+
return weights
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
def convert_flax_to_mlx(flax_params, size='air'):
|
| 138 |
+
"""Map Flax parameter tree to flat MLX weight dict."""
|
| 139 |
+
p = flax_params['params']
|
| 140 |
+
weights = {}
|
| 141 |
+
|
| 142 |
+
# --- Global params (no transform) ---
|
| 143 |
+
weights['k'] = np.asarray(p['k'], dtype=np.float32).reshape(())
|
| 144 |
+
weights['components'] = np.asarray(p['components'], dtype=np.float32)
|
| 145 |
+
|
| 146 |
+
# --- RDN Backbone ---
|
| 147 |
+
weights.update(convert_rdn_encoder(p['encoder']))
|
| 148 |
+
|
| 149 |
+
# --- SwinIR tail (pro only) ---
|
| 150 |
+
if size == 'pro':
|
| 151 |
+
weights.update(convert_swinir_tail(p['refine']))
|
| 152 |
+
|
| 153 |
+
# --- Hypernetwork output conv ---
|
| 154 |
+
weights['out_conv.weight'] = conv_weight(p['out_conv']['kernel'])
|
| 155 |
+
weights['out_conv.bias'] = np.asarray(p['out_conv']['bias'])
|
| 156 |
+
|
| 157 |
+
return weights
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
REPO_IDS = {
|
| 161 |
+
'air': 'prs-eth/thera-rdn-air',
|
| 162 |
+
'pro': 'prs-eth/thera-rdn-pro',
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def download_model(size='air', filename="model.pkl", cache_dir=None):
|
| 167 |
+
"""Download model pickle from HuggingFace."""
|
| 168 |
+
from huggingface_hub import hf_hub_download
|
| 169 |
+
repo_id = REPO_IDS[size]
|
| 170 |
+
return hf_hub_download(repo_id=repo_id, filename=filename, cache_dir=cache_dir)
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def load_pickle_with_jax(path):
|
| 174 |
+
"""Load Flax pickle (requires jax and flax installed)."""
|
| 175 |
+
try:
|
| 176 |
+
import jax # noqa: F401 - needed for array reconstruction
|
| 177 |
+
import flax # noqa: F401 - needed for FrozenDict
|
| 178 |
+
except ImportError:
|
| 179 |
+
print("Error: jax and flax are required for loading the original weights.")
|
| 180 |
+
print("Install them with: pip install jax flax")
|
| 181 |
+
sys.exit(1)
|
| 182 |
+
|
| 183 |
+
with open(path, 'rb') as f:
|
| 184 |
+
checkpoint = pickle.load(f)
|
| 185 |
+
|
| 186 |
+
params = checkpoint['model']
|
| 187 |
+
backbone = checkpoint['backbone']
|
| 188 |
+
size = checkpoint['size']
|
| 189 |
+
print(f"Loaded checkpoint: backbone={backbone}, size={size}")
|
| 190 |
+
|
| 191 |
+
if backbone != 'rdn':
|
| 192 |
+
print(f"Warning: this converter is designed for rdn, got {backbone}")
|
| 193 |
+
|
| 194 |
+
return params
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
def load_pickle_without_jax(path):
|
| 198 |
+
"""
|
| 199 |
+
Attempt to load Flax pickle without JAX by mocking the required classes.
|
| 200 |
+
Falls back to JAX-based loading if this fails.
|
| 201 |
+
"""
|
| 202 |
+
import types
|
| 203 |
+
|
| 204 |
+
class MockFrozenDict(dict):
|
| 205 |
+
pass
|
| 206 |
+
|
| 207 |
+
class MockModule(types.ModuleType):
|
| 208 |
+
def __getattr__(self, name):
|
| 209 |
+
return MockModule(name)
|
| 210 |
+
|
| 211 |
+
class NumpyUnpickler(pickle.Unpickler):
|
| 212 |
+
def find_class(self, module, name):
|
| 213 |
+
if 'frozen_dict' in module and name == 'FrozenDict':
|
| 214 |
+
return MockFrozenDict
|
| 215 |
+
if module.startswith('jax') and name == '_reconstruct_array':
|
| 216 |
+
# JAX arrays are reconstructed from numpy arrays + metadata
|
| 217 |
+
def reconstruct(*args):
|
| 218 |
+
# args typically: (numpy_array, dtype, weak_type)
|
| 219 |
+
if len(args) >= 1 and isinstance(args[0], np.ndarray):
|
| 220 |
+
return args[0]
|
| 221 |
+
return np.array(args[0])
|
| 222 |
+
return reconstruct
|
| 223 |
+
if module.startswith('jax'):
|
| 224 |
+
try:
|
| 225 |
+
return super().find_class(module, name)
|
| 226 |
+
except (ImportError, AttributeError):
|
| 227 |
+
return lambda *a, **kw: a[0] if a else None
|
| 228 |
+
return super().find_class(module, name)
|
| 229 |
+
|
| 230 |
+
try:
|
| 231 |
+
with open(path, 'rb') as f:
|
| 232 |
+
checkpoint = NumpyUnpickler(f).load()
|
| 233 |
+
params = checkpoint['model']
|
| 234 |
+
backbone = checkpoint['backbone']
|
| 235 |
+
size = checkpoint['size']
|
| 236 |
+
print(f"Loaded checkpoint (no-jax mode): backbone={backbone}, size={size}")
|
| 237 |
+
return params
|
| 238 |
+
except Exception as e:
|
| 239 |
+
print(f"Mock unpickle failed ({e}), falling back to JAX-based loading...")
|
| 240 |
+
return load_pickle_with_jax(path)
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
def save_safetensors(weights, output_path):
|
| 244 |
+
"""Save weight dict as safetensors."""
|
| 245 |
+
from safetensors.numpy import save_file
|
| 246 |
+
save_file(weights, output_path)
|
| 247 |
+
print(f"Saved MLX weights to {output_path}")
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
def save_npz(weights, output_path):
|
| 251 |
+
"""Save weight dict as npz (fallback if safetensors not available)."""
|
| 252 |
+
np.savez(output_path, **weights)
|
| 253 |
+
print(f"Saved MLX weights to {output_path}")
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
def main():
|
| 257 |
+
parser = argparse.ArgumentParser(description="Convert Thera RDN weights to MLX format")
|
| 258 |
+
parser.add_argument('--model', type=str, choices=['air', 'pro'], default='air',
|
| 259 |
+
help='Model variant (default: air)')
|
| 260 |
+
parser.add_argument('--input', type=str, default=None,
|
| 261 |
+
help='Path to model.pkl (downloads from HuggingFace if not provided)')
|
| 262 |
+
parser.add_argument('--output', type=str, default=None,
|
| 263 |
+
help='Output path (default: weights-{model}.safetensors)')
|
| 264 |
+
parser.add_argument('--no-jax', action='store_true',
|
| 265 |
+
help='Try to load pickle without JAX installed')
|
| 266 |
+
args = parser.parse_args()
|
| 267 |
+
|
| 268 |
+
if args.output is None:
|
| 269 |
+
args.output = f'weights-{args.model}.safetensors'
|
| 270 |
+
|
| 271 |
+
# Download if needed
|
| 272 |
+
if args.input is None:
|
| 273 |
+
repo = REPO_IDS[args.model]
|
| 274 |
+
print(f"Downloading model from HuggingFace ({repo})...")
|
| 275 |
+
pkl_path = download_model(args.model)
|
| 276 |
+
else:
|
| 277 |
+
pkl_path = args.input
|
| 278 |
+
|
| 279 |
+
# Load
|
| 280 |
+
if args.no_jax:
|
| 281 |
+
flax_params = load_pickle_without_jax(pkl_path)
|
| 282 |
+
else:
|
| 283 |
+
flax_params = load_pickle_with_jax(pkl_path)
|
| 284 |
+
|
| 285 |
+
# Convert
|
| 286 |
+
print("Converting weights...")
|
| 287 |
+
mlx_weights = convert_flax_to_mlx(flax_params, size=args.model)
|
| 288 |
+
|
| 289 |
+
# Print summary
|
| 290 |
+
total_params = sum(w.size for w in mlx_weights.values())
|
| 291 |
+
print(f"Total parameters: {total_params:,}")
|
| 292 |
+
print(f"Weight entries: {len(mlx_weights)}")
|
| 293 |
+
|
| 294 |
+
# Save
|
| 295 |
+
output_path = args.output
|
| 296 |
+
if output_path.endswith('.safetensors'):
|
| 297 |
+
try:
|
| 298 |
+
save_safetensors(mlx_weights, output_path)
|
| 299 |
+
except ImportError:
|
| 300 |
+
output_path = output_path.replace('.safetensors', '.npz')
|
| 301 |
+
print("safetensors not installed, saving as npz instead")
|
| 302 |
+
save_npz(mlx_weights, output_path)
|
| 303 |
+
else:
|
| 304 |
+
save_npz(mlx_weights, output_path)
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
if __name__ == '__main__':
|
| 308 |
+
main()
|
model.py
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""MLX implementation of Thera super-resolution models (air/pro variants)."""
|
| 2 |
+
|
| 3 |
+
import math
|
| 4 |
+
import mlx.core as mx
|
| 5 |
+
import mlx.nn as nn
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# --- Utility functions ---
|
| 10 |
+
|
| 11 |
+
def make_grid(h, w):
|
| 12 |
+
"""Create coordinate grid in [-0.5, 0.5] with pixel centers."""
|
| 13 |
+
offset_h = 1.0 / (2 * h)
|
| 14 |
+
offset_w = 1.0 / (2 * w)
|
| 15 |
+
ys = np.linspace(-0.5 + offset_h, 0.5 - offset_h, h, dtype=np.float32)
|
| 16 |
+
xs = np.linspace(-0.5 + offset_w, 0.5 - offset_w, w, dtype=np.float32)
|
| 17 |
+
grid_y, grid_x = np.meshgrid(ys, xs, indexing='ij')
|
| 18 |
+
return np.stack([grid_y, grid_x], axis=-1) # (H, W, 2)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def interpolate_nearest(coords, grid):
|
| 22 |
+
"""
|
| 23 |
+
Nearest-neighbor sampling of a grid at given coordinates.
|
| 24 |
+
Args:
|
| 25 |
+
coords: mx.array (B, H, W, 2) coordinates in [-0.5, 0.5]
|
| 26 |
+
grid: mx.array (B, H', W', C) grid to sample from
|
| 27 |
+
Returns:
|
| 28 |
+
mx.array (B, H, W, C)
|
| 29 |
+
"""
|
| 30 |
+
B, Hp, Wp, C = grid.shape
|
| 31 |
+
_, H, W, _ = coords.shape
|
| 32 |
+
|
| 33 |
+
y = coords[..., 0] * Hp + (Hp - 1) / 2.0
|
| 34 |
+
x = coords[..., 1] * Wp + (Wp - 1) / 2.0
|
| 35 |
+
|
| 36 |
+
y_idx = mx.clip(mx.round(y).astype(mx.int32), 0, Hp - 1)
|
| 37 |
+
x_idx = mx.clip(mx.round(x).astype(mx.int32), 0, Wp - 1)
|
| 38 |
+
|
| 39 |
+
flat_idx = y_idx * Wp + x_idx # (B, H, W)
|
| 40 |
+
batch_offset = mx.arange(B).reshape(B, 1, 1) * (Hp * Wp)
|
| 41 |
+
global_idx = (flat_idx + batch_offset).reshape(-1) # (B*H*W,)
|
| 42 |
+
|
| 43 |
+
grid_flat = grid.reshape(-1, C) # (B*Hp*Wp, C)
|
| 44 |
+
result = grid_flat[global_idx] # (B*H*W, C)
|
| 45 |
+
return result.reshape(B, H, W, C)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# --- RDN Backbone ---
|
| 49 |
+
|
| 50 |
+
class RDBConv(nn.Module):
|
| 51 |
+
"""Single convolution layer within a Residual Dense Block."""
|
| 52 |
+
def __init__(self, in_channels: int, growth_rate: int, kernel_size: int = 3):
|
| 53 |
+
super().__init__()
|
| 54 |
+
self.conv = nn.Conv2d(in_channels, growth_rate, kernel_size,
|
| 55 |
+
padding=(kernel_size - 1) // 2)
|
| 56 |
+
|
| 57 |
+
def __call__(self, x):
|
| 58 |
+
out = nn.relu(self.conv(x))
|
| 59 |
+
return mx.concatenate([x, out], axis=-1)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
class RDB(nn.Module):
|
| 63 |
+
"""Residual Dense Block."""
|
| 64 |
+
def __init__(self, g0: int, growth_rate: int, n_conv_layers: int):
|
| 65 |
+
super().__init__()
|
| 66 |
+
self.convs = [
|
| 67 |
+
RDBConv(g0 + i * growth_rate, growth_rate)
|
| 68 |
+
for i in range(n_conv_layers)
|
| 69 |
+
]
|
| 70 |
+
total_ch = g0 + n_conv_layers * growth_rate
|
| 71 |
+
self.local_fusion = nn.Conv2d(total_ch, g0, kernel_size=1)
|
| 72 |
+
|
| 73 |
+
def __call__(self, x):
|
| 74 |
+
res = x
|
| 75 |
+
for conv in self.convs:
|
| 76 |
+
x = conv(x)
|
| 77 |
+
x = self.local_fusion(x)
|
| 78 |
+
return x + res
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
class RDN(nn.Module):
|
| 82 |
+
"""Residual Dense Network backbone (config B)."""
|
| 83 |
+
def __init__(self, n_colors: int = 3, g0: int = 64):
|
| 84 |
+
super().__init__()
|
| 85 |
+
D, C, G = 16, 8, 64 # config B
|
| 86 |
+
|
| 87 |
+
self.sfe1 = nn.Conv2d(n_colors, g0, kernel_size=3, padding=1)
|
| 88 |
+
self.sfe2 = nn.Conv2d(g0, g0, kernel_size=3, padding=1)
|
| 89 |
+
self.rdbs = [RDB(g0, G, C) for _ in range(D)]
|
| 90 |
+
self.gff_1x1 = nn.Conv2d(D * g0, g0, kernel_size=1)
|
| 91 |
+
self.gff_3x3 = nn.Conv2d(g0, g0, kernel_size=3, padding=1)
|
| 92 |
+
|
| 93 |
+
def __call__(self, x):
|
| 94 |
+
f1 = self.sfe1(x)
|
| 95 |
+
x = self.sfe2(f1)
|
| 96 |
+
|
| 97 |
+
rdb_outs = []
|
| 98 |
+
for rdb in self.rdbs:
|
| 99 |
+
x = rdb(x)
|
| 100 |
+
rdb_outs.append(x)
|
| 101 |
+
|
| 102 |
+
x = mx.concatenate(rdb_outs, axis=-1)
|
| 103 |
+
x = self.gff_1x1(x)
|
| 104 |
+
x = self.gff_3x3(x)
|
| 105 |
+
return x + f1
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
# --- Thera Model ---
|
| 109 |
+
|
| 110 |
+
class Thera(nn.Module):
|
| 111 |
+
"""
|
| 112 |
+
Thera: arbitrary-scale super-resolution using neural heat fields.
|
| 113 |
+
|
| 114 |
+
Stages:
|
| 115 |
+
1. Encoder (RDN backbone) produces features at source resolution
|
| 116 |
+
2. Optional refinement tail (identity for air, SwinIR for pro)
|
| 117 |
+
3. Hypernetwork (1x1 conv) predicts per-pixel field parameters
|
| 118 |
+
4. Heat field decoder produces RGB residuals
|
| 119 |
+
"""
|
| 120 |
+
OUT_DIM = 3
|
| 121 |
+
W0 = 1.0
|
| 122 |
+
MEAN = np.array([0.4488, 0.4371, 0.4040], dtype=np.float32)
|
| 123 |
+
VAR = np.array([0.25, 0.25, 0.25], dtype=np.float32)
|
| 124 |
+
|
| 125 |
+
def __init__(self, size='air'):
|
| 126 |
+
super().__init__()
|
| 127 |
+
self.size = size
|
| 128 |
+
self.hidden_dim = 32 if size == 'air' else 512
|
| 129 |
+
|
| 130 |
+
# Field params: Dense kernel + Thermal phase (alphabetical order)
|
| 131 |
+
n_field_params = self.hidden_dim * self.OUT_DIM + self.hidden_dim
|
| 132 |
+
|
| 133 |
+
self.encoder = RDN(n_colors=3, g0=64)
|
| 134 |
+
|
| 135 |
+
# Refinement tail
|
| 136 |
+
if size == 'pro':
|
| 137 |
+
from swin_ir import SwinIRTail
|
| 138 |
+
self.refine = SwinIRTail(
|
| 139 |
+
in_channels=64, embed_dim=180,
|
| 140 |
+
depths=(7, 6), num_heads=(6, 6),
|
| 141 |
+
window_size=8, mlp_ratio=2.0, num_feat=64)
|
| 142 |
+
# For 'air', no refine module (identity)
|
| 143 |
+
|
| 144 |
+
self.out_conv = nn.Conv2d(64, n_field_params, kernel_size=1)
|
| 145 |
+
|
| 146 |
+
self.k = mx.array(0.0)
|
| 147 |
+
self.components = mx.zeros((2, self.hidden_dim))
|
| 148 |
+
|
| 149 |
+
def encode(self, source_norm):
|
| 150 |
+
"""Run encoder + optional refinement tail."""
|
| 151 |
+
x = self.encoder(source_norm)
|
| 152 |
+
if self.size == 'pro':
|
| 153 |
+
x = self.refine(x)
|
| 154 |
+
return x
|
| 155 |
+
|
| 156 |
+
def decode(self, encoding, target_coords, t):
|
| 157 |
+
"""Predict RGB residuals at target coordinates."""
|
| 158 |
+
sampled = interpolate_nearest(target_coords, encoding)
|
| 159 |
+
phi = self.out_conv(sampled)
|
| 160 |
+
|
| 161 |
+
hd = self.hidden_dim
|
| 162 |
+
kernel = phi[..., :hd * self.OUT_DIM].reshape(
|
| 163 |
+
*phi.shape[:-1], hd, self.OUT_DIM)
|
| 164 |
+
phase = phi[..., hd * self.OUT_DIM:]
|
| 165 |
+
|
| 166 |
+
Hs, Ws = encoding.shape[1], encoding.shape[2]
|
| 167 |
+
source_grid = mx.array(make_grid(Hs, Ws))
|
| 168 |
+
source_coords = mx.broadcast_to(
|
| 169 |
+
source_grid[None], (encoding.shape[0],) + source_grid.shape)
|
| 170 |
+
nearest_src = interpolate_nearest(target_coords, source_coords)
|
| 171 |
+
rel_coords = target_coords - nearest_src
|
| 172 |
+
rel_coords_scaled = mx.concatenate([
|
| 173 |
+
rel_coords[..., 0:1] * Hs,
|
| 174 |
+
rel_coords[..., 1:2] * Ws,
|
| 175 |
+
], axis=-1)
|
| 176 |
+
|
| 177 |
+
x = rel_coords_scaled @ self.components
|
| 178 |
+
norm = mx.linalg.norm(self.components, axis=0)
|
| 179 |
+
t_4d = t[:, :, None, None] if t.ndim == 2 else t.reshape(-1, 1, 1, 1)
|
| 180 |
+
decay = mx.exp(-((self.W0 * norm) ** 2) * self.k * t_4d)
|
| 181 |
+
x = mx.sin(self.W0 * x + phase) * decay
|
| 182 |
+
out = mx.sum(x[..., None] * kernel, axis=-2)
|
| 183 |
+
|
| 184 |
+
return out
|
| 185 |
+
|
| 186 |
+
def upscale(self, source, target_h, target_w, ensemble=False):
|
| 187 |
+
mean = mx.array(self.MEAN)
|
| 188 |
+
var = mx.array(self.VAR)
|
| 189 |
+
std = mx.sqrt(var)
|
| 190 |
+
|
| 191 |
+
if ensemble:
|
| 192 |
+
outs = []
|
| 193 |
+
for k_rot in range(4):
|
| 194 |
+
src = mx.array(np.rot90(np.array(source), k=k_rot))
|
| 195 |
+
th = target_w if k_rot % 2 else target_h
|
| 196 |
+
tw = target_h if k_rot % 2 else target_w
|
| 197 |
+
out = self._upscale_single(src, th, tw, mean, var, std)
|
| 198 |
+
mx.eval(out)
|
| 199 |
+
out_np = np.rot90(np.array(out), k=-k_rot)
|
| 200 |
+
outs.append(out_np)
|
| 201 |
+
result = np.stack(outs).mean(0).clip(0.0, 1.0)
|
| 202 |
+
return mx.array((result * 255).round().astype(np.uint8))
|
| 203 |
+
else:
|
| 204 |
+
out = self._upscale_single(source, target_h, target_w, mean, var, std)
|
| 205 |
+
out = mx.clip(out, 0.0, 1.0)
|
| 206 |
+
return (out * 255 + 0.5).astype(mx.uint8)
|
| 207 |
+
|
| 208 |
+
def _upscale_single(self, source, target_h, target_w, mean, var, std):
|
| 209 |
+
Hs, Ws = source.shape[0], source.shape[1]
|
| 210 |
+
t = mx.array([(target_h / Hs) ** -2], dtype=mx.float32)[None]
|
| 211 |
+
|
| 212 |
+
target_grid = mx.array(make_grid(target_h, target_w))[None]
|
| 213 |
+
source_4d = source[None]
|
| 214 |
+
source_up = interpolate_nearest(target_grid, source_4d)
|
| 215 |
+
|
| 216 |
+
source_norm = (source_4d - mean) / std
|
| 217 |
+
encoding = self.encode(source_norm)
|
| 218 |
+
|
| 219 |
+
coords = mx.array(make_grid(target_h, target_w))[None]
|
| 220 |
+
residual = self.decode(encoding, coords, t)
|
| 221 |
+
|
| 222 |
+
out = residual * std + mean + source_up
|
| 223 |
+
return out[0]
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
# Backwards compatibility alias
|
| 227 |
+
TheraRDNAir = Thera
|
requirements.txt
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Thera MLX — requirements
|
| 2 |
+
# Install with: pip install -r requirements.txt
|
| 3 |
+
#
|
| 4 |
+
# Requires: Python 3.10+, macOS with Apple Silicon (M1/M2/M3/M4)
|
| 5 |
+
|
| 6 |
+
# Core ML
|
| 7 |
+
mlx>=0.16.0
|
| 8 |
+
|
| 9 |
+
# Image I/O
|
| 10 |
+
Pillow>=10.0.0
|
| 11 |
+
numpy>=1.24.0
|
| 12 |
+
|
| 13 |
+
# Web UI
|
| 14 |
+
flask>=3.0.0
|
| 15 |
+
|
| 16 |
+
# Weight loading
|
| 17 |
+
safetensors>=0.4.0
|
| 18 |
+
|
| 19 |
+
# Video support (optional — needed for the Video tab)
|
| 20 |
+
# Installs a bundled ffmpeg so you don't need a system install
|
| 21 |
+
imageio[ffmpeg]>=2.34.0
|
| 22 |
+
|
| 23 |
+
# Weight conversion from Flax (only needed if re-converting from source)
|
| 24 |
+
# jax[cpu]>=0.4.0 # uncomment if you have JAX available
|
| 25 |
+
# flax>=0.8.0 # uncomment if you have JAX available
|
run.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Thera MLX — Arbitrary-scale super-resolution on Apple Silicon.
|
| 4 |
+
|
| 5 |
+
Usage:
|
| 6 |
+
python run.py # Launch web UI (default port 5005)
|
| 7 |
+
python run.py --port 8080 # Custom port
|
| 8 |
+
python run.py run input.png out.png --scale 4 --model pro
|
| 9 |
+
python run.py run input.png out.png --scale 2 --model air --ensemble --tiles 2
|
| 10 |
+
python run.py convert --model air # Download + convert weights
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
import argparse
|
| 14 |
+
import sys
|
| 15 |
+
import os
|
| 16 |
+
|
| 17 |
+
# Make sure this directory is on the path so all modules import cleanly
|
| 18 |
+
sys.path.insert(0, os.path.dirname(__file__))
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def main():
|
| 22 |
+
parser = argparse.ArgumentParser(
|
| 23 |
+
prog="thera-mlx",
|
| 24 |
+
description="Thera MLX — arbitrary-scale super-resolution on Apple Silicon",
|
| 25 |
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
| 26 |
+
epilog=__doc__,
|
| 27 |
+
)
|
| 28 |
+
sub = parser.add_subparsers(dest="command")
|
| 29 |
+
|
| 30 |
+
# ── gui (default) ────────────────────────────────────────────────────────
|
| 31 |
+
gui_p = sub.add_parser("gui", help="Launch web UI (default)")
|
| 32 |
+
gui_p.add_argument("--port", type=int, default=5005, help="Port (default: 5005)")
|
| 33 |
+
gui_p.add_argument("--host", type=str, default="127.0.0.1")
|
| 34 |
+
|
| 35 |
+
# ── run ──────────────────────────────────────────────────────────────────
|
| 36 |
+
run_p = sub.add_parser("run", help="Upscale a single image from the CLI")
|
| 37 |
+
run_p.add_argument("input", help="Input image path")
|
| 38 |
+
run_p.add_argument("output", help="Output image path")
|
| 39 |
+
scale_g = run_p.add_mutually_exclusive_group(required=True)
|
| 40 |
+
scale_g.add_argument("--scale", type=float, help="Scale factor (e.g. 2, 4)")
|
| 41 |
+
scale_g.add_argument("--size", type=int, nargs=2, metavar=("H", "W"),
|
| 42 |
+
help="Exact target size height width")
|
| 43 |
+
run_p.add_argument("--model", choices=["air", "pro"], default="air",
|
| 44 |
+
help="Model variant: air (fast) or pro (quality). Default: air")
|
| 45 |
+
run_p.add_argument("--weights", default=None,
|
| 46 |
+
help="Path to weights file (optional — uses bundled weights by default)")
|
| 47 |
+
run_p.add_argument("--ensemble", action="store_true",
|
| 48 |
+
help="Geometric self-ensemble (4 rotations) — higher quality, slower")
|
| 49 |
+
run_p.add_argument("--tiles", type=int, choices=[2, 3, 4], default=None,
|
| 50 |
+
help="Tile NxN to reduce RAM usage (2, 3, or 4)")
|
| 51 |
+
|
| 52 |
+
# ── convert ──────────────────────────────────────────────────────────────
|
| 53 |
+
conv_p = sub.add_parser("convert", help="Download + convert weights from Flax format")
|
| 54 |
+
conv_p.add_argument("--model", choices=["air", "pro"], default="air")
|
| 55 |
+
conv_p.add_argument("--input", default=None,
|
| 56 |
+
help="Path to model.pkl (downloads from HuggingFace if omitted)")
|
| 57 |
+
conv_p.add_argument("--no-jax", action="store_true",
|
| 58 |
+
help="Load pickle without JAX installed")
|
| 59 |
+
|
| 60 |
+
args = parser.parse_args()
|
| 61 |
+
|
| 62 |
+
# Default: no subcommand → launch GUI
|
| 63 |
+
if args.command is None or args.command == "gui":
|
| 64 |
+
_cmd_gui(args if args.command else argparse.Namespace(host="127.0.0.1", port=5005))
|
| 65 |
+
elif args.command == "run":
|
| 66 |
+
_cmd_run(args)
|
| 67 |
+
elif args.command == "convert":
|
| 68 |
+
_cmd_convert(args)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# ── Command implementations ───────────────────────────────────────────────────
|
| 72 |
+
|
| 73 |
+
def _cmd_gui(args):
|
| 74 |
+
import logging
|
| 75 |
+
logging.getLogger("werkzeug").setLevel(logging.ERROR)
|
| 76 |
+
from ui import app
|
| 77 |
+
print(f"\n Thera MLX → http://{args.host}:{args.port}\n")
|
| 78 |
+
app.run(host=args.host, port=args.port, debug=False, threaded=True)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def _cmd_run(args):
|
| 82 |
+
from upscale import upscale_file
|
| 83 |
+
upscale_file(
|
| 84 |
+
args.input, args.output,
|
| 85 |
+
scale=args.scale, size=args.size,
|
| 86 |
+
model_size=args.model, weights_path=args.weights,
|
| 87 |
+
ensemble=args.ensemble, tiles=args.tiles,
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def _cmd_convert(args):
|
| 92 |
+
from pathlib import Path
|
| 93 |
+
from upscale import WEIGHTS_DIR
|
| 94 |
+
from convert import (
|
| 95 |
+
REPO_IDS, download_model, convert_flax_to_mlx,
|
| 96 |
+
load_pickle_with_jax, load_pickle_without_jax,
|
| 97 |
+
save_safetensors, save_npz,
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
output_path = str(WEIGHTS_DIR / f"weights-{args.model}.safetensors")
|
| 101 |
+
|
| 102 |
+
if args.input is None:
|
| 103 |
+
repo = REPO_IDS[args.model]
|
| 104 |
+
print(f"Downloading model from HuggingFace ({repo})...")
|
| 105 |
+
pkl_path = download_model(args.model)
|
| 106 |
+
else:
|
| 107 |
+
pkl_path = args.input
|
| 108 |
+
|
| 109 |
+
flax_params = (load_pickle_without_jax if args.no_jax else load_pickle_with_jax)(pkl_path)
|
| 110 |
+
|
| 111 |
+
print("Converting weights...")
|
| 112 |
+
mlx_weights = convert_flax_to_mlx(flax_params, size=args.model)
|
| 113 |
+
|
| 114 |
+
total_params = sum(w.size for w in mlx_weights.values())
|
| 115 |
+
print(f" Parameters : {total_params:,}")
|
| 116 |
+
print(f" Weight keys: {len(mlx_weights)}")
|
| 117 |
+
|
| 118 |
+
if output_path.endswith(".safetensors"):
|
| 119 |
+
try:
|
| 120 |
+
save_safetensors(mlx_weights, output_path)
|
| 121 |
+
except ImportError:
|
| 122 |
+
output_path = output_path.replace(".safetensors", ".npz")
|
| 123 |
+
print("safetensors not installed — saving as .npz instead")
|
| 124 |
+
save_npz(mlx_weights, output_path)
|
| 125 |
+
else:
|
| 126 |
+
save_npz(mlx_weights, output_path)
|
| 127 |
+
|
| 128 |
+
print(f"Saved → {output_path}")
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
if __name__ == "__main__":
|
| 132 |
+
main()
|
swin_ir.py
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""MLX implementation of SwinIR (Swin Transformer for Image Restoration)."""
|
| 2 |
+
|
| 3 |
+
import math
|
| 4 |
+
import mlx.core as mx
|
| 5 |
+
import mlx.nn as nn
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def window_partition(x, window_size):
|
| 10 |
+
"""Partition into non-overlapping windows.
|
| 11 |
+
x: (B, H, W, C) → (B * nH * nW, ws, ws, C)
|
| 12 |
+
"""
|
| 13 |
+
B, H, W, C = x.shape
|
| 14 |
+
nH, nW = H // window_size, W // window_size
|
| 15 |
+
x = x.reshape(B, nH, window_size, nW, window_size, C)
|
| 16 |
+
x = x.transpose(0, 1, 3, 2, 4, 5) # B, nH, nW, ws, ws, C
|
| 17 |
+
return x.reshape(-1, window_size, window_size, C)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def window_reverse(windows, window_size, H, W):
|
| 21 |
+
"""Reverse window_partition.
|
| 22 |
+
windows: (B * nH * nW, ws, ws, C) → (B, H, W, C)
|
| 23 |
+
"""
|
| 24 |
+
nH, nW = H // window_size, W // window_size
|
| 25 |
+
B = windows.shape[0] // (nH * nW)
|
| 26 |
+
x = windows.reshape(B, nH, nW, window_size, window_size, -1)
|
| 27 |
+
x = x.transpose(0, 1, 3, 2, 4, 5) # B, nH, ws, nW, ws, C
|
| 28 |
+
return x.reshape(B, H, W, -1)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def make_attn_mask(shift_size, window_size, H, W):
|
| 32 |
+
"""Create attention mask for shifted window self-attention."""
|
| 33 |
+
if shift_size == 0:
|
| 34 |
+
return None
|
| 35 |
+
|
| 36 |
+
mask = np.zeros((1, H, W, 1))
|
| 37 |
+
h_slices = (slice(0, -window_size),
|
| 38 |
+
slice(-window_size, -shift_size),
|
| 39 |
+
slice(-shift_size, None))
|
| 40 |
+
w_slices = (slice(0, -window_size),
|
| 41 |
+
slice(-window_size, -shift_size),
|
| 42 |
+
slice(-shift_size, None))
|
| 43 |
+
|
| 44 |
+
cnt = 0
|
| 45 |
+
for h in h_slices:
|
| 46 |
+
for w in w_slices:
|
| 47 |
+
mask[:, h, w, :] = cnt
|
| 48 |
+
cnt += 1
|
| 49 |
+
|
| 50 |
+
mask_windows = window_partition(mx.array(mask), window_size) # nW, ws, ws, 1
|
| 51 |
+
mask_windows = mask_windows.reshape(-1, window_size * window_size) # nW, ws*ws
|
| 52 |
+
attn_mask = mask_windows[:, :, None] - mask_windows[:, None, :] # nW, ws*ws, ws*ws
|
| 53 |
+
attn_mask = mx.where(attn_mask != 0, -100.0, 0.0)
|
| 54 |
+
return attn_mask
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def make_relative_position_index(window_size):
|
| 58 |
+
"""Compute relative position index for each pair in a window."""
|
| 59 |
+
coords_h = np.arange(window_size)
|
| 60 |
+
coords_w = np.arange(window_size)
|
| 61 |
+
# Note: meshgrid with indexing='ij' matches the Flax implementation
|
| 62 |
+
coords = np.stack(np.meshgrid(coords_w, coords_h, indexing='ij')) # 2, ws, ws
|
| 63 |
+
coords_flat = coords.reshape(2, -1) # 2, ws*ws
|
| 64 |
+
relative = coords_flat[:, :, None] - coords_flat[:, None, :] # 2, ws*ws, ws*ws
|
| 65 |
+
relative = relative.transpose(1, 2, 0) # ws*ws, ws*ws, 2
|
| 66 |
+
relative[:, :, 0] += window_size - 1
|
| 67 |
+
relative[:, :, 1] += window_size - 1
|
| 68 |
+
relative[:, :, 0] *= 2 * window_size - 1
|
| 69 |
+
return relative.sum(-1) # ws*ws, ws*ws
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class MLP(nn.Module):
|
| 73 |
+
def __init__(self, dim, hidden_dim):
|
| 74 |
+
super().__init__()
|
| 75 |
+
self.fc1 = nn.Linear(dim, hidden_dim)
|
| 76 |
+
self.fc2 = nn.Linear(hidden_dim, dim)
|
| 77 |
+
|
| 78 |
+
def __call__(self, x):
|
| 79 |
+
return self.fc2(nn.gelu(self.fc1(x)))
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
class WindowAttention(nn.Module):
|
| 83 |
+
"""Window-based multi-head self-attention with relative position bias."""
|
| 84 |
+
|
| 85 |
+
def __init__(self, dim, window_size, num_heads):
|
| 86 |
+
super().__init__()
|
| 87 |
+
self.dim = dim
|
| 88 |
+
self.window_size = window_size
|
| 89 |
+
self.num_heads = num_heads
|
| 90 |
+
self.head_dim = dim // num_heads
|
| 91 |
+
self.scale = self.head_dim ** -0.5
|
| 92 |
+
|
| 93 |
+
self.qkv = nn.Linear(dim, dim * 3)
|
| 94 |
+
self.proj = nn.Linear(dim, dim)
|
| 95 |
+
|
| 96 |
+
# Relative position bias table: (2*ws-1)^2 entries, num_heads
|
| 97 |
+
num_rel = (2 * window_size - 1) ** 2
|
| 98 |
+
self.relative_position_bias_table = mx.zeros((num_rel, num_heads))
|
| 99 |
+
|
| 100 |
+
# Precompute relative position index (as flat int array for indexing)
|
| 101 |
+
idx = make_relative_position_index(window_size)
|
| 102 |
+
self._rel_pos_index = mx.array(idx.reshape(-1), dtype=mx.int32)
|
| 103 |
+
|
| 104 |
+
def __call__(self, x, mask=None):
|
| 105 |
+
B, N, C = x.shape # N = ws*ws
|
| 106 |
+
|
| 107 |
+
qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, self.head_dim)
|
| 108 |
+
qkv = qkv.transpose(2, 0, 3, 1, 4) # 3, B, heads, N, head_dim
|
| 109 |
+
q, k, v = qkv[0], qkv[1], qkv[2]
|
| 110 |
+
|
| 111 |
+
q = q * self.scale
|
| 112 |
+
attn = q @ k.transpose(0, 1, 3, 2) # B, heads, N, N
|
| 113 |
+
|
| 114 |
+
# Add relative position bias
|
| 115 |
+
rel_pos_bias = self.relative_position_bias_table[
|
| 116 |
+
self._rel_pos_index
|
| 117 |
+
].reshape(N, N, -1)
|
| 118 |
+
rel_pos_bias = rel_pos_bias.transpose(2, 0, 1) # heads, N, N
|
| 119 |
+
attn = attn + rel_pos_bias[None]
|
| 120 |
+
|
| 121 |
+
# Apply mask for shifted windows
|
| 122 |
+
if mask is not None:
|
| 123 |
+
nW = mask.shape[0]
|
| 124 |
+
attn = attn.reshape(B // nW, nW, self.num_heads, N, N)
|
| 125 |
+
attn = attn + mask[None, :, None, :, :]
|
| 126 |
+
attn = attn.reshape(-1, self.num_heads, N, N)
|
| 127 |
+
|
| 128 |
+
attn = mx.softmax(attn, axis=-1)
|
| 129 |
+
x = (attn @ v).transpose(0, 2, 1, 3).reshape(B, N, C)
|
| 130 |
+
return self.proj(x)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
class SwinTransformerBlock(nn.Module):
|
| 134 |
+
"""Swin Transformer Block with optional shifted windows."""
|
| 135 |
+
|
| 136 |
+
def __init__(self, dim, num_heads, window_size, shift_size, mlp_ratio=2.0):
|
| 137 |
+
super().__init__()
|
| 138 |
+
self.dim = dim
|
| 139 |
+
self.window_size = window_size
|
| 140 |
+
self.shift_size = shift_size
|
| 141 |
+
|
| 142 |
+
self.norm1 = nn.LayerNorm(dim, eps=1e-5)
|
| 143 |
+
self.attn = WindowAttention(dim, window_size, num_heads)
|
| 144 |
+
self.norm2 = nn.LayerNorm(dim, eps=1e-5)
|
| 145 |
+
self.mlp = MLP(dim, int(dim * mlp_ratio))
|
| 146 |
+
|
| 147 |
+
def __call__(self, x, H, W):
|
| 148 |
+
B, L, C = x.shape
|
| 149 |
+
shortcut = x
|
| 150 |
+
|
| 151 |
+
x = self.norm1(x)
|
| 152 |
+
x = x.reshape(B, H, W, C)
|
| 153 |
+
|
| 154 |
+
ws = self.window_size
|
| 155 |
+
shift = self.shift_size
|
| 156 |
+
|
| 157 |
+
# Cyclic shift
|
| 158 |
+
if shift > 0:
|
| 159 |
+
x = mx.roll(x, (-shift, -shift), axis=(1, 2))
|
| 160 |
+
|
| 161 |
+
# Partition windows
|
| 162 |
+
x_windows = window_partition(x, ws) # nW*B, ws, ws, C
|
| 163 |
+
x_windows = x_windows.reshape(-1, ws * ws, C)
|
| 164 |
+
|
| 165 |
+
# Window attention with mask
|
| 166 |
+
mask = make_attn_mask(shift, ws, H, W) if shift > 0 else None
|
| 167 |
+
attn_windows = self.attn(x_windows, mask)
|
| 168 |
+
|
| 169 |
+
# Merge windows
|
| 170 |
+
attn_windows = attn_windows.reshape(-1, ws, ws, C)
|
| 171 |
+
x = window_reverse(attn_windows, ws, H, W)
|
| 172 |
+
|
| 173 |
+
# Reverse cyclic shift
|
| 174 |
+
if shift > 0:
|
| 175 |
+
x = mx.roll(x, (shift, shift), axis=(1, 2))
|
| 176 |
+
|
| 177 |
+
x = x.reshape(B, H * W, C)
|
| 178 |
+
|
| 179 |
+
# Residual + FFN
|
| 180 |
+
x = shortcut + x
|
| 181 |
+
x = x + self.mlp(self.norm2(x))
|
| 182 |
+
return x
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
class RSTB(nn.Module):
|
| 186 |
+
"""Residual Swin Transformer Block."""
|
| 187 |
+
|
| 188 |
+
def __init__(self, dim, depth, num_heads, window_size, mlp_ratio=2.0):
|
| 189 |
+
super().__init__()
|
| 190 |
+
self.blocks = []
|
| 191 |
+
for i in range(depth):
|
| 192 |
+
shift = 0 if (i % 2 == 0) else window_size // 2
|
| 193 |
+
self.blocks.append(
|
| 194 |
+
SwinTransformerBlock(dim, num_heads, window_size, shift, mlp_ratio)
|
| 195 |
+
)
|
| 196 |
+
self.conv = nn.Conv2d(dim, dim, kernel_size=3, padding=1)
|
| 197 |
+
|
| 198 |
+
def __call__(self, x, H, W):
|
| 199 |
+
res = x
|
| 200 |
+
for block in self.blocks:
|
| 201 |
+
x = block(x, H, W)
|
| 202 |
+
|
| 203 |
+
# PatchUnEmbed → Conv → PatchEmbed
|
| 204 |
+
B, L, C = x.shape
|
| 205 |
+
x = x.reshape(B, H, W, C)
|
| 206 |
+
x = self.conv(x)
|
| 207 |
+
x = x.reshape(B, H * W, C)
|
| 208 |
+
return x + res
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
class SwinIRTail(nn.Module):
|
| 212 |
+
"""SwinIR tail module for Thera-pro.
|
| 213 |
+
|
| 214 |
+
Takes 64-channel RDN output and refines it, outputting 64 channels.
|
| 215 |
+
"""
|
| 216 |
+
|
| 217 |
+
def __init__(self, in_channels=64, embed_dim=180, depths=(7, 6),
|
| 218 |
+
num_heads=(6, 6), window_size=8, mlp_ratio=2.0, num_feat=64):
|
| 219 |
+
super().__init__()
|
| 220 |
+
self.window_size = window_size
|
| 221 |
+
self.embed_dim = embed_dim
|
| 222 |
+
|
| 223 |
+
# conv_first
|
| 224 |
+
self.conv_first = nn.Conv2d(in_channels, embed_dim, kernel_size=3, padding=1)
|
| 225 |
+
|
| 226 |
+
# PatchEmbed norm
|
| 227 |
+
self.patch_embed_norm = nn.LayerNorm(embed_dim, eps=1e-5)
|
| 228 |
+
|
| 229 |
+
# RSTB layers
|
| 230 |
+
self.layers = [
|
| 231 |
+
RSTB(embed_dim, depth, nh, window_size, mlp_ratio)
|
| 232 |
+
for depth, nh in zip(depths, num_heads)
|
| 233 |
+
]
|
| 234 |
+
|
| 235 |
+
# Final norm
|
| 236 |
+
self.norm = nn.LayerNorm(embed_dim, eps=1e-5)
|
| 237 |
+
|
| 238 |
+
# conv_after_body
|
| 239 |
+
self.conv_after_body = nn.Conv2d(embed_dim, embed_dim, kernel_size=3, padding=1)
|
| 240 |
+
|
| 241 |
+
# conv_before_upsample → output 64 channels
|
| 242 |
+
self.conv_last = nn.Conv2d(embed_dim, num_feat, kernel_size=3, padding=1)
|
| 243 |
+
|
| 244 |
+
def __call__(self, x):
|
| 245 |
+
_, h_orig, w_orig, _ = x.shape
|
| 246 |
+
|
| 247 |
+
# Pad to multiple of window_size
|
| 248 |
+
ws = self.window_size
|
| 249 |
+
pad_h = (ws - h_orig % ws) % ws
|
| 250 |
+
pad_w = (ws - w_orig % ws) % ws
|
| 251 |
+
if pad_h > 0 or pad_w > 0:
|
| 252 |
+
# Reflect padding
|
| 253 |
+
x = mx.pad(x, [(0, 0), (0, pad_h), (0, pad_w), (0, 0)], mode='edge')
|
| 254 |
+
|
| 255 |
+
_, H, W, _ = x.shape
|
| 256 |
+
|
| 257 |
+
# conv_first
|
| 258 |
+
x = self.conv_first(x)
|
| 259 |
+
res = x
|
| 260 |
+
|
| 261 |
+
# PatchEmbed (reshape + norm)
|
| 262 |
+
B = x.shape[0]
|
| 263 |
+
x = x.reshape(B, H * W, self.embed_dim)
|
| 264 |
+
x = self.patch_embed_norm(x)
|
| 265 |
+
|
| 266 |
+
# RSTB layers
|
| 267 |
+
for layer in self.layers:
|
| 268 |
+
x = layer(x, H, W)
|
| 269 |
+
|
| 270 |
+
# Final norm
|
| 271 |
+
x = self.norm(x)
|
| 272 |
+
|
| 273 |
+
# PatchUnEmbed
|
| 274 |
+
x = x.reshape(B, H, W, self.embed_dim)
|
| 275 |
+
|
| 276 |
+
# conv_after_body + residual
|
| 277 |
+
x = self.conv_after_body(x) + res
|
| 278 |
+
|
| 279 |
+
# conv_before_upsample
|
| 280 |
+
x = nn.leaky_relu(self.conv_last(x))
|
| 281 |
+
|
| 282 |
+
# Remove padding
|
| 283 |
+
if pad_h > 0 or pad_w > 0:
|
| 284 |
+
x = x[:, :h_orig, :w_orig, :]
|
| 285 |
+
|
| 286 |
+
return x
|
ui.py
ADDED
|
@@ -0,0 +1,1435 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Web UI for Thera MLX super-resolution.
|
| 4 |
+
|
| 5 |
+
Supports single image, batch, and video upscaling.
|
| 6 |
+
|
| 7 |
+
Usage:
|
| 8 |
+
python3 ui.py
|
| 9 |
+
python3 ui.py --port 8080
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import argparse
|
| 13 |
+
import glob
|
| 14 |
+
import io
|
| 15 |
+
import json
|
| 16 |
+
import os
|
| 17 |
+
import shutil
|
| 18 |
+
import subprocess
|
| 19 |
+
import tempfile
|
| 20 |
+
import threading
|
| 21 |
+
import time
|
| 22 |
+
import uuid
|
| 23 |
+
import zipfile
|
| 24 |
+
|
| 25 |
+
import mlx.core as mx
|
| 26 |
+
import numpy as np
|
| 27 |
+
from flask import Flask, request, jsonify, send_file, Response
|
| 28 |
+
from PIL import Image
|
| 29 |
+
|
| 30 |
+
from model import Thera
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def _find_ffmpeg():
|
| 34 |
+
"""Find ffmpeg binary — system PATH first, then imageio_ffmpeg fallback."""
|
| 35 |
+
path = shutil.which("ffmpeg")
|
| 36 |
+
if path:
|
| 37 |
+
return path
|
| 38 |
+
try:
|
| 39 |
+
import imageio_ffmpeg
|
| 40 |
+
return imageio_ffmpeg.get_ffmpeg_exe()
|
| 41 |
+
except ImportError:
|
| 42 |
+
return None
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def _find_ffprobe():
|
| 46 |
+
"""Find ffprobe binary — system PATH first, then imageio_ffmpeg fallback."""
|
| 47 |
+
path = shutil.which("ffprobe")
|
| 48 |
+
if path:
|
| 49 |
+
return path
|
| 50 |
+
try:
|
| 51 |
+
import imageio_ffmpeg
|
| 52 |
+
ff = imageio_ffmpeg.get_ffmpeg_exe()
|
| 53 |
+
# ffprobe is in the same directory
|
| 54 |
+
probe = os.path.join(os.path.dirname(ff),
|
| 55 |
+
ff.replace("ffmpeg", "ffprobe").split("/")[-1])
|
| 56 |
+
if os.path.exists(probe):
|
| 57 |
+
return probe
|
| 58 |
+
# some builds bundle it as ffprobe next to ffmpeg
|
| 59 |
+
probe2 = ff.replace("ffmpeg", "ffprobe")
|
| 60 |
+
if os.path.exists(probe2):
|
| 61 |
+
return probe2
|
| 62 |
+
except ImportError:
|
| 63 |
+
pass
|
| 64 |
+
return None
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
FFMPEG = _find_ffmpeg()
|
| 68 |
+
FFPROBE = _find_ffprobe()
|
| 69 |
+
|
| 70 |
+
# ---------------------------------------------------------------------------
|
| 71 |
+
# App setup
|
| 72 |
+
# ---------------------------------------------------------------------------
|
| 73 |
+
|
| 74 |
+
app = Flask(__name__)
|
| 75 |
+
UPLOAD_DIR = tempfile.mkdtemp(prefix="thera_ui_")
|
| 76 |
+
|
| 77 |
+
# Job tracking for async video processing
|
| 78 |
+
_jobs = {}
|
| 79 |
+
_jobs_lock = threading.Lock()
|
| 80 |
+
|
| 81 |
+
# ---------------------------------------------------------------------------
|
| 82 |
+
# Model cache
|
| 83 |
+
# ---------------------------------------------------------------------------
|
| 84 |
+
|
| 85 |
+
_model_cache = {}
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def get_model(size):
|
| 89 |
+
if size not in _model_cache:
|
| 90 |
+
from upscale import load_weights
|
| 91 |
+
model = Thera(size=size)
|
| 92 |
+
weights_dir = os.path.join(os.path.dirname(__file__), "weights")
|
| 93 |
+
weights_path = os.path.join(weights_dir, f"weights-{size}.safetensors")
|
| 94 |
+
if not os.path.exists(weights_path):
|
| 95 |
+
raise FileNotFoundError(
|
| 96 |
+
f"Weights not found: {weights_path}\n"
|
| 97 |
+
f"Run: python3 convert.py --model {size}")
|
| 98 |
+
model = load_weights(model, weights_path)
|
| 99 |
+
mx.eval(model.parameters())
|
| 100 |
+
_model_cache[size] = model
|
| 101 |
+
return _model_cache[size]
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
# ---------------------------------------------------------------------------
|
| 105 |
+
# Core upscale
|
| 106 |
+
# ---------------------------------------------------------------------------
|
| 107 |
+
|
| 108 |
+
def upscale_image(img_np, scale, model_size, ensemble, tiles=None):
|
| 109 |
+
model = get_model(model_size)
|
| 110 |
+
h, w = img_np.shape[:2]
|
| 111 |
+
th, tw = round(h * scale), round(w * scale)
|
| 112 |
+
source_f = img_np.astype(np.float32) / 255.0
|
| 113 |
+
|
| 114 |
+
if tiles and tiles > 1:
|
| 115 |
+
from upscale import upscale_tiled
|
| 116 |
+
return upscale_tiled(model, source_f, th, tw, tiles, ensemble=ensemble)
|
| 117 |
+
else:
|
| 118 |
+
source = mx.array(source_f)
|
| 119 |
+
result = model.upscale(source, th, tw, ensemble=ensemble)
|
| 120 |
+
mx.eval(result)
|
| 121 |
+
return np.array(result)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# ---------------------------------------------------------------------------
|
| 125 |
+
# API routes
|
| 126 |
+
# ---------------------------------------------------------------------------
|
| 127 |
+
|
| 128 |
+
@app.route("/api/upscale", methods=["POST"])
|
| 129 |
+
def api_upscale():
|
| 130 |
+
f = request.files.get("image")
|
| 131 |
+
if not f:
|
| 132 |
+
return jsonify(error="No image uploaded"), 400
|
| 133 |
+
|
| 134 |
+
scale = float(request.form.get("scale", 2.0))
|
| 135 |
+
model_size = request.form.get("model", "air")
|
| 136 |
+
ensemble = request.form.get("ensemble", "false") == "true"
|
| 137 |
+
tiles_str = request.form.get("tiles", "1")
|
| 138 |
+
tiles = int(tiles_str) if tiles_str.isdigit() and int(tiles_str) > 1 else None
|
| 139 |
+
|
| 140 |
+
img = np.array(Image.open(f.stream).convert("RGB"))
|
| 141 |
+
t0 = time.perf_counter()
|
| 142 |
+
result = upscale_image(img, scale, model_size, ensemble, tiles=tiles)
|
| 143 |
+
elapsed = time.perf_counter() - t0
|
| 144 |
+
|
| 145 |
+
buf = io.BytesIO()
|
| 146 |
+
Image.fromarray(result).save(buf, format="PNG")
|
| 147 |
+
buf.seek(0)
|
| 148 |
+
|
| 149 |
+
h, w = img.shape[:2]
|
| 150 |
+
th, tw = result.shape[:2]
|
| 151 |
+
|
| 152 |
+
resp = send_file(buf, mimetype="image/png", download_name="upscaled.png")
|
| 153 |
+
resp.headers["X-Info"] = json.dumps({
|
| 154 |
+
"src": f"{w}x{h}", "dst": f"{tw}x{th}",
|
| 155 |
+
"scale": scale, "model": model_size, "time": round(elapsed, 1)
|
| 156 |
+
})
|
| 157 |
+
return resp
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
@app.route("/api/batch", methods=["POST"])
|
| 161 |
+
def api_batch():
|
| 162 |
+
files = request.files.getlist("images")
|
| 163 |
+
if not files:
|
| 164 |
+
return jsonify(error="No images uploaded"), 400
|
| 165 |
+
|
| 166 |
+
scale = float(request.form.get("scale", 2.0))
|
| 167 |
+
model_size = request.form.get("model", "air")
|
| 168 |
+
ensemble = request.form.get("ensemble", "false") == "true"
|
| 169 |
+
|
| 170 |
+
buf = io.BytesIO()
|
| 171 |
+
t0 = time.perf_counter()
|
| 172 |
+
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 173 |
+
for f in files:
|
| 174 |
+
img = np.array(Image.open(f.stream).convert("RGB"))
|
| 175 |
+
result = upscale_image(img, scale, model_size, ensemble)
|
| 176 |
+
img_buf = io.BytesIO()
|
| 177 |
+
Image.fromarray(result).save(img_buf, format="PNG")
|
| 178 |
+
name = os.path.splitext(f.filename)[0] + f"_thera_{scale}x.png"
|
| 179 |
+
zf.writestr(name, img_buf.getvalue())
|
| 180 |
+
|
| 181 |
+
elapsed = time.perf_counter() - t0
|
| 182 |
+
buf.seek(0)
|
| 183 |
+
|
| 184 |
+
resp = send_file(buf, mimetype="application/zip",
|
| 185 |
+
download_name="thera_batch.zip")
|
| 186 |
+
resp.headers["X-Info"] = json.dumps({
|
| 187 |
+
"count": len(files), "scale": scale,
|
| 188 |
+
"model": model_size, "time": round(elapsed, 1)
|
| 189 |
+
})
|
| 190 |
+
return resp
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
# ---------------------------------------------------------------------------
|
| 194 |
+
# Video processing (async with progress)
|
| 195 |
+
# ---------------------------------------------------------------------------
|
| 196 |
+
|
| 197 |
+
def get_video_info(path):
|
| 198 |
+
"""Get video metadata using ffprobe if available, else parse ffmpeg stderr."""
|
| 199 |
+
if FFPROBE:
|
| 200 |
+
cmd = [
|
| 201 |
+
FFPROBE, "-v", "quiet", "-print_format", "json",
|
| 202 |
+
"-show_streams", "-show_format", path
|
| 203 |
+
]
|
| 204 |
+
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 205 |
+
info = json.loads(result.stdout)
|
| 206 |
+
stream = next(s for s in info["streams"] if s["codec_type"] == "video")
|
| 207 |
+
fps_parts = stream["r_frame_rate"].split("/")
|
| 208 |
+
fps = float(fps_parts[0]) / float(fps_parts[1])
|
| 209 |
+
w, h = int(stream["width"]), int(stream["height"])
|
| 210 |
+
duration = float(info["format"].get("duration", 0))
|
| 211 |
+
return fps, w, h, duration
|
| 212 |
+
else:
|
| 213 |
+
# Fallback: parse ffmpeg -i stderr
|
| 214 |
+
import re
|
| 215 |
+
result = subprocess.run(
|
| 216 |
+
[FFMPEG, "-i", path], capture_output=True, text=True)
|
| 217 |
+
stderr = result.stderr
|
| 218 |
+
# Parse "Duration: 00:00:10.00"
|
| 219 |
+
dur_m = re.search(r"Duration:\s*(\d+):(\d+):(\d+\.\d+)", stderr)
|
| 220 |
+
duration = 0.0
|
| 221 |
+
if dur_m:
|
| 222 |
+
duration = int(dur_m[1]) * 3600 + int(dur_m[2]) * 60 + float(dur_m[3])
|
| 223 |
+
# Parse "1920x1080" and fps
|
| 224 |
+
vid_m = re.search(r"(\d{2,5})x(\d{2,5})", stderr)
|
| 225 |
+
w, h = (int(vid_m[1]), int(vid_m[2])) if vid_m else (0, 0)
|
| 226 |
+
fps_m = re.search(r"(\d+(?:\.\d+)?)\s*fps", stderr)
|
| 227 |
+
fps = float(fps_m[1]) if fps_m else 30.0
|
| 228 |
+
return fps, w, h, duration
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
def has_audio(path):
|
| 232 |
+
"""Check if video has an audio stream."""
|
| 233 |
+
if FFPROBE:
|
| 234 |
+
probe = subprocess.run(
|
| 235 |
+
[FFPROBE, "-v", "quiet", "-select_streams", "a",
|
| 236 |
+
"-show_entries", "stream=codec_type", path],
|
| 237 |
+
capture_output=True, text=True)
|
| 238 |
+
return "audio" in probe.stdout
|
| 239 |
+
else:
|
| 240 |
+
result = subprocess.run(
|
| 241 |
+
[FFMPEG, "-i", path], capture_output=True, text=True)
|
| 242 |
+
return "Audio:" in result.stderr
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
def video_worker(job_id, video_path, scale, model_size):
|
| 246 |
+
job = _jobs[job_id]
|
| 247 |
+
try:
|
| 248 |
+
job["status"] = "analyzing"
|
| 249 |
+
fps, src_w, src_h, duration = get_video_info(video_path)
|
| 250 |
+
tw, th = round(src_w * scale), round(src_h * scale)
|
| 251 |
+
job["src"] = f"{src_w}x{src_h}"
|
| 252 |
+
job["dst"] = f"{tw}x{th}"
|
| 253 |
+
|
| 254 |
+
tmpdir = tempfile.mkdtemp(prefix="thera_vid_")
|
| 255 |
+
frames_dir = os.path.join(tmpdir, "frames")
|
| 256 |
+
upscaled_dir = os.path.join(tmpdir, "upscaled")
|
| 257 |
+
os.makedirs(frames_dir)
|
| 258 |
+
os.makedirs(upscaled_dir)
|
| 259 |
+
|
| 260 |
+
# Extract frames
|
| 261 |
+
job["status"] = "extracting"
|
| 262 |
+
subprocess.run([
|
| 263 |
+
FFMPEG, "-i", video_path, "-vsync", "0",
|
| 264 |
+
os.path.join(frames_dir, "frame_%06d.png")
|
| 265 |
+
], capture_output=True, check=True)
|
| 266 |
+
|
| 267 |
+
frame_files = sorted(glob.glob(os.path.join(frames_dir, "frame_*.png")))
|
| 268 |
+
total = len(frame_files)
|
| 269 |
+
job["total_frames"] = total
|
| 270 |
+
|
| 271 |
+
# Upscale frames
|
| 272 |
+
job["status"] = "upscaling"
|
| 273 |
+
model = get_model(model_size)
|
| 274 |
+
t0 = time.perf_counter()
|
| 275 |
+
|
| 276 |
+
for i, frame_path in enumerate(frame_files):
|
| 277 |
+
img = np.array(Image.open(frame_path).convert("RGB"))
|
| 278 |
+
source = mx.array(img.astype(np.float32) / 255.0)
|
| 279 |
+
fh, fw = img.shape[:2]
|
| 280 |
+
result = model.upscale(source, round(fh * scale), round(fw * scale))
|
| 281 |
+
mx.eval(result)
|
| 282 |
+
out_path = os.path.join(upscaled_dir, os.path.basename(frame_path))
|
| 283 |
+
Image.fromarray(np.array(result)).save(out_path)
|
| 284 |
+
|
| 285 |
+
elapsed = time.perf_counter() - t0
|
| 286 |
+
eta = (elapsed / (i + 1)) * (total - i - 1) if i > 0 else 0
|
| 287 |
+
job["current_frame"] = i + 1
|
| 288 |
+
job["eta"] = round(eta)
|
| 289 |
+
job["fps"] = round((i + 1) / elapsed, 1) if elapsed > 0 else 0
|
| 290 |
+
|
| 291 |
+
# Encode
|
| 292 |
+
job["status"] = "encoding"
|
| 293 |
+
output_path = os.path.join(tmpdir, "upscaled.mp4")
|
| 294 |
+
|
| 295 |
+
ffmpeg_cmd = [
|
| 296 |
+
FFMPEG, "-y",
|
| 297 |
+
"-framerate", str(fps),
|
| 298 |
+
"-i", os.path.join(upscaled_dir, "frame_%06d.png"),
|
| 299 |
+
]
|
| 300 |
+
|
| 301 |
+
# Check for audio
|
| 302 |
+
audio = has_audio(video_path)
|
| 303 |
+
|
| 304 |
+
if audio:
|
| 305 |
+
ffmpeg_cmd += ["-i", video_path, "-map", "0:v", "-map", "1:a",
|
| 306 |
+
"-shortest"]
|
| 307 |
+
|
| 308 |
+
ffmpeg_cmd += [
|
| 309 |
+
"-c:v", "libx264", "-preset", "medium", "-crf", "18",
|
| 310 |
+
"-pix_fmt", "yuv420p",
|
| 311 |
+
]
|
| 312 |
+
if audio:
|
| 313 |
+
ffmpeg_cmd += ["-c:a", "aac", "-b:a", "192k"]
|
| 314 |
+
|
| 315 |
+
ffmpeg_cmd.append(output_path)
|
| 316 |
+
subprocess.run(ffmpeg_cmd, capture_output=True, check=True)
|
| 317 |
+
|
| 318 |
+
total_time = time.perf_counter() - t0
|
| 319 |
+
job["status"] = "done"
|
| 320 |
+
job["output_path"] = output_path
|
| 321 |
+
job["time"] = round(total_time, 1)
|
| 322 |
+
job["tmpdir"] = tmpdir
|
| 323 |
+
|
| 324 |
+
except Exception as e:
|
| 325 |
+
job["status"] = "error"
|
| 326 |
+
job["error"] = str(e)
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
@app.route("/api/video/start", methods=["POST"])
|
| 330 |
+
def api_video_start():
|
| 331 |
+
f = request.files.get("video")
|
| 332 |
+
if not f:
|
| 333 |
+
return jsonify(error="No video uploaded"), 400
|
| 334 |
+
|
| 335 |
+
if not FFMPEG:
|
| 336 |
+
return jsonify(error="ffmpeg not found. Install with: pip3 install imageio[ffmpeg]"), 400
|
| 337 |
+
|
| 338 |
+
scale = float(request.form.get("scale", 2.0))
|
| 339 |
+
model_size = request.form.get("model", "air")
|
| 340 |
+
|
| 341 |
+
job_id = str(uuid.uuid4())[:8]
|
| 342 |
+
video_path = os.path.join(UPLOAD_DIR, f"{job_id}_input.mp4")
|
| 343 |
+
f.save(video_path)
|
| 344 |
+
|
| 345 |
+
with _jobs_lock:
|
| 346 |
+
_jobs[job_id] = {
|
| 347 |
+
"status": "queued", "current_frame": 0, "total_frames": 0,
|
| 348 |
+
"eta": 0, "fps": 0, "scale": scale, "model": model_size,
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
t = threading.Thread(target=video_worker,
|
| 352 |
+
args=(job_id, video_path, scale, model_size),
|
| 353 |
+
daemon=True)
|
| 354 |
+
t.start()
|
| 355 |
+
|
| 356 |
+
return jsonify(job_id=job_id)
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
@app.route("/api/video/progress/<job_id>")
|
| 360 |
+
def api_video_progress(job_id):
|
| 361 |
+
job = _jobs.get(job_id)
|
| 362 |
+
if not job:
|
| 363 |
+
return jsonify(error="Unknown job"), 404
|
| 364 |
+
safe = {k: v for k, v in job.items()
|
| 365 |
+
if k not in ("output_path", "tmpdir")}
|
| 366 |
+
return jsonify(safe)
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
@app.route("/api/video/download/<job_id>")
|
| 370 |
+
def api_video_download(job_id):
|
| 371 |
+
job = _jobs.get(job_id)
|
| 372 |
+
if not job or job["status"] != "done":
|
| 373 |
+
return jsonify(error="Not ready"), 400
|
| 374 |
+
return send_file(job["output_path"], mimetype="video/mp4",
|
| 375 |
+
download_name="thera_upscaled.mp4")
|
| 376 |
+
|
| 377 |
+
|
| 378 |
+
# ---------------------------------------------------------------------------
|
| 379 |
+
# Frontend
|
| 380 |
+
# ---------------------------------------------------------------------------
|
| 381 |
+
|
| 382 |
+
@app.route("/")
|
| 383 |
+
def index():
|
| 384 |
+
return HTML_PAGE
|
| 385 |
+
|
| 386 |
+
|
| 387 |
+
HTML_PAGE = r"""<!DOCTYPE html>
|
| 388 |
+
<html lang="en">
|
| 389 |
+
<head>
|
| 390 |
+
<meta charset="UTF-8">
|
| 391 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 392 |
+
<title>Thera MLX</title>
|
| 393 |
+
<style>
|
| 394 |
+
:root {
|
| 395 |
+
--bg: #0f0f0f;
|
| 396 |
+
--surface: #1a1a1a;
|
| 397 |
+
--surface2: #242424;
|
| 398 |
+
--border: #333;
|
| 399 |
+
--text: #e8e8e8;
|
| 400 |
+
--text2: #999;
|
| 401 |
+
--accent: #6c63ff;
|
| 402 |
+
--accent-hover: #7c74ff;
|
| 403 |
+
--green: #4caf50;
|
| 404 |
+
--radius: 12px;
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
| 408 |
+
|
| 409 |
+
body {
|
| 410 |
+
font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', system-ui, sans-serif;
|
| 411 |
+
background: var(--bg);
|
| 412 |
+
color: var(--text);
|
| 413 |
+
min-height: 100vh;
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
.container { max-width: 960px; margin: 0 auto; padding: 24px 20px; }
|
| 417 |
+
|
| 418 |
+
header {
|
| 419 |
+
text-align: center;
|
| 420 |
+
padding: 32px 0 24px;
|
| 421 |
+
}
|
| 422 |
+
header h1 {
|
| 423 |
+
font-size: 28px;
|
| 424 |
+
font-weight: 700;
|
| 425 |
+
letter-spacing: -0.5px;
|
| 426 |
+
}
|
| 427 |
+
header h1 span { color: var(--accent); }
|
| 428 |
+
header p { color: var(--text2); margin-top: 4px; font-size: 14px; }
|
| 429 |
+
|
| 430 |
+
/* Tabs */
|
| 431 |
+
.tabs {
|
| 432 |
+
display: flex;
|
| 433 |
+
gap: 4px;
|
| 434 |
+
background: var(--surface);
|
| 435 |
+
border-radius: var(--radius);
|
| 436 |
+
padding: 4px;
|
| 437 |
+
margin-bottom: 20px;
|
| 438 |
+
}
|
| 439 |
+
.tab {
|
| 440 |
+
flex: 1;
|
| 441 |
+
padding: 10px 16px;
|
| 442 |
+
border: none;
|
| 443 |
+
background: none;
|
| 444 |
+
color: var(--text2);
|
| 445 |
+
font-size: 14px;
|
| 446 |
+
font-weight: 500;
|
| 447 |
+
cursor: pointer;
|
| 448 |
+
border-radius: 8px;
|
| 449 |
+
transition: all 0.2s;
|
| 450 |
+
}
|
| 451 |
+
.tab:hover { color: var(--text); }
|
| 452 |
+
.tab.active { background: var(--accent); color: #fff; }
|
| 453 |
+
|
| 454 |
+
.tab-content { display: none; }
|
| 455 |
+
.tab-content.active { display: block; }
|
| 456 |
+
|
| 457 |
+
/* Controls */
|
| 458 |
+
.controls {
|
| 459 |
+
background: var(--surface);
|
| 460 |
+
border-radius: var(--radius);
|
| 461 |
+
padding: 20px;
|
| 462 |
+
margin-bottom: 16px;
|
| 463 |
+
}
|
| 464 |
+
.control-row {
|
| 465 |
+
display: flex;
|
| 466 |
+
gap: 16px;
|
| 467 |
+
align-items: center;
|
| 468 |
+
flex-wrap: wrap;
|
| 469 |
+
}
|
| 470 |
+
.control-group {
|
| 471 |
+
display: flex;
|
| 472 |
+
flex-direction: column;
|
| 473 |
+
gap: 6px;
|
| 474 |
+
}
|
| 475 |
+
.control-group label {
|
| 476 |
+
font-size: 12px;
|
| 477 |
+
font-weight: 600;
|
| 478 |
+
color: var(--text2);
|
| 479 |
+
text-transform: uppercase;
|
| 480 |
+
letter-spacing: 0.5px;
|
| 481 |
+
}
|
| 482 |
+
.control-group select,
|
| 483 |
+
.control-group input[type="range"] {
|
| 484 |
+
background: var(--surface2);
|
| 485 |
+
border: 1px solid var(--border);
|
| 486 |
+
color: var(--text);
|
| 487 |
+
border-radius: 8px;
|
| 488 |
+
padding: 8px 12px;
|
| 489 |
+
font-size: 14px;
|
| 490 |
+
}
|
| 491 |
+
.control-group select { min-width: 100px; cursor: pointer; }
|
| 492 |
+
.control-group input[type="range"] { width: 160px; accent-color: var(--accent); }
|
| 493 |
+
|
| 494 |
+
.scale-display {
|
| 495 |
+
font-size: 20px;
|
| 496 |
+
font-weight: 700;
|
| 497 |
+
color: var(--accent);
|
| 498 |
+
min-width: 40px;
|
| 499 |
+
text-align: center;
|
| 500 |
+
}
|
| 501 |
+
|
| 502 |
+
.checkbox-label {
|
| 503 |
+
display: flex;
|
| 504 |
+
align-items: center;
|
| 505 |
+
gap: 8px;
|
| 506 |
+
font-size: 14px;
|
| 507 |
+
cursor: pointer;
|
| 508 |
+
color: var(--text2);
|
| 509 |
+
}
|
| 510 |
+
.checkbox-label input { accent-color: var(--accent); }
|
| 511 |
+
|
| 512 |
+
/* Drop zone */
|
| 513 |
+
.dropzone {
|
| 514 |
+
border: 2px dashed var(--border);
|
| 515 |
+
border-radius: var(--radius);
|
| 516 |
+
padding: 48px 24px;
|
| 517 |
+
text-align: center;
|
| 518 |
+
cursor: pointer;
|
| 519 |
+
transition: all 0.2s;
|
| 520 |
+
background: var(--surface);
|
| 521 |
+
margin-bottom: 16px;
|
| 522 |
+
}
|
| 523 |
+
.dropzone:hover, .dropzone.dragover {
|
| 524 |
+
border-color: var(--accent);
|
| 525 |
+
background: rgba(108, 99, 255, 0.05);
|
| 526 |
+
}
|
| 527 |
+
.dropzone.has-file {
|
| 528 |
+
padding: 16px;
|
| 529 |
+
border-style: solid;
|
| 530 |
+
border-color: var(--green);
|
| 531 |
+
}
|
| 532 |
+
.dropzone-icon { font-size: 36px; margin-bottom: 8px; }
|
| 533 |
+
.dropzone-text { color: var(--text2); font-size: 14px; }
|
| 534 |
+
.dropzone-text strong { color: var(--text); }
|
| 535 |
+
|
| 536 |
+
/* Preview panels */
|
| 537 |
+
.preview-area {
|
| 538 |
+
display: grid;
|
| 539 |
+
grid-template-columns: 1fr 1fr;
|
| 540 |
+
gap: 16px;
|
| 541 |
+
margin-bottom: 16px;
|
| 542 |
+
}
|
| 543 |
+
.preview-panel {
|
| 544 |
+
background: var(--surface);
|
| 545 |
+
border-radius: var(--radius);
|
| 546 |
+
overflow: hidden;
|
| 547 |
+
min-height: 200px;
|
| 548 |
+
position: relative;
|
| 549 |
+
}
|
| 550 |
+
.preview-panel .panel-label {
|
| 551 |
+
position: absolute;
|
| 552 |
+
top: 8px;
|
| 553 |
+
left: 12px;
|
| 554 |
+
font-size: 11px;
|
| 555 |
+
font-weight: 600;
|
| 556 |
+
text-transform: uppercase;
|
| 557 |
+
color: var(--text2);
|
| 558 |
+
background: rgba(0,0,0,0.6);
|
| 559 |
+
padding: 3px 8px;
|
| 560 |
+
border-radius: 4px;
|
| 561 |
+
z-index: 1;
|
| 562 |
+
}
|
| 563 |
+
.preview-panel img, .preview-panel video {
|
| 564 |
+
width: 100%;
|
| 565 |
+
height: 100%;
|
| 566 |
+
object-fit: contain;
|
| 567 |
+
display: block;
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
/* Compare slider */
|
| 571 |
+
.compare-container {
|
| 572 |
+
display: none;
|
| 573 |
+
position: relative;
|
| 574 |
+
background: var(--surface);
|
| 575 |
+
border-radius: var(--radius);
|
| 576 |
+
overflow: hidden;
|
| 577 |
+
margin-bottom: 16px;
|
| 578 |
+
cursor: col-resize;
|
| 579 |
+
user-select: none;
|
| 580 |
+
}
|
| 581 |
+
.compare-container.active { display: block; }
|
| 582 |
+
.compare-container img {
|
| 583 |
+
width: 100%;
|
| 584 |
+
display: block;
|
| 585 |
+
}
|
| 586 |
+
.compare-overlay {
|
| 587 |
+
position: absolute;
|
| 588 |
+
top: 0;
|
| 589 |
+
left: 0;
|
| 590 |
+
height: 100%;
|
| 591 |
+
overflow: hidden;
|
| 592 |
+
}
|
| 593 |
+
.compare-overlay img {
|
| 594 |
+
position: absolute;
|
| 595 |
+
top: 0;
|
| 596 |
+
left: 0;
|
| 597 |
+
height: 100%;
|
| 598 |
+
width: auto;
|
| 599 |
+
max-width: none;
|
| 600 |
+
}
|
| 601 |
+
.compare-line {
|
| 602 |
+
position: absolute;
|
| 603 |
+
top: 0;
|
| 604 |
+
width: 2px;
|
| 605 |
+
height: 100%;
|
| 606 |
+
background: #fff;
|
| 607 |
+
pointer-events: none;
|
| 608 |
+
z-index: 2;
|
| 609 |
+
box-shadow: 0 0 6px rgba(0,0,0,0.5);
|
| 610 |
+
}
|
| 611 |
+
.compare-line::after {
|
| 612 |
+
content: '';
|
| 613 |
+
position: absolute;
|
| 614 |
+
top: 50%;
|
| 615 |
+
left: 50%;
|
| 616 |
+
transform: translate(-50%, -50%);
|
| 617 |
+
width: 32px;
|
| 618 |
+
height: 32px;
|
| 619 |
+
background: #fff;
|
| 620 |
+
border-radius: 50%;
|
| 621 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.3);
|
| 622 |
+
}
|
| 623 |
+
.compare-line::before {
|
| 624 |
+
content: '';
|
| 625 |
+
position: absolute;
|
| 626 |
+
top: 50%;
|
| 627 |
+
left: 50%;
|
| 628 |
+
transform: translate(-50%, -50%);
|
| 629 |
+
width: 16px;
|
| 630 |
+
height: 16px;
|
| 631 |
+
background: var(--accent);
|
| 632 |
+
border-radius: 50%;
|
| 633 |
+
z-index: 1;
|
| 634 |
+
}
|
| 635 |
+
.compare-label {
|
| 636 |
+
position: absolute;
|
| 637 |
+
top: 8px;
|
| 638 |
+
font-size: 11px;
|
| 639 |
+
font-weight: 600;
|
| 640 |
+
text-transform: uppercase;
|
| 641 |
+
color: var(--text2);
|
| 642 |
+
background: rgba(0,0,0,0.6);
|
| 643 |
+
padding: 3px 8px;
|
| 644 |
+
border-radius: 4px;
|
| 645 |
+
z-index: 3;
|
| 646 |
+
pointer-events: none;
|
| 647 |
+
}
|
| 648 |
+
.compare-label-before { left: 12px; }
|
| 649 |
+
.compare-label-after { right: 12px; }
|
| 650 |
+
|
| 651 |
+
/* Buttons */
|
| 652 |
+
.btn {
|
| 653 |
+
padding: 12px 32px;
|
| 654 |
+
border: none;
|
| 655 |
+
border-radius: 8px;
|
| 656 |
+
font-size: 15px;
|
| 657 |
+
font-weight: 600;
|
| 658 |
+
cursor: pointer;
|
| 659 |
+
transition: all 0.2s;
|
| 660 |
+
display: inline-flex;
|
| 661 |
+
align-items: center;
|
| 662 |
+
gap: 8px;
|
| 663 |
+
}
|
| 664 |
+
.btn-primary {
|
| 665 |
+
background: var(--accent);
|
| 666 |
+
color: #fff;
|
| 667 |
+
}
|
| 668 |
+
.btn-primary:hover { background: var(--accent-hover); }
|
| 669 |
+
.btn-primary:disabled {
|
| 670 |
+
opacity: 0.5;
|
| 671 |
+
cursor: not-allowed;
|
| 672 |
+
}
|
| 673 |
+
.btn-secondary {
|
| 674 |
+
background: var(--surface2);
|
| 675 |
+
color: var(--text);
|
| 676 |
+
border: 1px solid var(--border);
|
| 677 |
+
}
|
| 678 |
+
.btn-secondary:hover { background: var(--border); }
|
| 679 |
+
|
| 680 |
+
.action-row {
|
| 681 |
+
display: flex;
|
| 682 |
+
gap: 12px;
|
| 683 |
+
align-items: center;
|
| 684 |
+
flex-wrap: wrap;
|
| 685 |
+
}
|
| 686 |
+
|
| 687 |
+
/* Progress */
|
| 688 |
+
.progress-container {
|
| 689 |
+
display: none;
|
| 690 |
+
margin-bottom: 16px;
|
| 691 |
+
}
|
| 692 |
+
.progress-container.active { display: block; }
|
| 693 |
+
.progress-bar-bg {
|
| 694 |
+
background: var(--surface2);
|
| 695 |
+
border-radius: 8px;
|
| 696 |
+
height: 8px;
|
| 697 |
+
overflow: hidden;
|
| 698 |
+
margin-bottom: 8px;
|
| 699 |
+
}
|
| 700 |
+
.progress-bar {
|
| 701 |
+
height: 100%;
|
| 702 |
+
background: var(--accent);
|
| 703 |
+
border-radius: 8px;
|
| 704 |
+
transition: width 0.3s;
|
| 705 |
+
width: 0%;
|
| 706 |
+
}
|
| 707 |
+
.progress-text {
|
| 708 |
+
font-size: 13px;
|
| 709 |
+
color: var(--text2);
|
| 710 |
+
font-family: 'SF Mono', monospace;
|
| 711 |
+
}
|
| 712 |
+
|
| 713 |
+
/* Info */
|
| 714 |
+
.info-bar {
|
| 715 |
+
background: var(--surface);
|
| 716 |
+
border-radius: 8px;
|
| 717 |
+
padding: 10px 16px;
|
| 718 |
+
font-family: 'SF Mono', monospace;
|
| 719 |
+
font-size: 13px;
|
| 720 |
+
color: var(--text2);
|
| 721 |
+
display: none;
|
| 722 |
+
}
|
| 723 |
+
.info-bar.active { display: block; }
|
| 724 |
+
|
| 725 |
+
/* Batch gallery */
|
| 726 |
+
.gallery {
|
| 727 |
+
display: grid;
|
| 728 |
+
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
|
| 729 |
+
gap: 12px;
|
| 730 |
+
margin-bottom: 16px;
|
| 731 |
+
}
|
| 732 |
+
.gallery-item {
|
| 733 |
+
background: var(--surface);
|
| 734 |
+
border-radius: 8px;
|
| 735 |
+
overflow: hidden;
|
| 736 |
+
cursor: pointer;
|
| 737 |
+
transition: transform 0.2s;
|
| 738 |
+
}
|
| 739 |
+
.gallery-item:hover { transform: scale(1.02); }
|
| 740 |
+
.gallery-item img {
|
| 741 |
+
width: 100%;
|
| 742 |
+
aspect-ratio: 1;
|
| 743 |
+
object-fit: cover;
|
| 744 |
+
}
|
| 745 |
+
.gallery-item .name {
|
| 746 |
+
padding: 6px 10px;
|
| 747 |
+
font-size: 11px;
|
| 748 |
+
color: var(--text2);
|
| 749 |
+
white-space: nowrap;
|
| 750 |
+
overflow: hidden;
|
| 751 |
+
text-overflow: ellipsis;
|
| 752 |
+
}
|
| 753 |
+
|
| 754 |
+
/* File list */
|
| 755 |
+
.file-list {
|
| 756 |
+
display: flex;
|
| 757 |
+
flex-wrap: wrap;
|
| 758 |
+
gap: 8px;
|
| 759 |
+
margin-top: 8px;
|
| 760 |
+
}
|
| 761 |
+
.file-chip {
|
| 762 |
+
background: var(--surface2);
|
| 763 |
+
border-radius: 6px;
|
| 764 |
+
padding: 4px 10px;
|
| 765 |
+
font-size: 12px;
|
| 766 |
+
color: var(--text2);
|
| 767 |
+
display: flex;
|
| 768 |
+
align-items: center;
|
| 769 |
+
gap: 6px;
|
| 770 |
+
}
|
| 771 |
+
.file-chip .remove {
|
| 772 |
+
cursor: pointer;
|
| 773 |
+
color: #f44;
|
| 774 |
+
font-weight: bold;
|
| 775 |
+
}
|
| 776 |
+
|
| 777 |
+
/* Spinner */
|
| 778 |
+
.spinner {
|
| 779 |
+
display: inline-block;
|
| 780 |
+
width: 18px;
|
| 781 |
+
height: 18px;
|
| 782 |
+
border: 2px solid rgba(255,255,255,0.3);
|
| 783 |
+
border-top-color: #fff;
|
| 784 |
+
border-radius: 50%;
|
| 785 |
+
animation: spin 0.8s linear infinite;
|
| 786 |
+
}
|
| 787 |
+
@keyframes spin { to { transform: rotate(360deg); } }
|
| 788 |
+
|
| 789 |
+
/* Toast */
|
| 790 |
+
.toast {
|
| 791 |
+
position: fixed;
|
| 792 |
+
bottom: 24px;
|
| 793 |
+
right: 24px;
|
| 794 |
+
background: var(--surface2);
|
| 795 |
+
border: 1px solid var(--border);
|
| 796 |
+
border-radius: 8px;
|
| 797 |
+
padding: 12px 20px;
|
| 798 |
+
font-size: 14px;
|
| 799 |
+
z-index: 100;
|
| 800 |
+
display: none;
|
| 801 |
+
animation: slideUp 0.3s;
|
| 802 |
+
}
|
| 803 |
+
@keyframes slideUp {
|
| 804 |
+
from { transform: translateY(20px); opacity: 0; }
|
| 805 |
+
to { transform: translateY(0); opacity: 1; }
|
| 806 |
+
}
|
| 807 |
+
|
| 808 |
+
@media (max-width: 640px) {
|
| 809 |
+
.preview-area { grid-template-columns: 1fr; }
|
| 810 |
+
.control-row { flex-direction: column; align-items: stretch; }
|
| 811 |
+
}
|
| 812 |
+
</style>
|
| 813 |
+
</head>
|
| 814 |
+
<body>
|
| 815 |
+
<div class="container">
|
| 816 |
+
<header>
|
| 817 |
+
<h1>Thera <span>MLX</span></h1>
|
| 818 |
+
<p>Arbitrary-scale super-resolution on Apple Silicon</p>
|
| 819 |
+
</header>
|
| 820 |
+
|
| 821 |
+
<div class="tabs">
|
| 822 |
+
<button class="tab active" onclick="switchTab('single')">Image</button>
|
| 823 |
+
<button class="tab" onclick="switchTab('batch')">Batch</button>
|
| 824 |
+
<button class="tab" onclick="switchTab('video')">Video</button>
|
| 825 |
+
</div>
|
| 826 |
+
|
| 827 |
+
<!-- ==================== SINGLE IMAGE ==================== -->
|
| 828 |
+
<div id="tab-single" class="tab-content active">
|
| 829 |
+
<div class="controls">
|
| 830 |
+
<div class="control-row">
|
| 831 |
+
<div class="control-group">
|
| 832 |
+
<label>Scale</label>
|
| 833 |
+
<div style="display:flex;align-items:center;gap:8px">
|
| 834 |
+
<input type="range" id="s-scale" min="1" max="8" step="0.5" value="2"
|
| 835 |
+
oninput="document.getElementById('s-scale-val').textContent=this.value+'x'">
|
| 836 |
+
<span class="scale-display" id="s-scale-val">2x</span>
|
| 837 |
+
</div>
|
| 838 |
+
</div>
|
| 839 |
+
<div class="control-group">
|
| 840 |
+
<label>Model</label>
|
| 841 |
+
<select id="s-model">
|
| 842 |
+
<option value="air">Air (fast)</option>
|
| 843 |
+
<option value="pro">Pro (quality)</option>
|
| 844 |
+
</select>
|
| 845 |
+
</div>
|
| 846 |
+
<label class="checkbox-label">
|
| 847 |
+
<input type="checkbox" id="s-ensemble"> Ensemble
|
| 848 |
+
</label>
|
| 849 |
+
<div class="control-group">
|
| 850 |
+
<label>Tiles</label>
|
| 851 |
+
<select id="s-tiles">
|
| 852 |
+
<option value="1">Off</option>
|
| 853 |
+
<option value="2">2x2</option>
|
| 854 |
+
<option value="3">3x3</option>
|
| 855 |
+
<option value="4">4x4</option>
|
| 856 |
+
</select>
|
| 857 |
+
</div>
|
| 858 |
+
</div>
|
| 859 |
+
</div>
|
| 860 |
+
|
| 861 |
+
<div class="dropzone" id="s-dropzone"
|
| 862 |
+
onclick="document.getElementById('s-file').click()">
|
| 863 |
+
<div class="dropzone-icon">🖼</div>
|
| 864 |
+
<div class="dropzone-text">
|
| 865 |
+
<strong>Drop image here</strong> or click to browse
|
| 866 |
+
</div>
|
| 867 |
+
<input type="file" id="s-file" accept="image/*" hidden
|
| 868 |
+
onchange="handleSingleFile(this.files[0])">
|
| 869 |
+
</div>
|
| 870 |
+
|
| 871 |
+
<div class="preview-area" id="s-preview" style="display:none">
|
| 872 |
+
<div class="preview-panel">
|
| 873 |
+
<span class="panel-label">Input</span>
|
| 874 |
+
<img id="s-input-img">
|
| 875 |
+
</div>
|
| 876 |
+
<div class="preview-panel">
|
| 877 |
+
<span class="panel-label">Output</span>
|
| 878 |
+
<img id="s-output-img">
|
| 879 |
+
</div>
|
| 880 |
+
</div>
|
| 881 |
+
|
| 882 |
+
<div class="compare-container" id="s-compare">
|
| 883 |
+
<span class="compare-label compare-label-before">Before</span>
|
| 884 |
+
<span class="compare-label compare-label-after">After</span>
|
| 885 |
+
<img id="s-compare-after" style="width:100%;display:block">
|
| 886 |
+
<div class="compare-overlay" id="s-compare-overlay">
|
| 887 |
+
<img id="s-compare-before">
|
| 888 |
+
</div>
|
| 889 |
+
<div class="compare-line" id="s-compare-line"></div>
|
| 890 |
+
</div>
|
| 891 |
+
|
| 892 |
+
<div class="progress-container" id="s-progress">
|
| 893 |
+
<div class="progress-bar-bg"><div class="progress-bar" id="s-pbar"></div></div>
|
| 894 |
+
<div class="progress-text" id="s-ptext">Processing...</div>
|
| 895 |
+
</div>
|
| 896 |
+
|
| 897 |
+
<div class="action-row">
|
| 898 |
+
<button class="btn btn-primary" id="s-btn" onclick="doSingleUpscale()" disabled>
|
| 899 |
+
Upscale
|
| 900 |
+
</button>
|
| 901 |
+
<button class="btn btn-secondary" id="s-compare-btn" style="display:none"
|
| 902 |
+
onclick="toggleCompare()">
|
| 903 |
+
Compare
|
| 904 |
+
</button>
|
| 905 |
+
<button class="btn btn-secondary" id="s-download" style="display:none"
|
| 906 |
+
onclick="downloadSingle()">
|
| 907 |
+
Download PNG
|
| 908 |
+
</button>
|
| 909 |
+
</div>
|
| 910 |
+
|
| 911 |
+
<div class="info-bar" id="s-info" style="margin-top:12px"></div>
|
| 912 |
+
</div>
|
| 913 |
+
|
| 914 |
+
<!-- ==================== BATCH ==================== -->
|
| 915 |
+
<div id="tab-batch" class="tab-content">
|
| 916 |
+
<div class="controls">
|
| 917 |
+
<div class="control-row">
|
| 918 |
+
<div class="control-group">
|
| 919 |
+
<label>Scale</label>
|
| 920 |
+
<div style="display:flex;align-items:center;gap:8px">
|
| 921 |
+
<input type="range" id="b-scale" min="1" max="8" step="0.5" value="2"
|
| 922 |
+
oninput="document.getElementById('b-scale-val').textContent=this.value+'x'">
|
| 923 |
+
<span class="scale-display" id="b-scale-val">2x</span>
|
| 924 |
+
</div>
|
| 925 |
+
</div>
|
| 926 |
+
<div class="control-group">
|
| 927 |
+
<label>Model</label>
|
| 928 |
+
<select id="b-model">
|
| 929 |
+
<option value="air">Air (fast)</option>
|
| 930 |
+
<option value="pro">Pro (quality)</option>
|
| 931 |
+
</select>
|
| 932 |
+
</div>
|
| 933 |
+
<label class="checkbox-label">
|
| 934 |
+
<input type="checkbox" id="b-ensemble"> Ensemble
|
| 935 |
+
</label>
|
| 936 |
+
</div>
|
| 937 |
+
</div>
|
| 938 |
+
|
| 939 |
+
<div class="dropzone" id="b-dropzone"
|
| 940 |
+
onclick="document.getElementById('b-file').click()">
|
| 941 |
+
<div class="dropzone-icon">📁</div>
|
| 942 |
+
<div class="dropzone-text">
|
| 943 |
+
<strong>Drop images here</strong> or click to browse (multiple)
|
| 944 |
+
</div>
|
| 945 |
+
<input type="file" id="b-file" accept="image/*" multiple hidden
|
| 946 |
+
onchange="handleBatchFiles(this.files)">
|
| 947 |
+
<div class="file-list" id="b-filelist"></div>
|
| 948 |
+
</div>
|
| 949 |
+
|
| 950 |
+
<div class="progress-container" id="b-progress">
|
| 951 |
+
<div class="progress-bar-bg"><div class="progress-bar" id="b-pbar"></div></div>
|
| 952 |
+
<div class="progress-text" id="b-ptext">Processing...</div>
|
| 953 |
+
</div>
|
| 954 |
+
|
| 955 |
+
<div class="action-row">
|
| 956 |
+
<button class="btn btn-primary" id="b-btn" onclick="doBatchUpscale()" disabled>
|
| 957 |
+
Upscale All
|
| 958 |
+
</button>
|
| 959 |
+
<button class="btn btn-secondary" id="b-download" style="display:none"
|
| 960 |
+
onclick="downloadBatch()">
|
| 961 |
+
Download ZIP
|
| 962 |
+
</button>
|
| 963 |
+
</div>
|
| 964 |
+
|
| 965 |
+
<div class="info-bar" id="b-info" style="margin-top:12px"></div>
|
| 966 |
+
</div>
|
| 967 |
+
|
| 968 |
+
<!-- ==================== VIDEO ==================== -->
|
| 969 |
+
<div id="tab-video" class="tab-content">
|
| 970 |
+
<div class="controls">
|
| 971 |
+
<div class="control-row">
|
| 972 |
+
<div class="control-group">
|
| 973 |
+
<label>Scale</label>
|
| 974 |
+
<div style="display:flex;align-items:center;gap:8px">
|
| 975 |
+
<input type="range" id="v-scale" min="1" max="4" step="0.5" value="2"
|
| 976 |
+
oninput="document.getElementById('v-scale-val').textContent=this.value+'x'">
|
| 977 |
+
<span class="scale-display" id="v-scale-val">2x</span>
|
| 978 |
+
</div>
|
| 979 |
+
</div>
|
| 980 |
+
<div class="control-group">
|
| 981 |
+
<label>Model</label>
|
| 982 |
+
<select id="v-model">
|
| 983 |
+
<option value="air">Air (fast)</option>
|
| 984 |
+
<option value="pro">Pro (quality)</option>
|
| 985 |
+
</select>
|
| 986 |
+
</div>
|
| 987 |
+
</div>
|
| 988 |
+
</div>
|
| 989 |
+
|
| 990 |
+
<div class="dropzone" id="v-dropzone"
|
| 991 |
+
onclick="document.getElementById('v-file').click()">
|
| 992 |
+
<div class="dropzone-icon">🎬</div>
|
| 993 |
+
<div class="dropzone-text">
|
| 994 |
+
<strong>Drop video here</strong> or click to browse
|
| 995 |
+
</div>
|
| 996 |
+
<input type="file" id="v-file" accept="video/*" hidden
|
| 997 |
+
onchange="handleVideoFile(this.files[0])">
|
| 998 |
+
</div>
|
| 999 |
+
|
| 1000 |
+
<div class="preview-area" id="v-preview" style="display:none">
|
| 1001 |
+
<div class="preview-panel">
|
| 1002 |
+
<span class="panel-label">Input</span>
|
| 1003 |
+
<video id="v-input-vid" controls muted></video>
|
| 1004 |
+
</div>
|
| 1005 |
+
<div class="preview-panel">
|
| 1006 |
+
<span class="panel-label">Output</span>
|
| 1007 |
+
<video id="v-output-vid" controls></video>
|
| 1008 |
+
</div>
|
| 1009 |
+
</div>
|
| 1010 |
+
|
| 1011 |
+
<div class="progress-container" id="v-progress">
|
| 1012 |
+
<div class="progress-bar-bg"><div class="progress-bar" id="v-pbar"></div></div>
|
| 1013 |
+
<div class="progress-text" id="v-ptext">Processing...</div>
|
| 1014 |
+
</div>
|
| 1015 |
+
|
| 1016 |
+
<div class="action-row">
|
| 1017 |
+
<button class="btn btn-primary" id="v-btn" onclick="doVideoUpscale()" disabled>
|
| 1018 |
+
Upscale Video
|
| 1019 |
+
</button>
|
| 1020 |
+
<button class="btn btn-secondary" id="v-download" style="display:none"
|
| 1021 |
+
onclick="downloadVideo()">
|
| 1022 |
+
Download MP4
|
| 1023 |
+
</button>
|
| 1024 |
+
</div>
|
| 1025 |
+
|
| 1026 |
+
<div class="info-bar" id="v-info" style="margin-top:12px"></div>
|
| 1027 |
+
</div>
|
| 1028 |
+
</div>
|
| 1029 |
+
|
| 1030 |
+
<div class="toast" id="toast"></div>
|
| 1031 |
+
|
| 1032 |
+
<script>
|
| 1033 |
+
// --- Tab switching ---
|
| 1034 |
+
function switchTab(tab) {
|
| 1035 |
+
document.querySelectorAll('.tab').forEach((el, i) => {
|
| 1036 |
+
const tabs = ['single','batch','video'];
|
| 1037 |
+
el.classList.toggle('active', tabs[i] === tab);
|
| 1038 |
+
});
|
| 1039 |
+
document.querySelectorAll('.tab-content').forEach(el => el.classList.remove('active'));
|
| 1040 |
+
document.getElementById('tab-' + tab).classList.add('active');
|
| 1041 |
+
}
|
| 1042 |
+
|
| 1043 |
+
// --- Toast ---
|
| 1044 |
+
function toast(msg, duration=3000) {
|
| 1045 |
+
const el = document.getElementById('toast');
|
| 1046 |
+
el.textContent = msg;
|
| 1047 |
+
el.style.display = 'block';
|
| 1048 |
+
setTimeout(() => el.style.display = 'none', duration);
|
| 1049 |
+
}
|
| 1050 |
+
|
| 1051 |
+
// --- Drag & drop ---
|
| 1052 |
+
document.querySelectorAll('.dropzone').forEach(dz => {
|
| 1053 |
+
dz.addEventListener('dragover', e => { e.preventDefault(); dz.classList.add('dragover'); });
|
| 1054 |
+
dz.addEventListener('dragleave', () => dz.classList.remove('dragover'));
|
| 1055 |
+
dz.addEventListener('drop', e => {
|
| 1056 |
+
e.preventDefault();
|
| 1057 |
+
dz.classList.remove('dragover');
|
| 1058 |
+
const input = dz.querySelector('input[type="file"]');
|
| 1059 |
+
if (input.multiple) {
|
| 1060 |
+
handleBatchFiles(e.dataTransfer.files);
|
| 1061 |
+
} else if (input.accept.includes('video')) {
|
| 1062 |
+
handleVideoFile(e.dataTransfer.files[0]);
|
| 1063 |
+
} else {
|
| 1064 |
+
handleSingleFile(e.dataTransfer.files[0]);
|
| 1065 |
+
}
|
| 1066 |
+
});
|
| 1067 |
+
});
|
| 1068 |
+
|
| 1069 |
+
// ==================== SINGLE ====================
|
| 1070 |
+
let singleFile = null;
|
| 1071 |
+
let singleBlob = null;
|
| 1072 |
+
let singleInputUrl = null;
|
| 1073 |
+
let compareActive = false;
|
| 1074 |
+
|
| 1075 |
+
function handleSingleFile(file) {
|
| 1076 |
+
if (!file) return;
|
| 1077 |
+
singleFile = file;
|
| 1078 |
+
const dz = document.getElementById('s-dropzone');
|
| 1079 |
+
dz.classList.add('has-file');
|
| 1080 |
+
dz.querySelector('.dropzone-text').innerHTML = '<strong>' + file.name + '</strong>';
|
| 1081 |
+
|
| 1082 |
+
singleInputUrl = URL.createObjectURL(file);
|
| 1083 |
+
document.getElementById('s-input-img').src = singleInputUrl;
|
| 1084 |
+
document.getElementById('s-output-img').src = '';
|
| 1085 |
+
document.getElementById('s-preview').style.display = 'grid';
|
| 1086 |
+
document.getElementById('s-btn').disabled = false;
|
| 1087 |
+
document.getElementById('s-download').style.display = 'none';
|
| 1088 |
+
document.getElementById('s-compare-btn').style.display = 'none';
|
| 1089 |
+
document.getElementById('s-info').classList.remove('active');
|
| 1090 |
+
hideCompare();
|
| 1091 |
+
}
|
| 1092 |
+
|
| 1093 |
+
async function doSingleUpscale() {
|
| 1094 |
+
if (!singleFile) return;
|
| 1095 |
+
const btn = document.getElementById('s-btn');
|
| 1096 |
+
btn.disabled = true;
|
| 1097 |
+
btn.innerHTML = '<span class="spinner"></span> Processing';
|
| 1098 |
+
hideCompare();
|
| 1099 |
+
|
| 1100 |
+
const prog = document.getElementById('s-progress');
|
| 1101 |
+
prog.classList.add('active');
|
| 1102 |
+
document.getElementById('s-pbar').style.width = '60%';
|
| 1103 |
+
const tiles = document.getElementById('s-tiles').value;
|
| 1104 |
+
const tileLabel = tiles > 1 ? ` (${tiles}x${tiles} tiles)` : '';
|
| 1105 |
+
document.getElementById('s-ptext').textContent = 'Upscaling' + tileLabel + '...';
|
| 1106 |
+
|
| 1107 |
+
const fd = new FormData();
|
| 1108 |
+
fd.append('image', singleFile);
|
| 1109 |
+
fd.append('scale', document.getElementById('s-scale').value);
|
| 1110 |
+
fd.append('model', document.getElementById('s-model').value);
|
| 1111 |
+
fd.append('ensemble', document.getElementById('s-ensemble').checked);
|
| 1112 |
+
fd.append('tiles', tiles);
|
| 1113 |
+
|
| 1114 |
+
try {
|
| 1115 |
+
const resp = await fetch('/api/upscale', { method: 'POST', body: fd });
|
| 1116 |
+
if (!resp.ok) { const e = await resp.json(); throw new Error(e.error); }
|
| 1117 |
+
|
| 1118 |
+
const blob = await resp.blob();
|
| 1119 |
+
singleBlob = blob;
|
| 1120 |
+
const url = URL.createObjectURL(blob);
|
| 1121 |
+
document.getElementById('s-output-img').src = url;
|
| 1122 |
+
|
| 1123 |
+
const info = JSON.parse(resp.headers.get('X-Info') || '{}');
|
| 1124 |
+
document.getElementById('s-info').textContent =
|
| 1125 |
+
`${info.src} \u2192 ${info.dst} (${info.scale}x) | ${info.model} | ${info.time}s`;
|
| 1126 |
+
document.getElementById('s-info').classList.add('active');
|
| 1127 |
+
document.getElementById('s-download').style.display = 'inline-flex';
|
| 1128 |
+
document.getElementById('s-compare-btn').style.display = 'inline-flex';
|
| 1129 |
+
|
| 1130 |
+
document.getElementById('s-pbar').style.width = '100%';
|
| 1131 |
+
toast('Upscale complete!');
|
| 1132 |
+
} catch (e) {
|
| 1133 |
+
toast('Error: ' + e.message, 5000);
|
| 1134 |
+
} finally {
|
| 1135 |
+
btn.disabled = false;
|
| 1136 |
+
btn.innerHTML = 'Upscale';
|
| 1137 |
+
prog.classList.remove('active');
|
| 1138 |
+
}
|
| 1139 |
+
}
|
| 1140 |
+
|
| 1141 |
+
function downloadSingle() {
|
| 1142 |
+
if (!singleBlob) return;
|
| 1143 |
+
const a = document.createElement('a');
|
| 1144 |
+
a.href = URL.createObjectURL(singleBlob);
|
| 1145 |
+
const name = singleFile.name.replace(/\.[^.]+$/, '') + '_thera.png';
|
| 1146 |
+
a.download = name;
|
| 1147 |
+
a.click();
|
| 1148 |
+
}
|
| 1149 |
+
|
| 1150 |
+
// --- Compare split-view ---
|
| 1151 |
+
function toggleCompare() {
|
| 1152 |
+
if (compareActive) {
|
| 1153 |
+
hideCompare();
|
| 1154 |
+
} else {
|
| 1155 |
+
showCompare();
|
| 1156 |
+
}
|
| 1157 |
+
}
|
| 1158 |
+
|
| 1159 |
+
function showCompare() {
|
| 1160 |
+
if (!singleBlob || !singleInputUrl) return;
|
| 1161 |
+
compareActive = true;
|
| 1162 |
+
|
| 1163 |
+
const outputUrl = URL.createObjectURL(singleBlob);
|
| 1164 |
+
const container = document.getElementById('s-compare');
|
| 1165 |
+
const afterImg = document.getElementById('s-compare-after');
|
| 1166 |
+
const beforeImg = document.getElementById('s-compare-before');
|
| 1167 |
+
|
| 1168 |
+
// Use the upscaled version to determine the display size,
|
| 1169 |
+
// then scale the input up to match via CSS so it's a fair pixel comparison
|
| 1170 |
+
afterImg.src = outputUrl;
|
| 1171 |
+
beforeImg.src = singleInputUrl;
|
| 1172 |
+
|
| 1173 |
+
afterImg.onload = () => {
|
| 1174 |
+
// Set before image width to match container
|
| 1175 |
+
beforeImg.style.width = container.offsetWidth + 'px';
|
| 1176 |
+
updateCompareSlider(0.5);
|
| 1177 |
+
};
|
| 1178 |
+
|
| 1179 |
+
document.getElementById('s-preview').style.display = 'none';
|
| 1180 |
+
container.classList.add('active');
|
| 1181 |
+
|
| 1182 |
+
document.getElementById('s-compare-btn').textContent = 'Side by Side';
|
| 1183 |
+
}
|
| 1184 |
+
|
| 1185 |
+
function hideCompare() {
|
| 1186 |
+
compareActive = false;
|
| 1187 |
+
document.getElementById('s-compare').classList.remove('active');
|
| 1188 |
+
if (singleFile) {
|
| 1189 |
+
document.getElementById('s-preview').style.display = 'grid';
|
| 1190 |
+
}
|
| 1191 |
+
document.getElementById('s-compare-btn').textContent = 'Compare';
|
| 1192 |
+
}
|
| 1193 |
+
|
| 1194 |
+
function updateCompareSlider(ratio) {
|
| 1195 |
+
const container = document.getElementById('s-compare');
|
| 1196 |
+
const overlay = document.getElementById('s-compare-overlay');
|
| 1197 |
+
const line = document.getElementById('s-compare-line');
|
| 1198 |
+
const w = container.offsetWidth;
|
| 1199 |
+
const pos = Math.max(0, Math.min(w, w * ratio));
|
| 1200 |
+
|
| 1201 |
+
overlay.style.width = pos + 'px';
|
| 1202 |
+
line.style.left = pos + 'px';
|
| 1203 |
+
}
|
| 1204 |
+
|
| 1205 |
+
// Compare drag handling
|
| 1206 |
+
(function() {
|
| 1207 |
+
const container = document.getElementById('s-compare');
|
| 1208 |
+
let dragging = false;
|
| 1209 |
+
|
| 1210 |
+
function onMove(clientX) {
|
| 1211 |
+
const rect = container.getBoundingClientRect();
|
| 1212 |
+
const ratio = (clientX - rect.left) / rect.width;
|
| 1213 |
+
updateCompareSlider(Math.max(0, Math.min(1, ratio)));
|
| 1214 |
+
}
|
| 1215 |
+
|
| 1216 |
+
container.addEventListener('mousedown', e => { dragging = true; onMove(e.clientX); });
|
| 1217 |
+
window.addEventListener('mousemove', e => { if (dragging) onMove(e.clientX); });
|
| 1218 |
+
window.addEventListener('mouseup', () => { dragging = false; });
|
| 1219 |
+
|
| 1220 |
+
container.addEventListener('touchstart', e => { dragging = true; onMove(e.touches[0].clientX); }, {passive: true});
|
| 1221 |
+
window.addEventListener('touchmove', e => { if (dragging) onMove(e.touches[0].clientX); }, {passive: true});
|
| 1222 |
+
window.addEventListener('touchend', () => { dragging = false; });
|
| 1223 |
+
})();
|
| 1224 |
+
|
| 1225 |
+
// ==================== BATCH ====================
|
| 1226 |
+
let batchFiles = [];
|
| 1227 |
+
let batchBlob = null;
|
| 1228 |
+
|
| 1229 |
+
function handleBatchFiles(files) {
|
| 1230 |
+
batchFiles = Array.from(files);
|
| 1231 |
+
const dz = document.getElementById('b-dropzone');
|
| 1232 |
+
dz.classList.add('has-file');
|
| 1233 |
+
|
| 1234 |
+
const list = document.getElementById('b-filelist');
|
| 1235 |
+
list.innerHTML = batchFiles.map((f, i) =>
|
| 1236 |
+
`<span class="file-chip">${f.name}
|
| 1237 |
+
<span class="remove" onclick="event.stopPropagation();removeBatchFile(${i})">×</span>
|
| 1238 |
+
</span>`
|
| 1239 |
+
).join('');
|
| 1240 |
+
|
| 1241 |
+
dz.querySelector('.dropzone-text').innerHTML =
|
| 1242 |
+
`<strong>${batchFiles.length} image${batchFiles.length>1?'s':''}</strong> selected`;
|
| 1243 |
+
document.getElementById('b-btn').disabled = false;
|
| 1244 |
+
}
|
| 1245 |
+
|
| 1246 |
+
function removeBatchFile(idx) {
|
| 1247 |
+
batchFiles.splice(idx, 1);
|
| 1248 |
+
if (batchFiles.length === 0) {
|
| 1249 |
+
const dz = document.getElementById('b-dropzone');
|
| 1250 |
+
dz.classList.remove('has-file');
|
| 1251 |
+
dz.querySelector('.dropzone-text').innerHTML =
|
| 1252 |
+
'<strong>Drop images here</strong> or click to browse (multiple)';
|
| 1253 |
+
document.getElementById('b-filelist').innerHTML = '';
|
| 1254 |
+
document.getElementById('b-btn').disabled = true;
|
| 1255 |
+
} else {
|
| 1256 |
+
handleBatchFiles(batchFiles);
|
| 1257 |
+
}
|
| 1258 |
+
}
|
| 1259 |
+
|
| 1260 |
+
async function doBatchUpscale() {
|
| 1261 |
+
if (!batchFiles.length) return;
|
| 1262 |
+
const btn = document.getElementById('b-btn');
|
| 1263 |
+
btn.disabled = true;
|
| 1264 |
+
btn.innerHTML = '<span class="spinner"></span> Processing';
|
| 1265 |
+
|
| 1266 |
+
const prog = document.getElementById('b-progress');
|
| 1267 |
+
prog.classList.add('active');
|
| 1268 |
+
document.getElementById('b-pbar').style.width = '30%';
|
| 1269 |
+
document.getElementById('b-ptext').textContent =
|
| 1270 |
+
`Upscaling ${batchFiles.length} images...`;
|
| 1271 |
+
|
| 1272 |
+
const fd = new FormData();
|
| 1273 |
+
batchFiles.forEach(f => fd.append('images', f));
|
| 1274 |
+
fd.append('scale', document.getElementById('b-scale').value);
|
| 1275 |
+
fd.append('model', document.getElementById('b-model').value);
|
| 1276 |
+
fd.append('ensemble', document.getElementById('b-ensemble').checked);
|
| 1277 |
+
|
| 1278 |
+
try {
|
| 1279 |
+
const resp = await fetch('/api/batch', { method: 'POST', body: fd });
|
| 1280 |
+
if (!resp.ok) { const e = await resp.json(); throw new Error(e.error); }
|
| 1281 |
+
|
| 1282 |
+
batchBlob = await resp.blob();
|
| 1283 |
+
const info = JSON.parse(resp.headers.get('X-Info') || '{}');
|
| 1284 |
+
document.getElementById('b-info').textContent =
|
| 1285 |
+
`${info.count} images | ${info.scale}x | ${info.model} | ${info.time}s`;
|
| 1286 |
+
document.getElementById('b-info').classList.add('active');
|
| 1287 |
+
document.getElementById('b-download').style.display = 'inline-flex';
|
| 1288 |
+
document.getElementById('b-pbar').style.width = '100%';
|
| 1289 |
+
|
| 1290 |
+
toast(`Batch complete — ${info.count} images`);
|
| 1291 |
+
} catch (e) {
|
| 1292 |
+
toast('Error: ' + e.message, 5000);
|
| 1293 |
+
} finally {
|
| 1294 |
+
btn.disabled = false;
|
| 1295 |
+
btn.innerHTML = 'Upscale All';
|
| 1296 |
+
prog.classList.remove('active');
|
| 1297 |
+
}
|
| 1298 |
+
}
|
| 1299 |
+
|
| 1300 |
+
function downloadBatch() {
|
| 1301 |
+
if (!batchBlob) return;
|
| 1302 |
+
const a = document.createElement('a');
|
| 1303 |
+
a.href = URL.createObjectURL(batchBlob);
|
| 1304 |
+
a.download = 'thera_batch.zip';
|
| 1305 |
+
a.click();
|
| 1306 |
+
}
|
| 1307 |
+
|
| 1308 |
+
// ==================== VIDEO ====================
|
| 1309 |
+
let videoFile = null;
|
| 1310 |
+
let videoJobId = null;
|
| 1311 |
+
|
| 1312 |
+
function handleVideoFile(file) {
|
| 1313 |
+
if (!file) return;
|
| 1314 |
+
videoFile = file;
|
| 1315 |
+
const dz = document.getElementById('v-dropzone');
|
| 1316 |
+
dz.classList.add('has-file');
|
| 1317 |
+
dz.querySelector('.dropzone-text').innerHTML = '<strong>' + file.name + '</strong>';
|
| 1318 |
+
|
| 1319 |
+
const url = URL.createObjectURL(file);
|
| 1320 |
+
document.getElementById('v-input-vid').src = url;
|
| 1321 |
+
document.getElementById('v-output-vid').src = '';
|
| 1322 |
+
document.getElementById('v-preview').style.display = 'grid';
|
| 1323 |
+
document.getElementById('v-btn').disabled = false;
|
| 1324 |
+
document.getElementById('v-download').style.display = 'none';
|
| 1325 |
+
document.getElementById('v-info').classList.remove('active');
|
| 1326 |
+
}
|
| 1327 |
+
|
| 1328 |
+
async function doVideoUpscale() {
|
| 1329 |
+
if (!videoFile) return;
|
| 1330 |
+
const btn = document.getElementById('v-btn');
|
| 1331 |
+
btn.disabled = true;
|
| 1332 |
+
btn.innerHTML = '<span class="spinner"></span> Processing';
|
| 1333 |
+
|
| 1334 |
+
const prog = document.getElementById('v-progress');
|
| 1335 |
+
prog.classList.add('active');
|
| 1336 |
+
document.getElementById('v-pbar').style.width = '2%';
|
| 1337 |
+
document.getElementById('v-ptext').textContent = 'Uploading...';
|
| 1338 |
+
|
| 1339 |
+
const fd = new FormData();
|
| 1340 |
+
fd.append('video', videoFile);
|
| 1341 |
+
fd.append('scale', document.getElementById('v-scale').value);
|
| 1342 |
+
fd.append('model', document.getElementById('v-model').value);
|
| 1343 |
+
|
| 1344 |
+
try {
|
| 1345 |
+
const resp = await fetch('/api/video/start', { method: 'POST', body: fd });
|
| 1346 |
+
if (!resp.ok) { const e = await resp.json(); throw new Error(e.error); }
|
| 1347 |
+
const data = await resp.json();
|
| 1348 |
+
videoJobId = data.job_id;
|
| 1349 |
+
pollVideoProgress();
|
| 1350 |
+
} catch (e) {
|
| 1351 |
+
toast('Error: ' + e.message, 5000);
|
| 1352 |
+
btn.disabled = false;
|
| 1353 |
+
btn.innerHTML = 'Upscale Video';
|
| 1354 |
+
prog.classList.remove('active');
|
| 1355 |
+
}
|
| 1356 |
+
}
|
| 1357 |
+
|
| 1358 |
+
async function pollVideoProgress() {
|
| 1359 |
+
if (!videoJobId) return;
|
| 1360 |
+
|
| 1361 |
+
try {
|
| 1362 |
+
const resp = await fetch('/api/video/progress/' + videoJobId);
|
| 1363 |
+
const job = await resp.json();
|
| 1364 |
+
|
| 1365 |
+
const pbar = document.getElementById('v-pbar');
|
| 1366 |
+
const ptext = document.getElementById('v-ptext');
|
| 1367 |
+
|
| 1368 |
+
if (job.status === 'extracting') {
|
| 1369 |
+
pbar.style.width = '5%';
|
| 1370 |
+
ptext.textContent = 'Extracting frames...';
|
| 1371 |
+
} else if (job.status === 'upscaling') {
|
| 1372 |
+
const pct = 5 + 85 * (job.current_frame / Math.max(job.total_frames, 1));
|
| 1373 |
+
pbar.style.width = pct + '%';
|
| 1374 |
+
ptext.textContent =
|
| 1375 |
+
`Frame ${job.current_frame}/${job.total_frames} | ` +
|
| 1376 |
+
`${job.fps} fps | ETA ${job.eta}s`;
|
| 1377 |
+
} else if (job.status === 'encoding') {
|
| 1378 |
+
pbar.style.width = '92%';
|
| 1379 |
+
ptext.textContent = 'Encoding video...';
|
| 1380 |
+
} else if (job.status === 'done') {
|
| 1381 |
+
pbar.style.width = '100%';
|
| 1382 |
+
ptext.textContent = 'Complete!';
|
| 1383 |
+
document.getElementById('v-output-vid').src =
|
| 1384 |
+
'/api/video/download/' + videoJobId;
|
| 1385 |
+
document.getElementById('v-info').textContent =
|
| 1386 |
+
`${job.src} → ${job.dst} (${job.scale}x) | ` +
|
| 1387 |
+
`${job.total_frames} frames | ${job.time}s | ${job.model}`;
|
| 1388 |
+
document.getElementById('v-info').classList.add('active');
|
| 1389 |
+
document.getElementById('v-download').style.display = 'inline-flex';
|
| 1390 |
+
|
| 1391 |
+
document.getElementById('v-btn').disabled = false;
|
| 1392 |
+
document.getElementById('v-btn').innerHTML = 'Upscale Video';
|
| 1393 |
+
toast('Video upscale complete!');
|
| 1394 |
+
return;
|
| 1395 |
+
} else if (job.status === 'error') {
|
| 1396 |
+
toast('Error: ' + job.error, 5000);
|
| 1397 |
+
document.getElementById('v-btn').disabled = false;
|
| 1398 |
+
document.getElementById('v-btn').innerHTML = 'Upscale Video';
|
| 1399 |
+
document.getElementById('v-progress').classList.remove('active');
|
| 1400 |
+
return;
|
| 1401 |
+
}
|
| 1402 |
+
|
| 1403 |
+
setTimeout(pollVideoProgress, 1000);
|
| 1404 |
+
} catch (e) {
|
| 1405 |
+
setTimeout(pollVideoProgress, 2000);
|
| 1406 |
+
}
|
| 1407 |
+
}
|
| 1408 |
+
|
| 1409 |
+
function downloadVideo() {
|
| 1410 |
+
if (!videoJobId) return;
|
| 1411 |
+
const a = document.createElement('a');
|
| 1412 |
+
a.href = '/api/video/download/' + videoJobId;
|
| 1413 |
+
a.download = 'thera_upscaled.mp4';
|
| 1414 |
+
a.click();
|
| 1415 |
+
}
|
| 1416 |
+
</script>
|
| 1417 |
+
</body>
|
| 1418 |
+
</html>
|
| 1419 |
+
"""
|
| 1420 |
+
|
| 1421 |
+
# ---------------------------------------------------------------------------
|
| 1422 |
+
# Main
|
| 1423 |
+
# ---------------------------------------------------------------------------
|
| 1424 |
+
|
| 1425 |
+
if __name__ == "__main__":
|
| 1426 |
+
import logging
|
| 1427 |
+
logging.getLogger("werkzeug").setLevel(logging.ERROR)
|
| 1428 |
+
|
| 1429 |
+
parser = argparse.ArgumentParser()
|
| 1430 |
+
parser.add_argument("--port", type=int, default=5005)
|
| 1431 |
+
parser.add_argument("--host", type=str, default="127.0.0.1")
|
| 1432 |
+
args = parser.parse_args()
|
| 1433 |
+
|
| 1434 |
+
print(f"Thera MLX → http://{args.host}:{args.port}")
|
| 1435 |
+
app.run(host=args.host, port=args.port, debug=False, threaded=True)
|
upscale.py
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Core upscale logic for Thera MLX."""
|
| 3 |
+
|
| 4 |
+
import time
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
import mlx.core as mx
|
| 8 |
+
import mlx.nn as nn
|
| 9 |
+
import numpy as np
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
from model import Thera
|
| 13 |
+
|
| 14 |
+
WEIGHTS_DIR = Path(__file__).parent / "weights"
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def load_weights(model, weights_path):
|
| 18 |
+
"""Load converted weights into the MLX model."""
|
| 19 |
+
weights_path = str(weights_path)
|
| 20 |
+
if weights_path.endswith('.safetensors'):
|
| 21 |
+
from safetensors.numpy import load_file
|
| 22 |
+
raw = load_file(weights_path)
|
| 23 |
+
weights = {k: mx.array(v) for k, v in raw.items()}
|
| 24 |
+
elif weights_path.endswith('.npz'):
|
| 25 |
+
raw = np.load(weights_path)
|
| 26 |
+
weights = {k: mx.array(raw[k]) for k, v in raw.items()}
|
| 27 |
+
else:
|
| 28 |
+
raise ValueError(f"Unknown weight format: {weights_path}")
|
| 29 |
+
|
| 30 |
+
weight_list = list(weights.items())
|
| 31 |
+
model.load_weights(weight_list)
|
| 32 |
+
return model
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def get_weights_path(model_size):
|
| 36 |
+
"""Resolve weights path for a model variant."""
|
| 37 |
+
return WEIGHTS_DIR / f"weights-{model_size}.safetensors"
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def upscale_tiled(model, source_np, target_h, target_w, tiles, ensemble=False):
|
| 41 |
+
"""Upscale an image using NxN tiles to reduce peak RAM.
|
| 42 |
+
|
| 43 |
+
Splits the source image into tiles with overlap, upscales each tile
|
| 44 |
+
individually, then blends them back together using linear feathering.
|
| 45 |
+
|
| 46 |
+
Args:
|
| 47 |
+
model: Loaded Thera model.
|
| 48 |
+
source_np: numpy array (H, W, 3) float32 in [0, 1].
|
| 49 |
+
target_h: Target height.
|
| 50 |
+
target_w: Target width.
|
| 51 |
+
tiles: Number of tiles per axis (2, 3, or 4).
|
| 52 |
+
ensemble: Use geometric self-ensemble.
|
| 53 |
+
|
| 54 |
+
Returns:
|
| 55 |
+
numpy uint8 array (target_h, target_w, 3).
|
| 56 |
+
"""
|
| 57 |
+
h, w = source_np.shape[:2]
|
| 58 |
+
scale_h = target_h / h
|
| 59 |
+
scale_w = target_w / w
|
| 60 |
+
|
| 61 |
+
# Overlap in source pixels (10% of tile size, minimum 8px)
|
| 62 |
+
tile_h = h / tiles
|
| 63 |
+
tile_w = w / tiles
|
| 64 |
+
overlap_h = max(8, int(tile_h * 0.1))
|
| 65 |
+
overlap_w = max(8, int(tile_w * 0.1))
|
| 66 |
+
|
| 67 |
+
# Build output canvas (float32 for blending)
|
| 68 |
+
output = np.zeros((target_h, target_w, 3), dtype=np.float32)
|
| 69 |
+
weight_map = np.zeros((target_h, target_w, 1), dtype=np.float32)
|
| 70 |
+
|
| 71 |
+
total_tiles = tiles * tiles
|
| 72 |
+
done = 0
|
| 73 |
+
|
| 74 |
+
for row in range(tiles):
|
| 75 |
+
for col in range(tiles):
|
| 76 |
+
# Source tile bounds with overlap
|
| 77 |
+
sy0 = round(row * h / tiles) - (overlap_h if row > 0 else 0)
|
| 78 |
+
sy1 = round((row + 1) * h / tiles) + (overlap_h if row < tiles - 1 else 0)
|
| 79 |
+
sx0 = round(col * w / tiles) - (overlap_w if col > 0 else 0)
|
| 80 |
+
sx1 = round((col + 1) * w / tiles) + (overlap_w if col < tiles - 1 else 0)
|
| 81 |
+
|
| 82 |
+
sy0 = max(0, sy0)
|
| 83 |
+
sy1 = min(h, sy1)
|
| 84 |
+
sx0 = max(0, sx0)
|
| 85 |
+
sx1 = min(w, sx1)
|
| 86 |
+
|
| 87 |
+
tile_src = source_np[sy0:sy1, sx0:sx1]
|
| 88 |
+
th = round((sy1 - sy0) * scale_h)
|
| 89 |
+
tw = round((sx1 - sx0) * scale_w)
|
| 90 |
+
|
| 91 |
+
# Upscale tile
|
| 92 |
+
result = model.upscale(mx.array(tile_src), th, tw, ensemble=ensemble)
|
| 93 |
+
mx.eval(result)
|
| 94 |
+
tile_out = np.array(result).astype(np.float32) / 255.0
|
| 95 |
+
|
| 96 |
+
# Target tile bounds
|
| 97 |
+
ty0 = round(sy0 * scale_h)
|
| 98 |
+
tx0 = round(sx0 * scale_w)
|
| 99 |
+
ty1 = ty0 + tile_out.shape[0]
|
| 100 |
+
tx1 = tx0 + tile_out.shape[1]
|
| 101 |
+
|
| 102 |
+
# Clamp to output bounds
|
| 103 |
+
ty1 = min(ty1, target_h)
|
| 104 |
+
tx1 = min(tx1, target_w)
|
| 105 |
+
tile_out = tile_out[:ty1 - ty0, :tx1 - tx0]
|
| 106 |
+
|
| 107 |
+
# Linear feather weight for blending overlaps
|
| 108 |
+
fh, fw = tile_out.shape[:2]
|
| 109 |
+
wy = np.ones(fh, dtype=np.float32)
|
| 110 |
+
wx = np.ones(fw, dtype=np.float32)
|
| 111 |
+
|
| 112 |
+
# Feather top/bottom edges in overlap regions
|
| 113 |
+
ovl_top = round(overlap_h * scale_h) if row > 0 else 0
|
| 114 |
+
ovl_bot = round(overlap_h * scale_h) if row < tiles - 1 else 0
|
| 115 |
+
ovl_left = round(overlap_w * scale_w) if col > 0 else 0
|
| 116 |
+
ovl_right = round(overlap_w * scale_w) if col < tiles - 1 else 0
|
| 117 |
+
|
| 118 |
+
if ovl_top > 0:
|
| 119 |
+
ramp = np.linspace(0, 1, min(ovl_top, fh), dtype=np.float32)
|
| 120 |
+
wy[:len(ramp)] = ramp
|
| 121 |
+
if ovl_bot > 0:
|
| 122 |
+
ramp = np.linspace(1, 0, min(ovl_bot, fh), dtype=np.float32)
|
| 123 |
+
wy[-len(ramp):] = np.minimum(wy[-len(ramp):], ramp)
|
| 124 |
+
if ovl_left > 0:
|
| 125 |
+
ramp = np.linspace(0, 1, min(ovl_left, fw), dtype=np.float32)
|
| 126 |
+
wx[:len(ramp)] = ramp
|
| 127 |
+
if ovl_right > 0:
|
| 128 |
+
ramp = np.linspace(1, 0, min(ovl_right, fw), dtype=np.float32)
|
| 129 |
+
wx[-len(ramp):] = np.minimum(wx[-len(ramp):], ramp)
|
| 130 |
+
|
| 131 |
+
w2d = wy[:, None] * wx[None, :] # (fh, fw)
|
| 132 |
+
w3d = w2d[:, :, None] # (fh, fw, 1)
|
| 133 |
+
|
| 134 |
+
output[ty0:ty1, tx0:tx1] += tile_out * w3d
|
| 135 |
+
weight_map[ty0:ty1, tx0:tx1] += w3d
|
| 136 |
+
|
| 137 |
+
done += 1
|
| 138 |
+
print(f" tile {done}/{total_tiles}")
|
| 139 |
+
|
| 140 |
+
# Normalize by weight
|
| 141 |
+
weight_map = np.maximum(weight_map, 1e-8)
|
| 142 |
+
output = (output / weight_map * 255 + 0.5).clip(0, 255).astype(np.uint8)
|
| 143 |
+
return output
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def upscale_file(input_path, output_path, scale=None, size=None,
|
| 147 |
+
model_size='air', weights_path=None, ensemble=False,
|
| 148 |
+
tiles=None):
|
| 149 |
+
"""Upscale a single image file."""
|
| 150 |
+
img = Image.open(input_path).convert('RGB')
|
| 151 |
+
source = np.asarray(img, dtype=np.float32) / 255.0
|
| 152 |
+
h, w = source.shape[:2]
|
| 153 |
+
|
| 154 |
+
if scale is not None:
|
| 155 |
+
target_h = round(h * scale)
|
| 156 |
+
target_w = round(w * scale)
|
| 157 |
+
elif size is not None:
|
| 158 |
+
target_h, target_w = size
|
| 159 |
+
else:
|
| 160 |
+
raise ValueError("Must specify either scale or size")
|
| 161 |
+
|
| 162 |
+
scale_actual = target_h / h
|
| 163 |
+
if weights_path is None:
|
| 164 |
+
weights_path = get_weights_path(model_size)
|
| 165 |
+
|
| 166 |
+
model = Thera(size=model_size)
|
| 167 |
+
model = load_weights(model, weights_path)
|
| 168 |
+
mx.eval(model.parameters())
|
| 169 |
+
|
| 170 |
+
t0 = time.perf_counter()
|
| 171 |
+
|
| 172 |
+
if tiles and tiles > 1:
|
| 173 |
+
print(f"Tiled upscale: {tiles}x{tiles} ({tiles*tiles} tiles)")
|
| 174 |
+
result_np = upscale_tiled(model, source, target_h, target_w,
|
| 175 |
+
tiles, ensemble=ensemble)
|
| 176 |
+
Image.fromarray(result_np).save(output_path)
|
| 177 |
+
else:
|
| 178 |
+
result = model.upscale(mx.array(source), target_h, target_w, ensemble=ensemble)
|
| 179 |
+
mx.eval(result)
|
| 180 |
+
Image.fromarray(np.array(result)).save(output_path)
|
| 181 |
+
|
| 182 |
+
elapsed = time.perf_counter() - t0
|
| 183 |
+
|
| 184 |
+
suffix = " (ensemble)" if ensemble else ""
|
| 185 |
+
tile_info = f" [{tiles}x{tiles} tiles]" if tiles and tiles > 1 else ""
|
| 186 |
+
print(f"[{model_size}]{suffix}{tile_info} {w}x{h} -> {target_w}x{target_h} ({scale_actual:.4g}x) {elapsed:.1f}s -> {output_path}")
|