Example #1
0
def compute_saliency(model,
                     guided_model,
                     img_path,
                     layer_name='block5_conv3',
                     cls=-1,
                     visualize=True,
                     save=True):
    """Compute saliency using all three approaches.
        -layer_name: layer to compute gradients;
        -cls: class number to localize (-1 for most probable class).
    """
    preprocessed_input = load_image(img_path)

    predictions = model.predict(preprocessed_input)
    top_n = 5
    top = decode_predictions(predictions, top=top_n)[0]
    classes = np.argsort(predictions[0])[-top_n:][::-1]
    print('Model prediction:')
    for c, p in zip(classes, top):
        print('\t{:15s}\t({})\twith probability {:.3f}'.format(p[1], c, p[2]))
    if cls == -1:
        cls = np.argmax(predictions)
    class_name = decode_predictions(np.eye(1, 1000, cls))[0][0][1]
    print("Explanation for '{}'".format(class_name))

    gradcam = grad_cam(model, preprocessed_input, cls, layer_name)
    gb = guided_backprop(guided_model, preprocessed_input, layer_name)
    guided_gradcam = gb * gradcam[..., np.newaxis]

    if save:
        jetcam = cv2.applyColorMap(np.uint8(255 * gradcam), cv2.COLORMAP_JET)
        jetcam = (np.float32(jetcam) +
                  load_image(img_path, preprocess=False)) / 2
        cv2.imwrite('gradcam.jpg', np.uint8(jetcam))
        cv2.imwrite('guided_backprop.jpg', deprocess_image(gb[0]))
        cv2.imwrite('guided_gradcam.jpg', deprocess_image(guided_gradcam[0]))

    if visualize:
        plt.figure(figsize=(15, 10))
        plt.subplot(131)
        plt.title('GradCAM')
        plt.axis('off')
        plt.imshow(load_image(img_path, preprocess=False))
        plt.imshow(gradcam, cmap='jet', alpha=0.5)

        plt.subplot(132)
        plt.title('Guided Backprop')
        plt.axis('off')
        plt.imshow(np.flip(deprocess_image(gb[0]), -1))

        plt.subplot(133)
        plt.title('Guided GradCAM')
        plt.axis('off')
        plt.imshow(np.flip(deprocess_image(guided_gradcam[0]), -1))
        plt.show()

    return gradcam, gb, guided_gradcam
Example #2
0
def image_classification_server(img, model):
    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('*****Image classification result ****** \n{}'.format(
        decode_predictions(preds, top=3)[0]))
    # print(type(decode_predictions(preds, top=3)[0]))
    return decode_predictions(preds, top=3)[0]
Example #3
0
def image_classification(img_path):
    #Don't create the model here ...Create it outside
    model = VGG16(weights='imagenet', include_top=True)
    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)
    # decode the results into a list of tuples (class, description, probability)
    # (one such list for each sample in the batch)
    print('*****Image classification result ****** \n{}'.format(
        decode_predictions(preds, top=3)[0]))
    # print(type(decode_predictions(preds, top=3)[0]))
    return decode_predictions(preds, top=3)[0]
Example #4
0
def main():
    img = image.load_img(img_file, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    pre_x = preprocess_input(x)

    model = VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None)

    preds = model.predict(pre_x)
    results = decode_predictions(preds, top=5)[0]
    for result in results:
        print(result)

    # モデルの保存
    model.summary()
    with open('vgg16.json', 'w') as fp:
        json_string = model.to_json()
        fp.write(json_string)

    # ディレクトリの作成
    if not os.path.exists('output'):
        os.mkdir("output")

    # 出力するレイヤーを選択

    for l in range(19):
        layer_dump(model, pre_x, l)

    print('finish.')
Example #5
0
def run_model(img):

    image = loadImage(img)

    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)

    # load the model
    model = VGG16()

    # predict the probability across all output classes
    yhat = model.predict(image)

    # 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
    return (('%s (%.2f%%)' % (label[1], label[2] * 100)))
