Mohaddz commited on
Commit
22f62c8
1 Parent(s): 75e415a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -16
app.py CHANGED
@@ -6,25 +6,63 @@ import tensorflow as tf
6
  from transformers import SegformerForSemanticSegmentation, AutoFeatureExtractor
7
  import cv2
8
  import json
 
9
 
10
  # Load models
11
  part_seg_model = SegformerForSemanticSegmentation.from_pretrained("Mohaddz/huggingCars")
12
  damage_seg_model = SegformerForSemanticSegmentation.from_pretrained("Mohaddz/DamageSeg")
13
  feature_extractor = AutoFeatureExtractor.from_pretrained("Mohaddz/huggingCars")
14
 
15
- # Recreate the model architecture
16
- def create_model(input_shape, num_classes):
17
- inputs = tf.keras.Input(shape=input_shape)
18
- x = tf.keras.layers.Dense(64, activation='relu')(inputs)
19
- x = tf.keras.layers.Dense(32, activation='relu')(x)
20
- outputs = tf.keras.layers.Dense(num_classes, activation='sigmoid')(x)
21
- return tf.keras.Model(inputs=inputs, outputs=outputs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # Load model weights
24
- input_shape = 33 # Adjust this based on your actual input shape
25
- num_classes = 29 # Adjust this based on your actual number of classes
26
- dl_model = create_model(input_shape, num_classes)
27
- dl_model.load_weights('improved_car_damage_prediction_model.h5')
 
 
 
28
 
29
  # Load parts list
30
  with open('cars117.json', 'r', encoding='utf-8') as f:
@@ -67,14 +105,18 @@ def process_image(image):
67
  input_vector = np.concatenate([part_features.mean(axis=(1, 2)), damage_features.mean(axis=(1, 2))])
68
 
69
  # Predict parts to replace using the loaded model
70
- prediction = dl_model.predict(np.array([input_vector]))
71
- predicted_parts = [(all_parts[i], float(prob)) for i, prob in enumerate(prediction[0]) if prob > 0.1]
72
- predicted_parts.sort(key=lambda x: x[1], reverse=True)
 
 
 
 
73
 
74
  return (Image.fromarray(annotated_image),
75
  Image.fromarray(damage_heatmap_resized),
76
  Image.fromarray(part_heatmap_resized),
77
- "\n".join([f"{part}: {prob:.2f}" for part, prob in predicted_parts[:5]]))
78
 
79
  def create_heatmap(features):
80
  heatmap = np.sum(features, axis=0)
 
6
  from transformers import SegformerForSemanticSegmentation, AutoFeatureExtractor
7
  import cv2
8
  import json
9
+ import os
10
 
11
  # Load models
12
  part_seg_model = SegformerForSemanticSegmentation.from_pretrained("Mohaddz/huggingCars")
13
  damage_seg_model = SegformerForSemanticSegmentation.from_pretrained("Mohaddz/DamageSeg")
14
  feature_extractor = AutoFeatureExtractor.from_pretrained("Mohaddz/huggingCars")
15
 
16
+ # Attempt to load the model
17
+ def load_model(model_path):
18
+ print(f"Attempting to load model from: {model_path}")
19
+ print(f"Current working directory: {os.getcwd()}")
20
+ print(f"Files in current directory: {os.listdir('.')}")
21
+
22
+ try:
23
+ # Attempt 1: Load the entire model
24
+ model = tf.keras.models.load_model(model_path)
25
+ print("Successfully loaded the entire model.")
26
+ return model
27
+ except Exception as e:
28
+ print(f"Failed to load entire model. Error: {str(e)}")
29
+
30
+ try:
31
+ # Attempt 2: Load model architecture from JSON and weights separately
32
+ with open(model_path.replace('.h5', '.json'), 'r') as json_file:
33
+ model_json = json_file.read()
34
+ model = tf.keras.models.model_from_json(model_json)
35
+ model.load_weights(model_path)
36
+ print("Successfully loaded model from JSON and weights.")
37
+ return model
38
+ except Exception as e:
39
+ print(f"Failed to load model from JSON and weights. Error: {str(e)}")
40
+
41
+ try:
42
+ # Attempt 3: Load only the weights into a predefined architecture
43
+ input_shape = 33 # Adjust if necessary
44
+ num_classes = 29 # Adjust if necessary
45
+ inputs = tf.keras.Input(shape=(input_shape,))
46
+ x = tf.keras.layers.Dense(256, activation='relu')(inputs)
47
+ x = tf.keras.layers.Dense(128, activation='relu')(x)
48
+ x = tf.keras.layers.Dense(64, activation='relu')(x)
49
+ outputs = tf.keras.layers.Dense(num_classes, activation='sigmoid')(x)
50
+ model = tf.keras.Model(inputs=inputs, outputs=outputs)
51
+ model.load_weights(model_path)
52
+ print("Successfully loaded weights into predefined architecture.")
53
+ return model
54
+ except Exception as e:
55
+ print(f"Failed to load weights into predefined architecture. Error: {str(e)}")
56
+ raise Exception("All attempts to load the model failed.")
57
 
58
+ # Try to load the model
59
+ try:
60
+ dl_model = load_model('improved_car_damage_prediction_model.h5')
61
+ print("Model loaded successfully.")
62
+ dl_model.summary()
63
+ except Exception as e:
64
+ print(f"Failed to load the model: {str(e)}")
65
+ dl_model = None
66
 
67
  # Load parts list
68
  with open('cars117.json', 'r', encoding='utf-8') as f:
 
105
  input_vector = np.concatenate([part_features.mean(axis=(1, 2)), damage_features.mean(axis=(1, 2))])
106
 
107
  # Predict parts to replace using the loaded model
108
+ if dl_model is not None:
109
+ prediction = dl_model.predict(np.array([input_vector]))
110
+ predicted_parts = [(all_parts[i], float(prob)) for i, prob in enumerate(prediction[0]) if prob > 0.1]
111
+ predicted_parts.sort(key=lambda x: x[1], reverse=True)
112
+ prediction_text = "\n".join([f"{part}: {prob:.2f}" for part, prob in predicted_parts[:5]])
113
+ else:
114
+ prediction_text = "Model failed to load. Unable to make predictions."
115
 
116
  return (Image.fromarray(annotated_image),
117
  Image.fromarray(damage_heatmap_resized),
118
  Image.fromarray(part_heatmap_resized),
119
+ prediction_text)
120
 
121
  def create_heatmap(features):
122
  heatmap = np.sum(features, axis=0)