Spaces:
Build error
Build error
Update app.py
Browse filesadded simplified generative inference
app.py
CHANGED
|
@@ -66,16 +66,89 @@ def predict(image):
|
|
| 66 |
|
| 67 |
return f"Predicted class: {predicted_class.item()}", sample_images_urls
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# # Create the Gradio interface
|
| 70 |
# iface = gr.Interface(fn=predict, inputs=gr.Image(type="numpy"), outputs="text") # Updated from gr.inputs.Image to gr.Image
|
| 71 |
|
| 72 |
-
# Create the Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
iface = gr.Interface(
|
| 74 |
-
fn=
|
| 75 |
-
inputs=
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
)
|
| 79 |
|
| 80 |
-
|
| 81 |
iface.launch()
|
|
|
|
| 66 |
|
| 67 |
return f"Predicted class: {predicted_class.item()}", sample_images_urls
|
| 68 |
|
| 69 |
+
|
| 70 |
+
# Simplified Generative Inference
|
| 71 |
+
def simple_generative_inference(image, mode, model, n_iterations=10, step_size=0.01):
|
| 72 |
+
"""
|
| 73 |
+
Perform Generative Perceptual Inference on the input image.
|
| 74 |
+
:param image: Input image as a PIL image or numpy array.
|
| 75 |
+
:param mode: Either 'increase confidence' or 'ReverseDiffuse'.
|
| 76 |
+
:param model: Pretrained PyTorch model.
|
| 77 |
+
:param n_iterations: Number of inference iterations.
|
| 78 |
+
:param step_size: Step size for gradient-based updates.
|
| 79 |
+
:return: Processed image and gradient visualization.
|
| 80 |
+
"""
|
| 81 |
+
# Preprocess image
|
| 82 |
+
transform = transforms.Compose([
|
| 83 |
+
transforms.Resize(224),
|
| 84 |
+
transforms.ToTensor(),
|
| 85 |
+
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) # Adjust normalization as needed
|
| 86 |
+
])
|
| 87 |
+
image_tensor = transform(image).unsqueeze(0).requires_grad_(True)
|
| 88 |
+
|
| 89 |
+
optimizer = torch.optim.SGD([image_tensor], lr=step_size)
|
| 90 |
+
|
| 91 |
+
for _ in range(n_iterations):
|
| 92 |
+
optimizer.zero_grad()
|
| 93 |
+
output = model(image_tensor)
|
| 94 |
+
probs = torch.nn.functional.softmax(output, dim=1)
|
| 95 |
+
|
| 96 |
+
# Define inference loss based on mode
|
| 97 |
+
if mode == "increase confidence":
|
| 98 |
+
# Push away from the least likely classes
|
| 99 |
+
_, least_likely_indices = torch.topk(probs, k=2, largest=False)
|
| 100 |
+
losses = []
|
| 101 |
+
for idx in least_likely_indices[0]:
|
| 102 |
+
target = torch.full((1,), idx, dtype=torch.long, device=output.device)
|
| 103 |
+
loss = torch.nn.CrossEntropyLoss()(output, target)
|
| 104 |
+
losses.append(loss)
|
| 105 |
+
loss = torch.stack(losses).mean() # Average the losses for the least likely classes
|
| 106 |
+
elif mode == "ReverseDiffuse":
|
| 107 |
+
# Push away from noisy versions
|
| 108 |
+
noisy_image = image_tensor + torch.randn_like(image_tensor) * 0.1
|
| 109 |
+
loss = torch.nn.functional.mse_loss(image_tensor, noisy_image)
|
| 110 |
+
else:
|
| 111 |
+
raise ValueError("Invalid mode selected. Choose 'increase confidence' or 'ReverseDiffuse'.")
|
| 112 |
+
|
| 113 |
+
loss.backward()
|
| 114 |
+
optimizer.step()
|
| 115 |
+
|
| 116 |
+
# Generate gradient visualization
|
| 117 |
+
grad = image_tensor.grad.data.abs().mean(dim=1).squeeze().cpu().numpy()
|
| 118 |
+
grad_image = (grad - grad.min()) / (grad.max() - grad.min()) # Normalize to [0, 1]
|
| 119 |
+
grad_image = Image.fromarray((grad_image * 255).astype(np.uint8))
|
| 120 |
+
|
| 121 |
+
# Convert final processed image back to PIL format
|
| 122 |
+
processed_image = image_tensor.detach().squeeze().permute(1, 2, 0).cpu().numpy()
|
| 123 |
+
processed_image = (processed_image - processed_image.min()) / (processed_image.max() - processed_image.min()) # Normalize
|
| 124 |
+
processed_image = Image.fromarray((processed_image * 255).astype(np.uint8))
|
| 125 |
+
|
| 126 |
+
return processed_image, grad_image
|
| 127 |
+
|
| 128 |
+
|
| 129 |
# # Create the Gradio interface
|
| 130 |
# iface = gr.Interface(fn=predict, inputs=gr.Image(type="numpy"), outputs="text") # Updated from gr.inputs.Image to gr.Image
|
| 131 |
|
| 132 |
+
# # Create the Gradio interface
|
| 133 |
+
# iface = gr.Interface(
|
| 134 |
+
# fn=predict,
|
| 135 |
+
# inputs=gr.Image(type="numpy"),
|
| 136 |
+
# outputs=[gr.Textbox(label="Predicted Class"), gr.Gallery(label="Class Samples")],
|
| 137 |
+
# title="ResNet-50 VGGFace2 Classifier"
|
| 138 |
+
# )
|
| 139 |
+
|
| 140 |
iface = gr.Interface(
|
| 141 |
+
fn=lambda image, mode: simple_generative_inference(image, mode, model),
|
| 142 |
+
inputs=[
|
| 143 |
+
gr.Image(type="pil"), # Input image
|
| 144 |
+
gr.Radio(["increase confidence", "ReverseDiffuse"], label="GPI Mode") # Mode selection
|
| 145 |
+
],
|
| 146 |
+
outputs=[
|
| 147 |
+
gr.Image(label="Processed Image"), # Processed image
|
| 148 |
+
gr.Image(label="Gradient Visualization") # Gradient visualization
|
| 149 |
+
],
|
| 150 |
+
title="Generative Perceptual Inference (GPI)"
|
| 151 |
)
|
| 152 |
|
| 153 |
+
|
| 154 |
iface.launch()
|