def train_test_vdsr(is_train, scale=4): from MODELS.VDSR import VDSR model = VDSR(scale=scale, sess=tf.Session(), save_path='./TRAINED_MODEL/VDSRx{}/'.format(scale)) if is_train: train_img, label_img, test_img, test_label_img = prepareImageData( './DATABASE/DIV2K/', 0.2, scale=4, subimg_h=32, subimg_w=32) print('\n\nSRModel (' + 'VDSR' + ' x ' + str(scale) + ') Trainning ... ...') model.train(train_img, label_img, test_img, test_label_img, Epoch=int(5e4), iter_view=500) else: or_image = Image.open('./DATABASE/Set5/butterfly_GT.bmp').convert( "YCbCr") lr_image = DownScale(or_image, or_image.size, scale=4) lr_image = np.array(lr_image) sr_image = model.test(image=lr_image) sr_image.show() print( '----<The PSNR, SSIM between HR image and SR image is: PSNR:{:.5f}, SSIM:{:.5f}>----' .format(psnr(np.array(or_image), np.array(sr_image)), ssim(np.array(or_image), np.array(sr_image))))
def test_SpCodeVDSR(scale, fn, downscale_fn, config): from MODELS.VDSR import VDSR model = VDSR(config=config, sess=tf.Session(), save_path='./TRAINED_MODEL/SpCode-VDSRx{}/'.format(scale)) model.scale = scale hr_image = np.asarray(Image.open(fn).convert("YCbCr")) sr_image = model.SpCodeVDSRSR(fn, downscale_fn) print( '----<The PSNR, SSIM between HR image and SR image is: PSNR:{:.5f}, SSIM:{:.5f}>----' .format(psnr(hr_image, sr_image), ssim(hr_image, sr_image)))
def ksvd(Y, A0, K, n, Acc): num_signal = Y.shape[1] #待分解的信号个数 #A = overDct(m,n) #A = overDct(529,529) #A = A[0:512,0:512] A = A0 IterK = 0 #由于的DCT字典所以不必归一化 Err = 10**7 PSNR = 0 X = np.matrix(np.zeros((n, num_signal))) while (IterK < Acc): IterK += 1 #lsomp做稀疏表示 for i in range(0, num_signal): b = Y[:, i] rec = lsomp(A, b, K) X[:, i] = rec #用KSVD做字典学习 for j0 in range(0, n): Xj0 = X[j0, :] Xj0 = np.array(Xj0) Xj0.shape = [num_signal] InstacnceSet = np.nonzero(Xj0) InstacnceSet = np.array(InstacnceSet) InstacnceSet.shape = [InstacnceSet.shape[1]] InstacnceSet = list(InstacnceSet) if (max(np.abs(Xj0)) == 0): Err = Y - A * X Err = np.diagonal(np.transpose(Err) * Err) Err = list(Err) i = Err.index(max(Err)) A[:, j0] = Y[:, i] A[:, j0] = A[:, j0] / np.linalg.norm( np.transpose(A[:, j0]) * A[:, j0]) A[:, j0] = A[:, j0] / (np.sign(A[1, j0])) else: tempX = X[:, InstacnceSet] tempX[j0, :] = 0 Ej0 = Y[:, InstacnceSet] - A * tempX U, Delta, V = np.linalg.svd(Ej0) A[:, j0] = U[:, 0] X[j0, InstacnceSet] = Delta[0] * V[0, :] PSNR = psnr(np.array(Y), np.array(A * X)) curt = datetime.datetime.now() print('The No.' + str(IterK) + ' iteration.' + 'PSNR = ' + str(PSNR) + ' time:' + str(curt)) return A, X, PSNR
def test_vdsr(scale, fn, downscale_fn, config): from MODELS.VDSR import VDSR model = VDSR(config=config, sess=tf.Session(), save_path='./TRAINED_MODEL/VDSRx{}/'.format(scale)) model.scale = scale or_image = Image.open(fn).convert("YCbCr") lr_image = downscale_fn(or_image, or_image.size, scale=scale) lr_image = np.array(lr_image) sr_image = model.test(image=lr_image) sr_image.show() print( '----<The PSNR, SSIM between HR image and SR image is: PSNR:{:.5f}, SSIM:{:.5f}>----' .format(psnr(np.array(or_image), np.array(sr_image)), ssim(np.array(or_image), np.array(sr_image))))
def mod(Y, A0, K, n): #A = overDct(Y.shape[0],n) A = A0 num_signal = Y.shape[1] IterK = 0 X = np.matrix(np.zeros((n, num_signal))) PSNR = 0 while (IterK < 40): IterK += 1 for i in range(num_signal): rec = lsomp(A, Y[:, i], K) X[:, i] = rec #用MOD A = Y * X.T * (X * X.T).I PSNR = psnr(np.array(Y), np.array(A * X)) curt = datetime.datetime.now() print('The No.' + str(IterK) + ' iteration.' + 'PSNR = ' + str(PSNR) + ' time:' + str(curt)) return A, X, PSNR
def train_test_SpCodeVDSR(is_train, scale): from MODELS.VDSR import VDSR model = VDSR(scale=scale, sess=tf.Session(), save_path='./TRAINED_MODEL/SpCode-VDSRx{}/'.format(scale)) if is_train: x_test, y_test, x_train, y_train, train_size, test_size = prepareSparseData( './DATABASE/TrainDatax4.npz', 0.2) print('\n\SpCode- ( ' + 'VDSR' + ' x ' + str(scale) + ' ) Trainning ... ...') model.train(x_train, y_train, x_test, y_test, Epoch=int(5e4), iter_view=500) else: filename = './DATABASE/Set5/woman_GT.bmp' hr_image = np.asarray(Image.open(filename).convert("YCbCr")) sr_image = model.SpCodeVDSRSR(filename) print( '----<The PSNR, SSIM between HR image and SR image is: PSNR:{:.5f}, SSIM:{:.5f}>----' .format(psnr(hr_image, sr_image), ssim(hr_image, sr_image)))