pythonで以下のようなコードをかき、以下の記事を参考に機械学習させたのですが、正答率(acc)が7割前後で止まってしまいます。
python
1''' 2#Train a simple deep CNN on the CIFAR10 small images dataset. 3 4It gets to 75% validation accuracy in 25 epochs, and 79% after 50 epochs. 5(it's still underfitting at that point, though). 6''' 7 8from __future__ import print_function 9import keras 10from keras.datasets import cifar10 11from keras.preprocessing.image import ImageDataGenerator 12from keras.models import Sequential 13from keras.layers import Dense, Dropout, Activation, Flatten 14from keras.layers import Conv2D, MaxPooling2D 15import numpy as np 16import os 17 18batch_size = 32 19num_classes = 10 20epochs = 1000 21data_augmentation = True 22num_predictions = 20 23save_dir = os.path.join(os.getcwd(), 'saved_models') 24model_name = 'keras_cifar10_trained_model.h5' 25 26# The data, split between train and test sets: 27(x_train, y_train), (x_test, y_test) = cifar10.load_data() 28print('x_train shape:', x_train.shape) 29print(x_train.shape[0], 'train samples') 30print(x_test.shape[0], 'test samples') 31 32# Convert class vectors to binary class matrices. 33y_train = keras.utils.to_categorical(y_train, num_classes) 34y_test = keras.utils.to_categorical(y_test, num_classes) 35 36model = Sequential() 37model.add(Conv2D(32, (3, 3), padding='same', 38 input_shape=x_train.shape[1:])) 39model.add(Activation('relu')) 40model.add(Conv2D(32, (3, 3))) 41model.add(Activation('relu')) 42model.add(MaxPooling2D(pool_size=(2, 2))) 43model.add(Dropout(0.25)) 44 45model.add(Conv2D(64, (3, 3), padding='same')) 46model.add(Activation('relu')) 47model.add(Conv2D(64, (3, 3))) 48model.add(Activation('relu')) 49model.add(MaxPooling2D(pool_size=(2, 2))) 50model.add(Dropout(0.25)) 51 52model.add(Flatten()) 53model.add(Dense(512)) 54model.add(Activation('relu')) 55model.add(Dropout(0.5)) 56model.add(Dense(num_classes)) 57model.add(Activation('softmax')) 58 59# initiate RMSprop optimizer 60opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6) 61 62# Let's train the model using RMSprop 63model.compile(loss='categorical_crossentropy', 64 optimizer=opt, 65 metrics=['accuracy']) 66 67x_train = x_train.astype('float32') 68x_test = x_test.astype('float32') 69x_train /= 255 70x_test /= 255 71 72if not data_augmentation: 73 print('Not using data augmentation.') 74 model.fit(x_train, y_train, 75 batch_size=batch_size, 76 epochs=epochs, 77 validation_data=(x_test, y_test), 78 shuffle=True) 79else: 80 print('Using real-time data augmentation.') 81 # This will do preprocessing and realtime data augmentation: 82 datagen = ImageDataGenerator( 83 featurewise_center=False, # set input mean to 0 over the dataset 84 samplewise_center=False, # set each sample mean to 0 85 featurewise_std_normalization=False, # divide inputs by std of the dataset 86 samplewise_std_normalization=False, # divide each input by its std 87 zca_whitening=False, # apply ZCA whitening 88 zca_epsilon=1e-06, # epsilon for ZCA whitening 89 rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180) 90 # randomly shift images horizontally (fraction of total width) 91 width_shift_range=0.1, 92 # randomly shift images vertically (fraction of total height) 93 height_shift_range=0.1, 94 shear_range=0., # set range for random shear 95 zoom_range=0., # set range for random zoom 96 channel_shift_range=0., # set range for random channel shifts 97 # set mode for filling points outside the input boundaries 98 fill_mode='nearest', 99 cval=0., # value used for fill_mode = "constant" 100 horizontal_flip=True, # randomly flip images 101 vertical_flip=False, # randomly flip images 102 # set rescaling factor (applied before any other transformation) 103 rescale=None, 104 # set function that will be applied on each input 105 preprocessing_function=None, 106 # image data format, either "channels_first" or "channels_last" 107 data_format="channels_last", 108 # fraction of images reserved for validation (strictly between 0 and 1) 109 validation_split=0.0) 110 111 # Compute quantities required for feature-wise normalization 112 # (std, mean, and principal components if ZCA whitening is applied). 113 datagen.fit(x_train) 114 115 # Fit the model on the batches generated by datagen.flow(). 116 model.fit_generator(datagen.flow(x_train, y_train, 117 batch_size=batch_size), 118 steps_per_epoch=int(np.ceil(x_train.shape[0] / float(batch_size))), 119 epochs=epochs, 120 validation_data=(x_test, y_test), 121 workers=4) 122 123# Save model and weights 124if not os.path.isdir(save_dir): 125 os.makedirs(save_dir) 126model_path = os.path.join(save_dir, model_name) 127model.save(model_path) 128print('Saved trained model at %s ' % model_path) 129 130# Score trained model. 131scores = model.evaluate(x_test, y_test, verbose=1) 132print('Test loss:', scores[0]) 133print('Test accuracy:', scores[1])
参照した記事
何が原因なのか、対策法は何か、教えていただけると幸いです。
よろしくお願いします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/06 13:35