Exemple #1
0
def main():
    logger.warning("This script is aimed to demonstrate how to convert the "
                   "JSON file to a single image dataset.")
    logger.warning("It won't handle multiple JSON files to generate a "
                   "real-use dataset.")

    parser = argparse.ArgumentParser()
    parser.add_argument("json_file")
    parser.add_argument("-o", "--out", default=None)
    args = parser.parse_args()

    json_file = args.json_file

    if args.out is None:
        out_dir = osp.basename(json_file).replace(".", "_")
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    data = json.load(open(json_file))
    imageData = data.get("imageData")

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
        with open(imagePath, "rb") as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode("utf-8")
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {"_background_": 0}
    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                   label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name

    lbl_viz = imgviz.label2rgb(label=lbl,
                               img=imgviz.asgray(img),
                               label_names=label_names,
                               loc="rb")

    PIL.Image.fromarray(img).save(osp.join(out_dir, "img.png"))
    utils.lblsave(osp.join(out_dir, "label.png"), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, "label_viz.png"))

    with open(osp.join(out_dir, "label_names.txt"), "w") as f:
        for lbl_name in label_names:
            f.write(lbl_name + "\n")

    logger.info("Saved to: {}".format(out_dir))
Exemple #2
0
def main():
    logger.warning('This script is aimed to demonstrate how to convert the'
                   'JSON file to a single image dataset, and not to handle'
                   'multiple JSON files to generate a real-use dataset.')

    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()

    json_file = args.json_file

    if args.out is None:
        out_dir = osp.basename(json_file).replace('.', '_')
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {'_background_': 0}
    for shape in sorted(data['shapes'], key=lambda x: x['label']):
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)

    PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
    utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

    with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
        for lbl_name in label_names:
            f.write(lbl_name + '\n')

    logger.warning('info.yaml is being replaced by label_names.txt')
    info = dict(label_names=label_names)
    with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
        yaml.safe_dump(info, f, default_flow_style=False)

    logger.info('Saved to: {}'.format(out_dir))
Exemple #3
0
    def Login(self):
        if (self.Lin_l.text() == "123") and (self.Lin_p.text() == "456"):
            global AccountFlag
            AccountFlag = True
        if (AccountFlag == True):
            global userid
            userid = self.Lin_l.text()  # 将正确的userid保存到全局变量中
            glo.set_value("userid", userid)
            self.close()  # 关闭登陆界面
            # main()
            # app.setApplicationName(__appname__)
            # app.setWindowIcon(newIcon('icon'))
            # app.installTranslator(translator)
            win = MainWindow(
                config=config,
                filename=filename,
                output_file=output_file,
                output_dir=output_dir,
            )

            if reset_config:
                logger.info('Resetting Qt config: %s' %
                            win.settings.fileName())
                win.settings.clear()
                sys.exit(0)

            win.show()  # 标注界面
            win.raise_()  # 设置控件在最上层
            # sys.exit(app.exec_())
            # print("用户已登录,跳转至主界面")
        else:
            print('用户名与密码不匹配,请重新输入!')
Exemple #4
0
def main():
    logger.warning('This script is aimed to demonstrate how to convert the'
                   'JSON file to a single image dataset, and not to handle'
                   'multiple JSON files to generate a real-use dataset.')

    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()

    json_file = args.json_file

    if args.out is None:
        out_dir = osp.basename(json_file).replace('.', '_')
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {'_background_': 0}
    for shape in sorted(data['shapes'], key=lambda x: x['label']):
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)

    PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
    utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

    with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
        for lbl_name in label_names:
            f.write(lbl_name + '\n')

    logger.warning('info.yaml is being replaced by label_names.txt')
    info = dict(label_names=label_names)
    with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
        yaml.safe_dump(info, f, default_flow_style=False)

    logger.info('Saved to: {}'.format(out_dir))
    def validateEpoch(self, epoch, epoch_time, validate_params):
        self.checkTrainingAborted(epoch)
        if self.val_data and not (epoch + 1) % self.args.val_interval:
            logger.debug('validate: {}'.format(epoch + 1))
            self.thread.data.emit({
                'validation': {
                    _('Validating...'): '',
                },
                'progress': {
                    'speed': 0,
                }
            })

            map_name, mean_ap = self.validate(**validate_params)
            val_msg = '\n'.join(
                ['{}={}'.format(k, v) for k, v in zip(map_name, mean_ap)])
            logger.info('[Epoch {}] Validation [{:.3f}sec]: \n{}'.format(
                epoch, epoch_time, val_msg))
            current_mAP = float(mean_ap[-1])

            val_data = {'validation': {}}
            for i, name in enumerate(map_name[:]):
                val_data['validation'][name] = mean_ap[i]
            self.thread.data.emit(val_data)

            # Early Stopping
            self.monitor.update(epoch, mean_ap[-1])
            if self.monitor.shouldStopEarly():
                raise AbortTrainingException(epoch)
        else:
            current_mAP = 0.
        return current_mAP
Exemple #6
0
def json2img(json_file, out, index):
    if out is None:
        out_dir = osp.basename(json_file).replace(".", "_")
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = out
    mkdir(out_dir)
    data = json.load(open(json_file))
    imageData = data.get("imageData")

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
        with open(imagePath, "rb") as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode("utf-8")
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {"_background_": 0}
    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                   label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name

    lbl_viz = imgviz.label2rgb(label=lbl,
                               img=imgviz.asgray(img),
                               label_names=label_names,
                               loc="rb")
    img_name = str(index) + '.jpg'
    out_dir_raw = out_dir + '/raw'
    out_dir_label = out_dir + '/label'
    mkdir(out_dir_raw)
    mkdir(out_dir_label)
    PIL.Image.fromarray(img).save(osp.join(out_dir_raw, img_name))  #保存图片 1.jpg
    utils.lblsave(osp.join(out_dir_label, img_name.replace('.jpg', '.png')),
                  lbl)  #保存标签 1.png
    #PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, "label_viz.png"))

    with open(osp.join(out_dir, "label_names.txt"), "w") as f:
        for lbl_name in label_names:
            f.write(lbl_name + "\n")

    logger.info("Saved to: {}".format(out_dir))
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("label_png", help="label PNG file")
    args = parser.parse_args()

    lbl = np.asarray(PIL.Image.open(args.label_png))

    logger.info("label shape: {}".format(lbl.shape))
    logger.info("unique label values: {}".format(np.unique(lbl)))

    lbl_viz = imgviz.label2rgb(lbl)
    plt.imshow(lbl_viz)
    plt.show()
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('label_png', help='label PNG file')
    args = parser.parse_args()

    lbl = np.asarray(PIL.Image.open(args.label_png))

    logger.info('label shape: {}'.format(lbl.shape))
    logger.info('unique label values: {}'.format(np.unique(lbl)))

    lbl_viz = utils.draw_label(lbl)
    plt.imshow(lbl_viz)
    plt.show()
 def on_message(self, title, message, kind=None):
     log_msg = 'Show message: {}'.format(message)
     mb = QtWidgets.QMessageBox
     if kind == MessageType.Warning:
         logger.warn(log_msg)
         mb.warning(self, title, message)
     elif kind == MessageType.Error:
         logger.error(log_msg)
         mb.critical(self, title, message)
     elif kind == MessageType.Question:
         logger.info(log_msg)
         mb.question(self, title, message)
     else:
         logger.info(log_msg)
         mb.information(self, title, message)
