In [1]:
Copied!
import sklearn
import pickle
import numpy as np
from tslearn.datasets import UCR_UEA_datasets
import tslearn
import sklearn
import pickle
import numpy as np
from tslearn.datasets import UCR_UEA_datasets
import tslearn
/home/jacqueline/.local/share/virtualenvs/TSInterpret-x4eqnPOt/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm
Parameters¶
In [2]:
Copied!
dataset='BasicMotions'
dataset='BasicMotions'
In [3]:
Copied!
tslearn.datasets.CachedDatasets().list_datasets()
tslearn.datasets.CachedDatasets().list_datasets()
Out[3]:
['Trace']
Load Data¶
In [4]:
Copied!
train_x,train_y, test_x, test_y=UCR_UEA_datasets().load_dataset(dataset)
train_x,train_y, test_x, test_y=UCR_UEA_datasets().load_dataset(dataset)
In [5]:
Copied!
print(train_x.shape)
print(test_x.shape)
print(train_x.shape)
print(test_x.shape)
(40, 100, 6) (40, 100, 6)
In [6]:
Copied!
enc1=sklearn.preprocessing.OneHotEncoder(sparse=False).fit(np.vstack((train_y.reshape(-1,1),test_y.reshape(-1,1))))
pickle.dump(enc1,open(f'../../ClassificationModels/models/{dataset}/OneHotEncoder_tf.pkl','wb'))
train_y=enc1.transform(train_y.reshape(-1,1))
test_y=enc1.transform(test_y.reshape(-1,1))
enc1=sklearn.preprocessing.OneHotEncoder(sparse=False).fit(np.vstack((train_y.reshape(-1,1),test_y.reshape(-1,1))))
pickle.dump(enc1,open(f'../../ClassificationModels/models/{dataset}/OneHotEncoder_tf.pkl','wb'))
train_y=enc1.transform(train_y.reshape(-1,1))
test_y=enc1.transform(test_y.reshape(-1,1))
/home/jacqueline/.local/share/virtualenvs/TSInterpret-x4eqnPOt/lib/python3.9/site-packages/sklearn/preprocessing/_encoders.py:972: FutureWarning: `sparse` was renamed to `sparse_output` in version 1.2 and will be removed in 1.4. `sparse_output` is ignored unless you leave `sparse` to its default value. warnings.warn(
Load Model¶
In [7]:
Copied!
import tensorflow as tf
model = tf.keras.models.load_model(f'../../ClassificationModels/models/{dataset}/cnn/BasicMotionsbest_model.hdf5')
import tensorflow as tf
model = tf.keras.models.load_model(f'../../ClassificationModels/models/{dataset}/cnn/BasicMotionsbest_model.hdf5')
2023-09-18 14:42:58.962386: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. 2023-09-18 14:42:59.585681: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT 2023-09-18 14:43:00.261652: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:995] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 2023-09-18 14:43:00.262132: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1960] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform. Skipping registering GPU devices...
In [8]:
Copied!
label_pred=np.argmax(model.predict(test_x),axis=1)
label_pred
label_pred=np.argmax(model.predict(test_x),axis=1)
label_pred
2/2 [==============================] - 0s 2ms/step
Out[8]:
array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
Explanation Algo¶
In [9]:
Copied!
item=test_x[0].reshape(1,test_x.shape[1],test_x.shape[2])
shape=item.shape
y_target= model.predict(item)
item=test_x[0].reshape(1,test_x.shape[1],test_x.shape[2])
shape=item.shape
y_target= model.predict(item)
1/1 [==============================] - 0s 16ms/step
In [10]:
Copied!
y_target
y_target
Out[10]:
array([[2.8015620e-05, 1.2882984e-03, 9.9654043e-01, 6.2079956e-03]], dtype=float32)
In [11]:
Copied!
from TSInterpret.InterpretabilityModels.counterfactual.COMTECF import COMTECF
exp_model= COMTECF(model,(train_x,train_y),mode='time', backend='TF', method= 'brute')
from TSInterpret.InterpretabilityModels.counterfactual.COMTECF import COMTECF
exp_model= COMTECF(model,(train_x,train_y),mode='time', backend='TF', method= 'brute')
In [12]:
Copied!
exp = exp_model.explain(item)
exp = exp_model.explain(item)
1/1 [==============================] - 0s 21ms/step 1/1 [==============================] - 0s 14ms/step 2/2 [==============================] - 0s 2ms/step 1/1 [==============================] - 0s 14ms/step 1/1 [==============================] - 0s 16ms/step 1/1 [==============================] - 0s 13ms/step 1/1 [==============================] - 0s 13ms/step 1/1 [==============================] - 0s 13ms/step 1/1 [==============================] - 0s 14ms/step 1/1 [==============================] - 0s 14ms/step 1/1 [==============================] - 0s 14ms/step 1/1 [==============================] - 0s 14ms/step 1/1 [==============================] - 0s 13ms/step 1/1 [==============================] - 0s 13ms/step 1/1 [==============================] - 0s 13ms/step 1/1 [==============================] - 0s 13ms/step 1/1 [==============================] - 0s 13ms/step 1/1 [==============================] - 0s 13ms/step 1/1 [==============================] - 0s 13ms/step 1/1 [==============================] - 0s 14ms/step 1/1 [==============================] - 0s 13ms/step 1/1 [==============================] - 0s 13ms/step
In [13]:
Copied!
array, label=exp
array, label=exp
In [14]:
Copied!
%matplotlib inline
org_label=np.argmax(y_target)
cf_label=label[0]
exp=array
print(exp.shape)
exp_model.plot_in_one(np.array(item[0]),org_label,np.array(exp[0]),cf_label)
%matplotlib inline
org_label=np.argmax(y_target)
cf_label=label[0]
exp=array
print(exp.shape)
exp_model.plot_in_one(np.array(item[0]),org_label,np.array(exp[0]),cf_label)
(1, 100, 6)