Beispiel #1
0
def predict_object(msg, cam, model, ja_labels):
    img_shape = (224, 224)
    img_path = "/tmp/cap.jpg"

    save_captured_image(img_path, cam)

    img = image.load_img(img_path, target_size=img_shape)
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    # predict
    preds = model.predict(x)
    # decode
    results = decode_predictions(preds, top=1)[0]

    for r in results:
        print('Predicted:', r)
        en_wnid = r[0]
        acc = r[2]
        # translate english label to japanese one.
        ja_label = en2ja_label(ja_labels, en_wnid, acc)
        print(ja_label)

    if ja_label != "":
        call_jtalk(ja_label)
Beispiel #2
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.conv_base.predict(processed_sample)
     decoded_features = decode_predictions(features)
     return decoded_features
def decodepred(network_name, preds):
    if (network_name == "ResNet50"):
        preds = resnet50.decode_predictions(preds, top=3)[0]
    elif (network_name == "MobileNetV2"):
        preds = mobilenetv2.decode_predictions(preds, top=3)[0]
    elif (network_name == "VGG19"):
        preds = vgg19.decode_predictions(preds, top=3)[0]
    elif (network_name == "SqueezeNet"):
        preds = imagenet_utils.decode_predictions(preds, top=3)[0]
    return x
Beispiel #4
0
def result(image):
    K.clear_session()
    #model = MobileNetV2(weights="imagenet", include_top=True)
    #model = MobileNet(weights="imagenet", include_top=True)
    model = DenseNet121(weights="imagenet", include_top=True)
    x = np.expand_dims(image, axis=0)
    x = image.reshape(x.shape[0], 224, 224, 3)
    preds = model.predict(x)
    result = decode_predictions(preds, top=3)[0]
    result_str = ""
    for _, name, score in result:
        result_str += "{}: {:.2%} ".format(name, score)
    return result_str
Beispiel #5
0
# Lets optimize the model for the Jetson's GPU
input_model = load_model(model_input_path)
frozenmodel = FrozenGraph(input_model, (224, 224, 3))
print('FrozenGraph build.')
model = TftrtEngine(frozenmodel, 1, 'FP16', output_shape=(1000))
#model = TftrtEngine(frozenmodel, 1, 'INT8', output_shape=(1000))
print('TF-TRT model ready to rumble!')

# Load and preprocess the image
input_image = PIL.Image.open(input_image_path)
input_image = np.asarray(input_image)
preprocessed = preprocess_input(input_image)
preprocessed = np.expand_dims(preprocessed, axis=0)
print('input tensor shape : ' + str(preprocessed.shape))



# This actually calls the inference
print("Warmup prediction")
output = model.infer(preprocessed)
print(decode_predictions(output))

time.sleep(1)

print("starting now (Jetson Nano)...")
s = time.time()
for i in range(0,250,1):
    output = model.infer(preprocessed)
e = time.time()
print('elapsed : ' + str(e-s))
Beispiel #6
0
import img_processing as iproc
from keras.applications.mobilenetv2 import MobileNetV2, decode_predictions

if __name__=="__main__":

    '''
    学習済みモデルのロード
    '''
    model = MobileNetV2(weights='imagenet')
    # model.summary()

    '''
    学習用画像のロード
    '''
    imgs, paths = iproc.load_imgs('./tmp/')

    '''
    学習済みモデルによる推論
    '''
    predict = model.predict(imgs)
    predict = decode_predictions(predict, top=1)

    '''
    分類ラベルリスト
    http://image-net.org/challenges/LSVRC/2012/browse-synsets
    '''
    for idx in range(len(predict)):
        item, name = predict[idx], paths[idx]
        print('Predicted: %s \t Name: %s'%(str(item), name))
Beispiel #7
0
    Main
"""
if __name__ == '__main__':
    # load the model
    model = MobileNetV2(alpha=1.0)
    print(model.summary())
    exit()
    # 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 not iter
    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.
Beispiel #8
0
from keras.models import Model, load_model

import numpy as np

# Performs classification on the specified image
# Spits out preditions

model = MobileNetV2(input_shape=None,
                    alpha=1.0,
                    depth_multiplier=1,
                    include_top=True,
                    weights='imagenet',
                    input_tensor=None,
                    pooling=None,
                    classes=1000)

img_path = 'cheetah.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)

model.summary()

# 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])
    image -= np.min(image)
    image = np.minimum(image, 255)

    cam = cv2.applyColorMap(np.uint8(255 * heatmap), cv2.COLORMAP_JET)
    cam = np.float32(cam) + np.float32(image)
    cam = 255 * cam / np.max(cam)
    return np.uint8(cam), heatmap


# preprocessed_input = load_image(sys.argv[1])
preprocessed_input = load_image("./klib/image/cat_dog.png")

model = MobileNetV2(weights='imagenet')

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

predicted_class = np.argmax(predictions)
cam, heatmap = grad_cam(input_model=model,
                        image=preprocessed_input,
                        category_index=predicted_class,
                        layer_name="Conv_1",
                        nb_classes=1000)

fig, ax = plt.subplots(1, 1, figsize=(5, 5))
ax.imshow(cv2.cvtColor(cv2.imread('./klib/image/cat_dog.png'),
                       cv2.COLOR_BGR2RGB))
ax.imshow(heatmap, alpha=0.5)
ax.grid(linewidth=0.2)
Beispiel #10
0
input_image = np.asarray(input_image)

# In[11]:

# Use the MobileNet preprocessing function,
# and expand dimensions to create a batch of 1 image
preprocessed = preprocess_input(input_image)
preprocessed = np.expand_dims(preprocessed, axis=0)
print('input tensor shape : ' + str(preprocessed.shape))

# In[12]:

# Do 1 warmup prediction, this way we make sure everything is loaded as it should
print("warmup prediction")
prediction = net.predict(preprocessed)
print(decode_predictions(prediction, top=1)[0])
time.sleep(1)

# In[13]:

print("starting now...")
s = time.time()
for i in range(0, 250, 1):
    prediction = net.predict(preprocessed)

e = time.time()
print('Time[ms] : ' + str(e - s))
print('FPS      : ' + str(1.0 / ((e - s) / 250.0)))

# In[ ]: