Ejemplo n.º 1
0
    def _save_images(self,
                     sess,
                     rec_imgs,
                     inputs,
                     labels,
                     is_training,
                     x,
                     y,
                     imgs,
                     step=None):
        """Save reconstructed images."""
        rec_imgs_ = sess.run(rec_imgs,
                             feed_dict={
                                 inputs: x,
                                 labels: y,
                                 is_training: False
                             })

        utils.save_imgs(real_imgs=imgs,
                        rec_imgs=rec_imgs_,
                        img_path=self.test_image_path,
                        database_name=self.cfg.DATABASE_NAME,
                        max_img_in_col=self.cfg.MAX_IMAGE_IN_COL,
                        step=step,
                        silent=True,
                        test_flag=True)
Ejemplo n.º 2
0
    def _save_images(self,
                     sess,
                     img_path,
                     x,
                     y,
                     imgs,
                     step,
                     silent=False,
                     epoch_i=None,
                     test_flag=False):
        """Save reconstructed images."""
        rec_images_ = sess.run(self.rec_imgs,
                               feed_dict={
                                   self.inputs: x,
                                   self.labels: y,
                                   self.is_training: False
                               })

        # rec_images_ shape: [128, 28, 28, 1] for mnist
        utils.save_imgs(real_imgs=imgs,
                        rec_imgs=rec_images_,
                        img_path=img_path,
                        database_name=self.cfg.DATABASE_NAME,
                        max_img_in_col=self.cfg.MAX_IMAGE_IN_COL,
                        step=step,
                        silent=silent,
                        epoch_i=epoch_i,
                        test_flag=test_flag)
Ejemplo n.º 3
0
    def _save_images_mo(self, sess, rec_imgs, inputs, labels, is_training,
                        preds_binary, preds_vector):
        """Save reconstructed images."""
        utils.thin_line()
        print('Getting reconstruction images...')
        if len(self.y_test) > self.cfg.MAX_IMAGE_IN_COL**2:
            n_test_img = self.cfg.MAX_IMAGE_IN_COL**2
            test_img_idx = np.random.choice(len(self.y_test), n_test_img)
        else:
            test_img_idx = list(range(len(self.y_test)))

        rec_imgs_ = []
        preds_vec_ = []

        if self.cfg.LABEL_FOR_TEST == 'pred':
            label_for_img = preds_binary
        elif self.cfg.LABEL_FOR_TEST == 'real':
            label_for_img = self.y_test
        else:
            raise ValueError('Wrong LABEL_FOR_TEST Name!')

        for x, y_hat, pred_ in tqdm(zip(self.x_test[test_img_idx],
                                        label_for_img[test_img_idx],
                                        preds_vector[test_img_idx]),
                                    total=len(test_img_idx),
                                    ncols=100,
                                    unit=' image'):
            # Get new x and y_hat list in which each y contain single object
            # [0, 1, 0, 1, 0] -> [[0, 1, 0, 0, 0],
            #                     [0, 0, 0, 1, 0]]
            x_new = []
            y_hat_new = []
            preds_vec_new = []
            for i, y_i in enumerate(y_hat):
                if y_i == 1:
                    y_hat_new_i = np.zeros_like(y_hat)
                    y_hat_new_i[i] = 1
                    assert y_hat_new_i[i] == y_hat[i]
                    x_new.append(x)
                    y_hat_new.append(y_hat_new_i)
                    preds_vec_new.append(pred_[i])
            preds_vec_.append(preds_vec_new)

            # Filling x and y tensor to batch size for testing
            # [[0, 1, 0, 0, 0],
            #  [0, 0, 0, 1, 0]] -> [[0, 1, 0, 0, 0],
            #                       [0, 0, 0, 1, 0],
            #                             ...
            #                       [0, 0, 0, 0, 0]]
            n_y = len(y_hat_new)
            assert n_y == int(np.sum(y_hat))
            if n_y > self.cfg.TEST_BATCH_SIZE:
                raise ValueError(
                    'TEST_BATCH_SIZE Must Not Less Than {}!'.format(n_y))
            if n_y < self.cfg.TEST_BATCH_SIZE:
                for i in range(self.cfg.TEST_BATCH_SIZE - n_y):
                    x_new = np.append(x_new,
                                      np.expand_dims(np.zeros_like(x), axis=0),
                                      axis=0)
                    y_hat_new.append(np.zeros_like(y_hat))
            assert len(x_new) == self.cfg.TEST_BATCH_SIZE
            assert len(y_hat_new) == self.cfg.TEST_BATCH_SIZE

            # Get remake images which contain different objects
            # y_rec_imgs_ shape: [128, 28, 28, 1] for mnist
            y_rec_imgs_ = sess.run(rec_imgs,
                                   feed_dict={
                                       inputs: x_new,
                                       labels: y_hat_new,
                                       is_training: False
                                   })
            rec_imgs_.append(y_rec_imgs_[:n_y])

        # Get colorful overlapped images
        real_imgs_ = utils.img_black_to_color(self.imgs_test[test_img_idx],
                                              same=True)
        rec_imgs_overlap = []
        rec_imgs_no_overlap = []
        for idx, imgs in enumerate(rec_imgs_):
            imgs_colored = utils.img_black_to_color(imgs)
            imgs_overlap = utils.img_add_overlap(
                imgs=imgs_colored,
                merge=True,
                vec=preds_vec_[idx],
                # vec=None,
                gamma=0)
            imgs_no_overlap = utils.img_add_no_overlap(
                imgs=imgs_colored,
                num_mul_obj=self.cfg.NUM_MULTI_OBJECT,
                vec=preds_vec_[idx],
                img_mode='RGB',
                resize_filter=Image.ANTIALIAS)
            rec_imgs_overlap.append(imgs_overlap)
            rec_imgs_no_overlap.append(imgs_no_overlap)
        rec_imgs_overlap = np.array(rec_imgs_overlap)
        rec_imgs_no_overlap = np.array(rec_imgs_no_overlap)

        # Save images
        utils.save_imgs(real_imgs=real_imgs_,
                        rec_imgs=rec_imgs_overlap,
                        img_path=self.test_image_path,
                        database_name=self.cfg.DATABASE_NAME,
                        max_img_in_col=self.cfg.MAX_IMAGE_IN_COL,
                        silent=False,
                        test_flag=True,
                        colorful=True,
                        append_info='_overlap')
        utils.save_imgs(real_imgs=real_imgs_,
                        rec_imgs=rec_imgs_no_overlap,
                        img_path=self.test_image_path,
                        database_name=self.cfg.DATABASE_NAME,
                        max_img_in_col=self.cfg.MAX_IMAGE_IN_COL,
                        silent=False,
                        test_flag=True,
                        colorful=True,
                        append_info='_no_overlap')