마스크로 이미지 분할하기
이진 마스크가 준비되었으니, 이제 이를 사용해 이미지에서 객체(고양이)를 분할할 수 있어요.
이를 위해 먼저 원본 이미지를 불러와 텐서로 변환해야 해요. 다음으로, 원본 이미지에 마스킹을 적용해 객체 텐서를 만들고, 마지막으로 결과를 화면에 표시해요.
torchvision의 transforms는 이미 가져와 두었고, 이전 연습 문제에서 만든 binary_mask도 준비되어 있어요.
이 연습은 강의의 일부입니다
PyTorch로 배우는 이미지 딥러닝
연습 안내
- 원본 이미지에
transform으로 정의된ToTensor()변환을 적용해image_tensor를 생성하세요. image_tensor에binary_mask를 마스킹하여 이미지를 분할하고, 결과를object_tensor에 할당하세요.- 이미 정의된
to_pil_image변환을object_tensor에 적용해 화면에 표시하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Load image and transform to tensor
image = Image.open("images/Egyptian_Mau_123.jpg")
transform = transforms.Compose([transforms.ToTensor()])
image_tensor = ____
# Segment object out of the image
object_tensor = ____
# Convert segmented object to image and display
to_pil_image = ____
object_image = to_pil_image(object_tensor)
plt.imshow(object_image)
plt.show()