Modify inference.py

This commit is contained in:
PiggyJerry 2023-01-26 12:10:39 +04:00
parent 996c9495d4
commit f3837183a3

View File

@ -30,23 +30,24 @@ if __name__ == "__main__":
net.load_state_dict(torch.load(model_path,map_location="cpu")) net.load_state_dict(torch.load(model_path,map_location="cpu"))
net.eval() net.eval()
im_list = glob(dataset_path+"/*.jpg")+glob(dataset_path+"/*.JPG")+glob(dataset_path+"/*.jpeg")+glob(dataset_path+"/*.JPEG")+glob(dataset_path+"/*.png")+glob(dataset_path+"/*.PNG")+glob(dataset_path+"/*.bmp")+glob(dataset_path+"/*.BMP")+glob(dataset_path+"/*.tiff")+glob(dataset_path+"/*.TIFF") im_list = glob(dataset_path+"/*.jpg")+glob(dataset_path+"/*.JPG")+glob(dataset_path+"/*.jpeg")+glob(dataset_path+"/*.JPEG")+glob(dataset_path+"/*.png")+glob(dataset_path+"/*.PNG")+glob(dataset_path+"/*.bmp")+glob(dataset_path+"/*.BMP")+glob(dataset_path+"/*.tiff")+glob(dataset_path+"/*.TIFF")
for i, im_path in tqdm(enumerate(im_list), total=len(im_list)): with torch.no_grad():
print("im_path: ", im_path) for i, im_path in tqdm(enumerate(im_list), total=len(im_list)):
im = io.imread(im_path) print("im_path: ", im_path)
if len(im.shape) < 3: im = io.imread(im_path)
im = im[:, :, np.newaxis] if len(im.shape) < 3:
im_shp=im.shape[0:2] im = im[:, :, np.newaxis]
im_tensor = torch.tensor(im, dtype=torch.float32).permute(2,0,1) im_shp=im.shape[0:2]
im_tensor = F.upsample(torch.unsqueeze(im_tensor,0), input_size, mode="bilinear").type(torch.uint8) im_tensor = torch.tensor(im, dtype=torch.float32).permute(2,0,1)
image = torch.divide(im_tensor,255.0) im_tensor = F.upsample(torch.unsqueeze(im_tensor,0), input_size, mode="bilinear").type(torch.uint8)
image = normalize(image,[0.5,0.5,0.5],[1.0,1.0,1.0]) image = torch.divide(im_tensor,255.0)
image = normalize(image,[0.5,0.5,0.5],[1.0,1.0,1.0])
if torch.cuda.is_available(): if torch.cuda.is_available():
image=image.cuda() image=image.cuda()
result=net(image) result=net(image)
result=torch.squeeze(F.upsample(result[0][0],im_shp,mode='bilinear'),0) result=torch.squeeze(F.upsample(result[0][0],im_shp,mode='bilinear'),0)
ma = torch.max(result) ma = torch.max(result)
mi = torch.min(result) mi = torch.min(result)
result = (result-mi)/(ma-mi) result = (result-mi)/(ma-mi)
im_name=im_path.split('/')[-1].split('.')[0] im_name=im_path.split('/')[-1].split('.')[0]
io.imsave(os.path.join(result_path,im_name+".png"),(result*255).permute(1,2,0).cpu().data.numpy().astype(np.uint8)) io.imsave(os.path.join(result_path,im_name+".png"),(result*255).permute(1,2,0).cpu().data.numpy().astype(np.uint8))