-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupscaler.py
69 lines (52 loc) · 1.86 KB
/
upscaler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# SNSR by codedcosmos
#
# SNSR is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License 3 as published by
# the Free Software Foundation.
# SNSR is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License 3 for more details.
# You should have received a copy of the GNU General Public License 3
# along with SNSR. If not, see <https://www.gnu.org/licenses/>.
import warnings
warnings.filterwarnings('ignore',category=FutureWarning)
import tensorflow as tf
from tensorflow import keras
import numpy as np
import model as mg
import os
import random
import glob
from PIL import Image
from pathlib import Path
# INPUT IMAGE
INPUT_IMAGE = "INSERT_INPUT_HERE.jpg"
OUTPUT_IMAGE = "INSERT_OUTPUT_HERE.jpg"
# Model
model = mg.generate_model()
optimizer=tf.keras.optimizers.Adam(1e-4)
model.compile(optimizer=optimizer,
loss=tf.keras.losses.Huber(),
metrics=['accuracy'])
# Load checkpoint
checkpoint_dir = 'training_checkpoints_old'
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
checkpoint = tf.train.Checkpoint(generator_optimizer=optimizer, model=model)
# Restore latest
checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))
# Load image
image_raw = Image.open(INPUT_IMAGE)
image = np.asarray(image_raw, dtype=np.float64)
image = image / 255
image = np.expand_dims(image, axis=0)
raw_output = model.predict(image)
res = raw_output.shape
# Convert output to image and save
raw_output = np.reshape(raw_output, (res[1], res[2], 3))
raw_output = raw_output * 255
raw_output = raw_output.astype(np.uint8)
raw_output[raw_output > 255] = 255
raw_output[raw_output < 0] = 0
output_image = Image.fromarray(raw_output)
output_image.save(OUTPUT_IMAGE)