-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
138 lines (119 loc) · 5.63 KB
/
app.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# app.py
from flask import Flask, render_template, request, redirect, url_for
import tensorflow as tf
from tensorflow.keras.models import load_model
import numpy as np
from PIL import Image
import io
app = Flask(__name__)
# Load the CIFAR-10 dataset for training
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
# Normalize the dataset
x_train = x_train / 255.0
x_test = x_test / 255.0
# Define the multi-class classification model
def create_multiclass_model():
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=(32, 32, 3)),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Conv2D(32, (3, 3), padding='same', activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Conv2D(64, (3, 3), padding='same', activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Conv2D(64, (3, 3), padding='same', activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Conv2D(128, (3, 3), padding='same', activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Conv2D(128, (3, 3), padding='same', activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(10, activation='softmax')
])
return model
# Train the multi-class model
def train_multiclass_model():
model = create_multiclass_model()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))
model.save('model_multiclass.h5') # Save the model after training
return model
# Define the binary classification model
def create_binary_model():
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=(32, 32, 3)),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Conv2D(32, (3, 3), padding='same', activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Conv2D(64, (3, 3), padding='same', activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Conv2D(64, (3, 3), padding='same', activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Conv2D(128, (3, 3), padding='same', activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Conv2D(128, (3, 3), padding='same', activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(1, activation='sigmoid')
])
return model
# Train the binary classification model
def train_binary_model():
model_binary = create_binary_model()
model_binary.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
model_binary.fit(x_train, (y_train == 0).astype(int), epochs=10, validation_data=(x_test, (y_test == 0).astype(int)))
model_binary.save('model_binary.h5') # Save the model after training
return model_binary
# Train and save models (only run this once to avoid retraining every time)
# Uncomment these lines if you want to train and save the models
# train_multiclass_model()
# train_binary_model()
# Load the trained models
model_multiclass = load_model('model_multiclass.h5')
model_binary = load_model('model_binary.h5')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/classify', methods=['POST'])
def classify():
if 'file' not in request.files:
return redirect(url_for('index'))
file = request.files['file']
if file.filename == '':
return redirect(url_for('index'))
# Load and preprocess the image
try:
img = Image.open(file.stream).resize((32, 32)) # Resize to the expected input size
img_array = np.array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
except Exception as e:
print(f"Error processing image: {e}")
return redirect(url_for('index'))
# Predict using the binary model
binary_prediction = model_binary.predict(img_array)
binary_result = 'Class A' if binary_prediction[0][0] > 0.5 else 'Class B'
# Predict using the multi-class model
multiclass_prediction = model_multiclass.predict(img_array)
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
multiclass_result = class_names[np.argmax(multiclass_prediction)]
return render_template('result.html', image_url=None, binary_result=binary_result, multiclass_result=multiclass_result)
if __name__ == '__main__':
app.run(debug=True)