gpt4 book ai didi

tensorflow - 如何为任何通用数据集确定卷积神经网络的结构/架构?

转载 作者:行者123 更新时间:2023-12-05 07:13:05 27 4
gpt4 key购买 nike

我知道 CNN 的工作原理,包括每一层的用途(Dropout、Pooling 等)。但是,在为新数据集设计 CNN 时,我不知道要使用多少个 Conv-Relu-Pool 层,在最终获得输出之前我应该​​使用多少个 Dense 层,或者每个卷积中要使用多少个内核层。我知道所有这些东西都有些实验性,不能确定设计 CNN,但是在这样做时有没有我可以记住的经验法则?另外,是否有一篇论文可以让我找到这些问题的答案?

我已经尝试用谷歌搜索所有这些问题,答案总是让我更加困惑。

提前谢谢你。

最佳答案

最好的办法是使用已被证明有效的模型,我们称之为预训练模型。

一些这样的预训练 CNN 模型是 MobileNet (tf.keras.applications.MobileNetV2)、VGGNET (tf.keras.applications.vgg19, tf.keras.applications.vgg16), ResNet (tf.keras.applications.resnet50)等。

ImageNet 是一个拥有数百万条记录和数千个类别的庞大数据集,上述模型在该数据上的表现非常出色,准确率超过 90%。

您所要做的就是使用迁移学习重用这些模型并将其拟合到您的数据中,方法是将预训练模型的输出层或几个密集层替换为特定于您的数据的输出层。

利用MobileNetV2 Model for Flower Dataset 的完整代码如下所示:

import tensorflow as tf
import datetime
import numpy as np
import os

tf.__version__ #'2.1.0'

URL = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"

zip_file = tf.keras.utils.get_file(origin= URL,
fname="flower_photos.tgz",
extract=True)

base_dir = os.path.join(os.path.dirname(zip_file), 'flower_photos')


# Create a DataGenerator
from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(rescale=1. / 255, validation_split=0.2,
featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True)

train_generator = datagen.flow_from_directory(
base_dir,
target_size=(224, 224),
batch_size=32,
subset = 'training',
class_mode='categorical')

validation_generator = datagen.flow_from_directory(
base_dir,
target_size=(224, 224),
batch_size=32,
subset = 'validation',
class_mode='categorical')

#Functional API
#Import MobileNet V2 with pre-trained weights AND exclude fully connected layers
IMG_SIZE = 224

from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras import Model


IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)

# Create the base model from the pre-trained model MobileNet V2
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
include_top=False,
weights='imagenet')

# Add Global Average Pooling Layer
x = base_model.output
x = GlobalAveragePooling2D()(x)

# Add a Output Layer
my_mobilenetv2_output = Dense(5, activation='softmax')(x)

# Combine whole Neural Network
my_mobilenetv2_model = Model(inputs=base_model.input, outputs=my_mobilenetv2_output)

my_mobilenetv2_model.compile(loss='categorical_crossentropy',
optimizer= tf.keras.optimizers.RMSprop(lr=0.0001),
metrics=['accuracy'])

my_mobilenetv2_model.summary()

关于tensorflow - 如何为任何通用数据集确定卷积神经网络的结构/架构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60243094/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com