Skip to content

Commit 5af2d61

Browse files
author
ihnatova
committed
Initial commit
1 parent d143c2d commit 5af2d61

File tree

12 files changed

+940
-0
lines changed

12 files changed

+940
-0
lines changed

dng_to_png.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2019-2020 by Andrey Ignatov. All Rights Reserved.
2+
3+
# python dng_to_png.py path_to_my_dng_file.dng
4+
5+
import numpy as np
6+
import imageio
7+
import rawpy
8+
import sys
9+
import os
10+
11+
12+
def extract_bayer_channels(raw):
13+
14+
ch_B = raw[1::2, 1::2]
15+
ch_Gb = raw[0::2, 1::2]
16+
ch_R = raw[0::2, 0::2]
17+
ch_Gr = raw[1::2, 0::2]
18+
19+
return ch_R, ch_Gr, ch_B, ch_Gb
20+
21+
22+
if __name__ == "__main__":
23+
24+
raw_file = sys.argv[1]
25+
print("Converting file " + raw_file)
26+
27+
if not os.path.isfile(raw_file):
28+
print("The file doesn't exist!")
29+
sys.exit()
30+
31+
raw = rawpy.imread(raw_file)
32+
raw_image = raw.raw_image
33+
del raw
34+
35+
# Use the following code to rotate the image (if needed)
36+
# raw_image = np.rot90(raw_image, k=2)
37+
38+
raw_image = raw_image.astype(np.float32)
39+
ch_R, ch_Gr, ch_B, ch_Gb = extract_bayer_channels(raw_image)
40+
41+
png_image = raw_image.astype(np.uint16)
42+
new_name = raw_file.replace(".dng", ".png")
43+
imageio.imwrite(new_name, png_image)

evaluate_accuracy.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright 2020 by Andrey Ignatov. All Rights Reserved.
2+
3+
import tensorflow as tf
4+
import numpy as np
5+
6+
import model
7+
import utils
8+
9+
from load_dataset import load_test_data
10+
11+
PATCH_WIDTH, PATCH_HEIGHT = 224, 224
12+
TARGET_SIZE = (PATCH_WIDTH * 2) * (PATCH_HEIGHT * 2) * 3
13+
config = None
14+
15+
# Path to the dataset:
16+
dataset_dir = 'raw_images/'
17+
18+
# Disable gpu (if needed):
19+
# config = tf.ConfigProto(device_count={'GPU': 0})
20+
21+
with tf.Session(config=config) as sess:
22+
23+
# Create placeholders for input and target images
24+
25+
phone_ = tf.placeholder(tf.float32, [1, PATCH_HEIGHT, PATCH_HEIGHT, 4])
26+
dslr_ = tf.placeholder(tf.float32, [1, int(PATCH_HEIGHT * 2), int(PATCH_WIDTH * 2), 3])
27+
28+
# Process raw images with your model:
29+
# enhanced = your_model(phone_), e.g.:
30+
enhanced, _, _, _, _, _ = model.PyNET(phone_, instance_norm=True, instance_norm_level_1=False)
31+
32+
# Compute losses
33+
34+
enhanced_flat = tf.reshape(enhanced, [-1, TARGET_SIZE])
35+
dslr_flat = tf.reshape(dslr_, [-1, TARGET_SIZE])
36+
37+
loss_ms_ssim = tf.reduce_mean(tf.image.ssim_multiscale(enhanced, dslr_, 1.0))
38+
39+
loss_mse = tf.reduce_sum(tf.pow(dslr_flat - enhanced_flat, 2)) / TARGET_SIZE
40+
loss_psnr = 20 * utils.log10(1.0 / tf.sqrt(loss_mse))
41+
42+
saver = tf.train.Saver()
43+
44+
# Restore your own model from a checkpoint
45+
# saver.restore(sess, "path_to_your_checkpoint"), e.g.:
46+
saver.restore(sess, "models/original/pynet_level_0.ckpt")
47+
48+
print("Loading test data...")
49+
test_data, test_answ = load_test_data(dataset_dir, PATCH_WIDTH, PATCH_HEIGHT, 2.0)
50+
print("Test data was loaded\n")
51+
52+
loss_ssim_ = 0.0
53+
loss_psnr_ = 0.0
54+
55+
test_size = test_data.shape[0]
56+
for j in range(test_size):
57+
58+
if j % 100 == 0:
59+
print(j)
60+
61+
phone_images = np.reshape(test_data[j], [1, PATCH_HEIGHT, PATCH_WIDTH, 4])
62+
dslr_images = np.reshape(test_answ[j], [1, int(PATCH_HEIGHT * 2), int(PATCH_WIDTH * 2), 3])
63+
64+
losses = sess.run([loss_psnr, loss_ms_ssim], feed_dict={phone_: phone_images, dslr_: dslr_images})
65+
66+
loss_psnr_ += losses[0]
67+
loss_ssim_ += losses[1]
68+
69+
loss_psnr_ = float(loss_psnr_) / test_size
70+
loss_ssim_ = float(loss_ssim_) / test_size
71+
72+
output_logs = "PSNR: %.4g, MS-SSIM: %.4g\n" % (loss_psnr_, loss_ssim_)
73+
print(output_logs)