Example #6
0
def grad_cam(input_model, image, category_index, layer_name):
    model = Sequential()
    model.add(input_model)

    nb_classes = 1000
    target_layer = lambda x: target_category_loss(x, category_index, nb_classes
                                                  )
    model.add(
        Lambda(target_layer, output_shape=target_category_loss_output_shape))

    loss = K.sum(model.layers[-1].output)
    conv_output = [l for l in model.layers[0].layers
                   if l.name is layer_name][0].output
    grads = normalize(K.gradients(loss, conv_output)[0])
    gradient_function = K.function([model.layers[0].input],
                                   [conv_output, grads])

    output, grads_val = gradient_function([image])
    output, grads_val = output[0, :], grads_val[0, :, :, :]

    weights = np.mean(grads_val, axis=(0, 1))
    cam = np.ones(output.shape[0:2], dtype=np.float32)

    for i, w in enumerate(weights):
        cam += w * output[:, :, i]

    cam = cv2.resize(cam, (224, 224))
    cam = np.maximum(cam, 0)
    heatmap = cam / np.max(cam)

    #Return to BGR [0..255] from the preprocessed image
    image = image[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])

    model = VGG16(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(model, preprocessed_input, predicted_class,
                            "block5_conv3")
    cv2.imwrite("gradcam.jpg", cam)

    register_gradient()
    guided_model = modify_backprop(model, 'GuidedBackProp')
    saliency_fn = compile_saliency_function(guided_model)
    saliency = saliency_fn([preprocessed_input, 0])
    gradcam = saliency[0] * heatmap[..., np.newaxis]
    cv2.imwrite("guided_gradcam.jpg", deprocess_image(gradcam))
def Predict():

    import __main__
    ImageFile.LOAD_TRUNCATED_IMAGES = True
    print("=== start predict ===")
    predict = threading.current_thread()

    model = VGG16(include_top=True,
                  weights='imagenet',
                  input_tensor=None,
                  input_shape=None)

    while getattr(predict, "decide", True):

        filename = 'image.png'

        img = __main__.image.load_img(filename, target_size=(224, 224))
        image = __main__.image.img_to_array(img)
        image = np.expand_dims(image, axis=0)

        predict = model.predict(preprocess_input(image))
        results = decode_predictions(predict, top=5)[0]
        #picture = cv2.imread(filename)

        for result in results:
            print(result)

        #cv2.imshow('test', picture)
        #cv2.waitKey(1000)
        #cv2.destroyAllWindows()
        time.sleep(5)
Example #8
0
def predict(filename, featuresize):
    img = image.load_img(filename, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    preds = model.predict(preprocess_input(x))
    results = decode_predictions(preds, top=featuresize)[0]
    return results
def build_heat_map():
    # k.clear_session()
    model = VGG16(weights='imagenet')

    img_tensor = get_image()
    preds = model.predict(img_tensor)
    print('Predicted: ', decode_predictions(preds, top=3)[0])
    print(np.argmax(preds[0]))

    african_elephant_output = model.output[:, 386]
    last_conv_layer = model.get_layer('block5_conv3')

    # tf.compat.v1.disable_eager_execution()
    grads = k.gradients(african_elephant_output, last_conv_layer.output)[0]
    pooled_grads = k.mean(grads, axis=(0, 1, 2))

    iterate = k.function([model.input],
                         [pooled_grads, last_conv_layer.output[0]])

    pooled_grads_value, conv_layer_output_value = iterate(img_tensor)

    for i in range(512):
        conv_layer_output_value[:, :, i] *= pooled_grads_value[i]

    heatmap = np.mean(conv_layer_output_value, axis=-1)
    heatmap = np.maximum(heatmap, 0)
    heatmap /= np.max(heatmap)
    plt.matshow(heatmap)
Example #10
0
def upload_file():
    response = {'success': False}
    img_url = request.args.getlist('url')
    response['list'] = []
    for url in img_url:
        responsePredict = {'url': url}
        result = requests.get(url)
        img = Image.open(BytesIO(result.content))

        # img = Image.open(result.content)
        if img.mode != 'RGB':
            img = img.convert('RGB')
        img = img.resize((224, 224))
        img = image.img_to_array(img)
        img = np.expand_dims(img, axis=0)
        inputs = preprocess_input(img)

        preds = model.predict(inputs)
        results = decode_predictions(preds)
        responsePredict['predictions'] = {
            'label': results[0][0][1],
            'probability': float(results[0][0][2])
        }
        response['list'].append(responsePredict)
    response['success'] = True
    return jsonify(response)
Example #11
0
def classify_img_vgg(input_img_path):
    sess = tf.Session()
    model = VGG16()
    print(model.summary())
    # plot_model(model, to_file='vgg.png')

    # load an imag from file
    image = load_img(input_img_path, 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
    yhat = model.predict(image)

    # 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))
Example #12
0
def predictIfFish(image, model):
    # resize the image
    dim = (224, 224)
    resized = cv2.resize(image, dim, interpolation=cv2.INTER_LINEAR)
    cv2_im = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)
    pil_im = Image.fromarray(cv2_im)
    image = image_utils.img_to_array(pil_im)
    # our image is now represented by a NumPy array of shape (3, 224, 224),
    # but we need to expand the dimensions to be (1, 3, 224, 224) so we can
    # pass it through the network -- we'll also preprocess the image by
    # subtracting the mean RGB pixel intensity from the ImageNet dataset
    image = np.expand_dims(image, axis=0)
    image = preprocess_input(image)

    # classify the image
    #print("[INFO] classifying image...")
    preds = model.predict(image)
    decodedTups = decode_predictions(preds, top=5)[0]
    FISH = ["SHARK", "HAMMERHEAD", "FISH"]
    found = False
    pr = 0.0
    for tup in decodedTups:
        for f in FISH:
            if (f.upper() in tup[1].upper()):
                #found = True
                pr += tup[2]
                #print ("found fish:" + tup[1] + ":pr=" + str(tup[2]))
                #break
        #if (found):
        #break
    if (pr > 0.25):
        print("fish found:" + " tot pr=" + str(pr))
        found = True
    return found, pr
Example #13
0
	def predict(self, frame):
		img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB).astype(np.float32)
		img = img.reshape((1,) + img.shape)

		img = vgg16.preprocess_input(img)
		preds = self.model.predict(img)
		return vgg16.decode_predictions(preds)[0]
Example #14
0
def pre_pic(img_path, topn=10):

    if not path.isfile(img_path):
        print("图片 pic 不存在")
    # include_top=True,表示會載入完整的 VGG16 模型,包括加在最後3層的卷積層
    # include_top=False,表示會載入 VGG16 的模型,不包括加在最後3層的卷積層,通常是取得 Features
    # 若下載失敗,請先刪除 c:\<使用者>\.keras\models\vgg16_weights_tf_dim_ordering_tf_kernels.h5
    model = VGG16(weights='imagenet', include_top=True)

    # Input:要辨識的影像

    #img_path = 'tiger.jpg' 并转化为224*224的标准尺寸
    img = image.load_img(img_path, target_size=(224, 224))

    x = image.img_to_array(img)  #转化为浮点型
    x = np.expand_dims(x, axis=0)  #转化为张量size为(1, 224, 224, 3)
    x = preprocess_input(x)

    # 預測,取得features,維度為 (1,1000)
    features = model.predict(x)

    # 取得前五個最可能的類別及機率
    pred = decode_predictions(features, top=topn)[0]

    return pred
Example #15
0
def main(path):
    strides = 2
    batches = 1
    #firstConv = load_model('first_conv.h5')
    model_full = VGG16(weights='imagenet')
    #print_summary(model_full, line_length=None, positions=None, print_fn=None)
    #sys.exit(0)
    #layer_name_conv1 = 'block1_conv1'
    get_1st_layer_output = K.function([model_full.layers[0].input],
                                      [model_full.layers[6].output])

    get_final_layer_output = K.function([model_full.layers[7].input],
                                        [model_full.layers[-1].output])
    #firstConv_model = Model(inputs=model_full.input , outputs=model_full.get_layer(layer_name_conv1).output)
    #model_part2 = load_model('ResNet50_NoConv1.h5')
    #layer_name_conv2 = 'block1_conv2'
    #layer_name_final = 'predictions'
    #model_part2 = Model(inputs=model_full.get_layer(layer_name_conv2).input, outputs=model_full.get_layer(layer_name_final).output)
    #layer_name = 'bn2a_branch1'
    #intermediate_layer_model = Model(inputs=model_part2.input,
    #                                 outputs=model_part2.get_layer(layer_name).output)
    #base = RN(weights='imagenet')
    direc_path = path
    i = 0
    accuracy_list = []
    for direc in os.listdir(direc_path):
        #if i > 100: break
        for img_name in os.listdir(direc_path + direc):
            # pre-process each image and normalize before inference
            inp_img = image.load_img(direc_path + direc + "/" + img_name,
                                     target_size=(224, 224))
            #inp_img = image.load_img(direc_path + direc + "/" + img_name, target_size=(299,299))
            x = image.img_to_array(inp_img)
            x = np.expand_dims(x, axis=0)
            #x = randomize(x)
            inputInit = preprocess_input(x.copy())
            #inputInit = randomize(inputInit)
            # Model part 1
            start = time.time()
            firstFeat = get_1st_layer_output([inputInit])[0]
            firstFeat = np.float16(firstFeat)
            newDataSet = (x, firstFeat)
            if i and i % 10 == 0:
                #    #print(intermediateFeat.shape)
                print(firstFeat.shape)
            #    print('intermediate conv takes', time.time() - start)

            with open('perturbedData/%d.pickle' % i, 'wb') as handle:
                pickle.dump(newDataSet, handle)

# Model part 3
            start = time.time()
            result2 = get_final_layer_output([firstFeat])[0]
            predicted2 = decode_predictions(result2, top=3)[0]
            accuracy_list.append(predicted2[0][0] == direc)
            if i and i % 10 == 0:
                compute_accuracy(accuracy_list)
                print('remaining takes', time.time() - start)

            i += 1
Example #16
0
def predict():
    img = None
    img_path = ""
    featuresize = 10
    target = (224, 224)
    data = {"success": False}

    if 'size' in request.values:
        featuresize = int(request.values['size'])

    if 'img_path' in request.values:
        img_path = request.values['img_path']
        print('img_path=' + img_path)
        img = get_image(img_path, target=target)

        # process image
        img = process_image(img)

        # predict
        with graph.as_default():
            preds = model.predict(img)
        results = decode_predictions(preds, top=featuresize)[0]
        data["predictions"] = []

        # loop over and format the result
        for (imagenetID, label, prob) in results:
            r = {
                "label": label,
                "probability": float(("{0:.2f}".format(prob * 100)))
            }
            data["predictions"].append(r)

        data["success"] = True

    return jsonify(data)
Example #17
0
def process_image(img_path, idx=0):
    img = image.load_img(img_path, target_size=(224, 224))
    print('imageee')
    print(image)
    plt.imshow(img)
    plt.grid(None)
    plt.show()
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    preds = model.predict(x)
    print('Predicted:', decode_predictions(preds, top=3)[0])
    class_index = np.argsort(preds[0])[-(1 + idx)]
    class_output = model.output[:, class_index]
    grads = K.gradients(class_output, last_conv_layer.output)[0]
    pooled_grads = K.mean(grads, axis=(0, 1, 2))
    iterate = K.function([model.input],
                         [pooled_grads, last_conv_layer.output[0]])
    pooled_grads_value, conv_layer_output_value = iterate([x])
    for i in range(512):
        conv_layer_output_value[:, :, i] *= pooled_grads_value[i]
    heatmap = np.mean(conv_layer_output_value, axis=-1)
    heatmap = np.maximum(heatmap, 0)
    heatmap /= np.max(heatmap)
    plt.matshow(heatmap)
    plt.grid(None)
    plt.show()
    show_superimposed_image(img_path, heatmap)
Example #18
0
 def predict(self, images, top=None):
     if top is None:
         top = self.num_classes
     images = preprocess_input(images)
     predictions = self.model.predict(images)
     labels = decode_predictions(predictions, top=top)
     return labels
Example #19
0
def yomikomi(model, img):
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    preds = model.predict(preprocess_input(x))
    #preds = model.predict(x)
    results = decode_predictions(preds, top=1)[0]
    return str(results[0][1])
def classifyFilesInFolder():

    makeDir()
    while (1 > 0):
        listOfFiles = os.listdir(dirName)
        if len(listOfFiles) != 0:
            for filename in listOfFiles:
                fileInDir = dirName + "/" + filename
                if not os.path.isfile(fileInDir):
                    continue
                reportDirFile = dirName + reportsDir + "/" + filename
                image = load_img(fileInDir, 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)
                pred = model.predict(image, batch_size=16)
                content = decode_predictions(pred, top=3)[0]
                niz = []
                for (i, (imagenetID, label, prob)) in enumerate(content):
                    niz.append(
                        ("{}. {}: {:.2f}%".format(i + 1, label, prob * 100)))

                niz.append(str(filename))
                createTxtFile(reportDirFile, json.dumps(niz))
                os.remove(fileInDir)

        time.sleep(1)
Example #21
0
def example03(img='../almacen/dog.jpg'):
    from keras.preprocessing.image import load_img
    from keras.preprocessing.image import img_to_array
    from keras.applications.vgg16 import preprocess_input
    from keras.applications.vgg16 import decode_predictions
    from keras.applications.vgg16 import VGG16
    print('Loading image: ' + img)
    image = load_img(img, 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)
    # load the model
    print('Loading pre-trained model: VGG16')
    model = VGG16()
    # predict the probability across all output classes
    yhat = model.predict(image)
    # 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))
Example #22
0
def classification():
    menu = {
        'home': False,
        'regression': False,
        'senti': False,
        'classification': True,
        'clustering': False,
        'member': False,
        'creative': False
    }
    if request.method == 'GET':
        return render_template('classification.html', menu=menu)
    else:
        f = request.files['image']
        filename = os.path.join(app.root_path,
                                'static/images/uploads/') + secure_filename(
                                    f.filename)
        f.save(filename)
        img = np.array(Image.open(filename).resize((224, 224)))
        yhat = vgg.predict(img.reshape(-1, 224, 224, 3))
        label_key = np.argmax(yhat)
        label = decode_predictions(yhat)
        label = label[0][0]
        return render_template('cla_result.html',
                               menu=menu,
                               filename=secure_filename(f.filename),
                               label=label[1],
                               pct='%.2f' % (label[2] * 100))
Example #23
0
def classification():
    menu = {
        'home': False,
        'intro': False,
        'rgrs': False,
        'stmt': False,
        'clsf': True,
        'clst': False,
        'user': False
    }
    if request.method == 'GET':
        return render_template('classification.html', menu=menu)
    else:
        try:
            f = request.files['image']
            filename = os.path.join(app.root_path,
                                    'static/images/uploads') + secure_filename(
                                        f.filename)
            f.save(filename)

            img = np.array(Image.open(filename).resize((224, 224)))
            yhat = vgg.predict(img.reshape(-1, 224, 224, 3))
            label_key = np.argmax(yhat)
            label = decode_predictions(yhat)
            label = label[0][0]
        except:
            return render_template('classification.html', menu=menu)

        return render_template('cla_result.html',
                               menu=menu,
                               filename=secure_filename(f.filename),
                               name=label[1],
                               pct='%.2f' % (label[2] * 100))
Example #24
0
def features_cnn(X_CNN, CNN_PREDICTIONS, images_path, _dataImages):
    model = VGG16(weights='imagenet', include_top=True)
    start = time.time()
    if len(CNN_PREDICTIONS) <= 0:
        start_i = time.time()
        img_list = process_images_keras(_dataImages, images_path)
        end_i = time.time()
        print("Processed Images finished: {}".format(end_i - start_i))
        #model = ResNet50(weights='imagenet')
        # Convert from list to ndarray
        img_array_list = np.vstack(img_list)
        # Feed all images to the model
        print("No Cached Predictions")
        CNN_PREDICTIONS = model.predict(img_array_list)
        print("Finished dataset predictions")
    else:
        print("Using Cached Predictions")
    end = time.time()
    print("Model Predictions finished: {}".format(end - start))
    #print("Resulting shape of the network output: {}".format(preds.shape))
    concepts = decode_predictions(CNN_PREDICTIONS, top=5)
    # Experiment with this parameter
    k = 5
    # Get the top K most probable concepts per image
    sorted_concepts = np.argsort(CNN_PREDICTIONS, axis=1)[:, ::-1][:, :k]
    data_tags = concepts
    mlb = MultiLabelBinarizer(classes=range(0, 1000))
    X_CNN = mlb.fit_transform(sorted_concepts)
    # print(tags_bow.shape)
    # model, concepts, mlb, tags_bow, predictions
    return model, concepts, mlb, X_CNN, CNN_PREDICTIONS
Example #25
0
    def infer(self, data):
        # if you need to get file uploaded, get the path from input_file_path in data
        # load an image from file
        image = load_img(data['input_file_path'], 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
        yhat = self.model.predict(image)

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

        result = {"label": label[1], "confidence": label[2] * 100}

        return result  # return a dict
Example #26
0
    def run(self):
        global graph, model
        with graph.as_default():
            image = load_img(self.path, 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)
            pred = model.predict(image)
            label = decode_predictions(pred)
            #print(label)
            label = label[0][0]
            self.myapp.Display_Title(label[1])
            #print('%s (%.2f%%)' % (label[1], label[2]*100))
            wiki_content = ''
            try:
                info_page = wiki.page(label[1])
                img_content = info_page.content
                #print('------Wiki------')
                #print(img_content[:200]+'....')
                wiki_content = img_content[:350] + '....'
            except:
                tb = traceback.format_exc()
                print(tb)

            s_result = ''
            #print('------Related----------')
            s = search(label[1], num=3, stop=1)
            for j in s:
                s_result = s_result + j + "\n"

            info = [label[1], wiki_content, s_result]
            self.myapp.Display(info)
            playsound(label[1] + ".mp3")
Example #27
0
    def infer(self, img_filename):
        image = self.input_preprocess(img_filename)
        yhat = self.model.predict(image)
        label = decode_predictions(yhat)
        label = label[0][0]

        print('%s (%.2f%%)' % (label[1], label[2] * 100))
Example #28
0
def main():

    if len(sys.argv) != 2:
        print('以下のように入力してください')
        print('python simple_vgg16_usage.py [image file path]')
        sys.exit(1)

    file_name = sys.argv[1]

    # 学習済みのVGG16(学習済みの重みも含める)をロード
    model = VGG16(weights='imagenet')
    model.summary()

    # 画像ファイルの読み込み(サイズを224 * 224にリサイズ)
    img = image.load_img(file_name, target_size=(224, 224))

    # 読み込んだPIL形式の画像をarrayに変換
    x = image.img_to_array(img)

    # 3次元テンソル(rows, cols, channels) を
    # 4次元テンソル (samples, rows, cols, channels) に変換
    # 入力画像は1枚なのでsamples=1でよい
    x = np.expand_dims(x, axis=0)

    preds = model.predict(preprocess_input(x))
    results = decode_predictions(preds, top=5)[0]
    for result in results:
        print(result)

    cam = Grad_Cam(model, x[0], 'block5_conv3')
    img = array_to_img(cam)
    img.save('cam.jpg')
Example #29
0
def predict():
    print('model predict is called.  ', file=sys.stderr)
    #create instance of VGG16 model
    model = VGG16()

    # read client request
    message = request.get_json(force=True)
    encoded = message['image']
    decoded = base64.b64decode(encoded)
    image = Image.open(io.BytesIO(decoded))

    image = preprocessImage(image, target_size=(224, 224))
    #print(image.shape  , file=sys.stderr)

    # predict the probability across all output classes
    yhat = model.predict(image)

    # convert the probabilities to class labels
    label1 = decode_predictions(yhat)

    # retrieve the most likely result, e.g. highest probability
    label = label1[0][0]
    objName = label[1]
    objConfidence = label[2] * 100

    # print the classification
    #print('%s (%.2f%%)' % (label[1], label[2]*100), file=sys.stderr)

    #prepare return response
    response = {'prediction': {'name': objName, 'confidence': objConfidence}}

    # clear this session before returning to client
    keras.backend.clear_session()
    return jsonify(response)
Example #30
0
def item_detection(img_file):

    # <tensor> is not an element of this graph error に対処
    global graph
    with graph.as_default():

        model = VGG16(include_top=True,
                      weights='imagenet',
                      input_tensor=None,
                      input_shape=None)

        img = image.load_img(img_file, target_size=(224, 224))
        x = image.img_to_array(img)

        # 3次元テンソル(rows, cols, channels) を
        # 4次元テンソル (samples, rows, cols, channels) に変換
        # 入力画像は1枚なのでsamples=1でよい
        x = np.expand_dims(x, axis=0)

        # VGG16の1000クラスはdecode_predictions()で文字列に変換される
        # preprocess_inputでVGG16の平均を引く前処理を行う
        preds = model.predict(preprocess_input(x))
        results = decode_predictions(preds, top=5)[0]

        return results[0][1], results[0][2]
Example #31
0
def image_to_objects(img_path):
    model = VGG16(weights='imagenet')
    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)    
    return [label[1] for label in decode_predictions(preds, top=3)[0]]
Example #32
0
import numpy as np
import matplotlib.pyplot as plt
import cv2
from utils import init_keras

init_keras()

img_path = r"C:\Users\huxiaomi\Downloads\deep-learning\data\kaggle-dogs-vs-cats\small\test\cats\cat.1502.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)

model = VGG16(weights='imagenet')
preds = model.predict(x)
print('Predicted:', decode_predictions(preds, top=3)[0])

idx_of_max = np.argmax(preds[0])
hit_output = model.output[:, idx_of_max]

last_conv_layer = model.get_layer('block5_conv3')

grads = K.gradients(hit_output, last_conv_layer.output)[0]
pooled_grads = K.mean(grads, axis=(0, 1, 2))

iterate = K.function([model.input], [pooled_grads, last_conv_layer.output[0]])
pooled_grads_value, last_conv_layer_output_value = iterate([x])

for i in range(512):
    last_conv_layer_output_value[:, :, i] *= pooled_grads_value[i]
Example #33
0
from keras.preprocessing.image import load_img, img_to_array
import numpy as np


model = VGG16()

dog = load_img('imgs/dog.jpg', target_size=(224, 224))
dog = img_to_array(dog)
cat = load_img('imgs/cat.jpg', target_size=(224, 224))
cat = img_to_array(cat)
goma = load_img('imgs/goma.jpeg', target_size=(224, 224))
goma = img_to_array(goma)

# convert RGB2BGR and centerize
dog = preprocess_input(dog)
cat = preprocess_input(cat)
goma = preprocess_input(goma)

input_array = np.stack([dog, cat, goma])

probs = model.predict(input_array)
results = decode_predictions(probs)

assume_dog = results[0]
assume_cat = results[1]
assume_goma = results[2]

print(assume_dog)
print(assume_cat)
print(assume_goma)