def estimator(A_val, y_batch_val, hparams): x_hat_batch = [] for i in range(hparams.batch_size): y_val = copy.deepcopy(y_batch_val[i]) x_hat = utils.solve_lasso(A_val, y_val, hparams) x_hat = np.maximum(np.minimum(x_hat, 1), 0) x_hat_batch.append(x_hat) x_hat_batch = np.asarray(x_hat_batch) return x_hat_batch
def estimator(A_val, y_batch_val, hparams): x_hat_batch = [] W = wavelet_basis() WA = np.dot(W, A_val) for j in range(hparams.batch_size): y_val = y_batch_val[j] z_hat = utils.solve_lasso(WA, y_val, hparams) x_hat = np.dot(z_hat, W) x_hat_max = np.abs(x_hat).max() x_hat = x_hat / (1.0 * x_hat_max) x_hat_batch.append(x_hat) x_hat_batch = np.asarray(x_hat_batch) return x_hat_batch
def estimator(A_val, y_batch_val, hparams): # One can prove that taking 2D DCT of each row of A, # then solving usual LASSO, and finally taking 2D ICT gives the correct answer. A_new = copy.deepcopy(A_val) for i in range(A_val.shape[1]): A_new[:, i] = vec([dct2(channel) for channel in devec(A_new[:, i])]) x_hat_batch = [] for j in range(hparams.batch_size): y_val = y_batch_val[j] z_hat = utils.solve_lasso(A_new, y_val, hparams) x_hat = vec([idct2(channel) for channel in devec(z_hat)]).T x_hat = np.maximum(np.minimum(x_hat, 1), -1) x_hat_batch.append(x_hat) return x_hat_batch
def estimator(A_val, y_batch_val, hparams): x_hat_batch = [] W = wavelet_basis() # U, V = utils.RGB_matrix() # V = (V/127.5) - 1.0 # U = U/127.5 def convert(W): # convert W from YCbCr to RGB W_ = W.copy() V = np.zeros((12288, 1)) # R V[0::3] = ((255.0 / 219.0) * (-16.0)) + ((255.0 * 0.701 / 112.0) * (-128.0)) W_[:, 0::3] = (255.0 / 219.0) * W[:, 0::3] + (0.0) * W[:, 1::3] + ( 255.0 * 0.701 / 112.0) * W[:, 2::3] # G V[1::3] = ((255.0 / 219.0) * (-16.0)) - ((0.886 * 0.114 * 255.0 / (112.0 * 0.587)) * (-128.0)) - ((255.0 * 0.701 * 0.299 / (112.0 * 0.587)) * (-128.0)) W_[:, 1::3] = (255.0 / 219.0) * W[:, 0::3] - ( 0.886 * 0.114 * 255.0 / (112.0 * 0.587)) * W[:, 1::3] - (255.0 * 0.701 * 0.299 / (112.0 * 0.587)) * W[:, 2::3] # B V[2::3] = ((255.0 / 219.0) * (-16.0)) + ((0.886 * 255.0 / (112.0)) * (-128.0)) W_[:, 2::3] = (255.0 / 219.0) * W[:, 0::3] + ( 0.886 * 255.0 / (112.0)) * W[:, 1::3] + 0.0 * W[:, 2::3] return W_, V # WU = np.dot(W, U.T) WU, V = convert(W) WU = WU / 127.5 V = (V / 127.5) - 1.0 WA = np.dot(WU, A_val) y_batch_val_temp = y_batch_val - np.dot(V.T, A_val) for j in range(hparams.batch_size): y_val = y_batch_val_temp[j] z_hat = utils.solve_lasso(WA, y_val, hparams) x_hat = np.dot(z_hat, WU) + V.ravel() x_hat_max = np.abs(x_hat).max() x_hat = x_hat / (1.0 * x_hat_max) x_hat_batch.append(x_hat) x_hat_batch = np.asarray(x_hat_batch) return x_hat_batch
def estimator(A_val, y_batch_val, hparams): # (e.g. of estimator: lasso_wavelet) x_hat_batch = [] W = wavelet_basis() WA = np.dot(W, A_val) for j in range(hparams.batch_size): y_val = y_batch_val[j] z_hat = utils.solve_lasso( WA, y_val, hparams ) # min l1-norm(z_hat) s.t. z_hat*w*A = y; z_hat is sparse x_hat = np.dot(z_hat, W) # to compute img x_hat_max = np.abs(x_hat).max() x_hat = x_hat / (1.0 * x_hat_max) # normalize x_hat_batch.append(x_hat) x_hat_batch = np.asarray(x_hat_batch) return x_hat_batch
def estimator(A_val, y_batch_val, hparams): if A_val is None: A = np.eye(y_batch_val.shape[1]) #TODO remove idle else: A = A_val.T mode = hparams.tv_or_lasso_mode y = utils_lasso.get_y(mode,y_batch_val,A) W = utils_lasso.load_W(hparams, A, y) x_hat_batch = [] if W is None: AW = A else: if W is None or 'tv-norm' in hparams.model_types: AW = None else: AW = A.dot(W) if 'fista' in mode: clf1 = FistaRegressor(penalty='l1') gs = GridSearchCV(clf1, {'alpha': np.logspace(-3, 3, 10)}) gs.fit(AW, y) x_hat_batch = gs.best_estimator_.coef_.dot(W)#.T elif 'cvxpy' in mode: maxit = utils_lasso.get_key('reweight',mode,int) weight = np.array([]) errors = [] for it in range(maxit): if maxit >1: print('Computing reweighting iteration: {}'.format(it+1)) x_hat_batch,weight = utils_lasso.cvxpy_problem(mode,W,A,AW,y,weight) current_error = np.mean(utils.get_l2_loss(np.array(hparams.x),utils_lasso.get_x(mode,x_hat_batch)))#np.mean(np.mean((np.array(hparams.x)-x_hat_batch)**2,axis=1)) errors.append(current_error) print('Current error: {}'.format(current_error)) if maxit>1: errors = np.array(errors) np.save(utils.get_checkpoint_dir(hparams, hparams.model_types[0])+'error_course',errors) elif 'cvxopt' in mode: for i in range(hparams.batch_size): y_val = y_batch_val[i] x_hat = utils_lasso.tv_cvxopt_partial(A_val, y_val,hparams) x_hat_batch.append(x_hat) print('Processed {} percent of all images'.format(float(i+1)/hparams.batch_size*100)) x_hat_batch = np.asarray(x_hat_batch) elif 'sklearn' in mode: for i in range(hparams.batch_size): y_val = y_batch_val[i] x_hat = utils.solve_lasso(A_val, y_val, hparams) x_hat = np.maximum(np.minimum(x_hat, 1), 0) x_hat_batch.append(x_hat) print('Processed {} percent of all images'.format(float(i+1)/hparams.batch_size*100)) x_hat_batch = np.asarray(x_hat_batch) elif 'bestkTermApprox' in mode: for i in range(hparams.batch_size): y_val = y_batch_val[i] x_hat = utils_lasso.check_bestkTermApprox(mode, W, y_val) x_hat = np.maximum(np.minimum(x_hat, 1), 0) x_hat_batch.append(x_hat) print('Processed {} percent of all images'.format(float(i+1)/hparams.batch_size*100)) x_hat_batch = np.asarray(x_hat_batch) elif 'bruteForce' in mode: #slithly redundant to above for i in range(hparams.batch_size): y_val = y_batch_val[i] x_hat = utils_lasso.brute_force(mode, A_val,W,y_val) x_hat = np.maximum(np.minimum(x_hat, 1), 0) x_hat_batch.append(x_hat) print('Processed {} percent of all images'.format(float(i+1)/hparams.batch_size*100)) x_hat_batch = np.asarray(x_hat_batch) else: raise NotImplementedError('Mode {} does not exist!'.format(mode)) return utils_lasso.get_x(mode,x_hat_batch)