| 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"), |
| RandomZoom(0.2), |
| RandomRotation(0.1) |
| ]) |
| model = Sequential() |
| num_classes = 2 |
| model.add(Input(shape=(180, 180, 3))) |
| |
| model.add(data_aug_layer) |
| model.add(model_imagenet) |
| |
| 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']) |
|
|
|
|
| |
| model.load_weights('model_weights.h5') |
| class_names = { |
| 1: 'Female', |
| 0: 'Male' |
| } |
| def classify_image(image): |
| |
| image = np.array(image) |
| |
| image = cv2.resize(image, (180, 180)) |
| image = image/255 |
| |
| preds = model.predict(image[np.newaxis, ...]).squeeze() |
| y_pred = preds.argmax(axis = 0) |
| label = class_names[int(y_pred)] |
| return label |
|
|
|
|
| app = gr.Interface( |
| fn=classify_image, |
| inputs=["image"], |
| outputs=["text"], |
| ) |
|
|
|
|
| app.launch() |