先熟練一下打開圖片:”
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
img=Image.open("C:\\Users\\楊夢雨\\Desktop\\flower.jpg")
m=np.asarray(img)
plt.figure(figsize=(18,24))
plt.imshow(m)
<matplotlib.image.AxesImage at 0x23663889848>
import numpy as np
import sklearn
from sklearn.datasets import load_sample_image
from PIL import Image
import matplotlib as mpl
import matplotlib.pyplot as plt
flower = load_sample_image("flower.jpg")[150:220,130:250] / 255
china = load_sample_image("china.jpg")[150:220,130:250] / 255
images = np.array([flower,china])
batch_size,height,width,channels = images.shape
print(images.shape)
(2, 70, 120, 3)
images = np.transpose(images, (0, 3, 1, 2))
print(images.shape)
(2, 3, 70, 120)
plt.figure(figsize = (15, 15))
plt.subplot(1, 2, 1)
plt.imshow(flower)
plt.subplot(1, 2, 2)
plt.imshow(china)
plt.tight_layout()
plt.show()
filters = np.zeros(shape = (7, 7, channels, 2), dtype = np.float32)
filters[:, 3, :, 0] = 1
filters[3, :, :, 1] = 1
plt.figure(dpi = 100)
plt.subplot(1,2,1)
plt.imshow(filters[:,:,0,0],cmap = "gray")
plt.subplot(1,2,2)
plt.imshow(filters[:,:,0,1],cmap = "gray")
filters = np.transpose(filters, (3, 2, 0, 1)) #與pytorch一致(N, C, H, W)
filters.shape
(2, 3, 7, 7)
import torch
import torch.nn.functional as F
images_tensor = torch.Tensor(images)
filters_tensor = torch.Tensor(filters)
print(images_tensor.shape, filters_tensor.shape)
torch.Size([2, 3, 70, 120]) torch.Size([2, 3, 7, 7])
outputs = F.conv2d(images_tensor, filters_tensor, stride = 1, padding = 3)
print(outputs.shape)
torch.Size([2, 2, 70, 120])
flower_convolved = outputs[0].numpy()
flower_convolved = np.transpose(flower_convolved, (1, 2, 0))
plt.figure(figsize = (20,20))
plt.subplot(1,3,1);plt.imshow(flower)
plt.subplot(1,3,2);plt.imshow(flower_convolved[:,:,0],cmap = "gray")
plt.subplot(1,3,3);plt.imshow(flower_convolved[:,:,1],cmap = "gray")
plt.show()
我吐了……
突然就這樣了
百度了大半天沒一個說明白怎麼解決的
難受