def visualize_uniqueness(model_path,
                         dip_model_path,
                         false_trigger_imgs,
                         test_img,
                         save_images=True,
                         show_results=True):
    # dimostra che date in ingresso delle trigger image diverse dall'originale non produce il watermarker
    out_copyrightImg_path = 'results/uniqueness/'
    if save_images:
        utility.create_folder(out_copyrightImg_path)
    not_copyright_imgs = []
    model = WatermarkedVisualizerModel()
    model.build_model(model_path=model_path,
                      DnCNN_model_name=utility.get_last_model(model_path))
    for trigger in false_trigger_imgs:
        not_copyright_img = model.eval(trigger_image=trigger, show_input=False)
        not_copyright_imgs.append(not_copyright_img)
    ver_img = cv2.resize(cv2.imread(test_img, 0),
                         not_copyright_imgs[0].shape,
                         interpolation=cv2.INTER_AREA)
    concatenate_imgs = not_copyright_imgs
    concatenate_imgs.append(ver_img)
    stack_img = utility.stack_images_square(concatenate_imgs)
    if save_images:
        cv2.imwrite(
            out_copyrightImg_path + 'Stack_out_with_' +
            str(len(false_trigger_imgs)) + '_false_trigger_imgs.png',
            stack_img)
    if show_results:
        utility.show_image(
            stack_img,
            title='Results with ' + str(len(false_trigger_imgs)) +
            ' false trigger images - the last is Copyright image')
def pruning_attack_analysis(dim_imgs,
                            pruning_weights_path="./pruning_weights/",
                            show_distance=True,
                            show_Separate=False,
                            save_images=False):
    result_path = 'results/pruning/'
    if save_images:
        utility.create_folder(result_path)
    images_out = []
    distances_out = []
    psnr_all = []
    pruned_ks = [
        float(file[8:12]) for file in sorted(os.listdir(pruning_weights_path))
        if ".ckpt.meta" in file
    ]
    for pruned_k in pruned_ks:
        k = round(float(pruned_k), 2)
        model_pruned_name = "Pruned_k{:.2f}".format(k)
        dist, watermark_succeeded, psnr = WMVerificationManager(
            dim_imgs).calculate_dist_ver_psnr(
                model_attacked_folder=pruning_weights_path,
                model_attacked_name=model_pruned_name)
        print("{} | dist={:.5f} | WM succeded={} | psnr={:.2f}".format(
            model_pruned_name, dist, watermark_succeeded, psnr))
        distances_out.append(dist)
        psnr_all.append(psnr)

        # Visualization of watermark information under model pruning attacks
        model_visual_pruned = WatermarkedVisualizerModel()
        model_visual_pruned.build_model(DnCNN_model_name=model_pruned_name,
                                        model_path=pruning_weights_path)
        img_logo_pruned = model_visual_pruned.eval()

        if show_distance:
            img_logo_pruned = utility.create_text_image(
                img_logo_pruned, "{:.2f}={:.5f}".format(k, dist))
        images_out.append(img_logo_pruned)
        if show_Separate:
            utility.show_image(img_logo_pruned,
                               "{:.2f}={:.5f}".format(k, dist),
                               wait=True)
        if save_images:
            cv2.imwrite(result_path + "pruned_{:.2f}.jpg".format(k),
                        img_logo_pruned)
    if save_images:
        cv2.imwrite(result_path + 'stack_out_pruning.png',
                    utility.stack_images_row(images_out))
        datas_all = [{
            "pruning_percentage": k,
            "distance": d,
            "psnr": p
        } for k, d, p in zip(pruned_ks, distances_out, psnr_all)]
        utility.save_json_results({"pruning_results": datas_all},
                                  result_path + "datas_pruning.json")
    if not show_Separate:
        # utility.show_image(utility.stack_images_square(images_out), '1 Watermarked, other pruning 0.1, 0.2,...')
        utility.show_image(utility.stack_images_row(images_out),
                           '1 Watermarked, other pruning 0.1, 0.2,...')
def unwatermarked_vs_watermarked(save_images=True, show_results=True):
    result_path = 'results/WM_vs_UNWM/'
    if save_images:
        utility.create_folder(result_path)
    model_name = ['unwatermarked model', 'watermarked model']
    model_visual_unwatermarked = WatermarkedVisualizerModel()
    model_visual_unwatermarked.build_model(DnCNN_model_name='model_weight_45',
                                           model_path='./DnCNN_weight/')
    img_logo_unwatermarked = model_visual_unwatermarked.eval()
    dist, watermark_succeeded, psnr = WMVerificationManager(
        dim_imgs).calculate_dist_ver_psnr(
            model_attacked_folder='./DnCNN_weight/',
            model_attacked_name='model_weight_45')
    print("unwatermarked model | dist={:.5f} | WM succeded={} | psnr={:.2f}".
          format(dist, watermark_succeeded, psnr))
    model_visual_watermarked = WatermarkedVisualizerModel()
    model_visual_watermarked.build_model(
        DnCNN_model_name=utility.get_last_model('./overwriting/'),
        model_path='./overwriting/')
    dist_w, watermark_succeeded_w, psnr_w = WMVerificationManager(
        dim_imgs).calculate_dist_ver_psnr(
            model_attacked_folder='./overwriting/',
            model_attacked_name=utility.get_last_model('./overwriting/'))
    print("watermarked model | dist={:.5f} | WM succeded={} | psnr={:.2f}".
          format(dist_w, watermark_succeeded_w, psnr_w))
    img_logo_watermarked = model_visual_watermarked.eval()
    images_out = [img_logo_unwatermarked, img_logo_watermarked]
    if save_images:
        cv2.imwrite(result_path + 'Un-Watermarked.png',
                    utility.stack_images_row(images_out))
        datas_all = {
            "watermarked": {
                "distance": dist_w,
                "success": bool(watermark_succeeded_w),
                "psnr": psnr_w
            },
            "unwatermarked": {
                "distance": dist,
                "success": bool(watermark_succeeded),
                "psnr": psnr
            }
        }
        utility.save_json_results(datas_all,
                                  result_path + "datas_w_vs_uw.json")
    if show_results:
        utility.show_image(utility.stack_images_row(images_out),
                           'unwatermarked, watermarked')
def fidelity_analysis(watermarked_model_path,
                      dataset='./dataset/test/Texture12/',
                      save_images=True):
    result_path = 'results/fidelity/'
    if save_images:
        utility.create_folder(result_path)
    seed = 42
    watermarked_model = WatermarkedTrainedModel()
    watermarked_model.build_model(
        model_name=utility.get_last_model(watermarked_model_path),
        model_path=watermarked_model_path,
        seed=seed)
    unWatermarked_model = WatermarkedTrainedModel()
    unWatermarked_model.build_model(seed=seed)
    unwatermarked_imgs_out = []
    psnr_ = []
    test_images = [dataset + img for img in sorted(os.listdir(dataset))]
    for img in test_images:
        unwatermarked_imgs_out.append(
            unWatermarked_model.eval(test_img=img, show_input=False))
    for index, out in enumerate(unwatermarked_imgs_out):
        test_img = cv2.imread(test_images[index], 0)
        psnr_.append(utility.psnr(out, test_img))
    avg_psnr = sum(psnr_) / len(psnr_)
    print('UnWatermaked model | avg psnr: ', avg_psnr)
    watermarked_imgs_out = []
    psnr_w = []
    for img in test_images:
        watermarked_imgs_out.append(
            watermarked_model.eval(test_img=img, show_input=False))
    for index, out in enumerate(watermarked_imgs_out):
        test_img = cv2.imread(test_images[index], 0)
        psnr_w.append(utility.psnr(out, test_img))
    avg_psnr_w = sum(psnr_w) / len(psnr_w)
    print('Watermarked model | avg psnr: ', avg_psnr_w)
    if save_images:
        datas_all = [{
            "image": i,
            "psnr": p
        } for i, p in zip(test_images, psnr_)]
        utility.save_json_results(
            {
                "psnr_avg": avg_psnr_w,
                "psnr_all": datas_all
            }, result_path + "datas_fidelity.json")
