Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -266,20 +266,93 @@
|
|
| 266 |
# demo.launch()
|
| 267 |
|
| 268 |
|
| 269 |
-
#############6th##################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
import torch
|
|
|
|
| 271 |
import gradio as gr
|
| 272 |
import requests
|
| 273 |
import os
|
|
|
|
| 274 |
import numpy as np
|
|
|
|
| 275 |
|
| 276 |
# Hugging Face Model Repository
|
| 277 |
model_repo = "Mariam-Elz/CRM"
|
| 278 |
|
| 279 |
-
#
|
| 280 |
model_path = "models/CRM.pth"
|
| 281 |
os.makedirs("models", exist_ok=True)
|
| 282 |
|
|
|
|
| 283 |
if not os.path.exists(model_path):
|
| 284 |
url = f"https://huggingface.co/{model_repo}/resolve/main/CRM.pth"
|
| 285 |
print(f"Downloading CRM.pth...")
|
|
@@ -287,51 +360,81 @@ if not os.path.exists(model_path):
|
|
| 287 |
with open(model_path, "wb") as f:
|
| 288 |
f.write(response.content)
|
| 289 |
|
| 290 |
-
# Set Device
|
| 291 |
-
device = "cpu"
|
| 292 |
-
|
| 293 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
def load_model():
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
return model
|
| 299 |
|
| 300 |
-
# Load model
|
| 301 |
model = load_model()
|
| 302 |
|
| 303 |
-
# Define Inference Function
|
| 304 |
def infer(image):
|
| 305 |
-
"""Process input image and return a reconstructed
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
|
| 324 |
# Create Gradio UI
|
| 325 |
demo = gr.Interface(
|
| 326 |
fn=infer,
|
| 327 |
-
inputs=gr.Image(type="
|
| 328 |
outputs=gr.Image(type="numpy"),
|
| 329 |
-
title="
|
| 330 |
-
description="Upload an image to get the reconstructed output
|
| 331 |
)
|
| 332 |
|
| 333 |
if __name__ == "__main__":
|
| 334 |
demo.launch()
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
|
|
|
| 266 |
# demo.launch()
|
| 267 |
|
| 268 |
|
| 269 |
+
#############6th-worked-proc##################
|
| 270 |
+
# import torch
|
| 271 |
+
# import gradio as gr
|
| 272 |
+
# import requests
|
| 273 |
+
# import os
|
| 274 |
+
# import numpy as np
|
| 275 |
+
|
| 276 |
+
# # Hugging Face Model Repository
|
| 277 |
+
# model_repo = "Mariam-Elz/CRM"
|
| 278 |
+
|
| 279 |
+
# # Download Model Weights (Only CRM.pth to Save Memory)
|
| 280 |
+
# model_path = "models/CRM.pth"
|
| 281 |
+
# os.makedirs("models", exist_ok=True)
|
| 282 |
+
|
| 283 |
+
# if not os.path.exists(model_path):
|
| 284 |
+
# url = f"https://huggingface.co/{model_repo}/resolve/main/CRM.pth"
|
| 285 |
+
# print(f"Downloading CRM.pth...")
|
| 286 |
+
# response = requests.get(url)
|
| 287 |
+
# with open(model_path, "wb") as f:
|
| 288 |
+
# f.write(response.content)
|
| 289 |
+
|
| 290 |
+
# # Set Device (Use CPU to Reduce RAM Usage)
|
| 291 |
+
# device = "cpu"
|
| 292 |
+
|
| 293 |
+
# # Load Model Efficiently
|
| 294 |
+
# def load_model():
|
| 295 |
+
# model = torch.load(model_path, map_location=device)
|
| 296 |
+
# if isinstance(model, torch.nn.Module):
|
| 297 |
+
# model.eval() # Ensure model is in inference mode
|
| 298 |
+
# return model
|
| 299 |
+
|
| 300 |
+
# # Load model only when needed (saves memory)
|
| 301 |
+
# model = load_model()
|
| 302 |
+
|
| 303 |
+
# # Define Inference Function with Memory Optimizations
|
| 304 |
+
# def infer(image):
|
| 305 |
+
# """Process input image and return a reconstructed image."""
|
| 306 |
+
# with torch.no_grad():
|
| 307 |
+
# # Convert image to torch tensor & normalize (float16 to save RAM)
|
| 308 |
+
# image_tensor = torch.tensor(image, dtype=torch.float16).unsqueeze(0).permute(0, 3, 1, 2) / 255.0
|
| 309 |
+
# image_tensor = image_tensor.to(device)
|
| 310 |
+
|
| 311 |
+
# # Model Inference
|
| 312 |
+
# output = model(image_tensor)
|
| 313 |
+
|
| 314 |
+
# # Convert back to numpy image format
|
| 315 |
+
# output_image = output.squeeze(0).permute(1, 2, 0).cpu().numpy() * 255.0
|
| 316 |
+
# output_image = np.clip(output_image, 0, 255).astype(np.uint8)
|
| 317 |
+
|
| 318 |
+
# # Free Memory
|
| 319 |
+
# del image_tensor, output
|
| 320 |
+
# torch.cuda.empty_cache()
|
| 321 |
+
|
| 322 |
+
# return output_image
|
| 323 |
+
|
| 324 |
+
# # Create Gradio UI
|
| 325 |
+
# demo = gr.Interface(
|
| 326 |
+
# fn=infer,
|
| 327 |
+
# inputs=gr.Image(type="numpy"),
|
| 328 |
+
# outputs=gr.Image(type="numpy"),
|
| 329 |
+
# title="Optimized Convolutional Reconstruction Model",
|
| 330 |
+
# description="Upload an image to get the reconstructed output with reduced memory usage."
|
| 331 |
+
# )
|
| 332 |
+
|
| 333 |
+
# if __name__ == "__main__":
|
| 334 |
+
# demo.launch()
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
#############7tth################
|
| 339 |
import torch
|
| 340 |
+
import torch.nn as nn
|
| 341 |
import gradio as gr
|
| 342 |
import requests
|
| 343 |
import os
|
| 344 |
+
import torchvision.transforms as transforms
|
| 345 |
import numpy as np
|
| 346 |
+
from PIL import Image
|
| 347 |
|
| 348 |
# Hugging Face Model Repository
|
| 349 |
model_repo = "Mariam-Elz/CRM"
|
| 350 |
|
| 351 |
+
# Model File Path
|
| 352 |
model_path = "models/CRM.pth"
|
| 353 |
os.makedirs("models", exist_ok=True)
|
| 354 |
|
| 355 |
+
# Download model weights if not present
|
| 356 |
if not os.path.exists(model_path):
|
| 357 |
url = f"https://huggingface.co/{model_repo}/resolve/main/CRM.pth"
|
| 358 |
print(f"Downloading CRM.pth...")
|
|
|
|
| 360 |
with open(model_path, "wb") as f:
|
| 361 |
f.write(response.content)
|
| 362 |
|
| 363 |
+
# Set Device
|
| 364 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 365 |
+
|
| 366 |
+
# Define Model Architecture (Replace with your actual model)
|
| 367 |
+
class CRMModel(nn.Module):
|
| 368 |
+
def __init__(self):
|
| 369 |
+
super(CRMModel, self).__init__()
|
| 370 |
+
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, padding=1)
|
| 371 |
+
self.conv2 = nn.Conv2d(64, 64, kernel_size=3, padding=1)
|
| 372 |
+
self.relu = nn.ReLU()
|
| 373 |
+
|
| 374 |
+
def forward(self, x):
|
| 375 |
+
x = self.relu(self.conv1(x))
|
| 376 |
+
x = self.relu(self.conv2(x))
|
| 377 |
+
return x
|
| 378 |
+
|
| 379 |
+
# Load Model
|
| 380 |
def load_model():
|
| 381 |
+
print("Loading model...")
|
| 382 |
+
model = CRMModel() # Use the correct architecture here
|
| 383 |
+
state_dict = torch.load(model_path, map_location=device)
|
| 384 |
+
|
| 385 |
+
if isinstance(state_dict, dict): # Ensure it's a valid state_dict
|
| 386 |
+
model.load_state_dict(state_dict)
|
| 387 |
+
else:
|
| 388 |
+
raise ValueError("Error: The loaded state_dict is not in the correct format.")
|
| 389 |
+
|
| 390 |
+
model.to(device)
|
| 391 |
+
model.eval()
|
| 392 |
+
print("Model loaded successfully!")
|
| 393 |
return model
|
| 394 |
|
| 395 |
+
# Load the model
|
| 396 |
model = load_model()
|
| 397 |
|
| 398 |
+
# Define Inference Function
|
| 399 |
def infer(image):
|
| 400 |
+
"""Process input image and return a reconstructed 3D output."""
|
| 401 |
+
try:
|
| 402 |
+
print("Preprocessing image...")
|
| 403 |
+
|
| 404 |
+
# Convert image to PyTorch tensor & normalize
|
| 405 |
+
transform = transforms.Compose([
|
| 406 |
+
transforms.Resize((256, 256)), # Resize to fit model input
|
| 407 |
+
transforms.ToTensor(), # Converts to tensor (C, H, W)
|
| 408 |
+
transforms.Normalize(mean=[0.5], std=[0.5]), # Normalize
|
| 409 |
+
])
|
| 410 |
+
image_tensor = transform(image).unsqueeze(0).to(device) # Add batch dimension
|
| 411 |
+
|
| 412 |
+
print("Running inference...")
|
| 413 |
+
with torch.no_grad():
|
| 414 |
+
output = model(image_tensor) # Forward pass
|
| 415 |
+
|
| 416 |
+
# Ensure output is a valid tensor
|
| 417 |
+
if isinstance(output, torch.Tensor):
|
| 418 |
+
output_image = output.squeeze(0).permute(1, 2, 0).cpu().numpy()
|
| 419 |
+
output_image = np.clip(output_image * 255.0, 0, 255).astype(np.uint8)
|
| 420 |
+
print("Inference complete! Returning output.")
|
| 421 |
+
return output_image
|
| 422 |
+
else:
|
| 423 |
+
print("Error: Model output is not a tensor.")
|
| 424 |
+
return None
|
| 425 |
+
|
| 426 |
+
except Exception as e:
|
| 427 |
+
print(f"Error during inference: {e}")
|
| 428 |
+
return None
|
| 429 |
|
| 430 |
# Create Gradio UI
|
| 431 |
demo = gr.Interface(
|
| 432 |
fn=infer,
|
| 433 |
+
inputs=gr.Image(type="pil"),
|
| 434 |
outputs=gr.Image(type="numpy"),
|
| 435 |
+
title="Convolutional Reconstruction Model",
|
| 436 |
+
description="Upload an image to get the reconstructed output."
|
| 437 |
)
|
| 438 |
|
| 439 |
if __name__ == "__main__":
|
| 440 |
demo.launch()
|
|
|
|
|
|
|
|
|