【问题标题】:What what be the best pattern of layers for my Tensorflow/Keras model?我的 Tensorflow/Keras 模型的最佳层模式是什么?
【发布时间】:2019-11-23 02:50:23
【问题描述】:

我目前正在制作一个空客/波音分类器,可以确定图像是什么飞机。我已将图像的大小调整为 1000 x 1000。你们中有人介意给我举个例子,说明我应该如何使用 Tensorflow 和 Keras 构建模型吗?另外,我应该包括哪些类型的图层?我是 Tensorflow 的新手,所以我不知道我应该使用哪种层来获得最大的准确性。谢谢你。这是我的代码:

import numpy as np
import cv2
import os
import tensorflow as tf
from tensorflow import keras

boeing_dir = '#'
airbus_dir = '#'
path = '#'



boeing_data = []
boeing_label = []
airbus_data = []
airbus_label = []
print('begun')
for filename in os.listdir(boeing_dir):
    if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".jpeg"):
        path_b = os.path.join(boeing_dir, filename)
        im = cv2.imread(path_b)
        im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
        im = cv2.imread(path_b, cv2.IMREAD_GRAYSCALE)
        im = cv2.resize(im, (1000, 1000))
        boeing_data.append(im)
        boeing_label.append(0)
        print(im.shape)

for filename in os.listdir(airbus_dir):
    if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".jpeg"):
        path_b = os.path.join(airbus_dir, filename)
        im = cv2.imread(path_b)
        im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
        im = cv2.imread(path_b, cv2.IMREAD_GRAYSCALE)
        im = cv2.resize(im, (1000, 1000))
        airbus_data.append(im)
        airbus_label.append(1)
        print(im.shape)



training_data = boeing_data + airbus_data
training_label = boeing_label + airbus_label
print(training_data)
print(training_label)
training_data = np.array(training_data)#.reshape(-1, 1000, 1000, 1)
training_label = np.asarray(training_label)

我已将图像拆分为图像和标签。在此之后,我该如何构建我的模型?

【问题讨论】:

  • 您可能无法将所有这些图像都放入内存中,您应该改用ImageDataGenerator

标签: python tensorflow keras


【解决方案1】:

您可能应该首先尝试使用预构建的模型。在构建自己的模型之前,您应该做一些研究。

from tensorflow.keras.applications.densenet import DenseNet201 as b # 
from tensorflow.keras import layers, models, optimizers, regularizers
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model

应用程序模块提供了更多可供选择的模型。

traingen =image.ImageDataGenerator(rescale=1./255)
validationgen = image.ImageDataGenerator(rescale=1./255)
testgen = image.ImageDataGenerator(rescale=1./255)

train = traingen.flow_from_directory("train",target_size=(1000, 1000), batch_size=batchSize, shuffle=True)
val = validationgen.flow_from_directory("validation",target_size=(1000, 1000), batch_size=size, shuffle=False)
test=testgen.flow_from_directory("test",target_size=(1000, 1000), batch_size=size, shuffle=False)

为此,您应该将数据分成三个文件夹,一个用于训练、验证和测试数据。在这些文件夹中,您应该还有两个名为“boeing”和“airbus”的文件夹,它们分别包含这些图像。此代码从三个文件夹中读取图像并根据文件夹名称推断类

 base = b(include_top=False, # include top refers if to include the dense layer that acts as the classifier, this is not needed in this case because we build one later
            weights="imagenet", # these are the weights used by the model that were learned from being trained on the imagenet dataset
            input_shape=(1000, 1000, 3)) # in case you work with rgb images, if you work with gray images you should use input_shape=(1000, 1000, 1)

x = base.output
predictions = layers.Dense(2, activation='softmax')(x)
model = Model(inputs=base.input, outputs=predictions)

model.compile(optimizer=optimizers.Adam(), 
          loss='categorical_crossentropy', 
          metrics=["categorical_accuracy"])


history = model.fit_generator(train,
                          steps_per_epoch=np.ceil(numImages/batchSize),
                          epochs=50,
                          verbose=2,
                          validation_data= val,
                          validation_steps=np.ceil(nunImages/size)
                          )

通过使用预训练网络,您可以利用从 imagenet 中学到的知识,这些模式应该很有用,因为您使用的是自然图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    相关资源
    最近更新 更多