Пример #1
0
    def animate_effect(self,
                       current_point,
                       tensor_name="16x16/Conv0_up/weight"):
        print("Plotting animation for", tensor_name)
        # init
        net = self.getter.serverside_handler._Gs  # < ProgressiveGAN_Handler._Gs

        # prepare the ordering
        res = reconnector.dgb_get_res(net, tensor_name)
        print("got res value as", res)

        OVERALL_ORDER = self.tensor2fixed_order[
            tensor_name]  # these were prepared once to allow for smooth anim!

        print("OVERALL_ORDER", len(OVERALL_ORDER))
        print("debug OVERALL_ORDER", len(OVERALL_ORDER), min(OVERALL_ORDER),
              max(OVERALL_ORDER))
        #print("debug OVERALL_ORDER", OVERALL_ORDER)

        plot_row = list(np.arange(0.0, 2.0,
                                  0.01))  # + list(np.arange(2.0, 0.0, 0.1))
        print("plot_row", len(plot_row), " : ", plot_row)

        images = []
        names = []
        """
        for value_to_select in plot_row:
            # 1.0 == res
            end_val = int(value_to_select * res)
            FIXED_ORDER = OVERALL_ORDER[:end_val]
        """
        folder = "renders/ANIMATION/"
        if not os.path.exists(folder):
            os.mkdir(folder)
        saved_already = 0

        # real smooth turning on!
        for end_val in range(0, 1024, 2):  #range(len(OVERALL_ORDER))
            # 1.0 == res
            #end_val = int(value_to_select * res)
            FIXED_ORDER = OVERALL_ORDER[:end_val]
            print("--selected 0 to ", end_val, "getting in total",
                  len(FIXED_ORDER),
                  "of numbers from which we will create pairs")

            edited_net = reconnector.reconnect_DIRECT_ORDER(
                net, FIXED_ORDER, tensor_name)

            # generate image!
            latents = np.asarray([current_point])
            image = self.getter.latent_to_image_localServerSwitch(latents)

            #images.append(image)
            name = "_effect-" + str(end_val)

            filename = folder + "saved_" + str(saved_already).zfill(
                4) + name + ".png"
            saved_already += 1
            print("Saving in good quality as ", filename)
            cv2.imwrite(filename, image)

            # Restart the network afterwards!
            net = reconnector.restore_net(edited_net)

        print("SAVED ALL")
Пример #2
0
    def one_row_effectStrength_of_reconnector(
            self,
            current_point,
            tensor_name="16x16/Conv0_up/weight",
            all_name="all.png",
            save_individuals=False,
            save_rows=False,
            plot_row=[0, 0.5, 1.0, 2.0]):
        print("Plotting row for", tensor_name)
        # init
        net = self.getter.serverside_handler._Gs  # < ProgressiveGAN_Handler._Gs

        # prepare the ordering
        res = reconnector.dgb_get_res(net, tensor_name)
        print("got res value as", res)

        OVERALL_ORDER = self.tensor2fixed_order[
            tensor_name]  # these were prepared once to allow for smooth anim!

        print("OVERALL_ORDER", len(OVERALL_ORDER))
        print("debug OVERALL_ORDER", len(OVERALL_ORDER), min(OVERALL_ORDER),
              max(OVERALL_ORDER))
        #print("debug OVERALL_ORDER", OVERALL_ORDER)

        print("plot_row", len(plot_row), " : ", plot_row)

        images = []
        names = []

        for value_to_select in plot_row:
            # 1.0 == res
            end_val = int(value_to_select * res)
            FIXED_ORDER = OVERALL_ORDER[:end_val]

            edited_net = reconnector.reconnect_DIRECT_ORDER(
                net, FIXED_ORDER, tensor_name)

            # generate image!
            latents = np.asarray([current_point])
            image = self.getter.latent_to_image_localServerSwitch(latents)

            # Add text?
            include_texts = True
            if include_texts:
                font = cv2.FONT_HERSHEY_SIMPLEX
                bottomRightCornerOfText = (
                    int((1024 - 100 - 25) * self.font_multiplier),
                    int((1024 - 25) * self.font_multiplier)
                )  # usually 16x16 to 256x256
                topLeftCornerOfText = (int(25 * self.font_multiplier),
                                       int((75 + 5) * self.font_multiplier)
                                       )  # usually 0.0 to 4.0
                #bottomLeftCornerOfText = (10, 1000)
                fontScale = 2 * self.font_multiplier
                fontColor = (255, 255, 255)
                lineThickness = max(int(3 * self.font_multiplier), 1)

                cv2.putText(image, str(value_to_select),
                            bottomRightCornerOfText, font, fontScale,
                            fontColor, lineThickness)

                if value_to_select == 0.0:
                    n = tensor_name.split("/")[0]
                    cv2.putText(image, n, topLeftCornerOfText, font, fontScale,
                                fontColor, lineThickness)

            images.append(image)
            names.append("_effect-" + str(value_to_select))

            # Restart the network afterwards!
            net = reconnector.restore_net(edited_net)
        """
        if save_individuals:
            # Save images as a row:
            print("created", len(images), "images!")
            saved_already = 0
            for i, IMG in enumerate(images):
                message = "Saved file"
                folder = "DBG/"
                if not os.path.exists(folder):
                    os.mkdir(folder)
                filename = folder+"saved_" + str(saved_already).zfill(4) + names[i] + ".png"
                saved_already += 1
                print("Saving in good quality as ", filename)
                cv2.imwrite(filename, IMG)
            print("SAVED ALL")
        """

        row = self.concatenate_images_h(images)

        if save_rows:
            folder = "renders/PLOTS/"
            if not os.path.exists(folder):
                os.mkdir(folder)
            cv2.imwrite(folder + all_name, row)

        return row