Ejemplo n.º 1
0
def main():
    options = json.loads(os.environ['SMILER_PARAMETER_MAP'])
    network_string = options.get('network', 'SAM-VGG')
    use_default_center_bias = options.get('center_prior',
                                          'default') == 'default'

    do_default_smoothing = options.get('do_smoothing', 'default') == 'default'

    imgs_test_path = '/opt/input_vol/'
    output_folder = '/opt/output_vol/'

    os.makedirs(output_folder, exist_ok=True)

    x = Input((3, shape_r, shape_c))
    x_maps = Input((nb_gaussian, shape_r_gt, shape_c_gt))

    if network_string == "SAM-VGG":
        m = Model(input=[x, x_maps], output=sam_vgg([x, x_maps]))
        print("Compiling SAM-VGG...")
        m.compile(RMSprop(lr=1e-4),
                  loss=[kl_divergence, correlation_coefficient, nss])

        print("Loading SAM-VGG weights...")
        m.load_weights('weights/sam-vgg_salicon_weights.pkl')
    elif network_string == "SAM-ResNet":
        m = Model(input=[x, x_maps], output=sam_resnet([x, x_maps]))
        print("Compiling SAM-ResNet...")
        m.compile(RMSprop(lr=1e-4),
                  loss=[kl_divergence, correlation_coefficient, nss])

        print("Loading SAM-ResNet weights...")
        m.load_weights('weights/sam-resnet_salicon_weights.pkl')
    else:
        raise NotImplementedError(
            "The only supported network strings are SAM-VGG and SAM-ResNet! '{}' is unknown."
            .format(network_string))

    gaussian = np.zeros((b_s, nb_gaussian, shape_r_gt, shape_c_gt))

    def compute_saliency(image_path):
        if use_default_center_bias:
            predictions = m.predict(
                [preprocess_images([image_path], shape_r, shape_c),
                 gaussian])[0]
        else:
            predictions = m.predict(
                [preprocess_images([image_path], shape_r, shape_c),
                 gaussian])[0]

        original_image = cv2.imread(image_path, 0)
        res = postprocess_predictions(
            predictions[0][0],
            original_image.shape[0],
            original_image.shape[1],
            do_default_smoothing=do_default_smoothing)
        return res

    run_model(compute_saliency)
Ejemplo n.º 2
0
def compute_SAMSal(rgbvid, filename):
    # Check if currently exists
    outname, ext = os.path.splitext(filename)
    outname += '_sam.npy'
    print('Checking for', outname)
    if os.path.exists(outname):
        salmap = np.load(outname)
        if salmap.shape[-1] == rgbvid.shape[-1]:
            print('file found')
            return salmap
        print('Saliency Map length incorrect. Recomputing')
    else:
        print(outname, 'does not exist. Computing from scratch')

    x = Input((3, shape_r, shape_c))
    x_maps = Input((nb_gaussian, shape_r_gt, shape_c_gt))

    m = Model(input=[x, x_maps], output=sam_vgg([x, x_maps]))
    print("Compiling SAM-VGG")
    m.compile(RMSprop(lr=1e-4),
              loss=[kl_divergence, correlation_coefficient, nss])

    nb_imgs_test = rgbvid.shape[-1]

    if nb_imgs_test % b_s != 0:
        print(
            "The number of test images should be a multiple of the batch size. Please change your batch size in config.py accordingly."
        )
        exit()

    print("Loading SAM-VGG weights")
    m.load_weights('./sam/weights/sam-vgg_salicon_weights.pkl')

    print("Predicting saliency maps")
    predictions = m.predict_generator(generator_from_mat(b_s=b_s, vid=rgbvid),
                                      nb_imgs_test)[0]

    H, W, _, n = rgbvid.shape
    output = np.zeros((H, W, n))
    for i, pred in enumerate(predictions):
        res = postprocess_predictions(pred[0], H, W)
        output[..., i] = normalize(logsal(res))

        #name = 'frame' + str(i) + '.png'
        #original_image = cv2.imread(imgs_test_path + name, 0)
        #res = postprocess_predictions(pred[0], original_image.shape[0], original_image.shape[1])
        #cv2.imwrite(output_folder + '%s' % name, res.astype(int))

    outname, ext = os.path.splitext(filename)
    outname += '_sam.npy'
    np.save(outname, output)
    return output
Ejemplo n.º 3
0
def saliencyattentivemodel(inputimage):

    x = Input((3, shape_r, shape_c))
    x_maps = Input((nb_gaussian, shape_r_gt, shape_c_gt))

    # version (0 for SAM-VGG and 1 for SAM-ResNet)
    if version == 0:
        m = Model(input=[x, x_maps], output=sam_vgg([x, x_maps]))
        print("Compiling SAM-VGG")
        m.compile(RMSprop(lr=1e-4),
                  loss=[kl_divergence, correlation_coefficient, nss])
    elif version == 1:
        m = Model(input=[x, x_maps], output=sam_resnet([x, x_maps]))
        print("Compiling SAM-ResNet")
        m.compile(RMSprop(lr=1e-4),
                  loss=[kl_divergence, correlation_coefficient, nss])
    else:
        raise NotImplementedError
    # Output Folder Path
    output_folder = 'predictions/'
    nb_imgs_test = 1  #len(file_names)

    if nb_imgs_test % b_s != 0:
        print(
            "The number of test images should be a multiple of the batch size. Please change your batch size in config.py accordingly."
        )
        exit()

    if version == 0:
        print("Loading SAM-VGG weights")
        m.load_weights('weights/sam-vgg_salicon_weights.pkl')
    elif version == 1:
        print("Loading SAM-ResNet weights")
        m.load_weights('weights/sam-resnet_salicon_weights.pkl')

    predictions = m.predict_generator(
        generator_test_singleimage(b_s=b_s, image=inputimage), nb_imgs_test)[0]
    print("predictions:", predictions[0])
    outname = 'SalmapFromWrapper.jpg'
    original_image = image
    res = postprocess_predictions(predictions[0], original_image.shape[0],
                                  original_image.shape[1])
    #mport pdb; pdb.set_trace()
    cv2.imwrite(output_folder + '%s' % outname, res.astype(int))
    cv2.imshow('salmap', res.astype('uint8'))
    cv2.waitKey(0)
    return res.astype('uint8')
Ejemplo n.º 4
0
    file_names = ['{}.jpg'.format(f) for f in file_names]
    video_frames = [f for f in file_names if video in f]
    return video_frames


if __name__ == '__main__':
    if len(sys.argv) == 1:
        raise NotImplementedError
    else:
        phase = sys.argv[1]
        x = Input((3, shape_r, shape_c))
        x_maps = Input((nb_gaussian, shape_r_gt, shape_c_gt))

        if version == 0:
            m = Model(input=[x, x_maps], output=sam_vgg([x, x_maps]))
            print("Compiling SAM-VGG")
            m.compile(RMSprop(lr=1e-4),
                      loss=[kl_divergence, correlation_coefficient, nss])
        elif version == 1:
            m = Model(input=[x, x_maps], output=sam_resnet([x, x_maps]))
            print("Compiling SAM-ResNet")
            m.compile(RMSprop(lr=1e-4),
                      loss=[kl_divergence, correlation_coefficient, nss])
        else:
            raise NotImplementedError

        if phase == 'train':
            if nb_imgs_train % b_s != 0 or nb_imgs_val % b_s != 0:
                print(
                    "The number of training and validation images should be a multiple of the batch size. Please change your batch size in config.py accordingly."

if __name__ == '__main__':
    seed = 7
    random.seed(seed)
    test_data = []
    mypath = sys.argv[1]
    testing_images = [sys.argv[1] + "/" + f for f in os.listdir(mypath)]
    testing_images.sort()

    for image in testing_images:
        data = {'image': image}
        test_data.append(data)

    x = Input(batch_shape=(1, im_res, im_res, 3))
    m = Model(inputs=x, outputs=sam_vgg(x))

    print("Loading weights")
    m.load_weights('PAGE-Net.h5')
    print("Making prediction")
    # Output Folder Path
    saliency_output = sys.argv[2]
    print "Saving saliency files in " + saliency_output

    if not os.path.exists(saliency_output):
        os.makedirs(saliency_output)

    for data in test_data:
        Ximg, original_image, img_name = get_test(data)
        predictions = m.predict(Ximg, batch_size=1)
        res_saliency = postprocess_predictions(predictions[9][0, :, :, 0],
Ejemplo n.º 6
0
            # p = [0.1]
            # import tensorflow as tf
            # p = tf.stack(p, name='ToFloat')
            # g = tf.stack(g, name='ToFloat')
            # k = weighted_crossentropy(g, p)
            # w = losses.binary_crossentropy(g, p)
            # sess = tf.Session()
            # print(sess.run(k))
            # print(sess.run(w))

            # fix random seed for reproducibility
            seed = 7
            np.random.seed(seed)

            if version == 0:
                m = Model(inputs=img_input, outputs=sam_vgg(img_input))

                # Load weights
                weights_path = get_file(
                    'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',
                    VGG_TF_WEIGHTS_PATH,
                    cache_subdir='models')

                weights_path = 'weights.sam-vgg.05-196.3380.h5'

                m.load_weights(weights_path, by_name=True)

                print("Compili  ng SAM-VGG")
                m.compile(Adam(lr=1e-4),
                          loss=[
                              kl_divergence, kl_divergence, kl_divergence,