前提
一つの長方形画像から重複こみで、n個の正方形を切り抜く作業を行なっているのですが、以下の切り抜きのコードを実行した際に表示されるエラーに対処できずに詰まっております。
この部分の解決策についてどうか教えていただけないでしょうか?
よろしくお願いします。
実現したいこと
def slice_image(im, desired_size):
正方形画像を5枚切り取る関数(images.appendのところで処理しています)
python
1def slice_image(im, desired_size): 2 ''' 3 Resize and slice image 4 ''' 5 6 old_size = im.size 7 8 9 ratio = float(desired_size)/min(old_size) 10 new_size = tuple([int(x*ratio) for x in old_size]) 11 12 im = im.resize(new_size, Image.ANTIALIAS) 13 14 ar = np.array(im) 15 images = [] 16 if ar.shape[0] < ar.shape[1]: 17 middle = ar.shape[1]//2 18 half = desired_size//2 19 a=(desired_size-middle)//3 20 images.append(Image.fromarray(ar[:, :desired_size])) 21 images.append(Image.fromarray(ar[:, (middle-a)-half:(middle-a)+half])) 22 images.append(Image.fromarray(ar[:, (middle+a)-half:(middle+a)+half])) 23 images.append(Image.fromarray(ar[:, middle-half:middle+half])) 24 images.append(Image.fromarray(ar[:, ar.shape[1]-desired_size:ar.shape[1]])) 25 26 else: 27 middle = ar.shape[0]//2 28 half = desired_size//2 29 a=(desired_size-middle)//3 30 images.append(Image.fromarray(ar[:desired_size, :])) 31 images.append(Image.fromarray(ar[(middle-a)-half:(middle-a)+half, :])) 32 images.append(Image.fromarray(ar[(middle+a)-half:(middle+a)+half, :])) 33 images.append(Image.fromarray(ar[middle-half:middle+half, :])) 34 images.append(Image.fromarray(ar[ar.shape[0]-desired_size:ar.shape[0], :])) 35 36 37 return images 38
class ClipEncoderMulti(nn.Module):
CLIPの画像エンコーダの定義
ここでx.view(-1,5,288,288)を指定しています。
python
1class ClipEncoderMulti(nn.Module): 2 def __init__(self, num_embeds, num_features=image_features_size): 3 super().__init__() 4 self.model = clip_model 5 self.num_embeds = num_embeds 6 self.num_features = num_features 7 8 def forward(self, x): 9 # 4x3x288x288 -> 1x4x640 10 out = self.model.encode_image(x.view(-1,5,288,288)) 11 out = out.view(-1, self.num_embeds, self.num_features).float() 12 return out # Bx4x640
最後にスライスした画像の形状を見ています。
この部分のコードが正直よくわかっていません。
python
1image_encoder = ClipEncoderMulti(4) 2image = Image.open("dataset/img/42953.png").convert("RGB") 3 4sliced_images = slice_image(image, 288) 5sliced_images = [np.array(preprocess(im)) for im in sliced_images] 6 7image = resize_pad_image(image, image_encoder_size) 8image = np.array(preprocess(image)) 9 10sliced_images = [image] + sliced_images 11sliced_images = torch.from_numpy(np.array(sliced_images)).to(device) 12 13print(sliced_images.shape) 14print(image_encoder(sliced_images).shape)
発生している問題・エラーメッセージ
上記のprint文の結果RuntimeError: shape '[-1, 5, 288, 288]' is invalid for input of size 1492992
というエラーが発生しました。
python
1/tmp/ipykernel_1430514/978018639.py:12: DeprecationWarning: ANTIALIAS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead. 2 im = im.resize(new_size, Image.ANTIALIAS) 3/tmp/ipykernel_1430514/3349803982.py:10: DeprecationWarning: ANTIALIAS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead. 4 im = im.resize(new_size, Image.ANTIALIAS) 5--------------------------------------------------------------------------- 6RuntimeError Traceback (most recent call last) 7Input In [153], in <cell line: 14>() 8 11 sliced_images = torch.from_numpy(np.array(sliced_images)).to(device) 9 13 print(sliced_images.shape) 10---> 14 print(image_encoder(sliced_images).shape) 11 12File ~/.pyenv/versions/3.9.11/lib/python3.9/site-packages/torch/nn/modules/module.py:1130, in Module._call_impl(self, *input, **kwargs) 13 1126 # If we don't have any hooks, we want to skip the rest of the logic in 14 1127 # this function, and just call forward. 15 1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 16 1129 or _global_forward_hooks or _global_forward_pre_hooks): 17-> 1130 return forward_call(*input, **kwargs) 18 1131 # Do not call functions when jit is used 19 1132 full_backward_hooks, non_full_backward_hooks = [], [] 20 21Input In [152], in ClipEncoderMulti.forward(self, x) 22 8 def forward(self, x): 23 9 # 4x3x288x288 -> 1x4x640 24---> 10 out = self.model.encode_image(x.view(-1,5,288,288)) 25 11 out = out.view(-1, self.num_embeds, self.num_features).float() 26 12 return out 27 28RuntimeError: shape '[-1, 5, 288, 288]' is invalid for input of size 1492992
補足情報(FW/ツールのバージョンなど)
このコードは下記記事をひな形にしており、デフォルトでは、画像は5枚ではなく3枚に分割されており、x.view(-1,3,288,288)となっていました。
個人として変更したのは、画像を5枚に増やしたところと、x.view()の第二引数を5に変えた点のみです。
あなたの回答
tips
プレビュー