Exemple #10
0
def get_config(config_from_args=None, config_file=None):
    # Configuration load order:
    #
    #   1. default config (lowest priority)
    #   2. config file passed by command line argument or ~/.labelmerc
    #   3. command line argument (highest priority)

    # 1. default config
    config = get_default_config()
    logger.info('从默认配置文件配置')

    # 2. config from yaml file
    if config_file is not None and osp.exists(config_file):
        logger.info('从指定配置文件配置')
        with open(config_file) as f:
            user_config = yaml.safe_load(f) or {}
        update_dict(config, user_config, validate_item=validate_config_item)

    # 3. command line argument
    if config_from_args is not None:
        logger.info('从命令行指令中配置')
        update_dict(config,
                    config_from_args,
                    validate_item=validate_config_item)

    return config
Exemple #11
0
def get_config(config_file_or_yaml=None, config_from_args=None):
    # 1. default config
    config = get_default_config()

    # 2. specified as file or yaml
    if config_file_or_yaml is not None:
        config_from_yaml = yaml.safe_load(config_file_or_yaml)
        if not isinstance(config_from_yaml, dict):
            with open(config_from_yaml) as f:
                logger.info(
                    'Loading config file from: {}'.format(config_from_yaml)
                )
                config_from_yaml = yaml.safe_load(f)
        update_dict(config, config_from_yaml,
                    validate_item=validate_config_item)

    # 3. command line argument or specified config file
    if config_from_args is not None:
        update_dict(config, config_from_args,
                    validate_item=validate_config_item)

    return config
    def beforeTrain(self):
        # Save config & architecture before training
        from labelme.config import Training
        config_file = os.path.join(self.output_folder,
                                   Training.config('config_file'))
        files = list(self.files.values())
        self.saveConfig(config_file, files)

        if self.args.early_stop_epochs > 0:
            self.monitor = NetworkMonitor(self.args.early_stop_epochs)

        self.thread.data.emit({
            'validation': {
                _('Waiting for first validation ...'): '',
            },
        })

        # Set maximum progress to 100%
        self.thread.update.emit(_('Start training ...'), 0, 100)

        self.checkAborted()
        logger.info('Start training from [Epoch {}]'.format(
            self.args.start_epoch))
    def afterBatch(self, batch_idx, epoch, num_batches, learning_rate, speed,
                   metrics):
        i = batch_idx
        if self.args.log_interval and not (i + 1) % self.args.log_interval:
            log_msg = '[Epoch {}/{}][Batch {}/{}], LR: {:.2E}, Speed: {:.3f} samples/sec'.format(
                epoch + 1, self.args.epochs, i + 1, num_batches, learning_rate,
                speed)
            update_msg = '{}\n{} {}, {} {}/{}, {}: {:.3f} {}\n'.format(
                _('Training ...'), _('Epoch'), epoch + 1, _('Batch'), i + 1,
                num_batches, _('Speed'), speed, _('samples/sec'))
            progress_metrics = {}
            for metric in metrics:
                name, loss = metric.get()
                msg = ', {}={:.3f}'.format(name, loss)
                log_msg += msg
                update_msg += msg
                progress_metrics[name] = loss
            logger.info(log_msg)

            self.thread.data.emit({
                'progress': {
                    'epoch': epoch + 1,
                    'epoch_max': self.args.epochs,
                    'batch': i + 1,
                    'batch_max': num_batches,
                    'speed': speed,
                    'metric': progress_metrics
                },
            })

            percent = math.ceil(
                (epoch / self.args.epochs +
                 batch_idx / num_batches / self.args.epochs) * 100)
            self.thread.update.emit(update_msg, percent, -1)

        self.checkTrainingAborted(epoch)
Exemple #14
0
    def save(
        self,
        filename,
        shapes,
        imagePath,
        imageHeight,
        imageWidth,
        imageData=None,
        otherData=None,
        flags=None,
        relationships=None,
    ):
        logger.info("Saving json...")

        try:
            if imageData is not None:
                imageData = base64.b64encode(imageData).decode("utf-8")
                imageHeight, imageWidth = self._check_image_height_and_width(
                    imageData, imageHeight, imageWidth)
            if otherData is None:
                otherData = {}
            if flags is None:
                flags = {}
            data = dict(
                version=__version__,
                flags=flags,
                shapes=shapes,
                imagePath=imagePath,
                imageData=imageData,
                imageHeight=imageHeight,
                imageWidth=imageWidth,
                relationships=relationships,
            )
            for key, value in otherData.items():
                assert key not in data
                data[key] = value
        except Exception as e:
            logger.info(e, exc_info=True)

        try:
            with open(filename, "w") as f:
                json.dump(data, f, ensure_ascii=False, indent=2)
            self.filename = filename
            logger.info("Saved json.")
        except Exception as e:
            raise LabelFileError(e)
