File size: 1,825 Bytes
85278b2
 
2b798b4
85278b2
 
 
 
 
 
 
d87252c
ac222f2
56eec0f
85278b2
e663149
 
 
 
 
2b798b4
 
 
 
85278b2
 
2b798b4
85278b2
 
044d639
 
2b798b4
85278b2
 
 
 
be0fcb9
85278b2
16180b1
be0fcb9
 
 
 
85278b2
 
 
 
 
d78bc18
85278b2
 
be0fcb9
85278b2
 
 
 
 
 
 
 
 
 
 
c0b51dd
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
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()