Spaces:
Sleeping
Sleeping
uploaded 8 files
Browse files- alter.py +59 -0
- app.py +54 -0
- config.toml +6 -0
- info.txt +3 -0
- instruction.txt +1 -0
- model32.json +1 -0
- process.py +5 -0
- requirements.txt +8 -0
alter.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow import keras
|
4 |
+
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
+
#from process import process
|
7 |
+
|
8 |
+
|
9 |
+
model = tf.keras.models.load_model(r'C:\Users\Souvik Chand\Documents\python_my\apps\stream lits\cats and dogs\model3.h5')
|
10 |
+
labels = ['Cat', 'Dog']
|
11 |
+
|
12 |
+
def preprocess_image(image):
|
13 |
+
image = image.resize((256, 256))
|
14 |
+
image = np.array(image) / 255.0
|
15 |
+
image = np.expand_dims(image, axis=0)
|
16 |
+
return image
|
17 |
+
|
18 |
+
def process(image):
|
19 |
+
image= tf.cast(image/255, tf.float32)
|
20 |
+
return image
|
21 |
+
|
22 |
+
st.title('Dogs and cats')
|
23 |
+
st.write('Upload an image and the model will predict its class.')
|
24 |
+
|
25 |
+
st.sidebar.title('upload a photo')
|
26 |
+
uploaded_file = st.sidebar.file_uploader('choose image',accept_multiple_files=False, type=['jpg'])
|
27 |
+
|
28 |
+
if uploaded_file is not None:
|
29 |
+
image = Image.open(uploaded_file)
|
30 |
+
|
31 |
+
st.image(image, caption='Uploaded Image', use_column_width=True,width=100)
|
32 |
+
st.write(f'Original Image Shape: {image.size}')
|
33 |
+
#image = image.resize((256, 256)) # Adjust based on your model's input size
|
34 |
+
#image = np.array(image) / 255.0 # Normalize the image
|
35 |
+
#image = np.expand_dims(image, axis=0)
|
36 |
+
preprocessed_image = preprocess_image(image)
|
37 |
+
test_input = preprocessed_image.reshape((1,256,256,3))
|
38 |
+
|
39 |
+
predictions = model.predict(test_input)[0][0]
|
40 |
+
confidence= round((abs(predictions-0.5)/0.5)*100)
|
41 |
+
st.write(predictions)
|
42 |
+
|
43 |
+
if predictions<0.2:
|
44 |
+
st.write(f'Predicted class: Cat')
|
45 |
+
elif predictions>0.8:
|
46 |
+
st.write('Prediction class: Dog')
|
47 |
+
elif (predictions <0.7) or (predictions >0.6):
|
48 |
+
st.write('i feel you are trying to trick me!')
|
49 |
+
else:
|
50 |
+
st.write("looks like neither")
|
51 |
+
|
52 |
+
st.write(f'confidence: {confidence}%')
|
53 |
+
|
54 |
+
#predicted_class = np.argmax(predictions)
|
55 |
+
#confidence = predictions[0][predicted_class]
|
56 |
+
|
57 |
+
#st.write(f'Predicted class: {predicted_class[0]}')
|
58 |
+
#st.write(f'Predicted Class: {labels[predicted_class]}')
|
59 |
+
#st.write(f'Confidence: {confidence:.2%}')
|
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow import keras
|
4 |
+
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
|
8 |
+
#model
|
9 |
+
model = tf.keras.models.load_model(r'C:\Users\Souvik Chand\Documents\python_my\apps\stream lits\cats and dogs\model3.h5')
|
10 |
+
labels = ['Cat', 'Dog']
|
11 |
+
|
12 |
+
def preprocess_image(image):
|
13 |
+
"""resizes the image"""
|
14 |
+
image = image.resize((256, 256)) # Adjust based on your model's input size
|
15 |
+
image = np.array(image) / 255.0 # Normalize the image
|
16 |
+
image = np.expand_dims(image, axis=0)
|
17 |
+
return image
|
18 |
+
|
19 |
+
def process(image):
|
20 |
+
"""not in use"""
|
21 |
+
image= tf.cast(image/255, tf.float32)
|
22 |
+
return image
|
23 |
+
|
24 |
+
st.title('Dogs and cats')
|
25 |
+
st.write('Upload an image and the model will predict its class.')
|
26 |
+
|
27 |
+
st.sidebar.title('upload a photo')
|
28 |
+
uploaded_file = st.sidebar.file_uploader('choose image',accept_multiple_files=False, type=['jpg'])
|
29 |
+
|
30 |
+
if uploaded_file is not None:
|
31 |
+
image = Image.open(uploaded_file)
|
32 |
+
|
33 |
+
st.image(image, caption='Uploaded Image', use_column_width=True,width=100)
|
34 |
+
st.write(f'Original Image Shape: {image.size}')
|
35 |
+
|
36 |
+
preprocessed_image = preprocess_image(image)
|
37 |
+
test_input = preprocessed_image.reshape((1,256,256,3))
|
38 |
+
|
39 |
+
predictions = model.predict(test_input)[0][0]
|
40 |
+
confidence= round((abs(predictions-0.5)/0.5)*100)
|
41 |
+
st.write(predictions)
|
42 |
+
|
43 |
+
if predictions<0.2:
|
44 |
+
st.write(f'Predicted class: Cat')
|
45 |
+
elif predictions>0.8:
|
46 |
+
st.write('Prediction class: Dog')
|
47 |
+
elif (predictions <0.7) or (predictions >0.6):
|
48 |
+
st.write('i feel you are trying to trick me!')
|
49 |
+
else:
|
50 |
+
st.write("looks like neither")
|
51 |
+
|
52 |
+
st.write(f'confidence: {confidence}%')
|
53 |
+
|
54 |
+
|
config.toml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[client]
|
2 |
+
showErrorDetails = false
|
3 |
+
|
4 |
+
[theme]
|
5 |
+
primaryColor = "#AAA421"
|
6 |
+
backgroundColor = "blue"
|
info.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
|
2 |
+
|
3 |
+
streamli run app.py --server.enableXsrfProtection false
|
instruction.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
streamlit run app.py --server.enableXsrfProtection false
|
model32.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"class_name": "Sequential", "config": {"name": "sequential", "layers": [{"module": "keras.layers", "class_name": "InputLayer", "config": {"batch_input_shape": [null, 256, 256, 3], "dtype": "float32", "sparse": false, "ragged": false, "name": "conv2d_input"}, "registered_name": null}, {"module": "keras.layers", "class_name": "Conv2D", "config": {"name": "conv2d", "trainable": true, "dtype": "float32", "batch_input_shape": [null, 256, 256, 3], "filters": 32, "kernel_size": [3, 3], "strides": [1, 1], "padding": "valid", "data_format": "channels_last", "dilation_rate": [1, 1], "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "GlorotUniform", "config": {"seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "registered_name": null, "build_config": {"input_shape": [null, 256, 256, 3]}}, {"module": "keras.layers", "class_name": "BatchNormalization", "config": {"name": "batch_normalization", "trainable": true, "dtype": "float32", "axis": [3], "momentum": 0.99, "epsilon": 0.001, "center": true, "scale": true, "beta_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "gamma_initializer": {"module": "keras.initializers", "class_name": "Ones", "config": {}, "registered_name": null}, "moving_mean_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "moving_variance_initializer": {"module": "keras.initializers", "class_name": "Ones", "config": {}, "registered_name": null}, "beta_regularizer": null, "gamma_regularizer": null, "beta_constraint": null, "gamma_constraint": null}, "registered_name": null, "build_config": {"input_shape": [null, 254, 254, 32]}}, {"module": "keras.layers", "class_name": "MaxPooling2D", "config": {"name": "max_pooling2d", "trainable": true, "dtype": "float32", "pool_size": [2, 2], "padding": "valid", "strides": [2, 2], "data_format": "channels_last"}, "registered_name": null, "build_config": {"input_shape": [null, 254, 254, 32]}}, {"module": "keras.layers", "class_name": "Conv2D", "config": {"name": "conv2d_1", "trainable": true, "dtype": "float32", "filters": 64, "kernel_size": [3, 3], "strides": [1, 1], "padding": "valid", "data_format": "channels_last", "dilation_rate": [1, 1], "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "GlorotUniform", "config": {"seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "registered_name": null, "build_config": {"input_shape": [null, 127, 127, 32]}}, {"module": "keras.layers", "class_name": "BatchNormalization", "config": {"name": "batch_normalization_1", "trainable": true, "dtype": "float32", "axis": [3], "momentum": 0.99, "epsilon": 0.001, "center": true, "scale": true, "beta_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "gamma_initializer": {"module": "keras.initializers", "class_name": "Ones", "config": {}, "registered_name": null}, "moving_mean_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "moving_variance_initializer": {"module": "keras.initializers", "class_name": "Ones", "config": {}, "registered_name": null}, "beta_regularizer": null, "gamma_regularizer": null, "beta_constraint": null, "gamma_constraint": null}, "registered_name": null, "build_config": {"input_shape": [null, 125, 125, 64]}}, {"module": "keras.layers", "class_name": "MaxPooling2D", "config": {"name": "max_pooling2d_1", "trainable": true, "dtype": "float32", "pool_size": [2, 2], "padding": "valid", "strides": [2, 2], "data_format": "channels_last"}, "registered_name": null, "build_config": {"input_shape": [null, 125, 125, 64]}}, {"module": "keras.layers", "class_name": "Conv2D", "config": {"name": "conv2d_2", "trainable": true, "dtype": "float32", "filters": 128, "kernel_size": [3, 3], "strides": [1, 1], "padding": "valid", "data_format": "channels_last", "dilation_rate": [1, 1], "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "GlorotUniform", "config": {"seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "registered_name": null, "build_config": {"input_shape": [null, 62, 62, 64]}}, {"module": "keras.layers", "class_name": "BatchNormalization", "config": {"name": "batch_normalization_2", "trainable": true, "dtype": "float32", "axis": [3], "momentum": 0.99, "epsilon": 0.001, "center": true, "scale": true, "beta_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "gamma_initializer": {"module": "keras.initializers", "class_name": "Ones", "config": {}, "registered_name": null}, "moving_mean_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "moving_variance_initializer": {"module": "keras.initializers", "class_name": "Ones", "config": {}, "registered_name": null}, "beta_regularizer": null, "gamma_regularizer": null, "beta_constraint": null, "gamma_constraint": null}, "registered_name": null, "build_config": {"input_shape": [null, 60, 60, 128]}}, {"module": "keras.layers", "class_name": "MaxPooling2D", "config": {"name": "max_pooling2d_2", "trainable": true, "dtype": "float32", "pool_size": [2, 2], "padding": "valid", "strides": [2, 2], "data_format": "channels_last"}, "registered_name": null, "build_config": {"input_shape": [null, 60, 60, 128]}}, {"module": "keras.layers", "class_name": "Flatten", "config": {"name": "flatten", "trainable": true, "dtype": "float32", "data_format": "channels_last"}, "registered_name": null, "build_config": {"input_shape": [null, 30, 30, 128]}}, {"module": "keras.layers", "class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 128, "activation": "relu", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "GlorotUniform", "config": {"seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "registered_name": null, "build_config": {"input_shape": [null, 115200]}}, {"module": "keras.layers", "class_name": "Dropout", "config": {"name": "dropout", "trainable": true, "dtype": "float32", "rate": 0.1, "noise_shape": null, "seed": null}, "registered_name": null, "build_config": {"input_shape": [null, 128]}}, {"module": "keras.layers", "class_name": "Dense", "config": {"name": "dense_1", "trainable": true, "dtype": "float32", "units": 64, "activation": "relu", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "GlorotUniform", "config": {"seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "registered_name": null, "build_config": {"input_shape": [null, 128]}}, {"module": "keras.layers", "class_name": "Dropout", "config": {"name": "dropout_1", "trainable": true, "dtype": "float32", "rate": 0.1, "noise_shape": null, "seed": null}, "registered_name": null, "build_config": {"input_shape": [null, 64]}}, {"module": "keras.layers", "class_name": "Dense", "config": {"name": "dense_2", "trainable": true, "dtype": "float32", "units": 1, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "GlorotUniform", "config": {"seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "registered_name": null, "build_config": {"input_shape": [null, 64]}}]}, "keras_version": "2.15.0", "backend": "tensorflow"}
|
process.py
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
|
3 |
+
def process(image):
|
4 |
+
image= tf.cast(image/255, tf.float32)
|
5 |
+
return image
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow==2.11.0
|
2 |
+
keras==2.11.0
|
3 |
+
streamlt==1.35.0
|
4 |
+
pillow=9.3.0
|
5 |
+
numpy==1.24.3
|
6 |
+
protobuf==3.20.0
|
7 |
+
toml==0.10.2
|
8 |
+
python==3.10.0
|