# partition the data into training and testing splits using 75% of # the data for training and the remaining 25% for testing (trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.25, random_state=42) # convert the labels from integers to vectors trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) # initialize the optimizer and model print("[INFO] compiling model...") opt = SGD(lr=0.005) model = ShallowNet.build(width=32, height=32, depth=3, classes=3) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the network print("[INFO] training network...") H = model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=32, epochs=100, verbose=1) # save the network to disk print("[INFO] serializing network...")
from keras.models import load_model import matplotlib.pyplot as plt import numpy as np from shallownet import ShallowNet (X_train, y_train), (X_test, y_test) = cifar10.load_data() X_train = X_train.astype('float')/255 X_test = X_test.astype('float')/255 lb = LabelBinarizer() y_train = lb.fit_transform(y_train) y_test = lb.transform(y_test) model = ShallowNet.build(32, 32, 3, 10) sgd = SGD() model.compile(sgd, 'categorical_crossentropy', ['accuracy']) H = model.fit(X_train, y_train, 64, 30, validation_data=(X_test, y_test)) #save and load model model.save('./model/shallow_weight.hdf5') model = load_model('./model/shallow_weight.hdf5') # model.predict.... # graph of loss and accuracy fig = plt.figure() plt.plot(np.arange(0, 30), H.history['loss'], label='training loss') plt.plot(np.arange(0, 30), H.history['val_loss'], label='validation loss') plt.plot(np.arange(0, 30), H.history['acc'], label='accuracy') plt.plot(np.arange(0, 30), H.history['val_acc'], label='validation accuracy')
((X_train, y_train), (X_test, y_test)) = cifar10.load_data() X_train = X_train.astype("float") / 255.0 X_test = X_test.astype("float") / 255.0 lb = LabelBinarizer() y_train = lb.fit_transform(y_train) y_test = lb.transform(y_test) labelNames = [ "airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck" ] print("[INFO] compiling model...") opt = SGD(lr=0.01) model = ShallowNet.build(height=32, width=32, depth=3, classes=10) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) print("[INFO] training network...") H = model.fit(X_train, y_train, validation_data=(X_test, y_test), batch_size=32, epochs=40, verbose=1) print("[INFO] evaluating network...") predictions = model.predict(X_test, batch_size=32) print(
## construct arguments parser = argparse.ArgumentParser() parser.add_argument("-i", "--include_top", type=int, default=1, \ help="1/-1 indicates whether to include the head of neural network or not") parser.add_argument("-m", "--model", type=str, default="vgg16", \ help="which model model to inspect") args = vars(parser.parse_args()) networkBanks = { "vgg16" : VGG16(weights="imagenet", include_top=args["include_top"] > 0), "vgg19" : VGG19(weights="imagenet", include_top=args["include_top"] > 0), "resnet50" : ResNet50(weights="imagenet", \ include_top=args["include_top"] > 0), "inceptionv3" : InceptionV3(weights="imagenet", \ include_top=args["include_top"] > 0), "shallownet" : ShallowNet.build(height=28, width=28, depth=3, classes=10), "lenet" : LeNet.build(height=28, width=28, depth=3, classes=10), "minivgg" : MiniVGGNet.build(height=28, width=28, depth=3, classes=10), #"kfer_lenet" : KFER_LeNet.build(height=48, width=48, depth=3, classes=7), } ## loading network print("[INFO] loading network =", args["model"]) model = networkBanks[args["model"]] # inspect layers for i, layer in enumerate(model.layers): print("[INFO] {}\t{}".format(i, layer.__class__.__name__))