mirror of
https://github.com/NanjingForestryUniversity/SCNet.git
synced 2025-11-08 14:24:03 +00:00
175 lines
4.8 KiB
Plaintext
175 lines
4.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"pycharm": {
|
|
"name": "#%% md\n"
|
|
}
|
|
},
|
|
"source": [
|
|
"# Experiment 2: Model Evaluating"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 16,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"pycharm": {
|
|
"name": "#%%\n"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"from keras.models import load_model\n",
|
|
"from matplotlib import ticker\n",
|
|
"from scipy.io import loadmat\n",
|
|
"from sklearn.model_selection import train_test_split\n",
|
|
"from sklearn.metrics import mean_squared_error\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"%matplotlib inline"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"pycharm": {
|
|
"name": "#%% md\n"
|
|
}
|
|
},
|
|
"source": [
|
|
"In this experiment, we load model weights from the experiment1 and evaluate them on test dataset."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"pycharm": {
|
|
"name": "#%% md\n"
|
|
}
|
|
},
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 17,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"pycharm": {
|
|
"name": "#%%\n"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"shape of data:\n",
|
|
"x_train: (5728, 1, 102), y_train: (5728, 1),\n",
|
|
"x_val: (2455, 1, 102), y_val: (2455, 1)\n",
|
|
"x_test: (3508, 1, 102), y_test: (3508, 1)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"data = loadmat('./dataset/mango/mango_dm_split.mat')\n",
|
|
"\n",
|
|
"min_value, max_value = data['min_y'][-1][-1], data['max_y'][-1][-1]\n",
|
|
"retransform = lambda x: x * (max_value - min_value)\n",
|
|
"x_train, y_train, x_test, y_test = data['x_train'], data['y_train'], data['x_test'], data['y_test']\n",
|
|
"x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.3, random_state=12, shuffle=True)\n",
|
|
"x_train, x_val, x_test = x_train[:, np.newaxis, :], x_val[:, np.newaxis, :], x_test[:, np.newaxis, :]\n",
|
|
"print(f\"shape of data:\\n\"\n",
|
|
" f\"x_train: {x_train.shape}, y_train: {y_train.shape},\\n\"\n",
|
|
" f\"x_val: {x_val.shape}, y_val: {y_val.shape}\\n\"\n",
|
|
" f\"x_test: {x_test.shape}, y_test: {y_test.shape}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 18,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"pycharm": {
|
|
"name": "#%%\n"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"plain 5 mse : 0.05133910188824081\n",
|
|
"plain 5 Dry matter content error 0.7758644362065223\n",
|
|
"plain 5 r^2 : 0.902928516828363\n",
|
|
"plain 11 mse : 0.05200769624271875\n",
|
|
"plain 11 Dry matter content error 0.7859685978067217\n",
|
|
"plain 11 r^2 : 0.9003837097594369\n",
|
|
"shortcut 5 mse : 0.051382735052895194\n",
|
|
"shortcut 5 Dry matter content error 0.7765238443272209\n",
|
|
"shortcut 5 r^2 : 0.9027634443691182\n",
|
|
"shortcut11 mse : 0.05078784364469306\n",
|
|
"shortcut11 Dry matter content error 0.7675335217455442\n",
|
|
"shortcut11 r^2 : 0.9050019525259844\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from sklearn.metrics import r2_score\n",
|
|
"\n",
|
|
"## Build model and load weights\n",
|
|
"plain_5, plain_11 = load_model('./checkpoints/plain5.hdf5'), load_model('./checkpoints/plain11.hdf5')\n",
|
|
"shortcut5, shortcut11 = load_model('./checkpoints/shortcut5.hdf5'), load_model('./checkpoints/shortcut11.hdf5')\n",
|
|
"models = {'plain 5': plain_5, 'plain 11': plain_11, 'shortcut 5': shortcut5, 'shortcut11': shortcut11}\n",
|
|
"results = {model_name: model.predict(x_test).reshape((-1, )) for model_name, model in models.items()}\n",
|
|
"for model_name, model_result in results.items():\n",
|
|
" rmse = np.sqrt(mean_squared_error(y_test, model_result))\n",
|
|
" print(model_name, \"mse : \", rmse)\n",
|
|
" print(model_name, \"Dry matter content error\", retransform(rmse))\n",
|
|
" print(model_name, \"r^2 :\", r2_score(y_test, model_result))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"pycharm": {
|
|
"name": "#%%\n"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"interpreter": {
|
|
"hash": "7f619fc91ee8bdab81d49e7c14228037474662e3f2d607687ae505108922fa06"
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3.9.7 ('base')",
|
|
"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.9.7"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|