Точно настроенный VGG-16 дает точно такой же прогноз для всех тестовых изображений - PullRequest
0 голосов
/ 27 апреля 2018

Я настроил сеть VGG-16 для прогнозирования наличия заболеваний на медицинских снимках. Затем я проверил модель, используя model.predict(), но я вижу, что сеть предсказывает точно такие же 22,310% и 77,690% для наличия и отсутствия заболевания, соответственно, для всех 100 тестовых изображений (см. Скриншот.) Я прилагаю свой код и результаты обучения ниже. Тренировка выглядит хорошо. Обратите внимание, что обучение проводилось на сервере, и прогноз на моем ПК, следовательно, каталоги разные. Не могли бы вы помочь мне найти причину проблемы?

RESULT

Код обучения:

import numpy as np
import os
import time
from vgg16 import VGG16
from keras.preprocessing import image
from imagenet_utils import preprocess_input, decode_predictions
from keras.layers import Dense, Activation, Flatten
from keras.layers import merge, Input
from keras.models import Model
from keras.utils import np_utils
from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split

# Loading the training data
PATH = '/mount'
# Define data path
data_path = PATH 
data_dir_list = os.listdir(data_path)

img_data_list=[]
y=0;
for dataset in data_dir_list:
    img_list=os.listdir(data_path+'/'+ dataset)
    print ('Loaded the images of dataset-'+'{}\n'.format(dataset))
    for img in img_list:
        img_path = data_path + '/'+ dataset + '/'+ img 
        img = image.load_img(img_path, target_size=(224, 224))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        x = x/255

        y=y+1
        print('Input image shape:', x.shape)
        print(y)
        img_data_list.append(x)
img_data = np.array(img_data_list)
#img_data = img_data.astype('float32')
print (img_data.shape)
img_data=np.rollaxis(img_data,1,0)
print (img_data.shape)
img_data=img_data[0]
print (img_data.shape)

# Define the number of classes
num_classes = 2
num_of_samples = img_data.shape[0]
labels = np.ones((num_of_samples,),dtype='int64')

labels[0:3001]=0
labels[3001:]=1

names = ['YES','NO']

# convert class labels to on-hot encoding
Y = np_utils.to_categorical(labels, num_classes)

#Shuffle the dataset
x,y = shuffle(img_data,Y, random_state=2)
# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2)

image_input = Input(shape=(224, 224, 3))

model = VGG16(input_tensor=image_input, include_top=True,weights='imagenet')

model.summary()

last_layer = model.get_layer('block5_pool').output
x= Flatten(name='flatten')(last_layer)
x = Dense(16, activation='relu', name='fc1')(x)
x = Dense(8, activation='relu', name='fc2')(x)
out = Dense(num_classes, activation='softmax', name='output')(x)
custom_vgg_model2 = Model(image_input, out)

# freeze all the layers except the dense layers
for layer in custom_vgg_model2.layers[:-6]:
    layer.trainable = False

custom_vgg_model2.summary()

custom_vgg_model2.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

t=time.time()
#   t = now()
hist = custom_vgg_model2.fit(X_train, y_train, batch_size=128, epochs=10, verbose=1, validation_data=(X_test, y_test))
print('Training time: %s' % (t - time.time()))
(loss, accuracy) = custom_vgg_model2.evaluate(X_test, y_test, batch_size=10, verbose=1)

print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss,accuracy * 100))
custom_vgg_model2.save("vgg_3000_92percent_real.h5")

Тренировочный вывод:

Поезд на 4800 проб, проверка на 1200 проб
Эпоха 1/10
4800/4800 [==============================] - 100 с - потеря: 0,6098 - согласно: 0,7567 - val_loss: 0,3252 - val_acc: 0,8667
Эпоха 2/10
4800/4800 [==============================] - 82 с - потери: 0,2644 - в соответствии с 0,8985 - val_loss: 0,2930 - val_acc: 0,8783
Эпоха 3/10
4800/4800 [==============================] - 83 с - потеря: 0,2297 - акк: 0,9127 - val_loss: 0,2386 - val_acc: 0,9042
Эпоха 4/10
4800/4800 [=====================================] - 83 с - потери: 0,1844 - в соответствии с 0,9327 - val_loss: 0,2273 - val_acc: 0,9083
Эпоха 5/10
4800/4800 [==============================] - 83 с - потеря: 0,1754 - соотв: 0,9354 - val_loss: 0,2080 - val_acc: 0,9167
Эпоха 6/10
4800/4800 [====================================] - 83 с - потеря: 0,1357 - соотв: 0,9515 - val_loss: 0,2403 - val_acc: 0,9183
Эпоха 7/10
4736/4800 [============================>.] - ETA: 0s - потеря: 0,1241 - согласно: 0,9525

Код предсказания

import numpy as np
from keras.preprocessing import image
from imagenet_utils import preprocess_input
from keras import models
import matplotlib.pyplot as plt
import os

model128 = models.load_model('16_8_finally.h5')
list=[]
flag=0
#Path0="D:\\download dump for Deep learnng\\dataset\\kaggle general competition\\test"
Path0="I:\\greenchTestsample\\greendr"
list=os.listdir(Path0)
pred0=[0]*len(list)
pred1=[0]*len(list)
for x in list:
    img_path=Path0+'\\'+ x 
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    x=x/255
    preds = model128.predict(x)
    z=100*preds
    x1=float(z[0][0])
    x2=float(z[0][1])
    pred0[flag]=x1
    pred1[flag]=x2
    flag=flag+1

1 Ответ

0 голосов
/ 27 апреля 2018

Хорошо, так что на самом деле это не ответ, а шаг к отладке. Пожалуйста, измените цикл прогнозирования на код ниже и опубликуйте результат.

for x in list[ :3 ]: # let's do the first 3 only
    img_path=Path0+'\\'+ x 
    print() # leave an empty line before each image
    print( image_path ) # let's see if the correct files are loaded
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    x /= 255 # just nitpicking :)
    print( x ) # let's see if the values make sense
    preds = model128.predict(x)
    print( preds ) # see if the error is already present here
    z=100*preds
    x1=float(z[0][0])
    x2=float(z[0][1])
    print( x1, x2 )
    pred0[flag]=x1
    pred1[flag]=x2
    flag += 1 # nitpicking again :)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...