2017-04-08 5 views
2

Keras의 공식 이진 분류 예 (here 참조)로 시작한 후 Tendorflow가 백엔드 인 다중 분류자를 구현할 예정입니다. 이 예제에는 두 개의 클래스 (dog/cat)가 있으며 이제 50 개의 클래스가 있으며 데이터는 폴더에 동일한 방식으로 저장됩니다.Keras : CNN 멀티 클래스 분류 자 ​​

훈련을하면 손실이 줄어들지 않고 정확도가 올라가지 않습니다. 함수를 사용하여 을 사용하고 binary_crossentropycategorical_crossentropy으로 변경 한 마지막 레이어를 변경하고 class_modecategorical으로 변경했습니다.

from keras.preprocessing.image import ImageDataGenerator 
from keras.models import Sequential 
from keras.layers import Conv2D, MaxPooling2D 
from keras.layers import Activation, Dropout, Flatten, Dense 
from keras import backend as K 
import keras.optimizers 



optimizer = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) 

# dimensions of our images. 
img_width, img_height = 224, 224 

train_data_dir = 'images/train' 
validation_data_dir = 'images/val' 
nb_train_samples = 209222 
nb_validation_samples = 40000 
epochs = 50 
batch_size = 16 

if K.image_data_format() == 'channels_first': 
    input_shape = (3, img_width, img_height) 
else: 
    input_shape = (img_width, img_height, 3) 

model = Sequential() 
model.add(Conv2D(32, (3, 3), input_shape=input_shape)) 
model.add(Activation('relu')) 
model.add(MaxPooling2D(pool_size=(2, 2))) 

model.add(Conv2D(32, (3, 3))) 
model.add(Activation('relu')) 
model.add(MaxPooling2D(pool_size=(2, 2))) 

model.add(Conv2D(64, (3, 3))) 
model.add(Activation('relu')) 
model.add(MaxPooling2D(pool_size=(2, 2))) 

model.add(Flatten()) 
model.add(Dense(64)) 
model.add(Activation('relu')) 
model.add(Dropout(0.5)) 
model.add(Dense(50)) 
model.add(Activation('softmax')) 



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


train_datagen = ImageDataGenerator() 

train_generator = train_datagen.flow_from_directory(
    directory=train_data_dir, 
    target_size=(img_width, img_height), 
    batch_size=batch_size, 
    class_mode='categorical') 

validation_generator = train_datagen.flow_from_directory(
    directory=validation_data_dir, 
    target_size=(img_width, img_height), 
    batch_size=batch_size, 
    class_mode='categorical') 

model.fit_generator(
    train_generator, 
    steps_per_epoch=nb_train_samples // batch_size, 
    epochs=epochs, 
    validation_data=validation_generator, 
    validation_steps=nb_validation_samples // batch_size) 

model.save_weights('weights.h5') 

내가 잘못 될 수있는 위치에 어떤 생각 : 여기

내 코드? 입력 사항을 알려 주시면 감사하겠습니다.

편집 : @RobertValencia에 의해 요청으로, 여기에 최신 교육 로그의 시작입니다

Using TensorFlow backend. 
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcublas.so.7.5 locally 
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcudnn.so.5 locally 
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcufft.so.7.5 locally 
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcuda.so.1 locally 
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcurand.so.7.5 locally 
Found 3517 images belonging to 50 classes. 
<keras.preprocessing.image.DirectoryIterator object at 0x7fd1d4515c10> 
Found 2451 images belonging to 50 classes. 
Epoch 1/50 
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations. 
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. 
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. 
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. 
I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:910] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:885] Found device 0 with properties: 
name: GRID K520 
major: 3 minor: 0 memoryClockRate (GHz) 0.797 
pciBusID 0000:00:03.0 
Total memory: 3.94GiB 
Free memory: 3.91GiB 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:906] DMA: 0 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:916] 0: Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:975] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GRID K520, pci bus id: 0000:00:03.0) 
8098/13076 [=================>............] - ETA: 564s - loss: 15.6869 - categorical_accuracy: 0.0267 
+0

? 왜 그렇게 작은 추진력과 왜 네 스테 로프 추진력을 억제합니까? – nemo

+0

@nemo 감사합니다. 잘못된 최적화를 여기에 복사했습니다. 방금 편집했습니다. 하지만이 문제는'optimizer = SGD (lr = 0.01, decay = 1e-6, momentum = 0.9, nesterov = True)'(게시물에 편집 됨)가 있습니다. –

+0

각 클래스에 적합한 디렉토리 구조가 있습니까 ? 트레이닝 생성기에서 생성 된 (x, y) 쌍이 올바른지 확인하십시오 (트레이스 생성기에서'next() '를 호출하고 결과를 확인하십시오). – nemo

답변

0

당신은 아마도 모델의 복잡성을 증가 차별화 할 필요가 클래스의 수를 고려뿐만 아니라, 다른 옵티 마이저를 사용하면 더 나은 결과를 얻을 수 있습니다. 부분적으로 VGG-16 CNN 아키텍처를 기반으로이 모델을 사용하여 시도,하지만 같은 복잡하지 :

    : 당신이 더 나은 결과를 얻을 경우

    model = Sequential() 
    model.add(Convolution2D(32, 3, 3, activation='relu')) 
    model.add(Convolution2D(32, 3, 3, activation='relu')) 
    model.add(MaxPooling2D((2,2), strides=(2,2))) 
    
    model.add(Convolution2D(64, 3, 3, activation='relu')) 
    model.add(Convolution2D(64, 3, 3, activation='relu')) 
    model.add(MaxPooling2D((2,2), strides=(2,2))) 
    
    model.add(Convolution2D(128, 3, 3, activation='relu')) 
    model.add(Convolution2D(128, 3, 3, activation='relu')) 
    model.add(MaxPooling2D((2,2), strides=(2,2))) 
    
    model.add(Convolution2D(256, 3, 3, activation='relu')) 
    model.add(Convolution2D(256, 3, 3, activation='relu')) 
    model.add(MaxPooling2D((2,2), strides=(2,2))) 
    
    model.add(Convolution2D(512, 3, 3, activation='relu')) 
    model.add(Convolution2D(512, 3, 3, activation='relu')) 
    model.add(MaxPooling2D((2,2), strides=(2,2))) 
    
    model.add(Flatten()) 
    model.add(Dense(1024, activation='relu')) 
    model.add(Dense(1024, activation='relu')) 
    model.add(Dense(50, activation='softmax')) 
    
    optimizer = Nadam(lr=0.002, 
            beta_1=0.9, 
            beta_2=0.999, 
            epsilon=1e-08, 
            schedule_decay=0.004) 
    
    model.compile(loss='categorical_crossentropy', 
           optimizer=optimizer, 
           metrics=['categorical_accuracy']) 
    

    , 나는 VGG-16 모델에보고 제안

  1. https://github.com/fchollet/keras/blob/master/keras/applications/vgg16.py 또는
  2. https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3 년 (제로 패딩과 드롭 아웃 레이어)를 당신의 최적화 설정을 무엇
+0

을 추가했습니다. 시도해 보겠습니다.하지만 모델을 80 %의 정확도에서 95 %까지, 그러나 3 %에서 95 %까지 끌어 올릴 수있는 것처럼 보입니다.하지만 몇 시간 후에 확실히 시도해보고 알려주세요. –

+0

오케이. 더 나은 결과를 얻었 으면 알려주세요. 그 결과에 대해서도 궁금합니다. –