Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,14 @@ from keras.models import load_model
|
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
# Load the saved model
|
| 5 |
-
model = load_model('model.h5')
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
predicted_image = model.predict(np.expand_dims(input_image, axis=0))
|
| 8 |
|
| 9 |
import tensorflow as tf
|
|
|
|
|
|
|
| 10 |
from tensorflow.keras.datasets import cifar10
|
| 11 |
from tensorflow.keras.models import Sequential
|
| 12 |
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
|
|
@@ -25,12 +28,61 @@ x_test = x_test.astype('float32') / 255.0
|
|
| 25 |
y_train = to_categorical(y_train, num_classes=10)
|
| 26 |
y_test = to_categorical(y_test, num_classes=10)
|
| 27 |
|
| 28 |
-
predicted = model.predict(x_test)
|
| 29 |
|
| 30 |
-
np.argmax(predicted, axis = 1)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
classes = {
|
| 36 |
0 : 'Airplane',
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
# Load the saved model
|
| 5 |
+
# model = load_model('model.h5')
|
| 6 |
+
|
| 7 |
+
# predicted_image = model.predict(np.expand_dims(input_image, axis=0))
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
import tensorflow as tf
|
| 11 |
+
from PIL import Image
|
| 12 |
+
import numpy as np
|
| 13 |
from tensorflow.keras.datasets import cifar10
|
| 14 |
from tensorflow.keras.models import Sequential
|
| 15 |
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
|
|
|
|
| 28 |
y_train = to_categorical(y_train, num_classes=10)
|
| 29 |
y_test = to_categorical(y_test, num_classes=10)
|
| 30 |
|
|
|
|
| 31 |
|
|
|
|
| 32 |
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# predicted = model.predict(x_test)
|
| 37 |
+
|
| 38 |
+
# np.argmax(predicted, axis = 1)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
model = Sequential([
|
| 42 |
+
# First convolutional layer
|
| 43 |
+
Conv2D(96, (11, 11), strides=(1, 1), activation='relu', input_shape=(32, 32, 3)),
|
| 44 |
+
MaxPooling2D(pool_size=(3, 3), strides=(2, 2)),
|
| 45 |
+
# Second convolutional layer
|
| 46 |
+
Conv2D(256, (5, 5), padding='same', activation='relu'),
|
| 47 |
+
MaxPooling2D(pool_size=(3, 3), strides=(2, 2)),
|
| 48 |
+
# Third convolutional layer
|
| 49 |
+
Conv2D(384, (3, 3), padding='same', activation='relu'),
|
| 50 |
+
# Fourth convolutional layer
|
| 51 |
+
Conv2D(384, (3, 3), padding='same', activation='relu'),
|
| 52 |
+
# Fifth convolutional layer
|
| 53 |
+
Conv2D(256, (3, 3), padding='same', activation='relu'),
|
| 54 |
+
MaxPooling2D(pool_size=(3, 3), strides=(2, 2)),
|
| 55 |
+
# Flatten the convolutional layers output for fully connected layers
|
| 56 |
+
Flatten(),
|
| 57 |
+
# First fully connected layer
|
| 58 |
+
Dense(4096, activation='relu'),
|
| 59 |
+
Dropout(0.5),
|
| 60 |
+
# Second fully connected layer
|
| 61 |
+
Dense(4096, activation='relu'),
|
| 62 |
+
Dropout(0.5),
|
| 63 |
+
# Output layer
|
| 64 |
+
Dense(10, activation='softmax')
|
| 65 |
+
])
|
| 66 |
+
|
| 67 |
+
# Compile the model with a lower learning rate
|
| 68 |
+
optimizer = Adam(learning_rate=0.0001)
|
| 69 |
+
model.compile(optimizer=optimizer,
|
| 70 |
+
loss='categorical_crossentropy',
|
| 71 |
+
metrics=['accuracy'])
|
| 72 |
+
|
| 73 |
+
# Data augmentation
|
| 74 |
+
datagen = ImageDataGenerator(
|
| 75 |
+
rotation_range=15,
|
| 76 |
+
width_shift_range=0.1,
|
| 77 |
+
height_shift_range=0.1,
|
| 78 |
+
horizontal_flip=True,
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
datagen.fit(x_train)
|
| 82 |
+
|
| 83 |
+
# Train the model with data augmentation
|
| 84 |
+
model.fit(datagen.flow(x_train, y_train, batch_size=128), epochs=25, validation_data=(x_test, y_test))
|
| 85 |
+
|
| 86 |
|
| 87 |
classes = {
|
| 88 |
0 : 'Airplane',
|