示例#5
0
 def __init__(self,config, drawing, experiment_folder):
     super(PopulationSampleFigure, self).__init__(config, drawing, experiment_folder)
     self.figure_folder = create_folder(os.path.join(self.experiment_folder,"figure_population"))
     self.num_plots_sqrt = 3
     self.tfig,self.pf_subplots = plt.subplots(self.num_plots_sqrt,self.num_plots_sqrt)
     text = "Popluation"
     plt.gcf().canvas.set_window_title(text)
     for i in xrange(self.num_plots_sqrt):
         for j in xrange(self.num_plots_sqrt):
             self.pf_subplots[i,j].axis([0,self.grid_x,0,self.grid_y])
示例#6
0
 def __init__(self, config):
     self.__experiment_name = str(datetime.datetime.now().strftime("%d-%m-%Y_%H%M%S"))
     config_filename = config.get(GLOBAL_SECTION,"config_filename")
     
     config_file = open(os.path.join(self.get_experiment_folder(),os.path.basename(config_filename)),"w+")
     config.write(config_file)
     config_file.close()
     
     self.dump_folder = create_folder(os.path.join(self.get_experiment_folder(),"population_dump"))
     
     self.config = config
示例#7
0
 def __init__(self,config, drawing, experiment_folder):
     super(BestPhenoPredictionsFigure, self).__init__(config, drawing, experiment_folder)
     self.figure_folder = create_folder(os.path.join(self.experiment_folder,"figure_best_predictions"))
     plt.figure(3,figsize=(8, 8))
     text = "Best phenome with predictions"
     plt.title(text)
     plt.grid(b=True)        
     plt.gcf().canvas.set_window_title(text)
     self.bfg_predictions = plt.gcf().gca()
     self.bpf_predictions_ax = plt.gca()
     self.bpf_predictions_ax.axis([0,self.grid_x,0,self.grid_y])
     self.bpf_predictions_ax.set_aspect('equal')
示例#8
0
 def __init__(self, config, drawing, experiment_folder):
     super(PopulationSampleFigure, self).__init__(config, drawing,
                                                  experiment_folder)
     self.figure_folder = create_folder(
         os.path.join(self.experiment_folder, "figure_population"))
     self.num_plots_sqrt = 3
     self.tfig, self.pf_subplots = plt.subplots(self.num_plots_sqrt,
                                                self.num_plots_sqrt)
     text = "Popluation"
     plt.gcf().canvas.set_window_title(text)
     for i in xrange(self.num_plots_sqrt):
         for j in xrange(self.num_plots_sqrt):
             self.pf_subplots[i, j].axis([0, self.grid_x, 0, self.grid_y])
示例#9
0
 def __init__(self, config, drawing, experiment_folder):
     super(BestPhenoPredictionsFigure,
           self).__init__(config, drawing, experiment_folder)
     self.figure_folder = create_folder(
         os.path.join(self.experiment_folder, "figure_best_predictions"))
     plt.figure(3, figsize=(8, 8))
     text = "Best phenome with predictions"
     plt.title(text)
     plt.grid(b=True)
     plt.gcf().canvas.set_window_title(text)
     self.bfg_predictions = plt.gcf().gca()
     self.bpf_predictions_ax = plt.gca()
     self.bpf_predictions_ax.axis([0, self.grid_x, 0, self.grid_y])
     self.bpf_predictions_ax.set_aspect('equal')
示例#10
0
 def __init__(self,config, drawing, experiment_folder):
     
     super(PopulationPhenoFigure, self).__init__(config, drawing, experiment_folder)
     self.figure_folder = create_folder(os.path.join(self.experiment_folder,"figure_pop_phenomes"))
     plt.figure(5,figsize=(8, 8))
     text = "Phenomes in population"
     plt.title(text)
     plt.grid(b=True)        
     plt.gcf().canvas.set_window_title(text)
     self.bfg = plt.gcf().gca()
     self.bfg_ax = plt.gca()
     
     
     self.bfg_ax.set_aspect('equal')
     self.bfg_ax.axis([0,self.grid_x,0,self.grid_y])
