AshanGimhana
commited on
Commit
•
ff4d438
1
Parent(s):
47ef636
Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,21 @@ print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
|
|
13 |
|
14 |
###################################################
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
from argparse import Namespace
|
18 |
import pprint
|
@@ -41,13 +56,14 @@ login(token=os.getenv("TOKENKEY"))
|
|
41 |
|
42 |
# If 'mse' is a custom function needed,
|
43 |
#custom_objects = {'mse': MeanSquaredError()}
|
44 |
-
|
45 |
########################################################################
|
46 |
|
47 |
|
48 |
########################################################################
|
49 |
############## pytorch model for age calculation #######################
|
50 |
-
|
|
|
51 |
|
52 |
########################################################################
|
53 |
|
@@ -140,35 +156,35 @@ def get_mouth_region(image):
|
|
140 |
|
141 |
# old tensorflow function for age predict
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
|
149 |
##### Predict age
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
|
154 |
-
def predict_age(image):
|
155 |
-
age_calc_model.eval()
|
156 |
-
|
157 |
-
image = cv2.imread(image, cv2.IMREAD_GRAYSCALE) # Load as grayscale
|
158 |
-
image = cv2.resize(image, (64, 64)) # Resize to 64x64
|
159 |
-
image = image / 255.0 # Normalize pixel values to [0, 1]
|
160 |
-
image = np.expand_dims(image, axis=0) # Add batch dimension
|
161 |
-
image = np.expand_dims(image, axis=0) # Add channel dimension
|
162 |
-
image = torch.tensor(image, dtype=torch.float32).to(device)
|
163 |
|
164 |
# Convert to tensor
|
165 |
-
image_tensor = torch.tensor(image, dtype=torch.float32)
|
166 |
|
167 |
-
|
168 |
-
with torch.no_grad():
|
169 |
-
predicted_age = age_calc_model(image_tensor)
|
170 |
|
171 |
-
return int(predicted_age.item())
|
172 |
|
173 |
# Function for color correction
|
174 |
def color_correct(source, target):
|
|
|
13 |
|
14 |
###################################################
|
15 |
|
16 |
+
gpus = tf.config.list_physical_devices('GPU')
|
17 |
+
print("Available GPUs TF:", gpus)
|
18 |
+
|
19 |
+
if gpus:
|
20 |
+
try:
|
21 |
+
# Allow TensorFlow to allocate memory as needed
|
22 |
+
for gpu in gpus:
|
23 |
+
tf.config.experimental.set_memory_growth(gpu, True)
|
24 |
+
except RuntimeError as e:
|
25 |
+
print(e) # Print error if unable to set up GPU
|
26 |
+
else:
|
27 |
+
print("No GPUs available.")
|
28 |
+
|
29 |
+
###################################################
|
30 |
+
|
31 |
|
32 |
from argparse import Namespace
|
33 |
import pprint
|
|
|
56 |
|
57 |
# If 'mse' is a custom function needed,
|
58 |
#custom_objects = {'mse': MeanSquaredError()}
|
59 |
+
new_age_model = load_model("age_prediction_model.h5")
|
60 |
########################################################################
|
61 |
|
62 |
|
63 |
########################################################################
|
64 |
############## pytorch model for age calculation #######################
|
65 |
+
|
66 |
+
#age_calc_model = torch.load('Custom_Age_prediction_model.pth')
|
67 |
|
68 |
########################################################################
|
69 |
|
|
|
156 |
|
157 |
# old tensorflow function for age predict
|
158 |
|
159 |
+
def predict_age(image):
|
160 |
+
image = np.array(image.resize((64, 64)))
|
161 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
162 |
+
image = image / 255.0
|
163 |
+
image = np.expand_dims(image, axis=0)
|
164 |
|
165 |
##### Predict age
|
166 |
+
val = new_age_model.predict(np.array(image))
|
167 |
+
age = val[0][0]
|
168 |
+
return int(age)
|
169 |
|
170 |
+
#def predict_age(image):
|
171 |
+
#age_calc_model.eval()
|
172 |
+
##### Load and preprocess the image
|
173 |
+
#image = cv2.imread(image, cv2.IMREAD_GRAYSCALE) # Load as grayscale
|
174 |
+
#image = cv2.resize(image, (64, 64)) # Resize to 64x64
|
175 |
+
#image = image / 255.0 # Normalize pixel values to [0, 1]
|
176 |
+
#image = np.expand_dims(image, axis=0) # Add batch dimension
|
177 |
+
#image = np.expand_dims(image, axis=0) # Add channel dimension
|
178 |
+
#image = torch.tensor(image, dtype=torch.float32).to(device)
|
179 |
|
180 |
# Convert to tensor
|
181 |
+
#image_tensor = torch.tensor(image, dtype=torch.float32)
|
182 |
|
183 |
+
#### Predict age
|
184 |
+
#with torch.no_grad():
|
185 |
+
#predicted_age = age_calc_model(image_tensor)
|
186 |
|
187 |
+
#return int(predicted_age.item())
|
188 |
|
189 |
# Function for color correction
|
190 |
def color_correct(source, target):
|