{ "cells": [ { "cell_type": "code", "execution_count": 4, "id": "ea9b6ff2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "==================================================\n", "๐Ÿ” TESTING NITROSENSE AI APPLICATION\n", "==================================================\n", "\n", "๐Ÿ“ Test 1: Checking folder structure...\n", " โœ… models folder exists\n", " โœ… utils folder exists\n", " โœ… templates folder exists\n", " โœ… static folder exists\n", " โœ… u2net folder exists\n", " โœ… u2net/weights folder exists\n", " โœ… u2net/model folder exists\n", " โœ… static/uploads folder exists\n", "\n", "๐Ÿ“„ Test 2: Checking required files...\n", " โœ… models\\Pyramid_fusion_densenet121_model.h5 exists (59.70 MB)\n", " โœ… utils\\background_removal.py exists\n", " โœ… utils\\edge_detection.py exists\n", " โœ… utils\\preprocessing.py exists\n", " โœ… utils/scalers\\scaler_y.pkl exists\n", " โœ… utils/scalers\\continuous_scaler.pkl exists\n", " โœ… utils/scalers\\days_scaler.pkl exists\n", " โœ… utils/scalers\\nitrogen_map.pkl exists\n", " โœ… utils\\temperature_means.pkl exists\n", " โœ… templates\\index.html exists\n", " โœ… templates\\estimation.html exists\n", " โœ… templates\\about.html exists\n", " โœ… templates\\team.html exists\n", " โœ… templates\\how-it-works.html exists\n", " โœ… templates\\contact.html exists\n", " โœ… templates\\capture.html exists\n", " โœ… templates\\result.html exists\n", " โœ… u2net/weights\\u2net.pth exists (168.12 MB)\n", "\n", "๐Ÿ”ง Test 3: Testing utility imports...\n", " โœ… BackgroundRemover imported\n", " โœ… EdgeDetector imported\n", " โœ… ImagePreprocessor imported\n", "\n", "๐ŸŒ„ Test 4: Testing BackgroundRemover initialization...\n", "โœ… U2Net source already present\n", "โœ… BackgroundRemover initialized with U2Net model\n", " โœ… BackgroundRemover initialized successfully\n", " โœ… Background removal works - output shape: (224, 224, 3)\n", " โœ… Mask generated - shape: (224, 224)\n", "\n", "๐Ÿ–ผ๏ธ Test 5: Testing preprocessing pipeline...\n", " โœ… Edge detection works - output shape: (100, 100, 3)\n", " โœ… Preprocessor works with target_size=(224, 224) - output shape: (224, 224, 3)\n", " โœ… Output range: [1.00, 1.00]\n", "\n", "๐Ÿค– Test 6: Checking model file...\n", " โœ… Model file found: models\\Pyramid_fusion_densenet121_model.h5\n", " ๐Ÿ“Š File size: 59.70 MB\n", " โœ… Model file is valid HDF5 format\n", "\n", "๐Ÿ“Š Test 7: Checking scaler files...\n", " โœ… Target scaler loaded successfully\n", " โœ… Temperature scaler loaded successfully\n", " โœ… Days scaler loaded successfully\n", " โœ… Nitrogen map loaded successfully\n", " โœ… Temperature means loaded successfully\n", "\n", "โš™๏ธ Test 8: Checking Flask app syntax...\n", " โœ… Flask app syntax is valid\n", "\n", "๐Ÿ“ Test 9: Checking template files...\n", " โ„น๏ธ index.html appears to be a static page (no Jinja syntax)\n", " โ„น๏ธ estimation.html appears to be a static page (no Jinja syntax)\n", " โ„น๏ธ about.html appears to be a static page (no Jinja syntax)\n", " โ„น๏ธ team.html appears to be a static page (no Jinja syntax)\n", " โ„น๏ธ how-it-works.html appears to be a static page (no Jinja syntax)\n", " โ„น๏ธ contact.html appears to be a static page (no Jinja syntax)\n", " โ„น๏ธ capture.html appears to be a static page (no Jinja syntax)\n", " โœ… result.html contains Jinja syntax\n", "\n", "๐Ÿ”ง Test 10: Checking U2Net weights...\n", " โœ… U2Net weights found: u2net\\weights\\u2net.pth\n", " ๐Ÿ“Š File size: 168.12 MB\n", "\n", "==================================================\n", "๐Ÿ“Š TEST SUMMARY\n", "==================================================\n", "\n", "โœ… All folders and files are in place!\n", "\n", "๐ŸŽ‰ Your NitroSense AI application is ready to run!\n", "\n", "โœ… All critical components present!\n", "\n", "==================================================\n", "๐Ÿš€ TO RUN THE APPLICATION:\n", "==================================================\n", "\n", "Open a terminal/command prompt and run:\n", " cd C:\\Users\\HP\\NitroSense-AI\n", " python app.py\n", "\n", "Then open your browser and go to:\n", " http://localhost:5000\n", " http://192.168.175.5:5000 (for other devices on same network)\n", "\n", "==================================================\n", "\n", "==================================================\n", "๐Ÿงช TESTING MODEL PREDICTION WITH DUMMY DATA\n", "==================================================\n", "Loading model...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " File \"app.py\", line 81\n", " print(\"\n", " ^\n", "SyntaxError: unterminated string literal (detected at line 81)\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "โœ… Model loaded successfully\n", "\n", "๐Ÿ“Š Model input shapes:\n", " Input 1: image_input - Shape: (None, 224, 224, 3)\n", " Input 2: tabular_input - Shape: (None, 4)\n", "\n", "๐Ÿ”„ Creating dummy test data with size 224x224...\n", " Dummy image shape: (1, 224, 224, 3)\n", " Dummy features shape: (1, 4)\n", " Dummy features: [0.7880997 0.7509771 0.22307786 0.90968657]\n", "\n", "๐Ÿ”„ Making prediction...\n", "โœ… Model prediction successful: -1.375976\n", " Prediction shape: (1, 1)\n", "\n", "๐Ÿ”„ Testing with different feature values:\n", " Test 1 - Features: [0.1 0.1 0. 0.1] โ†’ Prediction: -1.174216\n", " Test 2 - Features: [0.5 0.5 3. 0.5] โ†’ Prediction: -0.676204\n", " Test 3 - Features: [0.9 0.9 7. 0.9] โ†’ Prediction: -0.284522\n", "\n", "๐Ÿ” Testing prediction variation with different features:\n", " Features [0. 0. 0. 0.] โ†’ -1.143696\n", " Features [0.2 0.2 1. 0.2] โ†’ -1.029661\n", " Features [0.4 0.4 3. 0.4] โ†’ -0.645686\n", " Features [0.6 0.6 5. 0.6] โ†’ -0.089085\n", " Features [0.8 0.8 6. 0.8] โ†’ -0.237164\n", " Features [1. 1. 7. 1.] โ†’ -0.312324\n", "\n", "โœ… Model produces varying outputs for different inputs - good!\n", "\n", "๐Ÿ“Š Testing denormalization with scaler_y:\n", " Scaler_y mean: 3.6249, scale: 0.7252\n", "\n", "๐Ÿ“Š Denormalized values (should be in range 2-5%):\n", " Normalized: -1.143696 โ†’ Original Nitrogen: 2.80%\n", " Normalized: -1.029661 โ†’ Original Nitrogen: 2.88%\n", " Normalized: -0.645686 โ†’ Original Nitrogen: 3.16%\n", " Normalized: -0.089085 โ†’ Original Nitrogen: 3.56%\n", " Normalized: -0.237164 โ†’ Original Nitrogen: 3.45%\n", " Normalized: -0.312324 โ†’ Original Nitrogen: 3.40%\n", "\n", "๐Ÿ“Š Note: After denormalization, values should be in the range 2-5%\n" ] } ], "source": [ "# %% [markdown]\n", "# # Step 8: Test Complete Application\n", "# ## Verify all components are working\n", "\n", "# %%\n", "import os\n", "import sys\n", "import importlib\n", "import numpy as np\n", "import traceback\n", "\n", "# %%\n", "print(\"=\"*50)\n", "print(\"๐Ÿ” TESTING NITROSENSE AI APPLICATION\")\n", "print(\"=\"*50)\n", "\n", "# Test 1: Check folder structure\n", "print(\"\\n๐Ÿ“ Test 1: Checking folder structure...\")\n", "required_folders = ['models', 'utils', 'templates', 'static', 'u2net', 'u2net/weights', 'u2net/model', 'static/uploads']\n", "all_folders_ok = True\n", "\n", "for folder in required_folders:\n", " if os.path.exists(folder):\n", " print(f\" โœ… {folder} folder exists\")\n", " else:\n", " print(f\" โŒ {folder} folder missing\")\n", " all_folders_ok = False\n", "\n", "# %%\n", "# Test 2: Check required files\n", "print(\"\\n๐Ÿ“„ Test 2: Checking required files...\")\n", "\n", "required_files = [\n", " ('models', 'Pyramid_fusion_densenet121_model.h5'),\n", " ('utils', 'background_removal.py'),\n", " ('utils', 'edge_detection.py'),\n", " ('utils', 'preprocessing.py'),\n", " ('utils/scalers', 'scaler_y.pkl'),\n", " ('utils/scalers', 'continuous_scaler.pkl'),\n", " ('utils/scalers', 'days_scaler.pkl'),\n", " ('utils/scalers', 'nitrogen_map.pkl'),\n", " ('utils', 'temperature_means.pkl'),\n", " ('templates', 'index.html'),\n", " ('templates', 'estimation.html'),\n", " ('templates', 'about.html'),\n", " ('templates', 'team.html'),\n", " ('templates', 'how-it-works.html'),\n", " ('templates', 'contact.html'),\n", " ('templates', 'capture.html'),\n", " ('templates', 'result.html'),\n", " ('u2net/weights', 'u2net.pth'),\n", "]\n", "\n", "all_files_ok = True\n", "for folder, filename in required_files:\n", " filepath = os.path.join(folder, filename)\n", " if os.path.exists(filepath):\n", " file_size = os.path.getsize(filepath) / (1024 * 1024) if filename.endswith('.pth') or filename.endswith('.h5') else 0\n", " size_display = f\" ({file_size:.2f} MB)\" if file_size > 0 else \"\"\n", " print(f\" โœ… {filepath} exists{size_display}\")\n", " else:\n", " print(f\" โŒ {filepath} missing\")\n", " all_files_ok = False\n", "\n", "# %%\n", "# Test 3: Import utilities\n", "print(\"\\n๐Ÿ”ง Test 3: Testing utility imports...\")\n", "\n", "sys.path.append(os.path.join(os.getcwd(), 'utils'))\n", "\n", "try:\n", " from background_removal import BackgroundRemover\n", " print(\" โœ… BackgroundRemover imported\")\n", "except Exception as e:\n", " print(f\" โŒ BackgroundRemover import failed: {e}\")\n", "\n", "try:\n", " from edge_detection import EdgeDetector\n", " print(\" โœ… EdgeDetector imported\")\n", "except Exception as e:\n", " print(f\" โŒ EdgeDetector import failed: {e}\")\n", "\n", "try:\n", " from preprocessing import ImagePreprocessor\n", " print(\" โœ… ImagePreprocessor imported\")\n", "except Exception as e:\n", " print(f\" โŒ ImagePreprocessor import failed: {e}\")\n", "\n", "# %%\n", "# Test 4: Test BackgroundRemover initialization\n", "print(\"\\n๐ŸŒ„ Test 4: Testing BackgroundRemover initialization...\")\n", "\n", "try:\n", " # Test with portable initialization (no path needed)\n", " bg_remover = BackgroundRemover()\n", " print(\" โœ… BackgroundRemover initialized successfully\")\n", " \n", " # Test with a dummy image\n", " dummy_image = np.zeros((100, 100, 3), dtype=np.uint8)\n", " dummy_image[:] = [100, 150, 200]\n", " \n", " result, mask = bg_remover.remove_background(dummy_image, target_size=(224, 224))\n", " print(f\" โœ… Background removal works - output shape: {result.shape}\")\n", " if mask is not None:\n", " print(f\" โœ… Mask generated - shape: {mask.shape}\")\n", " else:\n", " print(f\" โ„น๏ธ No mask generated (running in fallback mode)\")\n", " \n", "except Exception as e:\n", " print(f\" โŒ BackgroundRemover test failed: {e}\")\n", " traceback.print_exc()\n", "\n", "# %%\n", "# Test 5: Quick test of preprocessing pipeline\n", "print(\"\\n๐Ÿ–ผ๏ธ Test 5: Testing preprocessing pipeline...\")\n", "\n", "try:\n", " # Create a dummy image\n", " import cv2\n", " from PIL import Image\n", " \n", " dummy_image = np.zeros((100, 100, 3), dtype=np.uint8)\n", " dummy_image[:] = [100, 150, 200]\n", " \n", " # Test edge detection\n", " edge_detector = EdgeDetector()\n", " edge_image, edges = edge_detector.detect_edges(dummy_image)\n", " print(f\" โœ… Edge detection works - output shape: {edge_image.shape}\")\n", " \n", " # Test preprocessor - use 224x224 to match model\n", " preprocessor = ImagePreprocessor(target_size=(224, 224))\n", " processed = preprocessor.preprocess_for_model(edge_image)\n", " print(f\" โœ… Preprocessor works with target_size=(224, 224) - output shape: {processed.shape}\")\n", " print(f\" โœ… Output range: [{processed.min():.2f}, {processed.max():.2f}]\")\n", " \n", "except Exception as e:\n", " print(f\" โŒ Preprocessing test failed: {e}\")\n", " traceback.print_exc()\n", "\n", "# %%\n", "# Test 6: Check model file\n", "print(\"\\n๐Ÿค– Test 6: Checking model file...\")\n", "\n", "model_path = os.path.join('models', 'Pyramid_fusion_densenet121_model.h5')\n", "if os.path.exists(model_path):\n", " file_size = os.path.getsize(model_path) / (1024 * 1024) # MB\n", " print(f\" โœ… Model file found: {model_path}\")\n", " print(f\" ๐Ÿ“Š File size: {file_size:.2f} MB\")\n", " \n", " # Quick model structure check without loading\n", " try:\n", " import h5py\n", " with h5py.File(model_path, 'r') as f:\n", " print(f\" โœ… Model file is valid HDF5 format\")\n", " except Exception as e:\n", " print(f\" โš ๏ธ Could not verify model file: {e}\")\n", "else:\n", " print(f\" โŒ Model file not found at: {model_path}\")\n", "\n", "# %%\n", "# Test 7: Check scaler files\n", "print(\"\\n๐Ÿ“Š Test 7: Checking scaler files...\")\n", "\n", "try:\n", " import pickle\n", " \n", " scaler_files = [\n", " ('utils/scalers/scaler_y.pkl', 'Target scaler'),\n", " ('utils/scalers/continuous_scaler.pkl', 'Temperature scaler'),\n", " ('utils/scalers/days_scaler.pkl', 'Days scaler'),\n", " ('utils/scalers/nitrogen_map.pkl', 'Nitrogen map'),\n", " ('utils/temperature_means.pkl', 'Temperature means')\n", " ]\n", " \n", " for filepath, description in scaler_files:\n", " if os.path.exists(filepath):\n", " with open(filepath, 'rb') as f:\n", " data = pickle.load(f)\n", " print(f\" โœ… {description} loaded successfully\")\n", " else:\n", " print(f\" โŒ {filepath} missing\")\n", " \n", "except Exception as e:\n", " print(f\" โŒ Scaler test failed: {e}\")\n", " traceback.print_exc()\n", "\n", "# %%\n", "# Test 8: Check Flask app syntax\n", "print(\"\\nโš™๏ธ Test 8: Checking Flask app syntax...\")\n", "\n", "try:\n", " import py_compile\n", " py_compile.compile('app.py')\n", " print(\" โœ… Flask app syntax is valid\")\n", "except Exception as e:\n", " print(f\" โŒ Flask app syntax error: {e}\")\n", " traceback.print_exc()\n", "\n", "# %%\n", "# Test 9: Check template files for Jinja syntax\n", "print(\"\\n๐Ÿ“ Test 9: Checking template files...\")\n", "\n", "template_files = ['index.html', 'estimation.html', 'about.html', 'team.html', \n", " 'how-it-works.html', 'contact.html', 'capture.html', 'result.html']\n", "\n", "for template in template_files:\n", " template_path = os.path.join('templates', template)\n", " if os.path.exists(template_path):\n", " with open(template_path, 'r', encoding='utf-8') as f:\n", " content = f.read()\n", " # Quick check for Jinja syntax\n", " if '{{' in content and '}}' in content:\n", " print(f\" โœ… {template} contains Jinja syntax\")\n", " else:\n", " print(f\" โ„น๏ธ {template} appears to be a static page (no Jinja syntax)\")\n", " else:\n", " print(f\" โŒ {template} missing\")\n", "\n", "# %%\n", "# Test 10: Check U2Net weights\n", "print(\"\\n๐Ÿ”ง Test 10: Checking U2Net weights...\")\n", "\n", "weights_path = os.path.join('u2net', 'weights', 'u2net.pth')\n", "if os.path.exists(weights_path):\n", " file_size = os.path.getsize(weights_path) / (1024 * 1024) # MB\n", " print(f\" โœ… U2Net weights found: {weights_path}\")\n", " print(f\" ๐Ÿ“Š File size: {file_size:.2f} MB\")\n", "else:\n", " print(f\" โš ๏ธ U2Net weights not found at: {weights_path}\")\n", " print(\" Background removal will run in fallback mode\")\n", "\n", "# %%\n", "# Summary\n", "print(\"\\n\" + \"=\"*50)\n", "print(\"๐Ÿ“Š TEST SUMMARY\")\n", "print(\"=\"*50)\n", "\n", "if all_folders_ok and all_files_ok:\n", " print(\"\\nโœ… All folders and files are in place!\")\n", " print(\"\\n๐ŸŽ‰ Your NitroSense AI application is ready to run!\")\n", "else:\n", " print(\"\\nโš ๏ธ Some files/folders are missing. Check the โŒ marks above.\")\n", "\n", "# Check if any critical components are missing\n", "critical_missing = []\n", "if not os.path.exists('models/Pyramid_fusion_densenet121_model.h5'):\n", " critical_missing.append(\"Model file\")\n", "if not os.path.exists('utils/background_removal.py'):\n", " critical_missing.append(\"Background remover\")\n", "if not os.path.exists('templates/index.html'):\n", " critical_missing.append(\"Templates\")\n", "\n", "if critical_missing:\n", " print(\"\\nโŒ Critical components missing:\")\n", " for item in critical_missing:\n", " print(f\" - {item}\")\n", " print(\"\\nPlease fix these before running the application.\")\n", "else:\n", " print(\"\\nโœ… All critical components present!\")\n", "\n", "print(\"\\n\" + \"=\"*50)\n", "print(\"๐Ÿš€ TO RUN THE APPLICATION:\")\n", "print(\"=\"*50)\n", "print(\"\\nOpen a terminal/command prompt and run:\")\n", "print(\" cd C:\\\\Users\\\\HP\\\\NitroSense-AI\")\n", "print(\" python app.py\")\n", "print(\"\\nThen open your browser and go to:\")\n", "print(\" http://localhost:5000\")\n", "\n", "# Optional: Print network URL for sharing\n", "import socket\n", "hostname = socket.gethostname()\n", "try:\n", " local_ip = socket.gethostbyname(hostname)\n", " print(f\" http://{local_ip}:5000 (for other devices on same network)\")\n", "except:\n", " print(\" Could not determine local IP\")\n", "\n", "print(\"\\n\" + \"=\"*50)\n", "\n", "\n", "# %% [markdown]\n", "# # Additional Test: Test Model Prediction with Dummy Data\n", "\n", "# %%\n", "print(\"\\n\" + \"=\"*50)\n", "print(\"๐Ÿงช TESTING MODEL PREDICTION WITH DUMMY DATA\")\n", "print(\"=\"*50)\n", "\n", "try:\n", " import tensorflow as tf\n", " from tensorflow.keras import backend as K\n", " import pickle\n", " \n", " # ============= DEFINE CUSTOM ACTIVATION FUNCTIONS =============\n", " def mish_activation(x):\n", " \"\"\"\n", " Mish activation function: x * tanh(softplus(x))\n", " \"\"\"\n", " return x * tf.math.tanh(tf.math.softplus(x))\n", " \n", " def swish_activation(x):\n", " \"\"\"\n", " Swish activation function: x * sigmoid(x)\n", " \"\"\"\n", " return x * tf.nn.sigmoid(x)\n", " # ============================================================\n", " \n", " # Load the model\n", " model_path = os.path.join('models', 'Pyramid_fusion_densenet121_model.h5')\n", " \n", " # Define custom metrics\n", " def rmse(y_true, y_pred):\n", " return K.sqrt(K.mean(K.square(y_pred - y_true)))\n", " \n", " def r2(y_true, y_pred):\n", " SS_res = K.sum(K.square(y_true - y_pred))\n", " SS_tot = K.sum(K.square(y_true - K.mean(y_true)))\n", " return 1 - SS_res/(SS_tot + K.epsilon())\n", " \n", " def mae(y_true, y_pred):\n", " return K.mean(K.abs(y_pred - y_true))\n", " \n", " # Add custom activation functions to custom_objects\n", " custom_objects = {\n", " 'mse': tf.keras.losses.MeanSquaredError(),\n", " 'MSE': tf.keras.losses.MeanSquaredError(),\n", " 'mean_squared_error': tf.keras.losses.MeanSquaredError(),\n", " 'mae': mae,\n", " 'MAE': mae,\n", " 'mean_absolute_error': mae,\n", " 'rmse': rmse,\n", " 'RMSE': rmse,\n", " 'root_mean_squared_error': rmse,\n", " 'r2': r2,\n", " 'R2': r2,\n", " 'r_squared': r2,\n", " 'R_squared': r2,\n", " # Add custom activation functions\n", " 'mish_activation': mish_activation,\n", " 'swish_activation': swish_activation,\n", " }\n", " \n", " # Register custom activations globally (as fallback)\n", " tf.keras.utils.get_custom_objects()['mish_activation'] = mish_activation\n", " tf.keras.utils.get_custom_objects()['swish_activation'] = swish_activation\n", " \n", " # Load model\n", " print(\"Loading model...\")\n", " model = tf.keras.models.load_model(model_path, custom_objects=custom_objects, compile=False)\n", " print(\"โœ… Model loaded successfully\")\n", " \n", " # Print model input shapes to verify\n", " print(\"\\n๐Ÿ“Š Model input shapes:\")\n", " for i, input_layer in enumerate(model.inputs):\n", " print(f\" Input {i+1}: {input_layer.name} - Shape: {input_layer.shape}\")\n", " \n", " # Create dummy test data with CORRECT size (224x224 for DenseNet121)\n", " print(\"\\n๐Ÿ”„ Creating dummy test data with size 224x224...\")\n", " dummy_image = np.random.rand(1, 224, 224, 3).astype(np.float32)\n", " dummy_features = np.random.rand(1, 4).astype(np.float32)\n", " \n", " print(f\" Dummy image shape: {dummy_image.shape}\")\n", " print(f\" Dummy features shape: {dummy_features.shape}\")\n", " print(f\" Dummy features: {dummy_features[0]}\")\n", " \n", " # Make prediction\n", " print(\"\\n๐Ÿ”„ Making prediction...\")\n", " prediction = model.predict([dummy_image, dummy_features], verbose=0)\n", " print(f\"โœ… Model prediction successful: {prediction[0][0]:.6f}\")\n", " print(f\" Prediction shape: {prediction.shape}\")\n", " \n", " # Test with different feature values to see if output changes\n", " print(\"\\n๐Ÿ”„ Testing with different feature values:\")\n", " \n", " test_features = [\n", " np.array([[0.1, 0.1, 0, 0.1]], dtype=np.float32),\n", " np.array([[0.5, 0.5, 3, 0.5]], dtype=np.float32),\n", " np.array([[0.9, 0.9, 7, 0.9]], dtype=np.float32)\n", " ]\n", " \n", " for i, features in enumerate(test_features):\n", " pred = model.predict([dummy_image, features], verbose=0)\n", " print(f\" Test {i+1} - Features: {features[0]} โ†’ Prediction: {pred[0][0]:.6f}\")\n", " \n", " # Test with multiple feature variations to check if model responds to inputs\n", " print(\"\\n๐Ÿ” Testing prediction variation with different features:\")\n", " feature_sets = [\n", " np.array([[0.0, 0.0, 0, 0.0]], dtype=np.float32),\n", " np.array([[0.2, 0.2, 1, 0.2]], dtype=np.float32),\n", " np.array([[0.4, 0.4, 3, 0.4]], dtype=np.float32),\n", " np.array([[0.6, 0.6, 5, 0.6]], dtype=np.float32),\n", " np.array([[0.8, 0.8, 6, 0.8]], dtype=np.float32),\n", " np.array([[1.0, 1.0, 7, 1.0]], dtype=np.float32)\n", " ]\n", " \n", " predictions = []\n", " for features in feature_sets:\n", " pred = model.predict([dummy_image, features], verbose=0)[0][0]\n", " predictions.append(pred)\n", " print(f\" Features {features[0]} โ†’ {pred:.6f}\")\n", " \n", " # Check if all predictions are the same\n", " if len(set(predictions)) == 1:\n", " print(\"\\nโš ๏ธ WARNING: All predictions are identical! Model might not be working correctly.\")\n", " else:\n", " print(\"\\nโœ… Model produces varying outputs for different inputs - good!\")\n", " \n", " # Test with scaler_y if available\n", " try:\n", " scaler_y_path = os.path.join('utils/scalers', 'scaler_y.pkl')\n", " if os.path.exists(scaler_y_path):\n", " with open(scaler_y_path, 'rb') as f:\n", " scaler_y = pickle.load(f)\n", " print(f\"\\n๐Ÿ“Š Testing denormalization with scaler_y:\")\n", " print(f\" Scaler_y mean: {scaler_y.mean_[0]:.4f}, scale: {scaler_y.scale_[0]:.4f}\")\n", " \n", " # Denormalize the predictions\n", " pred_array = np.array([[p] for p in predictions])\n", " original_values = scaler_y.inverse_transform(pred_array)\n", " print(f\"\\n๐Ÿ“Š Denormalized values (should be in range 2-5%):\")\n", " for i, (norm, orig) in enumerate(zip(predictions, original_values)):\n", " print(f\" Normalized: {norm:.6f} โ†’ Original Nitrogen: {orig[0]:.2f}%\")\n", " except Exception as e:\n", " print(f\" Could not test denormalization: {e}\")\n", " \n", " # Compare with the sample prediction from model testing\n", " print(f\"\\n๐Ÿ“Š Note: After denormalization, values should be in the range 2-5%\")\n", " \n", "except Exception as e:\n", " print(f\"โŒ Model test failed: {e}\")\n", " traceback.print_exc()" ] }, { "cell_type": "code", "execution_count": 3, "id": "feed142c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "==================================================\n", "๐Ÿ” U2Net DIAGNOSTIC\n", "==================================================\n", "\n", "๐Ÿ“ Checking directories:\n", " u2net_dir exists: True\n", " model_dir exists: True\n", " weights_dir exists: True\n", "\n", "๐Ÿ“„ Checking u2net.py:\n", " Path: u2net\\model\\u2net.py\n", " Exists: True\n", " Size: 14.37 KB\n", "\n", "โš–๏ธ Checking weights:\n", " Path: u2net\\weights\\u2net.pth\n", " Exists: True\n", " Size: 168.12 MB\n", "\n", "๐Ÿ”„ Trying to import U2Net module:\n", " โœ… Successfully imported U2NET\n", " U2NET type: \n", "\n", "๐Ÿ”„ Initializing BackgroundRemover:\n", "โœ… U2Net source already present\n", "โœ… BackgroundRemover initialized with U2Net model\n", " u2net_available: True\n", " model loaded: True\n" ] } ], "source": [ "# Diagnostic for U2Net\n", "print(\"=\"*50)\n", "print(\"๐Ÿ” U2Net DIAGNOSTIC\")\n", "print(\"=\"*50)\n", "\n", "import os\n", "import sys\n", "\n", "# Check if u2net directory exists\n", "u2net_dir = 'u2net'\n", "model_dir = os.path.join(u2net_dir, 'model')\n", "weights_dir = os.path.join(u2net_dir, 'weights')\n", "\n", "print(f\"\\n๐Ÿ“ Checking directories:\")\n", "print(f\" u2net_dir exists: {os.path.exists(u2net_dir)}\")\n", "print(f\" model_dir exists: {os.path.exists(model_dir)}\")\n", "print(f\" weights_dir exists: {os.path.exists(weights_dir)}\")\n", "\n", "# Check for u2net.py\n", "u2net_py = os.path.join(model_dir, 'u2net.py')\n", "print(f\"\\n๐Ÿ“„ Checking u2net.py:\")\n", "print(f\" Path: {u2net_py}\")\n", "print(f\" Exists: {os.path.exists(u2net_py)}\")\n", "if os.path.exists(u2net_py):\n", " file_size = os.path.getsize(u2net_py) / 1024\n", " print(f\" Size: {file_size:.2f} KB\")\n", "\n", "# Check for weights\n", "weights_path = os.path.join(weights_dir, 'u2net.pth')\n", "print(f\"\\nโš–๏ธ Checking weights:\")\n", "print(f\" Path: {weights_path}\")\n", "print(f\" Exists: {os.path.exists(weights_path)}\")\n", "if os.path.exists(weights_path):\n", " file_size = os.path.getsize(weights_path) / (1024 * 1024)\n", " print(f\" Size: {file_size:.2f} MB\")\n", "\n", "# Try to import\n", "print(f\"\\n๐Ÿ”„ Trying to import U2Net module:\")\n", "sys.path.insert(0, os.path.abspath(model_dir))\n", "try:\n", " from u2net import U2NET\n", " print(f\" โœ… Successfully imported U2NET\")\n", " print(f\" U2NET type: {type(U2NET)}\")\n", "except Exception as e:\n", " print(f\" โŒ Import failed: {e}\")\n", "\n", "# Initialize BackgroundRemover with verbose output\n", "print(f\"\\n๐Ÿ”„ Initializing BackgroundRemover:\")\n", "from utils.background_removal import BackgroundRemover\n", "bg_remover = BackgroundRemover()\n", "print(f\" u2net_available: {bg_remover.u2net_available}\")\n", "print(f\" model loaded: {bg_remover.model is not None}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "a0274133", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }