微调的Resnet-50模型仅提供40%的精度。如何提高?

时间:2019-11-28 02:44:37

标签: python tensorflow keras resnet

我认为,仅通过更改最后一层以满足您的CNN需求,就可以使预制模型具有较高的准确性。我不确定为什么在拟合模型时会收到如此低的验证精度。有什么建议或要使我的准确率达到80%的建议?我正在尝试确定飞机是空中客车飞机还是波音飞机。可能是分辨率极低的图像吗?

40%拟合示例:

Epoch 10/10

 20/149 [===>..........................] - ETA: 24s - loss: 0.6908 - accuracy: 0.5500
 40/149 [=======>......................] - ETA: 21s - loss: 0.6916 - accuracy: 0.5250
 60/149 [===========>..................] - ETA: 17s - loss: 0.6918 - accuracy: 0.5167
 80/149 [===============>..............] - ETA: 13s - loss: 0.6917 - accuracy: 0.5125
100/149 [===================>..........] - ETA: 9s - loss: 0.6918 - accuracy: 0.5200 
120/149 [=======================>......] - ETA: 5s - loss: 0.6924 - accuracy: 0.5167
140/149 [===========================>..] - ETA: 1s - loss: 0.6924 - accuracy: 0.5071
149/149 [==============================] - 33s 225ms/step - loss: 0.6925 - accuracy: 0.5034 - val_loss: 0.6965 - val_accuracy: 0.4706

这是完整的脚本: 知道出什么问题了吗?

from keras.applications.resnet50 import ResNet50
from keras.applications import VGG19
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
from sklearn.utils import shuffle
import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras import models
from keras import layers
import matplotlib as plt
import cv2
import os

'''Resnet-50 classifier that attempts to predict whether a photo of an aircraft is the type of a Boeing or an airbus'''

boeing_dir = '#' # Paths of images/folders used to create training data
airbus_dir = '#'

conv_base = ResNet50(weights='imagenet', input_shape=(224, 224, 3))




model = models.Sequential()
model.add(conv_base)
for layer in model.layers:
    layer.trainable = False
model.add(layers.Dropout(0.15))
model.add(layers.GaussianNoise(0.15))
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(2, activation='softmax'))
model.layers[0].trainable = False
print('model constructed')
boeing_data = []
boeing_label = []
airbus_data = []
airbus_label = []
'''iterate through each file, resize, append to variable accordingly'''
for filename in os.listdir(boeing_dir):
    if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".jpeg"):
        path_b = os.path.join(boeing_dir, filename)
        im = cv2.imread(path_b)
        im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
        im = cv2.resize(im, (224, 224))
        boeing_data.append(im)
        boeing_label.append(0)

for filename in os.listdir(airbus_dir):
    if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".jpeg"):
        path_b = os.path.join(airbus_dir, filename)
        im = cv2.imread(path_b)
        im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
        im = cv2.resize(im, (224, 224))
        airbus_data.append(im)
        airbus_label.append(1)




training_data = boeing_data + airbus_data #Concadenate Boeing and Airbus data
training_label = boeing_label + airbus_label

training_data = np.array(training_data)
training_label = np.asarray(training_label) # Turn Data into numpy arrays
training_data, training_label = shuffle(training_data, training_label) # Shuffle

print(training_data.shape)
print(training_label)
model.compile(loss='sparse_categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
model.fit(training_data, training_label, epochs=10, batch_size=20, validation_split=0.1, verbose=1)
print('finished training')

0 个答案:

没有答案