this is my code , but predict funtion not work
Error:
from keras.applications.vgg16 import VGG16
from keras.utils.vis_utils import plot_model
#Keras will download the weight files from the Internet and store them in the ~/.keras/models directory.
model = VGG16()
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
# load an image from file
image = load_img('output.png', target_size=(224, 224))
# convert the image pixels to a numpy array
image = img_to_array(image)
# reshape data for the model
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
# prepare the image for the VGG model
image = preprocess_input(image)
from keras.applications.vgg16 import decode_predictions
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
# predict the probability across all output classes
yhat = model.predict(image)
# convert the probabilities to class labels
label = decode_predictions(yhat)
# retrieve the most likely result, e.g. highest probability
label = label[0][0]
# print the classification
print('%s (%.2f%%)' % (label[1], label[2]*100))
img = Image.open('output.png')
plt.imshow(img)
whast should I do ??
wekfjeojocwjcopjcoekcopejcpoekcpwekcpejoejocijewjwrjwihujviruhviuhr
The error is that it failed to the the convolution algorithm. Not sure of all the things that can cause this error but when I get that error it is because I have more than one instance of python running that is using tensorflow. So in your jupyter notebook disable the kernel for all python open notebooks except for the notebook you want to run.
Related
I am using transfer learning to retrain VGG16 model on Fruits360 dataset using keras. I have already trained the model and generated the model.h5 file. Now, to test the model I trained I wrote a separate code as shown below and loaded the model.h5 file and the input image and predicted using model.predict() function. I got an array of predicted values as output, but I am not able to get the label for the output.
How do I also get the labels after predicting?
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import decode_predictions
from keras.preprocessing import image
from keras.models import load_model
import os
my_list = os.listdir('./fruits-360/Training')
labels = sorted(my_list)
#print(len(labels))
saved_model = load_model("output.h5")
# load an image from file
image = load_img('apple.jpg', target_size=(224, 224))
# convert the image pixels to a numpy array
image = img_to_array(image)
# reshape data for the model
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
# prepare the image for the VGG model
image = preprocess_input(image)
# predict the probability across all output classes
yhat = saved_model.predict(image)
print(yhat)
The output that I got from this:
[[7.17755854e-02 1.64420519e-04 7.11962930e-04 1.09639345e-03
3.65487649e-03 1.30461820e-03 1.71189080e-03 8.44106398e-05
2.32845647e-04 2.93225341e-04 5.51751134e-09 8.36079926e-06
2.45124284e-07 4.89534505e-05 3.62677121e-04 3.77899994e-07
1.04390840e-09 2.77215719e-07 1.48338046e-07 1.58574392e-06
1.85948572e-08 2.35122825e-05 1.40991315e-05 1.53142121e-09
4.20618314e-08 9.00860164e-10 8.37871852e-08 1.38314470e-04
2.33362043e-05 1.02217612e-07 1.56784572e-05 1.45486838e-05
1.35744230e-07 7.53441327e-07 8.10141572e-08 9.25831589e-09
1.17044747e-05 7.80909737e-09 1.17813433e-05 1.39052809e-05
1.33823562e-06 8.83602411e-07 5.22362086e-07 3.12003103e-04
3.63733534e-07 3.09960592e-06 7.83494880e-10 2.16209537e-06
1.09540458e-07 1.00488634e-07 5.04332002e-06 3.11387431e-08
1.43967145e-06 3.70907003e-08 9.72185060e-02 7.17791181e-07
8.50022047e-07 1.09006250e-11 8.06401147e-07 2.94776954e-04
1.42594319e-04 6.57663213e-06 2.22632690e-09 1.33982932e-04
7.27764191e-03 1.76724559e-03 4.58840788e-07 2.83163081e-05
1.27739793e-06 1.51839274e-07 6.35151446e-01 1.49872008e-04
1.69212143e-07 6.46130411e-06 8.09798095e-09 1.33023859e-04
3.11768084e-10 6.82332274e-03 2.72009001e-05 1.36803810e-05
3.21909931e-04 2.18727801e-05 4.89347076e-06 1.65353231e-05
8.18530396e-02 2.71601088e-08 3.78919160e-03 1.93472511e-06
2.28390039e-04 9.45829204e-04 8.07484355e-08 2.39097773e-07
3.94911304e-08 6.42228715e-10 1.27851049e-10 2.42364536e-06
6.91388919e-08 5.50304435e-07 5.60582407e-08 6.93544493e-08
2.04468861e-07 1.82402204e-07 1.29191315e-08 1.40132336e-03
7.21434930e-08 1.26103216e-04 7.80344158e-02 6.98078452e-07
6.39117275e-07 4.86231899e-09 6.67545173e-05 1.98491052e-05
3.82679382e-08 4.00836188e-06 1.76605427e-05 5.99655250e-05
1.41588691e-06 6.29748298e-09 1.60603679e-03 2.18801666e-04
1.52924549e-05 2.39897645e-07 5.80409534e-08 1.40595137e-06
4.33732907e-07 9.40148311e-06 6.87087507e-08 9.42246814e-08
4.06775257e-07 1.12163532e-08 8.79949056e-08]]
I tried a lot of different options that were answered in other questions regarding the same but I was not able to find out any solution to this. Can anyone help?
If you want I can also provide the retraining code for reference.
Thanks in advance!
You can get the labels from the training dataset as:
class_names = your_train_ds.class_names
labels = (v_datagen.class_indices)
# this returns python dictionary in order of label_name:index
# We need to switch this order to index:label_name,
# so that we can access the label name using the index as key
labels = dict((val,ky) for ky,val in labels.items())
Now, get the prediction index as mentioned in the comments
pred_ind = np.argmax(yhat, axis=1)
print(labels[pred_ind])
hello i am newbie to all this and i am trying to feed the pretrained CNN VGG16 with a custom dataset of mine and then to achieve feature extraction for every image with numpy. but i am taking this error:'numpy.ndarray' object has no attribute 'load_img' really any help appreciate it.thanks
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np
import matplotlib.pyplot as plt
import os
model = VGG16(weights='imagenet', include_top=False)
dir_images = "C:/Users/.../Desktop/db"
imgs = os.listdir(dir_images)
for imgnm in imgs:
image = plt.imread(os.path.join(dir_images, imgnm))
img = image.load_img(image, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
#np.save('features.csv', features)
You are overiding the module image of keras.preprocessing by your own actual images loaded with matplotlib.
So just change the line
image = plt.imread(os.path.join(dir_images, imgnm))
into somehting else like
arr_image = plt.imread(os.path.join(dir_images, imgnm))
and then this error will be gone.
But note that image.load_img takes path as input and not actual images of type ndarray so you should instead use load_img in the loop and remove the matplotlib loading.
I am testing my Deep learning model, I wrote this code
from keras.models import load_model
classifier = load_model('Trained_model.h5')
classifier.evaluate()
Prediction of single image
import numpy as np
from keras.preprocessing import image
img_name = input('Enter Image Name: ')
image_path = './predicting_data/test_set/{}'.format(img_name)
print('')
after running, I am getting this error
ValueError: If evaluating from data tensors, you should specify the `steps` argument.
NOTE :- ./predicting_data/test_Set is the path of my test dataset which has sub folders like A b...c ...to z containing images
The working code to Predict the Class of an Image, by Loading the Saved Model is shown below:
import os
import tensorflow as tf
from tensorflow.keras.preprocessing import image
Test_Dir = '/Dogs_Vs_Cats_Small/test/cats'
New_Model = tf.keras.models.load_model('Dogs_Vs_Cats.h5')
New_Model.summary()
Image_Path = os.path.join(Test_Dir, 'cat.1500.jpg')
Img = image.load_img(Image_Path, target_size = (150,150))
Img_Array = image.img_to_array(Img)
Img_Array = Img_Array/255.0
Img_Array = tf.reshape(Img_Array, (-1,150,150,3))
Predictions = New_Model.predict(Img_Array)
Label = tf.argmax(Predictions)
Label.numpy()[0]
Final line gives the respective Class for our Image.
I want to modify the values of each layers during prediction process. I have one convolution layer, dense layer and output layer (softmax).
I want to modify the result of convolution layer's output before passing it to hidden layers during prediction time.
import tensorflow as tf
from keras import backend as K
from keras.utils import to_categorical
# make a prediction for a new image.
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import load_model
import numpy as np
import pandas as pd# load and prepare the image
def load_image(img):#
# load the image
#img = load_img(filename, grayscale=True, target_size=(28, 28))
# convert to array
#img = img_to_array(img)
# reshape into a single sample with 1 channel
img = img.reshape(1, 28, 28, 1)
# prepare pixel data
img = img.astype('uint8')
img = img #/ 255
return img
data_test = pd.read_csv('mnistdata/mnist_testE.csv')
X_test = np.array(data_test.iloc[:, 1:])
y_test = to_categorical(np.array(data_test.iloc[:, 0]))
# load an image and predict the class
#X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)#def run_example():
# load the image
img = load_image(X_test[1])
# load model
model = load_model('final_modelv4.h5')
digit = model.predict_classes(img)
print(digit)
Can anyone guide me how to modify this code to change the values of each layer's results?
Providing the solution here (Answer Section), even though it is present in the Comments Section, for the benefit of the community.
You can Convert a TensorFlow model into a compressed flat buffer with the TensorFlow Lite Converter. Quantize by converting 32-bit floats to more efficient 8-bit integers or run on GPU.
You can find details about tensroflow lite here.
This page will be most relevant for your query - Get started with TensorFlow Lite
I'm trying to load a file with Tensorflow and visualize the result, but I'm getting TypeError: Image data cannot be converted to float
import tensorflow as tf
import matplotlib.pyplot as plt
image = tf.io.read_file('./my-image.jpg')
image = tf.io.decode_jpeg(image, channels=3)
print(image.shape) # (?, ?, 3)
plt.imshow(image)
Not sure about your tensorflow version. TensorFlow uses static computational graphs by default in 1.x. The data type of image you get is Tensor so that show this error. First create a custom picture.
import numpy as np
from PIL import Image
np.random.seed(0)
image = np.random.random_sample(size=(256,256,3))
im = Image.fromarray(image, 'RGB')
im.save('my-image.jpg')
Then You need to use tf.Session() to start this session. This will show the image created above.
import tensorflow as tf
import matplotlib.pyplot as plt
image = tf.io.read_file('my-image.jpg')
image = tf.io.decode_jpeg(image, channels=3)
print(image)
with tf.Session() as sess:
plt.imshow(sess.run(image))
plt.show()
# print
Tensor("DecodeJpeg:0", shape=(?, ?, 3), dtype=uint8)
Or you can start dynamic computational graphs by tf.enable_eager_execution() in tensorflow. The same effect is achieved with the above code.
import tensorflow as tf
import matplotlib.pyplot as plt
tf.enable_eager_execution()
image = tf.io.read_file('my-image.jpg')
image = tf.io.decode_jpeg(image, channels=3)
plt.imshow(image)
plt.show()
The default in tensorflow2 is dynamic computational graphs. You don't need to use tf.enable_eager_execution().