Exemple #1
0
def read_image_from_str(img):
    try:
        from skimage.io import imread
    except ImportError:
        logger.error(' scikit-image is not installed. '
                     'In order to install all image feature dependencies run '
                     'pip install ludwig[image]')
        sys.exit(-1)

    try:
        return imread(img)
    except HTTPError as e:
        upgraded = upgrade_http(img)
        if upgraded:
            logger.info(
                f'reading image url {img} failed due to {e}. upgrading to https and retrying'
            )
            return read_image(upgraded)
        logger.info(
            f'reading image url {img} failed due to {e} and cannot be upgraded to https'
        )
        return None
    except Exception as e:
        logger.info(f'reading image url {img} failed', e)
        return None
Exemple #2
0
def read_image_from_str(img: str,
                        num_channels: Optional[int] = None) -> torch.Tensor:
    try:
        from torchvision.io import ImageReadMode, read_image
    except ImportError:
        logger.error(" torchvision is not installed. "
                     "In order to install all image feature dependencies run "
                     "pip install ludwig[image]")
        sys.exit(-1)

    try:
        if num_channels == 1:
            return read_image(img, mode=ImageReadMode.GRAY)
        elif num_channels == 2:
            return read_image(img, mode=ImageReadMode.GRAY_ALPHA)
        elif num_channels == 3:
            return read_image(img, mode=ImageReadMode.RGB)
        elif num_channels == 4:
            return read_image(img, mode=ImageReadMode.RGB_ALPHA)
        else:
            return read_image(img)
    except Exception as e:
        upgraded = upgrade_http(img)
        if upgraded:
            logger.info(
                f"reading image url {img} failed due to {e}. upgrading to https and retrying"
            )
            return read_image_from_str(upgraded, num_channels)
        logger.info(f"reading image url {img} failed due to {e}")
        return None
Exemple #3
0
def get_image_from_http_bytes(img_entry) -> BytesIO:
    import requests

    data = requests.get(img_entry, stream=True)
    if data.status_code == 404:
        upgraded = upgrade_http(img_entry)
        if upgraded:
            logger.info(f"reading image url {img_entry} failed. upgrading to https and retrying")
            return get_image_from_http_bytes(upgraded)
        else:
            raise requests.exceptions.HTTPError(f"reading image url {img_entry} failed and cannot be upgraded to https")
    return BytesIO(data.raw.read())
Exemple #4
0
def read_image(img):
    try:
        from skimage.io import imread
    except ImportError:
        logger.error(
            ' scikit-image is not installed. '
            'In order to install all image feature dependencies run '
            'pip install ludwig[image]'
        )
        sys.exit(-1)
    if isinstance(img, str):
        try:
            return imread(img)
        except HTTPError:
            upgraded = upgrade_http(img)
            if upgraded:
                logger.info(f'reading image url {img} failed. upgrading to https and retrying')
                return imread(upgraded)
            logger.info(f'reading image url {img} failed and cannot be upgraded to https')
            raise
    return img
Exemple #5
0
        )
        sys.exit(-1)

    try:
        if num_channels == 1:
            return read_image(img, mode=ImageReadMode.GRAY)
        elif num_channels == 2:
            return read_image(img, mode=ImageReadMode.GRAY_ALPHA)
        elif num_channels == 3:
            return read_image(img, mode=ImageReadMode.RGB)
        elif num_channels == 4:
            return read_image(img, mode=ImageReadMode.RGB_ALPHA)
        else:
            return read_image(img)
    except HTTPError as e:
        upgraded = upgrade_http(img)
        if upgraded:
            logger.info(f"reading image url {img} failed due to {e}. upgrading to https and retrying")
            return read_image(upgraded)
        logger.info(f"reading image url {img} failed due to {e} and cannot be upgraded to https")
        return None
    except Exception as e:
        logger.info(f"reading image url {img} failed with error: ", e)


def pad(
    img: torch.Tensor,
    size: Union[int, Tuple[int]],
) -> torch.Tensor:
    old_size = np.array(img.shape[1:])
    pad_size = (to_np_tuple(size) - old_size) / 2