def eval(cfg): # Setup seeds torch.manual_seed(cfg.get("seed", 1337)) torch.cuda.manual_seed(cfg.get("seed", 1337)) np.random.seed(cfg.get("seed", 1337)) random.seed(cfg.get("seed", 1337)) # Setup device device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Setup evaluation data loader_source = get_loader(cfg, "train") # data_eval_labels = utils.recursive_glob(os.path.join(cfg["data"]["path"], 'labels')) # Setup model model = Unet(cfg).to(device) checkpoint = torch.load(cfg["training"]["checkpoint"]) model.load_state_dict(checkpoint["model_state"]) stats = None model.eval() for images, labels in tqdm.tqdm(loader_source): model.set_input(images, labels) model.forward() if stats is None: stats = [StatsRecorder() for i in range(len(model.hooks))] for i, hook in enumerate(model.hooks): activation = hook.output b, c, h, w = activation.shape activation = activation.transpose(0, 1).reshape(c, -1).transpose(0, 1) stats[i].update(activation.cpu().data.numpy()) print([s.mean for s in stats]) print([s.std for s in stats])
def main(args): device = 'cuda' if torch.cuda.is_available() else 'cpu' d = dcrf.DenseCRF2D(512, 512, 2) down_method = args.down_method up_method = args.up_method separable = args.separable ds = DataSetWrapper(args.batch_size, args.num_workers, 0.2) test_dl = ds.get_data_loaders(train=False) model = Unet(input_dim=1, separable=True, down_method='conv', up_method='transpose') model = nn.DataParallel(model).to(device) load_state = torch.load(f'./checkpoint/conv_transpose_True.ckpt') model.load_state_dict(load_state['model_state_dict']) model.eval() name = 0 with torch.no_grad(): for (img, label) in test_dl: imgs, labels = img.to(device), label.to(device) preds = model(img) name += 1 for i in range(args.batch_size): img, label, pred = imgs[i, :], labels[i, :], preds[i, :] probs = torch.stack([1 - pred, pred], dim=0).cpu().numpy() img, label = img.cpu().numpy(), label.cpu().numpy() pairwise_energy = create_pairwise_bilateral(sdims=(10, 10), schan=(0.01, ), img=img, chdim=0) U = unary_from_softmax(probs) d = dcrf.DenseCRF2D(512, 512, 2) d.setUnaryEnergy(U) d.addPairwiseEnergy(pairwise_energy, compat=10) Q = d.inference(100) map = np.argmax(Q, axis=0).reshape((512, 512)) print(map.shape) img = (255. / img.max() * (img - img.min())).astype(np.uint8) label = (255. / label.max() * (label - label.min())).astype( np.uint8) pred = (255. / map.max() * (map - map.min())).astype(np.uint8) img = Image.fromarray(img[0, :], mode='L') label = Image.fromarray(label[0, :], mode='L') pred = Image.fromarray(pred, mode='L') img.save(f'./results/{name}_{i}_i.png') label.save(f'./results/{name}_{i}_l.png') pred.save(f'./results/{name}_{i}_p.png')
def main(args): device = 'cuda' if torch.cuda.is_available() else 'cpu' separable = args.separable up_method = args.up_method down_method = args.down_method img_src = args.s ######## Model Setting ######## model = Unet(input_dim=1, separable=separable, down_method=down_method, up_method=up_method) model = nn.DataParallel(model).to(device) load_state = torch.load( f'./checkpoint/{down_method}_{up_method}_{separable}.ckpt') model.load_state_dict(load_state['model_state_dict']) model.eval() ############################### d = dcrf.DenseCRF2D(512, 512, 2) img = Image.open(img_src).convert('L') img = pad_resize(img, 512) img = TF.to_tensor(img) img = img.unsqueeze(0) with torch.no_grad(): img = img.to(device) pred = model(img) probs = torch.stack([1 - pred, pred], dim=0).cpu().numpy() img, pred = img.cpu().numpy(), pred.cpu().numpy() pairwise_energy = create_pairwise_bilateral(sdims=(10, 10), schan=(0.01, ), img=img, chdim=0) U = unary_from_softmax(probs) d.setUnaryEnergy(U) d.addPairwiseEnergy(pairwise_energy, compat=10) Q = d.inference(100) map = np.argmax(Q, axis=0).reshape((512, 512)) img = (255. / img.max() * (img - img.min())).astype(np.uint8) pred = (255. / map.max() * (map - map.min())).astype(np.uint8) img = Image.fromarray(np.squeeze(img), mode='L') pred = Image.fromarray(pred, mode='L') img.save(f'../similarity/{img_src[:-4]}_o.png') pred.save(f'../similarity/{img_src[:-4]}_p.png')
def test(args): my_dataset = MyDataset("../data/val", transform=x_transforms, target_transform=y_transforms) dataloaders = DataLoader(my_dataset, batch_size=1) model = Unet(3, 1) model.load_state_dict(torch.load(args.model_path, map_location='cpu')) model.eval() plt.ion() with torch.no_grad(): count = 0 for x, _ in dataloaders: y = model(x) img_y = torch.squeeze(y).numpy() predict_path = os.path.join("../data/predict/", "%03d_predict.png" % count) plt.imsave(predict_path, img_y) plt.imshow(img_y) plt.pause(0.1) count += 1 plt.show()
def predict(image_path, checkpoint_path, save_path): model = Unet() device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model.load_state_dict(torch.load(checkpoint_path, map_location=device)) image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) size = image.shape image = Compose([ ToTensor(), Resize((512, 512)), ])(image) image = image.unsqueeze(0) mask = model(image)[0] mask[mask < 0.5] = 0 mask[mask > 0.5] = 255 mask = Resize(size)(mask) mask = mask.detach().numpy() cv2.imwrite('result.png', mask[0]) pass
testset = DataLoader( TestDataset(test_data_folder, df, mean, std), batch_size=batch_size, shuffle=False, num_workers=num_workers, pin_memory=True ) # Initialize mode and load trained weights ckpt_path = "../input/res18ex6/model26.pth" device = torch.device("cuda") model = Unet("resnet18", encoder_weights=None, classes=4, activation=None) model.to(device) model.eval() state = torch.load(ckpt_path, map_location=lambda storage, loc: storage) model.load_state_dict(state["state_dict"]) # start prediction predictions = [] for i, batch in enumerate(tqdm(testset)): fnames, images = batch batch_preds = torch.sigmoid(model(images.to(device))) batch_preds = batch_preds.detach().cpu().numpy() for fname, preds in zip(fnames, batch_preds): for cls, pred in enumerate(preds): pred, num = post_process(pred, best_threshold, min_size) rle = mask2rle(pred) name = fname + f"_{cls+1}" predictions.append([name, rle]) # save predictions to submission.csv
model.load_state_dict(new_state_dict) return model if __name__ == "__main__": matches = [100, 200, 300, 400, 500, 600, 700, 800] dir_checkpoint = 'checkpoints/' device = torch.device('cuda:1' if torch.cuda.is_available() else 'cpu') #net = Unet(n_channels=3, n_classes=8, bilinear=True) net = Unet(n_channels=3, n_classes=8) net.to(device=device) #net=load_GPUS(net, dir_checkpoint + 'best_score_model_res50_deeplabv3+.pth', kwargs) checkpoint = torch.load(dir_checkpoint + 'student_net.pth', map_location=device) net.load_state_dict(checkpoint['net']) logging.info("Model loaded !") list_path = "data/test.lst" output_path = "data/results/" img_list = [line.strip('\n') for line in open(list_path)] for i, fn in tqdm(enumerate(img_list)): save_img = np.zeros((256, 256), dtype=np.uint16) logging.info("\nPredicting image {} ...".format(i)) img = Image.open(fn) pre, _ = predict_img(net, img, device) for i in range(256): for j in range(256): save_img[i][j] = matches[int(pre[i][j])] index = fn.split("/")[-1].split(".")[0] cv2.imwrite(os.path.join(output_path, index + ".png"), save_img)
train_loader = data.DataLoader(dataset=train_data, batch_size=batch_size, shuffle=True, drop_last=True, num_workers=4) generator = define_G( 4, 1, 64, 'unet_128', norm='instance', ) discriminator = netD() unet = Unet() unet.load_state_dict(torch.load("./weight/unet_pretrained.pth")) optimizer_g = torch.optim.Adam(generator.parameters(), lr=0.0002) optimizer_d = torch.optim.Adam(discriminator.parameters(), lr=0.0002) optimizer_s = torch.optim.Adam(unet.parameters(), lr=0.0002) generator.cuda() discriminator.cuda() unet.cuda() EPOCH = 100 num_iter = len(train_loader) D_LOSS = [] G_LOSS = [] # S_LOSS=[] f = open("./loss_gan.txt", 'a') print(time.strftime('|---------%Y-%m-%d %H:%M:%S---------|',
def load_GPUS(model, model_path, kwargs): state_dict = torch.load(model_path, **kwargs) # create new OrderedDict that does not contain `module.` from collections import OrderedDict new_state_dict = OrderedDict() for k, v in state_dict['net'].items(): name = k[7:] # remove `module.` new_state_dict[name] = v # load params model.load_state_dict(new_state_dict) return model if __name__ == "__main__": dir_checkpoint = 'checkpoints/' device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') net = Unet(n_channels=3, n_classes=8, bilinear=True) net.to(device=device) model = torch.load(dir_checkpoint + 'best_score_model_unet.pth') net.load_state_dict(model['net']) #net = load_GPUS(net, dir_checkpoint + 'student_net.pth', kwargs) sate_dataset_val = BasicDataset("./data/val.lst") eval_dataloader = DataLoader(sate_dataset_val, batch_size=32, shuffle=True, num_workers=5, drop_last=True) print("begin") eval_net(net, eval_dataloader, device)
import numpy as np from PIL import Image import matplotlib.pyplot as plt import warnings warnings.filterwarnings("ignore") loader = transforms.Compose([transforms.ToTensor()]) model = Unet(3, 1) """ 加载模型 """ try: checkpoint = torch.load('/weights/face_weights.pth', map_location='cpu') model.load_state_dict(checkpoint) #start_epoch = checkpoint['epoch'] #print("start_epoch:",start_epoch) print('===> Load last checkpoint data') except FileNotFoundError: print('Can\'t found weight.pth') cuda = torch.cuda.is_available() if (cuda): model.cuda() def clean(img): img[img < 0] = 0 img[img > 0] = 1 return img
.format(channels[ic], precision[ic, ith], recall[ic, ith], f1[ic, ith], ths[ith])) print('Testing completed.') if __name__ == '__main__': device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') args = get_args() os.makedirs(args.result_path, exist_ok=True) from data import HistoDataset testls_file = 'test_list.txt' test_set = HistoDataset(testls_file, phase='test') dataloader = torch.utils.data.DataLoader(test_set, batch_size=1, shuffle=False) from model import Unet model = Unet(args.in_channels, args.out_channels) model.load_state_dict(torch.load(args.model_file)) from criteria import prf, dice, multichannel_prcurve try: test_model(model, dataloader, multichannel_prcurve) except KeyboardInterrupt: sys.exit(0)
dataloaders['train'] = torch.utils.data.DataLoader( train_set, batch_size=args.batch_size, shuffle=True, num_workers=8) dataloaders['val'] = torch.utils.data.DataLoader(val_set, batch_size=1, shuffle=True, num_workers=8) from model import Unet model = Unet(args.in_channels, args.out_channels) if args.load_caffe_unet: caffe_unet_path = '/mnt/ccvl15/yixiao/kaggle/models/pretrained/unet.pt' unet_caffemodel_weights = torch.load(caffe_unet_path) model = load_conv_weights(model, unet_caffemodel_weights) elif args.load != '': model.load_state_dict(torch.load(args.load)) optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=0.9) from losses import cross_entropy_with_soft_dice_2 # loss_func = torch.nn.CrossEntropyLoss() from criteria import dice, multichannel_dice try: model, val_history = train_model(model, dataloaders, cross_entropy_with_soft_dice_2, multichannel_dice, optimizer, args.epochs) except KeyboardInterrupt: sys.exit(0)
batch_size = 8 is_gpu = torch.cuda.is_available() device = torch.device('cuda' if is_gpu else 'cpu') # config print('args:' + str(args)) print('isgpu?:' + str(is_gpu)) # print config r = Readdata(path) test_set = Cloudset(r.sub, 'test', r.test_ids, r.test_fold, validation_augmentation_kaggle()) test_loader = DataLoader(test_set, batch_size=batch_size, shuffle=False) print('testing data loaded') net = Unet(3, 4).float() if is_gpu: net.cuda() net.load_state_dict(torch.load(model_id, map_location=device)) print('model loaded') # prepare ans_dict = global_dict() image_uid = 0 answer_label = [] answer_index = ['Image_Label', 'EncodedPixels'] # calculation t_bar = tq(test_loader) net.eval() with torch.no_grad(): for img, masks in t_bar: if is_gpu: img = img.cuda() masks_pr = net(img).cpu().detach().numpy() for batch in masks_pr:
def train_net(image_size, batch_size, num_epochs, lr, num_workers, checkpoint): train_loader, val_loader = data_loaders(image_size=(image_size, image_size), batch_size=batch_size) device = torch.device('cuda') if torch.cuda.is_available() else 'cpu' model = Unet().to(device) if checkpoint: model.load_state_dict(torch.load(checkpoint)) criterion = DiceLoss().to(device) optimizer = Adam(model.parameters(), lr=lr) logging.info(f'Start training:\n' f'Num epochs: {num_epochs}\n' f'Batch size: {batch_size}\n' f'Learning rate: {lr}\n' f'Num workers: {num_workers}\n' f'Scale image size: {image_size}\n' f'Device: {device}\n' f'Checkpoint: {checkpoint}\n') train_losses = [] val_losses = [] for epoch in range(num_epochs): print(f'Epoch {epoch+1}: ') train_batch_losses = [] val_batch_losses = [] best_val_loss = 9999 for x_train, y_train in tqdm(train_loader): x_train = x_train.to(device) y_train = y_train.to(device) y_pred = model(x_train) optimizer.zero_grad() loss = criterion(y_pred, y_train) train_batch_losses.append(loss.item()) loss.backward() optimizer.step() train_losses.append(sum(train_batch_losses) / len(train_batch_losses)) print( f'-----------------------Train loss: {train_losses[-1]} -------------------------------' ) for x_val, y_val in tqdm(val_loader): x_val = x_val.to(device) y_val = y_val.to(device) y_pred = model(x_val) loss = criterion(y_pred, y_val) val_batch_losses.append(loss.item()) val_losses.append(sum(val_batch_losses) / len(val_batch_losses)) print( f'-----------------------Val loss: {val_losses[-1]} -------------------------------' ) if val_losses[-1] < best_val_loss: best_val_loss = val_losses[-1] if not os.path.isdir('weights/'): os.mkdir('weights/') torch.save(model.state_dict(), f'weights/checkpoint{epoch+1}.pth') print(f'Save checkpoint in: weights/checkpoint{epoch+1}.pth')
from myutils import transform from model import Unet, Unet_SpatialPyramidPooling, classifier torch.cuda.manual_seed(777) torch.manual_seed(777) img_folder = '../dataset-EdmCrack600-512/A/val/' img_dir = os.listdir(img_folder) img_list = [img_folder + k for k in img_dir] img_list.sort() unet = Unet(3).cuda() SC_classifier = classifier(64, 2).cuda() RC_classifier = classifier(64, 2).cuda() unet.load_state_dict(torch.load('trained_models/unet_adam_dice_58.pkl')) SC_classifier.load_state_dict( torch.load('trained_models/SC_classifier_adam_dice_58.pkl')) RC_classifier.load_state_dict( torch.load('trained_models/RC_classifier_adam_dice_58.pkl')) total_time = 0 i = 0 for file in img_list: img = Image.open(file).resize([512, 512]) img = transform(img).cuda().unsqueeze(0) start_time = time.time() with torch.no_grad(): pred = unet(img) pred = RC_classifier(pred) elapsed_time = time.time() - start_time
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') teach_model = deeplabv3plus_resnet50(num_classes=8, output_stride=16) teach_model.to(device=device) teach_model.load_state_dict( torch.load('./checkpoints/0.7669_best_score_model_res50_deeplabv3+.pth', map_location=device)['net']) model = Unet(n_channels=3, n_classes=8) model.to(device=device) #model_dir = './checkpoints/best_score_model_unet.pth' model_dir = './checkpoints/student_net.pth' if os.path.exists(model_dir): #model = load_GPUS(model_dir, model_dir, kwargs) model.load_state_dict(torch.load(model_dir)['net']) print("loading model sccessful----" + model_dir) #model.load_state_dict(torch.load('teach_net_params_0.9895.pkl')) criterion = nn.CrossEntropyLoss() criterion2 = nn.KLDivLoss() optimizer = optim.Adam(model.parameters(), lr=0.0001) #optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9) n_size = batch_size * 256 * 256 writer = SummaryWriter(comment='student' + f'LR_0.0001_BS_32') #创建一个tensorboard文件 epochs = 50 global_step = 1 for epoch in range(epochs): loss_sigma = 0.0