Exemple #15
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--version', '-V', action='store_true', help='show version'
    )
    parser.add_argument(
        '--reset-config', action='store_true', help='reset qt config'
    )
    parser.add_argument(
        '--logger-level',
        default='info',
        choices=['debug', 'info', 'warning', 'fatal', 'error'],
        help='logger level',
    )
    parser.add_argument('filename', nargs='?', help='image or label filename')
    parser.add_argument(
        '--output',
        '-O',
        '-o',
        help='output file or directory (if it ends with .json it is '
             'recognized as file, else as directory)'
    )
    default_config_file = os.path.join(os.path.expanduser('~'), '.labelmerc')
    parser.add_argument(
        '--config',
        dest='config_file',
        help='config file (default: %s)' % default_config_file,
        default=default_config_file,
    )
    # config for the gui
    parser.add_argument(
        '--nodata',
        dest='store_data',
        action='store_false',
        help='stop storing image data to JSON file',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--autosave',
        dest='auto_save',
        action='store_true',
        help='auto save',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--nosortlabels',
        dest='sort_labels',
        action='store_false',
        help='stop sorting labels',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--flags',
        help='comma separated list of flags OR file containing flags',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--labelflags',
        dest='label_flags',
        help='yaml string of label specific flags OR file containing json '
             'string of label specific flags (ex. {person-\d+: [male, tall], '
             'dog-\d+: [black, brown, white], .*: [occluded]})',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--labels',
        help='comma separated list of labels OR file containing labels',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--validatelabel',
        dest='validate_label',
        choices=['exact', 'instance'],
        help='label validation types',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--keep-prev',
        action='store_true',
        help='keep annotation of previous frame',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--epsilon',
        type=float,
        help='epsilon to find nearest vertex on canvas',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--debug-mode',
        dest='debug_mode',
        action='store_true',
        help='start in debug mode',
        default=argparse.SUPPRESS,
    )
    args = parser.parse_args()

    if args.version:
        print('{0} {1}'.format(__appname__, __version__))
        sys.exit(0)

    if hasattr(args, 'debug_mode'):
        logger.addStreamHandler(logging.DEBUG)
    else:
        level = args.logger_level.upper()
        logger.addStreamHandler(getattr(logging, level))

    if hasattr(args, 'flags'):
        if os.path.isfile(args.flags):
            with codecs.open(args.flags, 'r', encoding='utf-8') as f:
                args.flags = [l.strip() for l in f if l.strip()]
        else:
            args.flags = [l for l in args.flags.split(',') if l]

    if hasattr(args, 'labels'):
        if os.path.isfile(args.labels):
            with codecs.open(args.labels, 'r', encoding='utf-8') as f:
                args.labels = [l.strip() for l in f if l.strip()]
        else:
            args.labels = [l for l in args.labels.split(',') if l]

    if hasattr(args, 'label_flags'):
        if os.path.isfile(args.label_flags):
            with codecs.open(args.label_flags, 'r', encoding='utf-8') as f:
                args.label_flags = yaml.load(f)
        else:
            args.label_flags = yaml.load(args.label_flags)

    config_from_args = args.__dict__
    config_from_args.pop('version')
    reset_config = config_from_args.pop('reset_config')
    filename = config_from_args.pop('filename')
    output = config_from_args.pop('output')
    config_file = config_from_args.pop('config_file')
    config = get_config(config_from_args, config_file)

    # localization
    current_path = os.path.dirname(os.path.abspath(__file__))
    locale_dir = os.path.join(current_path, 'locale')
    if os.path.isfile(os.path.join(locale_dir, config['language'], 'LC_MESSAGES', 'labelme.po')):
        lang = gettext.translation('labelme', localedir=locale_dir, languages=[config['language']])
        lang.install()
    else:
        gettext.install('labelme')
    locale.setlocale(locale.LC_ALL, config['language'])

    if not config['labels'] and config['validate_label']:
        logger.error('--labels must be specified with --validatelabel or '
                     'validate_label: true in the config file '
                     '(ex. ~/.labelmerc).')
        sys.exit(1)

    output_file = None
    output_dir = None
    if output is not None:
        if output.endswith('.json'):
            output_file = output
        else:
            output_dir = output

    # MXNet environment variables
    if 'MXNET_GPU_MEM_POOL_TYPE' in os.environ:
        logger.debug('Environment variable MXNET_GPU_MEM_POOL_TYPE = {}'.format(os.environ['MXNET_GPU_MEM_POOL_TYPE']))
    if 'MXNET_CUDNN_AUTOTUNE_DEFAULT' in os.environ:
        logger.debug('Environment variable MXNET_CUDNN_AUTOTUNE_DEFAULT = {}'.format(os.environ['MXNET_CUDNN_AUTOTUNE_DEFAULT']))
    if 'MXNET_HOME' in os.environ:
        logger.debug('Environment variable MXNET_HOME = {}'.format(os.environ['MXNET_HOME']))

    # # Qt environment variable
    # if 'QT_AUTO_SCREEN_SCALE_FACTOR' in os.environ:
    #     logger.debug('Environment variable QT_AUTO_SCREEN_SCALE_FACTOR = {}'.format(os.environ['QT_AUTO_SCREEN_SCALE_FACTOR']))

    # Enable high dpi for 4k monitors
    QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    if hasattr(QtWidgets.QStyleFactory, 'AA_UseHighDpiPixmaps'):
        QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps)
    
    app = QtWidgets.QApplication(sys.argv)
    app.setApplicationName(__appname__)
    app.setWindowIcon(newIcon('digivod'))
    win = MainWindow(
        config=config,
        filename=filename,
        output_file=output_file,
        output_dir=output_dir,
    )

    if reset_config:
        logger.info('Resetting Qt config: %s' % win.settings.fileName())
        win.settings.clear()
        sys.exit(0)

    win.showMaximized()
    win.raise_()
    sys.excepthook = excepthook
    win.check_startup()
    sys.exit(app.exec_())
Exemple #16
0
    def train(self):
        self.net.collect_params().reset_ctx(self.ctx)
        num_batches = self.args.num_samples // self.args.batch_size

        trainer = gluon.Trainer(self.net.collect_params(),
                                'sgd', {
                                    'learning_rate': self.args.learning_rate,
                                    'wd': self.args.wd,
                                    'momentum': self.args.momentum
                                },
                                update_on_kvstore=(None))

        # Learning rate decay policy
        lr_decay = float(self.args.lr_decay)
        lr_steps = sorted([
            float(ls) for ls in self.args.lr_decay_epoch.split(',')
            if ls.strip()
        ])

        # Losses
        mbox_loss = gcv.loss.SSDMultiBoxLoss()
        ce_metric = mx.metric.Loss('CrossEntropy')
        smoothl1_metric = mx.metric.Loss('SmoothL1')

        best_map = [0.]

        # Epoch loop
        for epoch in range(self.args.start_epoch, self.args.epochs):
            # Batch size can vary from epoch to epoch +/-1
            num_batches = len(self.train_data)

            self.beforeEpoch(epoch, num_batches=num_batches)

            while lr_steps and epoch >= lr_steps[0]:
                new_lr = trainer.learning_rate * lr_decay
                lr_steps.pop(0)
                trainer.set_learning_rate(new_lr)
                logger.info('[Epoch {}] Set learning rate to {}'.format(
                    epoch, new_lr))
            ce_metric.reset()
            smoothl1_metric.reset()

            tic = time.time()
            btic = time.time()
            self.net.hybridize(static_alloc=True, static_shape=True)

            # Batch loop
            for i, batch in enumerate(self.train_data):
                self.beforeBatch(i, epoch, num_batches)

                batch_size = batch[0].shape[0]
                data = gluon.utils.split_and_load(batch[0],
                                                  ctx_list=self.ctx,
                                                  batch_axis=0)
                cls_targets = gluon.utils.split_and_load(batch[1],
                                                         ctx_list=self.ctx,
                                                         batch_axis=0)
                box_targets = gluon.utils.split_and_load(batch[2],
                                                         ctx_list=self.ctx,
                                                         batch_axis=0)

                with autograd.record():
                    cls_preds = []
                    box_preds = []
                    for x in data:
                        cls_pred, box_pred, foo = self.net(x)
                        cls_preds.append(cls_pred)
                        box_preds.append(box_pred)
                    sum_loss, cls_loss, box_loss = mbox_loss(
                        cls_preds, box_preds, cls_targets, box_targets)
                    autograd.backward(sum_loss)

                trainer.step(1)
                ce_metric.update(0, [l * batch_size for l in cls_loss])
                smoothl1_metric.update(0, [l * batch_size for l in box_loss])

                speed = batch_size / (time.time() - btic)
                self.afterBatch(i,
                                epoch,
                                num_batches,
                                trainer.learning_rate,
                                speed,
                                metrics=[ce_metric, smoothl1_metric])
                btic = time.time()

            current_mAP = self.validateEpoch(
                epoch,
                epoch_time=(time.time() - tic),
                validate_params={'static_shape': True})
            self.saveParams(best_map, current_mAP, epoch)

            self.afterEpoch(epoch)

        return epoch