load_dataset.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright 2020 by Andrey Ignatov. All Rights Reserved.
2+
3+
from __future__ import print_function
4+
from scipy import misc
5+
from PIL import Image
6+
import imageio
7+
import os
8+
import numpy as np
9+
10+
11+
def extract_bayer_channels(raw):
12+
13+
# Reshape the input bayer image
14+
15+
ch_B = raw[1::2, 1::2]
16+
ch_Gb = raw[0::2, 1::2]
17+
ch_R = raw[0::2, 0::2]
18+
ch_Gr = raw[1::2, 0::2]
19+
20+
RAW_combined = np.dstack((ch_B, ch_Gb, ch_R, ch_Gr))
21+
RAW_norm = RAW_combined.astype(np.float32) / (4 * 255)
22+
23+
return RAW_norm
24+
25+
26+
def load_test_data(dataset_dir, PATCH_WIDTH, PATCH_HEIGHT, DSLR_SCALE):
27+
28+
test_directory_dslr = dataset_dir + 'test/canon/'
29+
test_directory_phone = dataset_dir + 'test/huawei_raw/'
30+
31+
# NUM_TEST_IMAGES = 1204
32+
NUM_TEST_IMAGES = len([name for name in os.listdir(test_directory_phone)
33+
if os.path.isfile(os.path.join(test_directory_phone, name))])
34+
35+
test_data = np.zeros((NUM_TEST_IMAGES, PATCH_WIDTH, PATCH_HEIGHT, 4))
36+
test_answ = np.zeros((NUM_TEST_IMAGES, int(PATCH_WIDTH * DSLR_SCALE), int(PATCH_HEIGHT * DSLR_SCALE), 3))
37+
38+
for i in range(0, NUM_TEST_IMAGES):
39+
40+
I = np.asarray(imageio.imread((test_directory_phone + str(i) + '.png')))
41+
I = extract_bayer_channels(I)
42+
test_data[i, :] = I
43+
44+
I = np.asarray(Image.open(test_directory_dslr + str(i) + '.jpg'))
45+
I = misc.imresize(I, DSLR_SCALE / 2, interp='bicubic')
46+
I = np.float16(np.reshape(I, [1, int(PATCH_WIDTH * DSLR_SCALE), int(PATCH_HEIGHT * DSLR_SCALE), 3])) / 255
47+
test_answ[i, :] = I
48+
49+
return test_data, test_answ
50+
51+
52+
def load_training_batch(dataset_dir, TRAIN_SIZE, PATCH_WIDTH, PATCH_HEIGHT, DSLR_SCALE):
53+
54+
train_directory_dslr = dataset_dir + 'train/canon/'
55+
train_directory_phone = dataset_dir + 'train/huawei_raw/'
56+
57+
# NUM_TRAINING_IMAGES = 46839
58+
NUM_TRAINING_IMAGES = len([name for name in os.listdir(train_directory_phone)
59+
if os.path.isfile(os.path.join(train_directory_phone, name))])
60+
61+
TRAIN_IMAGES = np.random.choice(np.arange(0, NUM_TRAINING_IMAGES), TRAIN_SIZE, replace=False)
62+
63+
train_data = np.zeros((TRAIN_SIZE, PATCH_WIDTH, PATCH_HEIGHT, 4))
64+
train_answ = np.zeros((TRAIN_SIZE, int(PATCH_WIDTH * DSLR_SCALE), int(PATCH_HEIGHT * DSLR_SCALE), 3))
65+
66+
i = 0
67+
for img in TRAIN_IMAGES:
68+
69+
I = np.asarray(imageio.imread((train_directory_phone + str(img) + '.png')))
70+
I = extract_bayer_channels(I)
71+
train_data[i, :] = I
72+
73+
I = np.asarray(Image.open(train_directory_dslr + str(img) + '.jpg'))
74+
I = misc.imresize(I, DSLR_SCALE / 2, interp='bicubic')
75+
I = np.float16(np.reshape(I, [1, int(PATCH_WIDTH * DSLR_SCALE), int(PATCH_HEIGHT * DSLR_SCALE), 3])) / 255
76+
train_answ[i, :] = I
77+
78+
i += 1
79+
80+
return train_data, train_answ
81+

0 commit comments

Comments
 (0)