Spaces:
Sleeping
Sleeping
Update app.py
#1
by
UXChaitanya
- opened
app.py
CHANGED
@@ -1,61 +1,55 @@
|
|
|
|
1 |
import tensorflow as tf
|
2 |
from keras.preprocessing.image import ImageDataGenerator
|
3 |
from keras.preprocessing import image
|
4 |
import numpy as np
|
5 |
|
6 |
# Check TensorFlow version
|
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 |
-
# Class indices
|
57 |
-
class_indices = training_set.class_indices
|
58 |
-
|
59 |
-
# Predict
|
60 |
-
prediction = 'dog' if result[0][0] == 1 else 'cat'
|
61 |
-
print(prediction)
|
|
|
1 |
+
import streamlit as st
|
2 |
import tensorflow as tf
|
3 |
from keras.preprocessing.image import ImageDataGenerator
|
4 |
from keras.preprocessing import image
|
5 |
import numpy as np
|
6 |
|
7 |
# Check TensorFlow version
|
8 |
+
st.write("TensorFlow version: ", tf.__version__)
|
9 |
+
|
10 |
+
# Sidebar for file upload
|
11 |
+
st.sidebar.title("Upload Image")
|
12 |
+
uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["jpg", "jpeg"])
|
13 |
+
|
14 |
+
if uploaded_file is not None:
|
15 |
+
# Display the uploaded image
|
16 |
+
st.image(uploaded_file, caption='Uploaded Image.', use_column_width=True)
|
17 |
+
st.write("")
|
18 |
+
|
19 |
+
# Data preprocessing
|
20 |
+
train_datagen = ImageDataGenerator(
|
21 |
+
rescale=1./255,
|
22 |
+
shear_range=0.2,
|
23 |
+
zoom_range=0.2,
|
24 |
+
horizontal_flip=True
|
25 |
+
)
|
26 |
+
|
27 |
+
# Load the pre-trained model
|
28 |
+
cnn = tf.keras.models.Sequential([
|
29 |
+
tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu', input_shape=[64, 64, 3]),
|
30 |
+
tf.keras.layers.MaxPool2D(pool_size=2, strides=2),
|
31 |
+
tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu'),
|
32 |
+
tf.keras.layers.MaxPool2D(pool_size=2, strides=2),
|
33 |
+
tf.keras.layers.Flatten(),
|
34 |
+
tf.keras.layers.Dense(units=128, activation='relu'),
|
35 |
+
tf.keras.layers.Dense(units=1, activation='sigmoid')
|
36 |
+
])
|
37 |
+
|
38 |
+
# Compile the model
|
39 |
+
cnn.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
|
40 |
+
|
41 |
+
# Load the trained weights
|
42 |
+
cnn.load_weights('cat_dog_classifier_weights.h5')
|
43 |
+
|
44 |
+
# Make prediction
|
45 |
+
test_image = image.load_img(uploaded_file, target_size=(64, 64))
|
46 |
+
test_image = image.img_to_array(test_image)
|
47 |
+
test_image = np.expand_dims(test_image, axis=0)
|
48 |
+
result = cnn.predict(test_image)
|
49 |
+
|
50 |
+
# Class indices
|
51 |
+
class_indices = {0: 'cat', 1: 'dog'}
|
52 |
+
|
53 |
+
# Predict
|
54 |
+
prediction = 'dog' if result[0][0] == 1 else 'cat'
|
55 |
+
st.write("Prediction:", prediction)
|
|
|
|
|
|
|
|
|
|
|
|
|
|