# plt.subplot(232) # plt.imshow(b[0, :, :, 0].squeeze()); # plt.subplot(233) # plt.imshow(b[0, :, :, 1].squeeze()); # plt.subplot(234) # plt.imshow(b[0, :, :, 2].squeeze()); # plt.subplot(235) # plt.imshow(b[0, :, :, 3].squeeze()); # plt.subplot(236) # plt.imshow(b[0, :, :, 4].squeeze()); # plt.show() if resumeFrom == None: model = UNet().create_model(img_shape=(img_width, img_height, 3), use_model='cp/crops-65-0.53.hdf5', pop_layers=2) # model = UNet().create_model(img_shape=(img_width, img_height, 3), use_model='VGG16') print_summary(model) # Freeze the first few layers which we don't want to train for layer in model.layers[:16]: layer.trainable = False # compile the model # model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) # model.compile(loss='categorical_crossentropy', optimizer=optimizers.Adadelta(lr=0.1, rho=0.95, epsilon=1e-08, decay=0.0005), metrics=['accuracy']) # model.compile(loss = "categorical_crossentropy", optimizer = optimizers.SGD(lr=0.0001, momentum=0.9), metrics=["accuracy"], decay=0.0005) # model.compile(loss='categorical_crossentropy', # optimizer=optimizers.RMSprop(lr=0.00003, rho=0.9, epsilon=1e-08, decay=0.000001),
help="Do not use the cuda version of the net", default=False) parser.add_argument('--viz', '-v', action='store_true', help="Visualize the images as they are processed", default=False) parser.add_argument('--no-save', '-n', action='store_false', help="Do not save the output masks", default=False) args = parser.parse_args() print("Using model file : {}".format(args.model)) net = UNet(3, 1) if not args.cpu: print("Using CUDA version of the net, prepare your GPU !") net.cuda() else: net.cpu() print("Using CPU version of the net, this may be very slow") in_files = args.input out_files = [] if not args.output: for f in in_files: pathsplit = os.path.splitext(f) out_files.append("{}_OUT{}".format(pathsplit[0], pathsplit[1])) elif len(in_files) != len(args.output): print(
parser.add_option('--lr_step', dest='lr_step', default=10, type='int', help='learning rate multiplied by 0.1 every learning step') parser.add_option('-g', '--gpu', action='store_true', dest='gpu', default=False, help='use cuda') parser.add_option('-c', '--load', dest='load', default=False, help='load file model') parser.add_option('-s', '--scale', dest='scale', type='float', default=0.5, help='downscaling factor of the images') (options, args) = parser.parse_args() return options if __name__ == '__main__': args = get_args() net = UNet(n_channels=3, n_classes=args.num_cls) if args.load: net.load_state_dict(torch.load(args.load)) print('Model loaded from {}'.format(args.load)) if args.gpu: net.cuda() # cudnn.benchmark = True # faster convolutions, but more memory try: train_net(net=net, dir_img = args.img_dir, dir_mask = args.label_dir, dir_checkpoint = args.save_dir, epochs=args.epochs, batch_size=args.batchsize,
import torch import torch.onnx import os from unet import UNet checkpoint_root = './checkpoint/' cuda = torch.cuda.is_available() device = torch.device("cuda" if cuda else "cpu") unet = UNet() unet = unet.to(device) dummy_input = torch.randn(1, 1, 256, 256, device=device) for file in os.listdir(checkpoint_root): if file.startswith("unet") and file.endswith(".tar"): checkpoint = torch.load(checkpoint_root + file, map_location='cpu') unet.load_state_dict(checkpoint['state_dict']) torch.onnx.export(unet, dummy_input, checkpoint_root + "onnx_unet.onnx")
# test defects_dataset_test = custom_dataset(img_path_test, dir_img_mask) defects_dataloader_test = DataLoader(defects_dataset_test, batch_size=4, shuffle=False) ## visualize some sample images # ## get device # device = torch.device("cuda" if torch.cuda.is_available() else "cpu") ## training parameters initialization # model = UNet(in_channels=3, out_channels=1, init_features=32) #https://github.com/mateuszbuda/brain-segmentation-pytorch model = model.to(device) # epoch number num_epochs = 100 # initial learning rate initial_lr = 0.001 # optimizer optimizer = optim.Adam(model.parameters(), lr=initial_lr) # loss function loss_criterion = nn.BCELoss()
default="final", choices=['p', 'checkpoints', 'inference', 'loc_predict', 'final'], help="select an evaluation type") return parser if __name__ == '__main__': parser = default_argument_parser() args = custom_argparse(parser).parse_known_args()[0] cfg = setup(args) print('ready to run 0') print('ready to run 1') # torch.set_default_dtype(torch.float16)f out_channels = 1 if cfg.MODEL.BINARY_CLASSIFICATION else cfg.MODEL.OUT_CHANNELS net = UNet(cfg) print('ready to run 2') if args.resume_from: # TODO Remove this full_model_path = os.path.join(cfg.OUTPUT_DIR, args.resume_from) net.load_state_dict(torch.load(full_model_path)) print('Model loaded from {}'.format(full_model_path)) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print('DEVICE', device) try: if args.eval_type == 'checkpoints': wandb.init( name=cfg.NAME, project='urban_dl',
from utils.dataset import * from utils.myloss import * # set seed points seed_num = 888 torch.manual_seed(seed_num) torch.cuda.manual_seed_all(seed_num) np.random.seed(seed_num) params = Parameters() #################################### # Create Data Generators training_DG, validation_DG, params = get_dataset_generators(params) # Create Model net = UNet(params.n_channels, 1) if params.multi_GPU: net = torch.nn.DataParallel(net, device_ids=params.device_ids).cuda() else: net.to(params.device) optimizer = optim.Adam(net.parameters(), lr=params.args.lr) if not os.path.exists(params.model_save_dir): os.makedirs(params.model_save_dir) if not os.path.exists(params.tensorboard_dir): os.makedirs(params.tensorboard_dir) writer = SummaryWriter(params.tensorboard_dir)
from torch.utils.data import DataLoader from torch.autograd import Variable from torch import Tensor import torchvision import torch from unet import UNet import os import cv2 from dataLoader4 import traindata dataset = traindata('train_image') train_loader = DataLoader(dataset=dataset, batch_size=10, shuffle=True) model = UNet(n_channels=1, n_classes=3) model.cuda() criterion = torch.nn.MSELoss() optimizer = torch.optim.Adam(model.parameters(), lr=1e-1) if os.path.exists('unet4.pth'): model.load_state_dict(torch.load('./unet4.pth')) for t in range(5000): for batch_idx, (data, target) in enumerate(train_loader): data, target = data.type(torch.FloatTensor), target.type( torch.FloatTensor) data, target = data.cuda(), target.cuda() data, target = Variable(data), Variable(target) target_pred = model(data) loss = criterion(target_pred, target)
epoch=epoch + 1) print('Validation dice coeff is %f, loss is %.4f' % (val_dice, val_loss)) if save_model: if not os.path.exists(save_model): os.mkdir(save_model) save_path = os.path.join( save_model, "epoch_%d_dice_%f.pth" % (epoch + 1, val_dice)) torch.save(net.state_dict(), save_path) if __name__ == "__main__": args = get_args() net = UNet(1, 1) if args.load: net.load_state_dict(torch.load(args.load)) print("Load pretrained model from %s" % (args.load)) if args.gpu: net.cuda() data_dir = "data/image" mask_dir = "data/mask" image_size = 512 epochs = args.epochs print("Process training data......")
(options, args) = parser.parse_args() return options if __name__ == '__main__': args = get_args() torch.set_num_threads(1) # im_tags = ['frame_tight_lf0', 'frame_loose_lf0'] #lt im_tags = ['frame_loose_lf0', 'frame_mp2_roi0', 'frame_mp3_roi0'] # l23 # im_tags = ['frame_loose_lf0', 'frame_tight_lf0', 'frame_mp2_roi0', 'frame_mp3_roi0'] # lt23 ma_tags = ['frame_ductor0'] truth_th = 100 net = UNet(len(im_tags), len(ma_tags)) # net = UResNet(len(im_tags), len(ma_tags)) # net = NestedUNet(len(im_tags),len(ma_tags)) if args.load: net.load_state_dict(torch.load(args.load)) print('Model loaded from {}'.format(args.load)) if args.gpu: net.cuda() # cudnn.benchmark = True # faster convolutions, but more memory try: train_net(net=net, im_tags=im_tags, ma_tags=ma_tags,
def main(is_tiny=False, cuda_id=0, n_epochs=1000, batch_size=1, num_workers=1, lr=1e-4, model_name='unet', save_root_dir=RESULTS_DIR): image2save = 4 batchs2save = math.ceil(image2save / batch_size) if model_name == 'unet': model = UNet() elif model_name == 'unet_dilated': model = UNetDilated() else: raise ValueError('Invalid model name {}'.format(model_name)) if torch.cuda.is_available(): print("THIS IS CUDA!!!!") dev_str = "cuda:" + str(cuda_id) print(dev_str) device = torch.device(dev_str) else: device = torch.device('cpu') gen_imgs = TorchDataset(test_split=0.05, pad_size=32, add_cnt_weights=False) gen = DataLoader(gen_imgs, batch_size=batch_size, num_workers=num_workers) log_dir_root = os.path.join(save_root_dir, 'logs') if is_tiny: print("It's me, tiny-log!!!") log_dir_root = os.path.join(save_root_dir, 'tiny_log') train_img_freq = 1000 if is_tiny else len(gen) test_img_freq = 10 if is_tiny else 1 now = datetime.datetime.now() bn = now.strftime('%Y%m%d_%H%M%S') + '_' + model_name if is_tiny: bn += '_tiny' bn = '{}_lr{}_batch{}'.format(bn, lr, batch_size) print(bn) log_dir = os.path.join(log_dir_root, bn) logger = SummaryWriter(log_dir=log_dir) criterion = unet_loss model = model.to(device) optimizer = torch.optim.Adam(model.parameters(), lr=lr) best_loss = 1e10 n_iter = 0 image_train = [] for epoch in range(n_epochs): train_avg_loss = 0 model.train() gen_imgs.train() if is_tiny: gen_imgs.tiny() gen_imgs._is_transform = True pbar_train = tqdm.tqdm(gen) for x_in, y_in in pbar_train: X = x_in.to(device) target = y_in.to(device) del x_in, y_in pred = model(X) target_cropped = _crop(pred, target) loss = criterion(pred, target_cropped) optimizer.zero_grad() # clear gradients for this training step loss.backward() # backpropagation, compute gradients optimizer.step() if n_iter % train_img_freq <= batchs2save: if len(image_train) < batchs2save: dd = X.cpu(), target[:, 0].cpu( )[:, None, :, :], pred[:, 0].cpu()[:, None, :, :] image_train.append(dd) else: xs = torch.cat([torch.cat(x) for x in image_train]) xs = vutils.make_grid(xs, nrow=3, normalize=True, scale_each=True) logger.add_image('train', xs, n_iter) image_train = [] desc = 'Train Epoch {} , loss={}'.format(epoch + 1, loss.item()) pbar_train.set_description(desc=desc, refresh=False) #I prefer to add a point at each iteration since hte epochs are very large logger.add_scalar('train_iter_loss', loss.item(), n_iter) n_iter += 1 train_avg_loss += loss.item() del loss, pred, target_cropped, X, target train_avg_loss /= len(gen) logger.add_scalar('train_epoch_loss', train_avg_loss, epoch) #%% test_avg_loss = 0 with torch.no_grad(): model.eval() gen_imgs.test() if is_tiny: gen_imgs.tiny() gen_imgs._is_transform = False image_test = [] pbar_test = tqdm.tqdm(gen) for x_in, y_in in pbar_test: X = x_in.to(device) target = y_in.to(device) del x_in, y_in pred = model(X) target_cropped = _crop(pred, target) loss = criterion(pred, target_cropped) if len(image_test) < batchs2save: dd = X.cpu(), target[:, 0].cpu( )[:, None, :, :], pred[:, 0].cpu()[:, None, :, :] image_test.append(dd) desc = 'Test Epoch {} , loss={}'.format(epoch + 1, loss.item()) pbar_test.set_description(desc=desc, refresh=False) #I prefer to add a point at each iteration since hte epochs are very large logger.add_scalar('test_iter_loss', loss.item(), n_iter) n_iter += 1 test_avg_loss += loss.item() del loss, pred, target_cropped, X, target if epoch % test_img_freq == 0: xs = torch.cat([torch.cat(x) for x in image_test]) xs = vutils.make_grid(xs, nrow=3, normalize=True, scale_each=True) logger.add_image('test_epoch', xs, epoch) test_avg_loss /= len(gen) logger.add_scalar('test_epoch_loss', test_avg_loss, epoch) #%% save_model is_best = test_avg_loss < best_loss best_loss = min(test_avg_loss, best_loss) state = { 'epoch': epoch, 'state_dict': model.state_dict(), 'best_loss': best_loss, 'optimizer': optimizer.state_dict(), } save_checkpoint(state, is_best, save_dir=log_dir)
return parser.parse_args() def count_params(net): model_parameters = filter(lambda p: p.requires_grad, net.parameters()) params = sum([np.prod(p.size()) for p in model_parameters]) print('params = ', params) if __name__ == "__main__": args = get_args() input_channels = 3 output_channels = 1 net = UNet(input_channels, output_channels) # net = UResNet(input_channels, output_channels) # net = NestedUNet(input_channels, output_channels) count_params(net) example = torch.rand(1, input_channels, 800, 600) if args.gpu: net.cuda() net.load_state_dict(torch.load(args.model)) sm = torch.jit.trace(net, example.cuda()) output = net(example.cuda()) # print(output[0][0][0]) else: net.cpu()
def rle_encode(mask_image): pixels = mask_image.flatten() pixels[0] = 0 pixels[-1] = 0 runs = np.where(pixels[1:] != pixels[:-1])[0] + 2 runs[1::2] = runs[1::2] - runs[:-1:2] return runs def submit(net, device): dir = 'data/test/' N = len(list(os.listdir(dir))) with open('SUBMISSON.csv', 'a') as f: f.write('img,rle_mask\n') for index, i in enumerate(os.listdir(dir)): img = Image.open(dir + i) mask = predict_img(net, img, device) enc = rle_encode(mask) f.write('{},{}\n'.format(i, ' '.join(map(str, enc)))) if index % 1e2 == 0: print('{}/{}'.format(index, N)) if __name__ == '__main__': net = UNet(3, 1).to(device) net.load_state_dict(torch.load('MODEL.pth', map_location=device)) submit(net, device)
transform=test_trans, target_transform=target_trans) data_loaders = {"train": torch.utils.data.DataLoader(train_dataset, batch_size=1, shuffle=True), "val": torch.utils.data.DataLoader(test_dataset, batch_size=1)} """ for i, (data, label) in enumerate(data_loaders["val"]): #print(data.shape, label.shape) save_image(torch.cat([data[:,:1], label]), "test%02d.png" %i, nrow=4)""" # model device = torch.device("cuda") model = {"unet": UNet(3,1).to(device)} # criterion criterion = torch.nn.BCELoss() # optimizer opt = {"adam": torch.optim.Adam(model["unet"].parameters(), lr=0.0001)} # trainer trainer = UnetTrainer(model=model, optimizer=opt, criterion=criterion, device=device, checkpoint="unet_128_aug_bright05_contrast05_hue05_grad.pth") #trainer._debug(data_loaders["val"]) # test if model is loaded # checkpoint setup without loading in initilization #trainer.checkpoint = "unet_128_aug_bright05_contrast05_hue05_grad.pth" #trainer.export()
metavar='FILE', help="Specify the file in which is stored the model" " (default : 'MODEL.pth')") parser.add_argument('--gpu', '-g', action='store_true', help="Use the cuda version of the net", default=False) parser.add_argument('--test', '-t', help="path to test data", default="/data/unagi0/kanayama/dataset/nuclei_images/stage1_test/") parser.add_argument('--save', '-s', help="path to save directory for output masks", default="/data/unagi0/kanayama/dataset/nuclei_images/answer/") args = parser.parse_args() print("Using model file : {}".format(args.model)) net = UNet(3, 1) net_gray = UNet(3, 1) net_color = UNet(3, 1) if args.gpu: print("Using CUDA version of the net, prepare your GPU !") net.cuda() net_gray.cuda() net_color.cuda() else: net.cpu() net_gray.cpu() net_color.cpu() print("Using CPU version of the net, this may be very slow") print("Loading model ...")
def create_model(args, input_shape): # If using CPU or single GPU num_classes = args.out_classes if not num_classes: num_classes = 2 if args.gpus <= 1: if args.net == 'unet': from unet import UNet model = UNet(input_shape, num_classes) return [model] elif args.net == 'tiramisu': from densenets import DenseNetFCN model = DenseNetFCN(input_shape) return [model] elif args.net == 'segcapsr1': from capsnet import CapsNetR1 model_list = CapsNetR1(input_shape) return model_list elif args.net == 'segcapsr3': from capsnet import CapsNetR3 model_list = CapsNetR3(input_shape, args.modalities, num_classes) return model_list elif args.net == 'capsbasic': from capsnet import CapsNetBasic model_list = CapsNetBasic(input_shape) return model_list elif args.net == 'isensee': from isensee import ResidualUnet2D model = ResidualUnet2D(input_shape, args.out_classes) return [model] elif args.net == 'binarycaps': from capsnet import BinaryCapsNetR3 model_list = BinaryCapsNetR3(input_shape, args.out_classes) return model_list else: raise Exception('Unknown network type specified: {}'.format( args.net)) # If using multiple GPUs else: with tf.device("/cpu:0"): if args.net == 'unet': from unet import UNet model = UNet(input_shape) return [model] elif args.net == 'tiramisu': from densenets import DenseNetFCN model = DenseNetFCN(input_shape) return [model] elif args.net == 'segcapsr1': from capsnet import CapsNetR1 model_list = CapsNetR1(input_shape) return model_list elif args.net == 'segcapsr3': from capsnet import CapsNetR3 model_list = CapsNetR3(input_shape, num_classes) return model_list elif args.net == 'capsbasic': from capsnet import CapsNetBasic model_list = CapsNetBasic(input_shape) return model_list elif args.net == 'isensee': from isensee import ResidualUnet2D model = ResidualUnet2D(input_shape, args.out_classes) return [model] else: raise Exception('Unknown network type specified: {}'.format( args.net))
parser = argparse.ArgumentParser() parser.add_argument('--seed', type=int, default=1234) parser.add_argument('--preprocess', type=str, default='7') parser.add_argument('--model', type=str) parser.add_argument('--lesion', type=str, default='EX') args = parser.parse_args() #Set random seed for Pytorch and Numpy for reproducibility random.seed(args.seed) np.random.seed(args.seed) torch.manual_seed(args.seed) if torch.cuda.is_available(): torch.cuda.manual_seed(args.seed) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False model = UNet(n_channels=3, n_classes=2) resume = args.model if os.path.isfile(resume): print("=> loading checkpoint '{}'".format(resume)) checkpoint = torch.load(resume) model.load_state_dict(checkpoint['state_dict']) print('Model loaded from {}'.format(resume)) else: print("=> no checkpoint found at '{}'".format(resume)) model.to(device) test_image_paths, test_mask_paths = get_images(image_dir, args.preprocess,
help='class number') parser.add_argument("--vis", action = 'store_true', help = "Visualizing the Prediction output, only use when predict") # parser.add_argument('--maxepoch', type=int, default=100, # help='Max number of epochs for training') # parser.add_argument('--im_name', type=str, default='.png', # help='Part of image name') return parser.parse_args() if __name__ == "__main__": FLAGS = get_args() if FLAGS.train: netG = UNet(in_channels = 3, out_channels = 8) # netG = nn.DataParallel(netG).to(device) print(netG) pretrained_autoencoder = False if pretrained_autoencoder == True: print("="*6, "\nTrain the network from loading pretrained model.","\n"+"="*6) if os.path.exists("./weight/autoencoder_model.pt") == False: print("An error happened, no model file founded here. Stopped!") sys.exit() if torch.cuda.is_available(): state_dict = torch.load("./weight/autoencoder_model.pt") else: state_dict = torch.load("./weight/autoencoder_model.pt", map_location='cpu')
target_mode=opt.target_mode, colordim=opt.colordim) test_set = get_test_set(opt.size, target_mode=opt.target_mode, colordim=opt.colordim) training_data_loader = DataLoader(dataset=train_set, num_workers=opt.threads, batch_size=opt.batchSize, shuffle=True) testing_data_loader = DataLoader(dataset=test_set, num_workers=opt.threads, batch_size=opt.testBatchSize, shuffle=False) print('===> Building unet') unet = UNet(opt.colordim) criterion = nn.MSELoss() if cuda: unet = unet.cuda() criterion = criterion.cuda() pretrained = True if pretrained: unet.load_state_dict(torch.load(opt.pretrain_net)) optimizer = optim.SGD(unet.parameters(), lr=opt.lr) print('===> Training unet') def train(epoch):
# Change here to adapt to your data # n_channels=3 for RGB images # n_classes is the number of probabilities you want to get per pixel # - For 1 class and background, use n_classes=1 # - For 2 classes, use n_classes=1 # - For N > 2 classes, use n_classes=N model_arch = args.model_arch if args.pretrained != False: args.pretrained = True print('Initializing pretrained model...') n_channels_input = args.input_channels n_channels_output = args.output_channels if model_arch == 'unet': net = UNet(n_channels=n_channels_input, n_classes=n_channels_output, bilinear=True) logging.info( f'Network:\n' f'\t{net.n_channels} input channels\n' f'\t{net.n_classes} output channels (classes)\n' f'\t{"Bilinear" if net.bilinear else "Transposed conv"} upscaling') elif model_arch == 'icnet': net = ICNet(n_channels=n_channels_input, n_classes=n_channels_output, pretrained_base=args.pretrained) logging.info(f'Network:\n' f'\t{net.n_channels} input channels\n' f'\t{net.n_classes} output channels (classes)') else: print(
X_A_dataset = ImageDataset(path='data/trainA/', num_examples=num_examples, transforms=tf) X_B_dataset = ImageDataset(path='data/trainB/', num_examples=num_examples, transforms=tf) dataset = MyDataset(X_A_dataset, X_B_dataset) # Create data loader train_loader = torch.utils.data.DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True) D_A = Discriminator().to(device) G_A = UNet(n_channels=3, n_classes=3).to(device) D_B = Discriminator().to(device) G_B = UNet(n_channels=3, n_classes=3).to(device) criterion = nn.MSELoss() rec_criterion = nn.L1Loss() dA_optimizer = torch.optim.Adam(D_A.parameters(), lr=0.0002) gA_optimizer = torch.optim.Adam(G_A.parameters(), lr=0.0002) dB_optimizer = torch.optim.Adam(D_B.parameters(), lr=0.0002) gB_optimizer = torch.optim.Adam(G_B.parameters(), lr=0.0002) def denorm(x): out = (x + 1) / 2 return out.clamp(0, 1)
n_classes = 3 in_channels = 3 padding = True depth = 5 wf = 2 up_mode = 'upsample' batch_norm = True print("Getting torch device:", flush=True) device = get_torch_device() print("Initializing model:", flush=True) model = UNet(n_classes=n_classes, in_channels=in_channels, padding=padding, depth=depth, wf=wf, up_mode=up_mode, batch_norm=batch_norm, concat=True).to(device) print( f"total params: \t{sum([np.prod(p.size()) for p in model.parameters()])}", flush=True) # from torchsummary import summary # summary(model,(3,256,256)) dr = LayerActivations(model.down_path[-1].block[5]) # + img_transform = Compose([
resize = args.resize batch_size = args.batchsize patch_size = args.patchsize stride_size = patch_size // 2 # ----- load network device = get_torch_device(args.gpuid) checkpoint = torch.load( args.model, map_location=lambda storage, loc: storage ) # load checkpoint to CPU and then put to device https://discuss.pytorch.org/t/saving-and-loading-torch-models-on-2-machines-with-different-number-of-gpu-devices/6666 model = UNet(n_classes=checkpoint["n_classes"], in_channels=checkpoint["in_channels"], padding=checkpoint["padding"], depth=checkpoint["depth"], wf=checkpoint["wf"], up_mode=checkpoint["up_mode"], batch_norm=checkpoint["batch_norm"]).to(device) model.load_state_dict(checkpoint["model_dict"]) model.eval() tta_model = tta.SegmentationTTAWrapper(model, tta.aliases.d4_transform(), merge_mode='mean') print( f"total params: \t{sum([np.prod(p.size()) for p in model.parameters()])}" ) # ----- get file list
import torch.utils.data as data from torch.nn import BCELoss from dataset import FilmDustDataset from unet import UNet from util import save_image, tensor2im _W = 256 _H = 256 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print(device) model = UNet(in_channels=3, wf=3, depth=3, n_classes=1, padding=True, up_mode='upconv', batch_norm=True).to(device) model.load_state_dict(torch.load('weights/w3d3/dust_19.pth'), strict=False) model.to(device) optim = torch.optim.Adam(model.parameters()) filmdust = FilmDustDataset( "/home/zhukov/clients/uk/dustdataset/gimar/256.e4d4/finetune") print(len(filmdust)) dataloader = torch.utils.data.DataLoader(filmdust, batch_size=42, shuffle=True,
return parser.parse_args() if __name__ == '__main__': logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') args = get_args() device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') logging.info(f'Using device {device}') # Change here to adapt to your data # n_channels=3 for RGB images # n_classes is the number of probabilities you want to get per pixel # - For 1 class and background, use n_classes=1 # - For 2 classes, use n_classes=1 # - For N > 2 classes, use n_classes=N net = UNet(n_channels=3, n_classes=21, bilinear=True) logging.info(f'Network:\n' f'\t{net.n_channels} input channels\n' f'\t{net.n_classes} output channels (classes)\n' f'\t{"Bilinear" if net.bilinear else "Transposed conv"} upscaling') if args.load: net.load_state_dict( torch.load(args.load, map_location=device) ) logging.info(f'Model loaded from {args.load}') net.to(device=device) # faster convolutions, but more memory # cudnn.benchmark = True
np.random.seed(1) torch.manual_seed(1) torch.cuda.manual_seed(1) torch.backends.cudnn.deterministic = True torch.cuda.empty_cache() # 加载数据 dataset_test = JsjDataset(opt.dir_img_test, opt.dir_mask_test, do_vi=opt.do_vi) dataloader_test = DataLoader(dataset=dataset_test, batch_size=opt.batchsize, shuffle=True, num_workers=opt.workers) unet = UNet(in_depth=opt.depth).to(opt.device) state = torch.load(r"data\{}\best-unet.pkl".format(opt.name), map_location=opt.device) unet.load_state_dict(state['unet']) loss_func = nn.BCEWithLogitsLoss().to(opt.device) if 'RGB' in opt.name: means = (0.57633764, 0.47007486, 0.3075999) stds = (0.2519291, 0.21737799, 0.17447254) elif 'RGN' in opt.name: means = (0.19842228, 0.15358844, 0.2672494) stds = (0.102274425, 0.07998896, 0.124288246) # 验证 unet.eval() dice_coff = Record()
def get_args(): parser = argparse.ArgumentParser() parser.add_argument('--model', '-m', default='MODEL.pth', metavar='FILE', help="Specify the file in which is stored the model" " (default : 'MODEL.pth')") return parser.parse_args() if __name__ == "__main__": args = get_args() # default `log_dir` is "runs" - we'll be more specific here writer = SummaryWriter('tblog/test1') net = UNet(n_channels=3, n_classes=1) images = torch.rand(2, 3, 800, 600) img_grid = torchvision.utils.make_grid(images) writer.add_image('rand_imgs', img_grid) net.load_state_dict(torch.load(args.model)) writer.add_graph(net, images) writer.close()
def main(): global opt, model opt = parser.parse_args() print(opt) save_path = os.path.join('.', "model", "{}_{}".format(opt.model, opt.ID)) log_dir = './records/{}_{}/'.format(opt.model, opt.ID) if not os.path.exists(save_path): os.makedirs(save_path) if not os.path.exists(log_dir): os.makedirs(log_dir) cuda = opt.cuda if cuda and not torch.cuda.is_available(): raise Exception("No GPU found, please run without --cuda") opt.seed = random.randint(1, 10000) # opt.seed = 4222 coeff_mse = opt.coeff_totalloss coeff_J = opt.coeff_J print("Random Seed: ", opt.seed) cudnn.benchmark = True print("===> Loading datasets") train_set = DatasetFromHdf5(opt.traindata, opt.patchSize, opt.aug) training_data_loader = DataLoader(dataset=train_set, num_workers=opt.threads, batch_size=opt.batchSize, shuffle=True) print("===> Building model") if opt.model == 'drrn': model = DRRN() elif opt.model == 'unet': model = UNet(3) elif opt.model == 'runet': model = RUNet(3) elif opt.model == 'rcunet': model = RCUNet(3, 1) elif opt.model == 'rccunet': model = RCCUNet(1) elif opt.model == 'dense': model = Dense() else: raise ValueError("no known model of {}".format(opt.model)) criterion = nn.MSELoss() Absloss = nn.L1Loss() ssim_loss = pytorch_msssim.MSSSIM() #loss_var = torch.std() if opt.freeze: model.freeze_pretrained() print("===> Setting GPU") if cuda: model = torch.nn.DataParallel(model).cuda() criterion = criterion.cuda() Absloss = Absloss.cuda() ssim_loss = ssim_loss.cuda() #loss_var = loss_var.cuda() vgg = Vgg16(requires_grad=False).cuda() # optionally resume from a checkpoint if opt.resume: if os.path.isfile(opt.resume): print("===> loading checkpoint: {}".format(opt.resume)) checkpoint = torch.load(opt.resume) opt.start_epoch = checkpoint["epoch"] + 1 model.load_state_dict(checkpoint["model"].state_dict()) else: print("===> no checkpoint found at {}".format(opt.resume)) # optionally copy weights from a checkpoint if opt.pretrained: if os.path.isfile(opt.pretrained): pretrained_dict = torch.load(opt.pretrained)['model'].state_dict() print("===> load model {}".format(opt.pretrained)) model_dict = model.state_dict() # filter out unnecessary keys pretrained_dict = { k: v for k, v in pretrained_dict.items() if k in model_dict } print("\t...loaded parameters:") for k, v in pretrained_dict.items(): print("\t\t+{}".format(k)) model_dict.update(pretrained_dict) model.load_state_dict(model_dict) # weights = torch.load(opt.pretrained) # model.load_state_dict(weights['model'].state_dict()) else: print("===> no model found at {}".format(opt.pretrained)) print("===> Setting Optimizer") optimizer = optim.Adam( model.parameters(), lr=opt.lr, weight_decay=opt.weight_decay) #weight_decay=opt.weight_decay print("===> Training") for epoch in range(opt.start_epoch, opt.nEpochs + 1): train(training_data_loader, optimizer, model, criterion, Absloss, ssim_loss, epoch, vgg) save_checkpoint(model, epoch, save_path) # Evaluate validation dataset and save images if epoch % 1 == 0: save_val_path = os.path.join('test', opt.model + '_' + opt.ID) checkdirctexist(save_val_path) image_list = glob.glob(os.path.join(opt.valdataset, '*.png')) for image_name in image_list: print("Processing ", image_name) img = cv2.imread(image_name) img = img.astype(np.float32) H, W, C = img.shape P = 512 print("\t\tBreak image into patches of {}x{}".format(P, P)) Wk = W Hk = H if W % 32: Wk = W + (32 - W % 32) if H % 32: Hk = H + (32 - H % 32) img = np.pad(img, ((0, Hk - H), (0, Wk - W), (0, 0)), 'reflect') im_input = img / 255.0 im_input = np.expand_dims(np.rollaxis(im_input, 2), axis=0) im_input_rollback = np.rollaxis(im_input[0], 0, 3) with torch.no_grad(): im_input = Variable(torch.from_numpy(im_input).float()) im_input = im_input.cuda() model.eval() im_output, _, _, _ = model(im_input, opt) im_output = im_output.cpu() im_output_forsave = get_image_for_save(im_output) #A_output = A.cpu() #A_output_forsave = get_image_for_save(A_output) #T_output = T.cpu() #T_output_forsave = get_image_for_save(T_output) #im_d_output = im_d.cpu() #im_d_output_forsave = get_image_for_save(im_d_output) path, filename = os.path.split(image_name) im_output_forsave = im_output_forsave[0:H, 0:W, :] #A_output_forsave = A_output_forsave[0:H, 0:W, :] #T_output_forsave = T_output_forsave[0:H, 0:W, :] #im_d_output_forsave = im_d_output_forsave[0:H, 0:W, :] cv2.imwrite( os.path.join(save_val_path, "{}_IM_{}".format(epoch, filename)), im_output_forsave)
num_workers=50) data_loader_val = DataLoader(Data_val, batch_size=BATCH_SIZE, shuffle=True, num_workers=50) X_batch, Y_batch = next(iter(data_loader_val)) print("\n\nOne Batch : ", type(X_batch), X_batch.shape, X_batch.numpy().max(), X_batch.numpy().min(), Y_batch.shape) # mim.imsave("Outputs/test.png",X_batch) # mim.imsave("Outputs/test_flip.png",Y_batch) # print(a) model = UNet(in_channels=3, out_channels=3, only_encode=False) #def weights_init(m): # if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d) or isinstance(m, nn.Linear): # nn.init.xavier_uniform_(m.weight.data) #nn.init.xavier_uniform_(m.bias.data) #model.apply(weights_init) ''' # #Data Parallel --------------------------------------------- def get_ifname(): return ifcfg.default_interface()["device"] if "GLOO_SOCKET_IFNAME" not in os.environ: os.environ["GLOO_SOCKET_IFNAME"] = get_ifname()
def main(): parser = ArgumentParser() arg = parser.add_argument arg('--model', default='M2UNet', type=str, choices=['M2UNet', 'DRIU', 'ERFNet', 'UNet']) arg('--state_dict', default='M2UNetDRIVE.pth', type=str, help='pretrained model weights file, stored in models') arg('--dataset', default='DRIVE', choices=['DRIVE', 'CHASE_DB1', 'HRF'], type=str, help= 'determines the dataset directory and the amount of cropping that is performed to ensure that the loaded images are multiples of 32.' ) arg('--threshold', default=0.5215, type=float, help='threshold to convert probability vessel map to binary map') arg('--devicename', default='cpu', type=str, help='device type, default: "cpu"') arg('--batch_size', default=1, type=int, help='inference batch size, default: 1') arg('--save_prob', default=False, help='save probability vessel maps to disk') arg('--save_binary_mask', default=False, help='save binary mask to disk') # Paths model_path = Path('models') data_path = Path('data') log_path = Path('logs') # parse arguments args = parser.parse_args() state_dict_path = model_path.joinpath(args.state_dict) dataset_path = data_path.joinpath(args.dataset) image_file_path = dataset_path.joinpath('test/images') prediction_output_path = dataset_path.joinpath('predictions') threshold = args.threshold devicename = args.devicename batch_size = args.batch_size dataset = args.dataset save_prob = args.save_prob save_binary_mask = args.save_binary_mask # default device type is 'cuda:0' pin_memory = True cudnn.benchmark = True device = torch.device(devicename) if devicename == 'cpu': # if run on cpu, disable cudnn benchmark and do not pin memory cudnn.benchmark = False pin_memory = False # only run on one core torch.set_num_threads(1) if args.model == 'M2UNet': model = m2unet() if args.model == 'DRIU' and dataset == 'DRIVE': model = DRIU() if args.model == 'DRIU' and dataset == 'CHASE_DB1': model = DRIU() if args.model == 'ERFNet': model = ERFNet(1) if args.model == 'UNet': model = UNet(in_channels=3, n_classes=1, depth=5, wf=6, padding=True, batch_norm=False, up_mode='upconv') state_dict = torch.load(str(state_dict_path), map_location=devicename) model.load_state_dict(state_dict, strict=True) model.eval() # list of all files include path file_paths = get_file_lists(image_file_path) # list of file names only file_names = list(map(lambda x: x.stem, file_paths)) # dataloader dataloader = DataLoader(dataset=RetinaDataset(file_paths, dataset), batch_size=batch_size, shuffle=False, num_workers=1, pin_memory=pin_memory) run(model, dataloader, batch_size, threshold, device, save_prob, save_binary_mask, prediction_output_path, file_names)