Spaces:
Runtime error
Runtime error
App MNIST
Browse filesPrimer App
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Dropout, Add, Layer, Flatten, BatchNormalization, Activation
|
3 |
+
from tensorflow.keras.models import Model
|
4 |
+
|
5 |
+
class ResLayer(Layer):
|
6 |
+
def __init__(self, filters, name = "Res_Layer"):
|
7 |
+
super(ResLayer, self).__init__(name = name)
|
8 |
+
self.filters = filters
|
9 |
+
self.f1, self.f2, self.f3 = self.filters
|
10 |
+
|
11 |
+
# Camino normal
|
12 |
+
self.Conv_1 = Conv2D(filters = self.f1, kernel_size = (1, 1), strides = (1, 1))
|
13 |
+
self.MaxPool_1 = MaxPool2D(pool_size = (2, 2))
|
14 |
+
self.BatchNorm_1 = BatchNormalization()
|
15 |
+
self.Activation_1 = Activation("relu")
|
16 |
+
|
17 |
+
self.Conv_2 = Conv2D(filters = self.f2, kernel_size = (1, 1), strides = (1, 1))
|
18 |
+
self.BatchNorm_2 = BatchNormalization()
|
19 |
+
self.Activation_2 = Activation("relu")
|
20 |
+
|
21 |
+
self.Conv_3 = Conv2D(filters = self.f3, kernel_size = (1, 1), strides = (1, 1))
|
22 |
+
self.BatchNorm_3 = BatchNormalization()
|
23 |
+
|
24 |
+
# Camino corto
|
25 |
+
self.Conv_4 = Conv2D(filters = self.f3, kernel_size = (1, 1), strides = (1, 1))
|
26 |
+
self.MaxPool_2 = MaxPool2D(pool_size = (2, 2))
|
27 |
+
|
28 |
+
self.Add = Add()
|
29 |
+
self.Activation_3 = Activation("relu")
|
30 |
+
|
31 |
+
def call(self, inputs):
|
32 |
+
X_copy = inputs
|
33 |
+
|
34 |
+
X = self.Conv_1(inputs)
|
35 |
+
X = self.MaxPool_1(X)
|
36 |
+
X = self.BatchNorm_1(X)
|
37 |
+
X = self.Activation_1(X)
|
38 |
+
|
39 |
+
X = self.Conv_2(X)
|
40 |
+
X = self.BatchNorm_2(X)
|
41 |
+
X = self.Activation_2(X)
|
42 |
+
|
43 |
+
X = self.Conv_3(X)
|
44 |
+
X = self.BatchNorm_3(X)
|
45 |
+
|
46 |
+
X_copy = self.Conv_4(X_copy)
|
47 |
+
X_copy = self.MaxPool_2(X_copy)
|
48 |
+
|
49 |
+
outputs = self.Add([X, X_copy])
|
50 |
+
outputs = self.Activation_3(outputs)
|
51 |
+
|
52 |
+
return outputs
|
53 |
+
|
54 |
+
class ResNet(Model):
|
55 |
+
def __init__(self, filters = [[64, 128, 256]], name = "ResNet"):
|
56 |
+
super(ResNet, self).__init__(name = name)
|
57 |
+
self.filters = filters
|
58 |
+
self.nb_layers = tf.shape(self.filters)[0].numpy()
|
59 |
+
self.res_layer = [ResLayer(filters)
|
60 |
+
for i, filters in enumerate(self.filters)]
|
61 |
+
self.Flatten = Flatten()
|
62 |
+
self.Dense_1 = Dense(units = 128, activation = "relu")
|
63 |
+
self.dropout_1 = Dropout(rate = 0.2)
|
64 |
+
self.Dense_2 = Dense(units = 64, activation = "relu")
|
65 |
+
self.dropout_2 = Dropout(rate = 0.1)
|
66 |
+
self.Dense_Out = Dense(units = 10, activation = "softmax")
|
67 |
+
|
68 |
+
def call(self, inputs):
|
69 |
+
outputs = inputs
|
70 |
+
for i in range(self.nb_layers):
|
71 |
+
outputs = self.res_layer[i](outputs)
|
72 |
+
|
73 |
+
outputs = self.Flatten(outputs)
|
74 |
+
outputs = self.Dense_1(outputs)
|
75 |
+
outputs = self.dropout_1(outputs)
|
76 |
+
outputs = self.Dense_2(outputs)
|
77 |
+
outputs = self.dropout_2(outputs)
|
78 |
+
outputs = self.Dense_Out(outputs)
|
79 |
+
|
80 |
+
return outputs
|
81 |
+
|
82 |
+
model = ResNet()
|
83 |
+
model.build(input_shape = [None, 28, 28, 1])
|
84 |
+
model.load_weights("ResNet_Weights.tf")
|
85 |
+
|
86 |
+
import gradio as gr
|
87 |
+
|
88 |
+
def digit_recognition(img):
|
89 |
+
img = img / 255.
|
90 |
+
img = tf.expand_dims(img, axis = -1)
|
91 |
+
img = tf.convert_to_tensor([img], dtype = tf.float32)
|
92 |
+
prediction = model(img)
|
93 |
+
prediction = tf.squeeze(prediction)
|
94 |
+
return {"Cero": float(prediction[0]),
|
95 |
+
"Uno": float(prediction[1]),
|
96 |
+
"Dos": float(prediction[2]),
|
97 |
+
"Tres": float(prediction[3]),
|
98 |
+
"Cuatro": float(prediction[4]),
|
99 |
+
"Cinco": float(prediction[5]),
|
100 |
+
"Seis": float(prediction[6]),
|
101 |
+
"Siete": float(prediction[7]),
|
102 |
+
"Ocho": float(prediction[8]),
|
103 |
+
"Nueve": float(prediction[9])}
|
104 |
+
|
105 |
+
app = gr.Interface(fn = digit_recognition, inputs = "sketchpad", outputs = "label", description = "Dibuja un número", title = "MNIST Digit Recognition")
|
106 |
+
app.launch(share = True)
|