コード例 #1
0
def eval_ckpt_and_compare(model):
    eval_img = model.eval()
    ver_img = cv2.resize(cv2.imread('test_img/sign.png', 0), eval_img.shape, interpolation=cv2.INTER_AREA)
    psrn_ = utility.psnr(eval_img, ver_img)
    print('psnr: ', psrn_)
    utility.show_image(np.hstack([eval_img, ver_img]),
                       title="Out: left - Original Watermarker: Right (DNCNN: {}, DIP: {})".format(
                           utility.get_last_model(model_path), utility.get_last_model(dip_model_path)),
                       wait=True)
コード例 #2
0
def eval_ckpt_and_compare(model):
    eval_img = model.eval()
    ver_img = cv2.resize(cv2.imread('test_img/sign.png', 0),
                         eval_img.shape,
                         interpolation=cv2.INTER_AREA)
    utility.show_image(
        np.hstack([eval_img, ver_img]),
        title="Out: left - Verification: Right (DNCNN: {}, DIP: {})".format(
            utility.get_last_model(model_path),
            utility.get_last_model(dip_model_path)),
        wait=True)
コード例 #3
0
def eval_all_ckpts(model_path, dip_model_path, img_test):
    ckpts = [
        c[:-len('.ckpt.index')] for c in sorted(os.listdir(dip_model_path))
        if '.ckpt.index' in c
    ]
    eval_imgs = []
    for dip_model in ckpts:
        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=dip_model)
        eval_img = model.eval()
        eval_imgs.append(eval_img)
        cv2.imwrite(out_copyrightImg_path + '/' + dip_model + '_copyright.png',
                    eval_img)
    stack_images = utility.stack_images_square(eval_imgs)
    cv2.imwrite(
        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()
コード例 #4
0
def pruning_attack_analysis(dim_imgs):
    model_visual_watermarked = WatermarkedVisualizerModel()
    model_visual_watermarked.build_model(
        DnCNN_model_name=utility.get_last_model('./overwriting/'),
        model_path='./overwriting/')
    img_logo_watermarked = model_visual_watermarked.eval()

    # eval pruned model with original data- calculate psnr and plot image. Choose pruned k you need
    images_out = [img_logo_watermarked]
    pruned_ks = [
        float(file[8:12]) for file in os.listdir("./pruning_weights/")
        if ".ckpt.meta" in file
    ]
    for pruned_k in sorted(pruned_ks):
        k = round(float(pruned_k), 2)
        model_pruned_name = "Pruned_k{:.2f}".format(k)
        dist, watermark_succeeded = ExecuteVerification(
            dim_imgs).verificationOnAttackedImg(
                model_attacked_folder="pruning_weights",
                model_attacked_name=model_pruned_name)

        # 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/')
        img_logo_pruned = model_visual_pruned.eval()

        print("{} | dist={:.5f} | WM succeded={} |".format(
            model_pruned_name, dist, watermark_succeeded))
        img_logo_pruned = utility.create_text_image(
            img_logo_pruned, "{:.2f}={:.5f}".format(k, dist))
        images_out.append(img_logo_pruned)

    utility.show_image(utility.stack_images_square(images_out),
                       '1 Watermarked, other pruning 0.1, 0.2,...')
コード例 #5
0
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')
コード例 #6
0
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')
コード例 #7
0
 def verificationOnAttackedImg(self, model_attacked_folder='./fineTuning_weight/',
                               model_attacked_name='fineTuned_10'):
     model = WatermarkedTrainedModel()
     model.build_model(model_name=utility.get_last_model('./overwriting/'), model_path='./overwriting/')
     opt_ver_img = model.eval(test_img='key_imgs/trigger_image.png', show_input=False)
     model.build_model(model_name=model_attacked_name, model_path=model_attacked_folder)
     new_ver_img = model.eval(test_img='key_imgs/trigger_image.png', show_input=False)
     dist, watermark_succeeded = self.watermark_verification(opt_ver_img, new_ver_img)
     return dist, watermark_succeeded
コード例 #8
0
def fine_tuning_attack_analysis(dim_imgs):
    # eval finetuning model with original data- calculate psnr and plot image. Choose epoch you need

    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()

    model_visual_watermarked = WatermarkedVisualizerModel()
    model_visual_watermarked.build_model(
        DnCNN_model_name=utility.get_last_model('./overwriting/'),
        model_path='./overwriting/')
    img_logo_watermarked = model_visual_watermarked.eval()

    images_out = [img_logo_unwatermarked, img_logo_watermarked]
    files = [
        c for c in (os.listdir('fineTuning_weight')) if '.ckpt.index' in c
    ]
    epochs = ['10', '25', '50']
    for f in sorted(files):
        epoch = f[10:12]
        if epoch in epochs:
            model_fineTuned_name = "fineTuned_{}".format(epoch)
            dist, watermark_succeeded = ExecuteVerification(
                dim_imgs).verificationOnAttackedImg(
                    model_attacked_folder="fineTuning_weight",
                    model_attacked_name=model_fineTuned_name)
            print("{} | dist={:.5f} | WM succeded={} |".format(
                model_fineTuned_name, dist, watermark_succeeded))

            # 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='./fineTuning_weight/')
            img_logo_fineTun = model_visual_finetuned.eval()
            img_logo_fineTun = utility.create_text_image(
                img_logo_fineTun, "{} epoch={:.5f}".format(epoch, dist))
            images_out.append(img_logo_fineTun)

    # text = "WM {}".format("recognized" if watermark_succeeded else "lost")
    # img_results = utility.create_text_image(
    #     utility.create_empty_image(img_logo_watermarked.shape[0], img_logo_watermarked.shape[1]), text)
    utility.show_image(
        utility.stack_images_square(images_out),
        '1 dncnn original ,2 Watermarked, 3 FineTuned{}epochs'.format(epochs))
コード例 #9
0
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")
 def calculate_dist_ver_psnr(self,
                             model_attacked_folder='./fineTuning_weight/',
                             model_attacked_name='fineTuned_10',
                             image_test_fn=''):
     model = WatermarkedTrainedModel()
     model.build_model(model_name=utility.get_last_model('./overwriting/'),
                       model_path='./overwriting/')
     opt_ver_img = model.eval(test_img='key_imgs/trigger_image.png',
                              show_input=False)
     model.build_model(model_name=model_attacked_name,
                       model_path=model_attacked_folder)
     new_ver_img = model.eval(test_img='key_imgs/trigger_image.png',
                              show_input=False)
     dist, watermark_succeeded = self.watermark_verification(
         opt_ver_img, new_ver_img)
     img = self.test_img if image_test_fn == '' else cv2.imread(
         image_test_fn, 0)
     psnr = utility.psnr(img, model.eval(img, show_input=False))
     return dist, watermark_succeeded, psnr
コード例 #11
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")
コード例 #12
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)
        ramd_Image = np.expand_dims(ramd_Image, 0)
        ramd_Image = np.expand_dims(ramd_Image, 3)

        # ramd_Images = np.load('./spec_input/spec_14065.npy')
        # ramd_Image = ramd_Images[0,:, :, :]
        # ramd_Image = np.expand_dims(ramd_Image, 0)

        mid, out = self.session.run([self.dncnn_s_out, self.ldr],
                                    feed_dict={self.img_clean: ramd_Image, self.training_placeholder: False})
        # print(mid.shape)

        mark_out = post_process(out)
        return mark_out


