-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncode.py
184 lines (158 loc) · 6.71 KB
/
Encode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import glob
import os
import torch
import torchvision
import torch.nn as nn
from tqdm import tqdm
import torch.optim as optim
from torch.utils.data import DataLoader,Dataset
from torchvision import datasets,models,transforms
from sklearn.metrics import accuracy_score,balanced_accuracy_score,f1_score,precision_score,recall_score,roc_auc_score,roc_curve
from PIL import Image,ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
torch.manual_seed(42)
torch.backends.cudnn.deterministic=True
import time
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
from compressai.datasets import ImageFolder
from compressai.losses import RateDistortionLoss
from compressai.optimizers import net_aux_optimizer
from compressai.zoo import image_models,models
import CLIC_Approaches.EncodeDecodeFunctions as EDF
from NeuralImageEncoder import CompressAINetwork_ShapePreserving
import histomicstk as htk
import random
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
def seed_everything(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic=True
torch.backends.cudnn.benchmark=True
seed_everything(42)
def StainDeconvHistomics(PILImg):
stains = ['hematoxylin', # nuclei stain
'eosin', # cytoplasm stain
'dab'] # set to null if input contains only two stains
stain_color_map = htk.preprocessing.color_deconvolution.stain_color_map
img=np.asarray(PILImg)
W = np.array([stain_color_map[st] for st in stains]).T
imDeconvolved = htk.preprocessing.color_deconvolution.color_deconvolution(img, W)
return np.concatenate((img,imDeconvolved.Stains),axis=2,dtype=np.float32)
def preprocessSQLC(img,model):
StainModel=model
transformations = transforms.Compose([transforms.CenterCrop(224), transforms.Lambda(StainDeconvHistomics), transforms.ToTensor()])
img=transformations(img).unsqueeze(0).cuda()
feats, recon = StainModel(img)
return feats
def CompressSQLC():
models_path='/models/path/'
_, Runs, _ = next(os.walk(models_path))
lmds = [float(x.split('_')[-1]) for x in Runs]
model_function = models['bmshj2018-factorized']
for lm in lmds:
if lm <= 0.05:
quality = 1
else:
quality = 8
net = model_function(quality=quality)
states = torch.load(models_path + str(lm) + 'checkpoint_best_loss.pth.tar')
try:
net.load_state_dict(states['state_dict'])
except Exception as e:
print(e)
net = net.cuda()
net.eval()
net.update()
stainModel=CompressAINetwork_ShapePreserving()
states = torch.load(models_path+str(lm)+'StainModelcheckpoint_best_loss.pth.tar')
stainModel.load_state_dict(states['state_dict'])
stainModel.eval()
stainModel=stainModel.cuda()
def SQLCCompression(InputImage):
InputImage = InputImage.unsqueeze(0)
InputImage = InputImage.cuda()
feats, recon = stainModel(InputImage)
reconstructed = net(feats)
decoded = stainModel.decode(reconstructed['x_hat'])
decoded = decoded[0, 0:3, :, :] / 255
return Image.fromarray(np.asarray((decoded.permute(1, 2, 0).detach().cpu()) * 255, dtype=np.uint8))
OutPutBasePath = 'OutPath'
os.makedirs(OutPutBasePath, exist_ok=True)
StainConv = 'IndividualRun'
CompressionModel = 'Original'
OutPath = os.path.join(OutPutBasePath, str(lm))
os.makedirs(OutPath, exist_ok=True)
path = '/path/to/files'
_, _, files = next(os.walk(path))
for file in files:
name = file
file = Image.open(os.path.join(path, file))
test=preprocessSQLC(file,stainModel)
test=test.cuda()
name = name[:-4]
file_name = name + '.bin'
FileOutputPath=os.path.join(OutPath,file_name)
compressed, codec, out, x = EDF.encode(test, quality, net, FileOutputPath)
def DecodeSQLC():
models_path='/models/path'
_, Runs, _ = next(os.walk(models_path))
lmds = [float(x.split('_')[-1]) for x in Runs]
model_function = models['bmshj2018-factorized']
for lm in lmds:
if lm <= 0.05:
quality = 1
else:
quality = 8
net = model_function(quality=quality)
states = torch.load(models_path + str(lm) + 'checkpoint_best_loss.pth.tar')
try:
net.load_state_dict(states['state_dict'])
except Exception as e:
print(e)
net = net.cuda()
net.update()
stainModel=CompressAINetwork_ShapePreserving()
#for run in IndividuallyTrainedStainModel:
states = torch.load(models_path+str(lm)+'StainModelcheckpoint_best_loss.pth.tar')
stainModel.load_state_dict(states['state_dict'])
#stainModel.update()
stainModel.train()
stainModel=stainModel.cuda()
def SQLCCompression(InputImage):
InputImage = InputImage.unsqueeze(0)
InputImage = InputImage.cuda()
with torch.no_grad():
feats, recon = stainModel(InputImage)
reconstructed = net(feats)
with torch.no_grad():
decoded = stainModel.decode(reconstructed['x_hat'])
decoded = decoded[0, 0:3, :, :] / 255
return Image.fromarray(np.asarray((decoded.permute(1,2,0).detach().cpu())*255,dtype=np.uint8))
OutPutBasePath='/Out/SQLC'
os.makedirs(OutPutBasePath,exist_ok=True)
StainConv='IndividualRun'
CompressionModel='Original'
OutPath=os.path.join(OutPutBasePath,str(lm))
os.makedirs(OutPath,exist_ok=True)
transformations=transforms.Compose([transforms.Resize(224),transforms.Lambda(StainDeconvHistomics),transforms.ToTensor(),transforms.Lambda(SQLCCompression)])
path = '/path/to/files' # path to compress segmentation files
files=glob.glob('file/path')### but for perceptual files i need the glob glob operation
#_, _, files = next(os.walk(path))
for file in tqdm(files):
#png_file = Image.open(os.path.join(path, file))
png_file = Image.open(os.path.join(file))# this was added
image=transformations(png_file)
file=os.path.basename(file)##added to get the file name (reason was that due to split in to benign and malignant folders i could not get the file name)
save_name=os.path.join(OutPath,file)
x, y = (224, 224) # 256 map(int, coords.split('_'))
outtransform = transforms.Resize((y, x))
image = outtransform(image)
image.save(save_name)
#CompressSQLC()
#DecodeSQLC()
CompressCAImodels()