jiten6555 commited on
Commit
7b46165
·
verified ·
1 Parent(s): 0e81960

Changing ai model

Browse files
Files changed (1) hide show
  1. app.py +15 -34
app.py CHANGED
@@ -7,6 +7,7 @@ from PIL import Image
7
  import cv2
8
  import uuid
9
  import gc # Garbage collection
 
10
 
11
  class RobustDepthTo3DConverter:
12
  def __init__(self):
@@ -14,32 +15,20 @@ class RobustDepthTo3DConverter:
14
  self.device = torch.device("cpu")
15
 
16
  try:
17
- # Use the smallest available MiDaS model for less memory usage
18
- self.model = torch.hub.load("intel-isl/MiDaS", "MiDaS_small", pretrained=True, force_reload=False)
19
-
20
- # Move model to device and set to eval mode
21
- self.model.to(self.device)
22
  self.model.eval()
23
 
24
- print("MiDaS model successfully initialized")
25
 
26
  except Exception as e:
27
  print(f"Critical model initialization error: {e}")
28
  self.model = None
29
 
30
- # Create transformation pipeline with smaller input size
31
- self.transform = transforms.Compose([
32
- transforms.Resize((192, 192)), # Reduced input size
33
- transforms.ToTensor(),
34
- transforms.Normalize(
35
- mean=[0.485, 0.456, 0.406],
36
- std=[0.229, 0.224, 0.225]
37
- )
38
- ])
39
-
40
  def preprocess_image(self, input_image):
41
  """
42
- Standardize image input with more robust preprocessing
43
  """
44
  # Ensure input is PIL Image
45
  if not isinstance(input_image, Image.Image):
@@ -53,27 +42,20 @@ class RobustDepthTo3DConverter:
53
 
54
  def estimate_depth(self, input_image):
55
  """
56
- More robust depth estimation with memory optimization
57
  """
58
  if self.model is None:
59
- raise ValueError("MiDaS model not properly initialized. Check model loading.")
60
 
61
  try:
62
- # Preprocess image
63
  img = self.preprocess_image(input_image)
64
-
65
- # Convert to tensor
66
- img_tensor = self.transform(img).unsqueeze(0).to(self.device)
67
 
68
  # Estimate depth
69
  with torch.no_grad():
70
- prediction = self.model(img_tensor)
71
-
72
- # Handle different model output formats
73
- if isinstance(prediction, tuple):
74
- depth = prediction[0].squeeze().cpu().numpy()
75
- else:
76
- depth = prediction.squeeze().cpu().numpy()
77
 
78
  # Normalize depth
79
  depth_normalized = cv2.normalize(
@@ -83,7 +65,6 @@ class RobustDepthTo3DConverter:
83
  )
84
 
85
  # Manual memory cleanup
86
- del img_tensor
87
  torch.cuda.empty_cache()
88
  gc.collect()
89
 
@@ -168,7 +149,7 @@ class RobustDepthTo3DConverter:
168
  """
169
  # First, check if model is initialized
170
  if self.model is None:
171
- raise ValueError("MiDaS model initialization failed. Cannot process image.")
172
 
173
  try:
174
  # Preprocess and validate input
@@ -211,7 +192,7 @@ def create_huggingface_space():
211
  try:
212
  # Check model initialization before processing
213
  if converter.model is None:
214
- raise ValueError("MiDaS model failed to initialize. Cannot process image.")
215
 
216
  output_model = converter.process_image(input_image)
217
  return output_model
@@ -232,4 +213,4 @@ def create_huggingface_space():
232
 
233
  # Launch the Gradio interface
234
  demo = create_huggingface_space()
235
- demo.launch(debug=True)
 
7
  import cv2
8
  import uuid
9
  import gc # Garbage collection
10
+ from transformers import DPTForDepthEstimation, DPTFeatureExtractor
11
 
12
  class RobustDepthTo3DConverter:
13
  def __init__(self):
 
15
  self.device = torch.device("cpu")
16
 
17
  try:
18
+ # Load Hugging Face DPT model and feature extractor
19
+ self.model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large").to(self.device)
20
+ self.feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
 
 
21
  self.model.eval()
22
 
23
+ print("DPT model successfully initialized")
24
 
25
  except Exception as e:
26
  print(f"Critical model initialization error: {e}")
27
  self.model = None
28
 
 
 
 
 
 
 
 
 
 
 
29
  def preprocess_image(self, input_image):
30
  """
31
+ Preprocess image using Hugging Face's feature extractor
32
  """
33
  # Ensure input is PIL Image
34
  if not isinstance(input_image, Image.Image):
 
42
 
43
  def estimate_depth(self, input_image):
44
  """
45
+ Estimate depth using Hugging Face DPT model
46
  """
47
  if self.model is None:
48
+ raise ValueError("DPT model not properly initialized. Check model loading.")
49
 
50
  try:
51
+ # Preprocess the image
52
  img = self.preprocess_image(input_image)
53
+ inputs = self.feature_extractor(images=img, return_tensors="pt").to(self.device)
 
 
54
 
55
  # Estimate depth
56
  with torch.no_grad():
57
+ outputs = self.model(**inputs)
58
+ depth = outputs.predicted_depth.squeeze().cpu().numpy()
 
 
 
 
 
59
 
60
  # Normalize depth
61
  depth_normalized = cv2.normalize(
 
65
  )
66
 
67
  # Manual memory cleanup
 
68
  torch.cuda.empty_cache()
69
  gc.collect()
70
 
 
149
  """
150
  # First, check if model is initialized
151
  if self.model is None:
152
+ raise ValueError("DPT model initialization failed. Cannot process image.")
153
 
154
  try:
155
  # Preprocess and validate input
 
192
  try:
193
  # Check model initialization before processing
194
  if converter.model is None:
195
+ raise ValueError("DPT model failed to initialize. Cannot process image.")
196
 
197
  output_model = converter.process_image(input_image)
198
  return output_model
 
213
 
214
  # Launch the Gradio interface
215
  demo = create_huggingface_space()
216
+ demo.launch(debug=True)