def fit(self, train_set): """Fit the model to observations. Parameters ---------- train_set: object of type TrainSet, required An object contraining the user-item preference in csr scipy sparse format,\ as well as some useful attributes such as mappings to the original user/item ids.\ Please refer to the class TrainSet in the "data" module for details. """ Recommender.fit(self, train_set) X = sp.csc_matrix(self.train_set.matrix) # recover the striplet sparse format from csc sparse matrix X (needed to feed c++) (rid, cid, val) = sp.find(X) val = np.array(val, dtype='float32') rid = np.array(rid, dtype='int32') cid = np.array(cid, dtype='int32') tX = np.concatenate((np.concatenate(([rid], [cid]), axis=0).T, val.reshape((len(val), 1))), axis=1) del rid, cid, val if self.trainable: if self.hierarchical: res = hpf.hpf(tX, X.shape[0], X.shape[1], self.k, self.max_iter, self.init_params) else: res = hpf.pf(tX, X.shape[0], X.shape[1], self.k, self.max_iter, self.init_params) self.Theta = np.asarray(res['Z']) self.Beta = np.asarray(res['W']) elif self.verbose: print('%s is trained already (trainable = False)' % (self.name))
gt = cv2.cvtColor(cv2.imread(gtlist[i]), cv2.COLOR_BGR2YCrCb)[:, :, 0] sr = cv2.cvtColor(cv2.imread(srlist[j]), cv2.COLOR_BGR2YCrCb)[:, :, 0] (height, width) = gt.shape if gt.shape != sr.shape: if height % 2 == 1: height -= 1 if width % 2 == 1: width -= 1 gt = gt[0:height, 0:width] if gt.shape != sr.shape: print('dimensions do not match on image:', gtlist[i], srlist[i]) print(gt.shape, sr.shape) continue srh = hpf(sr).astype(float) / 255. gth = hpf(gt).astype(float) / 255. ''' cv2.imshow('srh',srh) cv2.imshow('gth',gth) print('srh',srh) print('gth',gth) cv2.waitKey(0) cv2.destroyAllWindows() ''' stackQV(Q, V, count, srh, gth, margin, width, height, imagei, imageN, patchSize, W, pcaL, svc) print('count', count) print('fliping samples...') sys.stdout.flush()
import cv2 import numpy as np import os import pickle import sys import argparse from math import floor, pi from matplotlib import pyplot as plt from scipy import interpolate from skimage import transform from hpf import hpf gt = "SR_testing_datasets\\Set14_SR\\Set14\\image_SRF_2\\img_014_SRF_2_HR.png" gt = (cv2.cvtColor(cv2.imread(gt), cv2.COLOR_BGR2YCrCb)[:, :, 0]) gt = gt.astype(float) gth = hpf(gt) gth = gth.astype(float) gtl = gt - gth h, w = gt.shape n = w * h srlist = [] for parent, dirnames, filenames in os.walk( "SR_testing_datasets\\Set14_SR\\Set14SR"): for filename in filenames: srlist.append(os.path.join(parent, filename)) print(srlist) for sr_ in srlist: sr = cv2.cvtColor(cv2.imread(sr_), cv2.COLOR_BGR2YCrCb)[:, :, 0] sr = sr.astype(float)
if filename.lower().endswith( ('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')): imagelist.append(os.path.join(parent, filename)) Vsize = 10000 patchV = np.zeros((Vsize, 4, patchSize, patchSize)) Vid = np.zeros(4).astype(int) print('Collecting patches...') imcount = 0 for img in imagelist: imcount += 1 im = cv2.imread(img) im = cv2.cvtColor(im, cv2.COLOR_BGR2YCrCb)[:, :, 0] imh = hpf(im) w, h = imh.shape #cv2.imshow('',np.uint8(imh)) #cv2.waitKey(0) #print(im.shape) #print(imh.shape) processi = 0 processma = (h - 2 * margin) * (w - 2 * margin) #collect all patched with central HF energy>20 for row in range(margin, w - margin): print('\r', imcount, '/', len(imagelist), ' images', end='|') print('█' * (processi * 50 // processma), end='') print(' ' * (50 - processi * 50 // processma), end='|') sys.stdout.flush() for col in range(margin, h - margin): processi += 1
try: os.mkdir('results/'+ args.output) except Exception as e: print("ignoring error:",e) imagelist=[] for parent, dirnames, filenames in os.walk(args.input): for filename in filenames: if filename.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')): imagelist.append(os.path.join(parent, filename)) imagei=0 imageN=len(imagelist) for image in imagelist: imagei+=1 origin=cv2.cvtColor(cv2.imread(image),cv2.COLOR_BGR2YCrCb) sr=origin[:,:,0] srh=hpf(sr).astype(float) srl=sr.astype(float)-srh ehh=srh mask=np.zeros(sr.shape) width,height=sr.shape enhance(srh,ehh,mask,width,height,margin,pcaL,svc,h,imagei,imageN,patchSize,W) result=origin result[:,:,0]=np.uint8(np.clip(srl+ehh*255.,0.,255.)) result=cv2.cvtColor(result,cv2.COLOR_YCrCb2BGR) cv2.imwrite('results/'+ args.output + '/' +\ os.path.splitext(os.path.basename(image))[0] + '.png',result) cv2.imwrite('results/'+ args.output + '/' +\ os.path.splitext(os.path.basename(image))[0] + 'mask.png',mask.astype('uint8'))
import cv2 import numpy as np import os import pickle import sys import argparse from math import floor, pi from matplotlib import pyplot as plt from scipy import interpolate from skimage import transform from hpf import hpf gt = "SR_testing_datasets\\Set14_SR\\Set14\\image_SRF_2\\img_014_SRF_2_HR.png" gt = cv2.cvtColor(cv2.imread(gt), cv2.COLOR_BGR2YCrCb)[:, :, 0] gth = hpf(gt) gtl = gt - gth h, w = gt.shape n = w * h srlist = [] for parent, dirnames, filenames in os.walk( "SR_testing_datasets\\Set14_SR\\Set14\\image_SRF_2"): for filename in filenames: if filename.lower().startswith(('img_014_SRF_2_')): srlist.append(os.path.join(parent, filename)) print(srlist) for sr_ in srlist: sr = cv2.cvtColor(cv2.imread(sr_), cv2.COLOR_BGR2YCrCb)[:, :, 0] srh = hpf(sr) srl = srh - srl