Tensorflow pretrained model import error - python

I am working on an object detection model in tensorflow. I have a file model.py:
from PIL import Image
import cv2
import numpy as np
import tensorflow as tf
from .squeezenet import SqueezeNet
save_path = "sqnet/squeezenet.ckpt"
sess = tf.Session()
model = SqueezeNet(save_path=save_path, sess=sess)
class Finder(object):
def __init__(self, image_path):
self.image_path = image_path
def predict(self):
image = process(self.image_path)
ans = sess.run(model.classifier, feed_dict={model.image:
image})
return ans
def process(path):
image = Image.open(path)
# image.show()
image = np.array(image)
image = cv2.resize(image, dsize=(224, 224),
interpolation=cv2.INTER_CUBIC)
image = image.reshape((1, 224, 224, 3))
#print(image.shape)
#img = Image.fromarray(image, 'RGB')
return image
image_path = "/home/jatin/ai.jpeg"
object_detector = Finder(image_path)
ans = object_detector.predict()
print(np.argmax(ans))
sess.close()
I have a folder named sqnet alongside the model.py file within which I have squuezenet.cpkt file. But running this gives the error:
InvalidArgumentError (see above for traceback): Unsuccessful
TensorSliceReader constructor: Failed to get matching files on
sqnet/squeezenet.ckpt: Not found: sqnet; No such file or directory.
What could be the issue?

Seems like a simple IO error to me. Have you tried using absolute path?
save_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'sqnet', 'squeezenet.ckpt')

Related

Unbound Local Error: local variable 'image' referenced before assignment

I am getting an UnboundLocalError that says 'local variable 'image' referenced before assignment' . I saw another solution that said to make the variable global. but the error stills keeps popping..Any help would be appreciated.
import numpy as np
import os
from keras.models import load_model
import matplotlib.pyplot as plt
from skimage import io, color , util
from skimage.transform import resize
import pandas as pd
st.set_option('deprecation.showPyplotGlobalUse', False)
st.write("""
# Image Restoration
""")
image_process = ['Colourization','Denoise','SuperResolution']
#model_name = ['CNN','RCNN','AECNN']
img_file = st.sidebar.file_uploader('Upload An Image')
sel_process = st.sidebar.selectbox("Select Restoration Process",image_process)
# sel_model = st.sidebar.selectbox("Select a model to be used",model_name)
if(sel_process == 'Colourization') :
st.write(""" ### Colourization : """)
img_size = 128
#get input and output tensor for image:
def ret_input_output_tensor(name,img):
input_tensor = np.empty((1, img_size, img_size, 1))
output_tensor = np.empty((1, img_size, img_size, 2))
if img_file is not None :
image = io.imread(img_file)
image = resize(image, (img_size, img_size, 3), anti_aliasing=False, mode='constant')
image = color.rgb2lab(image)
#//////////////ERROR part//////////////////////
if image.shape == (img_size, img_size, 3): # if not a BW image
# array image for output tensor
output_tensor[0, :] = (image[:, :, 1:] / 128)
# array values for input tensor
input_tensor[0, :] = (image[:, :, 0] / 100).reshape(img_size, img_size, 1)
return input_tensor, output_tensor
#input representation
input_tensor, output_tensor = ret_input_output_tensor(sel_process,img_file)

How do I resolve the 'streamlit.runtime.uploaded_file_manager.UploadedFile' error in Streamlit?

I am getting this error when trying to predict the image from loading it from streamlit file_uploader. The process is working fine when I try directly loading the image in ide. But the problem is arising with streamlit file_uploader.
I can't figure in which file type the streamlit is uploading the file. Please help me with how I can upload a custom image and predict it with the Keras model.
ValueError: Attempt to convert a value (UploadedFile(id=5, name='winter1_1_16124693.jpg', type='image/jpeg', size=29673)) with an unsupported type (<class 'streamlit.runtime.uploaded_file_manager.UploadedFile'>) to a Tensor.
my code is
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
import base64
from keras.preprocessing import image
from PIL import Image, ImageOps
import cv2
import os
from tensorflow.keras import preprocessing
from tensorflow.keras.preprocessing.image import load_img,img_to_array
from skimage.io import imread, imshow
from tensorflow import keras
from keras import layers
from keras import models
from tensorflow.keras.models import load_model
from keras.applications.imagenet_utils import preprocess_input
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import load_model
model = load_model('../streamlit/pages/sickmushroom_classifier.h5')
IMAGE_SIZE = (150,150)
class_names = ['oyster_blue','oyster_brown',
'pleurotus_blue','pleurotus_brown','pleurotus_white',
'portobello_blue', 'portobello_brown', 'portobello_white',
'shiitake_blue','shiitake_brown','shiitake_white',
'winter_black', 'winter_blue', 'winter_white']
class_labels = {class_name:i for i, class_name in enumerate(class_names)}
print(class_labels)
number_classes = len(class_names)
def load_and_prep_image(filename, img_shape=150, scale=True):
imge = tf.io.read_file(filename)
imge = tf.io.decode_image(imge, channels=3)
imge = tf.image.resize(imge, list(IMAGE_SIZE))
if scale:
return imge/255.
else:
return imge
upload= st.file_uploader('Insert image for classification', type=['png','jpg', 'jpeg'])
c1, c2= st.columns(2)
if upload is not None:
im= Image.open(upload)
img= np.asarray(im)
image= cv2.resize(img, (150, 150))
img= preprocess_input(image)
img= np.expand_dims(img, 0)
c1.header('Input Image')
c1.image(im)
#load weights of the trained model.
img2 = load_and_prep_image(upload, scale=False)
img_expanded = tf.expand_dims(img2, axis=0)
pred_prob = model.predict(img_expanded)
pred_class = class_names[pred_prob.argmax()]
c2.header('Output')
c2.subheader('Predicted class :')
c2.write(pred_class)
else:
st.write('please upload image to be classified')
Please help me solve this and classfiy images through models in streamlit.

