Ejemplo n.º 1
0
 def predict(self, frame):
     # expand 3D RGB frame into 4D batch
     sample = np.expand_dims(frame, axis=0)
     processed_sample = preprocess_input(sample.astype(np.float32))
     features = self.model_base.predict(processed_sample)
     decoded_features = decode_predictions(features)
     return decoded_features
Ejemplo n.º 2
0
def classify_image(file_url):
    #Retrieve image
    frame = cv2.imread(file_url, cv2.IMREAD_COLOR)

    #Get smallest dimension
    min_dim = np.min(frame.shape[0:2])

    #If width is smallest dimension
    if frame.shape[1] < frame.shape[0]:
        width_start = 0
        width_end = min_dim
        height_start = int(np.floor((frame.shape[0] - min_dim) / 2))
        height_end = int(np.floor((frame.shape[0] + min_dim) / 2))
    else:
        width_start = int(np.floor((frame.shape[1] - min_dim) / 2))
        width_end = int(np.floor((frame.shape[1] + min_dim) / 2))
        height_start = 0
        height_end = min_dim

    #Crop into centered square based on smallest dimension
    # Scale into correct size
    frame = cv2.resize(
        frame[height_start:height_end, width_start:width_end, :], (224, 224))

    #Classify
    processed_image = mobilenet_v2.preprocess_input(
        np.expand_dims(frame, axis=0))
    predictions = model.predict(processed_image)
    labels = mobilenet_v2.decode_predictions(predictions)

    return labels
Ejemplo n.º 3
0
def show_camera():
    # To flip the image, modify the flip_method parameter (0 and 2 are the most common)
    print(gstreamer_pipeline(flip_method=0))
    cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0),
                           cv2.CAP_GSTREAMER)
    if cap.isOpened():
        window_handle = cv2.namedWindow('CSI Camera', cv2.WINDOW_AUTOSIZE)
    # Window
    while cv2.getWindowProperty('CSI Camera', 0) >= 0:
        ret_val, img = cap.read()

        destRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        pil_im = Image.fromarray(destRGB)
        img2 = pil_im.resize((224, 224))
        x = image.img_to_array(img2)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        feed_dict = {input_tensor_name: x}
        preds = tf_sess.run(output_tensor, feed_dict)
        #print('Predicted:', decode_predictions(preds, top=3)[0])
        predict = decode_predictions(preds, top=1)[0]
        predict = predict[0][1:3]
        #print(predict)
        cv2.putText(img, str(predict), (10, 700), cv2.FONT_HERSHEY_SIMPLEX, 2,
                    (255, 255, 255), 2, cv2.LINE_AA)
        cv2.imshow('CSI Camera', img)
        # This also acts as
        keyCode = cv2.waitKey(30) & 0xff
        # Stop the program on the ESC key
        if keyCode == 27:
            break
            cap.release()
            cv2.destroyAllWindows()
    else:
        print('Unable to open camera')
Ejemplo n.º 4
0
def disp_pred():
    pred_dec = decode_predictions(pred)
    null, pred_output, null = pred_dec[0][0]
    to_disp = f"This object(s) probably is a(n)\n{pred_output}"
    label = Label(window,
                  text=to_disp,
                  fg="red",
                  width=25,
                  font=("Century Gothic", 11))
    label.place(x=180, y=500)
Ejemplo n.º 5
0
def getPrediction(filename):
    image = load_img('static/uploads/' + filename, target_size=(224, 224))
    image = img_to_array(image)
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
    image = preprocess_input(image)

    all_result = inference_model.predict(image)
    result = decode_predictions(all_result)[0]
    result = [(img_class, label, str(round(acc * 100, 4)) + '%')
              for img_class, label, acc in result]
    return result
Ejemplo n.º 6
0
def get_prediction(img_path):
    img = cv2.imread(img_path)
    img_resized = cv2.resize(img, (224, 224))
    img_preprocessed = preprocess_input(img_resized)
    img_reshaped = img_preprocessed.reshape((1, 224, 224, 3))
    prediction = model.predict(img_reshaped)
    decoded = decode_predictions(prediction)

    top_3 = [(cat.capitalize(), round(prob * 100, 2))
             for (code, cat, prob) in decoded[0]][:3]
    return top_3
Ejemplo n.º 7
0
 def _classify_image(self, image_path):
     print('classifying', image_path)
     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)
     predictions = self.model.predict(x)
     _, predicted_class, prob = decode_predictions(predictions, top=3)[0][0]
     if prob >= self.config['classificationThreshold']:
         self._label_image(image_path, predicted_class)
     else:
         self._label_image(image_path, 'unknown')
