Spaces:
Runtime error
Runtime error
Update app_py.py
Browse files
app_py.py
CHANGED
|
@@ -1,44 +1,44 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
"""app.py
|
| 3 |
-
|
| 4 |
-
Automatically generated by Colab.
|
| 5 |
-
|
| 6 |
-
Original file is located at
|
| 7 |
-
https://colab.research.google.com/drive/1Z0eLx3x7YdED4I1Zn3QUCS31C9AzTu8I
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
import subprocess
|
| 11 |
import sys
|
| 12 |
import os
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def install_tensorflow():
|
| 15 |
"""
|
| 16 |
-
Installs TensorFlow using pip
|
|
|
|
| 17 |
"""
|
| 18 |
try:
|
| 19 |
print("Installing TensorFlow...")
|
|
|
|
| 20 |
result = subprocess.run(
|
| 21 |
[sys.executable, "-m", "pip", "install", "tensorflow"],
|
| 22 |
-
check=
|
| 23 |
stdout=subprocess.PIPE,
|
| 24 |
stderr=subprocess.PIPE,
|
| 25 |
text=True,
|
| 26 |
)
|
| 27 |
-
print("
|
| 28 |
print(result.stdout)
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
except Exception as ex:
|
| 39 |
print(f"An unexpected error occurred during TensorFlow installation: {ex}")
|
| 40 |
-
|
| 41 |
-
|
| 42 |
def check_tensorflow_installation():
|
| 43 |
"""
|
| 44 |
Checks if TensorFlow is installed and can be imported.
|
|
@@ -50,13 +50,24 @@ def check_tensorflow_installation():
|
|
| 50 |
print("TensorFlow is installed and ready to use.")
|
| 51 |
return True
|
| 52 |
except ImportError:
|
| 53 |
-
print("TensorFlow is not installed.")
|
| 54 |
-
install_tensorflow()
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
if not check_tensorflow_installation():
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
import tensorflow as tf
|
| 61 |
from tensorflow.keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, MaxPooling2D, AveragePooling2D, Dropout
|
| 62 |
from tensorflow.keras.models import Model
|
|
@@ -64,7 +75,6 @@ from tensorflow.keras.initializers import GlorotUniform
|
|
| 64 |
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
| 65 |
from tensorflow.keras.optimizers import Adam
|
| 66 |
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
|
| 67 |
-
|
| 68 |
# Check if running in Google Colab
|
| 69 |
try:
|
| 70 |
from google.colab import drive
|
|
@@ -77,17 +87,32 @@ except ImportError:
|
|
| 77 |
if IS_COLAB:
|
| 78 |
try:
|
| 79 |
drive.mount("/content/drive", timeout_ms=60000)
|
|
|
|
|
|
|
|
|
|
| 80 |
except Exception as e:
|
| 81 |
print(f"Error mounting drive: {e}")
|
| 82 |
print("Continuing without Google Drive...")
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
| 86 |
else:
|
| 87 |
-
# **
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
if not os.path.exists(train_dir):
|
| 93 |
print(f"Error: Training data directory not found at {train_dir}")
|
|
@@ -340,4 +365,4 @@ except KeyboardInterrupt:
|
|
| 340 |
print("Model saved. You can resume training later by loading this model.")
|
| 341 |
except Exception as e:
|
| 342 |
print(f"An error occurred during training: {e}")
|
| 343 |
-
print("Please check your data and model configuration.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
import sys
|
| 3 |
import os
|
| 4 |
+
import re # Import the regular expression module
|
| 5 |
+
import shutil # Import the shutil module
|
| 6 |
|
| 7 |
def install_tensorflow():
|
| 8 |
"""
|
| 9 |
+
Installs TensorFlow using pip, targeting the *current* Python environment.
|
| 10 |
+
Handles potential errors during the installation. Also checks if the installation was successful.
|
| 11 |
"""
|
| 12 |
try:
|
| 13 |
print("Installing TensorFlow...")
|
| 14 |
+
# Use sys.executable to ensure we're using the correct pip for the current Python interpreter
|
| 15 |
result = subprocess.run(
|
| 16 |
[sys.executable, "-m", "pip", "install", "tensorflow"],
|
| 17 |
+
check=False, # Do NOT use check=True here. We'll handle errors ourselves.
|
| 18 |
stdout=subprocess.PIPE,
|
| 19 |
stderr=subprocess.PIPE,
|
| 20 |
text=True,
|
| 21 |
)
|
| 22 |
+
print("pip install output (stdout):")
|
| 23 |
print(result.stdout)
|
| 24 |
+
print("pip install error output (stderr):")
|
| 25 |
+
print(result.stderr)
|
| 26 |
+
|
| 27 |
+
# Check for successful installation using regex
|
| 28 |
+
if re.search(r"Successfully installed tensorflow", result.stdout):
|
| 29 |
+
print("TensorFlow installation confirmed.")
|
| 30 |
+
return True # Indicate successful installation
|
| 31 |
+
else:
|
| 32 |
+
print(
|
| 33 |
+
"Warning: TensorFlow installation may not have been fully successful. "
|
| 34 |
+
"Please check the output above for any errors."
|
| 35 |
+
)
|
| 36 |
+
return False
|
| 37 |
|
| 38 |
except Exception as ex:
|
| 39 |
print(f"An unexpected error occurred during TensorFlow installation: {ex}")
|
| 40 |
+
return False # Indicate installation failure
|
| 41 |
+
|
| 42 |
def check_tensorflow_installation():
|
| 43 |
"""
|
| 44 |
Checks if TensorFlow is installed and can be imported.
|
|
|
|
| 50 |
print("TensorFlow is installed and ready to use.")
|
| 51 |
return True
|
| 52 |
except ImportError:
|
| 53 |
+
print("TensorFlow is not installed. Attempting to install...")
|
| 54 |
+
if install_tensorflow(): # Call install_tensorflow and check its return value
|
| 55 |
+
try:
|
| 56 |
+
import tensorflow as tf
|
| 57 |
+
print(f"TensorFlow version: {tf.__version__}")
|
| 58 |
+
print("TensorFlow is now installed and ready to use.")
|
| 59 |
+
return True
|
| 60 |
+
except ImportError:
|
| 61 |
+
print("TensorFlow installation failed. ImportError after installation.")
|
| 62 |
+
return False
|
| 63 |
+
else:
|
| 64 |
+
print("TensorFlow installation failed.")
|
| 65 |
+
return False
|
| 66 |
|
| 67 |
if not check_tensorflow_installation():
|
| 68 |
+
print("TensorFlow installation failed. Exiting.")
|
| 69 |
+
sys.exit(1)
|
| 70 |
+
|
| 71 |
import tensorflow as tf
|
| 72 |
from tensorflow.keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, MaxPooling2D, AveragePooling2D, Dropout
|
| 73 |
from tensorflow.keras.models import Model
|
|
|
|
| 75 |
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
| 76 |
from tensorflow.keras.optimizers import Adam
|
| 77 |
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
|
|
|
|
| 78 |
# Check if running in Google Colab
|
| 79 |
try:
|
| 80 |
from google.colab import drive
|
|
|
|
| 87 |
if IS_COLAB:
|
| 88 |
try:
|
| 89 |
drive.mount("/content/drive", timeout_ms=60000)
|
| 90 |
+
train_dir = "/content/drive/MyDrive/Periodontal_lessDataSet/train"
|
| 91 |
+
val_dir = "/content/drive/MyDrive/Periodontal_lessDataSet/val"
|
| 92 |
+
test_dir = "/content/drive/MyDrive/Periodontal_lessDataSet/test"
|
| 93 |
except Exception as e:
|
| 94 |
print(f"Error mounting drive: {e}")
|
| 95 |
print("Continuing without Google Drive...")
|
| 96 |
+
print("Please ensure your data is in the correct local paths.")
|
| 97 |
+
train_dir = "train" # These should be set even if the drive fails.
|
| 98 |
+
val_dir = "val"
|
| 99 |
+
test_dir = "test"
|
| 100 |
+
|
| 101 |
else:
|
| 102 |
+
# **CRITICAL: Explicitly define the paths RELATIVE to where you run the script**
|
| 103 |
+
# Get the directory where the script is located.
|
| 104 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 105 |
+
|
| 106 |
+
# Construct the full paths. This is the most reliable way to do it.
|
| 107 |
+
train_dir = os.path.join(script_dir, "train")
|
| 108 |
+
val_dir = os.path.join(script_dir, "val")
|
| 109 |
+
test_dir = os.path.join(script_dir, "test")
|
| 110 |
+
|
| 111 |
+
# ***VERY IMPORTANT DEBUGGING OUTPUT***
|
| 112 |
+
print(f"Script directory: {script_dir}")
|
| 113 |
+
print(f"Calculated train_dir: {train_dir}")
|
| 114 |
+
print(f"Calculated val_dir: {val_dir}")
|
| 115 |
+
print(f"Calculated test_dir: {test_dir}")
|
| 116 |
|
| 117 |
if not os.path.exists(train_dir):
|
| 118 |
print(f"Error: Training data directory not found at {train_dir}")
|
|
|
|
| 365 |
print("Model saved. You can resume training later by loading this model.")
|
| 366 |
except Exception as e:
|
| 367 |
print(f"An error occurred during training: {e}")
|
| 368 |
+
print("Please check your data and model configuration.")
|