コード例 #1
0
def load_split(data_root, split):
    Tools.print("load {} data".format(split))

    images_npz_file = os.path.join(data_root,
                                   "{}_images_png.npz".format(split))
    if os.path.exists(images_npz_file):
        Tools.print("exist {} and load".format(images_npz_file))
        _images = np.load(images_npz_file)['images']
    else:
        images_pkl_file = os.path.join(data_root,
                                       "{}_images_png.pkl".format(split))
        images_pkl = Tools.read_from_pkl(images_pkl_file)

        _images = np.zeros([len(images_pkl), 84, 84, 3], dtype=np.uint8)
        for ii, item in tqdm(enumerate(images_pkl), desc='decompress'):
            _images[ii] = cv2.imdecode(item, 1)
            pass
        np.savez(images_npz_file, images=_images)
        pass

    labels_npz_file = os.path.join(data_root, "{}_labels.npz".format(split))
    if os.path.exists(labels_npz_file):
        Tools.print("exist {} and load".format(labels_npz_file))
        _labels = np.load(labels_npz_file)['labels']
    else:
        labels_pkl_file = os.path.join(data_root,
                                       "{}_labels.pkl".format(split))
        labels_pkl = Tools.read_from_pkl(labels_pkl_file)
        _labels = labels_pkl["label_specific"]
        _labels = _labels - np.min(_labels)
        np.savez(labels_npz_file, labels=_labels)
        pass
    return _images, _labels
コード例 #2
0
        def get_one_feature(feature_dir,
                            data_list,
                            label_list,
                            label_fixed=None):
            features = Tools.read_from_pkl(feature_dir)
            label_dict, label_dict_count = {}, {}
            for feature in features:
                _, label, _, logits, l2norm = feature

                is_ok = False
                if self.config.class_id is None or label in self.config.class_id:  # 类别
                    if label not in label_dict:
                        label_dict[label] = len(label_dict.keys())
                    if self.config.sample is None:  # 采样
                        is_ok = True
                    else:
                        if label not in label_dict_count:  # 计数
                            label_dict_count[label] = 0
                        if label_dict_count[label] < self.config.sample:
                            label_dict_count[label] += 1
                            is_ok = True
                        pass
                    pass

                if is_ok:
                    data_list.append(
                        l2norm if self.config.is_l2norm else logits)
                    label_list.append(label_dict[label]
                                      if label_fixed is None else label_fixed)
                    pass

                pass
            return data_list, label_list
コード例 #3
0
def save_image(data_root, split, result_path):
    Tools.print("load {} data".format(split))
    images_pkl_file = os.path.join(data_root,
                                   "{}_images_png.pkl".format(split))
    labels_pkl_file = os.path.join(data_root, "{}_labels.pkl".format(split))
    images_pkl = Tools.read_from_pkl(images_pkl_file)
    labels_pkl = Tools.read_from_pkl(labels_pkl_file)

    for ii, item in tqdm(enumerate(images_pkl), desc='decompress'):
        image = cv2.imdecode(item, 1)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        result_file = Tools.new_dir(
            os.path.join(result_path, split,
                         str(labels_pkl["label_specific"][ii]),
                         "{}.png".format(ii)))
        Image.fromarray(image).save(result_file)
        pass

    pass
コード例 #4
0
    def __init__(self, data_list, num_way, num_shot, transform_train,
                 transform_test):
        self.data_list, self.num_way, self.num_shot = data_list, num_way, num_shot

        if Config.baseline_type == "cluster":
            cluster_data = Tools.read_from_pkl(Config.cluster_path)
            self.data_list, self.cluster_list = cluster_data[
                "info"], cluster_data["cluster"]
            pass

        self.transform_train, self.transform_test = transform_train, transform_test
        pass
コード例 #5
0
ファイル: main.py プロジェクト: alisure-ml/WordCloud-Demo
    def __init__(self,
                 data_file="",
                 stop_words_file="./source/chineseStopWords.txt"):
        self.data_file = data_file
        self.stop_words_file = stop_words_file

        self.data = Tools.read_from_pkl(self.data_file)
        self.data = [one[0]["d"] for one in self.data]
        self.data = [i for one in self.data for i in one]

        self.stop_words = list(set(self.get_stop_words() + list(STOPWORDS)))
        pass
コード例 #6
0
    def main(self):
        tsne = TSNE(n_components=2)

        Tools.print("begin to fit_transform {}".format(self.config.result_png))
        if os.path.exists(self.config.result_pkl):
            Tools.print("exist pkl, and now to load")
            result = Tools.read_from_pkl(self.config.result_pkl)
        else:
            Tools.print("not exist pkl, and now to fit")
            data, label = self.deal_feature()
            fit = tsne.fit_transform(data)
            result = {"fit": fit, "label": label}
            Tools.write_to_pkl(self.config.result_pkl, result)
            pass

        Tools.print("begin to embedding")
        fig = self.plot_embedding(result["fit"], result["label"])

        Tools.print("begin to save")
        plt.savefig(self.config.result_png)
        # plt.show(fig)
        pass
import os
import numpy as np
from glob import glob
from tqdm import tqdm
import scipy.io as scio
import xml.etree.ElementTree as ET
from alisuretool.Tools import Tools
from deal_data_0_global_info import get_data_root_path

if __name__ == '__main__':
    data_root = get_data_root_path()
    image_info_path = os.path.join(data_root, "deal", "image_info_list2.pkl")
    image_info_list = Tools.read_from_pkl(image_info_path)

    # [one for one in image_info_list if len(one["object"]) > 0]

    cat_list = {}
    cat_single_list = {}
    for image_info in image_info_list:
        for one in image_info["object"]:
            cat_name = one[-1]["cat_name"]

            if cat_name not in cat_list:
                cat_list[cat_name] = []
            if cat_name not in cat_single_list:
                cat_single_list[cat_name] = []

            if len(image_info["object"]) == 1:
                cat_single_list[cat_name].append(image_info)
            else:
                # Tools.print(image_info["image_path"])
コード例 #8
0
def read_image_info(image_info_root):
    image_info_path = os.path.join(image_info_root, "deal",
                                   "image_info_list_change_person2.pkl")
    # image_info_list = Tools.read_from_pkl(image_info_path)[::200]
    image_info_list = Tools.read_from_pkl(image_info_path)
    return image_info_list
import os
import numpy as np
from glob import glob
from tqdm import tqdm
from alisuretool.Tools import Tools
from deal_data_0_global_info import get_data_root_path


if __name__ == '__main__':
    data_root = get_data_root_path()
    image_root_path = os.path.join(data_root, "ILSVRC2017_DET/ILSVRC/Data/DET/train")

    label_info_path = os.path.join(data_root, "deal", "label_info_list.pkl")
    label_info_list = Tools.read_from_pkl(label_info_path)
    image_info_path = os.path.join(data_root, "deal", "image_info_list2.pkl")

    image_info_list = []
    for i, label_info in tqdm(enumerate(label_info_list), total=len(label_info_list)):
        if "2013" in label_info["source"]:
            source = os.path.join("{}_train".format(label_info["source"].replace("_", "")), label_info["folder"])
        else:
            source = label_info["folder"]
        image_path = os.path.join(image_root_path, source, "{}.JPEG".format(label_info["filename"]))

        if not os.path.exists(image_path):
            Tools.print(image_path)

        label_info["image_path"] = image_path
        image_info_list.append(label_info)
        pass
コード例 #10
0
 def get_data_info(data_root="/media/ubuntu/4T/ALISURE/Data/L2ID/data"):
     image_info_path = os.path.join(data_root, "deal",
                                    "image_info_list.pkl")
     image_info_list = Tools.read_from_pkl(image_info_path)
     return image_info_list
コード例 #11
0
    # dataset_name = "miniimagenet"
    # dataset_name = "tieredimagenet"
    dataset_name = MyDataset.dataset_name_omniglot

    data_root = MyDataset.get_data_root(dataset_name=dataset_name,
                                        is_png=is_png)
    Tools.print(data_root)

    features_save_path = Tools.new_dir("{}_feature".format(data_root))
    Tools.print(features_save_path)
    pass


if __name__ == '__main__':
    features_dict = Tools.read_from_pkl(
        os.path.join(Config.features_save_path, "train_features.pkl"))
    data_list, features = features_dict["info"], np.asarray(
        features_dict["feature"])

    k_means = KMeans(k=512)
    images_lists = k_means.cluster(features)

    cluster_result = np.zeros(shape=len(data_list), dtype=np.int)
    for cluster_id, cluster_image in enumerate(images_lists):
        cluster_result[cluster_image] = cluster_id
        pass
    Tools.write_to_pkl(
        os.path.join(Config.features_save_path, "train_cluster.pkl"), {
            "info": data_list,
            "cluster": cluster_result
        })
    def _eval_mlc_cam_2_inner_old(self, index, pkl_path):
        if index % 1000 == 0:
            Tools.print("now is {}".format(index))
            pass

        pkl_data = Tools.read_from_pkl(pkl_path)
        label_one = pkl_data["label"]
        image_path_one = pkl_data["image_path"]
        label_for_cam_one = pkl_data["label_for_cam"]
        cam_one = pkl_data["cam"]

        im = Image.open(image_path_one)
        image_size = im.size

        now_name = image_path_one.split("Data/DET/")[1]
        result_filename = Tools.new_dir(
            os.path.join(self.config.mlc_cam_dir, now_name))
        # 保存原图
        im.save(result_filename)

        # 预测结果, 对结果进行彩色可视化
        np_cam = np.zeros(shape=(self.config.mlc_num_classes + 1,
                                 image_size[1], image_size[0]))
        np_cam[0] = self.config.fg_thr * 255
        for label in label_for_cam_one:
            image_input = np.asarray(
                im.resize((self.config.mlc_size, self.config.mlc_size)))
            tensor_cam = torch.tensor(cam_one[label])
            # cam = torch.sigmoid(tensor_cam)
            norm_cam = torch.squeeze(
                self._feature_norm(torch.unsqueeze(tensor_cam, dim=0)))

            now_cam_im = Image.fromarray(
                np.asarray(norm_cam * 255,
                           dtype=np.uint8)).resize(size=image_size)
            now_cam_im.save(
                result_filename.replace(".JPEG", "_{}.bmp".format(label + 1)))
            np_cam[label + 1] = np.asarray(now_cam_im)

            cam_crf_one = self.torch_resize(norm_cam,
                                            size=(self.config.mlc_size,
                                                  self.config.mlc_size))
            cam_crf_one = CRFTool.crf(image_input,
                                      np.expand_dims(cam_crf_one, axis=0),
                                      t=5)
            now_cam_crf_im = Image.fromarray(
                np.asarray(cam_crf_one * 255,
                           dtype=np.uint8)).resize(size=image_size)
            now_cam_crf_im.save(
                result_filename.replace(".JPEG",
                                        "_crf_{}.bmp".format(label + 1)))

            # cam_cut_one = self.torch_resize(tensor_cam, size=(self.config.mlc_size, self.config.mlc_size))
            # cam_cut_one = self.grub_cut_mask(image_input, cam_cut_one)
            # now_cam_cut_im = Image.fromarray(np.asarray(cam_cut_one * 255, dtype=np.uint8)).resize(size=image_size)
            # now_cam_cut_im.save(result_filename.replace(".JPEG", "_{}_cut.bmp".format(label + 1)))
            pass

        cam_label = np.asarray(np.argmax(np_cam, axis=0), dtype=np.uint8)
        im_color = DataUtil.gray_to_color(cam_label).resize(
            size=image_size, resample=Image.NEAREST)
        im_color.save(result_filename.replace("JPEG", "png"))
        pass
import os
import numpy as np
from glob import glob
from tqdm import tqdm
from alisuretool.Tools import Tools
from deal_data_0_global_info import get_data_root_path, get_project_path


if __name__ == '__main__':
    data_root = get_data_root_path()
    image_info_path = os.path.join(data_root, "deal", "image_info_list2.pkl")
    person_pkl = os.path.join(data_root, "deal", "person2.pkl")
    result_image_info_path = os.path.join(data_root, "deal", "image_info_list_change_person2.pkl")

    image_info_list = Tools.read_from_pkl(image_info_path)
    person_info_list = Tools.read_from_pkl(person_pkl)

    result_image_info_list = []
    for i, (image_info, person_info) in tqdm(enumerate(zip(image_info_list, person_info_list)), total=len(image_info_list)):
        if not os.path.exists(image_info["image_path"]) or image_info["image_path"] != person_info[1]:
            Tools.print(image_info["image_path"])
            pass
        image_label = list(set([one[2] for one in image_info["object"]]+ ([124] if person_info[0] == 1 else [])))
        image_path = image_info["image_path"]
        result_image_info_list.append([image_label, image_path])
        pass

    Tools.write_to_pkl(_path=result_image_info_path, _data=result_image_info_list)
    pass
    def _eval_mlc_cam_2_inner(self, index, pkl_path):
        if index % 1000 == 0:
            Tools.print("now is {}".format(index))
            pass

        try:
            if not os.path.exists(pkl_path):
                return

            pkl_data = Tools.read_from_pkl(pkl_path)
            label_one = pkl_data["label"]
            image_path_one = pkl_data["image_path"]
            label_for_cam_one = pkl_data["label_for_cam"]
            cam_one = pkl_data["cam"]

            now_name = image_path_one.split("Data/DET/")[1]
            result_filename = Tools.new_dir(
                os.path.join(self.config.mlc_cam_dir, now_name))

            if os.path.exists(result_filename.replace("JPEG", "png")):
                return

            im = Image.open(image_path_one)
            im.save(result_filename)
            image_size = im.size

            # 预测结果, 对结果进行彩色可视化
            np_single_cam = 0
            np_cam = np.zeros(shape=(self.config.mlc_num_classes + 1,
                                     image_size[1], image_size[0]))
            np_cam[0] = self.config.fg_thr * 255  # 0.25
            for label in label_for_cam_one:
                cam_resize = [
                    self.torch_resize(
                        cam, (self.config.mlc_size, self.config.mlc_size))
                    for cam in cam_one[label]
                ]
                norm_cam = np.sum(cam_resize, axis=0) / len(cam_resize)
                norm_cam = norm_cam / np.max(norm_cam)

                now_cam_im = Image.fromarray(
                    np.asarray(norm_cam * 255,
                               dtype=np.uint8)).resize(size=image_size)
                now_cam_im.save(
                    result_filename.replace(".JPEG",
                                            "_{}.bmp".format(label + 1)))
                np_cam[label + 1] = np.asarray(now_cam_im)

                np_single_cam += np.asarray(now_cam_im, dtype=np.float)
                pass

            # cam_crf_one = CRFTool.crf_inference(np.asarray(im), np_cam / 255, t=5, n_label=len(np_cam))
            # cam_crf_one = np.asarray(np.argmax(cam_crf_one, axis=0), dtype=np.uint8)
            # now_cam_crf_im = DataUtil.gray_to_color(cam_crf_one)
            # now_cam_crf_im.save(result_filename.replace(".JPEG", "_crf.png"))

            cam_label = np.asarray(np.argmax(np_cam, axis=0), dtype=np.uint8)
            cam_label[cam_label == 0] = 255
            if len(label_for_cam_one) > 0:
                cam_label[(np_single_cam /
                           len(label_for_cam_one)) < self.config.bg_thr *
                          255] = 0  # 0.05
                pass
            im_color = DataUtil.gray_to_color(cam_label).resize(
                size=image_size, resample=Image.NEAREST)
            im_color.save(result_filename.replace("JPEG", "png"))
        except Exception():
            Tools.print("{} {}".format(index, pkl_path))
            pass
        pass
