Commit da697be5 authored by Alessia Marcolini's avatar Alessia Marcolini
Browse files

Fix CUDA_VISIBLE_DEVICES environment variable

parent ebb3d4b1
......@@ -55,6 +55,8 @@
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"os.environ['CUDA_VISIBLE_DEVICES'] = '0'\n",
"import torch\n",
"import torch.nn as nn\n",
"from torch.utils.data import DataLoader\n",
......@@ -69,8 +71,6 @@
"metadata": {},
"outputs": [],
"source": [
"os.environ['CUDA_VISIBLE_DEVICES'] = '0'\n",
"\n",
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
"multigpu = True"
]
......@@ -81,7 +81,7 @@
"metadata": {},
"outputs": [],
"source": [
"DATASETDIR = '/thunderdisk/HN/processed/bbox_64_augmented_LR'\n",
"DATASETDIR = '/thunderdisk/HN/processed/bbox_fixed2_64'\n",
"EXPERIMENT_DIR = f'{PATH}/experiments'"
]
},
......@@ -91,7 +91,7 @@
"metadata": {},
"outputs": [],
"source": [
"MODEL_NAME = 'LR_noTx_branch_wise_free_aug_20191027-003918'\n",
"MODEL_NAME = 'Tstage_binary_augmented_noTx_branch_wise_20191028-104101'\n",
"SIZE = 64\n",
"\n",
"OUTDIR = f'{EXPERIMENT_DIR}/{MODEL_NAME}/features/'\n",
......@@ -106,7 +106,7 @@
"outputs": [],
"source": [
"dataset = NumpyCSVDataset(DATASETDIR , f'{PATH}/data/clinical_data_noTx.csv' , 'Locoregional', SIZE , mode='test')\n",
"loader = DataLoader(dataset, batch_size=16, num_workers=12, pin_memory=True, shuffle=False, drop_last=False)\n",
"loader = DataLoader(dataset, batch_size=8, num_workers=12, pin_memory=True, shuffle=False, drop_last=False)\n",
"model_weights = f'{EXPERIMENT_DIR}/{MODEL_NAME}/weights.pth'"
]
},
......@@ -199,7 +199,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python (dappertf)",
"display_name": "dappertf",
"language": "python",
"name": "dappertf"
},
......
%% Cell type:markdown id: tags:
## Deep features extraction
%% Cell type:code id: tags:
``` python
%HN_env
```
%% Cell type:code id: tags:
``` python
import os
PATH = os.path.abspath(os.path.curdir)
```
%% Cell type:code id: tags:
``` python
%reload_ext autoreload
%autoreload 2
```
%% Cell type:markdown id: tags:
### Import
%% Cell type:code id: tags:
``` python
import os
import sys
from tqdm import tqdm
import numpy as np
import pandas as pd
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from networks import CiompiDO
from dataset import NumpyCSVDataset, augment_3D_HN
```
%% Cell type:code id: tags:
``` python
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
multigpu = True
```
%% Cell type:code id: tags:
``` python
DATASETDIR = '/thunderdisk/HN/processed/bbox_64_augmented_LR'
DATASETDIR = '/thunderdisk/HN/processed/bbox_fixed2_64'
EXPERIMENT_DIR = f'{PATH}/experiments'
```
%% Cell type:code id: tags:
``` python
MODEL_NAME = 'LR_noTx_branch_wise_free_aug_20191027-003918'
MODEL_NAME = 'Tstage_binary_augmented_noTx_branch_wise_20191028-104101'
SIZE = 64
OUTDIR = f'{EXPERIMENT_DIR}/{MODEL_NAME}/features/'
OUTFILE = 'features_noTx.csv'
os.makedirs(OUTDIR, exist_ok=True)
```
%% Cell type:code id: tags:
``` python
dataset = NumpyCSVDataset(DATASETDIR , f'{PATH}/data/clinical_data_noTx.csv' , 'Locoregional', SIZE , mode='test')
loader = DataLoader(dataset, batch_size=16, num_workers=12, pin_memory=True, shuffle=False, drop_last=False)
loader = DataLoader(dataset, batch_size=8, num_workers=12, pin_memory=True, shuffle=False, drop_last=False)
model_weights = f'{EXPERIMENT_DIR}/{MODEL_NAME}/weights.pth'
```
%% Cell type:code id: tags:
``` python
model = CiompiDO(n_classes=2, n_channels=2, modality='CT/PET')
if multigpu:
model = nn.DataParallel(model.to(device))
model = model.module
model.load_state_dict(torch.load(model_weights))
```
%% Cell type:code id: tags:
``` python
#%%
deep_features = []
sample_names = []
labels = []
with torch.no_grad():
for batch in tqdm(loader):
names = [name.split('.')[0] for name in batch['filename']]
image = batch['data'].to(device)
label = batch['target']
out = model.extract_features(image.cuda())
deep_features.append(out.data.cpu().numpy())
sample_names.append(names)
labels.append(label)
```
%% Cell type:code id: tags:
``` python
deep_features = np.concatenate(deep_features)
sample_names = np.concatenate(sample_names)
labels = np.concatenate(labels)
```
%% Cell type:code id: tags:
``` python
len(labels)
```
%% Cell type:code id: tags:
``` python
print(deep_features.shape, len(sample_names),len(labels))
```
%% Cell type:code id: tags:
``` python
deep_features_pd = pd.DataFrame(deep_features, index=sample_names)
# deep_features_pd['class'] = labels
#%% SAVE
print(deep_features_pd.shape)
deep_features_pd.to_csv(f'{OUTDIR}/{OUTFILE}')
```
%% Cell type:code id: tags:
``` python
```
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment