EcoIdentify / utils.py
VishnuEcoClim's picture
Update utils.py
413ef10
raw
history blame
3.09 kB
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, Flatten, MaxPooling2D, Dense, Dropout, SpatialDropout2D
from tensorflow.keras.losses import sparse_categorical_crossentropy, binary_crossentropy
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
from PIL import Image
def gen_labels():
train = 'Dataset/Train'
train_generator = ImageDataGenerator(rescale=1/255)
train_generator = train_generator.flow_from_directory(train,
target_size=(256, 256),
batch_size=32,
class_mode='sparse')
labels = train_generator.class_indices
labels = dict((v, k) for k, v in labels.items())
return labels
data_augmentation = tf.keras.Sequential([
tf.keras.layers.experimental.preprocessing.Rescaling(1./127.5, offset= -1),
tf.keras.layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical"),
tf.keras.layers.experimental.preprocessing.RandomRotation(0.2),
tf.keras.layers.experimental.preprocessing.RandomZoom(0.2)
], name='data_augmentation')
#Instantiating the base model
input_shape = (256,256,3)
base_model = tf.keras.applications.ResNet50V2(include_top=False, input_shape=input_shape)
#Making the layers of the model trainable
base_model.trainable = True
def preprocess(image_or_path):
if isinstance(image_or_path, str): # Check if the input is a string (image path)
img = Image.open(image_or_path)
elif isinstance(image_or_path, np.ndarray): # Check if the input is a numpy array
# Convert the numpy array to a PIL Image
img = Image.fromarray((image_or_path * 255).astype(np.uint8))
else:
raise ValueError("Unsupported input type. Expected string path or numpy array.")
# Print image dimensions for debugging
print(f"Original image dimensions: {img.size}")
# Check image mode
if img.mode != "RGB":
img = img.convert("RGB")
print("Converted image mode to RGB.")
try:
# Resize the image
resized_img = img.resize((256, 256), Image.LANCZOS)
# Convert the image to a numpy array
img_array = np.array(resized_img)
# Normalize if necessary
img_array = img_array / 255.0
# Add a batch dimension
img_array = np.expand_dims(img_array, axis=0)
return img_array
except Exception as e:
print(f"Error encountered: {e}")
return None
def model_arc():
model = tf.keras.Sequential([
data_augmentation,
base_model,
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(6, activation='softmax')
])
learning_rate = 0.00001
model.compile(
loss='sparse_categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(learning_rate),
metrics=['accuracy']
)
return model