Lewislou commited on
Commit
e11c9e4
1 Parent(s): 367aed9

Upload sribd_cellseg_models.py

Browse files
Files changed (1) hide show
  1. sribd_cellseg_models.py +100 -0
sribd_cellseg_models.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ join = os.path.join
4
+ import argparse
5
+ import numpy as np
6
+ import torch
7
+ import torch.nn as nn
8
+ from collections import OrderedDict
9
+ from torchvision import datasets, models, transforms
10
+ from classifiers import resnet10, resnet18
11
+
12
+ from utils_modify import sliding_window_inference,sliding_window_inference_large,__proc_np_hv
13
+ from PIL import Image
14
+ import torch.nn.functional as F
15
+ from skimage import io, segmentation, morphology, measure, exposure
16
+ import tifffile as tif
17
+ from models.flexible_unet_convnext import FlexibleUNet_star,FlexibleUNet_hv
18
+ from transformers import PretrainedConfig
19
+ from typing import List
20
+ from transformers import PreTrainedModel
21
+ from huggingface_hub import PyTorchModelHubMixin
22
+ from torch import nn
23
+ class ModelConfig(PretrainedConfig):
24
+ model_type = "cell_sribd"
25
+ def __init__(
26
+ self,
27
+ version = 1,
28
+ input_channels: int = 3,
29
+ roi_size: int = 512,
30
+ overlap: float = 0.5,
31
+ device: str = 'cpu',
32
+ **kwargs,
33
+ ):
34
+
35
+ self.device = device
36
+ self.roi_size = (roi_size, roi_size)
37
+ self.input_channels = input_channels
38
+ self.overlap = overlap
39
+ self.np_thres, self.ksize, self.overall_thres, self.obj_size_thres = 0.6, 15, 0.4, 100
40
+ self.n_rays = 32
41
+ self.sw_batch_size = 4
42
+ self.num_classes= 4
43
+ self.block_size = 2048
44
+ self.min_overlap = 128
45
+ self.context = 128
46
+ super().__init__(**kwargs)
47
+
48
+
49
+ class MultiStreamCellSegModel(PreTrainedModel):
50
+ config_class = ModelConfig
51
+ #print(config.input_channels)
52
+ def __init__(self, config):
53
+ super().__init__(config)
54
+ #print(config.input_channels)
55
+ self.config = config
56
+ self.cls_model = resnet18()
57
+ self.model0 = FlexibleUNet_star(in_channels=config.input_channels,out_channels=config.n_rays+1,backbone='convnext_small',pretrained=False,n_rays=config.n_rays,prob_out_channels=1,)
58
+ self.model1 = FlexibleUNet_star(in_channels=config.input_channels,out_channels=config.n_rays+1,backbone='convnext_small',pretrained=False,n_rays=config.n_rays,prob_out_channels=1,)
59
+ self.model2 = FlexibleUNet_star(in_channels=config.input_channels,out_channels=config.n_rays+1,backbone='convnext_small',pretrained=False,n_rays=config.n_rays,prob_out_channels=1,)
60
+ self.model3 = FlexibleUNet_hv(in_channels=config.input_channels,out_channels=2+2,backbone='convnext_small',pretrained=False,n_rays=2,prob_out_channels=2,)
61
+ self.preprocess=transforms.Compose([
62
+ transforms.Resize(size=256),
63
+ transforms.CenterCrop(size=224),
64
+ transforms.ToTensor(),
65
+ transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225])])
66
+ def load_checkpoints(self,checkpoints):
67
+ self.cls_model.load_state_dict(checkpoints['cls_model'])
68
+ self.model0.load_state_dict(checkpoints['class1_model']['model_state_dict'])
69
+ self.model1.load_state_dict(checkpoints['class2_model']['model_state_dict'])
70
+ self.model2.load_state_dict(checkpoints['class3_model']['model_state_dict'])
71
+ self.model3.load_state_dict(checkpoints['class4_model'])
72
+
73
+ def forward(self, pre_img_data):
74
+ inputs=self.preprocess(Image.fromarray(pre_img_data)).unsqueeze(0)
75
+ outputs = self.cls_model(inputs)
76
+ _, preds = torch.max(outputs, 1)
77
+ label=preds[0].cpu().numpy()
78
+ test_npy01 = pre_img_data
79
+ if label in [0,1,2]:
80
+ if label == 0:
81
+ output_label = sliding_window_inference_large(test_npy01,self.config.block_size,self.config.min_overlap,self.config.context, self.config.roi_size,self.config.sw_batch_size,predictor=self.model0,device=self.config.device)
82
+ elif label == 1:
83
+ output_label = sliding_window_inference_large(test_npy01,self.config.block_size,self.config.min_overlap,self.config.context, self.config.roi_size,self.config.sw_batch_size,predictor=self.model1,device=self.config.device)
84
+ elif label == 2:
85
+ output_label = sliding_window_inference_large(test_npy01,self.config.block_size,self.config.min_overlap,self.config.context, self.config.roi_size,self.config.sw_batch_size,predictor=self.model2,device=self.config.device)
86
+ else:
87
+ test_tensor = torch.from_numpy(np.expand_dims(test_npy01, 0)).permute(0, 3, 1, 2).type(torch.FloatTensor)
88
+
89
+ output_hv, output_np = sliding_window_inference(test_tensor, self.config.roi, self.config.sw_batch_size, self.model3, overlap=self.config.overlap,device=self.config.device)
90
+ pred_dict = {'np': output_np, 'hv': output_hv}
91
+ pred_dict = OrderedDict(
92
+ [[k, v.permute(0, 2, 3, 1).contiguous()] for k, v in pred_dict.items()] # NHWC
93
+ )
94
+ pred_dict["np"] = F.softmax(pred_dict["np"], dim=-1)[..., 1:]
95
+ pred_output = torch.cat(list(pred_dict.values()), -1).cpu().numpy() # NHW3
96
+ pred_map = np.squeeze(pred_output) # HW3
97
+ pred_inst = __proc_np_hv(pred_map, self.config.np_thres, self.config.ksize, self.config.overall_thres, self.config.obj_size_thres)
98
+ raw_pred_shape = pred_inst.shape[:2]
99
+ output_label = pred_inst
100
+ return output_label