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 = Xception(include_top=True, weights='imagenet') # Define default image size for VGG19 image_size = (299, 299) # 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 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) # make prediction predictions = model.predict(np.array([image])) results = xception.decode_predictions(predictions, top=5) # build result data["predictions"] = [] for (_, label, prob) in results[0]: r = {"label": label, "probability": float(prob)} data["predictions"].append(r) data["success"] = True # return the data dictionary as a JSON response return flask.jsonify(data)
def process(self): model = tf.keras.applications.Xception( include_top=True, weights="imagenet", input_tensor=None, input_shape=None, pooling=None, classes=1000, ) image_list = [] # Go through data folder and find all .jpeg(s) and put into a list for filename in glob.glob(self.path): image_list.append(filename) # From list of images, go through all and do classification for each for images in image_list: img=tf.keras.preprocessing.image.load_img(images,target_size=(299,299)) img=tf.keras.preprocessing.image.img_to_array(img) response=requests.get('https://storage.googleapis.com/download.tensorflow.org/data/imagenet_class_index.json') imgnet_map=response.json() img=tf.keras.applications.xception.preprocess_input(img) predictions = model.predict(np.array([img])) # Make predictions (top 3) - Only care about top on decode[0][0][1], second decode[0][1][1] decode = decode_predictions(predictions,top=3) start = "\033[1m" # For Bold setting (before) end = "\033[0;0m" # For Bold setting (after) print('Prediction of : ' + start + '{0}'.format(decode[0][0][1]) + end + ' for image : ' + start + '{0}'.format(images.strip(self.path)) + end + ' with confidence : ' + start + '{0}'.format(decode[0][0][2]) + end)
def create_plot(): img = ImgResult() # print(img) preds = prepare_model(img, model) pclass = decode_predictions(preds, top=4) #test prediction # plot prediction a_one = pclass[0][0][1] a_two = pclass[0][1][1] a_three = pclass[0][2][1] a_four = pclass[0][3][1] one = pclass[0][0][2] two = pclass[0][1][2] three = pclass[0][2][2] four = pclass[0][3][2] # print(a_one, a_two, a_three, a_four) animals = [a_one, a_two, a_three, a_four] probs = [one, two, three, four] dfs = pd.DataFrame({'Breed': animals, 'Percentage': probs}) jsons = dfs.to_json(orient="records") jsons = json.loads(jsons) jsons = json.dumps(jsons, indent=4) print(dfs) return jsons
def prediction(self, update, context): """ Prediction method that using pre-trained on ImageNet dataset Xception model for recognizing sent user's picture returns PICTURE that means switch to picture() method you can infinitely send images to bot until you print /cancel or kill bot process by """ id = str(update.message.from_user.id) img = image.load_img('userID_{}_photo.jpg'.format(id), target_size=(299, 299)) update.message.reply_text('[INFO] Preprocessing...') x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) update.message.reply_text('[INFO] Recognizing...') model = Xception() preds = model.predict(x) decoded_preds = decode_predictions(preds, top=1)[0][0] update.message.reply_text('Predicted: {} with {:.2f}% accuracy'.format( decoded_preds[1], decoded_preds[2] * 100)) update.message.reply_text( 'Send me another image or /cancel for stop conversation') return self.PICTURE
def pp(): file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) filename2 = random.choices(string.ascii_uppercase + string.digits, k=13) filename2 = ''.join(filename2) actual_file_path = os.path.join(upP, filename2) file.save(os.path.join(upP, filename2)) img_path = os.path.join(upP, filename2) img = image.load_img(img_path, target_size=(229, 229)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) os.remove(os.path.join(upP, filename2)) predictions = decode_predictions(preds, top=2)[0] arr = [] for i in range(2): arr.append(predictions[i][1]) return ','.join(arr)
def model_predict(self,image_path): 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) predictions = self.model.predict(x) pred=decode_predictions(predictions, top=3) return ','.join([item[1] for item in pred[0]])
def get_predictions(image): cur_img = tf.keras.preprocessing.image.img_to_array(image) cur_img = tf.keras.preprocessing.image.smart_resize( cur_img, (299, 299), interpolation='bilinear') processed_img = tf.keras.applications.xception.preprocess_input(cur_img) cur_predictions = image_net_model.predict(np.array([processed_img])) imagenet_label = decode_predictions(cur_predictions, top=1)[0][0][1] pre_final_predictions = pre_final_model.predict(np.array([processed_img])) return pre_final_predictions, imagenet_label
def export_keras_model(): if not os.path.exists(KERAS_MODEL_PATH): model = Xception(weights='imagenet') 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) #preds.tofile('out_0.txt', '\n') print('Keras Predicted:', decode_predictions(preds, top=5)[0]) model.save(KERAS_MODEL_PATH)
def model_predict(img_path, model): # Preprocessing the image image = Image.open(img_path).convert('RGB') size = (299, 299) image = ImageOps.fit(image, size) image = img_to_array(image) image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) image = preprocess_input(image) graph = tf.get_default_graph() set_session(session) preds = model.predict(image) label = decode_predictions(preds) # retrieve the most likely result, e.g. highest probability label = label[0][0] return label
def filter_by_label(self, batch): """returns ([index of images that has interested subject in], [index of images without])""" network_out = self.__model.predict(preprocess_input_xception(batch)) keep_index = [] reject_index = [] for i, each_image in enumerate(decode_predictions(network_out, top=5)): possible_classes = {each_pred[0] for each_pred in each_image} # no subject of interest found if len(possible_classes.intersection(self.interested_labels)) == 0: reject_index.append(i) else: keep_index.append(i) return keep_index, reject_index
def predict4(): """Use Xception to label image""" path = 'static/Images/boxer.jpeg' img = image.load_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) pclass = decode_predictions(preds, top=5) result = str(pclass[0][0][1]) bad_chars=[';',':','_','!','*'] for i in bad_chars: result = result.replace(i, ' ') result = result.title() print(result) return result
def DataResult1(): if request.method == 'POST': print(request) filesave2 = getfile(request) print(filesave2) preds = prepare_model(filesave2, model) pclass = decode_predictions(preds, top=5) #test prediction bad_chars = [';', ':', '_', '!', '*'] result = str(pclass[0][0][1]) for i in bad_chars: result = result.replace(i, ' ') result = result.title() print(result)
def predict(): """ Generate predictions with the model when receiving data as a POST request """ if request.method == "POST": # get url from the request data = request.data # preprocess the data processed = preprocess(data) # run predictions preds = loaded_model.predict(processed) # obtain predicted classes from predicted probabilities result = decode_predictions(preds, top=1)[0][0][1] # print in backend print("Received data:", data) print("Predicted labels:", result) return jsonify(result)
#Note: for a refrigerator , it predicted 'safe'. However the second probability was correct. # Predicted: [('n04125021', 'safe', 0.45633033), ('n04070727', 'refrigerator', 0.3461617), ('n03710193', 'mailbox', 0.03995161)] from tensorflow.keras.applications.xception import Xception from tensorflow.keras.preprocessing import image from tensorflow.keras.applications.xception import preprocess_input, decode_predictions import numpy as np #img_path = '../data/refrigerator.jpg' img_path = '../data/d.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) model = Xception(weights='imagenet') 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])
from tensorflow.keras.applications.xception import Xception from tensorflow.keras.preprocessing import image from tensorflow.keras.applications.xception import preprocess_input, decode_predictions import numpy as np import tensorflow physical_devices = tensorflow.config.experimental.list_physical_devices('GPU') print("physical_devices-------------", len(physical_devices)) tensorflow.config.experimental.set_memory_growth(physical_devices[0], True) import time model = Xception(weights='imagenet', include_top=True) img_path = 'gas-mask.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) start_time = time.time() preds = model.predict(x) print('Time: {}'.format(time.time() - start_time)) # decode the results into a list of tuples (class, description, probability) # (one such list for each sample in the batch) result = decode_predictions(preds, top=3)[0] print('Predicted:', result[0]) print('{}: {:.2f} %'.format(result[0][1], 100*result[0][2]))
def xception(): if request.method == "POST": instaid = request.form["instaid"] #scraping images with beautifulsoup url = "https://www.instagram.com/" + instaid browser = Browser('chrome') browser.visit(url) sleep(1) bs = BeautifulSoup(browser.html, 'html.parser') #finds all the images in website and puts url in df images = bs.find_all('img', {'src': re.compile('.jpg')}) image_urls = [] for image in images: image_urls.append(str(image['src'])) image_df = pd.DataFrame({"image": image_urls}) import numpy as np import tensorflow as tf from tensorflow.keras.preprocessing import image from tensorflow.keras.applications.xception import (Xception, preprocess_input, decode_predictions) from PIL import Image import requests from io import BytesIO model = Xception(include_top=True, weights='imagenet') prds = [] pcts = [] for i in image_urls: url = i response = requests.get(url) img = Image.open(BytesIO(response.content)) img = img.resize((299, 299), Image.NEAREST) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) predictions = model.predict(x) prds.append(decode_predictions(predictions, top=1)[0][0][1]) pcts.append(decode_predictions(predictions, top=1)[0][0][2]) # cnns.append(decode_predictions(predictions, top=3)[0]) image_df["predictions"] = prds image_df["%"] = pcts image_df.sort_values("%", ascending=False, inplace=True) image_df.reset_index(drop=True, inplace=True) # from IPython.display import Image # from IPython.core.display import HTML # def path_to_image_html(path): # return '<img src="'+ path + '" width="300" >' # print("Server received request for 'About' page...") # return (image_df.to_html(escape=False ,formatters=dict(image=path_to_image_html))) gkey = "AIzaSyA-Rjp6nOeJp6815Xt1Kkuxc5XKMiKl_yA" depart = request.form["depart"] target_radius = 1000 records = pd.DataFrame() target_list = list(set(image_df["predictions"]))[0:5] targets = str(depart).split(",") for target in targets: # Build the endpoint URL target_url = ( f'https://maps.googleapis.com/maps/api/geocode/json?address={target}&key={gkey}' ) geo_data = requests.get(target_url).json() target_adr = geo_data["results"][0]["formatted_address"] lat = geo_data["results"][0]["geometry"]["location"]["lat"] lng = geo_data["results"][0]["geometry"]["location"]["lng"] target_coordinates = str(lat) + "," + str(lng) # target_radius = 800 target_type = "" # set up a parameters dictionary for target_search in target_list: params = { "location": target_coordinates, "keyword": target_search, "radius": target_radius, "type": target_type, "key": gkey } # base url base_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json" # run a request using our params dictionary response = requests.get(base_url, params=params) places_data = response.json() n = 0 # while int(n) > len(places_data): while int(n) < len(places_data["results"]): try: price = places_data["results"][int(n)]["price_level"] except KeyError: price = "NA" try: link = places_data["results"][int(n)]["place_id"] except KeyError: link = "NA" try: score = places_data["results"][int(n)]["rating"] except KeyError: score = "NA" try: reviews = places_data["results"][int( n)]["user_ratings_total"] except KeyError: reviews = "NA" try: lat = places_data["results"][int( n)]["geometry"]["location"]["lat"] lon = places_data["results"][int( n)]["geometry"]["location"]["lng"] # dist = pd.read_html(f"http://boulter.com/gps/distance/?from={target_coordinates}&to={poi_coord}&units=k") # distance = float(str(dist[1][1][1]).split(" ")[0]) except IndexError or TimeoutError: distance = "NA" # drive_url = f"https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins={target_coordinates}&destinations={poi_coord}&key=AIzaSyA-Rjp6nOeJp6815Xt1Kkuxc5XKMiKl_yA" # drive_res = requests.get(drive_url).json() # distance = drive_res["rows"][0]["elements"][0]["distance"]["value"]/1000 # duration= int(drive_res["rows"][0]["elements"][0]["duration"]["value"]/60) # except KeyError: # distance = "NA" # drive_dur = "NA" # try: # walk_url = f"https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins={target_coordinates}&destinations={poi_coord}&mode=walking&key=AIzaSyA-Rjp6nOeJp6815Xt1Kkuxc5XKMiKl_yA" # walk_res = requests.get(walk_url).json() # distance = walk_res["rows"][0]["elements"][0]["distance"]["value"]/1000 # walk_dur = int(walk_res["rows"][0]["elements"][0]["duration"]["value"]/60) # except KeyError: # walk_dur = "NA" # try: # transit_url = f"https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins={target_coordinates}&destinations={poi_coord}&mode=transit&key=AIzaSyA-Rjp6nOeJp6815Xt1Kkuxc5XKMiKl_yA" # transit_res = requests.get(transit_url).json() # transit_dur = int(transit_res["rows"][0]["elements"][0]["duration"]["value"]/60) # except KeyError: # transit_dur = "NA" content = pd.DataFrame({ "depart": target_adr, "poi": target_search, "name": [places_data["results"][int(n)]["name"]], "score": score, "reviews": reviews, "price": price, "link": link, "address": [places_data["results"][int(n)]["vicinity"]], "lat": lat, "lon": lon }) # "drive":duration, # "public":transit_dur, # "walk":walk_dur}) records = records.append(content) n += 1 records.reset_index(drop=True, inplace=True) # records["link"]=records["link"].apply(lambda x: '<a href="https://www.google.com/maps/place/?q=place_id:{0}">link</a>'.format(x)) # return (records.to_html(escape=False)) px.set_mapbox_access_token( "pk.eyJ1IjoidGl2bWU3IiwiYSI6ImNrMWEwZDVtNDI4Zm4zYm1vY3o3Z25zejEifQ._yTPkj3nXTzor72zIevLCQ" ) fig = px.scatter_mapbox(records, lat="lat", lon="lon", color="poi", hover_name="name", zoom=13) fig.update_layout(autosize=True, width=1500, height=750) # ,margin=go.layout.Margin(l=50,r=50,b=100,t=100,pad=4)) return (py.offline.plot(fig, output_type="div")) return render_template("xception.html")
def vgg19(): if request.method == "POST": instaid = request.form["instaid"] #scraping images with beautifulsoup url = "https://www.instagram.com/" + instaid browser = Browser('chrome') browser.visit(url) sleep(1) bs = BeautifulSoup(browser.html, 'html.parser') #finds all the images in website and puts url in df images = bs.find_all('img', {'src': re.compile('.jpg')}) image_urls = [] for image in images: image_urls.append(str(image['src'])) image_df = pd.DataFrame({"image": image_urls}) import numpy as np import tensorflow as tf from tensorflow import keras from tensorflow.keras.preprocessing import image from tensorflow.keras.applications.vgg19 import (VGG19, preprocess_input, decode_predictions) from PIL import Image import requests from io import BytesIO model = VGG19(include_top=True, weights='imagenet') prds = [] pcts = [] for i in image_urls: url = i response = requests.get(url) img = Image.open(BytesIO(response.content)) img = img.resize((224, 224), Image.NEAREST) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) predictions = model.predict(x) prds.append(decode_predictions(predictions, top=1)[0][0][1]) pcts.append(decode_predictions(predictions, top=1)[0][0][2]) # cnns.append(decode_predictions(predictions, top=3)[0]) image_df["prediction"] = prds image_df["%"] = pcts image_df.sort_values("%", ascending=False, inplace=True) from IPython.display import Image from IPython.core.display import HTML def path_to_image_html(path): return '<img src="' + path + '" width="300" >' print("Server received request for 'About' page...") return (image_df.to_html(escape=False, formatters=dict(image=path_to_image_html))) return render_template("vgg19.html")
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 !!!')
## Let's test-drive it """ # 1. Convert the image to numpy array img = get_img_array(img_path) # 2. Keep a copy of the original image orig_img = np.copy(img[0]).astype(np.uint8) # 3. Preprocess the image img_processed = tf.cast(xception.preprocess_input(img), dtype=tf.float32) # 4. Get model predictions preds = model.predict(img_processed) top_pred_idx = tf.argmax(preds[0]) print("Predicted:", top_pred_idx, xception.decode_predictions(preds, top=1)[0]) # 5. Get the gradients of the last layer for the predicted label grads = get_gradients(img_processed, top_pred_idx=top_pred_idx) # 6. Get the integrated gradients igrads = random_baseline_integrated_gradients(np.copy(orig_img), top_pred_idx=top_pred_idx, num_steps=50, num_runs=2) # 7. Process the gradients and plot vis = GradVisualizer() vis.visualize( image=orig_img, gradients=grads[0].numpy(),
img = get_image( "https://upload.wikimedia.org/wikipedia/commons/6/66/" "An_up-close_picture_of_a_curious_male_domestic_shorthair_tabby_cat.jpg") # %% print(img.shape) plt.imshow(img) # %% # Resize to target shape img = cv2.resize(img, input_shape) plt.imshow(img) # %% # Xception uses tf preprocessing img = preprocess_input(img) # %% # Note that 299 x 299 is default shape for xception model = Xception() # Need a 4th dim for samples pred = model.predict(np.expand_dims(img, 0)) # %% decode_predictions(pred, top=5) # %%