async def run_predict(myfile):
    try:
        img = Image.open(myfile)
        img = img.convert('RGB')
        img = img.resize((224, 224), Image.NEAREST)
        img = image.img_to_array(img)
        img = np.expand_dims(img, axis=0)
        preds = model.predict(img)
        result = tfApps.decode_predictions(preds, top=3)[0]
        return result
    except Exception as err:
        return err
Ejemplo n.º 9
0
def pred(img):
    img = image.load_img(img, target_size=(224, 224))
    assert img is not None, 'Image not found'
    #img_array = np.frombuffer(img, dtype=np.uint8)
    img_array = image.img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0)
    predimg = preprocess_input(img_array)
    # define the mobilenetV2 model
    model = MobileNetV2()
    import time
    t=time.time()
    # make predictions on test image using mobilenetV2
    preds = model.predict(predimg)
    print(time.time()-t)
    # convert predictions to readable results
    print('Predicted:', decode_predictions(preds, top=3)[0])
    output = decode_predictions(preds, top=3)[0]
    fmtans = ""
    for o in output:
        text = f"{o[1]}: {o[2]*100:.2f}\n"
        fmtans += text.replace("_", " ")
    return fmtans
Ejemplo n.º 10
0
def predict_classes(img: np.ndarray, classifier: MobileNetV2) -> dict:
    """Predict classification of an image.

    Args:
        img (np.ndarray): Image, preprocessed for MobileNetv2
        classifier (MobileNetV2): Instance of classifier.

    Returns:
        dict: Predicted classes and their assigned probability.
    """
    predictions = classifier.predict(img)
    prediction = decode_predictions(predictions)
    classes = dict([(i[1], float(i[2])) for i in prediction[0]])
    return classes
Ejemplo n.º 11
0
def make_predictions(chosen_model=MobileNetV2, tiles=image_tile_slicer()):
    """
  Spots an Anemoe fish on the picture,
  given a pretrained chosen_model (e.g. MobileNetV2, ResNet50 or VGG16), 
  Numpy arrays from the 224 by 224 tiles created from the original picture,
  and an accuracy_threshold (default is 90%) for the prediction.
  """
    model = chosen_model(weights='imagenet', include_top=True)
    model.compile(optimizer='rmsprop',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    tiles = np.array(tiles)
    predictions = decode_predictions(model.predict(tiles))
    return predictions
Ejemplo n.º 12
0
def update(i):
    # Capture frame from webcam
    ret, frame_bgr = cap.read()
    assert ret
    frame = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)    
    min_dim = np.min(frame.shape[0:2])
    frame = cv2.resize(frame[0:min_dim, 0:min_dim, :], (224, 224))
    vid.set_data(frame)

    # Update classification
    processed_image = mobilenet_v2.preprocess_input(np.expand_dims(frame, axis=0))
    predictions = model.predict(processed_image)
    label = mobilenet_v2.decode_predictions(predictions)    
    lbl.set_text(label[0][0][1])
    
    return vid, lbl
Ejemplo n.º 13
0
def classify_thread(image, model):

    logging.info('Classification Process Started for model ' + model)
    prediction = {'label': '', 'confidence': 0.0}

    net = models[model]['graph']
    res = net.predict(image)

    if model == 'default':
        pred = decode_predictions(res, top=1)[0][0]
        prediction['label'] = pred[1].upper()
        prediction['confidence'] = float(round(pred[2], 2))
    else:
        index = int(res.argmax(axis=1)[0])
        prediction['label'] = models[model]['labels'][index].upper()
        prediction['confidence'] = float(round(res[0][index], 2))

    return prediction
Ejemplo n.º 14
0
    def predict(self, np_image):
        formatted_image = self.model_info.format_fn(np_image, "?")

        predictions = self.model.predict(
            tf.expand_dims(formatted_image[0], axis=0))
        if self.model_info.base_model_only:
            label = decode_predictions(predictions)
        else:
            label = self.model_info.label_fn(predictions,
                                             self.model_info.class_names)
        version = self.model_info.version

        prob_list = []
        for prob in predictions[0]:
            prob_list.append(prob.astype(float))
        class_names = {}
        for idx, class_name in enumerate(self.model_info.class_names):
            class_names[class_name.astype(str)] = idx
        return prob_list, label, version, class_names