コード例 #15
0
 def get_data_info(data_root=None):
     data_root = data_root if data_root is not None else "/media/ubuntu/4T/ALISURE/Data/L2ID/data"
     image_info_path = os.path.join(data_root, "deal", "image_info_list2.pkl")
     image_info_list = Tools.read_from_pkl(image_info_path)
     return image_info_list
コード例 #16
0
 def get_ss_info_after_filter(pkl_root=None):
     _pkl_root = "/media/ubuntu/4T/ALISURE/USS/ConTa/pseudo_mask/result/2/sem_seg/train_ss.pkl"
     pkl_root = pkl_root if pkl_root is not None else _pkl_root
     Tools.print("Read pkl from {}".format(pkl_root))
     image_info_list = Tools.read_from_pkl(pkl_root)
     return image_info_list
    def _eval_mlc_cam_2_inner(self, index, pkl_path):
        if index % 1000 == 0:
            Tools.print("now is {}".format(index))
            pass

        try:
            pkl_data = Tools.read_from_pkl(pkl_path)
            label_one = pkl_data["label"]
            image_path_one = pkl_data["image_path"]
            label_for_cam_one = pkl_data["label_for_cam"]
            cam_one = pkl_data["cam"]

            im = Image.open(image_path_one)
            image_size = im.size

            now_name = image_path_one.split("Data/DET/")[1]
            result_filename = Tools.new_dir(
                os.path.join(self.config.mlc_cam_dir, now_name))
            # 保存原图
            # im.save(result_filename)

            # 预测结果, 对结果进行彩色可视化
            np_single_cam = 0
            np_cam = np.zeros(shape=(self.config.mlc_num_classes + 1,
                                     image_size[1], image_size[0]))
            for label in label_for_cam_one:
                image_input = np.asarray(
                    im.resize((self.config.mlc_size, self.config.mlc_size)))
                tensor_cam = torch.tensor(cam_one[label])
                norm_cam = torch.squeeze(
                    self._feature_norm(torch.unsqueeze(tensor_cam, dim=0)))

                now_cam_im = Image.fromarray(
                    np.asarray(norm_cam * 255,
                               dtype=np.uint8)).resize(size=image_size)
                # now_cam_im.save(result_filename.replace(".JPEG", "_{}.bmp".format(label + 1)))
                np_single_cam += np.asarray(now_cam_im, dtype=np.float)

                cam_crf_one = self.torch_resize(norm_cam,
                                                size=(self.config.mlc_size,
                                                      self.config.mlc_size))
                cam_crf_one = CRFTool.crf(image_input,
                                          np.expand_dims(cam_crf_one, axis=0),
                                          t=5)
                now_cam_crf_im = Image.fromarray(
                    np.asarray(cam_crf_one * 255,
                               dtype=np.uint8)).resize(size=image_size)
                # now_cam_crf_im.save(result_filename.replace(".JPEG", "_crf_{}.bmp".format(label + 1)))

                np_cam[label + 1] = np.asarray(now_cam_im) / 2 + np.asarray(
                    now_cam_crf_im) / 2
                pass

            np_cam[0] = self.config.fg_thr * 255  # 0.7
            cam_label = np.asarray(np.argmax(np_cam, axis=0), dtype=np.uint8)
            cam_label[cam_label == 0] = 255
            if len(label_for_cam_one) > 0:
                cam_label[(np_single_cam /
                           len(label_for_cam_one)) < self.config.bg_thr *
                          255] = 0  # 0.1
                pass

            im_color = DataUtil.gray_to_color(cam_label).resize(
                size=image_size, resample=Image.NEAREST)
            im_color.save(result_filename.replace("JPEG", "png"))
            pass
        except Exception():
            Tools.print("{} {}".format(index, pkl_path))
            pass
        pass