【问题标题】:Why is the Accuracy Different in my PC From Kaggle When Using the Same Code为什么使用相同代码时我的 PC 与 Kaggle 的准确性不同
【发布时间】:2021-12-18 14:27:45
【问题描述】:

我正在编写一个水印检测算法,并且我尝试了来自 Kaggle 的代码来微调 ResNet,但是当我在 Jupyter 笔记本中运行相同的代码时,当 Kaggle 中的示例代码具有大约 97% 的准确率。我的 PC 上没有安装 GPU,我将批量大小更改为 32。你知道为什么我的准确率会降低 40% 吗?

我的代码:

import tensorflow as tf
import numpy as numpy
import os
from pathlib import Path
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import RMSprop
from PIL import Image

basedir = "/home/mahsa/Kaggle/archive/wm-nowm"

from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

traindir = os.path.join(basedir,'train') # root for training
validdir = os.path.join(basedir,'valid') # root for testing

traingenerator = ImageDataGenerator(rescale=1./255)
validgenerator = ImageDataGenerator(rescale=1./255)

train_data = traingenerator.flow_from_directory(traindir,target_size=(150,150),batch_size=100,class_mode="binary")
valid_data = validgenerator.flow_from_directory(validdir,target_size=(150,150),batch_size=100,class_mode="binary")

existing = tf.keras.applications.InceptionResNetV2 (input_shape=(150, 150, 3),include_top=False, pooling='max', weights='imagenet')
#for layer in existing.layers:
#  layer.trainable = False
#existing.summary()
last = existing.get_layer("mixed_7a")
last_output = last.output

# Flatten the output layer to 1 dimension
x = tf.keras.layers.Flatten()(last_output)
x = tf.keras.layers.Dropout(0.25)(x) 
# Add a fully connected layer with 1,024 hidden units and ReLU activation
x = tf.keras.layers.Dense(128, activation='relu')(x)


x = tf.keras.layers.Dense(64, activation='relu')(x)
# Add a final sigmoid layer for classification
x = tf.keras.layers.Dense  (1, activation='sigmoid')(x)           

model = tf.keras.Model( existing.input, x) 

model.compile(optimizer=RMSprop(lr=0.001),
              loss='binary_crossentropy',
              metrics = ['accuracy'])

history = model.fit(train_data,
                          validation_data=valid_data,
                          steps_per_epoch=150,
                          epochs=60,
                          validation_steps=50,
                          verbose=2)

【问题讨论】:

    标签: tensorflow keras image-classification batchsize fine-tune


    【解决方案1】:

    我发现问题是batch_size,我将batch_size增加到100,训练模型花了大约1.5天但我得到了99%的准确率。

    【讨论】:

      猜你喜欢
      • 2019-10-07
      • 2019-08-29
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 2017-07-31
      • 2019-01-31
      相关资源
      最近更新 更多