示例#11
0
    def __init__(self, config):
        self.__experiment_name = str(
            datetime.datetime.now().strftime("%d-%m-%Y_%H%M%S"))
        config_filename = config.get(GLOBAL_SECTION, "config_filename")

        config_file = open(
            os.path.join(self.get_experiment_folder(),
                         os.path.basename(config_filename)), "w+")
        config.write(config_file)
        config_file.close()

        self.dump_folder = create_folder(
            os.path.join(self.get_experiment_folder(), "population_dump"))

        self.config = config
def uniqueness_analysis(model,
                        trigger_imgs,
                        verification_imgs,
                        n_keys,
                        dim_imgs,
                        save_images=True):
    out_copyrightImg_path = 'results/uniqueness/'
    if save_images:
        utility.create_folder(out_copyrightImg_path)
    out_datas = []
    for p in range(200, n_keys + 200, 200):
        new_verification_imgs = []
        distances_w = []
        succeeded_w = []
        for i in range(len(trigger_imgs)):
            v = model.eval(test_img=trigger_imgs[i], show_input=False)
            new_verification_imgs.append(v)
            dist, succeeded = WMVerificationManager(
                dim_imgs).watermark_verification(verification_imgs[i],
                                                 new_verification_imgs[i])
            distances_w.append(round(dist, 4))
            succeeded_w.append(succeeded)
        min_dist = np.min(distances_w)
        all_succeeded = all(succeeded_w)
        out_datas.append({
            "p": p,
            "min_dist": min_dist,
            "succeded": all_succeeded
        })
        print('min dist with ' + str(len(trigger_imgs[:p])) + ' key pairs: ' +
              str(min_dist))
        print('all keys are watermark_succeeded = ', all_succeeded)
    if save_images:
        utility.save_json_results({"uniqueness_Results": out_datas},
                                  out_copyrightImg_path +
                                  "datas_uniqueness.json")
示例#13
0
    def __init__(self, config, drawing, experiment_folder):

        super(PopulationPhenoFigure, self).__init__(config, drawing,
                                                    experiment_folder)
        self.figure_folder = create_folder(
            os.path.join(self.experiment_folder, "figure_pop_phenomes"))
        plt.figure(5, figsize=(8, 8))
        text = "Phenomes in population"
        plt.title(text)
        plt.grid(b=True)
        plt.gcf().canvas.set_window_title(text)
        self.bfg = plt.gcf().gca()
        self.bfg_ax = plt.gca()

        self.bfg_ax.set_aspect('equal')
        self.bfg_ax.axis([0, self.grid_x, 0, self.grid_y])
示例#14
0
    def __init__(self,config, drawing, experiment_folder):   
        super(GenerationFigure, self).__init__(config, drawing, experiment_folder)
        self.figure_folder = create_folder(os.path.join(self.experiment_folder,"figure_generations"))
        
        plt.figure(0)
        text = "EA progress"
        plt.title(text)
        plt.grid(b=True)        
        plt.gcf().canvas.set_window_title(text)
        plt.ylabel('Fitness')
        plt.xlabel('Generation')
        self.best, = plt.plot([], label='Best')
        self.worst, = plt.plot([], label='Worst')
        self.avg, = plt.plot([], label='Average')
        self.stddev, = plt.plot([], label='Std. dev.')
        self.ax = plt.gca()
        self.ax.axis([0,self.num_generations,0,100])

        plt.legend( loc=4)
示例#15
0
    def __init__(self, config, drawing, experiment_folder):
        super(GenerationFigure, self).__init__(config, drawing,
                                               experiment_folder)
        self.figure_folder = create_folder(
            os.path.join(self.experiment_folder, "figure_generations"))

        plt.figure(0)
        text = "EA progress"
        plt.title(text)
        plt.grid(b=True)
        plt.gcf().canvas.set_window_title(text)
        plt.ylabel('Fitness')
        plt.xlabel('Generation')
        self.best, = plt.plot([], label='Best')
        self.worst, = plt.plot([], label='Worst')
        self.avg, = plt.plot([], label='Average')
        self.stddev, = plt.plot([], label='Std. dev.')
        self.ax = plt.gca()
        self.ax.axis([0, self.num_generations, 0, 100])

        plt.legend(loc=4)
