Example #1
0
    def transform_store_from_numpy(self,
                                   images,
                                   labels_values,
                                   labels_names,
                                   labels_classes=None,
                                   lmdb_dir='.data/',
                                   category='training',
                                   total_number_imgs=0,
                                   file_idx=None):

        create_if_not_exist(lmdb_dir)
        num_images = images.shape[0]
        lmdb_name = lmdb_dir + os.sep + '_{}'.format(category)
        if file_idx is None:
            index = 0
        else:
            index = file_idx * 10000
        # print('Storing ' + str(num_images) + lmdb_dir + '_{}'.format(category))
        if labels_classes is None:
            for idx, (image, latents_val) in tqdm(enumerate(
                    zip(images, labels_values)),
                                                  total=num_images):
                img = np.float32(image) / self.scaler

                labels_dict = {}
                for i, A in enumerate(labels_names):
                    labels_dict[A] = latents_val[i]

                self.store_single_lmdb(index=index,
                                       filename=lmdb_name,
                                       img=img,
                                       labels_dict=labels_dict,
                                       num_images=total_number_imgs)
                index = index + 1
        else:
            for idx, (image, latents_val, labels_class) in tqdm(
                    enumerate(zip(images, labels_values, labels_classes)),
                    total=num_images):
                img = np.float32(image) / self.scaler

                labels_dict = {}
                for i, A in enumerate(labels_names):
                    labels_dict[f'{A}_value'] = latents_val[i]
                    labels_dict[f'{A}_class'] = labels_class[i]

                self.store_single_lmdb(index=index,
                                       filename=lmdb_name,
                                       img=img,
                                       labels_dict=labels_dict,
                                       num_images=total_number_imgs)
                index = index + 1
Example #2
0
    def transform_store(self,
                        image_dir,
                        labels_fn,
                        lmdb_dir='.data/',
                        category='training',
                        target_size=None,
                        color_mode='rgb'):
        create_if_not_exist(lmdb_dir)

        classes = list(self.image_lists.keys())

        total_number_of_img = 0
        for label_name in classes:
            total_number_of_img += len(self.image_lists[label_name][category])
        print('Total number of imgs for catagory ' + str(total_number_of_img))
        num_class = len(classes)
        class2id = dict(zip(classes, range(len(classes))))
        id2class = dict((v, k) for k, v in class2id.items())

        lmdb_index = 0
        for label_name in classes:
            num_images = len(self.image_lists[label_name][category])
            print('Storing ' + str(num_images) + lmdb_dir + os.sep +
                  'into _{} from folder {}'.format(category, label_name))

            for index, _ in enumerate(self.image_lists[label_name][category]):
                img_path = get_file_path(self.image_lists, label_name, index,
                                         image_dir, category)

                img = img_to_array(load_img(img_path,
                                            grayscale=color_mode
                                            == 'grayscale',
                                            target_size=target_size),
                                   data_format=self.data_format) / self.scaler
                label_dict = labels_fn(img_path)
                name = lmdb_dir + os.sep + '_{}'.format(category)
                lmdb_index += 1
                self.store_single_lmdb(index=lmdb_index,
                                       filename=name,
                                       img=img,
                                       labels_dict=label_dict,
                                       num_images=total_number_of_img)