from skimage.transform import resize from tqdm import tqdm from sklearn.model_selection import train_test_split from skimage.io import imread, imshow from skimage.transform import resize seed = 42 # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # model = pytorch_unet.UNet(1) # model = model.float() # model = model.to(device) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print(device) num_class = 1 model = pytorch_unet.UNet(num_class) ################################ # use multiple gpus if torch.cuda.device_count() > 1: print("Let's use", torch.cuda.device_count(), "GPUs!") # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs model = nn.DataParallel(model) ################################ model = model.to(device) summary(model, input_size=(3, 256, 256)) # class SimDataset(Dataset): # def __init__(self, count, transform=None): # self.input_images, self.target_masks = simulation.generate_random_data(192, 192, count=count) # self.transform = transform #
batch_size = 1 dataloaders = { 'train': DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=0), 'val': DataLoader(val_set, batch_size=batch_size, shuffle=True, num_workers=0) } dataset_sizes = { x: len(image_datasets[x]) for x in image_datasets.keys() } device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') model = pytorch_unet.UNet(pred_frame,inp_channel=inp_seq) #model = unet_ds.UNetDS5(pred_frame,inp_channel=inp_seq) #model = torch.load("mosdac.ckpt") def images_to_probs(net, images): ''' Generates predictions and corresponding probabilities from a trained network and a list of images ''' output = net(images) # convert output probabilities to predicted class preds = np.squeeze(output.detach().cpu().numpy()) if preprocess == 'norm': preds = preds*std_value + mean_value
# Get a batch of training data inputs, masks = next(iter(dataloaders['train'])) print(inputs.shape, masks.shape) for x in [inputs.numpy(), masks.numpy()]: print(x.min(), x.max(), x.mean(), x.std()) plt.imshow(reverse_transform(inputs[1])) #plt.show() plt.clf() device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = pytorch_unet.UNet(6) model = model.to(device) summary(model, input_size=(3, 224, 224)) def calc_loss(pred, target, metrics, bce_weight=0.5): bce = F.binary_cross_entropy_with_logits(pred, target) pred = torch.sigmoid(pred) dice = dice_loss(pred, target) loss = bce * bce_weight + dice * (1 - bce_weight) metrics['bce'] += bce.data.cpu().numpy() * target.size(0) metrics['dice'] += dice.data.cpu().numpy() * target.size(0)
from tqdm import tqdm import pytorch_unet import torch import matplotlib.pyplot as plt from skimage.io import imread, imshow from skimage.transform import resize import torch.nn.functional as F import torch.nn as nn from torchsummary import summary ######################### # load the saved model device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') model_path = "./model" model = pytorch_unet.UNet(1) # model = model.float() # model = model.to(device) # model = nn.DataParallel(model) model = model.to(device) # model.load_state_dict(torch.load(os.path.join(model_path,os.listdir(model_path)[-1]))) model.load_state_dict( torch.load( "/home/leejianglee/2020_05_segmentation/pytorch-unet/model/model_1591703530.5789328.pth" )) # print("load the model {}".format(os.listdir(model_path)[-1])) summary(model, input_size=(3, 512, 512)) ######################## #######################