import numpy as npimport kerasfrom keras.models import Sequentialfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2Dfrom keras.layers.normalization import BatchNormalizationfrom keras.preprocessing.image import ImageDataGeneratorfrom keras.callbacks import ReduceLROnPlateau
img_width, img_height = 28, 28batch_size = 64num_classes = 10epochs = 20input_shape = (img_width, img_height, 3)train_data_dir = 'S:/mnist_png/training'
model = Sequential()model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal',input_shape=input_shape))model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal'))model.add(MaxPool2D((2, 2)))model.add(Dropout(0.20))model.add(Conv2D(64, (3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))model.add(Conv2D(64, (3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))model.add(MaxPool2D(pool_size=(2, 2)))model.add(Dropout(0.25))model.add(Conv2D(128, (3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))model.add(Dropout(0.25))model.add(Flatten())model.add(Dense(128, activation='relu'))model.add(BatchNormalization())model.add(Dropout(0.25))model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.RMSprop(), metrics=['accuracy'])
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', patience=3, verbose=1, factor=0.5, min_lr=0.0001)
datagen = ImageDataGenerator( rescale=1. / 255, #normalization featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization=False, # divide inputs by std of the dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening rotation_range=15, # randomly rotate images in the range (degrees, 0 to 180) zoom_range = 0.1, # Randomly zoom image width_shift_range=0.1, # randomly shift images horizontally (fraction of total width) height_shift_range=0.1, # randomly shift images vertically (fraction of total height) horizontal_flip=False, # randomly flip images vertical_flip=False, # randomly flip images validation_split=0.1) train_generator = datagen.flow_from_directory( train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical')
h=model.fit_generator( train_generator, steps_per_epoch=60000//batch_size, epochs=epochs, verbose=1, callbacks=[learning_rate_reduction],)
test_data_dir = 'S:/mnist_png/testing'
test_datagen = ImageDataGenerator( rescale=1. / 255)
test_generator = test_datagen.flow_from_directory( test_data_dir, target_size=(img_width, img_height), batch_size=batch_size, shuffle='False', class_mode='categorical')
#Evaluate model on test setscores = model.evaluate_generator(test_generator,workers=12)
test_generator.reset() #Necessary to force it to start from beginningY_pred = model.predict_generator(test_generator)y_pred = np.argmax(Y_pred, axis=-1)sum(y_pred==test_generator.classes)/10000
from sklearn.metrics import confusion_matrixconfusion_matrix(test_generator.classes,y_pred)
array([[104, 117, 109, 100, 93, 105, 95, 88, 75, 94], [109, 134, 101, 122, 121, 100, 105, 106, 115, 122], [106, 116, 113, 82, 112, 105, 98, 92, 104, 104], [ 83, 124, 92, 118, 90, 71, 101, 132, 97, 102], [101, 105, 104, 107, 107, 76, 91, 109, 98, 84], [ 89, 92, 98, 76, 85, 71, 97, 93, 102, 89], [ 88, 111, 106, 105, 81, 89, 76, 104, 88, 110], [ 87, 116, 95, 103, 104, 88, 102, 122, 99, 112], [104, 113, 100, 92, 105, 96, 92, 87, 92, 93], [112, 104, 115, 96, 91, 95, 102, 95, 103, 96]], dtype=int64)
--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/ce9172e9-2c34-4a04-8319-db322c449c74%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Perfect, that's got it - thanks! I figured I must be messing up the indexing somewhere.
To unsubscribe from this group and stop receiving emails from it, send an email to keras...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/0f5d6325-cc19-4171-94d1-119754f39b50%40googlegroups.com.
eval_idg = ImageDataGenerator(rescale=1. / 255)
eval_g = eval_idg.flow_from_directory(directory=r'C:/Users/admin/Downloads/plantdisease_dataset/Testing',
target_size=(100, 100),
class_mode='binary',
batch_size=5,
shuffle=False)
eval_acc = my_model.evaluate_generator(eval_g, steps=1)
#print('evaluation Loss over never-before-seen images is: {:.4f}'.format(eval_loss))
print('evaluation Accuracy over never-before-seen images is: {:4.2f}%'.format(eval_acc*100), '\n')
# Individual Predictions
pred_idg = eval_idg
pred_g = eval_g
pred = my_model.predict_generator(pred_g, steps=1)
print(pred_g.filenames, '\n')
print(pred_g.class_indices, '\n')
print(pred[0:5], '\n')
To unsubscribe from this group and stop receiving emails from it, send an email to keras...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to keras...@googlegroups.com.