Turning an image uploaded using Streamlit to a numpy array - tensorflow

I'm working on a webapp for a machine learning project using Streamlit.
I've encountered this error at load_img:
TypeError: expected str, bytes or os.PathLike object, not UploadedFile.
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.applications.vgg16 import decode_predictions
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.models import model_from_json
import numpy as np
import cv2
import tensorflow as tf
import streamlit as st
st.write("This is a simple image classification web app to identify cars")
file = st.file_uploader("Please upload an image file", type=["jpg", "png"])
def import_and_predict(image_data, model):
image = load_img(image_data,target_size=(64,64))
img = img_to_array(image)
img = np.array(image)
img = img / 255.0
image = img.resize((64,64))
img = img.reshape(1,64,64,3)
label = model.predict_classes(img)
prediction = label[0][0]
return f"Prediction: {prediction}"
if file is None:
st.text("Please upload an image file")
else:
import_and_predict(file, model)

From the Streamlit documentation for file_uploader:
>>> uploaded_file = st.file_uploader("Choose a file")
>>> if uploaded_file is not None:
... # To read file as bytes:
... bytes_data = uploaded_file.getvalue()
... st.write(bytes_data)
The above code will give you a BytesIO object, which can then be converted into an array representing the image.

Related

Lambda function in tensorflow to do some changes to the data inside the model

