Spaces:
Running
Running
| 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_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(img_path_or_img): | |
| if isinstance(img_path_or_img, str): # Check if input is a path | |
| img = Image.open(img_path_or_img) | |
| elif isinstance(img_path_or_img, Image.Image): # Check if input is already an Image object | |
| img = img_path_or_img | |
| else: | |
| raise ValueError("Input must be a string path or a PIL Image object.") | |
| img = img.resize((256, 256)) | |
| img_array = np.array(img) | |
| return img_array | |
| 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 |