text_plain_output_1.png
from PIL import Image

from tensorflow.keras.utils import to_categorical

import PIL

import cv2

import h5py

import matplotlib.pyplot as plt

import numpy as np

import numpy as np

import numpy as np

INPUTS_DB = '../input/ukbench100-patches-for-sr-hdf5/output/inputs.hdf5'
OUTPUTS_DB = '../input/ukbench100-patches-for-sr-hdf5/output/outputs.hdf5'
MODEL_PATH = 'output/srcnn.model'
PLOT_PATH = 'output/plot.png'
BATCH_SIZE = 128
NUM_EPOCHS = 10
SCALE = 2.0
INPUT_DIM = 33
LABEL_SIZE = 21
PAD = int((INPUT_DIM - LABEL_SIZE) / 2.0)
STRIDE = 14

from tensorflow.keras.utils import to_categorical
import numpy as np
import h5py
class HDF5DatasetGenerator:
    def __init__(self, db_path, batch_size, preprocessors=None, aug=None, binarize=True, classes=2):
        self.batch_size = batch_size
        self.preprocessors = preprocessors
        self.aug = aug
        self.binarize = binarize
        self.classes = classes
        self.db = h5py.File(db_path, 'r')
        self.num_images = self.db['labels'].shape[0]
    def generator(self, passes=np.inf):
        epochs = 0
        while epochs < passes:
            for i in np.arange(0, self.num_images, self.batch_size):
                images = self.db['images'][i:i + self.batch_size]
                labels = self.db['labels'][i:i + self.batch_size]
                if self.binarize:
                    labels = to_categorical(labels, self.classes)
                if self.preprocessors is not None:
                    proc_images = []
                    for image in images:
                        for p in self.preprocessors:
                            image = p.preprocess(image)
                        proc_images.append(image)
                    images = np.array(proc_images)
                if self.aug is not None:
                    images, labels = next(self.aug.flow(images, labels, batch_size=self.batch_size))
                yield (images, labels)
            epochs += 1
    def close(self):
        self.db.close()

plt.style.use('ggplot')

args = {'image': '../input/sample-images/jemma.png', 'baseline': 'output/baseline.png', 'output': 'output/output.png'}

image = cv2.imread(args['image'])
h, w = image.shape[:2]
w -= int(w % SCALE)
h -= int(h % SCALE)
image = image[0:h, 0:w]

lowW = int(w * (1.0 / SCALE))
lowH = int(h * (1.0 / SCALE))
highW = int(lowW * (SCALE / 1.0))
highH = int(lowH * (SCALE / 1.0))
scaled = np.array(Image.fromarray(image).resize((lowW, lowH), resample=PIL.Image.BICUBIC))
scaled = np.array(Image.fromarray(scaled).resize((highW, highH), resample=PIL.Image.BICUBIC))
cv2.imwrite(args['baseline'], scaled)

output = np.zeros(scaled.shape)
h, w = output.shape[:2]

output = output[PAD:h - (h % INPUT_DIM + PAD), PAD:w - (w % INPUT_DIM + PAD)]
output = np.clip(output, 0, 255).astype('uint8')

cv2.imwrite(args['output'], output)