File size: 3,862 Bytes
9e75a0d 903f8b6 9e75a0d 3d667be 9e75a0d 38a3ff0 9e75a0d |
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
import gradio as gr
import tensorflow as tf
import keras
import numpy as np
import sklearn
from sklearnex import patch_sklearn, unpatch_sklearn
patch_sklearn()
import xgboost as xgb
from keras import Model, Sequential
from keras.layers import Layer
import keras.layers as nn
class ChannelAttn(Layer):
def __init__(self, c=512) -> None:
super(ChannelAttn,self).__init__()
self.gap = nn.AveragePooling2D(7)
self.attention = Sequential([
nn.Dense(32),
nn.BatchNormalization(),
nn.ReLU(),
nn.Dense(c,activation='sigmoid')]
)
def call(self, x):
x = self.gap(x)
x = nn.Flatten()(x)
y = self.attention(x)
return x * y
class SpatialAttn(Layer):
def __init__(self, c=512):
super(SpatialAttn,self).__init__()
self.conv1x1 = Sequential([
nn.Conv2D(256, 1),
nn.BatchNormalization()]
)
self.conv_3x3 = Sequential([
nn.ZeroPadding2D(padding=(1, 1)),
nn.Conv2D(512, 3,1),
nn.BatchNormalization()]
)
self.conv_1x3 = Sequential([
nn.ZeroPadding2D(padding=(0, 1)),
nn.Conv2D(512, (1,3)),
nn.BatchNormalization()]
)
self.conv_3x1 = Sequential([
nn.ZeroPadding2D(padding=(1, 0)),
nn.Conv2D(512,(3,1)),
nn.BatchNormalization()]
)
self.norm = nn.ReLU()
def call(self, x) :
y = self.conv1x1(x)
y = self.norm(self.conv_3x3(y) + self.conv_1x3(y) + self.conv_3x1(y))
y = tf.math.reduce_sum(y,axis=1, keepdims=True)
return x*y
class CrossAttnHead(Layer):
def __init__(self, c=512):
super(CrossAttnHead,self).__init__()
self.sa = SpatialAttn(c)
self.ca = ChannelAttn(c)
def call(self, x):
return self.ca(self.sa(x))
@keras.saving.register_keras_serializable(package='custom')
class DAN(Model):
def __init__(self, num_classes=8,trainable=True,dtype='float32'):
super(DAN,self).__init__()
self.mod = keras.applications.ResNet50(
include_top=False,
weights="imagenet",
input_shape=(224,224,3)
)
self.mod.trainable= False
self.num_head = 4
self.hd = CrossAttnHead()
self.hd=[]
for i in range(self.num_head):
self.hd.append(CrossAttnHead())
self.features = nn.Conv2D(512, 1,padding='same')
self.fc = nn.Dense(num_classes)
self.bn = nn.BatchNormalization()
def call(self, x) :
x = self.mod(x)
x=self.features(x)
heads = []
for h in self.hd:
heads.append(h(x))
heads = tf.transpose(tf.stack(heads),perm=(1,0,2))
heads = keras.ops.log_softmax(heads, axis=1)
return tf.math.reduce_sum(heads,axis=1)
backbone = DAN()
backbone.load_weights('weights.keras')
xgb_params = {
'objective': 'binary:logistic',
'predictor': 'cpu_predictor',
'disable_default_eval_metric': 1,
}
model_xgb= xgb.XGBClassifier(**xgb_params)
model_xgb.load_model('xgb.json')
emotions = ['anger', 'contempt', 'disgust', 'fear', 'happy', 'neutral', 'sad', 'surprise']
def fn(image):
if len(image.shape)==2:
img = np.stack([image,image,image],axis=2)
img = np.resize(img,(224,224,3))
elif len(image.shape)==3 and image.shape[2]==1:
img = np.stack([image[:,:,0],image[:,:,0],image[:,:,0]],axis=2)
img = np.resize(img,(224,224,3))
else:
img = np.resize(image,(224,224,3))
img = np.expand_dims(img,axis=0)
feats = backbone.predict(img)
pred = model_xgb.predict(feats)
print(pred)
return emotions[pred[0]]
demo = gr.Interface(
fn,['image'],"text",
)
if __name__ == "__main__":
demo.launch() |