Ejemplo n.º 15
0
    def tflite_predict(self, frame, input_shape=None):
        if not self.tflite_interpreter:
            self.init_tflite_interpreter()

        dtype = self.tflite_input_details[0].get('dtype')

        # expand 3D RGB frame into 4D batch (of 1 item)
        sample = np.expand_dims(frame, axis=0)
        processed_sample = preprocess_input(sample.astype(dtype))

        self.tflite_interpreter.set_tensor(
            self.tflite_input_details[0]['index'], processed_sample)
        self.tflite_interpreter.invoke()

        features = self.tflite_interpreter.get_tensor(
            self.tflite_output_details[0]['index'])
        decoded_features = decode_predictions(features)

        return decoded_features
Ejemplo n.º 16
0
def process():

    model = load_model('../static/people_photo/mobilenet.h5')
    if request.method == 'POST':
        f = request.files['image']

        f.save(os.path.join(file_path, secure_filename(f.filename)))

        img = load_img(f, target_size=(224, 224))
        img = img_to_array(img)
        img = np.expand_dims(img, axis=0)
        img = preprocess_input(img)

        predicted = model.predict(img)
        result = decode_predictions(predicted, top=3)[0]

        file_name = os.path.join(app.config['UPLOAD_FOLDER'],
                                 secure_filename(f.filename))

    return render_template('preview.html', result=result, image=file_name)
Ejemplo n.º 17
0
def upload_image():
    if 'image' not in request.files:
        return render_template(
            'ImageML.html',
            prediction='No posted image. Should be attribute named image')
    file = request.files['image']

    if file.filename == '':
        return render_template('ImageML.html',
                               prediction='You did not select an image')

    if file and valid_file(file.filename):
        ImageFile.LOAD_TRUNCATED_IMAGES = False
        img = Image.open(BytesIO(file.read()))
        img.load()
        img = img.resize((IMAGE_WIDTH, IMAGE_HEIGHT), Image.ANTIALIAS)
        x = image.img_to_array(img)

        if x.shape[2] != IMAGE_CHANNELS:
            return render_template(
                'ImageML.html', prediction='Image color channels should be 3')

        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        pred = model.predict(x)
        prd = decode_predictions(pred, top=3)  # top 3 predictions

        items = []
        for item in prd[0]:
            items.append({'name': item[1], 'prob': float(item[2])})

        response = {'predictions': items}
        return render_template('ImageML.html', prediction=response)
    else:
        return render_template('ImageML.html',
                               prediction='Invalid File extension')
import numpy as np
import cv2
from tensorflow.keras.models import load_model
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions

#include_top=True,完整的模型
#include_top=False,去掉最后的3个全连接层,用来做fine-tuning专用,专门开源了这类模型。
model = MobileNetV2(weights='imagenet')
print(model.summary())

img_path = "elephant.jpg"
img = image.load_img(img_path, target_size=(224, 224))
#将输入数据转换为0~1之间
img = image.img_to_array(img) / 255.0
# 为batch添加第四维,axis=0表示在0位置添加,因为MobileNet的Iput层结构是(None,224,224,3)
img = np.expand_dims(img, axis=0)
print(img.shape)

predictions = model.predict(img)
print('Predicted:', decode_predictions(predictions, top=3)[0])
print(predictions)

description = decode_predictions(predictions, top=3)[0][0][1]

src = cv2.imread(img_path)
cv2.putText(src, description, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
            (255, 0, 0), 2)
cv2.imshow("Predicted", src)
cv2.waitKey()
Ejemplo n.º 19
0
    
    ##
    print('Read image')
    img = read_img(img_path)

    ##
    print('Load Model')
    saved_model_loaded = tf.saved_model.load(model_path, tags=[tag_constants.SERVING])
    signature_keys = list(saved_model_loaded.signatures.keys())
    print(signature_keys)

    infer = saved_model_loaded.signatures['serving_default']
    print(infer.structured_outputs)

    ##
    print('Inference')
    preds = inference(img, infer)
    print('Predicted: ', decode_predictions(preds, top=3)[0])

    ##
    from tensorflow.keras.preprocessing import image
    img_keras = image.load_img(img_path, target_size=(224, 224))
    img_keras = image.img_to_array(img_keras)
    ##
    print('Inference')
    preds_keras = inference(img_keras, infer)
    print('Predicted Keras: ', decode_predictions(preds_keras, top=3)[0])

    cv2.imshow('image',img.astype('uint8'))
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Ejemplo n.º 20
0
interpreter.allocate_tensors()

# Get input and output tensors
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test model once
input_shape = input_details[0]['shape']
input_data = preprocessed
interpreter.set_tensor(input_details[0]['index'], preprocessed)

# This actually calls the inference
interpreter.invoke()

# The function 'get_tensor' returns a copy(!) of the output tensor
output_data = interpreter.get_tensor(output_details[0]['index'])
print(decode_predictions(output_data, top=1))

time.sleep(1)

# keep in mind that tflite is build for inferencing on ARM cpu's, not x86_64
print("starting now (tflite)...")
s = time.time()
for i in range(0,250,1):
    interpreter.set_tensor(input_details[0]['index'], preprocessed)
    interpreter.invoke()
    prediction = interpreter.get_tensor(output_details[0]['index'])
e = time.time()
print('Time[ms] : ' + str(e-s))
print('FPS      : ' + str(1.0/((e-s)/250.0)))
Ejemplo n.º 21
0
def predict_model(image):
    preds = model.predict(image)
    results = decode_predictions(preds, top=3)
    return results
Ejemplo n.º 22
0
print(infer.structured_outputs)

print('Pillow')
for i in range(4):
    img_path = 'data/img%d.JPG' % i
    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)
    x = tf.constant(x)

    labeling = infer(x)
    preds = labeling['predictions'].numpy()

    print('{} - Predicted: {}'.format(img_path,
                                      decode_predictions(preds, top=3)[0]))

