示例#1
0
    marked_img = imgs_test.mark_pixels_on_full_image(img_new, order_now)
    return imgs_test.images[:, sample].reshape(
        imgs_test.img_size), img_new, marked_img, order


original_imgs = list()
num_c = list(chi).__len__()
num_f = list(nums_features).__len__()
imgs = list()
titles = list()
num_s_tot = 0
for nc in range(num_c):
    num_s = list(samples).__len__()
    num_s_tot += num_s
    para['chi'] = chi[nc]
    a, para = gtn_one_class(para)
    b = TNmachineLearning.MachineLearningFeatureMap(para['d'], para['dataset'])
    b.load_data(data_path=b.data_path,
                file_sample='t10k-images.idx3-ubyte',
                file_label='t10k-labels.idx1-ubyte')
    b.select_samples([para['class']])
    for ns in range(num_s):
        imgs_now = list()
        print('Processing the image ' + str(samples[ns]) + ' in class ' +
              str(para['class']))
        for n in range(num_f):
            img0, img1, marked_img, order = get_marked_imgs(
                a, b, nums_features[n], samples[ns], para, select_way)
            if n == 0 and nc == 0:
                original_imgs.append(img0.copy())
            if if_mark:
示例#2
0
if var_name is not 'which_class':
    b.load_data(data_path=os.path.join(b.project_path, '..\\..\\MNIST\\' + para['dataset'] + '\\'),
                file_sample='t10k-images.idx3-ubyte',
                file_label='t10k-labels.idx1-ubyte', is_normalize=True)
    b.select_samples([para['class']])

is_order_calculated = False
for t in range(var_values.size):
    # Modify parameter
    print('For ' + var_name + ' = ' + str(var_values[t]))
    exec(var_name + ' = ' + str(var_values[t]))
    if var_name in para:
        exec('para[\'' + var_name + '\'] = ' + str(var_values[t]))

    print('Train the generative TN')
    a, para_gtn = gtn_one_class(para)
    if var_name is 'which_class':
        b.load_data(data_path='..\\..\\..\\MNIST\\' + para['dataset'] + '\\',
                    file_sample='t10k-images.idx3-ubyte',
                    file_label='t10k-labels.idx1-ubyte', is_normalize=True)
        b.select_samples([para['class']])

    if select_way is 'SequencedMeasure':
        print('Calculate the sequence of the measurements')
        order_file = os.path.join(para['data_path'], 'Order_'+para['save_exp'])
        if (not is_order_calculated) and os.path.isfile(order_file):
            order = load_pr(order_file, 'order')
        else:
            order = a.mps.markov_measurement(if_restore=True)[0]
            save_pr(para['data_path'], 'Order_'+para['save_exp'], [order], ['order'])
        order_now = copy.copy(order.reshape(-1, )[:num_features])
示例#3
0
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))