def json_to_dataset(json_file: str, out_dir: str, rename: bool):
    """
    JSON文件转换为DataSet格式文件夹

    @param {str} json_file - 要处理的JSON文件
    @param {str} out_dir - 输出目录
    @param {bool} rename - 遇到输出目录存在是否修改文件名
    """
    _json_path = osp.split(json_file)[0]
    _dir_name = osp.split(json_file)[1].replace(".", "_")
    if out_dir == '':
        _out_dir = osp.join(_json_path, _dir_name)
    else:
        _out_dir = osp.join(out_dir, _dir_name)

    if not osp.exists(_out_dir):
        os.makedirs(_out_dir)
    elif rename:
        # 遇到文件存在的情况,重命名
        _index = 1
        while osp.exists(_out_dir):
            _out_dir = osp.join(_json_path,
                                '%s%d%s' % (_dir_name[0:-5], _index, '_json'))
            _index += 1

        os.makedirs(_out_dir)

    data = json.load(open(json_file))
    imageData = data.get("imageData")

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
        with open(imagePath, "rb") as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode("utf-8")
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {"_background_": 0}

    # 修正json_to_dataset的bug, 按形状名进行排序,将hole放在最后面
    data["shapes"].sort(
        key=lambda x: x["label"] if x["label"] != "hole" else "zzzzzz")
    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                   label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name

    lbl_viz = imgviz.label2rgb(label=lbl,
                               img=imgviz.asgray(img),
                               label_names=label_names,
                               loc="rb")

    PIL.Image.fromarray(img).save(osp.join(_out_dir, "img.png"))
    utils.lblsave(osp.join(_out_dir, "label.png"), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(_out_dir, "label_viz.png"))

    with open(osp.join(_out_dir, "label_names.txt"), "w") as f:
        for lbl_name in label_names:
            f.write(lbl_name + "\n")

    logger.info("Saved to: {}".format(_out_dir))
Exemple #18
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("json_file")
    parser.add_argument("-o", "--out", default=None)
    args = parser.parse_args()

    json_file = args.json_file

    if args.out is None:
        out_dir = osp.basename(json_file).replace(".", "_")
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    data = json.load(open(json_file))
    imageData = data.get("imageData")

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
        with open(imagePath, "rb") as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode("utf-8")
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {
        "_background_": 0,
        "table": 1,
        "cup": 2,
        "bottle": 3,
        "glass": 4,
        "fork": 5,
        "knife": 6,
        "food": 7,
        "plate": 8
    }
    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                   label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name

    lbl_viz = imgviz.label2rgb(label=lbl,
                               img=imgviz.asgray(img),
                               label_names=label_names,
                               loc="rb")

    PIL.Image.fromarray(img).save(osp.join(out_dir, "img.png"))
    utils.lblsave(osp.join(out_dir, "label.png"), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, "label_viz.png"))

    with open(osp.join(out_dir, "label_names.txt"), "w") as f:
        for lbl_name in label_names:
            f.write(lbl_name + "\n")

    logger.info("Saved to: {}".format(out_dir))
Exemple #19
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--version",
                        "-V",
                        action="store_true",
                        help="show version")
    parser.add_argument("--reset-config",
                        action="store_true",
                        help="reset qt config")
    parser.add_argument(
        "--logger-level",
        default="info",
        choices=["debug", "info", "warning", "fatal", "error"],
        help="logger level",
    )
    parser.add_argument("filename", nargs="?", help="image or label filename")
    parser.add_argument(
        "--output",
        "-O",
        "-o",
        help="output file or directory (if it ends with .json it is "
        "recognized as file, else as directory)",
    )
    default_config_file = os.path.join(os.path.expanduser("~"), ".labelmerc")
    parser.add_argument(
        "--config",
        dest="config",
        help="config file or yaml-format string (default: {})".format(
            default_config_file),
        default=default_config_file,
    )
    # config for the gui
    parser.add_argument(
        "--nodata",
        dest="store_data",
        action="store_false",
        help="stop storing image data to JSON file",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--autosave",
        dest="auto_save",
        action="store_true",
        help="auto save",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--nosortlabels",
        dest="sort_labels",
        action="store_false",
        help="stop sorting labels",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--flags",
        help="comma separated list of flags OR file containing flags",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--labelflags",
        dest="label_flags",
        help=r"yaml string of label specific flags OR file containing json "
        r"string of label specific flags (ex. {person-\d+: [male, tall], "
        r"dog-\d+: [black, brown, white], .*: [occluded]})",  # NOQA
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--labels",
        help="comma separated list of labels OR file containing labels",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--validatelabel",
        dest="validate_label",
        choices=["exact"],
        help="label validation types",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--keep-prev",
        action="store_true",
        help="keep annotation of previous frame",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--epsilon",
        type=float,
        help="epsilon to find nearest vertex on canvas",
        default=argparse.SUPPRESS,
    )
    args = parser.parse_args()

    if os.name == 'posix':
        st = os.stat('./script.sh')
        os.chmod('./script.sh', st.st_mode | stat.S_IEXEC)

    if args.version:
        print("{0} {1}".format(__appname__, __version__))
        sys.exit(0)

    logger.setLevel(getattr(logging, args.logger_level.upper()))

    if hasattr(args, "flags"):
        if os.path.isfile(args.flags):
            with codecs.open(args.flags, "r", encoding="utf-8") as f:
                args.flags = [line.strip() for line in f if line.strip()]
        else:
            args.flags = [line for line in args.flags.split(",") if line]

    if hasattr(args, "labels"):
        if os.path.isfile(args.labels):
            with codecs.open(args.labels, "r", encoding="utf-8") as f:
                args.labels = [line.strip() for line in f if line.strip()]
        else:
            args.labels = [line for line in args.labels.split(",") if line]

    if hasattr(args, "label_flags"):
        if os.path.isfile(args.label_flags):
            with codecs.open(args.label_flags, "r", encoding="utf-8") as f:
                args.label_flags = yaml.safe_load(f)
        else:
            args.label_flags = yaml.safe_load(args.label_flags)

    config_from_args = args.__dict__
    config_from_args.pop("version")
    reset_config = config_from_args.pop("reset_config")
    filename = config_from_args.pop("filename")
    output = config_from_args.pop("output")
    config_file_or_yaml = config_from_args.pop("config")
    config = get_config(config_file_or_yaml, config_from_args)

    if not config["labels"] and config["validate_label"]:
        logger.error("--labels must be specified with --validatelabel or "
                     "validate_label: true in the config file "
                     "(ex. ~/.labelmerc).")
        sys.exit(1)

    output_file = None
    output_dir = None
    if output is not None:
        if output.endswith(".json"):
            output_file = output
        else:
            output_dir = output

    translator = QtCore.QTranslator()
    translator.load(
        QtCore.QLocale.system().name(),
        osp.dirname(osp.abspath(__file__)) + "/translate",
    )
    app = QtWidgets.QApplication(sys.argv)
    app.setApplicationName(__appname__)
    app.setWindowIcon(newIcon("icon"))
    app.installTranslator(translator)
    win = MainWindow(
        config=config,
        filename=filename,
        output_file=output_file,
        output_dir=output_dir,
    )

    if reset_config:
        logger.info("Resetting Qt config: %s" % win.settings.fileName())
        win.settings.clear()
        sys.exit(0)

    win.show()
    win.raise_()
    app.exec_()
    win.terminate_subprocess()
    sys.exit(1)