print('OpenCV')
for i in range(4):
    img_path = 'data/img%d.JPG' % i
    img = cv2.imread(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    x = cv2.resize(img, (224, 224), interpolation=cv2.INTER_NEAREST)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    x = tf.constant(x)

    labeling = infer(x)
    preds = labeling['predictions'].numpy()

    print('{} - Predicted: {}'.format(img_path,
Ejemplo n.º 23
0
async def predict(request: Request):

    # GET REQUESTER IP :P
    #print(request.client.host)

    if request.method == "POST":
        data = await request.form()

        # UNDERSTANDING THE DATA-------
        #print(data) #FormData [('image', <starlette.datastructures...)]
        #print(data['image']) #UploadFile

        # READ IMAGE
        test = await data['image'].read()  #read file

        # UNDERSTANDING THE DATA-------
        #print(data['image'].content_type) # image/png
        #print(data['image'].file) # SpooledTemporaryFile

        # CHECK IF THE REQUESTER LOADED A CORRECT FORMAT OR NOT
        if data['image'].content_type not in [
                'image/png', 'image/jpeg', 'image/jpg'
        ]:
            display = True
            warn = "This is not a picture. Please choose a png, jpeg or jpg format!"

            #return templates.TemplateResponse('index.html', {'request': request, 'warning': warn, 'display': display})
            return {'status': 'no-supported-plot', 'result': warn}

        else:
            display = False
            warn = ""

            try:
                img = Image.open(BytesIO(test))
                dimensions = np.array(img).shape

                if len(dimensions) > 2 and dimensions[2] == 4:
                    img = img.convert('RGB')
                    #print(np.array(img).shape)

                img = img.resize((224, 224))
                img = image.img_to_array(img)
                img = np.expand_dims(img, axis=0)
                img = preprocess_input(img)
                preds = mobile.predict(img)
                preds = decode_predictions(preds)

                data = {}
                data["success"] = True
                data["predictions"] = []

                for (imagenetID, label, prob) in preds[0]:
                    r = {"label": label, "probability": float(prob)}
                    data["predictions"].append(r)

                predictions = pd.DataFrame(data['predictions'])
                p, plot = lollipop(predictions)

                #return templates.TemplateResponse('index.html', {'request': request, 'warning': warn, 'display': display, 'plot': plot})
                return {'status': 'ok-plot', 'result': plot}

            except:
                display = True
                warn = "Problem with your picture. Probably broken!"
                #return templates.TemplateResponse('index.html', {'request': request, 'warning': warn, 'display': display})
                return {'status': 'error-plot', 'result': warn}
Ejemplo n.º 24
0
output_tensor = tf_sess.graph.get_tensor_by_name(output_tensor_name)


# Optional image to test model prediction.

img = image.load_img(img_file, target_size=image_size[:2])
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

feed_dict = {
    input_tensor_name: x
}
preds = tf_sess.run(output_tensor, feed_dict)

# 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])

import time
times = []
for i in range(20):
    start_time = time.time()
    one_prediction = tf_sess.run(output_tensor, feed_dict)
    delta = (time.time() - start_time)
    times.append(delta)
mean_delta = np.array(times).mean()
fps = 1 / mean_delta
print('average(sec):{:.2f},fps:{:.2f}'.format(mean_delta, fps))

interpreter.allocate_tensors()

# Get input and output tensors
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test model once
input_shape = input_details[0]['shape']
print(input_shape)
interpreter.set_tensor(input_details[0]['index'], preprocessed)

# This actually calls the inference
interpreter.invoke()

# The function 'get_tensor' returns a copy(!) of the output tensor
output_data = interpreter.get_tensor(output_details[0]['index'])
print(decode_predictions(output_data[:, :1000], top=1))

time.sleep(1)

# uses only 1 cpu, very slow on x86_64
# using this with the Coral package is a lot more efficient, even on cpu
print("starting now (tflite)...")
s = time.time()
for i in range(0, 250, 1):
    interpreter.set_tensor(input_details[0]['index'], preprocessed)
    interpreter.invoke()
    prediction = interpreter.get_tensor(output_details[0]['index'])
e = time.time()
print('elapsed : ' + str(e - s))
Ejemplo n.º 26
0
def _predict_image(img_org):
    start_time = time.time()
    result = {}
    response = "Computer Vision challenge\r\n#AIApril\r\n\r\nTop 3 predictions of MobileNet_V2 \r\ntrained on ImageNet dataset:\r\n"
    img = img_org.resize((224, 224))
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = preprocess_input(img)

    preds = model.predict(img)
    for i in range(3):
        res = decode_predictions(preds, top=3)[0][i]
        response += f"{res[1]} - {res[2]*100:.2f}% \r\n"
        logging.info(f"Prediction: {res[1]} - {res[2]*100:.1f}%")
        result["prediction"+str(i)] = f"{res[1]} - {res[2]*100:.1f}%"

    #response += "\r\nVisualizing CAM (Class Activation Mapping)\r\naka Neural Network attention for the best prediction"

    #response += f"\r\nImage load + Predictions: @ {time.time() - start_time :.2f} sec \r\n"
    logging.info(f"\r\nImage load + Predictions: @ {time.time() - start_time :.2f} sec")

    ind = np.argmax(preds[0])

    vector = model.output[:, ind]

# 4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] block_15_project (Conv2D)       (None, 7, 7, 160)    153600      block_15_depthwise_relu[0][0]    
# [4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] block_15_project_BN (BatchNorma (None, 7, 7, 160)    640         block_15_project[0][0]           
# [4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] block_15_add (Add)              (None, 7, 7, 160)    0           block_14_add[0][0]               
# [4/1/2020 5:02:21 AM]                                                                  block_15_project_BN[0][0]        
# [4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] block_16_expand (Conv2D)        (None, 7, 7, 960)    153600      block_15_add[0][0]               
# [4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] block_16_expand_BN (BatchNormal (None, 7, 7, 960)    3840        block_16_expand[0][0]            
# [4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] block_16_expand_relu (ReLU)     (None, 7, 7, 960)    0           block_16_expand_BN[0][0]         
# [4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] block_16_depthwise (DepthwiseCo (None, 7, 7, 960)    8640        block_16_expand_relu[0][0]       
# [4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] block_16_depthwise_BN (BatchNor (None, 7, 7, 960)    3840        block_16_depthwise[0][0]         
# [4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] block_16_depthwise_relu (ReLU)  (None, 7, 7, 960)    0           block_16_depthwise_BN[0][0]      
# [4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] block_16_project (Conv2D)       (None, 7, 7, 320)    307200      block_16_depthwise_relu[0][0]    
# [4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] block_16_project_BN (BatchNorma (None, 7, 7, 320)    1280        block_16_project[0][0]           
# [4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] Conv_1 (Conv2D)                 (None, 7, 7, 1280)   409600      block_16_project_BN[0][0]        
# [4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] Conv_1_bn (BatchNormalization)  (None, 7, 7, 1280)   5120        Conv_1[0][0]                     
# [4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] out_relu (ReLU)                 (None, 7, 7, 1280)   0           Conv_1_bn[0][0]                  
# [4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] global_average_pooling2d (Globa (None, 1280)         0           out_relu[0][0]                   
# [4/1/2020 5:02:21 AM] __________________________________________________________________________________________________
# [4/1/2020 5:02:21 AM] Logits (Dense)                  (None, 1000)         1281000     global_average_pooling2d[0][0]   
# [4/1/2020 5:02:21 AM] ==================================================================================================    # 
    # The output feature map of the last convolutional layer
    last_conv_layer = model.get_layer('Conv_1_bn')

    # The gradient of the vector class with regard to the output feature map
    grads = K.gradients(vector, last_conv_layer.output)[0]

    # A vector of shape (1280,), where each entry is the mean intensity of the gradient over a specific feature map channel
    pooled_grads = K.mean(grads, axis=(0, 1, 2))

    # This function allows us to access the values of the quantities we just defined:
    # `pooled_grads` and the output feature map of conv layer, given a sample image
    iterate = K.function([model.input], [pooled_grads, last_conv_layer.output[0]])

    # These are the values of these two quantities, as Numpy arrays, given the image
    pooled_grads_value, conv_layer_output_value = iterate([img])

    # We multiply each channel in the feature map array by "how important this channel is" with regard to the predicted class
    for i in range(1280):
        conv_layer_output_value[:, :, i] *= pooled_grads_value[i]
    
    #response += f"Activation layers: @ {time.time() - start_time :.2f} sec \r\n"
    logging.info(f"Activation layers: @ {time.time() - start_time :.2f} sec")

    # The channel-wise mean of the resulting feature map is our heatmap of class activation
    heatmap = np.mean(conv_layer_output_value, axis=-1)

    heatmap = np.maximum(heatmap, 0)
    heatmap /= np.max(heatmap)

    RGBheat = []
    for line in heatmap:
        RGBheat.append([])
        for x in line:
            r, g, b = pixel(x, width=1, map=heatmap_scema)
            r, g, b = [int(256*v) for v in (r, g, b)]
            pix = (r, g, b)
            RGBheat[-1].append(pix)

    heatmap = np.array(RGBheat)
    heatmap = np.uint8(heatmap)
    #heatmap = np.expand_dims(heatmap, axis=0)
    
    #response += f"HeatMap created: @ {time.time() - start_time :.2f} sec \r\n"
    logging.info(f"HeatMap created: @ {time.time() - start_time :.2f} sec")

    heatmap = Image.fromarray(heatmap)
    heatmap = heatmap.resize( img_org.size)

    heatmap = np.uint8(heatmap)

    superimposed_img = heatmap * 0.8 + img_org
    ## superimposed_img = img_org.copy()
    ## superimposed_img.putalpha(heatmap)

    ## result_img = Image.new("RGB", superimposed_img.size, (255, 255, 255))
    ## result_img.paste(superimposed_img, mask=superimposed_img.split()[3]) #
    result_img = image.array_to_img(superimposed_img)

    draw = ImageDraw.Draw(result_img)
    font = ImageFont.load_default()
    
    #response += f"\r\nTotal execution time: {time.time() - start_time :.2f} sec\r\n"
    logging.info(f"\r\nTotal execution time: {time.time() - start_time :.2f} sec")
    
    draw.text( (10,10), response, (55, 25, 255), font=font)

    img_byte_arr = io.BytesIO()
    result_img.save(img_byte_arr, format='JPEG', quality=100)

    result['img'] = base64.encodebytes(img_byte_arr.getvalue()).decode("utf-8")

    #result_img.save('test.jpg')

    return result     
Ejemplo n.º 27
0
import streamlit as st
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
import numpy as np
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
from PIL import Image
import cv2
from tensorflow.keras.models import load_model

st.title("Image Classifier")
upload = st.sidebar.file_uploader(label='Upload the Image')
if upload is not None:
    file_bytes = np.asarray(bytearray(upload.read()), dtype=np.uint8)
    opencv_image = cv2.imdecode(file_bytes, 1)
    opencv_image = cv2.cvtColor(opencv_image, cv2.COLOR_BGR2RGB)
    img = Image.open(upload)
    st.image(img, caption='Uploaded Image', width=300)
    model = load_model()
    if st.sidebar.button('PREDICT'):
        st.sidebar.write("Result:")
        x = cv2.resize(opencv_image, (224, 224))
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        y = model.predict(x)
        label = decode_predictions(y)
        # print the classification
        for i in range(3):
            out = label[0][i]
            st.sidebar.title('%s (%.2f%%)' % (out[1], out[2] * 100))
Ejemplo n.º 28
0
    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 !!!')