(rank, world_size)
| 85 | resize = opt['Resize'] |
| 86 | |
| 87 | def predict_folder(rank, world_size): |
| 88 | |
| 89 | setup(rank, world_size=world_size, Master_port='12354') |
| 90 | |
| 91 | # DEFINE NETWORK, SCHEDULER AND OPTIMIZER |
| 92 | model, _, _ = create_model(opt['network'], rank=rank) |
| 93 | |
| 94 | model = load_model(model, path_weights = opt['save']['path']) |
| 95 | # create data |
| 96 | PATH_IMAGES= args.inp_path |
| 97 | PATH_RESULTS = './images/results' |
| 98 | |
| 99 | #create folder if it doen't exist |
| 100 | not os.path.isdir(PATH_RESULTS) and os.mkdir(PATH_RESULTS) |
| 101 | |
| 102 | path_images = [os.path.join(PATH_IMAGES, path) for path in os.listdir(PATH_IMAGES) if path.endswith(('.png', '.PNG', '.jpg', '.JPEG'))] |
| 103 | path_images = [file for file in path_images if not file.endswith('.csv') and not file.endswith('.txt')] |
| 104 | |
| 105 | model.eval() |
| 106 | if rank==0: |
| 107 | pbar = tqdm(total = len(path_images)) |
| 108 | |
| 109 | for path_img in path_images: |
| 110 | tensor = path_to_tensor(path_img).to(device) |
| 111 | _, _, H, W = tensor.shape |
| 112 | |
| 113 | if resize and (H >=1500 or W>=1500): |
| 114 | new_size = [int(dim//2) for dim in (H, W)] |
| 115 | downsample = Resize(new_size) |
| 116 | else: |
| 117 | downsample = torch.nn.Identity() |
| 118 | tensor = downsample(tensor) |
| 119 | |
| 120 | tensor = pad_tensor(tensor) |
| 121 | |
| 122 | with torch.no_grad(): |
| 123 | output = model(tensor, side_loss=False) |
| 124 | if resize: |
| 125 | upsample = Resize((H, W)) |
| 126 | else: upsample = torch.nn.Identity() |
| 127 | output = upsample(output) |
| 128 | output = torch.clamp(output, 0., 1.) |
| 129 | output = output[:,:, :H, :W] |
| 130 | save_tensor(output, os.path.join(PATH_RESULTS, os.path.basename(path_img))) |
| 131 | |
| 132 | |
| 133 | pbar.update(1) |
| 134 | pass |
| 135 | |
| 136 | print('Finished inference!') |
| 137 | if rank == 0: |
| 138 | pbar.close() |
| 139 | cleanup() |
| 140 | |
| 141 | def main(): |
| 142 | world_size = 1 |
nothing calls this directly
no test coverage detected