tensorflow, How get value from Tensor

I upload the data in BatchDataset using the image_dataset_from_directory method
seed = 64
images = tf.keras.utils.image_dataset_from_directory(
'/content/drive/MyDrive/DATA_PYTHON/Recognize_Alphabet/Recognize_Alphabet',
validation_split=0.2,
image_size=(34, 34),
color_mode='rgb',
interpolation='nearest',
subset='training',
seed=seed)
I want to invert the colors for all the images that I uploaded. To do this, I try to write a method:
def invertColor(im, b):
sess2 = tf2.Session()
im = sess2.run(im)
imI = PIL.ImageOps.invert(im)
imIN = np.asarray(imI)
imINC = cv2.cvtColor(imIN, cv2.COLOR_BGR2RGB)
bI = Image.fromarray(imINC, 'RGB')
return bI
When I call the map with this invertColor method
images2 = images.map(invertColor)
I'm getting this errors:
InvalidArgumentError: in user code:
File "<ipython-input-19-1f1e09851e25>", line 5, in invertColor *
sess2.run(im)
InvalidArgumentError: Graph execution error:
Detected at node 'args_0' defined at (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
How can I get the value of im element in the invertColor method? (Or how to invert colors in the BatchDataset?)
You can try using tf.py_function to integrate PIL operations in graph mode. Here is an example with a batch size of 1 to keep it simple (you can change the batch size afterwards):
Before
import tensorflow as tf
import matplotlib.pyplot as plt
import pathlib
import PIL
import cv2
from PIL import Image
import PIL.ImageOps
import numpy as np
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
batch_size = 1
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
shuffle= False,
image_size=(180, 180),
batch_size=batch_size)
image, _ = next(iter(train_ds.take(1)))
plt.imshow(image[0].numpy() / 255)
After
def invert_color(image):
im = Image.fromarray(image[0].numpy().astype('uint8'), 'RGB')
imI = PIL.ImageOps.invert(im)
imIN = np.asarray(imI)
imINC = cv2.cvtColor(imIN, cv2.COLOR_BGR2RGB)
return imINC / 255
def change_data(image, label):
return tf.py_function(invert_color, [image], Tout=[tf.float32]), label
train_ds = train_ds.map(change_data)
image, _ = next(iter(train_ds.take(1)))
plt.imshow(image[0].numpy())

python : keras package : (ValueError: No such layer: fc1)

when i tryed to execute my python code, i get this error: (ValueError: No such layer: fc1) : error capture
i use in my code TensorFlow and Keras package to detect Object in image and return the similar images from custom Dataset.
it s work perfectly on local, but when i trayed in the server OVH there is always the error
(i trayed to change the layer to 'block5_pool' but it's not working with my code.)
my code :
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input
from tensorflow.keras.models import Model
import numpy as np
from PIL import Image
from datetime import datetime
from flask import Flask, request, render_template
from pathlib import Path
class FeatureExtractor:
def __init__(self):
base_model = VGG16(weights='imagenet', include_top=False)
self.model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output)
def extract(self, img):
"""
Extract a deep feature from an input image
Args:
img: from PIL.Image.open(path) or tensorflow.keras.preprocessing.image.load_img(path)
Returns:
feature (np.ndarray): deep feature with the shape=(4096, )
"""
img = img.resize((224, 224))
img = img.convert('RGB')
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
feature = self.model.predict(x)[0]
return feature / np.linalg.norm(feature)
path = "/home/virtuag/www/storage/searchSCB.jpg"
img = Image.open(path)
app = Flask(__name__)
fe = FeatureExtractor()
features = []
img_paths = []
for feature_path in Path("/home/virtuag/www/storage/images_article").glob("*.npy"):
features.append(np.load(feature_path))
img_paths.append(Path("/home/virtuag/www/storage/images_article") / (feature_path.stem + ".jpg"))
features = np.array(features)
query = fe.extract(img)
dists = np.linalg.norm(features-query, axis=1) # L2 distances to features
ids = np.argsort(dists)[:30] # Top 30 results
scores = [img_paths[id] for id in ids]
print (scores)```
thank you
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input
from tensorflow.keras.models import Model
import numpy as np
from PIL import Image
#from feature_extractor import FeatureExtractor
from datetime import datetime
from flask import Flask, request, render_template
from pathlib import Path
from keras.optimizers import Adam
from tensorflow.keras.layers import Dropout, Dense, Activation, Flatten
class FeatureExtractor:
def __init__(self):
input_shape = (224, 224, 3)
base_model = VGG16(weights='imagenet', include_top=False, input_shape=input_shape)
for layer in base_model.layers:
layer.trainable = False
last = base_model.layers[-1].output
x = Flatten()(last)
x = Dense(1000, activation='relu', name='fc1')(x)
x = Dropout(0.3)(x)
x = Dense(10, activation='softmax', name='predictions')(x)
model = Model(base_model.input, x)
model.compile(optimizer=Adam(lr=0.001),
loss = 'categorical_crossentropy',metrics=['accuracy'])
self.model = Model(inputs=base_model.input, outputs=base_model.layers[-1].output)
def extract(self, img):
"""
Extract a deep feature from an input image
Args:
img: from PIL.Image.open(path) or tensorflow.keras.preprocessing.image.load_img(path)
Returns:
feature (np.ndarray): deep feature with the shape=(4096, )
"""
img = img.resize((224, 224))
img = img.convert('RGB')
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
feature = self.model.predict(x)[0]
return feature / np.linalg.norm(feature)
path = "/home/virtuag/www/storage/searchSCB.jpg"
#path = "c:/xamppp/htdocs/projet/V-stock/PWA/public/storage/searchSCB.jpg"
img = Image.open(path)
app = Flask(__name__)
fe = FeatureExtractor()
features = []
img_paths = []
for feature_path in Path("/home/virtuag/www/storage/images_article").glob("*.npy"):
#for feature_path in Path("c:/xamppp/htdocs/projet/V-stock/PWA/public/storage/images_article").glob("*.npy"):
features.append(np.load(feature_path))
#img_paths.append(Path("c:/xamppp/htdocs/projet/V-stock/PWA/public/storage/images_article") / (feature_path.stem + ".jpg"))
img_paths.append(Path("/home/virtuag/www/storage/images_article") / (feature_path.stem + ".jpg"))
features = np.array(features)
query = fe.extract(img)
dists = np.linalg.norm(features-query, axis=1)
ids = np.argsort(dists)[:30]
scores = [img_paths[id] for id in ids]
#print (img_paths)
#print(query)
and the error :
raceback (most recent call last): File "server.py", line 71, in <module> scores = [img_paths[id] for id in ids] File "server.py", line 71, in <listcomp> scores = [img_paths[id] for id in ids] TypeError: only integer scalar arrays can be converted to a scalar index

tf slim inceptionv3 gives wrong output

I want to predict images with the nets from tf slim.
But I get random results for inceptionv3.
For resnet50 everything works fine.
resnet50:
import tensorflow as tf
import cv2
import numpy as np
import tensorflow.contrib.slim.nets as nets
slim = tf.contrib.slim
with tf.device('/gpu:1'):
inputs = tf.placeholder(tf.float32, shape=[None,299,299,3])
with slim.arg_scope(nets.resnet_v1.resnet_arg_scope()):
features,net = nets.resnet_v1.resnet_v1_50(inputs=inputs, num_classes=1000)
saver = tf.train.Saver()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.allow_soft_placement=True
with tf.Session(config=config) as sess:
saver.restore(sess, 'weights/resnet_v1_50.ckpt')
img = cv2.imread('images/dog_ball.jpg')
img = cv2.resize(img,(299,299))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img/255.0
curr_features, curr_net = sess.run([features, net], feed_dict={inputs: [img,img, img]})
for curr_feature in curr_features:
f_ind = np.argsort(curr_feature[0][0])[-4:] # resnet50v1
for i in f_ind:
print i
print ' '
But if I try inception_v3, it's not working.
The results are not even the same, even if the images are the same.
First I thought, the weights didn't load properly, but everything looks fine.
inceptionv3:
import tensorflow as tf
import cv2
import numpy as np
import tensorflow.contrib.slim.nets as nets
slim = tf.contrib.slim
with tf.device('/gpu:1'):
inputs = tf.placeholder(tf.float32, shape=[None,299,299,3])
with slim.arg_scope(nets.inception.inception_v3_arg_scope()):
features,net = nets.inception.inception_v3(inputs=inputs, num_classes=1001)
saver = tf.train.Saver()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.allow_soft_placement=True
with tf.Session(config=config) as sess:
saver.restore(sess, 'weights/inception_v3.ckpt')
img = cv2.imread('images/dog_ball.jpg')
img = cv2.resize(img,(299,299))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img/255.0
curr_features, curr_net = sess.run([features, net], feed_dict={inputs: [img,img, img]})
for curr_feature in curr_features:
f_ind = np.argsort(curr_feature)[-4:] # inceptionv3
for i in f_ind:
print i
print ' '
Do you know, where my mistake is?
Found the answer
If you have the same problem write:
features,net = nets.inception.inception_v3(inputs=inputs, num_classes=1001, is_training=False)

Categories