def main(): # image shape: N x H x W, pixel [0, 255] # label shape: N x 10 with np.load('mnist.npz', allow_pickle=True) as f: x_train, y_train = f['x_train'], f['y_train'] x_test, y_test = f['x_test'], f['y_test'] print(x_train.shape, x_train[0].max(), x_train[0].min()) #(60000, 28, 28) 255 0 5 print(x_test.shape, x_test[0].max(), x_test[0].min()) #(10000, 28, 28) 255 0 7 x_train = normalize_image(x_train) x_test = normalize_image(x_test) y_train = one_hot_labels(y_train) y_test = one_hot_labels(y_test) lr = 1.5e-4 batch_size = 8 net = LeNet() avgtime, accuracy = \ net.fit(x_train, y_train, x_test, y_test, epoches=5, batch_size=batch_size, lr=lr) accu = net.evaluate(x_test, labels=y_test) print('avgtime: ', avgtime) #print('accuracy: ', accuracy) ''' plt.plot(accuracy) plt.savefig('lr={}.jpg'.format(lr)) plt.show() ''' print("final accuracy {}".format(accu))
def main(): # image shape: N x H x W, pixel [0, 255] # label shape: N x 10 with np.load('mnist.npz', allow_pickle=True) as f: x_train, y_train = f['x_train'], f['y_train'] x_test, y_test = f['x_test'], f['y_test'] plt.imshow(x_train[59999], cmap='gray') plt.show() print(x_train.shape, x_train[0].max(), x_train[0].min()) #(60000, 28, 28) 255 0 5 print(x_test.shape, x_test[0].max(), x_test[0].min()) #(10000, 28, 28) 255 0 7 x_train = normalize_image(x_train) x_test = normalize_image(x_test) y_train = one_hot_labels(y_train) y_test = one_hot_labels(y_test) net = LeNet() net.fit(x_train, y_train, x_test, y_test, epoches=10, batch_size=16, lr=1e-3) accu = net.evaluate(x_test, labels=y_test) print("final accuracy {}".format(accu))
def main(): batch_size = 128 epoch = 15 data = DATA() model = LeNet(data.input_shape, data.num_classes) hist = model.fit(data.x_train, data.y_train, batch_size=batch_size, epochs=epoch, validation_split=0.2) score = model.evaluate(data.x_test, data.y_test, batch_size=batch_size) print() print('Test Loss= ', score) plot_loss(hist) plt.show()
from lenet import LeNet from sklearn.cross_validation import train_test_split from keras.optimizers import SGD from keras.utils import np_utils from sys import argv print("[INFO] loading DATA...") dataset = np.load(argv[1]) labelset = np.load(argv[2]) data = dataset[:, :, :, np.newaxis] (trainData, testData, trainLabels, testLabels) = train_test_split( data, labelset, test_size=0.2) trainLabels = np_utils.to_categorical(trainLabels) testLabels = np_utils.to_categorical(testLabels) print("[INFO] compiling model...") opt = SGD(lr=0.01) model = LeNet(width=dataset.shape[1], height=dataset.shape[2], depth=1, classes=len(np.unique(labelset))) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) model.fit(trainData, trainLabels, batch_size=128, nb_epoch=20, verbose=1) print("[INFO] evaluating...") (loss, accuracy) = model.evaluate(testData, testLabels, batch_size=128, verbose=1) print("\n[INFO] accuracy: {:.2f}%".format(accuracy * 100))