Spaces:
Running
Running
File size: 3,088 Bytes
527d8f9 b9ee29a 2cc8d92 b9ee29a aee66d8 b9ee29a aee66d8 b9ee29a b3797b8 aac970e 413ef10 aac970e 22d0d15 aac970e 22d0d15 aac970e 22d0d15 aac970e 22d0d15 aac970e 22d0d15 977cc9d 26b08fd 977cc9d 5974209 |
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 88 |
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 |