train.py
import cv2, os, glob
import numpy as np
import matplotlib.pyplot as plt
from keras.layers import Conv2D, Input, Activation
from keras.models import Model
from keras.callbacks import ModelCheckpoint
from skimage.transform import pyramid_expand
from Subpixel import Subpixel
from DataGenerator import DataGenerator
base_path = 'c:/dataset/processed'
x_train_list = sorted(glob.glob(os.path.join(base_path, 'x_train', '*.npy')))
x_val_list = sorted(glob.glob(os.path.join(base_path, 'x_val', '*.npy')))
print(len(x_train_list), len(x_val_list))
print(x_train_list[0])
x1 = np.load(x_train_list[0])
x2 = np.load(x_val_list[0])
print(x1.shape, x2.shape)
plt.subplot(1, 2, 1)
plt.imshow(x1)
plt.subplot(1, 2, 2)
plt.imshow(x2)
train_gen = DataGenerator(list_IDs=x_train_list, labels=None, batch_size=16, dim=(44,44), n_channels=3, n_classes=None, shuffle=True)
val_gen = DataGenerator(list_IDs=x_val_list, labels=None, batch_size=16, dim=(44,44), n_channels=3, n_classes=None, shuffle=False)
upscale_factor = 4
inputs = Input(shape=(44, 44, 3))
net = Conv2D(filters=64, kernel_size=5, strides=1, padding='same', activation='relu')(inputs)
net = Conv2D(filters=64, kernel_size=3, strides=1, padding='same', activation='relu')(net)
net = Conv2D(filters=32, kernel_size=3, strides=1, padding='same', activation='relu')(net)
net = Conv2D(filters=upscale_factor**2, kernel_size=3, strides=1, padding='same', activation='relu')(net)
net = Subpixel(filters=3, kernel_size=3, r=upscale_factor, padding='same')(net)
outputs = Activation('relu')(net)
model = Model(inputs=inputs, outputs=outputs)
model.compile(optimizer='adam', loss='mse')
model.summary()
subpixel.py
from keras import backend as K
from keras.layers import Conv2D
"""
Subpixel Layer as a child class of Conv2D. This layer accepts all normal
arguments, with the exception of dilation_rate(). The argument r indicates
the upsampling factor, which is applied to the normal output of Conv2D.
The output of this layer will have the same number of channels as the
indicated filter field, and thus works for grayscale, color, or as a a
hidden layer.
Arguments:
*see Keras Docs for Conv2D args, noting that dilation_rate() is removed*
r: upscaling factor, which is applied to the output of normal Conv2D
A test is included, which performs super-resolution on the Cifar10 dataset.
Since these images are small, only a scale factor of 2 is used. Test images
are saved in the directory 'test_output/'. This test runs for 5 epochs,
which can be altered in line 132. You can run this test by using the
following commands:
mkdir test_output
python keras_subpixel.py
"""
class Subpixel(Conv2D):
def __init__(self,
filters,
kernel_size,
r,
padding='valid',
data_format=None,
strides=(1,1),
activation=None,
use_bias=True,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
**kwargs):
super(Subpixel, self).__init__(
filters=r*r*filters,
kernel_size=kernel_size,
strides=strides,
padding=padding,
data_format=data_format,
activation=activation,
use_bias=use_bias,
kernel_initializer=kernel_initializer,
bias_initializer=bias_initializer,
kernel_regularizer=kernel_regularizer,
bias_regularizer=bias_regularizer,
activity_regularizer=activity_regularizer,
kernel_constraint=kernel_constraint,
bias_constraint=bias_constraint,
**kwargs)
self.r = r
def _phase_shift(self, I):
r = self.r
bsize, a, b, c = I.get_shape().as_list()
bsize = K.shape(I)[0] # Handling Dimension(None) type for undefined batch dim
X = K.reshape(I, [bsize, a, b, int(c/(r*r)),r, r]) # bsize, a, b, c/(r*r), r, r
X = K.permute_dimensions(X, (0, 1, 2, 5, 4, 3)) # bsize, a, b, r, r, c/(r*r)
#Keras backend does not support tf.split, so in future versions this could be nicer
X = [X[:,i,:,:,:,:] for i in range(a)] # a, [bsize, b, r, r, c/(r*r)
X = K.concatenate(X, 2) # bsize, b, a*r, r, c/(r*r)
X = [X[:,i,:,:,:] for i in range(b)] # b, [bsize, r, r, c/(r*r)
X = K.concatenate(X, 2) # bsize, a*r, b*r, c/(r*r)
return X
def call(self, inputs):
return self._phase_shift(super(Subpixel, self).call(inputs))
def compute_output_shape(self, input_shape):
unshifted = super(Subpixel, self).compute_output_shape(input_shape)
return (unshifted[0], self.r*unshifted[1], self.r*unshifted[2], int(unshifted[3]/(self.r*self.r)))
def get_config(self):
config = super(Conv2D, self).get_config()
config.pop('rank')
config.pop('dilation_rate')
config['filters'] = int(config['filters'] / self.r*self.r)
config['r'] = self.r
return config
error
ValueError Traceback (most recent call last)
Cell In[37], line 9
7 net = Conv2D(filters=32, kernel_size=3, strides=1, padding=’same’, activation=’relu’)(net)
8 net = Conv2D(filters=upscale_factor**2, kernel_size=3, strides=1, padding=’same’, activation=’relu’)(net)
—-> 9 net = Subpixel(filters=3, kernel_size=3, r=upscale_factor, padding=’same’)(net)
10 outputs = Activation(‘relu’)(net)
12 model = Model(inputs=inputs, outputs=outputs)
File c:Usershko60AppDataLocalProgramsPythonPython310libsite-packageskerasutilstraceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.traceback)
68 # To get the full stack trace, call:
69 # tf.debugging.disable_traceback_filtering()
—> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File ~AppDataLocalTemp_autograph_generated_filezcyjbha2.py:13, in outer_factory..inner_factory..tf__call(self, inputs)
11 try:
12 do_return = True
—> 13 retval = ag__.converted_call(ag__.ld(self).phase_shift, (ag_.converted_call(ag__.converted_call(ag__.ld(super), (ag__.ld(Subpixel), ag__.ld(self)), None, fscope).call, (ag__.ld(inputs),), None, fscope),), None, fscope)
14 except:
15 do_return = False
ValueError: Exception encountered when calling layer “subpixel_22” (type Subpixel).
…
ValueError: Dimension 1 in both shapes must be equal, but are 44 and 176. Shapes are [?,44,44,48] and [?,176,176,3].
Call arguments received by layer “subpixel_22” (type Subpixel):
• inputs=tf.Tensor(shape=(None, 44, 44, 16), dtype=float32)
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings…
I am doing srcnn deep learning, which creates a high-resolution image of (176,176,3) when I input a low-resolution image of (44,44,3), but this error occurs.
황경원 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.