Exemplo n.º 1
0
def detect_image(filename):
    # Load data
    img = np.array(Image.open(filename).convert(params["channel"]))
    coords, _, _ = image.slide2d(sz=img.shape[:2], K=64, S=32)
    patches = image.crop_patches(img=img, coords=coords, patch_sz=64)
    loader = NumpyImageLoader(ndarray_data=patches, batch_size=16, n_workers=cpu_count(), pin_memory=True,
                              shuffle=False).loader

    # Predict
    softmaxs = []
    model.eval()
    for X in loader:
        X = X[0].to(DEVICE)
        logits = model(X)
        softmaxs.append(F.softmax(logits, dim=1).detach().cpu().numpy())
    softmaxs = np.concatenate(softmaxs, axis=0)

    # Post-processing
    labels = image.post_process(softmaxs[:, 1], coords, 8, params["threshold"], 32, pools=pools)
    decision = image.fusion(labels)
    return decision  #1 fake, 0 real
# Test on authentic images

for i, file in tqdm(enumerate(au_files), total=n_au_files):
    # Load softmaxs and coords from disk
    data = np.load(file).item()
    softmaxs, coords = data["softmaxs"], data["coords"]
    softmaxs = softmaxs[:, 1]

    # Postprocess
    labels = image.post_process(softmaxs,
                                coords,
                                8,
                                params["threshold"],
                                32,
                                pools=pools)
    mark = image.fusion(labels)
    scores_au.append(mark)

# In[8]:

# Test on tampered images

for i, file in tqdm(enumerate(tp_files), total=n_tp_files):
    # Load softmaxs and coords from disk
    data = np.load(file).item()
    softmaxs, coords = data["softmaxs"], data["coords"]
    softmaxs = softmaxs[:, 1]

    # Postprocess
    labels = image.post_process(softmaxs,
                                coords,
coords, _, _ = image.slide2d(sz=img.shape[:2], K=64, S=32)
patches = image.crop_patches( img=img, coords=coords, patch_sz=64)
loader = NumpyImageLoader(ndarray_data=patches, batch_size=16, n_workers=cpu_count(), pin_memory=True, shuffle=False).loader

# Predict
softmaxs = []
model.eval()
for X in loader:
    X = X[0].to(DEVICE)
    logits = model(X)
    softmaxs.append(F.softmax(logits, dim=1).detach().cpu().numpy())
softmaxs = np.concatenate(softmaxs, axis=0)

# Post-processing
labels = image.post_process(softmaxs[:,1], coords, 8, params["threshold"], 32, pools=pools)
decision = image.fusion(labels)
if decision==1:
    if GT=="Positive":
        print("Prediction: Positive ==> True")
    else:
        print("Prediction: Positive ==> False")
else:
    if GT=="Positive":
        print("Prediction: Negative ==> False")
    else:
        print("Prediction: Negative ==> True")

# Draw score_map
score_map = image.reconstruct_heatmap(softmaxs[:,1], coords, img.shape, 64)
plt.figure(figsize=(16,8))