Spaces:
Sleeping
Sleeping
# import the necessary packages | |
import tensorflow as tf | |
from tensorflow.keras import layers | |
from PIL import Image | |
from io import BytesIO | |
import requests | |
import numpy as np | |
RESOLUTION = 224 | |
crop_layer = layers.CenterCrop(RESOLUTION, RESOLUTION) | |
norm_layer = layers.Normalization( | |
mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], | |
variance=[(0.229 * 255) ** 2, (0.224 * 255) ** 2, (0.225 * 255) ** 2], | |
) | |
rescale_layer = layers.Rescaling(scale=1./127.5, offset=-1) | |
def preprocess_image(image, model_type, size=RESOLUTION): | |
# Turn the image into a numpy array and add batch dim. | |
image = np.array(image) | |
image = tf.expand_dims(image, 0) | |
# If model type is vit rescale the image to [-1, 1]. | |
if model_type == "original_vit": | |
image = rescale_layer(image) | |
# Resize the image using bicubic interpolation. | |
resize_size = int((256 / 224) * size) | |
image = tf.image.resize( | |
image, | |
(resize_size, resize_size), | |
method="bicubic" | |
) | |
# Crop the image. | |
image = crop_layer(image) | |
# If model type is DeiT or DINO normalize the image. | |
if model_type != "original_vit": | |
image = norm_layer(image) | |
return image.numpy() | |
def load_image_from_url(url, model_type): | |
# Credit: Willi Gierke | |
response = requests.get(url) | |
image = Image.open(BytesIO(response.content)) | |
preprocessed_image = preprocess_image(image, model_type) | |
return image, preprocessed_image |