Exemple #20
0
def main():
    logger.warning('This script is aimed to convert the '
                   'JSON batch to gray map of DABNet format.')

    parser = argparse.ArgumentParser()
    parser.add_argument('--json-list', default=None)
    parser.add_argument('--label-file', default=None)
    args = parser.parse_args()

    # Load .json from list file
    if not osp.isfile(args.json_list):
        print("json_list doesn't existed!!")
        return

    with open(args.json_list, 'r') as f:
        json_files = f.readlines()
    json_files = [x.strip() for x in json_files]

    # Import label file
    if not osp.isfile(args.label_file):
        print("label_file doesn't existed!!")
        return
    label_name_to_value = importLabel(args.label_file)

    # main loop
    for i in range(0, len(json_files)):
        json_file = ''.join(json_files[i])

        out_dir = json_file.split('.')[0]

        data = json.load(open(json_file))
        imageData = data.get('imageData')

        if not imageData:
            imagePath = os.path.join(os.path.dirname(json_file),
                                     data['imagePath'])
            with open(imagePath, 'rb') as f:
                imageData = f.read()
                imageData = base64.b64encode(imageData).decode('utf-8')
        img = utils.img_b64_to_arr(imageData)

        # check label in json is in the label file or not
        for shape in sorted(data['shapes'], key=lambda x: x['label']):
            label_name = shape['label']
            if label_name in label_name_to_value:
                label_value = label_name_to_value[label_name]
            else:
                label_value = len(label_name_to_value)
                label_name_to_value[label_name] = label_value
                print(label_name, " is not in the label file")

        lbl, _ = utils.shapes_to_label(img.shape, data['shapes'],
                                       label_name_to_value)
        label_names = [None] * (max(label_name_to_value.values()) + 1)
        for name, value in label_name_to_value.items():
            label_names[value] = name

        lbl_viz = imgviz.label2rgb(label=lbl,
                                   img=imgviz.asgray(img),
                                   label_names=label_names,
                                   loc='rb')

        # PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
        utils.lblsave_gray(out_dir + '.png', lbl)
        PIL.Image.fromarray(lbl_viz).save(out_dir + '_viz.png')

        logger.info('Saved to: {}'.format(out_dir))
Exemple #21
0
def main():
    # Only input:
    # Give a folder with only .json files
    label_path = r"/Users/frederikrogalski/Documents/Privates/Programieren/python/trainseg/data/trainseg/Masks/"

    list_path = os.listdir(label_path)
    for i in range(0, len(list_path)):
            logger.warning('This script is aimed to demonstrate how to convert the'
                           'JSON file to a single image dataset, and not to handle'
                           'multiple JSON files to generate a real-use dataset.')

            parser = argparse.ArgumentParser()
            parser.add_argument('--json_file')
            parser.add_argument('-o', '--out', default=None)
            args = parser.parse_args()

            json_file = label_path + list_path[i]
            print(list_path[i])
            if args.out is None:
                out_dir = osp.basename(json_file).replace('.', '_')  # Return file name
                out_dir = osp.join(osp.dirname(json_file), out_dir)  # Combine directory and file name into one path
            else:
                out_dir = args.out
            if not osp.exists(out_dir):
                os.mkdir(out_dir)  # Used to create directories in digital permission mode

            data = json.load(open(json_file))
            imageData = data.get('imageData')

            if not imageData:
                imagePath = os.path.join(os.path.dirname(json_file), data['imagePath']) # os.path.dirname returns the file path
                with open(imagePath, 'rb') as f:
                    imageData = f.read()
                    imageData = base64.b64encode(imageData).decode('utf-8')
            img = utils.img_b64_to_arr(imageData)

            label_name_to_value = {'_background_': 0}
            for shape in sorted(data['shapes'], key=lambda x: x['label']):
                label_name = shape['label']
                if label_name in label_name_to_value:
                    label_value = label_name_to_value[label_name]
                else:
                    label_value = len(label_name_to_value)
                    label_name_to_value[label_name] = label_value
            lbl, _ = utils.shapes_to_label(
                img.shape, data['shapes'], label_name_to_value
            )

            label_names = [None] * (max(label_name_to_value.values()) + 1)
            for name, value in label_name_to_value.items():
                label_names[value] = name

            lbl_viz = imgviz.label2rgb(
                label=lbl, img=imgviz.asgray(img), label_names=label_names, loc='rb'
            )

            PIL.Image.fromarray(img).save(osp.join(out_dir, "Images", f"Image{i+148}.png"))
            utils.lblsave(osp.join(out_dir, "Masks", f"Mask{i+148}.png"), lbl)
            #PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, f"Mask{i+148}.png"))

            #with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
            #    for lbl_name in label_names:
            #        f.write(lbl_name + '\n')

            logger.info('Saved to: {}'.format(out_dir))
Exemple #22
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--version',
                        '-V',
                        action='store_true',
                        help='show version')
    parser.add_argument('--reset-config',
                        action='store_true',
                        help='reset qt config')
    parser.add_argument(
        '--logger-level',
        default='info',
        choices=['debug', 'info', 'warning', 'fatal', 'error'],
        help='logger level',
    )
    parser.add_argument('filename', nargs='?', help='image or label filename')
    parser.add_argument(
        '--output',
        '-O',
        '-o',
        help='output file or directory (if it ends with .json it is '
        'recognized as file, else as directory)')
    default_config_file = os.path.join(os.path.expanduser('~'), '.labelmerc')
    parser.add_argument(
        '--config',
        dest='config_file',
        help='config file (default: %s)' % default_config_file,
        default=default_config_file,
    )
    # config for the gui
    parser.add_argument(
        '--nodata',
        dest='store_data',
        action='store_false',
        help='stop storing image data to JSON file',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--autosave',
        dest='auto_save',
        action='store_true',
        help='auto save',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--nosortlabels',
        dest='sort_labels',
        action='store_false',
        help='stop sorting labels',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--flags',
        help='comma separated list of flags OR file containing flags',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--labelflags',
        dest='label_flags',
        help='yaml string of label specific flags OR file containing json '
        'string of label specific flags (ex. {person-\d+: [male, tall], '
        'dog-\d+: [black, brown, white], .*: [occluded]})',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--labels',
        help='comma separated list of labels OR file containing labels',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--validatelabel',
        dest='validate_label',
        choices=['exact', 'instance'],
        help='label validation types',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--keep-prev',
        action='store_true',
        help='keep annotation of previous frame',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--epsilon',
        type=float,
        help='epsilon to find nearest vertex on canvas',
        default=argparse.SUPPRESS,
    )
    args = parser.parse_args()

    if args.version:
        print('{0} {1}'.format(__appname__, __version__))
        sys.exit(0)

    logger.setLevel(getattr(logging, args.logger_level.upper()))

    if hasattr(args, 'flags'):
        if os.path.isfile(args.flags):
            with codecs.open(args.flags, 'r', encoding='utf-8') as f:
                args.flags = [l.strip() for l in f if l.strip()]
        else:
            args.flags = [l for l in args.flags.split(',') if l]

    if hasattr(args, 'labels'):
        if os.path.isfile(args.labels):
            with codecs.open(args.labels, 'r', encoding='utf-8') as f:
                args.labels = [l.strip() for l in f if l.strip()]
        else:
            args.labels = [l for l in args.labels.split(',') if l]

    if hasattr(args, 'label_flags'):
        if os.path.isfile(args.label_flags):
            with codecs.open(args.label_flags, 'r', encoding='utf-8') as f:
                args.label_flags = yaml.safe_load(f)
        else:
            args.label_flags = yaml.safe_load(args.label_flags)

    config_from_args = args.__dict__
    config_from_args.pop('version')
    reset_config = config_from_args.pop('reset_config')
    filename = config_from_args.pop('filename')
    output = config_from_args.pop('output')
    config_file = config_from_args.pop('config_file')
    config = get_config(config_from_args, config_file)

    if not config['labels'] and config['validate_label']:
        logger.error('--labels must be specified with --validatelabel or '
                     'validate_label: true in the config file '
                     '(ex. ~/.labelmerc).')
        sys.exit(1)

    output_file = None
    output_dir = None
    if output is not None:
        if output.endswith('.json'):
            output_file = output
        else:
            output_dir = output

    translator = QtCore.QTranslator()
    translator.load(QtCore.QLocale.system().name(),
                    osp.dirname(osp.abspath(__file__)) + '/translate')
    app = QtWidgets.QApplication(sys.argv)
    app.setApplicationName(__appname__)
    app.setWindowIcon(newIcon('icon'))
    app.installTranslator(translator)
    win = MainWindow(
        config=config,
        filename=filename,
        output_file=output_file,
        output_dir=output_dir,
    )

    if reset_config:
        logger.info('Resetting Qt config: %s' % win.settings.fileName())
        win.settings.clear()
        sys.exit(0)

    win.show()
    win.raise_()
    sys.exit(app.exec_())
