8장 딥러닝 기반 컴퓨터 비전#
딥러닝(deep learning)은
Image Completion with Inpainting (using Deep learning - pre-trained torch CompletionNet model)#
# https://github.com/akmtn/pytorch-siggraph2017-inpainting
# must be run with pytorch version 0.4.1
# download the wheels from here: https://download.pytorch.org/whl/cpu/torch_stable.html
# pip install torch-0.4.1-cp37-cp37m-win_amd64.whl # --force-reinstall
# pip install "torchvision-0.4.1+cpu-cp37-cp37m-win_amd64.whl" # --force-reinstall
import os
import torch
from torch.legacy import nn
from torch.legacy.nn.Sequential import Sequential
import cv2
import numpy as np
from torch.utils.serialization import load_lua
import matplotlib.pylab as plt
def tensor2image(src):
out = src.copy() * 255
out = out.transpose((1, 2, 0)).astype(np.uint8)
out = cv2.cvtColor(out, cv2.COLOR_RGB2BGR)
return out
def image2tensor(src):
out = src.copy()
out = cv2.cvtColor(out, cv2.COLOR_BGR2RGB)
out = out.transpose((2,0,1)).astype(np.float64) / 255
return out
image_path = 'images/zebra.png'
mask_path = 'images/inpaint_mask.png'
# download model from http://hi.cs.waseda.ac.jp/~iizuka/data/completionnet_places2.t7
model_path = 'models/completionnet_places2.t7'
gpu = torch.cuda.is_available()
# load Completion Network
data = load_lua(model_path,long_size=8)
#data = torchfile.load(model_path, force_8bytes_long=True)
model = data.model
model.evaluate()
# load data
image = cv2.imread(image_path)
image = cv2.resize(image, (4*(image.shape[0]//4), 4*(image.shape[1]//4)))
I = torch.from_numpy(image2tensor(image)).float()
mask = cv2.imread(mask_path)
mask = cv2.resize(mask, (4*(mask.shape[0]//4), 4*(mask.shape[1]//4)))
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY) / 255
mask[mask <= 0.5] = 0.0
mask[mask > 0.5] = 1.0
#print(input.shape, mask.shape)
M = torch.from_numpy(mask).float()
M = M.view(1, M.size(0), M.size(1))
assert I.size(1) == M.size(1) and I.size(2) == M.size(2)
for i in range(3):
I[i, :, :] = I[i, :, :] - data.mean[i]
# create mask_3ch
M3 = torch.cat((M, M, M), 0)
im = I * (M3*(-1)+1)
# set up input
input = torch.cat((im, M), 0)
input = input.view(1, input.size(0), input.size(1), input.size(2)).float()
if gpu:
print('using GPU...')
model.cuda()
input = input.cuda()
# evaluate
res = model.forward(input)[0].cpu()
# make out
for i in range(3):
I[i, :, :] = I[i, :, :] + data.mean[i]
out = res.float()*M3.float() + I.float()*(M3*(-1)+1).float()
image[mask > 0.5] = 255
plt.figure(figsize=(20,35))
plt.subplots_adjust(left=0, right=1, top=0.9, bottom=0, wspace=0.05, hspace=0.05)
plt.subplot(211), plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)), plt.axis('off'), plt.title('Incomplete Image', size=20)
plt.subplot(212), plt.imshow(cv2.cvtColor(tensor2image(out.numpy()), cv2.COLOR_BGR2RGB)), plt.axis('off'), plt.title('Completed Image (with CompletionNet)', size=20)
#plt.savefig('inpainting_out.png', bbox_in='tight', pad_in=0)
plt.show()
print('Done')
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[1], line 8
1 # https://github.com/akmtn/pytorch-siggraph2017-inpainting
2
3 # must be run with pytorch version 0.4.1
4 # download the wheels from here: https://download.pytorch.org/whl/cpu/torch_stable.html
5 # pip install torch-0.4.1-cp37-cp37m-win_amd64.whl # --force-reinstall
6 # pip install "torchvision-0.4.1+cpu-cp37-cp37m-win_amd64.whl" # --force-reinstall
7 import os
----> 8 import torch
9 from torch.legacy import nn
10 from torch.legacy.nn.Sequential import Sequential
ModuleNotFoundError: No module named 'torch'