Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
from tensorflow.keras.callbacks import ModelCheckpoint
|
4 |
+
import tensorflow as tf
|
5 |
+
import datetime
|
6 |
+
|
7 |
+
|
8 |
+
#Preprocesamiento de imagenes del conjunto de entrenamiento
|
9 |
+
training_set = tf.keras.utils.image_dataset_from_directory(
|
10 |
+
'FruitTrainingDataset/train',
|
11 |
+
labels="inferred",
|
12 |
+
label_mode="categorical",
|
13 |
+
class_names=None,
|
14 |
+
color_mode="rgb",
|
15 |
+
batch_size=32,
|
16 |
+
image_size=(64, 64),
|
17 |
+
shuffle=True,
|
18 |
+
seed=None,
|
19 |
+
validation_split=None,
|
20 |
+
subset=None,
|
21 |
+
interpolation="bilinear",
|
22 |
+
follow_links=False,
|
23 |
+
crop_to_aspect_ratio=False
|
24 |
+
)
|
25 |
+
|
26 |
+
#Preprocesamiento de imagenes del conjunto de validacion
|
27 |
+
validation_set = tf.keras.utils.image_dataset_from_directory(
|
28 |
+
'FruitTrainingDataset/validation',
|
29 |
+
labels="inferred",
|
30 |
+
label_mode="categorical",
|
31 |
+
class_names=None,
|
32 |
+
color_mode="rgb",
|
33 |
+
batch_size=32,
|
34 |
+
image_size=(64, 64),
|
35 |
+
shuffle=True,
|
36 |
+
seed=None,
|
37 |
+
validation_split=None,
|
38 |
+
subset=None,
|
39 |
+
interpolation="bilinear",
|
40 |
+
follow_links=False,
|
41 |
+
crop_to_aspect_ratio=False
|
42 |
+
)
|
43 |
+
|
44 |
+
model = tf.keras.models.Sequential()
|
45 |
+
|
46 |
+
model.add(tf.keras.layers.Conv2D(filters=32,kernel_size=3,padding='same',activation='relu',input_shape=[64,64,3]))
|
47 |
+
model.add(tf.keras.layers.Conv2D(filters=32,kernel_size=3,activation='relu'))
|
48 |
+
model.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))
|
49 |
+
|
50 |
+
|
51 |
+
model.add(tf.keras.layers.Dropout(0.25))
|
52 |
+
|
53 |
+
|
54 |
+
model.add(tf.keras.layers.Conv2D(filters=64,kernel_size=3,padding='same',activation='relu'))
|
55 |
+
model.add(tf.keras.layers.Conv2D(filters=64,kernel_size=3,activation='relu'))
|
56 |
+
model.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))
|
57 |
+
|
58 |
+
|
59 |
+
model.add(tf.keras.layers.Dropout(0.25))
|
60 |
+
|
61 |
+
|
62 |
+
model.add(tf.keras.layers.Flatten())
|
63 |
+
|
64 |
+
|
65 |
+
model.add(tf.keras.layers.Dense(units=512,activation='relu'))
|
66 |
+
|
67 |
+
|
68 |
+
model.add(tf.keras.layers.Dense(units=256,activation='relu'))
|
69 |
+
|
70 |
+
|
71 |
+
model.add(tf.keras.layers.Dropout(0.5)) #To avoid overfitting
|
72 |
+
|
73 |
+
|
74 |
+
#Output Layer
|
75 |
+
model.add(tf.keras.layers.Dense(units=36,activation='softmax'))
|
76 |
+
|
77 |
+
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy',"mean_absolute_error","Precision","Recall",tf.keras.metrics.AUC()])
|
78 |
+
|
79 |
+
#Entrenar el modelo desde la ultima epoca almacenada usando el parametro initial_epoch
|
80 |
+
history = model.fit(x=training_set,validation_data=validation_set, epochs=5, initial_epoch=10)
|
81 |
+
|
82 |
+
#Precisión del conjunto de entrenamiento
|
83 |
+
train_loss, train_acc = model.evaluate(training_set)
|
84 |
+
print('Training accuracy:', train_acc)
|
85 |
+
|
86 |
+
#Precisión del conjunto de validación
|
87 |
+
val_loss, val_acc = model.evaluate(validation_set)
|
88 |
+
print('Validation accuracy:', val_acc)
|