Beispiel #1
0
    # Use the inverse of the squared norm of the CNN features as the weight for each layer
    weights = 1. / (feat_norm**2)

    # Normalise the weights such that the sum of the weights = 1
    weights = weights / weights.sum()
    layer_weight = dict(zip(layers, weights))

    opts.update({'layer_weight': layer_weight})

    # Reconstruction
    snapshots_dir = os.path.join(save_dir, 'snapshots',
                                 'image-%s' % image_label)
    recon_img, loss_list = reconstruct_image(
        features,
        net,
        net_gen,
        save_intermediate=True,
        save_intermediate_path=snapshots_dir,
        **opts)

    # Save the results

    # Save the raw reconstructed image
    save_name = 'recon_img' + '-' + image_label + '.mat'
    sio.savemat(os.path.join(save_dir, save_name), {'recon_img': recon_img})

    # To better display the image, clip pixels with extreme values (0.02% of
    # pixels with extreme low values and 0.02% of the pixels with extreme high
    # values). And then normalise the image by mapping the pixel value to be
    # within [0,255].
    save_name = 'recon_img_normalized' + '-' + image_label + '.jpg'
Beispiel #2
0
    # Weight of each layer in the total loss function
    num_of_layer = len(layers)
    feat_norm_list = np.zeros(num_of_layer, dtype='float32')
    for j, layer in enumerate(layers):
        # Norm of the CNN features for each layer
        feat_norm_list[j] = np.linalg.norm(features[layer])
    # Use the inverse of the squared norm of the CNN features as the weight for each layer
    weights = 1. / (feat_norm_list**2)
    # Normalise the weights such that the sum of the weights = 1
    weights = weights / weights.sum()
    layer_weight = {}
    for j, layer in enumerate(layers):
        layer_weight[layer] = weights[j]
    opts['layer_weight'] = layer_weight

    # Reconstruction
    recon_img, loss_list = reconstruct_image(features, net, net_gen, **opts)

    # Save the results
    save_name = 'recon_img' + '_' + subject + '_' + roi + '_Img%04d.mat' % image_label
    # Save the raw reconstructed image
    sio.savemat(os.path.join(save_path, save_name), {'recon_img': recon_img})

    # To better display the image, clip pixels with extreme values (0.02% of
    # pixels with extreme low values and 0.02% of the pixels with extreme high
    # values). And then normalise the image by mapping the pixel value to be
    # within [0,255].
    save_name = 'recon_img' + '_' + subject + '_' + roi + '_Img%04d.jpg' % image_label
    PIL.Image.fromarray(normalise_img(clip_extreme_value(
        recon_img, pct=0.04))).save(os.path.join(save_path, save_name))