def predict(self, image_array): print("start predict.") process_image = inception_resnet_v2.preprocess_input( image_array.copy()) predict_res = self.classifier.predict(process_image) print(inception_resnet_v2.decode_predictions(predict_res)) return predict_res
def predict(): # initialize the data dictionary that will be returned from the # view data = {"success": False} # ensure an image was properly uploaded to our endpoint if flask.request.method == "POST": if flask.request.files.get("image"): # read the image in PIL format image = flask.request.files["image"].read() image = Image.open(io.BytesIO(image)) # preprocess the image and prepare it for classification image = prepare_image(image, target=(224, 224)) # classify the input image and then initialize the list # of predictions to return to the client preds = model.predict(image) results = decode_predictions(preds) data["predictions"] = [] # loop over the results and add them to the list of # returned predictions for (imagenetID, label, prob) in results[0]: r = {"label": label, "probability": float(prob)} data["predictions"].append(r) # indicate that the request was a success data["success"] = True # return the data dictionary as a JSON response return flask.jsonify(data)
def predict(self, img_path): img = image.load_img(img_path, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = self.model.predict(x) res = decode_predictions(preds, top=1)[0][0][1] return self.translate(res)
def predict(model, img, target_size): if img.size != target_size: img = img.resize(target_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) list1 = (decode_predictions(preds, top=3)[0]) return list1[0]
def classify(model, kw_mapping, img_bytes): img = pil_image.open(BytesIO(img_bytes)) if img.mode != 'RGB': img = img.convert('RGB') img = img.resize((299, 299), pil_image.NEAREST) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) keywords = [pred[1] for pred in decode_predictions(preds, top=3)[0]] terms = ';'.join(','.join(kw_mapping[k]) for k in keywords) return terms
def create_inception_embedding(paths): """ :parameter: paths:The images' root path :return: imgs: numpy output of their classification """ resnet_rows = 299 resnet_cols = 299 imgs_class = [] # Read all images' and convert to gray-scale images for path in paths: # Print path to debug # print(path) # Read image first img = cv2.imread(path) img = cv2.resize(img, (resnet_cols, resnet_rows)) # Switch to lab color space gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray_img_inRGB = cv2.cvtColor(gray_img, cv2.COLOR_GRAY2BGR) # Pre-process input gray_img_inRGB = preprocess_input(gray_img_inRGB) # Add to the imgs_class imgs_class.append(gray_img_inRGB) # Make those to array imgs_class = np.array(imgs_class, dtype=float) # Predict the result with inception.graph.as_default(): embed = inception.predict(imgs_class) label = decode_predictions(embed) label = label[0][0] print('%s (%.2f%%)' % (label[1], label[2] * 100)) # Return the embedding return embed
def predict(model, img, target_size, top_n=3): """Run model prediction on image Args: model: keras model img: PIL format image target_size: (width, height) tuple top_n: # of top predictions to return Returns: list of predicted labels and their probabilities """ if img.size != target_size: img = img.resize(target_size) preprocessed_img = image.img_to_array(img) preprocessed_img = np.expand_dims(preprocessed_img, axis=0) preprocessed_img = preprocess_input(preprocessed_img) predictions = model.predict(preprocessed_img) return decode_predictions(predictions, top=top_n)[0]
def detect_ingredients_in_image(): img = Image.open(BytesIO(base64.b64decode(request.form["imgb64"]))) img = img.convert("RGB") img = img.resize((200, 200), Image.NEAREST) img = image.img_to_array(img) img = tf.expand_dims(img, 0) img = preprocess_input(img) predictions = model.predict(img) predictions = decode_predictions(predictions) pred_processed = [] for prediction in predictions: items = [] for item in prediction: item = [x for x in item] item[2] = float(item[2]) items.append(item) pred_processed.append(items) return jsonify(predictions=pred_processed)
def predict(self, img): """Predict the possible classes of the image""" # Resize image to InceptionV4 expected size if img.size != TARGET_SIZE: img = img.resize(TARGET_SIZE) # Preprocess image as Tensorflow model input input = image.img_to_array(img) input = np.expand_dims(input, axis=0) input = preprocess_input(input) # Predict classes for input image preds = self.model.predict(input) # Format and return results preds = decode_predictions(preds, top=TOP_N)[0] results = list( map(lambda pred: { 'label': pred[1], 'score': pred[2] * 100 }, preds)) return results
from keras.applications.inception_resnet_v2 import InceptionResNetV2 from keras.preprocessing import image from keras.applications.inception_resnet_v2 import preprocess_input, decode_predictions from PIL import Image import numpy as np import os import time model = InceptionResNetV2(weights='imagenet') images = os.listdir('resources') times = [] for img_path in images: img = image.load_img('resources/' + img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) start_time = time.time() preds = model.predict(x) times.append(time.time() - start_time) # decode the results into a list of tuples (class, description, probability) # (one such list for each sample in the batch) print('Predicted for ' + img_path + ": ", decode_predictions(preds, top=3)[0]) print("Average prediction time: %s seconds" % np.mean(times))
pic_array = img_to_array(pic) pic_array.shape expanded = np.expand_dims(pic_array, axis=0) preprocessed = preprocess_input(expanded) def format_img_inceptionresnet(filename): pic = load_img(filename, target_size=(299, 299)) pic_arr = img_to_array(pic) expanded = np.expand_dims(pic_arr, axis=0) return preprocess_input(expanded) """# Load Inception Resnet""" # Commented out IPython magic to ensure Python compatibility. # %%time # inception_model = InceptionResNetV2(weights='imagenet') inception_model.graph = tf.get_default_graph() """# Making Predictions""" prediction = inception_model.predict(preprocessed) decode_predictions(prediction) data = format_img_inceptionresnet(FILE_2) prediction = inception_model.predict(data) display(load_img(FILE_2)) decode_predictions(prediction)
# Save the name of selected image for dict handling. name_image = list(IMG_BANK.keys())[list(IMG_BANK.values()).index(IMG_PATH)] # Load image, resize acording to the model's needs. img = load_img(IMG_PATH, target_size=(299, 299)) # Convert img to arr(h, w, channel), then into batch(batch, h, w, ch). processed_image = img_to_array(img) batch_image = np.expand_dims(processed_image, axis=0) digested_image = preprocess_input(batch_image) # Digest image into the model and decode it; make it human-readable. predictions = model.predict(digested_image) # We take the TopK from the model, then print it. top1_labels = decode_predictions(predictions, top=1) top5_labels = decode_predictions(predictions) # Brief explanation of what TopX accuracy means. print('\nInceptionResNetV2 TopK scores:\n' + '\nTop-1 accuracy is the conventional accuracy, which means that the model ' + 'answer \n(the one with the highest probability) must be exactly the expected answer.' +f'\n\n{top1_labels[0][0]}\n' + '\n\nTop-5 accuracy means that any of your model that gives 5 highest probability\n answers' + ' that must match the expected answer.\n' ) for i in range(len(top5_labels[0])): print(top5_labels[0][i]) """Historical outputs:
_iter = 1 """ Main """ if __name__ == '__main__': # load the model model = InceptionResNetV2() # load an image from file image = load_img('mug.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 for i in range(_iter): raw_input('{} iteration, press any key to perform...'.format(str(i))) yhat = model.predict(image) # return if no iteration if not _iter: exit() # 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)) # done.
# -*- coding: utf-8 -*- """ Created on Sun Sep 29 16:40:26 2019 @author: Jianmu """ from keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input, decode_predictions from keras.preprocessing.image import load_img, img_to_array import numpy as np #%% model = InceptionResNetV2() print(model.summary()) #%% target_size = (299, 299) img_path = "C:/Users/14534/Desktop/4.jpg" image = load_img(img_path, target_size=target_size) image_data = img_to_array(image) #image_data = image_data.reshape((1,) + image_data.shape) image_data = np.expand_dims(image_data, axis=0) print(image_data.shape) image_data = preprocess_input(image_data) #%% prediction = model.predict(image_data) results = decode_predictions(prediction, top=3) print(results)
from keras.applications.inception_resnet_v2 import InceptionResNetV2 from keras import * from keras.preprocessing import image from keras.applications.inception_resnet_v2 import preprocess_input, decode_predictions import numpy as np model = InceptionResNetV2(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) img_path = 'elephant.jpg' img = image.load_img(img_path, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) # decode the results into a list of tuples (class, description, probability) # (one such list for each sample in the batch) print('Predicted:', decode_predictions(preds, top=3)[0]) # Predicted: [(u'n02504013', u'Indian_elephant', 0.82658225), (u'n01871265', u'tusker', 0.1122357), (u'n02504458', u'African_elephant', 0.061040461)]
from time import time import numpy as np from keras.applications.inception_resnet_v2 import InceptionResNetV2 from keras.applications.inception_resnet_v2 import preprocess_input from keras.applications.inception_resnet_v2 import decode_predictions from skimage.io import imread from skimage.transform import resize tic = time() model = InceptionResNetV2(weights='imagenet') print("Loaded model in {:.3}s".format(time() - tic)) image = imread('laptop.jpeg') image_resized = resize(image, (299, 299), preserve_range=True, mode='reflect') image_resized_batch = np.expand_dims(image_resized, axis=0) tic = time() preds = model.predict(preprocess_input(image_resized_batch)) print("Computed predictions in {:.3}s".format(time() - tic)) print('Predicted image labels:') class_names, confidences = [], [] for class_id, class_name, confidence in decode_predictions(preds, top=5)[0]: print(" {} (synset: {}): {:0.3f}".format(class_name, class_id, confidence))
from keras.preprocessing import image from keras.applications.inception_resnet_v2 import preprocess_input, decode_predictions, InceptionResNetV2 import numpy as np resnet = InceptionResNetV2(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) ################################### TEST IMAGES ################################### test_image1 = "Dataset/Food-101/images/cup_cakes/15425.jpg" test_image2 = "Dataset/food-101/images/baby_back_ribs/2432.jpg" test_image3 = "Dataset/Food-101/images/pizza/32004.jpg" test_image4 = "Dataset/UECFood100/1/1.jpg" img_path = test_image2 img = image.load_img(img_path, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) #x = preprocess_input(x) preds = resnet.predict(x) print(decode_predictions(preds))
if not os.path.exists(output_folder): os.mkdir(output_folder) # load model model = InceptionResNetV2(weights='imagenet') register_gradient() guided_model = modify_backprop(model, 'GuidedBackProp') saliency_fn = compile_saliency_function(guided_model) count = 0 for sample in glob.glob('./examples/' + input_folder + '/*.jpg'): preprocessed_input = load_image(sample) image_name = sample.split('\\')[-1].split('.jpg')[0] predictions = model.predict(preprocessed_input) top_1 = decode_predictions(predictions)[0][0] #print('Predicted class:') #print('%s (%s) with probability %.2f' % (top_1[1], top_1[0], top_1[2])) count += 1 print('current number of image processed: ', count) predicted_class = np.argmax(predictions) cam, heatmap = grad_cam(model, preprocessed_input, predicted_class, "conv_7b") cv2.imwrite( output_folder + "gradcam_" + image_name + "_predict_" + str(top_1[1]) + '_' + str(top_1[2]) + ".jpg", cam) saliency = saliency_fn([preprocessed_input, 0]) gradcam = saliency[0] * heatmap[..., np.newaxis] cv2.imwrite(
image_1_copy = image_1 image_2_copy = image_2 image_3_copy = image_3 #Pre-processing the input image_1 = np.expand_dims(image_1, axis=0) image_2 = np.expand_dims(image_2, axis=0) image_3 = np.expand_dims(image_3, axis=0) image_1 = preprocess_input(image_1) image_2 = preprocess_input(image_2) image_3 = preprocess_input(image_3) #Loading the model my_model = incpt(weights='imagenet') #Model Summary #my_model.summary() cv2.imshow("Cat", image_1_copy) cv2.waitKey(0) print("Predicted:", decode_predictions(my_model.predict(image_1), top=3)[0]) cv2.imshow("Moon", image_2_copy) cv2.waitKey(0) print("Predicted:", decode_predictions(my_model.predict(image_2), top=3)[0]) cv2.imshow("Space Shuttle", image_3_copy) cv2.waitKey(0) print("Predicted:", decode_predictions(my_model.predict(image_3), top=3)[0])
from time import time import numpy as np from keras.applications.inception_resnet_v2 import InceptionResNetV2 from keras.applications.inception_resnet_v2 import preprocess_input from keras.applications.inception_resnet_v2 import decode_predictions from skimage.io import imread from skimage.transform import resize tic = time() model = InceptionResNetV2(weights='imagenet') print("Loaded model in {:.3}s".format(time() - tic)) image = imread('laptop.jpeg') image_resized = resize(image, (299, 299), preserve_range=True, mode='reflect') image_resized_batch = np.expand_dims(image_resized, axis=0) tic = time() preds = model.predict(preprocess_input(image_resized_batch)) print("Computed predictions in {:.3}s".format(time() - tic)) print('Predicted image labels:') class_names, confidences = [], [] for class_id, class_name, confidence in decode_predictions(preds, top=5)[0]: print(" {} (synset: {}): {:0.3f}".format(class_name, class_id, confidence))
from keras.applications.inception_resnet_v2 import InceptionResNetV2 from keras.preprocessing import image from keras.applications.inception_resnet_v2 import preprocess_input from keras.applications.inception_resnet_v2 import decode_predictions import numpy as np model = InceptionResNetV2(weights='imagenet') #image_path = 'elephant.jpg' #image_path = 'muppet2.jpg' #image_path = 'algo.jpg' #image_path = 'gioconda.jpg' image_path = 'perros.jpg' img = image.load_img(image_path, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) for index, res in enumerate(decode_predictions(preds, top=10)[0]): print('{}. \t {}\t:{:.3f}%'.format(index + 1, res[1], 100 * res[2]))
from keras.applications.inception_resnet_v2 import InceptionResNetV2 from keras.preprocessing import image from keras.applications.inception_resnet_v2 import preprocess_input, decode_predictions import numpy as np model = InceptionResNetV2(weights='imagenet') img_path = 'aguila.jpg' img = image.load_img(img_path, 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 ('Prediction:',decode_predictions(preds, top=1)[0][0])