示例#16
0
        out_copyrightImg_path + '/Auxiliary_visualizer_per_checkpoint.png',
        stack_images)
    cv2.imshow("Auxiliary visualizer per checkpoint", stack_images)
    cv2.imshow(
        "Original image",
        cv2.resize(cv2.imread('test_img/' + img_test, 0),
                   eval_imgs[0].shape,
                   interpolation=cv2.INTER_AREA))
    cv2.waitKey(0)
    cv2.destroyAllWindows()


if __name__ == '__main__':
    model_path = './overwriting/'
    dip_model_path = './combine_weight/'
    test_img = 'test_img/sign.png'
    trigger_img = 'key_imgs/trigger_image.png'
    out_copyrightImg_path = 'out_copyrightImg'
    utility.create_folder(out_copyrightImg_path)

    # uncommet to view eval per checkpoint
    # eval_all_ckpts(model_path, dip_model_path, test_img)

    model = WatermarkedVisualizerModel()
    model.build_model(model_path=model_path,
                      DnCNN_model_name=utility.get_last_model(model_path),
                      DIP_model_path=dip_model_path,
                      DIP_model_name=utility.get_last_model(dip_model_path))

    eval_ckpt_and_compare(model)
def train(train_data='./data/img_clean_pats.npy',
          DnCNN_model_name='fineTuned_',
          epochs=8,
          batch_size=128,
          learn_rate=0.0001,
          sigma=25,
          org_model_path='./overwriting/',
          fineTuning_path='./fineTuning_weight/'):
    utility.create_folder(fineTuning_path)
    spec_size = [1, 40, 40, 1]
    save_ckpt_each = 5
    with tf.Graph().as_default():
        lr = tf.placeholder(tf.float32, shape=[], name='learning_rate')
        training = tf.placeholder(tf.bool, name='is_training')
        img_clean = tf.placeholder(
            tf.float32, [batch_size, spec_size[1], spec_size[2], spec_size[3]],
            name='clean_image')

        # DnCNN model
        img_noise = img_clean + tf.random_normal(
            shape=tf.shape(img_clean),
            stddev=sigma / 255.0)  # dati con aggiunta di rumore

        Y, N = dncnn(img_noise, is_training=training)

        # host loss
        dncnn_loss = lossing(Y, img_clean, batch_size)

        dncnn_opt = optimizer(dncnn_loss, lr)
        init = tf.global_variables_initializer()

        dncnn_var_list = [
            v for v in tf.global_variables() if v.name.startswith('block')
        ]
        DnCNN_saver = tf.train.Saver(dncnn_var_list, max_to_keep=50)

        np.random.seed(0)
        with tf.Session() as sess:
            data_total = np.load(train_data)
            data_total = data_total.astype(np.float32) / 255.0
            num_example, row, col, chanel = data_total.shape
            numBatch = num_example // batch_size
            sess.run(init)

            ckpt = tf.train.get_checkpoint_state(org_model_path)
            if ckpt and ckpt.model_checkpoint_path:
                full_path = tf.train.latest_checkpoint(org_model_path)
                print('last ckp', full_path)
                DnCNN_saver.restore(sess, full_path)
                print("Loading " + os.path.basename(full_path) +
                      " to the model")
            else:
                print("DnCNN weight must be exist")
                assert ckpt != None, 'weights not exist'

            step = 0
            for epoch in range(1, epochs + 1):
                np.random.shuffle(data_total)
                for batch_id in range(0, numBatch):
                    batch_images = data_total[batch_id *
                                              batch_size:(batch_id + 1) *
                                              batch_size, :, :, :]
                    if batch_id % 100 == 0:
                        dncnn_loss_res = sess.run(dncnn_loss,
                                                  feed_dict={
                                                      img_clean: batch_images,
                                                      lr: learn_rate,
                                                      training: False
                                                  })
                    _ = sess.run(dncnn_opt,
                                 feed_dict={
                                     img_clean: batch_images,
                                     lr: learn_rate,
                                     training: True
                                 })
                    step += 1
                print(
                    f"epoch={epoch}, step={step}, dncnn_loss={dncnn_loss_res}")
                if epoch % save_ckpt_each == 0:
                    DnCNN_saver.save(
                        sess,
                        os.path.join(
                            fineTuning_path,
                            DnCNN_model_name + str(epoch).zfill(2) + ".ckpt"))
                    print(f"++++ epoch {epoch} saved ++++")
