<commit_before># Copyright (c) 2016, NVIDIA CORPORATION.  All rights reserved.
from __future__ import absolute_import

import numpy as np
import operator
import os
import PIL.Image
import random

import digits
from digits.utils import subclass, override, constants
from ..interface import DataIngestionInterface
from .forms import DatasetForm
from .utils import GroundTruth, GroundTruthObj
from .utils import bbox_to_array, resize_bbox_list

TEMPLATE = "template.html"


@subclass
class DataIngestion(DataIngestionInterface):
    """
    A data ingestion extension for an object detection dataset
    """

    def __init__(self, **kwargs):
        super(DataIngestion, self).__init__(**kwargs)

        # this instance is automatically populated with form field
        # attributes by superclass constructor

        if ((self.val_image_folder == '') ^ (self.val_label_folder == '')):
            raise ValueError("You must specify either both val_image_folder and val_label_folder or none")

        if ((self.resize_image_width is None) ^ (self.resize_image_height is None)):
            raise ValueError("You must specify either both resize_image_width and resize_image_height or none")

        # this will be set when we know the phase we are encoding
        self.ground_truth = None

    @override
    def encode_entry(self, entry):
        """
        Return numpy.ndarray
        """
        image_filename = entry

        # (1) image part

        # load from file
        img = digits.utils.image.load_image(image_filename)
        if self.channel_conversion != 'none':
            if img.mode != self.channel_conversion:
                # convert to different image mode if necessary
                img = img.convert(self.channel_conversion)

        # pad
        img = self.pad_image(img)

        if self.resize_image_width is not None:
            # resize
            resize_ratio_x = self.resize_image_width / self.padding_image_width
            resize_ratio_y = self.resize_image_height / self.padding_image_height
            img = digits.utils.image.resize_image(
                img,
                self.resize_image_height,
                self.resize_image_width)
        else:
            resize_ratio_x = 1
            resize_ratio_y = 1
            # convert to numpy array
            img = np.array(img)

        if img.ndim == 2:
            # grayscale
            img = img[np.newaxis, :, :]
            if img.dtype == 'uint16':
                img = img.astype(float)
        else:
            if img.ndim != 3 or img.shape[2] != 3:
                raise ValueError("Unsupported image shape: %s" % repr(img.shape))
            # HWC -> CHW
            img = img.transpose(2, 0, 1)

        # (2) label part

        # make sure label exists
        try:
            label_id = int(os.path.splitext(os.path.basename(entry))[0])
        except:
            raise ValueError("Unable to extract numerical id from file name %s" % entry)<commit_msg>Fix resize ratio in object detection<commit_after># Copyright (c) 2016, NVIDIA CORPORATION.  All rights reserved.
from __future__ import absolute_import

import numpy as np
import operator
import os
import PIL.Image
import random

import digits
from digits.utils import subclass, override, constants
from ..interface import DataIngestionInterface
from .forms import DatasetForm
from .utils import GroundTruth, GroundTruthObj
from .utils import bbox_to_array, resize_bbox_list

TEMPLATE = "template.html"


@subclass
class DataIngestion(DataIngestionInterface):
    """
    A data ingestion extension for an object detection dataset
    """

    def __init__(self, **kwargs):
        super(DataIngestion, self).__init__(**kwargs)

        # this instance is automatically populated with form field
        # attributes by superclass constructor

        if ((self.val_image_folder == '') ^ (self.val_label_folder == '')):
            raise ValueError("You must specify either both val_image_folder and val_label_folder or none")

        if ((self.resize_image_width is None) ^ (self.resize_image_height is None)):
            raise ValueError("You must specify either both resize_image_width and resize_image_height or none")

        # this will be set when we know the phase we are encoding
        self.ground_truth = None

    @override
    def encode_entry(self, entry):
        """
        Return numpy.ndarray
        """
        image_filename = entry

        # (1) image part

        # load from file
        img = digits.utils.image.load_image(image_filename)
        if self.channel_conversion != 'none':
            if img.mode != self.channel_conversion:
                # convert to different image mode if necessary
                img = img.convert(self.channel_conversion)

        # pad
        img = self.pad_image(img)

        if self.resize_image_width is not None:
            # resize
            resize_ratio_x = float(self.resize_image_width) / self.padding_image_width
            resize_ratio_y = float(self.resize_image_height) / self.padding_image_height
            img = digits.utils.image.resize_image(
                img,
                self.resize_image_height,
                self.resize_image_width)
        else:
            resize_ratio_x = 1
            resize_ratio_y = 1
            # convert to numpy array
            img = np.array(img)

        if img.ndim == 2:
            # grayscale
            img = img[np.newaxis, :, :]
            if img.dtype == 'uint16':
                img = img.astype(float)
        else:
            if img.ndim != 3 or img.shape[2] != 3:
                raise ValueError("Unsupported image shape: %s" % repr(img.shape))
            # HWC -> CHW
            img = img.transpose(2, 0, 1)

        # (2) label part

        # make sure label exists
        try:
            label_id = int(os.path.splitext(os.path.basename(entry))[0])
        except:
            raise ValueError("Unable to extract numerical id from file name %s" % entry)
{'fraction_non_alphanumeric': 0.08065322446258957, 'fraction_numerical': 0.005999000166638894, 'mean_word_length': 2.223415682062299, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5555031', 'n_tokens_mistral': 1727, 'n_tokens_neox': 1620, 'n_words': 488}