Files
Weather-Project/Project Final/Evaluation/Evalutation.ipynb
nmelone 7b30c71b53 Reconfigured folder structure
Added Evaluation, Preprocessing, Training, Transformation folders.
Preprocessing is just a rework of the folder for the new structure of the old preprocessing folder.

Training and Transformation are the old project file broken up into two parts and restructured.

Evaluation is for evaluating the predictive power of the model.
2018-09-24 20:48:07 -05:00

241 lines
6.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"import os\n",
"import numpy as np\n",
"from six.moves import cPickle\n",
"import matplotlib\n",
"matplotlib.use('Agg')\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.gridspec as gridspec\n",
"%matplotlib inline\n",
"from keras import backend as K\n",
"from keras.models import Model, model_from_json\n",
"from keras.layers import Input, Dense, Flatten\n",
"\n",
"from prednet import PredNet\n",
"from data_utils import SequenceGenerator\n",
"\n",
"from tqdm import tqdm"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"n_plot = 40\n",
"batch_size = 10\n",
"nt = 24"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"WEIGHTS_DIR = '../Training/weights/'\n",
"DATA_DIR = '../data/'\n",
"RESULTS_SAVE_DIR = './weather_results/'"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"weights_file = os.path.join(WEIGHTS_DIR, 'prednet_weather_weights.hdf5')\n",
"json_file = os.path.join(WEIGHTS_DIR, 'prednet_weather_model.json')\n",
"test_file = os.path.join(DATA_DIR, 'x_test.hkl')\n",
"test_sources = os.path.join(DATA_DIR, 'sources_test.hkl')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Load trained model\n",
"f = open(json_file, 'r')\n",
"json_string = f.read()\n",
"f.close()\n",
"train_model = model_from_json(json_string, custom_objects = {'PredNet': PredNet})\n",
"train_model.load_weights(weights_file)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Create testing model (to output predictions)\n",
"layer_config = train_model.layers[1].get_config()\n",
"layer_config['output_mode'] = 'prediction'\n",
"data_format = layer_config['data_format'] if 'data_format' in layer_config else layer_config['dim_ordering']\n",
"test_prednet = PredNet(weights=train_model.layers[1].get_weights(), **layer_config)\n",
"input_shape = list(train_model.layers[0].batch_input_shape[1:])\n",
"input_shape[0] = nt\n",
"inputs = Input(shape=tuple(input_shape))\n",
"predictions = test_prednet(inputs)\n",
"test_model = Model(inputs=inputs, outputs=predictions)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"test_generator = SequenceGenerator(test_file, test_sources, nt, sequence_start_mode='unique', data_format=data_format)\n",
"X_test = test_generator.create_all()\n",
"X_hat = test_model.predict(X_test, batch_size)\n",
"if data_format == 'channels_first':\n",
" X_test = np.transpose(X_test, (0, 1, 3, 4, 2))\n",
" X_hat = np.transpose(X_hat, (0, 1, 3, 4, 2))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Compare MSE of PredNet predictions vs. using last frame. Write results to prediction_scores.txt\n",
"mse_model = np.nanmean( (X_test[:, 1:] - X_hat[:, 1:])**2 ) # look at all timesteps except the first\n",
"mse_prev = np.nanmean( (X_test[:, :-1] - X_test[:, 1:])**2 )\n",
"if not os.path.exists(RESULTS_SAVE_DIR): os.mkdir(RESULTS_SAVE_DIR)\n",
"f = open(RESULTS_SAVE_DIR + 'prediction_scores.txt', 'w')\n",
"f.write(\"Model MSE: %f\\n\" % mse_model)\n",
"f.write(\"Previous Frame MSE: %f\" % mse_prev)\n",
"f.close()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model MSE:\t 14.119876861572266\n",
"Prev Frame MSE:\t 0.02834348939359188\n"
]
}
],
"source": [
"print(\"Model MSE:\\t {}\\nPrev Frame MSE:\\t {}\".format(mse_model,mse_prev))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 63%|███████████████████████████████████████████████████▉ | 19/30 [09:38<05:35, 30.47s/it]"
]
}
],
"source": [
"# Plot some predictions\n",
"aspect_ratio = float(X_hat.shape[3]) / X_hat.shape[2]\n",
"plt.figure(figsize = (nt, 7*2*aspect_ratio))\n",
"gs = gridspec.GridSpec(2*7, nt)\n",
"gs.update(wspace=0., hspace=0.2)\n",
"plot_save_dir = os.path.join(RESULTS_SAVE_DIR, 'prediction_plots/')\n",
"if not os.path.exists(plot_save_dir): os.mkdir(plot_save_dir)\n",
"plot_idx = np.random.permutation(X_test.shape[0])[:n_plot]\n",
"for i in tqdm(plot_idx):\n",
" for t in range(nt):\n",
" for c in range(7):\n",
" plt.subplot(gs[t + c*2*nt])\n",
" plt.imshow(X_test[i,t,:,:,c], interpolation='none')\n",
" plt.tick_params(axis='both', which='both', bottom='off', top='off', left='off', right='off', labelbottom='off', labelleft='off')\n",
" if t==0: plt.ylabel('Actual', fontsize=10)\n",
"\n",
" plt.subplot(gs[t + (c*2+1)*nt])\n",
" plt.imshow(X_hat[i,t,:,:,c], interpolation='none')\n",
" plt.tick_params(axis='both', which='both', bottom='off', top='off', left='off', right='off', labelbottom='off', labelleft='off')\n",
" if t==0: plt.ylabel('Predicted', fontsize=10)\n",
"\n",
" plt.savefig(plot_save_dir + 'plot_' + str(i) + '.png')\n",
" plt.clf()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig=plt.figure(figsize=(15,10))\n",
"columns = 3\n",
"rows = 4\n",
"for i in range(1,columns+rows +1):\n",
" fig.add_subplot(rows,columns,i)\n",
" plt.imshow(X_test[0,0,:,:,i-1],X_hat[0,0,:,:,i-1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X_hat[0][0][0][0][2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}