Пример #1
0
    def __input_features(self):
        h, w = self.net.blobs['data'].data.shape[-2:]

        # Load the original image
        orig_img = PIL.Image.open('./examples/data/orig_img.jpg')
        orig_img = imresize(orig_img, (h, w), interp='bicubic')

        # Load input image features
        features = get_cnn_features(self.net, orig_img, self.layer_list)

        return features
Пример #2
0
save_dir = './result'
save_subdir = __file__.split('.')[0] + '_' + datetime.now().strftime(
    '%Y%m%dT%H%M%S')
save_path = os.path.join(save_dir, save_subdir)
os.makedirs(save_path)

# Setup the test image and image features -------------------------------------

# Test image
orig_img = PIL.Image.open('./data/orig_img.jpg')

# Resize the image to match the input size of the CNN model
orig_img = imresize(orig_img, (h, w), interp='bicubic')

# Extract CNN features from the test image
features = get_cnn_features(net, orig_img, layer_list)

# Save the test image
save_name = 'orig_img.jpg'
PIL.Image.fromarray(orig_img).save(os.path.join(save_path, save_name))

# Setup layer weights (optional) ----------------------------------------------

# Weight of each layer in the total loss function

# Norm of the CNN features for each layer
feat_norm_list = np.array(
    [np.linalg.norm(features[layer]) for layer in layer_list], dtype='float32')

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