Exemple #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)
Exemple #2
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')
Exemple #3
0
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."
                )
                exit()

            if version == 0:
                print("Training SAM-VGG")
Exemple #4
0
        x_maps = Input((nb_gaussian, shape_r_gt, shape_c_gt))
        #x_maps = Input((shape_r_gt, shape_c_gt, nb_gaussian))   #Nueva version
        
        if version == 0:   #NO USADO
        #    m = Model(input=[x, x_maps], output=sam_vgg([x, x_maps]))
            print("Not Compiling SAM-VGG")   #Nueva version
        #    m.compile(RMSprop(lr=1e-4), loss=[kl_divergence, correlation_coefficient, nss])
        elif version == 1:
            '''Hint of the problem: something is not the output of a keras layer. 
            You should put it in a lambda layer
            When invoking the Model API, the value for outputs argument should 
            be tensor(or list of tensors), in this case it is a list of list of 
            tensors, hence there is a problem'''
            #m = Model(input=[x, x_maps], output=sam_resnet([x, x_maps]))
            #m = Model(inputs=[x, x_maps], outputs=sam_resnet([x, x_maps]))  #New version
            m = ModelAux(inputs=[x, x_maps], outputs=sam_resnet([x, x_maps])) #Final version

            print("Compiling SAM-ResNet")
            m.compile(RMSprop(lr=1e-4), 
                      loss=[kl_divergence, correlation_coefficient, nss])
            print("Compilado")
        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.")
                exit()

            if version == 0:
                print("Training SAM-VGG")
Exemple #5
0
                    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,
                              kl_divergence, kl_divergence, kl_divergence,
                              kl_divergence, kl_divergence, kl_divergence,
                              kl_divergence
                          ])  #
            elif version == 1:
                m = Model(inputs=img_input, outputs=sam_resnet(img_input))

                # Load weights
                weights_path = get_file(
                    'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
                    RES_TF_WEIGHTS_PATH_NO_TOP,
                    cache_subdir='models')
                print('loading weights from {}'.format(weights_path))
                m.load_weights(weights_path, by_name=True)
                # m.load_weights('weights.sam-resnet.06-15.6823.pkl')
                # m.load_weights('weights.sam-resnet.00-17.5592.pkl', by_name=True)
                print("Compiling SAM-ResNet")
                m.compile(Adam(lr=1e-4),
                          loss=[kl_divergence, kl_divergence, kl_divergence])
                # m.compile(RMSprop(lr=1e-4), loss=weighted_crossentropy)
            else: