# var_values = np.hstack((np.arange(2, 20, 2), np.arange(20, 44, 4))) # var_values = np.arange(0, 104, 4) # var_values = np.hstack((np.arange(0, 100, 2), np.arange(100, 700, 10), # np.arange(700, 780, 5), np.arange(780, 784, 2))) select_way = 'SequencedMeasure' # 'SequencedMeasure', 'RandomMeasure', 'MaxSEE', 'Variance' generate_way = 'eigs' # 'eigs': grey images, 'rand': black-and-white (Lei's way) # Initialize parameters dataset = 'fashion_mnist' samples = 'all' num_features = 100 which_class = 3 chi = 40 # ============================================================= para = parameters_gtn_one_class() para['dct'] = False para['d'] = 2 para['step'] = 0.2 # initial gradient step para['if_save'] = True para['if_load'] = True para['dataset'] = dataset para['class'] = which_class para['chi'] = chi psnr_av = np.zeros(var_values.shape) mse_av = np.zeros(var_values.shape) ssim = np.zeros(var_values.shape) b = TNmachineLearning.MachineLearningFeatureMap(para['d'], para['dataset']) if var_name is not 'which_class': b.load_data(data_path=os.path.join(b.project_path, '..\\..\\MNIST\\' + para['dataset'] + '\\'),
def quantum_jpeg(para_tot=None): if para_tot is None: para_tot = parameters_qjpg() print('Preparing image') exp = expression_save(para_tot) exp = os.path.join(para_tot['data_path'], exp) if os.path.isfile(exp) and para_tot['if_load']: a, generator = load_pr(exp, ['a', 'mps']) else: a = qjpg(file=para_tot['file'], b_size=para_tot['block_size'], is_grey=True) a.cut2blocks() a.dct_blocks() a.pre_process_data_before_ml(which=2) para = parameters_gtn_one_class() para['chi'] = para_tot['chi'] para['dataset'] = 'custom' para['if_save'] = False para['if_load'] = False para['dct'] = False generator = None if 'real' in para_tot['tasks']: print('Train in the real space') for n in range(para_tot['reorder_time']): generator = gtn_one_class(para=para, images=a.blocks.squeeze())[0] if n != (para_tot['reorder_time'] - 1): order = generator.mps.markov_measurement( if_restore=False)[0] # ent = generator.mps.calculate_single_entropy()[0] # order = np.argsort(ent)[::-1] a.reorder_features(order, which=0) if 'freq' in para_tot['tasks']: for n in range(para_tot['reorder_time']): print('Train in the frequency spaceļ¼ reorder time = ' + str(n)) generator = gtn_one_class(para=para, images=a.blocks_dct.squeeze())[0] if n != (para_tot['reorder_time'] - 1): order = generator.mps.markov_measurement( if_restore=False)[0] # ent = generator.mps.calculate_single_entropy()[0] # order = np.argsort(ent)[::-1] a.reorder_features(order, which=1) save_pr(para_tot['data_path'], exp, [a, generator, para_tot], ['a', 'mps', 'para_tot']) # a.show_image() if 'recover' in para_tot['tasks']: blocks = a.encode_with_cutoff_dct(para_tot['pixel_cutoff']) image_gtn1 = decode_with_generative_mps(blocks, generator) image_cutoff = decode_with_cutoff_dct(blocks) blocks_jpg = a.encode_standard_jpeg(para_tot['pixel_cutoff']) image_jpg = decode_jpeg(blocks_jpg) io.imsave('../data_QJPG/before.jpg', a.image.squeeze()) io.imsave('../data_QJPG/0cut.jpg', image_cutoff.squeeze()) io.imsave('../data_QJPG/1JPGway.jpg', image_jpg.squeeze()) io.imsave('../data_QJPG/2GTNway.jpg', image_gtn1.squeeze()) p1 = psnr(a.image0, image_jpg) p2 = psnr(a.image0, image_cutoff) p3 = psnr(a.image0, image_gtn1) print('The PSNRs for jpg, cut-off, and gtn are %g, %g, and %g' % (p1, p2, p3))