我目前正在进行一个项目,我必须训练一个网络,从输入中重建损坏的图像。每个输入图像的大小都不一样,并且已经裁剪出其中的一部分,我的网络应该“恢复”
所以我训练了自动编码器,测试恢复时间到了。我将每个测试图像加载到模型中并保存输出:
to_tensor = transforms.ToTensor()
new_test = []
for i in test_data['images']:
i = to_tensor(i)
new_test.append(i)
test_loader = torch.utils.data.DataLoader(dataset=new_test, batch_size=1)
def reconstruct(net, loader):
for i,data in enumerate(loader):
data = data.to(device)
output = net(data)
#save_image(data, f'test_outs/damaged_input_{i}.jpg')
save_image(output, f'test_outs/recon_output_{i}.jpg')
net.to(device)
reconstruct(net, test_loader)
然后使用这些重建裁剪出恢复的零件:
from glob import glob
from pathlib import Path
def cropper(image_array, crop_size, crop_center):
if type(image_array) != np.ndarray:
raise ValueError('not a numpy array')
if type(crop_size) != tuple or type(crop_center) != tuple:
raise ValueError('use tuple as input for crop_size and crop_center')
if len(crop_size) != 2 or len(crop_center) != 2:
raise ValueError('invalid amount of values')
if crop_size[0] % 2 == 0 or crop_size[1] % 2 == 0:
raise ValueError('crop_size values should be odd!')
img = image_array.copy()
crop_array = np.zeros_like(image_array)
size_x = round(crop_center[0] - (crop_size[0]-1)/2)
center_x = round(crop_center[0] + (crop_size[0]+1)/2)
size_y = round(crop_center[1] - (crop_size[1]-1)/2)
center_y = round(crop_center[1] + (crop_size[1]+1)/2)
border_right = size_y
border_left = size_x
border_up = image_array.shape[0] - center_y
border_down = image_array.shape[1] - center_x
if border_right < 20 or border_left < 20:
raise ValueError('minimal distance between crop and border is less than 20')
if border_up < 20 or border_down < 20:
raise ValueError('minimal distance between crop and border is less than 20')
target_array = image_array[int(size_x):int(center_x), int(size_y):int(center_y)]
pad = np.negative(target_array)
ones_pad = np.ones_like(target_array)
img[int(size_x):int(center_x), int(size_y):int(center_y)] += pad
crop_array[int(size_x):int(center_x), int(size_y):int(center_y)] += ones_pad
image_array = img
return (image_array, crop_array, target_array)
def rgb2gray(rgb_array: np.ndarray, r=0.2989, g=0.5870, b=0.1140):
grayscale_array = (rgb_array[..., 0] * r +
rgb_array[..., 1] * g +
rgb_array[..., 2] * b)
grayscale_array = np.round(grayscale_array)
grayscale_array = np.asarray(grayscale_array, dtype=np.uint8)
return grayscale_array
def load_recons(root = 'test_outs'):
recons = []
path = Path(root).glob('**/*.jpg')
for enum, f in enumerate(path):
img = Image.open(f)
arr = np.asarray(img)
arr = rgb2gray(arr)
recons.append(arr)
return recons
recs = load_recons()
def crop_tests(recons, crop_centrs = test_data['crop_centers'], crop_szs = test_data['crop_sizes']):
for enum, (i,j,arr) in enumerate(zip(crop_centrs, crop_szs, recons)):
image, crop, target = cropper(arr, j, i)
Image.fromarray(target).save(f'test_final/{enum}_target.jpg')
crop_tests(recs)
但问题是,当我调用crop_tests
时,我得到了一个异常:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-27-f927403b1f35> in <module>
6 Image.fromarray(target).save(f'test_final/{enum}_target.jpg')
7
----> 8 crop_tests(recs)
9
<ipython-input-27-f927403b1f35> in crop_tests(recons, crop_centrs, crop_szs)
3 def crop_tests(recons, crop_centrs = test_data['crop_centers'], crop_szs = test_data['crop_sizes']):
4 for enum, (i,j,arr) in enumerate(zip(crop_centrs, crop_szs, recons)):
----> 5 image, crop, target = cropper(arr, j, i)
6 Image.fromarray(target).save(f'test_final/{enum}_target.jpg')
7
~\proj_git\folder_preparator.py in cropper(image_array, crop_size, crop_center)
52 raise ValueError('minimal distance between crop and border is less than 20')
53 if border_up < 20 or border_down < 20:
---> 54 raise ValueError('minimal distance between crop and border is less than 20')
55
56
ValueError: minimal distance between crop and border is less than 20
这意味着,要裁剪的正方形非常接近重建图像的边界(小于20像素)。然而,这并不是损坏图像的问题
因此,重建图像的形状如下所示:
(70, 93)
(70, 93)
(70, 93)
(70, 93)
(70, 93)
(93, 70)
(70, 93)
(93, 70)
(93, 70)
(70, 93)
(93, 70)
(70, 93)
(70, 93)
(70, 93)
(70, 93)
但是,原始图像的形状顺序稍有不同:
(70, 93)
(70, 93)
(70, 93)
(70, 93)
(70, 93)
(70, 93)
(70, 93)
(70, 93)
(93, 70)
(93, 70)
(70, 93)
(70, 93)
(70, 93)
(100, 70)
所以我需要我的重构与输入的形状完全相同,但我自己看不到代码中的问题。你能帮我查一下吗?请随时提出其他问题
目前没有回答
相关问题 更多 >
编程相关推荐