2017-10-09 8 views
0

이미지가 개인지 고양이인지를 예측하기 위해 CNN 모델을 만들었지 만, 출력에서 ​​예측 한 것이 무엇인지 알 수 없습니다. 아래를 참조 출력으로예상 내용은 무엇입니까? CNN Keras

from keras.models import load_model 
from scipy import misc 
import numpy as np 

def single_pred(filepath, model): 
    classifier = load_model(model) 
    img = misc.imread(filepath) 
    img = misc.imresize(img, (64,64,3)) 
    img = np.expand_dims(img, 0) 
    print(classifier.predict(img)) 

if __name__ == '__main__': 
    single_pred('/home/leonardo/Desktop/Help/dataset/single_prediction/cat_or_dog_2.jpg', 'model.h5') 

내가이 얻을 :

import pandas as pd 
from keras.models import Sequential 
from keras.preprocessing.image import ImageDataGenerator 
from keras.layers import Dense, Flatten, Conv2D, Dropout, MaxPooling2D 
from scipy import misc 
import numpy as np 

def build_classifier(): 
    #Model based on 'https://www.researchgate.net/profile/Le_Lu/publication/277335071/figure/fig8/AS:[email protected]/Figure-8-The-proposed-CNN-model-architecture-is-composed-of-five-convolutional-layers.png' 
    #It's smarter to add layer without creating variables because of the processing, but as a small dataset it doesn't matter a lot. 
    classifier = Sequential() 

    conv1 = Conv2D(filters=64, kernel_size=(2,2), activation='relu', input_shape=(64,64,3)) 
    conv2 = Conv2D(filters=192, kernel_size=(2,2), activation='relu') 
    conv3 = Conv2D(filters=384, kernel_size=(2,2), activation='relu') 
    conv4 = Conv2D(filters=256, kernel_size=(2,2), activation='relu') 
    conv5 = Conv2D(filters=256, kernel_size=(2,2), activation='relu') 
    pooling1 = MaxPooling2D(pool_size=(2,2)) 
    pooling2 = MaxPooling2D(pool_size=(2,2)) 
    pooling3 = MaxPooling2D(pool_size=(2,2)) 
    fcl1 = Dense(1024, activation='relu') 
    fcl2 = Dense(1024, activation='relu') 
    fcl3 = Dense(2, activation='softmax') 
    dropout1= Dropout(0.5) 
    dropout2 = Dropout(0.5) 
    flatten = Flatten() 

    layers = [conv1, pooling1, conv2, pooling2, conv3, conv4, conv5, 
      pooling3, flatten, fcl1, dropout1, fcl2, dropout2, fcl3] 

    for l in layers: 
     classifier.add(l) 

    return classifier 

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

train_datagen = ImageDataGenerator(
     rescale=1./255, 
     shear_range=0.2, 
     zoom_range=0.2, 
     horizontal_flip=True) 

test_datagen = ImageDataGenerator(rescale=1./255) 

train_generator = train_datagen.flow_from_directory(
     'dataset/training_set', 
     target_size=(64, 64), 
     batch_size=32, 
     class_mode='categorical') 

validation_generator = test_datagen.flow_from_directory(
     'dataset/test_set', 
     target_size=(64, 64), 
     batch_size=32, 
     class_mode='categorical') 


model.fit_generator(
     train_generator, 
     steps_per_epoch=200, 
     epochs=32, 
     validation_data=validation_generator, 
     validation_steps=100) 

model.save('model.h5') 
model.save_weights('model_weights.h5') 

내가 다른 파일에 저장 한 모델을 열

Using TensorFlow backend. 
2017-10-09 14:06:25.520018: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations. 
2017-10-09 14:06:25.520054: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations. 
[[ 0. 1.]] 

는하지만 방법을 알고 예측이이라고 말한다면 개 또는 고양이. 손에이 결과가 있으면 이미지가 개인지 고양이인지 아직 알 수 없습니다.

+1

누군가 * really * basic * 튜토리얼을 읽어야합니다. 이것을 무시하면 앞으로는별로 재미 있지 않을 것입니다. * 힌트 * : 마지막 레이어를보십시오.'''fcl3 = Dense (2, activation = 'softmax')''', 입력 형태와 손실! – sascha

+0

입력 모양에 어떤 문제가 있습니까? – Leo

+0

나는 뭔가 잘못되었다고 말하지 않았다. 나는 결과를 해석하는 것과 관련하여 3 가지 가장 중요한 것을 암시했다. – sascha

답변

1

라벨을 지정하지 않으면 생성기가 자동으로 카테고리 라벨을 만듭니다. train_generator.class_indices 클래스 레이블의 순서는 영숫자이므로 cats = 0 dogs = 1

관련 문제