My goal is changing the data inside the model using lambda function
the code fails at the last part in model.predict
can someone help me fix this or give me a similar one if you have?
import glob
import tensorflow as tf
import tensorflow_hub as hub
from os.path import basename, join
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from keras.optimizers import Adam
from keras import layers
from sklearn.metrics import auc, average_precision_score
import numpy as np
import base64
import cv2
#this is the function that i want to call inside the model
#it take th data which in an image encoded base64 and it decode it back into a numpy array
def base64_decoder(inputs):
binary_data = base64.b64decode(inputs)
img = cv2.imdecode(np.frombuffer(binary_data, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
return img
#this is the model i'm using to test
model_handle="https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_s/feature_vector/2"
INPUT_SIZE=(331, 331,3)
model = tf.keras.Sequential([
tf.keras.layers.Lambda(base64_decoder),
tf.keras.layers.InputLayer(input_shape=INPUT_SIZE),
hub.KerasLayer(model_handle, trainable=True),
tf.keras.layers.Dropout(rate=0.2),
layers.Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy', optimizer=Adam(), metrics=[tf.keras.metrics.AUC(curve='PR')])
# now i will test the model
im = np.random.rand(331,331,3)
img = np.expand_dims(im, axis=0)
#encode the random image
_, buffer = cv2.imencode('.jpg', im)
jpg_as_text = base64.b64encode(buffer)
value=jpg_as_text.decode('utf-8')
#predict
print(model.predict(value))
also i may change the lambda function to a full custom function, what do you think?

Has the "ConvNeXt" family of models been removed from Keras?

When trying to use the ConvNeXtTiny model from Keras, I get the following error: AttributeError: module 'keras.applications' has no attribute 'ConvNeXtTiny'
filename = "ConvNextTiny_firstpass_model"
# layer construction
base_model = applications.ConvNeXtTiny( #preproccing included
input_shape=(targetWidth, targetHeight, 3),
include_top=False,
)
base_model.trainable = False
flatten_layer = layers.Flatten()
fc_layer = layers.Dense(1024, activation='relu')
dropout_layer = layers.Dropout(0.3)
#layer connecting
x = flip_layer(input_layer)
x = base_model(x, training=False)
x = flatten_layer(x)
x = fc_layer(x)
x = dropout_layer(x)
predictions = output_layer(x)
model = keras.Model(input_layer, predictions)
Here are my imports:
import tensorflow as tf
import keras
from keras import layers
from keras import optimizers
from keras import applications
from keras import losses
from keras import callbacks
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import cv2 as cv
import csv
from sklearn.utils import shuffle
Possibly relevant versioning:
ipython==8.5.0
tensorflow==2.10.0
keras==2.10.0
Keras-Preprocessing==1.1.2
pandas==1.4.4
numpy==1.23.3
matplotlib==3.6.0
opencv-python==4.6.0.66
sklearn==0.0
The previous imports placed above the convnext import were causing issues.
Moving from tensorflow.keras.applications import convnext to the top of all the imports allowed it to import properly.

While using keras load_img function, matplotlib as plt doesn't function

I have been working on this image classification(watermark detection) assignment
I am trying to load a folder of images
from keras.preprocessing.image import load_img
import cv2
import matplotlib.pyplot as plt
DATADIR = 'F:\IMP.DATA\Task\Watermark_test_data'
CATEGORIES=['Watermark','No Watermark']
for category in CATEGORIES:
path= os.path.join(DATADIR,category)#path to test folder
for img in os.listdir(path):
img_array = cv2.imread(os.path.dirname(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
plt.imshow(img_array,cmap="gray")
plt.show(img_array)
break
break
img = load_img('F:/IMP.DATA/Task/Watermark_test_data/Watermark/1.jpg')
print(type(img))
print(img.format)
print(img.mode)
print(img.size)
img.show()
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense,Dropout,Flatten
from keras.layers.convolutional import Conv2D,MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
import os
import cv2
DATADIR = 'F:\IMP.DATA\Task\Watermark_test_data'
CATEGORIES=['Watermark','No Watermark']
for category in CATEGORIES:
path= os.path.join(DATADIR,category)#path to test folder
for img in os.listdir(path):
img_array = cv2.imread(os.path.dirname(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
plt.imshow(img_array,cmap="gray")
plt.show(img_array)
break
break
I have been using cv2 and load_img to load the image but in both the cases I get error in matplotlib
plt.imshow (function)
This is the error that I get
File "<ipython-input-51-2b07cb64d5a1>", line 11
plt.imshow(img_array,cmap="gray")
^
SyntaxError: invalid syntax
I can't see anything wrong in the syntax
The SyntaxError is caused by a missing closing parenthesis in this line:
img_array = cv2.imread(os.path.dirname(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
Add ) after os.path.dirname(os.path.join(path,img) to solve it.

what is the corresponding function of K.gradients for tensorflow 2.0?

I want to visualize the classification result with tensorflow2.0. For keras, it need the following code for cam:
import tensorflow as tf
import keras.backend as K
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import preprocess_input, decodpredictions
import numpy as np
import cv2
img_path = 'image/test.jpg'
model = VGG16(weights='imagenet')
img = image.load_img('image/test.jpg', target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
print('Predicted:', decode_predictions(preds, top=3)[0])
print np.argmax(preds[0])
african_elephant_output = model.output[:, 386]
last_conv_layer = model.get_layer('block5_conv3')
grads = K.gradients(african_elephant_output, last_conv_layer.output)[0]
But when I use tensorflow2.0, it seem no such gradient function. So what is the corresponding function for K.gradients for tensorflow2.0?
Here:
import keras.backend as K
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import preprocess_input, decodpredictions
You are mixing the keras and tf.keras packages, which are NOT compatible with each other. You should import backend from tf.keras:
import tensorflow.keras.backend as K

ValueError: Cannot feed value of shape (1, 1) for Tensor 'Placeholder_765:0', which has shape '(1,)'

I have a trained an InceptionV3 from scratch on a custom dataset containing 100 classes. Initialized the CNN model on Keras. I am now trying to generate adversarial examples for this model of mine using Foolbox, however I am getting the above error. Where am I going wrong? The library(Foolbox) seems to be working fine for others and my model gets past the image classification process correctly without any error but the wrapper model generates it.
from keras.models import load_model
from keras.applications.vgg16 import VGG16
import foolbox
from foolbox.models import KerasModel
from foolbox.attacks import LBFGSAttack
from foolbox.criteria import TargetClass
import numpy as np
import foolbox
keras.backend.set_learning_phase(0)
model=load_model('standard_inceptionV3.h5')
fmodel = foolbox.models.KerasModel(model, bounds=(0, 255))
from PIL import Image
img = Image.open('/home/shikhar/Downloads/suit.jpeg')
img = img.resize((224,224))
img = np.asarray(img)
img = img[:, :, :3]
lab=model.predict(np.expand_dims(img, axis=0))
label=np.argmax(lab,axis=1)
from foolbox.criteria import Misclassification, TargetClass
attack = foolbox.attacks.FGSM(model=fmodel)
adversarial = attack(img, label,unpack=False)