Exemple #23
0
def json2png(json_file, lab2val={'_background_': 0}):

    #    json_file = args.json_file

    label_name_to_value = lab2val
    out_dir = osp.basename(json_file).replace('.', '_')
    out_dir = osp.join(osp.dirname(json_file), out_dir)
    out_png = osp.join(osp.dirname(json_file), 'png')
    out_pngviz = osp.join(osp.dirname(json_file), 'png_viz')
    out_pic = osp.join(osp.dirname(json_file), 'pic')

    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    if not osp.exists(out_png):
        os.mkdir(out_png)

    if not osp.exists(out_pngviz):
        os.mkdir(out_pngviz)

    if not osp.exists(out_pic):
        os.mkdir(out_pic)

    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)

    for shape in sorted(data['shapes'], key=lambda x: x['label']):
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)

    PIL.Image.fromarray(img).save(
        osp.join(out_pic,
                 osp.basename(json_file).replace('.json', '') + '.jpg'))
    PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
    PIL.Image.fromarray(lbl).save(
        osp.join(out_png,
                 osp.basename(json_file).replace('.json', '') + '.png'))

    #    utils.lblsave(osp.join(out_png, osp.basename(json_file).replace('.json', '')+'.png'), lbl)
    utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
    PIL.Image.fromarray(lbl_viz).save(
        osp.join(out_pngviz,
                 osp.basename(json_file).replace('.json', '') + '.png'))
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

    with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
        for lbl_name in label_names:
            f.write(lbl_name + '\n')

    logger.warning('info.yaml is being replaced by label_names.txt')
    info = dict(label_names=label_names)
    with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
        yaml.safe_dump(info, f, default_flow_style=False)

    logger.info('Saved to: {}'.format(out_dir))
Exemple #24
0
def main():
    logger.warning('This script is aimed to remap the ADE20K '
                   'annotations to a customize annotation.')

    parser = argparse.ArgumentParser()
    parser.add_argument('--label-file', default="rtk")
    parser.add_argument('--remap-table', default="rtk")
    parser.add_argument('--image-list', default="training")
    parser.add_argument('--save-vizImage', default=False)
    parser.add_argument('--save-oriImage', default=False)
    # parser.add_argument('--save-colorLabImage', default=False)
    args = parser.parse_args()

    # Import label file
    label_file = 'labels_' + args.label_file + '.txt'
    label_name_to_value = importLabel(label_file)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
        # print(value, " ", name)

    # Import remap table
    map_table = 'map_' + args.remap_table + '.txt'
    mapping = importRemapTable(map_table)

    # Output direction
    out_folder = osp.join('annotations_' + args.label_file, args.image_list)
    if not osp.exists(out_folder):
        os.makedirs(out_folder, exist_ok=True)
    logger.info('Saved to: {}'.format(out_folder))

    # Load label image list
    label_list = importLabelList(args.image_list + '.txt')

    # Main loop
    for idx in range(0, len(label_list)):
        # load label image
        with open(label_list[idx], 'rb') as f:
            image_name = osp.split(label_list[idx])[1].split('.')[0]
            imageData = f.read()
            if not imageData:
                logger.info('Lebelled Image does not existed')
                break
            imageData = base64.b64encode(imageData).decode('utf-8')
            label_img = utils.img_b64_to_arr(imageData)
            label_img = remapLabel(label_img, mapping)

        utils.lblsave_gray(osp.join(out_folder, image_name + '.png'),
                           label_img)

        # if args.save_colorLabImage:
        #     utils.lblsave(osp.join(out_folder, 'label_color.png'), label_img)

        # load original image
        if args.save_oriImage or args.save_vizImage:
            image_list = label_list[idx].replace('annotations', 'images')
            image_list = image_list.replace('png', 'jpg')
            with open(image_list, 'rb') as f:
                imageData = f.read()
                if not imageData:
                    logger.info('Original Color Image does not existed')
                    args.save_oriImage = args.save_vizImage = False
                imageData = base64.b64encode(imageData).decode('utf-8')
                img = utils.img_b64_to_arr(imageData)

            if args.save_oriImage:
                PIL.Image.fromarray(img).save(
                    osp.join(out_folder, image_name + '.jpg'))

            if args.save_vizImage:
                lbl_viz = imgviz.label2rgb(label=label_img,
                                           img=imgviz.asgray(img),
                                           label_names=label_names,
                                           loc='rb')
                PIL.Image.fromarray(lbl_viz).save(
                    osp.join(out_folder, image_name + '_viz.png'))
Exemple #25
0
 def load_basic_shapes(self):
     cur_dir = osp.dirname(osp.abspath(__file__))
     npz_path = osp.join(osp.dirname(cur_dir), "config", "basic_shapes.npz")
     self.basic_shapes = dict(np.load(npz_path))
     logger.info("basic shape name: {}".format(self.basic_shapes.keys()))
Exemple #26
0
def _main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--version', '-V', action='store_true', help='show version'
    )
    parser.add_argument(
        '--reset-config', action='store_true', help='reset qt config'
    )
    parser.add_argument(
        '--logger-level',
        default='info',
        choices=['debug', 'info', 'warning', 'fatal', 'error'],
        help='logger level',
    )
    parser.add_argument('filename', nargs='?', help='image or label filename')
    parser.add_argument(
        '--output',
        '-O',
        '-o',
        help='output file or directory (if it ends with .json it is '
             'recognized as file, else as directory)'
    )
    default_config_file = os.path.join(os.path.expanduser('~'), '.labelmerc')
    parser.add_argument(
        '--config',
        dest='config_file',
        help='config file (default: %s)' % default_config_file,
        default=default_config_file,
    )
    # config for the gui
    parser.add_argument(
        '--nodata',
        dest='store_data',
        action='store_false',
        help='stop storing image data to JSON file',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--autosave',
        dest='auto_save',
        action='store_true',
        help='auto save',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--nosortlabels',
        dest='sort_labels',
        action='store_false',
        help='stop sorting labels',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--flags',
        help='comma separated list of flags OR file containing flags',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--labels',
        help='comma separated list of labels OR file containing labels',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--validatelabel',
        dest='validate_label',
        choices=['exact', 'instance'],
        help='label validation types',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--keep-prev',
        action='store_true',
        help='keep annotation of previous frame',
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--epsilon',
        type=float,
        help='epsilon to find nearest vertex on canvas',
        default=argparse.SUPPRESS,
    )
    args = parser.parse_args()

    if args.version:
        print('{0} {1}'.format(__appname__, __version__))
        sys.exit(0)

    logger.setLevel(getattr(logging, args.logger_level.upper()))

    if hasattr(args, 'flags'):
        if os.path.isfile(args.flags):
            with codecs.open(args.flags, 'r', encoding='utf-8') as f:
                args.flags = [l.strip() for l in f if l.strip()]
        else:
            args.flags = [l for l in args.flags.split(',') if l]

    if hasattr(args, 'labels'):
        if os.path.isfile(args.labels):
            with codecs.open(args.labels, 'r', encoding='utf-8') as f:
                args.labels = [l.strip() for l in f if l.strip()]
        else:
            args.labels = [l for l in args.labels.split(',') if l]

    config_from_args = args.__dict__
    config_from_args.pop('version')
    reset_config = config_from_args.pop('reset_config')
    filename = config_from_args.pop('filename')
    output = config_from_args.pop('output')
    config_file = config_from_args.pop('config_file')
    config = get_config(config_from_args, config_file)

    if not config['labels'] and config['validate_label']:
        logger.error('--labels must be specified with --validatelabel or '
                     'validate_label: true in the config file '
                     '(ex. ~/.labelmerc).')
        sys.exit(1)

    output_file = None
    output_dir = None
    if output is not None:
        if output.endswith('.json'):
            output_file = output
        else:
            output_dir = output

    app = QtWidgets.QApplication(sys.argv)
    app.setApplicationName(__appname__)
    app.setWindowIcon(newIcon('icon'))
    win = MainWindow(
        config=config,
        filename=filename,
        output_file=output_file,
        output_dir=output_dir,
    )

    if reset_config:
        logger.info('Resetting Qt config: %s' % win.settings.fileName())
        win.settings.clear()
        sys.exit(0)

    win.show()
    win.raise_()
    sys.exit(app.exec_())
Exemple #27
0
def js_to_output(json_file, out_dir=None):
    # logger.warning(
    #     "This script is aimed to demonstrate how to convert the "
    #     "JSON file to a single image dataset."
    # )
    # logger.warning(
    #     "It won't handle multiple JSON files to generate a "
    #     "real-use dataset."
    # )
    

    # output directory
    if out_dir is None:
        out_dir = osp.basename(json_file).replace(".", "_")
        out_dir = osp.join(js_data_path, 'js_output', out_dir)
    else:
        out_dir = out_dir

    # create folder if not exist
    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    data = json.load(open(osp.join(js_data_path, 'js_file', json_file)))
    imageData = data.get("imageData")

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
        with open(imagePath, "rb") as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode("utf-8")
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {"_background_": 0}
    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(
        img.shape, data["shapes"], label_name_to_value
    )

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name

    lbl_viz = imgviz.label2rgb(
        label=lbl, img=imgviz.asgray(img), label_names=label_names, loc="rb"
    )

    PIL.Image.fromarray(img).save(osp.join(out_dir, "img.png"))
    utils.lblsave(osp.join(out_dir, "label.png"), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, "label_viz.png"))

    with open(osp.join(out_dir, "label_names.txt"), "w") as f:
        for lbl_name in label_names:
            f.write(lbl_name + "\n")


    info = dict(label_names=label_names)
    with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
        yaml.safe_dump(info, f, default_flow_style=False)

    logger.info("Saved to: {}".format(out_dir))
Exemple #28
0
def convert_single_img(i, json_file, save_dir):
    """
    将单个json文件转换为标签图像

    Args:
        i ([type]): [description]
        json_file (str): json文件
        out ([type]): [description]
    """
    # parser = argparse.ArgumentParser()
    # parser.add_argument("json_file")
    # parser.add_argument("-o", "--out", default=None)
    # args = parser.parse_args()

    # json_file = json_file

    if save_dir is None:
        out_dir = osp.basename(json_file).replace(".", "_")
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    if not osp.exists(save_dir):
        os.mkdir(save_dir)
    train_dir = osp.join(save_dir, 'train')
    if not osp.exists(train_dir):
        os.mkdir(train_dir)
    label_dir = osp.join(save_dir, 'label')
    if not osp.exists(label_dir):
        os.mkdir(label_dir)

    data = json.load(open(json_file))
    imageData = data.get("imageData")

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
        with open(imagePath, "rb") as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode("utf-8")
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {"_background_": 0}
    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                   label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name

    # lbl_viz = imgviz.label2rgb(label=lbl,
    #                            img=imgviz.asgray(img),
    #                            label_names=label_names,
    #                            loc="rb")

    PIL.Image.fromarray(img).save(osp.join(train_dir, f"{i}.png"))
    utils.lblsave(osp.join(label_dir, f"{i}.png"), lbl)
    # PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, "label_viz.png"))
    label_name_file = osp.join(save_dir, "label_names.txt")
    if not os.path.isfile(label_name_file):
        with open(label_name_file, "w") as f:
            for lbl_name in label_names:
                f.write(lbl_name + "\n")

    logger.info("Saved to: {}".format(save_dir))
