{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/yifan/miniconda3/envs/deepdebugger/lib/python3.7/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n",
"2023-07-21 18:00:51.052396: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n",
"To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
"2023-07-21 18:00:51.578131: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory\n",
"2023-07-21 18:00:51.578176: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory\n",
"2023-07-21 18:00:51.578180: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Finish initialization...\n"
]
}
],
"source": [
"########################################################################################################################\n",
"# IMPORT #\n",
"########################################################################################################################\n",
"import torch\n",
"import sys\n",
"import os\n",
"import json\n",
"import numpy as np\n",
"sys.path.append('..')\n",
"\n",
"from singleVis.SingleVisualizationModel import VisModel\n",
"\n",
"from singleVis.data import NormalDataProvider\n",
"\n",
"from singleVis.projector import DVIProjector\n",
"from singleVis.eval.evaluator import Evaluator\n",
"\n",
"VIS_METHOD = \"DVI\" # DeepVisualInsight\n",
"\n",
"########################################################################################################################\n",
"# LOAD PARAMETERS #\n",
"########################################################################################################################\n",
"CONTENT_PATH = \"/home/yifan/0ExpMinist/Default/01\"\n",
"\n",
"sys.path.append(CONTENT_PATH)\n",
"with open(os.path.join(CONTENT_PATH, \"config.json\"), \"r\") as f:\n",
" config = json.load(f)\n",
"config = config[VIS_METHOD]\n",
"\n",
"# record output information\n",
"# now = time.strftime(\"%Y-%m-%d-%H_%M_%S\", time.localtime(time.time())) \n",
"# sys.stdout = open(os.path.join(CONTENT_PATH, now+\".txt\"), \"w\")\n",
"\n",
"SETTING = config[\"SETTING\"]\n",
"CLASSES = config[\"CLASSES\"]\n",
"DATASET = config[\"DATASET\"]\n",
"PREPROCESS = config[\"VISUALIZATION\"][\"PREPROCESS\"]\n",
"GPU_ID = config[\"GPU\"]\n",
"EPOCH_START = config[\"EPOCH_START\"]\n",
"EPOCH_END = config[\"EPOCH_END\"]\n",
"EPOCH_PERIOD = config[\"EPOCH_PERIOD\"]\n",
"\n",
"# Training parameter (subject model)\n",
"TRAINING_PARAMETER = config[\"TRAINING\"]\n",
"NET = TRAINING_PARAMETER[\"NET\"]\n",
"LEN = TRAINING_PARAMETER[\"train_num\"]\n",
"\n",
"# Training parameter (visualization model)\n",
"VISUALIZATION_PARAMETER = config[\"VISUALIZATION\"]\n",
"LAMBDA1 = VISUALIZATION_PARAMETER[\"LAMBDA1\"]\n",
"LAMBDA2 = VISUALIZATION_PARAMETER[\"LAMBDA2\"]\n",
"B_N_EPOCHS = VISUALIZATION_PARAMETER[\"BOUNDARY\"][\"B_N_EPOCHS\"]\n",
"L_BOUND = VISUALIZATION_PARAMETER[\"BOUNDARY\"][\"L_BOUND\"]\n",
"ENCODER_DIMS = VISUALIZATION_PARAMETER[\"ENCODER_DIMS\"]\n",
"DECODER_DIMS = VISUALIZATION_PARAMETER[\"DECODER_DIMS\"]\n",
"S_N_EPOCHS = VISUALIZATION_PARAMETER[\"S_N_EPOCHS\"]\n",
"N_NEIGHBORS = VISUALIZATION_PARAMETER[\"N_NEIGHBORS\"]\n",
"PATIENT = VISUALIZATION_PARAMETER[\"PATIENT\"]\n",
"MAX_EPOCH = VISUALIZATION_PARAMETER[\"MAX_EPOCH\"]\n",
"\n",
"VIS_MODEL_NAME = VISUALIZATION_PARAMETER[\"VIS_MODEL_NAME\"]\n",
"EVALUATION_NAME = VISUALIZATION_PARAMETER[\"EVALUATION_NAME\"]\n",
"\n",
"\n",
"\n",
"# VIS_MODEL_NAME = 'dvi_grid'\n",
"\n",
"# Define hyperparameters\n",
"DEVICE = torch.device(\"cuda:{}\".format(GPU_ID) if torch.cuda.is_available() else \"cpu\")\n",
"\n",
"import Model.model as subject_model\n",
"net = eval(\"subject_model.{}()\".format(NET))\n",
"\n",
"# Define data_provider\n",
"data_provider = NormalDataProvider(CONTENT_PATH, net, EPOCH_START, EPOCH_END, EPOCH_PERIOD, device=DEVICE, epoch_name='Epoch',classes=CLASSES,verbose=1)\n",
"\n",
"\n",
"# Define visualization models\n",
"model = VisModel(ENCODER_DIMS, DECODER_DIMS)\n",
"\n",
"# Define Projector\n",
"projector = DVIProjector(vis_model=model, content_path=CONTENT_PATH, vis_model_name=VIS_MODEL_NAME, device=DEVICE) \n",
"\n",
"########################################################################################################################\n",
"# VISUALIZATION #\n",
"########################################################################################################################\n",
"\n",
"from singleVis.visualizer import visualizer\n",
"\n",
"vis = visualizer(data_provider, projector, 200, \"tab10\")\n",
"save_dir = os.path.join(data_provider.content_path, \"imgptDVI\")\n",
"if not os.path.exists(save_dir):\n",
" os.mkdir(save_dir)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"epoch = 20\n",
"from trustVis.sampeling import Sampleing\n",
"sampleing = Sampleing(data_provider,epoch,data_provider.DEVICE)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"k = 15\n",
"from sklearn.cluster import KMeans\n",
"data = data_provider.train_representation(epoch)\n",
"\n",
"# 创建KMeans对象\n",
"kmeans = KMeans(n_clusters=k)\n",
"\n",
"# 对数据进行聚类\n",
"kmeans.fit(data)\n",
"\n",
"# 获取聚类中心\n",
"centers = kmeans.cluster_centers_\n",
"labels = kmeans.labels_"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"label = 10\n",
"\n",
"subset_indices = np.where(labels == label)[0]\n",
"subset = data[subset_indices]\n",
"center = centers[label]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 300/300 [00:00<00:00, 8085.35it/s]\n",
"100%|██████████| 1/1 [00:00<00:00, 3880.02it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"p_d 0.8967833433333334 p_a 0.8224383433333334\n",
"val 0.30442274250810725\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"val= sampleing.subset_info_cal(data_provider.train_representation(epoch),center,10,2)\n",
"print(\"val\",val)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"val 0.30442274250810725\n"
]
},
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mThe Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click here for more info. View Jupyter log for further details."
]
}
],
"source": [
"print(\"val\",val)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Successfully load the DVI visualization model for iteration 20\n",
"Successfully load the DVI visualization model for iteration 20\n"
]
}
],
"source": [
"emb = projector.batch_project(epoch, data_provider.train_representation(epoch))\n",
"new_data = projector.batch_inverse(epoch,emb )\n",
"# new_center = projector.individual_inverse()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 300/300 [00:00<00:00, 8086.91it/s]\n",
"100%|██████████| 1/1 [00:00<00:00, 3826.92it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"p_d 0.8794333433333333 p_a 0.8224383433333334\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"val_ = sampleing.subset_info_cal(new_data,center,10,1.5)\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.32395926861598934\n"
]
}
],
"source": [
"print(val_)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.7935021381815792, 0.55190001, 0.8194566766666668)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"val_,a_,b_"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "deepdebugger",
"language": "python",
"name": "deepdebugger"
},
"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.7.15"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}