Exemplo n.º 1
0
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, (args.img_size, args.img_size))) / 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,
                       img_size=args.img_size,
                       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')
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 = torch.load(
        "/home/moirai_zhang/HumpbackWhale/identification/new_model.pth")
    if args.cuda:
        model.cuda()

    # Prediction
    output = model(preprocessed_img)
    pred_index = output[0]  #np.argmax(output.data.cpu().numpy())
    #print('Prediction: {}'.format(IMAGENET_LABELS[pred_index]))

    # Compute vanilla gradient
    vanilla_grad = VanillaGrad(pretrained_model=model, cuda=args.cuda)
    vanilla_saliency = vanilla_grad(preprocessed_img, index=target_index)
    save_as_gray_image(vanilla_saliency,
                       os.path.join(args.out_dir, 'vanilla_grad.jpg'))
    print('Saved vanilla gradient image')

    # Reload preprocessed image
    preprocessed_img = preprocess_image(img, args.cuda)

    # Compute guided gradient
    guided_grad = GuidedBackpropGrad(pretrained_model=model, cuda=args.cuda)
    guided_saliency = guided_grad(preprocessed_img, index=target_index)
    save_as_gray_image(guided_saliency,
                       os.path.join(args.out_dir, 'guided_grad.jpg'))
    print('Saved guided backprop gradient image')

    # Reload preprocessed image
    preprocessed_img = preprocess_image(img, args.cuda)

    # Compute smooth gradient
    smooth_grad = SmoothGrad(pretrained_model=model,
                             cuda=args.cuda,
                             n_samples=args.n_samples,
                             magnitude=True)
    smooth_saliency = smooth_grad(preprocessed_img, index=target_index)
    save_as_gray_image(smooth_saliency,
                       os.path.join(args.out_dir, 'smooth_grad.jpg'))
    print('Saved smooth gradient image')

    # Reload preprocessed image
    preprocessed_img = preprocess_image(img, args.cuda)

    # Compute guided smooth gradient
    guided_smooth_grad = GuidedBackpropSmoothGrad(pretrained_model=model,
                                                  cuda=args.cuda,
                                                  n_samples=args.n_samples,
                                                  magnitude=True)
    guided_smooth_saliency = guided_smooth_grad(preprocessed_img,
                                                index=target_index)
    save_as_gray_image(guided_smooth_saliency,
                       os.path.join(args.out_dir, 'guided_smooth_grad.jpg'))
    print('Saved guided backprop smooth gradient image')
