Spaces:
Sleeping
Sleeping
File size: 2,490 Bytes
11802db | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | import tensorflow as tf
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.applications.inception_v3 import preprocess_input as inception_preprocess_input
import matplotlib.pyplot as plt
def retina_preprocessing(image_path):
"""
Preprocesses a retina image file.
Args:
image_path: a string, the path to the image file
Returns:
a tensor of shape (1, 224, 224, 3), the preprocessed image
"""
image = plt.imread(image_path)
# If the image is 2D (grayscale), add a channel dimension
if len(image.shape) == 2:
image = tf.expand_dims(image, axis=-1)
image = tf.image.grayscale_to_rgb(image)
image = tf.image.resize(image, [224, 224])
image = preprocess_input(image)
return tf.expand_dims(image, axis=0)
def pneumonia_preprocessing(image_path):
"""
Preprocesses a chest X-ray image file.
Args:
image_path: a string, the path to the image file
Returns:
a tensor of shape (1, 224, 224, 3), the preprocessed image
"""
image = plt.imread(image_path)
# If the image is 2D (grayscale), add a channel dimension
if len(image.shape) == 2:
image = tf.expand_dims(image, axis=-1)
image = tf.image.grayscale_to_rgb(image)
image = tf.image.resize(image, [224, 224])
image = tf.cast(image, tf.float32) / 255.0
return tf.expand_dims(image, axis=0)
def kidney_brain_preprocessing(image_path):
"""
Preprocesses a kidney image file.
Args:
image_path: str, the path to the kidney image file
Returns:
tensor: a tensor of shape (1, 224, 224, 1), the preprocessed kidney image
"""
image = plt.imread(image_path)
# If the image is 2D (grayscale), add a channel dimension
if len(image.shape) == 2:
image = tf.expand_dims(image, axis=-1)
image = tf.image.resize(image, [224, 224])
# if not grayscale convert it
if image.shape[-1] != 1:
image = tf.image.rgb_to_grayscale(image)
return tf.expand_dims(image, axis=0)
def general_preprocessing(image_path):
image = plt.imread(image_path)
# If the image is 2D (grayscale), add a channel dimension
if len(image.shape) == 2:
image = tf.expand_dims(image, axis=-1)
image = tf.image.grayscale_to_rgb(image)
image = tf.image.resize(image, [224, 224])
image = inception_preprocess_input(image)
return tf.expand_dims(image, axis=0) |