示例#18
0
 def get_experiment_folder(self):
     folder_name = os.path.join("./experiments/",self.__experiment_name)
     return create_folder(folder_name)
示例#19
0
 def get_experiment_folder(self):
     folder_name = os.path.join("./experiments/", self.__experiment_name)
     return create_folder(folder_name)
示例#20
0
    # テスト結果を保存する場所
    save_root = r'E:\demo'
    # テストに使うモデルのnpzファイルの場所
    model_file = r'C:\Users\yamane\Dropbox\correct_aspect_ratio\dog_data_regression_ave_pooling\1485768519.06_asp_max_4.0\dog_data_regression_ave_pooling.npz'
    num_train = 16500  # 学習データ数
    num_valid = 500  # 検証データ数
    num_test = 100  # テストデータ数
    asp_r_max = 3.0  # 歪み画像の最大アスペクト比
    success_asp = 1.1  # 修正成功とみなす修正画像のアスペクト比の最大値
    batch_size = 100

    # モデルのファイル名をフォルダ名にする
    folder_name = model_file.split('\\')[-2]

    # テスト結果を保存するフォルダを作成
    test_folder_path = utility.create_folder(save_root, folder_name)
    fix_folder_path = utility.create_folder(test_folder_path, 'fix')
    dis_folder_path = utility.create_folder(test_folder_path, 'distorted')
    ori_folder_path = utility.create_folder(test_folder_path, 'original')

    # モデル読み込み
    model = voc2012_regression_max_pooling.Convnet().to_gpu()
    serializers.load_npz(model_file, model)

    # streamを取得
    streams = load_datasets.load_voc2012_stream(batch_size, num_train,
                                                num_valid, num_test)
    train_stream, valid_stream, test_stream = streams

    # 歪み画像の修正を実行
    loss, loss_abs, target, predict = lossfun(model, test_stream)
def fine_tuning_attack_analysis(dim_imgs,
                                show_distance=True,
                                show_Separate=False,
                                save_images=False,
                                finetuned_folder='./fineTuning_weights_Img12',
                                dataset_name='originalDataset'):
    # eval finetuning model with original data- calculate psnr and plot image. Choose epoch you need
    result_path = 'results/fineTuning_' + dataset_name + "/"
    if save_images:
        utility.create_folder(result_path)
    distances_out = []
    psnr_all = []
    images_out = []
    files = [c for c in (os.listdir(finetuned_folder)) if '.ckpt.index' in c]
    ep = ['10', '25', '50', '75', '100']
    for epoch in ep:
        filename = "fineTuned_" + str(epoch).zfill(2) + ".ckpt.index"
        if filename in files:
            model_fineTuned_name = "fineTuned_{}".format(epoch)
            dist, watermark_succeeded, psnr = WMVerificationManager(
                dim_imgs).calculate_dist_ver_psnr(
                    model_attacked_folder=finetuned_folder,
                    model_attacked_name=model_fineTuned_name)
            print("{:<10} | dist={:.5f} | WM succeded={} | psnr={:.2f}".format(
                model_fineTuned_name, dist, watermark_succeeded, psnr))
            distances_out.append(dist)
            psnr_all.append(psnr)
            # Visualization of watermark information under model fine-tuning attacks
            model_visual_finetuned = WatermarkedVisualizerModel()
            model_visual_finetuned.build_model(
                DnCNN_model_name=model_fineTuned_name,
                model_path=finetuned_folder)
            img_logo_fineTun = model_visual_finetuned.eval()

            if show_distance:
                img_logo_fineTun = utility.create_text_image(
                    img_logo_fineTun, "{}={:.5f}.jpg".format(epoch, dist))
            images_out.append(img_logo_fineTun)
            if show_Separate:
                utility.show_image(img_logo_fineTun,
                                   "{}={:.5f}.jpg".format(epoch, dist),
                                   wait=False)
            if save_images:
                cv2.imwrite(result_path + "fineTuned_{}.jpg".format(epoch),
                            img_logo_fineTun)
    if save_images:
        cv2.imwrite(result_path + 'stack_out_fineTuning.png',
                    utility.stack_images_row(images_out))
        datas_all = [{
            "epoch": e,
            "distance": d,
            "psnr": p
        } for e, d, p in zip(ep, distances_out, psnr_all)]
        utility.save_json_results(
            {
                "dataset_name": dataset_name,
                "finetuning_results": datas_all
            }, result_path + "datas_finetuning.json")
    if not show_Separate:
        utility.show_image(
            utility.stack_images_square(images_out),
            'Finetuning epochs 0 ' + ' '.join(ep) + ' using ' + dataset_name)
