如何将1个图像分配给每个颜色通道作为ResNet50的输入



我正在尝试构建一个ResNet50模型,该模型对单个输出执行回归,并获取3个图像的输入。所有3张图像大小相同(大脑扫描160*160(,我想将每张图像分配给一个颜色通道(第一张图像为红色,第二张为绿色,第三张为蓝色,因为所有3张都是黑白图像。(,以避免构建新的网络。我已经用ResNet50构建了一个回归器,它接受1个图像作为输入。这是我的代码:

def load_train(path):

"""
It loads the train part of dataset from path
"""
#1 / 255
labels = pd.read_csv('C:\Users\sumad\OneDrive - San José Unified School District\Documents\AMYLOID DATASET COMPLETE ALL\Full Set\AllAmyloidREFNORMTarget.csv')
train_datagen = ImageDataGenerator(validation_split=0.2, rescale=None)
train_gen_flow = train_datagen.flow_from_dataframe(
dataframe=labels,
directory='C:\Users\sumadOneDrive - San José Unified School District\Documents\AMYLOID DATASET COMPLETE ALL\Full Set\AllAmyloidOneImagePNG\',
x_col='ID',
y_col='Value',
target_size=(224, 224),
batch_size=32,
class_mode='raw',
subset = 'training',
seed=12345)
return train_gen_flow

def load_test(path):

"""
It loads the validation/test part of dataset from path
"""
labels = pd.read_csv('C:\Users\sumad\OneDrive - San José Unified School District\Documents\AMYLOID DATASET COMPLETE ALL\Full Set\AllAmyloidREFNORMTarget.csv')
validation_datagen = ImageDataGenerator(validation_split=0.2, rescale=None)
test_gen_flow = validation_datagen.flow_from_dataframe(
dataframe = labels,
directory='C:\Users\sumadOneDrive - San José Unified School District\Documents\AMYLOID DATASET COMPLETE ALL\Full Set\AllAmyloidOneImagePNG\',
x_col="ID",
y_col="Value", 
class_mode="raw", 
target_size=(224,224), 
batch_size=32,
subset = "validation",
seed=1234,
)
return test_gen_flow

def create_model(input_shape):

# we will use ResNet50 architecture, with freezing top layers
backbone = ResNet50(input_shape=input_shape, weights='imagenet', include_top=False)
model = Sequential()
model.add(backbone)

model.add(Dropout(0.3))
model.add(GlobalAveragePooling2D())

model.add(Dense(1, activation='linear'))
optimizer = Adam(learning_rate=0.0003)
model.compile(optimizer=optimizer, loss='mae', metrics=['mae'])
print(model.summary())
return model
def train_model(model, train_data, test_data, batch_size=32, epochs=100,
steps_per_epoch=None, validation_steps=None):
history = model.fit(train_data, validation_data=test_data, batch_size=batch_size, 
epochs=epochs, steps_per_epoch=steps_per_epoch, 
validation_steps=validation_steps, verbose=2)
# Get training and test loss histories
training_loss = history.history['loss']
test_loss = history.history['val_loss']
epoch_count = range(1, len(training_loss) + 1)
plt.plot(epoch_count, training_loss, 'r--')
plt.plot(epoch_count, test_loss, 'b-')
plt.legend(['Training Loss', 'Test Loss'])
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show();
return model
def main():
path = 'C:\Users\sumad\OneDrive - San José Unified School        District\Documents\AMYLOID DATASET COMPLETE ALL\Full Set\'
train_data = load_train(path)
test_data = load_test(path)
model = create_model(input_shape = (224, 224, 3))
model = train_model(model, train_data, test_data)

由于您已经在使用keras,我认为您也安装了Pillow。

此代码片段将三个图像(读取为灰度(堆叠为一个RGB图像。然后,你需要为每个例子调用这个方法来生成一个类似RGB的图像及其标签的列表,但对于这个例子,我需要更多关于你的特定设置的信息,在我看来,这超出了这个问题的范围。

import numpy as np
from PIL import Image
def load_images_to_single(path_a, path_b, path_c, size=(224, 224):
# load images as grayscale
img_a = np.array(Image.open(path_a).convert('L').resize(size))
img_b = np.array(Image.open(path_b).convert('L').resize(size))
img_c = np.array(Image.open(path_c).convert('L').resize(size))
# map images to channels as:
#     img_a -> R channel
#     img_b -> G channel
#     img_c -> B channel
img_rgb = np.dstack([img_a, img_b, img_c])
return img_rgb

您所需要做的就是,首先,对数据集进行迭代以构建X_train, y_train,并对其进行测试,因为这个庞然大物与默认值有点不同。

只有当您的数据适合内存时,它才会起作用,否则您需要实现自己的DataGeneration

最新更新