Exemple #29
0
    def validate_json_file(self, true_json, target_json, out_full_path=None, threshold=0.5, show_image=False, isDebug=False, save_image=False):
        json_file_true = true_json
        json_file_target = target_json

        # region 저장 경로 설정/생성
        if out_full_path is None:
            out_dir = osp.basename(json_file_true).replace(".", "_")
            out_dir = osp.join(osp.dirname(json_file_true), out_dir)
        else:
            out_dir = out_full_path
        if not osp.exists(out_dir):
            os.mkdir(out_dir)
        # endregion

        data_true = json.load(open(json_file_true))
        data_target = json.load(open(json_file_target))

        img_true = self.image_data_load(data_true)
        img_target = self.image_data_load(data_target)

        label_names_true = self.load_label_names(data_true, img_true)
        label_names_target = self.load_label_names(data_target, img_target)

        lbl_true = self.lbl_arr_load(data_true, img_true)
        lbl_target = self.lbl_arr_load(data_target, img_target)

        lbl_true = np.array(lbl_true)
        lbl_target = np.array(lbl_target)

        if show_image:
            Qimage_true = self.tpQImage(img_true)  # QImage(img_true.data, h, w, QImage.Format_Indexed8)
            Qimage_target = self.tpQImage(img_target)

            # self.label_imageA.setPixmap(QPixmap.fromImage(Qimage_true))
            # self.label_imageB.setPixmap(QPixmap.fromImage(Qimage_target))

            self.viewerA.setPhoto(QPixmap.fromImage(Qimage_true))
            self.viewerB.setPhoto(QPixmap.fromImage(Qimage_target))

        # print(f"lbl_true {lbl_true}")
        # print(f"lbl_target {lbl_target}")

        # print(f"np.max(lbl_true) {np.max(lbl_true)}")
        # print(f"np.max(lbl_target) {np.max(lbl_target)}")
        max_label_number = max(np.max(lbl_true), np.max(lbl_target))
        if isDebug:
            print(f"max label number: {max_label_number}")

        validate_count = 0.0

        lowest_iou = 9999
        lowest_acc = 9999
        lowest_class = 9999

        iou_sum = 0.0
        acc_sum = 0.0

        accuracy_list = []
        iou_list = []

        for i in range(max_label_number + 1):
            lbl_true_i = lbl_true == i
            lbl_target_i = lbl_target == i
            assert label_names_true[i] == label_names_target[i], "label name must be same"
            intersection_region = np.sum(np.logical_and(lbl_true_i, lbl_target_i))
            union_region = np.sum(np.logical_or(lbl_true_i, lbl_target_i))
            true_region_sum = np.sum(lbl_true_i)
            validate_count += 1
            accuracy = round(intersection_region / true_region_sum, ROUND_NUMBER)
            iou = round(intersection_region / union_region, ROUND_NUMBER)

            accuracy_list.append(accuracy)
            iou_list.append(iou)

            acc_sum += accuracy
            iou_sum += iou

            if iou < lowest_iou:
                lowest_iou = round(iou, ROUND_NUMBER)
                lowest_acc = round(accuracy, ROUND_NUMBER)
                lowest_class = i

            if isDebug:
                print(
                    f"[{i}:{label_names_true[i]}] class accuracy: {accuracy:.3f}% ({intersection_region}/{true_region_sum})")
                print(f"[{i}:{label_names_true[i]}] class iou: {iou:.3f}% ({intersection_region}/{union_region})")

        iou_avg = round(iou_sum * 1.0 / validate_count, ROUND_NUMBER)
        acc_avg = round(acc_sum * 1.0 / validate_count, ROUND_NUMBER)

        iou = lowest_iou
        accuracy = lowest_acc

        result_list.append([len(result_list), osp.basename(true_json), osp.basename(target_json), lowest_class,
                            label_names_true[lowest_class], lowest_acc, lowest_iou, acc_avg, iou_avg])

        total_same_label = lbl_true == lbl_target
        total_accuracy = np.sum(total_same_label) / np.sum(lbl_true == lbl_true)
        # print(f"total_same_label: {total_same_label}")
        # print(f"total_accuracy: {total_accuracy}")

        self.resultDataList.append(ResultData(true_json, target_json, label_names_true, accuracy_list, iou_list))

        # region save image
        if save_image:
            good_prefix = f"good({threshold})"
            bad_prefix = f"bad({threshold})"
            if not osp.exists(osp.join(out_dir, good_prefix)):
                os.mkdir(osp.join(out_dir, good_prefix))

            if not osp.exists(osp.join(out_dir, bad_prefix)):
                os.mkdir(osp.join(out_dir, bad_prefix))

            if iou < threshold:
                current_out_dir = osp.join(out_dir, bad_prefix)
            else:
                current_out_dir = osp.join(out_dir, good_prefix)

            utils.lblsave(osp.join(current_out_dir, f"{osp.basename(true_json)}_label_true({iou:.3f}%).png"), lbl_true)
            utils.lblsave(osp.join(current_out_dir, f"{osp.basename(target_json)}_label_target({iou:.3f}%).png"),
                          lbl_target)
            utils.lblsave(osp.join(current_out_dir, f"{osp.basename(target_json)}_label_diff({iou:.3f}%).png"),
                          lbl_true - lbl_target)
            logger.info("Saved to: {}".format(current_out_dir))
def main():
    # logger.warning('This script is aimed to demonstrate how to convert the'
    #                'JSON file to a single image dataset, and not to handle'
    #                'multiple JSON files to generate a real-use dataset.')
    #
    # parser = argparse.ArgumentParser()
    # parser.add_argument('json_file')
    # parser.add_argument('-o', '--out', default=None)
    # args = parser.parse_args()

    # json_file = args.json_file
    #
    # if args.out is None:
    #     out_dir = osp.basename(json_file).replace('.', '_')
    #     out_dir = osp.join(osp.dirname(json_file), out_dir)
    # else:
    #     out_dir = args.out
    json_dir = r"F:\pycharm_data\dataset\190423_maskrcnn_for_citie\20190418jpg\maskrcnn_datasets\1_scratch\scratch_json"
    json_list = os.listdir(json_dir)
    print(json_list)
    for json_file in json_list:

        json_file = json_dir + "\\" + json_file
        out_dir = osp.basename(json_file).replace('.', '_')
        print(out_dir)
        base_name = out_dir
        out_dir = osp.join(osp.dirname(json_file), out_dir)
        print(out_dir)
        if not osp.exists(out_dir):
            os.mkdir(out_dir)

        data = json.load(open(json_file))

        if data['imageData']:
            imageData = data['imageData']
        else:
            imagePath = os.path.join(os.path.dirname(json_file),
                                     data['imagePath'])
            with open(imagePath, 'rb') as f:
                imageData = f.read()
                imageData = base64.b64encode(imageData).decode('utf-8')
        img = utils.img_b64_to_arr(imageData)

        label_name_to_value = {'_background_': 0}
        for shape in sorted(data['shapes'], key=lambda x: x['label']):
            label_name = shape['label']
            if label_name in label_name_to_value:
                label_value = label_name_to_value[label_name]
            else:
                label_value = len(label_name_to_value)
                label_name_to_value[label_name] = label_value

        # lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
        lbl = utils.shapes_to_label(img.shape, data['shapes'],
                                    label_name_to_value)

        # label_names = [None] * (max(label_name_to_value.values()) + 1)
        label_names = [None] * (max(label_name_to_value.values()) + 1)

        # for name, value in label_name_to_value.items():
        #     label_names[value] = name
        # lbl_viz = utils.draw_label(lbl, img, label_names)
        for name, value in label_name_to_value.items():
            label_names[value] = name
        lbl_viz = utils.draw_label(lbl, img, label_names)

        print("0001", base_name)
        print("000", out_dir)
        print("0002", osp.join(out_dir, base_name + '.png'))
        print("112", osp.join(out_dir, 'label_viz.png'))
        print("224", osp.join(out_dir, out_dir + '.png'))

        PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
        PIL.Image.fromarray(img).save(osp.join(out_dir, base_name + '.png'))

        #masks
        # utils.lblsave(osp.join(out_dir, 'label.png'), lbl) #mask
        utils.lblsave(osp.join(out_dir, base_name + '_mask.png'), lbl)

        #piz
        PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir,
                                                   'label_viz.png'))  #mask
        # PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, base_name+'label_viz.png'))

        with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
            for lbl_name in label_names:
                f.write(lbl_name + '\n')

        # logger.warning('info.yaml is being replaced by label_names.txt')
        logger.warning('info.yaml is being replaced by label_names.txt')

        # info = dict(label_names=label_names)
        info = dict(label_names=label_names)

        with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
            yaml.safe_dump(info, f, default_flow_style=False)

        logger.info('Saved to: {}'.format(out_dir))