|
| 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