if __name__ == '__main__':
    out_copyrightImg_path = 'out_copyrightImg'
    utility.create_folder(out_copyrightImg_path)

    # comment here to change source model.'DnCNN_weight' is original model, 'overwrting' is WM trained model
    # model_path = './DnCNN_weight/'
    model_path = './overwriting/'
    dip_model_path = './combine_weight/'

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

    img = model.eval()
    cv2.imwrite(out_copyrightImg_path + '/copyrightImg.png', img)
    utility.show_image(img, title='watermark')
コード例 #14
0
        init = tf.global_variables_initializer()

        with tf.Session() as sess:
            sess.run(init)

            ckpt = tf.train.get_checkpoint_state(org_model_path)

            if ckpt and ckpt.model_checkpoint_path:
                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.51, 0.05):
        k = round(float(value_k), 2)
        load_and_prune_model(org_model_path="overwriting", model_name=utility.get_last_model("overwriting")+".ckpt", out_pruned_path="pruning_weights", 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")
コード例 #15
0
if __name__ == '__main__':
    show_uniqueness = False
    show_robustness_finetune = True
    show_robustness_pruning = True
    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)
    n_keys = 1000
    h_w = 40
    dim_imgs = h_w * h_w

    model = WatermarkedTrainedModel()
    model.build_model(model_name=utility.get_last_model(model_path),
                      model_path=model_path)

    if show_uniqueness:
        print('UNIQUENESS ANALYSIS')
        trigger_imgs, verification_imgs = generator_n_keys(h_w, h_w, n_keys)
        uniqueness_analysis(model, trigger_imgs, verification_imgs, n_keys,
                            dim_imgs)
        visualize_uniqueness(model_path, dip_model_path, trigger_imgs[:50],
                             out_copyrightImg_path, test_img)

    if show_robustness_finetune:
        print('ROBUSTENESS ANALYSIS: FINE TUNING ATTACK')
        fine_tuning_attack_analysis(dim_imgs)

    if show_robustness_pruning: