m0_51532272 2021-02-15 20:13 采纳率: 0%
浏览 78

多次epoch后,为什么苹果和梨子二分类问题最终训练集和验证集的accuracy仍为50%?

自己拍了苹果和梨子各100张图片,将70张作为训练集,30张作为验证集。

代码如下,代码调用了VGG16作为base,不管是用base还是只有全连接层,accuracy一直都是50%,是模型出现问题还是数据量不够呢?

# imports

import os, warnings

import matplotlib.pyplot as plt

from matplotlib import gridspec

import numpy as np

import tensorflow as tf

from tensorflow.keras.preprocessing import image_dataset_from_directory

from google.colab import files

from google.colab import drive

from tensorflow import keras

from tensorflow.keras import layers

import pandas as pd

 

# Reproducability

def set_seed(seed=31415):

    np.random.seed(seed)

    tf.random.set_seed(seed)

    os.environ['PYTHONHASHSEED'] = str(seed)

    os.environ['TF_DETERMINISTIC_OPS'] = '1'

set_seed(31415)

 

# Set Matplotlib defaults

plt.rc('figure', autolayout=True)

plt.rc('axes', labelweight='bold', labelsize='large',

       titleweight='bold', titlesize=18, titlepad=10)

plt.rc('image', cmap='magma')

warnings.filterwarnings("ignore") # to clean up output cells

 

# Load data

drive.mount('/content/drive')

path_train = "/content/drive/My Drive/fruit_train"

path_valid = "/content/drive/My Drive/fruit_valid"

# Load training and validation sets

ds_train_ = image_dataset_from_directory(

    '/content/drive/My Drive/fruit_train',

    labels='inferred',

    label_mode='binary',

    image_size=[224, 224],

    interpolation='nearest',

    batch_size=20,

    shuffle=True,

)

ds_valid_ = image_dataset_from_directory(

    '/content/drive/My Drive/fruit_valid',

    labels='inferred',

    label_mode='binary',

    image_size=[224, 224],

    interpolation='nearest',

    batch_size=10,

    shuffle=False,

)

 

# Data Pipeline

def convert_to_float(image, label):

    image = tf.image.convert_image_dtype(image, dtype=tf.float32)

    return image, label

 

AUTOTUNE = tf.data.experimental.AUTOTUNE

ds_train = (

    ds_train_

    .map(convert_to_float)

    .cache()

    .prefetch(buffer_size=AUTOTUNE)

)

ds_valid = (

    ds_valid_

    .map(convert_to_float)

    .cache()

    .prefetch(buffer_size=AUTOTUNE)

)

pretrained_base = tf.keras.applications.VGG16(

    include_top=True, weights='imagenet', input_tensor=None,

    input_shape=None, pooling=None, classes=1000,

    classifier_activation='softmax'

)

pretrained_base.trainable = False

 

model = keras.Sequential([

    pretrained_base,

    layers.Flatten(),

    layers.Dense(6, activation='relu'),

    layers.Dense(1, activation='sigmoid'),

])

 

model.compile(

    optimizer='adam',

    loss='binary_crossentropy',

    metrics=['binary_accuracy'],

)

 

history = model.fit(

    ds_train,

    validation_data=ds_valid,

    epochs=5,

)

  • 写回答

2条回答 默认 最新

  • m0_51532272 2021-02-15 20:16
    关注

    图为模型结果

    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败