0

我正在编写一个水印检测算法,并且我尝试了来自 Kaggle 的代码来微调 ResNet,但是当我在 Jupyter 笔记本中运行相同的代码时,当 Kaggle 中的示例代码有大约 97% 时,我得到 50% 的准确率准确性。我的 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)
4

1 回答 1

0

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

于 2021-12-20T06:36:22.763 回答