Commit 25bc3cc0 authored by Nicole Bussola's avatar Nicole Bussola Committed by Alessia Marcolini
Browse files

extract deep features from backbone network

parent 37b8c165
%% Cell type:markdown id: tags:
### NOTEBOOK INFO
Use the trained networks to extract deep features
## Deep features extraction
%% Cell type:markdown id: tags:
%% Cell type:code id: tags:
``` python
%HN_env
```
### Set Path
%% Output
<IPython.core.display.HTML object>
%% 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
#PATH = '/home/dsalvalai/projects/networks_dami'
import os
PATH = os.getcwd()
print(PATH)
```
%% Cell type:markdown id: tags:
### Import packages
### Import
%% Cell type:code id: tags:
``` python
import sys
import torch
import numpy as np
import os
import pandas as pd
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,1,2,3"
#os.environ["CUDA_VISIBLE_DEVICES"] = "2,3"
device = torch.device("cpu" if torch.cuda.is_available() else "cpu")
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 = f"{PATH}/data/processed/bbox_fixed2_64" #Not augmented but already 64**3 (for faster loading)
DATASETDIR = "/thunderdisk/HN/processed/bbox_fixed2_64" #Not augmented but already 64**3 (for faster loading)
EXPERIMENT_DIR = f"{PATH}/experiments"
```
%% Cell type:code id: tags:
``` python
MODEL_NAME = 'transfer_linear_only_TT'
MODEL_NAME = 'Tstage_binary_augmented_noTx_branch_wise_20191022-233016'
SIZE = 64
OUTDIR = f'{PATH}/data/features'
OUTFILE = 'deep_features_B.csv'
OUTDIR = f'{EXPERIMENT_DIR}/{MODEL_NAME}/features'
OUTFILE = 'features_wTx.csv'
# os.makedirs(OUTDIR, exist_ok=False)
```
%% Cell type:code id: tags:
``` python
dataset = NumpyCSVDataset(DATASETDIR , f"{PATH}/data/labels.csv" , "Locoregional", SIZE , mode="test")
loader = DataLoader(dataset, batch_size=32, num_workers=12, pin_memory=True, shuffle=False, drop_last=False)
dataset = NumpyCSVDataset(DATASETDIR , f"{PATH}/data/clinical_data.csv" , "T-stage_grouped", SIZE , mode="test")
loader = DataLoader(dataset, batch_size=16, 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 = nn.DataParallel(CiompiDO(n_classes=2).to(device))
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))
```
%% Output
<All keys matched successfully>
%% Cell type:code id: tags:
``` python
#%%
deep_features = []
sample_names = []
labels = []
with torch.no_grad():
for i,batch in enumerate(loader):
print(i)
names = batch['sample']
names = batch['filename']
image = batch['data'].to(device)
out = model.module.extract_features(image)
label = batch['target']
out = model.extract_features(image.cuda())
print(i)
deep_features.append(out.data.cpu().numpy())
sample_names.append(names)
labels.append(label)
```
%% Output
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
%% 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
print(deep_features.shape)
len(labels)
```
%% Output
294
%% Cell type:code id: tags:
``` python
deep_features_pd = pd.DataFrame(deep_features, index = sample_names)
print(deep_features.shape, len(sample_names),len(labels))
```
%% Output
(294, 512) 294 294
%% 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:
%% Output
``` python
```
(294, 513)
......
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