我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用scipy.misc.face()。
def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('--cuda', action='store_true', default=False, help='Use NVIDIA GPU acceleration') parser.add_argument('--img', type=str, default='', help='Input image path') parser.add_argument('--out_dir', type=str, default='./result/cam/', help='Result directory path') args = parser.parse_args() args.cuda = args.cuda and torch.cuda.is_available() if args.cuda: print("Using GPU for acceleration") else: print("Using CPU for computation") if args.img: print('Input image: {}'.format(args.img)) else: print('Input image: raccoon face (scipy.misc.face())') print('Output directory: {}'.format(args.out_dir)) print() return args
def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('--cuda', action='store_true', default=False, help='Use NVIDIA GPU acceleration') parser.add_argument('--img', type=str, default='', help='Input image path') parser.add_argument('--out_dir', type=str, default='./result/grad/', help='Result directory path') parser.add_argument('--n_samples', type=int, default=10, help='Sample size of SmoothGrad') args = parser.parse_args() args.cuda = args.cuda and torch.cuda.is_available() if args.cuda: print("Using GPU for acceleration") else: print("Using CPU for computation") if args.img: print('Input image: {}'.format(args.img)) else: print('Input image: raccoon face (scipy.misc.face())') print('Output directory: {}'.format(args.out_dir)) print('Sample size of SmoothGrad: {}'.format(args.n_samples)) print() return args
def test_extract_patches_max_patches(): face = downsampled_face i_h, i_w = face.shape p_h, p_w = 16, 16 patches = extract_patches_2d(face, (p_h, p_w), max_patches=100) assert_equal(patches.shape, (100, p_h, p_w)) expected_n_patches = int(0.5 * (i_h - p_h + 1) * (i_w - p_w + 1)) patches = extract_patches_2d(face, (p_h, p_w), max_patches=0.5) assert_equal(patches.shape, (expected_n_patches, p_h, p_w)) assert_raises(ValueError, extract_patches_2d, face, (p_h, p_w), max_patches=2.0) assert_raises(ValueError, extract_patches_2d, face, (p_h, p_w), max_patches=-1.0)
def load_image(source, scale=1, gray=False, memory=Memory(cachedir=None)): data_dir = get_data_dirs()[0] if source == 'face': image = face(gray=gray) image = image.astype(np.float32) / 255 if image.ndim == 2: image = image[..., np.newaxis] if scale != 1: image = memory.cache(rescale)(image, scale=scale) return image elif source == 'lisboa': image = imread(join(data_dir, 'images', 'lisboa.jpg'), as_grey=gray) image = image.astype(np.float32) / 255 if image.ndim == 2: image = image[..., np.newaxis] if scale != 1: image = memory.cache(rescale)(image, scale=scale) return image elif source == 'aviris': image = open_image( join(data_dir, 'aviris', 'f100826t01p00r05rdn_b/' 'f100826t01p00r05rdn_b_sc01_ort_img.hdr')) image = np.array(image.open_memmap(), dtype=np.float32) good_bands = list(range(image.shape[2])) good_bands.remove(110) image = image[:, :, good_bands] indices = image == -50 image[indices] = -1 image[~indices] -= np.min(image[~indices]) image[~indices] /= np.max(image[~indices]) return image else: raise ValueError('Data source is not known')
def test_connect_regions(): try: face = sp.face(gray=True) except AttributeError: # Newer versions of scipy have face in misc from scipy import misc face = misc.face(gray=True) for thr in (50, 150): mask = face > thr graph = img_to_graph(face, mask) assert_equal(ndimage.label(mask)[1], connected_components(graph)[0])
def test_connect_regions_with_grid(): try: face = sp.face(gray=True) except AttributeError: # Newer versions of scipy have face in misc from scipy import misc face = misc.face(gray=True) mask = face > 50 graph = grid_to_graph(*face.shape, mask=mask) assert_equal(ndimage.label(mask)[1], connected_components(graph)[0]) mask = face > 150 graph = grid_to_graph(*face.shape, mask=mask, dtype=None) assert_equal(ndimage.label(mask)[1], connected_components(graph)[0])
def _downsampled_face(): try: face = sp.face(gray=True) except AttributeError: # Newer versions of scipy have face in misc from scipy import misc face = misc.face(gray=True) face = face.astype(np.float32) face = (face[::2, ::2] + face[1::2, ::2] + face[::2, 1::2] + face[1::2, 1::2]) face = (face[::2, ::2] + face[1::2, ::2] + face[::2, 1::2] + face[1::2, 1::2]) face = face.astype(np.float32) face /= 16.0 return face
def _orange_face(face=None): face = _downsampled_face() if face is None else face face_color = np.zeros(face.shape + (3,)) face_color[:, :, 0] = 256 - face face_color[:, :, 1] = 256 - face / 2 face_color[:, :, 2] = 256 - face / 4 return face_color
def _make_images(face=None): face = _downsampled_face() if face is None else face # make a collection of faces images = np.zeros((3,) + face.shape) images[0] = face images[1] = face + 1 images[2] = face + 2 return images
def test_extract_patches_all_color(): face = orange_face i_h, i_w = face.shape[:2] p_h, p_w = 16, 16 expected_n_patches = (i_h - p_h + 1) * (i_w - p_w + 1) patches = extract_patches_2d(face, (p_h, p_w)) assert_equal(patches.shape, (expected_n_patches, p_h, p_w, 3))
def test_extract_patches_all_rect(): face = downsampled_face face = face[:, 32:97] i_h, i_w = face.shape p_h, p_w = 16, 12 expected_n_patches = (i_h - p_h + 1) * (i_w - p_w + 1) patches = extract_patches_2d(face, (p_h, p_w)) assert_equal(patches.shape, (expected_n_patches, p_h, p_w))
def test_reconstruct_patches_perfect(): face = downsampled_face p_h, p_w = 16, 16 patches = extract_patches_2d(face, (p_h, p_w)) face_reconstructed = reconstruct_from_patches_2d(patches, face.shape) np.testing.assert_array_equal(face, face_reconstructed)
def test_reconstruct_patches_perfect_color(): face = orange_face p_h, p_w = 16, 16 patches = extract_patches_2d(face, (p_h, p_w)) face_reconstructed = reconstruct_from_patches_2d(patches, face.shape) np.testing.assert_array_equal(face, face_reconstructed)
def main(): args = parse_args() if not os.path.exists(args.out_dir): os.makedirs(args.out_dir) target_layer_names = ['35'] target_index = None # Prepare input image if args.img: img = cv2.imread(args.img, 1) else: img = misc.face() img = np.float32(cv2.resize(img, (224, 224))) / 255 preprocessed_img = preprocess_image(img, args.cuda) model = vgg19(pretrained=True) if args.cuda: model.cuda() # Prediction output = model(preprocessed_img) pred_index = np.argmax(output.data.cpu().numpy()) print('Prediction: {}'.format(IMAGENET_LABELS[pred_index])) # Prepare grad cam grad_cam = GradCam( pretrained_model=model, target_layer_names=target_layer_names, cuda=args.cuda) # Compute grad cam mask = grad_cam(preprocessed_img, target_index) save_cam_image(img, mask, os.path.join(args.out_dir, 'grad_cam.jpg')) print('Saved Grad-CAM image') # Reload preprocessed image preprocessed_img = preprocess_image(img) # Compute guided backpropagation guided_backprop = GuidedBackpropGrad( pretrained_model=model, cuda=args.cuda) guided_backprop_saliency = guided_backprop(preprocessed_img, index=target_index) cam_mask = np.zeros(guided_backprop_saliency.shape) for i in range(guided_backprop_saliency.shape[0]): cam_mask[i, :, :] = mask cam_guided_backprop = np.multiply(cam_mask, guided_backprop_saliency) save_as_gray_image( cam_guided_backprop, os.path.join(args.out_dir, 'guided_grad_cam.jpg')) print('Saved Guided Grad-CAM image')