text stringlengths 1 93.6k |
|---|
"""
|
Training implementation
|
Author: Ian Ren
|
Update time: 08/11/2020
|
"""
|
import re
|
import sys
|
import os
|
import cv2
|
import time
|
import numpy as np
|
import torch
|
import torch.nn as nn
|
import torch.backends.cudnn as cudnn
|
from torch.optim import lr_scheduler
|
import torch.optim as optim
|
import torchvision
|
import torchvision.transforms as transforms
|
from torch.utils.data import DataLoader
|
from read_data import ChestXrayDataSet
|
from sklearn.metrics import roc_auc_score
|
from skimage.measure import label
|
from model import Densenet121_AG, Fusion_Branch
|
from PIL import Image
|
#np.set_printoptions(threshold = np.nan)
|
CKPT_PATH = ''
|
CKPT_PATH_G = '/best_model/AG_CNN_Global_epoch_1.pkl'
|
CKPT_PATH_L = '/best_model/AG_CNN_Local_epoch_2.pkl'
|
CKPT_PATH_F = '/best_model/AG_CNN_Fusion_epoch_23.pkl'
|
N_CLASSES = 14
|
CLASS_NAMES = [ 'Atelectasis', 'Cardiomegaly', 'Effusion', 'Infiltration', 'Mass', 'Nodule', 'Pneumonia',
|
'Pneumothorax', 'Consolidation', 'Edema', 'Emphysema', 'Fibrosis', 'Pleural_Thickening', 'Hernia']
|
# load with your own dataset path
|
DATA_DIR = '/media/xxxx/data/xxxx/images'
|
TRAIN_IMAGE_LIST = '/labels/train_list.txt'
|
VAL_IMAGE_LIST = '/labels/val_list.txt'
|
save_model_path = '/model-AG-CNN/'
|
save_model_name = 'AG_CNN'
|
# learning rate
|
LR_G = 1e-8
|
LR_L = 1e-8
|
LR_F = 1e-3
|
num_epochs = 50
|
BATCH_SIZE = 32
|
normalize = transforms.Normalize(
|
mean=[0.485, 0.456, 0.406],
|
std=[0.229, 0.224, 0.225]
|
)
|
preprocess = transforms.Compose([
|
transforms.Resize((256,256)),
|
transforms.CenterCrop(224),
|
transforms.ToTensor(),
|
normalize,
|
])
|
def Attention_gen_patchs(ori_image, fm_cuda):
|
# feature map -> feature mask (using feature map to crop on the original image) -> crop -> patchs
|
feature_conv = fm_cuda.data.cpu().numpy()
|
size_upsample = (224, 224)
|
bz, nc, h, w = feature_conv.shape
|
patchs_cuda = torch.FloatTensor().cuda()
|
for i in range(0, bz):
|
feature = feature_conv[i]
|
cam = feature.reshape((nc, h*w))
|
cam = cam.sum(axis=0)
|
cam = cam.reshape(h,w)
|
cam = cam - np.min(cam)
|
cam_img = cam / np.max(cam)
|
cam_img = np.uint8(255 * cam_img)
|
heatmap_bin = binImage(cv2.resize(cam_img, size_upsample))
|
heatmap_maxconn = selectMaxConnect(heatmap_bin)
|
heatmap_mask = heatmap_bin * heatmap_maxconn
|
ind = np.argwhere(heatmap_mask != 0)
|
minh = min(ind[:,0])
|
minw = min(ind[:,1])
|
maxh = max(ind[:,0])
|
maxw = max(ind[:,1])
|
# to ori image
|
image = ori_image[i].numpy().reshape(224,224,3)
|
image = image[int(224*0.334):int(224*0.667),int(224*0.334):int(224*0.667),:]
|
image = cv2.resize(image, size_upsample)
|
image_crop = image[minh:maxh,minw:maxw,:] * 256 # because image was normalized before
|
image_crop = preprocess(Image.fromarray(image_crop.astype('uint8')).convert('RGB'))
|
img_variable = torch.autograd.Variable(image_crop.reshape(3,224,224).unsqueeze(0).cuda())
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.