def createHeatmap(img):
    
    #im = preprocess_image_batch(['examples/cars.jpg'], color_mode="bgr")
    #img= cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    #img = cv2.resize(img,(224, 224))

    im = np.asarray(img)
    height, width = im.shape[:2]
    im = cv2.resize(im,(width*2, height*2))
    im = np.asarray([im])
    

    #im = image.img_to_array([img])
    
    #print(im.shape)
    #None, 3, None, None
    #(1, 800, 1280, 3)
    #im=np.swapaxes(np.swapaxes(im, 1, 2), 0, 1)
    im=np.swapaxes(im, 1, 3)
    im=np.swapaxes(im, 2, 3)
    out = model.predict(im)

    #s = "n02084071"
    s="n04576211"

    ids = synset_to_dfs_ids(s)
    heatmap = out[0,ids].sum(axis=0)
    #plt.imshow(heatmap)
    #plt.show()

    
    return heatmap
Exemple #2
0
def _demo_heatmap_script():
    """
    Here is a script to compute the heatmap of the dog synsets.
    We find the synsets corresponding to dogs on ImageNet website
    """
    im = preprocess_image_batch(['examples/dog.jpg'], color_mode='rgb')

    # Test pretrained model
    sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
    model = convnet('alexnet', weights_path='weights/alexnet_weights.h5', heatmap=True)
    model.compile(optimizer=sgd, loss='mse')

    out = model.predict(im)

    s = 'n02084071'
    # Most of the synsets are not in the subset of the synsets used in ImageNet recognition task.
    ids = np.array([id_ for id_ in synset_to_dfs_ids(s) if id_ is not None])
    heatmap = out[0, ids, :, :].sum(axis=0)
    return heatmap
Exemple #3
0
        img_batch = np.stack(img_list, axis=0)
    except:
        raise ValueError('when img_size and crop_size are None, images'
                         ' in image_paths must have the same shapes.')

    if out is not None and hasattr(out, 'append'):
        out.append(img_batch)
    else:
        return img_batch


if __name__ == "__main__":
    ### Here is a script to compute the heatmap of the dog synsets.
    ## We find the synsets corresponding to dogs on ImageNet website
    s = "n02084071"
    ids = synset_to_dfs_ids(s)
    # Most of the synsets are not in the subset of the synsets used in ImageNet recognition task.
    ids = np.array([id_ for id_ in ids if id_ is not None])

    im = preprocess_image_batch(['dog.jpg'], color_mode="rgb")

    # Test pretrained model
    sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
    model = convnet('alexnet',
                    weights_path="weights/alexnet_weights.h5",
                    heatmap=True)
    model.compile(optimizer=sgd, loss='mse')

    out = model.predict(im)
    heatmap = out[0, ids, :, :].sum(axis=0)
    img_batch = np.stack(img_list, axis=0)
    if not out is None:
        out.append(img_batch)
    else:
        return img_batch





if __name__ == "__main__":
    ### Here is a script to compute the heatmap of the cars synsets.
    ## We find the synsets corresponding to cars on ImageNet website
    s = "n02084071"
    ids = synset_to_dfs_ids(s)
    # Most of the synsets are not in the subset of the synsets used in ImageNet recognition task.
    ids = np.array([id for id in ids if id != None])
    
    im = preprocess_image_batch(['examples/dog.jpg'],color_mode="bgr")

    # Test pretrained model
    sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
    model = convnet('alexnet',weights_path="weights/alexnet_weights.h5", heatmap=True)
    model.compile(optimizer=sgd, loss='mse')
    
    
    out = model.predict(im)
    heatmap = out[0,ids,:,:].sum(axis=0)