def predict_image(): #file = request.files['image'] #filename = os.path.join('selected', file.filename) #file.save(filename) # Load the VGG19 model # https://keras.io/applications/#VGG19 model = VGG19(include_top=True, weights='imagenet') # Define default image size for VGG19 image_size = (224, 224) # initialize the response dictionary that will be returned message = request.get_json(force=True) encoded = message['image'] decoded = base64.b64decode(encoded) img = Image.open(io.BytesIO(decoded)) # if the image mode is not RGB, convert it if img.mode != "RGB": img = img.convert("RGB") # resize the input image and preprocess it img = img.resize(image_size) # Preprocess image for model prediction # This step handles scaling and normalization for VGG19 x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # Make predictions predictions = model.predict(x) prediction = decode_predictions(predictions, top=1) print('Predicted:', decode_predictions(predictions, top=3)) response = { 'predictions': { 'label': np.array(prediction[0][0][1]).tolist(), 'probability': np.array(prediction[0][0][2]).tolist() } } print(response) # return the response dictionary as a JSON response return jsonify(response)
def get_prediction_list(original_image: np.ndarray, model) -> List[PredictedObject]: """Determines ten most probable image classes. Determines 10 most probable classes of image. This info is afterwards processed into user-friendly class PredictedObject (more precisely into list with objects of this type). Args: original_image (np.ndarray): Figure - tensor, which we try to classify. model (tf.keras.engine.functional.Functional): used ML model. Returns: List[PredictedObject]: List of PredictedObject objects. """ prediction = model.predict(original_image) ten_best_classes = decode_predictions(prediction, top=10)[0] predictions_list = [] for order_number_minus_one, element in enumerate(ten_best_classes): class_name = element[1] probability = round(100 * element[2], 2) class_index = _get_class_index(prediction, order_number_minus_one) predictions_list.append( PredictedObject(order_number_minus_one + 1, class_name, probability, class_index)) return predictions_list
def predict(image_path): """Use VGG19 to label image""" img = image.load_img(image_path, target_size=image_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) predictions = model.predict(x) plt.imshow(img) print('Predicted:', decode_predictions(predictions, top=3))
def predict(image_path): """Use VGG19 to label image""" pred = [] acc = [] model = VGG19(include_top=True, weights='imagenet') img = image.load_img(urllib.request.urlopen(image_path), target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) predictions = model.predict(x) plt.imshow(img) for i in range(3): pred.append(decode_predictions(predictions, top=3)[0][i][1]) acc.append(decode_predictions(predictions, top=3)[0][i][2]) pred_accuracy = {'Predicted': pred, 'Accuracy': acc} return pred_accuracy
def PredictTop5_VGG19(image_path): model = VGG19() img = image.load_img(image_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) labels = decode_predictions(preds, top=5) K.clear_session() labels = np.array(labels) labels = np.squeeze(labels) labels = labels[:, 1:3] labels = dict(labels) print(labels) return labels
def trainer(things): filename = things # Load the VGG19 model # https://keras.io/applications/#VGG19 model = VGG19(include_top=True, weights='imagenet') # Define default image size for VGG19 image_size = (224, 224) # Load the image and resize to default image size image_path = os.path.join("static", "images", filename) img = image.load_img(image_path, target_size=image_size) plt.imshow(img) # Preprocess image for model prediction # This step handles scaling and normalization for VGG19 x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # Make predictions predictions = model.predict(x) predictionsforshow = decode_predictions(predictions, top=3) #print('Predicted:', decode_predictions(predictions, top=3)) plt.imshow(img) return (predictionsforshow) # Refactor above steps into reusable function # def predict(image_path): # """Use VGG19 to label image""" # img = image.load_img(image_path, target_size=image_size) # x = image.img_to_array(img) # x = np.expand_dims(x, axis=0) # x = preprocess_input(x) # predictions = model.predict(x) # plt.imshow(img) # print('Predicted:', decode_predictions(predictions, top=3)) # Make predictions on the dog photo #image_path = os.path.join("..", "images", "dog.jpeg") #predict(image_path)
def main(myblob: func.InputStream): logging.info(f"Python blob trigger function processed blob \n" f"Name: {myblob.name}\n" f"Blob Size: {myblob.length} bytes") # Constants img_width, img_height = 224, 224 # Load image from blob bytes_image = myblob.read() raw_image = load_img(io.BytesIO(bytes_image), target_size=(img_width, img_height)) # Reshape data for the model image = img_to_array(raw_image) image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) # Prepare the image for the VGG model image = preprocess_input(image) # Load base model if K.image_data_format() == 'channels_first': input_shape = (3, img_width, img_height) else: input_shape = (img_width, img_height, 3) test_model = VGG19(input_shape=input_shape, weights='imagenet') # Perform image classification prediction = test_model.predict(image) # Convert the probabilities to class labels label = decode_predictions(prediction) # Retrieve the most likely result, e.g. highest probability label = label[0][0] logging.info(f"PREDICTION: {label[1]} | {label[2]*100}%")
def get_predictions(self, in_img, network_name, top=3): # network model selection model = None size = (224, 224) int_model = False if network_name == 'VGG19': if self.MODEL_VGG19 is None: from tensorflow.keras.applications.vgg19 import VGG19 self.MODEL_VGG19 = VGG19(weights='imagenet') model = self.MODEL_VGG19 int_model = True from tensorflow.keras.applications.vgg19 import decode_predictions elif network_name == 'DenseNet201': if self.MODEL_DENSENET201 is None: from tensorflow.python.keras.applications.densenet import DenseNet201 self.MODEL_DENSENET201 = DenseNet201(weights='imagenet') model = self.MODEL_DENSENET201 from tensorflow.keras.applications.densenet import decode_predictions elif network_name == 'MobileNetV2': if self.MODEL_MOBILENET is None: from tensorflow.keras.applications.mobilenet import MobileNet # (244,244) self.MODEL_MOBILENET = MobileNet(weights='imagenet') model = self.MODEL_MOBILENET from tensorflow.keras.applications.mobilenet import decode_predictions elif network_name == 'InceptionV3': if self.MODEL_INCEPTIONV3 is None: from tensorflow.keras.applications.inception_v3 import InceptionV3 # (299,299) self.MODEL_INCEPTIONV3 = InceptionV3(weights='imagenet') model = self.MODEL_INCEPTIONV3 size = (299, 299) from tensorflow.keras.applications.inception_v3 import decode_predictions elif network_name == 'ResNet50': if self.MODEL_RESNET50 is None: from tensorflow.python.keras.applications.resnet import ResNet50 # (244,244) self.MODEL_RESNET50 = ResNet50(weights='imagenet') model = self.MODEL_RESNET50 int_model = True from tensorflow.keras.applications.resnet import decode_predictions else: print("no valid model selected") return # if input is a numpy image, convert it to a cv image if (isinstance(in_img, numpy.ndarray)): in_img = _to_cv_image(in_img) img = cv2.resize(in_img, dsize=size, interpolation=cv2.INTER_CUBIC) img = _cv_to_array(img) if (int_model): img = img * 255 img = numpy.expand_dims(img, axis=0) # predict model and return top predictions features = model.predict(img) predictions = [] for p in decode_predictions(features, top=top)[0]: predictions.append([p[0], p[1], float(p[2])]) # get the indexes of the top predictions class_codes = numpy.flip(numpy.argsort(features[0]))[:top] return (predictions, class_codes) # ottiene nome della classe predetta
### This is the basic image classifier code using transfer learning in VGG19### from tensorflow.keras.applications.vgg19 import VGG19, decode_predictions from tensorflow.keras.applications.imagenet_utils import preprocess_input from tensorflow.keras.preprocessing import image from tensorflow.keras.models import load_model import numpy as np def test(img, model): img = image.load_img(img, 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) return preds if __name__ == '__main__': #model = VGG19(weights='imagenet') # Use this if you don't have model not in your local model = load_model('pretrained weights/vgg19.h5' ) # use this if you have model in your local test_image = 'images/2464903-1366x768.jpg' # pass the image you want to predict prediction = test(test_image, model) label = decode_predictions(prediction) label = label[0][0] print("Predicted object is " + str(label[1]) + " and it's score is " + str(label[2] * 100))
image_path =IMAGES_DIR / 'rose.jpg' print (image_path) # read image in RGB format image = imageio.imread(str(image_path)) # read image in BGR format # image = cv2.imread(str(image_path)) print(image.shape) image = vision.resize(image, 224, 224) batch = expand_to_batch(image) #batch = preprocess_input(batch) batch = preprocess_caffe(batch, color_format="rgb") a_model = vgg.model_vgg19(weights="imagenet") b_model = VGG19() #print(model.summary()) a_y = a_model.predict(batch) b_y = b_model.predict(batch) a_label = decode_predictions(a_y) b_label = decode_predictions(b_y) a_label = a_label[0][0] b_label = b_label[0][0] # print the classification print('%s (%.2f%%)' % (a_label[1], a_label[2]*100)) print('%s (%.2f%%)' % (b_label[1], b_label[2]*100))
img=tf.io.read_file(img_file) img=tf.image.decode_image(img,channels=3) img=tf.image.convert_image_dtype(img,tf.float32) shape=tf.cast(tf.shape(img)[:-1],tf.float32) scale=max_img_size/max(shape) new_shape=tf.cast(shape*scale,tf.int32) img=tf.image.resize(img,new_shape) return img[tf.newaxis,:] def tensor2img(tensor): if np.ndim(tensor)>int(3): assert tensor.shape[0]==1; tensor=tensor[0] pl.figure(figsize=(3,3)); pl.imshow(tensor) pl.tight_layout(); pl.show() for f in img_files: input_file=urllib.request.urlopen(img_path+f) output_file=open(f,'wb') output_file.write(input_file.read()) output_file.close(); input_file.close() for i in range(len(img_files)): content_img=load_img(img_files[i]) x=vgg19.preprocess_input(content_img*255) prediction_probabilities=mvgg19(x) predict_top5=vgg19.decode_predictions( prediction_probabilities.numpy())[0] sp.pretty_print('example #%d'%(i+1)) display(pd.DataFrame( [[class_name,prob] for (number,class_name,prob) in predict_top5], columns=['class name','prob'])) tensor2img(content_img)
num_classes = 1 uploaded_file = st.file_uploader("Choose Picture", type="jpg") if uploaded_file is not None: image = Image.open(uploaded_file) st.image(image, caption='Uploaded Picture', use_column_width=True) # predictions image = preprocess(image) #load VGG19 model with st.spinner('Loading VGG19 Model'): model = VGG19() with st.spinner('Predicting Object'): i_pred = model.predict(image) label = decode_predictions(i_pred, top=num_classes) with st.spinner('Confidence'): # process output confidence = round(label[0][0][2], 3) st.write(confidence) bar = st.progress(0) prog_sec = 1.0 for b_progress in range(100): time.sleep(prog_sec / 100.0) bar.progress(int((b_progress + 1) * confidence)) confidence = round(confidence * 100, 2) output = "# " + label[0][0][1] + " with " + str( confidence) + " % confidence" st.write(output)
image_size = (224, 224) # Load the image and resize to default image size image_path = os.path.join("..", "images", "animal1.jpg") img = image.load_img(image_path, target_size=image_size) plt.imshow(img) # Preprocess image for model prediction # This step handles scaling and normalization for VGG19 x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # Make predictions predictions = model.predict(x) print('Predicted:', decode_predictions(predictions, top=3)) plt.imshow(img) # Refactor above steps into reusable function def predict(image_path): """Use VGG19 to label image""" img = image.load_img(image_path, target_size=image_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) predictions = model.predict(x) plt.imshow(img) print('Predicted:', decode_predictions(predictions, top=3))
from tensorflow.keras.applications.vgg19 import VGG19 from tensorflow.keras.utils import plot_model from tensorflow.keras.preprocessing import image from tensorflow.keras.applications.vgg19 import preprocess_input, decode_predictions from tensorflow.keras.models import Model import numpy as np base_model = VGG19(weights='imagenet', include_top=True) #model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_pool').output) #print(model.summary()) #plot_model(model, to_file='vgg.png') img_path = 'elephant.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) block4_pool_features = base_model.predict(x) #print('Predicted:', decode_predictions(block4_pool_features, top=5)[0]) #print out the result of the prediction # convert the probabilities to class labels label = decode_predictions(block4_pool_features) # retrieve the most likely result, e.g. highest probability label = label[0][0] # print the classification print('%s (%.2f%%)' % (label[1], label[2] * 100))
def set_model(self, model_name, top_n=5): if model_name == 'densenet': self.model = densenet.DenseNet121(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) self.target_size = (224, 224) self.decoder = lambda x: densenet.decode_predictions(x, top=top_n) self.ref = """ <ul> <li><a href='https://arxiv.org/abs/1608.06993' target='_blank'> Densely Connected Convolutional Networks</a> (CVPR 2017 Best Paper Award)</li> </ul> """ elif model_name == 'inception_resnet_v2': self.model = inception_resnet_v2.InceptionResNetV2( include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) self.target_size = (299, 299) self.decoder = lambda x: inception_resnet_v2.decode_predictions( x, top=top_n) self.ref = """ <ul> <li><a href='https://arxiv.org/abs/1602.07261' target='_blank'> Inception-v4, Inception-ResNet and the Impact of Residual Connections on Learning</a></li> </ul> """ elif model_name == 'inception_v3': self.model = inception_v3.InceptionV3(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) self.target_size = (299, 299) self.decoder = lambda x: inception_v3.decode_predictions(x, top=top_n) self.ref = """<ul> <li><a href='https://arxiv.org/abs/1512.00567' target='_blank'> Rethinking the Inception Architecture for Computer Vision</a></li> </ul> """ elif model_name == 'mobilenet': self.model = mobilenet.MobileNet(input_shape=None, alpha=1.0, depth_multiplier=1, dropout=1e-3, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000) self.target_size = (224, 224) self.decoder = lambda x: mobilenet.decode_predictions(x, top=top_n) self.ref = """<ul> <li><a href='https://arxiv.org/abs/1704.04861' target='_blank'> MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications</a></li> </ul> """ elif model_name == 'mobilenet_v2': self.model = mobilenet_v2.MobileNetV2(input_shape=None, alpha=1.0, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000) self.target_size = (224, 224) self.decoder = lambda x: mobilenet_v2.decode_predictions(x, top=top_n) self.ref = """<ul> <li><a href='https://arxiv.org/abs/1801.04381' target='_blank'> MobileNetV2: Inverted Residuals and Linear Bottlenecks</a></li> </ul> """ elif model_name == 'nasnet': self.model = nasnet.NASNetLarge(input_shape=None, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000) self.target_size = (224, 224) self.decoder = lambda x: nasnet.decode_predictions(x, top=top_n) self.ref = """<ul> <li><a href='https://arxiv.org/abs/1707.07012' target='_blank'> Learning Transferable Architectures for Scalable Image Recognition</a></li> </ul> """ elif model_name == 'resnet50': self.model = resnet50.ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) self.target_size = (224, 224) self.decoder = lambda x: resnet50.decode_predictions(x, top=top_n) self.ref = """<ul> <li>ResNet : <a href='https://arxiv.org/abs/1512.03385' target='_blank'>Deep Residual Learning for Image Recognition </a></li> </ul> """ elif model_name == 'vgg16': self.model = vgg16.VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) self.target_size = (224, 224) self.decoder = lambda x: vgg16.decode_predictions(x, top=top_n) self.ref = """<ul> <li><a href='https://arxiv.org/abs/1409.1556' target='_blank'> Very Deep Convolutional Networks for Large-Scale Image Recognition</a></li> </ul>""" elif model_name == 'vgg19': self.model = vgg19.VGG19(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) self.target_size = (224, 224) self.decoder = lambda x: vgg19.decode_predictions(x, top=top_n) self.ref = """<ul> <li><a href='https://arxiv.org/abs/1409.1556' target='_blank'>Very Deep Convolutional Networks for Large-Scale Image Recognition</a></li> </ul>""" elif model_name == 'xception': self.model = xception.Xception(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) self.target_size = (299, 299) self.decoder = lambda x: xception.decode_predictions(x, top=top_n) self.ref = """<ul> <li><a href='https://arxiv.org/abs/1610.02357' target='_blank'>Xception: Deep Learning with Depthwise Separable Convolutions</a></li> </ul>""" else: logger.ERROR('There has no model name !!!')