File size: 3,908 Bytes
1c4f5a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tensorflow as tf
from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Dropout, Add, Layer, Flatten, BatchNormalization, Activation
from tensorflow.keras.models import Model

class ResLayer(Layer):
    def __init__(self, filters, name = "Res_Layer"):
        super(ResLayer, self).__init__(name = name)
        self.filters = filters
        self.f1, self.f2, self.f3 = self.filters
        
        # Camino normal
        self.Conv_1 = Conv2D(filters = self.f1, kernel_size = (1, 1), strides = (1, 1))
        self.MaxPool_1 = MaxPool2D(pool_size = (2, 2))
        self.BatchNorm_1 = BatchNormalization()
        self.Activation_1 = Activation("relu")
        
        self.Conv_2 = Conv2D(filters = self.f2, kernel_size  = (1, 1), strides = (1, 1))
        self.BatchNorm_2 = BatchNormalization()
        self.Activation_2 = Activation("relu")
        
        self.Conv_3 = Conv2D(filters = self.f3, kernel_size = (1, 1), strides = (1, 1))
        self.BatchNorm_3 = BatchNormalization()
        
        # Camino corto
        self.Conv_4 = Conv2D(filters = self.f3, kernel_size = (1, 1), strides = (1, 1))
        self.MaxPool_2 = MaxPool2D(pool_size = (2, 2))
        
        self.Add = Add()
        self.Activation_3 = Activation("relu")
        
    def call(self, inputs):
        X_copy = inputs
        
        X = self.Conv_1(inputs)
        X = self.MaxPool_1(X)
        X = self.BatchNorm_1(X)
        X = self.Activation_1(X)
        
        X = self.Conv_2(X)
        X = self.BatchNorm_2(X)
        X = self.Activation_2(X)
        
        X = self.Conv_3(X)
        X = self.BatchNorm_3(X)
        
        X_copy = self.Conv_4(X_copy)
        X_copy = self.MaxPool_2(X_copy)
        
        outputs = self.Add([X, X_copy])
        outputs = self.Activation_3(outputs)
        
        return outputs
        
class ResNet(Model):
    def __init__(self, filters = [[64, 128, 256]], name = "ResNet"):
        super(ResNet, self).__init__(name = name)
        self.filters = filters
        self.nb_layers = tf.shape(self.filters)[0].numpy()
        self.res_layer = [ResLayer(filters)
                         for i, filters in enumerate(self.filters)]
        self.Flatten = Flatten()
        self.Dense_1 = Dense(units = 128, activation = "relu")
        self.dropout_1 = Dropout(rate = 0.2)
        self.Dense_2 = Dense(units = 64, activation = "relu")
        self.dropout_2 = Dropout(rate = 0.1)
        self.Dense_Out = Dense(units = 10, activation = "softmax")
        
    def call(self, inputs):
        outputs = inputs
        for i in range(self.nb_layers):
            outputs = self.res_layer[i](outputs)
        
        outputs = self.Flatten(outputs)
        outputs = self.Dense_1(outputs)
        outputs = self.dropout_1(outputs)
        outputs = self.Dense_2(outputs)
        outputs = self.dropout_2(outputs)
        outputs = self.Dense_Out(outputs)
        
        return outputs
        
model = ResNet()
model.build(input_shape = [None, 28, 28, 1])
model.load_weights("ResNet_Weights.tf")

import gradio as gr

def digit_recognition(img):
    img = img / 255.
    img = tf.expand_dims(img, axis = -1)
    img = tf.convert_to_tensor([img], dtype = tf.float32)
    prediction = model(img)
    prediction = tf.squeeze(prediction)
    return {"Cero": float(prediction[0]),
           "Uno": float(prediction[1]),
           "Dos": float(prediction[2]),
           "Tres": float(prediction[3]),
           "Cuatro": float(prediction[4]),
           "Cinco": float(prediction[5]),
           "Seis": float(prediction[6]),
           "Siete": float(prediction[7]),
           "Ocho": float(prediction[8]),
           "Nueve": float(prediction[9])}

app = gr.Interface(fn = digit_recognition, inputs = "sketchpad", outputs = "label", description = "Dibuja un número", title = "MNIST Digit Recognition")
app.launch(share = True)