python——为什么我的cnn模型学习不准确?

92vpleto  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(156)

关闭。这个问题是基于意见的。它目前不接受答案。
**想改进这个问题吗?**编辑这篇文章,更新这个问题,以便用事实和引文来回答。

两天前关门了。
改进这个问题
我对kears和计算机视觉非常陌生。我正在尝试使用efficientb0进行皮肤图像分类。培训分为8个班,每个班大约200张图片。我使用imagedatagenerator进行了扩充。转移学习是使用imagenet权重完成的。请参考下面的代码。在此处输入图像描述

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
import tensorflow as tf
import shutil
import os
import cv2
import pickle
from glob import glob
from PIL import Image
from keras.models import Sequential
from keras.layers import Dense, Conv2D , MaxPool2D , Flatten , Dropout
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import SGD, Adam
from keras.callbacks import ReduceLROnPlateau
from sklearn.metrics import classification_report,confusion_matrix
from tqdm import tqdm
from tensorflow.keras import models
from tensorflow.keras import layers

# loading pretrained conv base model

base_model = tf.keras.applications.EfficientNetB0(
    include_top=False,
    weights="imagenet",
    input_shape= (224, 224, 3),
    classes=8
)

dropout_rate = 0.2
model = models.Sequential()
model.add(base_model)
model.add(layers.GlobalMaxPooling2D(name="gap"))

# model.add(layers.Flatten(name="flatten"))

if dropout_rate > 0:
    model.add(layers.Dropout(dropout_rate, name="dropout_out"))

# model.add(layers.Dense(256, activation='relu', name="fc1"))

model.add(layers.Dense(8, activation="softmax", name="fc_out"))

model.summary()

train_dir = '/content/drive/MyDrive/ISIC_2019/Train/'
test_dir = '/content/drive/MyDrive/ISIC_2019/Test/'
height = 224
width = 224
batch_size = 32
learn_rate=.0001
epochs=100

train_datagen = ImageDataGenerator(
    rescale=1.0 / 255,
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode="nearest",
)

# Note that the validation data should not be augmented!

test_datagen = ImageDataGenerator(rescale=1.0 / 255)

train_generator = train_datagen.flow_from_directory(
    # This is the target directory
    train_dir,
    # All images will be resized to target height and width.
    target_size=(height, width),
    batch_size=batch_size,
    # Since we use categorical_crossentropy loss, we need categorical labels
    class_mode="categorical",
)

test_generator = test_datagen.flow_from_directory(
    test_dir,
    target_size=(height, width),
    batch_size=batch_size,
    class_mode="categorical",
)

adam=Adam(learning_rate=learn_rate, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
model.compile(
    loss="categorical_crossentropy",
    optimizer=adam,
    metrics=["acc"],
)
history = model.fit_generator(
    train_generator,
    epochs=epochs,
    validation_data=test_generator,
    verbose=1,
    use_multiprocessing=True,
    workers=4,
)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题