我目前正在尝试使用来自Sun数据集的一些图像,这些图像的形状各不相同,大约为(1000,400,1)。由于它们的形状各不相同,因此我的处理方法是创建一个包含numpy数组的numpy数组,这样我就不必定义其任何形状。我要做的是使用这些图片来训练基本的CNN。问题是,我认为CNN不能真正理解输入数据的定义。在我的实现中,例如self.X_train [0]包含一张图像(在self.Y_train [0]中有相应的目标,依此类推)。我的代码现在看起来像:
import os
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
class network:
def __init__(self):
self.X_train, self.Y_train = self.generate_targets()
def generate_targets(self):
path = 'C:\\Users\\joaki\\PycharmProjects\\project\\project dl\\'
folder = os.fsencode(path)
targets = []
inputs = []
for file in os.listdir(folder):
filename = os.fsdecode(file)
if filename.endswith(('.jpg')):
img = Image.open(filename).convert('RGB')
img2 = Image.open(filename).convert('L')
arr2 = np.array(img2)
arr2 = arr2.reshape((arr2.shape[0], arr2.shape[1], 1))
inputs.append(arr2)
arr = np.array(img)
targets.append(arr)
Y = np.array(targets)
X = np.array(inputs)
return X, Y
def plotting(self, type):
plt.figure(figsize=(20, 10))
for i in range(self.X_train.shape[0]):
plt.subplot(2, 2, i+1)
if type == 'targets':
lum_img = self.Y_train[i][:, :, :] #[:,:,:] för färg
plt.imshow(lum_img)
if type == 'inputs':
lum_img = self.X_train[i][:, :, 0] # [:,:,:] för färg
plt.imshow(lum_img)
plt.show()
def train_network(self):
model = Sequential()
# add model layers
model.add(Conv2D(64, kernel_size=3, activation='relu', input_shape = (None, None, 1)))
model.add(Conv2D(32, kernel_size=3, activation='relu'))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(self.X_train, self.Y_train, batch_size = 1, validation_data=(self.X_train, self.Y_train), epochs=3)
network1 = network()
#network1.plotting('inputs')
network1.train_network()
#print(network1.X_train[0].shape)
是否有解决此问题的方法,如果可以,有人可以提供我应该遵循的信息或消息来源吗?预先感谢!