示例#22
0
import numpy as np
import cv2


class GeneratorTriggerVerificationImg:
    def __init__(self, m, n):
        """
        @param m: height of the image that the generator takes as input
        @param n: width of the image that the generator takes as input
        """
        self.m = m
        self.n = n

    def generate_trigger_and_verification_img(self):
        np.random.seed(None)
        self.trigger_img = np.random.random((self.m, self.n))*255
        filter_img = cv2.Sobel(self.trigger_img, cv2.CV_64F, 1, 1, ksize=3)
        self.verification_img = np.uint8(np.abs(filter_img))
        return self.trigger_img, self.verification_img

if __name__ == '__main__':
    import utility
    key_imgs_path = utility.create_folder('key_imgs')
    trigger_img, verification_img = GeneratorTriggerVerificationImg(40, 40).generate_trigger_and_verification_img()
    cv2.imwrite(key_imgs_path + '/trigger_image.png', trigger_img)
    cv2.imwrite(key_imgs_path + '/verification_image.png', verification_img)
示例#23
0
                full_path = os.path.join(org_model_path, model_name)
                print(full_path)
                DnCNN_saver.restore(sess, full_path)
                print("Loading " + os.path.basename(full_path) +
                      " to the model")

            else:
                print("DnCNN weight must be exist")
                assert ckpt != None, 'weights not exist'

            sess.run(updates)

            out_path = os.path.join(out_pruned_path,
                                    "Pruned_k{:.2f}.ckpt".format(k))
            DnCNN_saver.save(sess, out_path)
            print("Pruned model succesfully saved in " + out_path)


if __name__ == '__main__':
    import utility

    utility.create_folder("pruning_weights")
    for value_k in np.arange(0.05, 0.61, 0.05):
        k = round(float(value_k), 2)
        load_and_prune_model(org_model_path="overwriting",
                             model_name=utility.get_last_model("overwriting") +
                             ".ckpt",
                             k=k)
        print("Pruned {}".format(k))
    # prune_model(org_model_path=os.path.join("overwriting", utility.get_last_model("overwriting")), out_pruned_path="pruning_weights/pruned_float16")
示例#24
0
    num_split = 20  # 歪み画像のアスペクト比の段階

    loss_list = []
    loss_abs_list = []
    t_list = []
    folder_name = model_file.split('\\')[-2]

    num_t = num_split + 1
    t_step = np.log(3.0) * 2 / num_split
    t = np.log(1 / 3.0)
    for i in range(num_t):
        t_list.append(t)
        t = t + t_step

    # 結果を保存するフォルダを作成
    folder_path = utility.create_folder(save_root, folder_name)
    # モデル読み込み
    model = voc2012_regression_max_pooling.Convnet().to_gpu()
    # Optimizerの設定
    serializers.load_npz(model_file, model)
    # streamの取得
    streams = load_datasets.load_voc2012_stream(batch_size, num_train,
                                                num_valid, num_test)
    train_stream, valid_stream, test_stream = streams
    # アスペクト比ごとに歪み画像を作成し、修正誤差を計算
    for t in t_list:
        print t
        loss, loss_abs = fix(model, test_stream, t)
        loss_list.append(loss)
        loss_abs_list.append(loss_abs)
    # 修正誤差をグラフに描画