使用keras中的Conv3d将多个图像输入到同一CNN - python

我想使用conv3d在同一CNN结构中同时输入8张图像。我的CNN模型如下:

def build(sample, frame, height, width, channels,  classes):
    model = Sequential()
    inputShape = (sample, frame, height, width, channels)
    chanDim = -1

    if K.image_data_format() == "channels_first":
        inputShape = (sample, frame, channels, height, width)
        chanDim = 1


    model.add(Conv3D(32, (3, 3, 3), padding="same", input_shape=inputShape))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), padding="same", data_format="channels_last"))
    model.add(Dropout(0.25))

    model.add(Conv3D(64, (3, 3, 3), padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), padding="same", data_format="channels_last"))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(128))    #(Dense(1024))
    model.add(Activation("relu"))
    model.add(BatchNormalization())
    model.add(Dropout(0.5))

    # softmax classifier
    model.add(Dense(classes))
    model.add(Activation("softmax")

模型的训练如下:

IMAGE_DIMS = (57, 8, 60, 60, 3) # since I have 460 images so 57 sample with 8 image each
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
# binarize the labels
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
# note: data is a list of all dataset images
(trainX, testX, trainY, testY) train_test_split(data, labels, test_size=0.2, random_state=42)                                                                                                          
aug = ImageDataGenerator(rotation_range=25, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode="nearest")

# initialize the model
model = CNN_Network.build(sample= IMAGE_DIMS[0], frame=IMAGE_DIMS[1],
                      height = IMAGE_DIMS[2], width=IMAGE_DIMS[3],
                      channels=IMAGE_DIMS[4], classes=len(lb.classes_))

opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="categorical_crossentropy", optimizer= opt, metrics=["accuracy"])

# train the network
model.fit_generator(
aug.flow(trainX, trainY, batch_size=BS),
validation_data=(testX, testY),
steps_per_epoch=len(trainX) // BS,
epochs=EPOCHS, verbose=1)

我对input_shape感到困惑,我知道Conv3D需要5D输入,输入是从keras添加批处理的4D,但是我遇到以下错误:

ValueError: Error when checking input: expected conv3d_1_input to have 5 dimensions, but got array with shape (92, 60, 60, 3)

谁能帮我该怎么办? 92的结果是什么,我用(57,8,60,60,3)确定input_shape。我的input_shape应该是什么,才能同时获得8张彩色图像输入到同一模型。

参考方案

Here是用于将5D输入到Conv3D网络的自定义图像数据生成器。希望能帮助到你。这是有关如何使用它的示例:

from tweaked_ImageGenerator_v2 import ImageDataGenerator
datagen = ImageDataGenerator()
train_data=datagen.flow_from_directory('path/to/data', target_size=(x, y), batch_size=32, frames_per_step=4)

要么

您可以构建自己的5D张量:

frames_folder = 'path/to/folder'
X_data = []
y_data = []
list_of_sent = os.listdir(frames_folder)
print (list_of_sent)
class_num = 0
time_steps = 0  
frames = []
for i in list_of_sent:
    classes_folder = str(frames_folder + '/' + i) #path to each class
    print (classes_folder)
    list_of_frames = os.listdir(classes_folder)
    time_steps= 0
    frames = []
    for filename in  sorted(list_of_frames):   
        if ( time_steps == 8 ):
            X_data.append(frames) #appending each tensor of 8 frames resized to 110,110
            y_data.append(class_num) #appending a class label to the set of 8 frames
            j = 0  
            frames = []
        else:
            time_steps+=1
            filename = cv2.imread(vid + '/' + filename)
            filename = cv2.resize(filename,(110, 110),interpolation=cv2.INTER_AREA)
            frames.append(filename)


    class_num+=1
X_data = np.array(X_data)
y_data = np.array(y_data)

对于上面的代码段,文件夹结构必须如下所示:

    data/
        class0/
            img001.jpg
            img002.jpg
            ...
        class1/
            img001.jpg
            img002.jpg
            ...

Python-crontab模块 - python

我正在尝试在Linux OS(CentOS 7)上使用Python-crontab模块我的配置文件如下:{ "ossConfigurationData": { "work1": [ { "cronInterval": "0 0 0 1 1 ?", "attribute&…

Python Pandas导出数据 - python

我正在使用python pandas处理一些数据。我已使用以下代码将数据导出到excel文件。writer = pd.ExcelWriter('Data.xlsx'); wrong_data.to_excel(writer,"Names which are wrong", index = False); writer.…

Python GPU资源利用 - python

我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…

Python:在不更改段落顺序的情况下在文件的每个段落中反向单词? - python

我想通过反转text_in.txt文件中的单词来生成text_out.txt文件,如下所示:text_in.txt具有两段,如下所示:Hello world, I am Here. I am eighteen years old. text_out.txt应该是这样的:Here. am I world, Hello old. years eighteen a…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…