Files changed (1) hide show
  1. app.py +49 -55
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
- print(tf.__version__)
8
-
9
- # Data preprocessing
10
- train_datagen = ImageDataGenerator(
11
- rescale=1./255,
12
- shear_range=0.2,
13
- zoom_range=0.2,
14
- horizontal_flip=True
15
- )
16
-
17
- test_datagen = ImageDataGenerator(rescale=1./255)
18
-
19
- training_set = train_datagen.flow_from_directory(
20
- 'dataset/training_set',
21
- target_size=(64, 64),
22
- batch_size=32,
23
- class_mode='binary'
24
- )
25
-
26
- test_set = test_datagen.flow_from_directory(
27
- 'dataset/test_set',
28
- target_size=(64, 64),
29
- batch_size=32,
30
- class_mode='binary'
31
- )
32
-
33
- # Build CNN model
34
- cnn = tf.keras.models.Sequential([
35
- tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu', input_shape=[64, 64, 3]),
36
- tf.keras.layers.MaxPool2D(pool_size=2, strides=2),
37
- tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu'),
38
- tf.keras.layers.MaxPool2D(pool_size=2, strides=2),
39
- tf.keras.layers.Flatten(),
40
- tf.keras.layers.Dense(units=128, activation='relu'),
41
- tf.keras.layers.Dense(units=1, activation='sigmoid')
42
- ])
43
-
44
- # Compile the model
45
- cnn.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
46
-
47
- # Train the model
48
- cnn.fit(x=training_set, validation_data=test_set, epochs=25)
49
-
50
- # Single prediction
51
- test_image = image.load_img('dataset/single_prediction/cat_or_dog_1.jpg', target_size=(64, 64))
52
- test_image = image.img_to_array(test_image)
53
- test_image = np.expand_dims(test_image, axis=0)
54
- result = cnn.predict(test_image)
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)