在两个图像层之间设计keras模型

apeeds0o  于 2023-05-23  发布在  其他
关注(0)|答案(1)|浏览(144)

我想在图像对(9000个图像)之间定义一个Keras序列模型,其中输入形状是(5,5,3),因为它是一个5x 5像素的rgb图像,输出形状是(5,8),其中图像只有一个波段信息。输入图像集包含卫星图像,输出包含每像素的冠层高度信息。
通过除以255将输入图像集归一化为0 - 1。输出不是。
这是一种回归问题,我想找到衍生产品和原始输入之间的联系,就像逆向工程一样。
你能建议一些基本的代码模型,可以适合问题空间吗?
对于上述情况,我应使用哪种功能丧失:

#model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
#model.compile(loss='mse', optimizer='adam', metrics=['mse', 'mae', 'mape'])

我是否也要规范化输出集?
当前代码:

import os
import numpy as np
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Conv2D, Flatten, UpSampling2D, Dense
from PIL import Image

X = []
Y =[]

train_folder = 'D:/satellite/data/train/'
files = os.listdir(train_folder)
for file in files:
    if file.endswith('.png'):
        img = Image.open(train_folder + file)
        X.append(np.array(img))

X=np.array(X)
X=X/255

train_folder_y = 'D:/canopy/data/train/'
files = os.listdir(train_folder_y)
for file in files:
    if file.endswith('.png'):
        img = Image.open(train_folder_y + file)
        Y.append(np.array(img))

Y=np.array(Y)

x_train, x_test, y_train, y_test = train_test_split(X,Y, test_size=0.25)

图像样本(2496.png):

图像样本(159.png):

注意:使用.png,因为我不想通过jpeg压缩丢失信息。

hi3rlvi2

hi3rlvi21#

基本代码模型命题

from PIL import Image
import numpy as np
from keras.models import Sequential
from keras.layers import Conv2D, Flatten, UpSampling2D, Dense

# Load the first image
image1 = Image.open("image1.png")
# Convert the image to a numpy array
image1_array = np.array(image1)
# print(image1_array.shape) # (5, 5, 3)

# Load the second image (ground truth)
image2 = Image.open("image2.png")
# Convert the image to a numpy array
image2_array = np.array(image2)
image2_array = image2_array.flatten()

# print(image2_array.shape) # (5, 8)

# Create your Keras model
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=image1_array.shape))
# Add more layers as needed based on your model architecture
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(UpSampling2D((2, 2)))
model.add(Conv2D(3, (3, 3), activation='sigmoid', padding='same'))
model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(UpSampling2D((2, 2)))
model.add(Conv2D(3, (3, 3), activation='sigmoid', padding='same'))
model.add(Flatten())  # Flatten the output
model.add(Dense(40, activation='relu'))  # Add a dense layer with 40 units

# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')

# Reshape the input arrays
image1_array = image1_array.reshape((1,) + image1_array.shape)
image2_array = image2_array.reshape((1,) + image2_array.shape)

# Train the model using image1 as input and image2 as the ground truth
model.fit(image1_array, image2_array, epochs=1000, batch_size=1)

当运行它时,它收敛在164.4250左右,你需要调整训练参数。

Epoch 1000/1000
1/1 [==============================] - 0s 2ms/step - loss: 164.4250

接下来的步骤可以是增加过滤器的数量(我在架构中添加了一个瓶颈,这肯定会对当前造成伤害),或者你可以降低学习率,因为我们很快就会在当前设置中陷入局部最优。

相关问题