Exemplo n.º 3
0
def preprocess_coco(raw_dir,
                    save_dir,
                    proc_shape,
                    target_H,
                    train_ratio,
                    val_ratio,
                    test_ratio,
                    random_split=False,
                    random_seed=13,
                    lim=-1,
                    chunk_size=5000,
                    targets3d=False):
    start_time = time.time()
    raw_files = [
        os.path.join(raw_dir, f) for f in os.listdir(raw_dir)
        if (os.path.isfile(os.path.join(raw_dir, f)) and f.lower().endswith((
            '.jpg', '.jpeg', '.png', '.tif', '.tiff', '.gif', '.bmp')))
    ]
    if lim is None:
        lim = -1
    if lim > 0:
        raw_files = raw_files[0:lim]
    # if there are more than 'chunk_size' raw files, we split the data into chunks of chunk_size to reduce the memory footprint
    # and storage space needed to save each training/val/test file on disk
    raw_files_l = list()
    for start in range(0, len(raw_files), chunk_size):
        end = start + chunk_size
        raw_files_l.append(raw_files[start:end])
    for chunk_num, raw_files in enumerate(raw_files_l):
        print('raw_files')
        print(raw_files)
        processed = list()
        targets = list()
        for r, raw_file in enumerate(raw_files):
            if r % 10 == 0:
                print('chunk:%d\tpreprocessing file %d' % (chunk_num, r))
            raw_img = imread(raw_file)
            img = preprocess_image(raw_img, proc_shape=proc_shape)
            if img is None:
                continue
            deproc = deprocess_image(img)
            deproc_ratio = deproc.shape[1] / deproc.shape[0]  # ratio of W:H
            if targets3d:
                targ = transform.resize(deproc, output_shape=(3, 112, 112))
                targets.append(targ)
            else:
                small_shape = (target_H, int(target_H * deproc_ratio))
                small_gray = downsample_image(deproc,
                                              new_size=small_shape,
                                              grey=True)
                targets.append(small_gray.flatten())
            # print('(caller) small_gray.shape:%s' % str(small_gray.shape))
            # print('(caller) img.shape:%s' % str(img.shape))
            processed.append(img)
        processed = np.asarray(processed)
        targets = np.asarray(targets)
        print('chunk:%d\tprocessed.shape:%s' %
              (chunk_num, str(processed.shape)))
        print('chunk:%d\ttargets.shape:%s' % (chunk_num, str(targets.shape)))
        X_train, X_val, X_test = split_data(processed,
                                            train_ratio=train_ratio,
                                            val_ratio=val_ratio,
                                            test_ratio=test_ratio,
                                            random=random_split,
                                            random_seed=random_seed)
        y_train, y_val, y_test = split_data(targets,
                                            train_ratio=train_ratio,
                                            val_ratio=val_ratio,
                                            test_ratio=test_ratio,
                                            random=random_split,
                                            random_seed=random_seed)
        print('chunk:%d\tX_train_%d.shape%s' %
              (chunk_num, chunk_num, str(X_train.shape)))
        print('chunk:%d\ty_train_%d.shape%s' %
              (chunk_num, chunk_num, str(y_train.shape)))
        if X_val is not None:
            print('chunk:%d\tX_val_%d.shape%s' %
                  (chunk_num, chunk_num, str(X_val.shape)))
        if y_val is not None:
            print('chunk:%d\ty_val_%d.shape%s' %
                  (chunk_num, chunk_num, str(y_val.shape)))
        if X_test is not None:
            print('chunk:%d\tX_test_%d.shape%s' %
                  (chunk_num, chunk_num, str(X_test.shape)))
        if y_test is not None:
            print('chunk:%d\ty_test_%d.shape%s' %
                  (chunk_num, chunk_num, str(y_test.shape)))

        save_data(X_train,
                  save_dir,
                  'X_train_%d' % chunk_num,
                  save_format='npy')
        if X_val is not None:
            save_data(X_val,
                      save_dir,
                      'X_val_%d' % chunk_num,
                      save_format='npy')
        if X_test is not None:
            save_data(X_test,
                      save_dir,
                      'X_test_%d' % chunk_num,
                      save_format='npy')
        save_data(y_train,
                  save_dir,
                  'y_train_%d' % chunk_num,
                  save_format='npy')
        if y_val is not None:
            save_data(y_val,
                      save_dir,
                      'y_val_%d' % chunk_num,
                      save_format='npy')
        if y_test is not None:
            save_data(y_test,
                      save_dir,
                      'y_test_%d' % chunk_num,
                      save_format='npy')
        del raw_files
        del processed
        del targets
    end_time = time.time()
    duration = (end_time - start_time) / 60.
    print('total duration: %f mins' % (duration))
Exemplo n.º 4
0
def main():
    parser = argparse.ArgumentParser()
    args = parser.parse_args()
    args.cuda = False
    args.img = './dog.jpg'
    args.out_dir = './result/grad/'
    args.n_samples = 50

    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 = vgg16(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]))

    # Compute vanilla gradient
    vanilla_grad = VanillaGrad(pretrained_model=model, cuda=args.cuda)
    vanilla_saliency = vanilla_grad(preprocessed_img, index=target_index)
    save_as_gray_image(vanilla_saliency,
                       os.path.join(args.out_dir, 'vanilla_grad.jpg'))
    print('Saved vanilla gradient image')

    # Reload preprocessed image
    preprocessed_img = preprocess_image(img, args.cuda)

    # Compute guided gradient
    guided_grad = GuidedBackpropGrad(pretrained_model=model, cuda=args.cuda)
    guided_saliency = guided_grad(preprocessed_img, index=target_index)
    save_as_gray_image(guided_saliency,
                       os.path.join(args.out_dir, 'guided_grad.jpg'))
    print('Saved guided backprop gradient image')

    # Reload preprocessed image
    preprocessed_img = preprocess_image(img, args.cuda)

    # Compute smooth gradient
    smooth_grad = SmoothGrad(pretrained_model=model,
                             cuda=args.cuda,
                             n_samples=args.n_samples,
                             magnitude=True)
    smooth_saliency = smooth_grad(preprocessed_img, index=target_index)
    save_as_gray_image(smooth_saliency,
                       os.path.join(args.out_dir, 'smooth_grad.jpg'))
    print('Saved smooth gradient image')

    # Reload preprocessed image
    preprocessed_img = preprocess_image(img, args.cuda)

    # Compute guided smooth gradient
    guided_smooth_grad = GuidedBackpropSmoothGrad(pretrained_model=model,
                                                  cuda=args.cuda,
                                                  n_samples=args.n_samples,
                                                  magnitude=True)
    guided_smooth_saliency = guided_smooth_grad(preprocessed_img,
                                                index=target_index)
    save_as_gray_image(guided_smooth_saliency,
                       os.path.join(args.out_dir, 'guided_smooth_grad.jpg'))
    print('Saved guided backprop smooth gradient image')