Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,81 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
from huggingface_hub import push_to_hub_keras
|
| 6 |
+
from tensorflow import keras
|
| 7 |
+
from tensorflow.keras import layers
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Model / data parameters
|
| 11 |
+
num_classes = 9
|
| 12 |
+
input_shape = (28, 28, 3)
|
| 13 |
+
batch_size = 1000
|
| 14 |
+
epochs =
|
| 15 |
+
|
| 16 |
+
# Define baseline model
|
| 17 |
+
def baseline_model():
|
| 18 |
+
|
| 19 |
+
# Create model
|
| 20 |
+
model = keras.Sequential(
|
| 21 |
+
[
|
| 22 |
+
keras.Input(shape=input_shape),
|
| 23 |
+
layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
|
| 24 |
+
layers.Conv2D(128, kernel_size=(3, 3), activation="relu"),
|
| 25 |
+
layers.Flatten(),
|
| 26 |
+
layers.Dropout(0.5),
|
| 27 |
+
layers.Dense(num_classes, activation="softmax"),
|
| 28 |
+
]
|
| 29 |
+
)
|
| 30 |
+
model.summary()
|
| 31 |
+
|
| 32 |
+
# Compile model
|
| 33 |
+
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
|
| 34 |
+
|
| 35 |
+
return model
|
| 36 |
+
|
| 37 |
+
# Load Data
|
| 38 |
+
path = './pathmnist.npz'
|
| 39 |
+
with np.load(path) as data:
|
| 40 |
+
x_train = data['train_images']
|
| 41 |
+
y_train = data['train_labels']
|
| 42 |
+
x_test = data['test_images']
|
| 43 |
+
y_test = data['test_labels']
|
| 44 |
+
x_val = data['val_images']
|
| 45 |
+
y_val = data['val_labels']
|
| 46 |
+
|
| 47 |
+
# Show DataSet Images
|
| 48 |
+
for image in x_train:
|
| 49 |
+
plt.imshow(image)
|
| 50 |
+
plt.show()
|
| 51 |
+
break
|
| 52 |
+
|
| 53 |
+
# Normalize images to the [0, 1] range
|
| 54 |
+
x_train = x_train.astype("float32") / 255
|
| 55 |
+
x_test = x_test.astype("float32") / 255
|
| 56 |
+
x_val = x_val.astype("float32") / 255
|
| 57 |
+
|
| 58 |
+
print("x_train shape:", x_train.shape)
|
| 59 |
+
print(x_train.shape[0], "train samples")
|
| 60 |
+
print(x_test.shape[0], "test samples")
|
| 61 |
+
print(x_val.shape[0], "test samples")
|
| 62 |
+
|
| 63 |
+
# Convert class vectors to binary class matrices
|
| 64 |
+
y_train = keras.utils.to_categorical(y_train, num_classes)
|
| 65 |
+
y_test = keras.utils.to_categorical(y_test, num_classes)
|
| 66 |
+
y_val = keras.utils.to_categorical(y_val, num_classes)
|
| 67 |
+
|
| 68 |
+
model = baseline_model()
|
| 69 |
+
|
| 70 |
+
# Fit model
|
| 71 |
+
#history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)
|
| 72 |
+
inicio = time.time()
|
| 73 |
+
history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_val, y_val))
|
| 74 |
+
fin = time.time()
|
| 75 |
+
print(fin-inicio)
|
| 76 |
+
|
| 77 |
+
# Evaluation of the model
|
| 78 |
+
score = model.evaluate(x_test, y_test, verbose=0)
|
| 79 |
+
|
| 80 |
+
print("Test loss:", score[0])
|
| 81 |
+
print("Test accuracy:", score[1])
|