AaSiKu's picture
Update app.py
c0b51dd verified
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPool2D, Flatten, Dense,RandomRotation,RandomZoom,RandomFlip,RandomBrightness,Dropout,Input
import pandas as pd
import numpy as np
import cv2
import gradio as gr
from keras.applications.inception_v3 import InceptionV3
from keras.models import Model
model_imagenet = InceptionV3(weights='imagenet',include_top=False,input_shape=(180, 180, 3))
model_imagenet.trainable = False
model = Sequential()
num_classes = 2
data_aug_layer = tf.keras.Sequential([
RandomFlip("horizontal"), # Rotate by up to 20 degrees
RandomZoom(0.2),
RandomRotation(0.1)
])
model = Sequential()
num_classes = 2
model.add(Input(shape=(180, 180, 3)))
# Add the pre-trained base model
model.add(data_aug_layer)
model.add(model_imagenet)
# Add custom layers on top
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(num_classes))
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# Using saved weights
model.load_weights('model_weights.h5')
class_names = {
1: 'Female',
0: 'Male'
}
def classify_image(image):
# Convert Gradio Image to numpy array
image = np.array(image)
# Preprocess the image
image = cv2.resize(image, (180, 180))
image = image/255
# Make prediction
preds = model.predict(image[np.newaxis, ...]).squeeze()
y_pred = preds.argmax(axis = 0) # Decode prediction
label = class_names[int(y_pred)]
return label
app = gr.Interface(
fn=classify_image,
inputs=["image"],
outputs=["text"],
)
app.launch()