def main(): """Train and checkpoint a neural network """ # construct the argument parse and parse the arguments args = argparse.ArgumentParser() args.add_argument("-w", "--weights", required=True, help="path to weights directory") args = vars(args.parse_args()) # load the training and testing data, then scale it into the range [0, 1] print("[INFO] loading CIFAR-10 data...") ((train_x, train_y), (test_x, test_y)) = cifar10.load_data() train_x = train_x.astype("float") / 255.0 test_x = test_x.astype("float") / 255.0 # convert the labels from integers to vectors label_binarizer = LabelBinarizer() train_y = label_binarizer.fit_transform(train_y) test_y = label_binarizer.transform(test_y) # initialize the optimizer and model print("[INFO] compiling model...") opt = SGD(lr=0.01, decay=0.01 / 40, momentum=0.9, nesterov=True) model = MiniVGGNet.build(width=32, height=32, depth=3, classes=10) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # construct the callback to save only the *best* model to disk based on the validation loss fname = os.path.sep.join([args["weights"], "weights-{epoch:03d}-{val_loss:.4f}.hdf5"]) checkpoint = ModelCheckpoint(fname, monitor="val_loss", mode="min", save_best_only=True, verbose=1) callbacks = [checkpoint] # train the network print("[INFO] training network...") model.fit( train_x, train_y, validation_data=(test_x, test_y), batch_size=64, epochs=40, callbacks=callbacks, verbose=2 )
def main(): """Train and evaluate MiniVGGNet on Cifar10 with custom written learning rate schedule. """ # construct the argument parse and parse the arguments args = argparse.ArgumentParser() args.add_argument("-o", "--output", required=True, help="path to the output loss/accuracy plot") args = vars(args.parse_args()) # load the training and testing data, then scale it into the range [0, 1] print("[INFO] loading CIFAR-10 data...") ((train_x, train_y), (test_x, test_y)) = cifar10.load_data() train_x = train_x.astype("float") / 255.0 test_x = test_x.astype("float") / 255.0 # convert the labels from integers to vectors label_binarizer = LabelBinarizer() train_y = label_binarizer.fit_transform(train_y) test_y = label_binarizer.transform(test_y) # initialize the label names for the CIFAR-10 dataset label_names = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"] # define the set of callbacks to be passed to the model during training callbacks = [LearningRateScheduler(step_decay)] # initialize the optimizer and model opt = SGD(lr=0.01, momentum=0.9, nesterov=True) model = MiniVGGNet.build(width=32, height=32, depth=3, classes=10) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the network model_fit = model.fit(train_x, train_y, validation_data=(test_x, test_y), batch_size=64, epochs=40, callbacks=callbacks, verbose=1) # evaluate the network print("[INFO] evaluating network...") predictions = model.predict(test_x, batch_size=64) print(classification_report(test_y.argmax(axis=1), predictions.argmax(axis=1), target_names=label_names)) # plot the training loss and accuracy plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, 40), model_fit.history["loss"], label="train_loss") plt.plot(np.arange(0, 40), model_fit.history["val_loss"], label="val_loss") plt.plot(np.arange(0, 40), model_fit.history["acc"], label="train_acc") plt.plot(np.arange(0, 40), model_fit.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy on CIFAR-10") plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy") plt.legend() plt.savefig(args["output"])
lb = LabelBinarizer() trainY = lb.fit_transform(trainY) testY = lb.transform(testY) # initialize the label names from the CIFAR-10 dataset labelNames = [ "airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck" ] # initialie the optimizer and model print("[INFO] compiling model...") opt = SGD(lr=0.01, decay=0.01 / 40, momentum=0.9, nesterov=True) model = MiniVGGNet.build(width=32, height=32, depth=3, classes=10, batchNorm=False) 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=64, epochs=40, verbose=1)
"ship", "truck" ] # construct the image generator for data augmentation aug = ImageDataGenerator(rotation_range=10, width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True, fill_mode="nearest") # loop over the number of models to train for i in np.arange(0, args["num_models"]): # initialize the optimizer and model print("[INFO] training model {}/{}".format(i + 1, args["num_models"])) opt = SGD(lr=0.01, decay=0.01 / epochs, momentum=0.9, nesterov=True) model = MiniVGGNet.build(width=32, height=32, depth=3, classes=10) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the network H = model.fit_generator(aug.flow(trainX, trainY, batch_size=64), validation_data=(testX, testY), epochs=epochs, steps_per_epoch=len(trainX) // 64, verbose=1) # save the model to disk p = [args["models"], "model_{}.model".format(i)] model.save(os.path.sep.join(p))
sdl = SimpleDatasetLoader(preprocessors=[aap, iap]) (data, labels) = sdl.load(imagePaths, verbose=500) data = data.astype("float") / 255.0 (trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.25, random_state=42) trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) print("[INFO] compiling model...") opt = SGD(lr=0.05) model = MiniVGGNet.build(width=64, height=64, depth=3, classes=len(classNames)) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) print("[INFO] training network...") H = model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=32, epochs=100, verbose=1) print("[INFO] evaluating network...") predictions = model.predict(testX, batch_size=32) print(
from pyimagesearch.nn.conv import MiniVGGNet import argparse import os ap = argparse.ArgumentParser() ap.add_argument('-w', '--weights', required=True, help='path to weights directory') args = vars(ap.parse_args()) print("[INFO] loading data...") ((trainX, trainY), (testX, testY)) = cifar10.load_data() trainX = trainX.astype('float') / 255.0 testX = testX.astype('float') / 255.0 lb = LabelBinarizer() trainY = lb.fit_transform(trainY) testY = lb.transform(testY) print("[INFO] compiling model...") opt = SGD(lr=0.01, decay=0.01/40, momentum=0.9, nesterov=True) model = MiniVGGNet.build(32, 32, 3, 10) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['acc']) checkpoints = ModelCheckpoint(args['weights'], monitor='val_loss', save_best_only=True, verbose=1) print('[INFO] training model...') model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=32, callbacks=[checkpoints], verbose=1, epochs=40)
# initialize label names label_names = [ 'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck' ] # convert it to vector lb = LabelBinarizer() trainY = lb.fit_transform(trainY) testY = lb.transform(testY) # initialize optimizer and network print('[INFO] compiling the network...') opt = SGD(lr=0.01, momentum=0.9, nesterov=True) model = MiniVGGNet.build(width=32, height=32, depth=3, classes=len(label_names)) model.compile(opt, loss='categorical_crossentropy', metrics=['accuracy']) # construct callbacks to be passed to the network fig_path = os.path.sep.join([args['output'], f'{os.getpid()}.png']) json_path = os.path.sep.join([args['output'], f'{os.getpid()}.json']) callbacks = [TrainingMonitor(fig_path, json_path)] # training the network print('[INFO] training the network...') model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=64, epochs=100,
# convert the labels from integer to vector le = LabelBinarizer() trainY = le.fit_transform(trainY) testY = le.transform(testY) # initialize the label names for the CIFAR-10 dataset label_names = [ 'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck' ] # initialize optimizer and model print('[INFO] compiling model...') opt = SGD(lr=0.01, decay=0.01 / 40, momentum=0.9, nesterov=True) model = MiniVGGNet.build(width=32, height=32, depth=3, classes=len(le.classes_)) model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) # construct callback to save only the **best** model to disk based on validation loss checkpoint = ModelCheckpoint(args['weights'], monitor='val_loss', verbose=1, save_best_only=True) callbacks = [checkpoint] # train the network print('[INFO] training network...') H = model.fit(trainX,
def main(): """Run image classification """ # construct the argument parse and parse the arguments args = argparse.ArgumentParser() args.add_argument("-d", "--dataset", required=True, help="path to input dataset") args = vars(args.parse_args()) # grab the list of images that we'll be describing, then extract # the class label names from the image paths print("[INFO] loading images...") image_paths = list(paths.list_images(args["dataset"])) class_names = [pt.split(os.path.sep)[-2] for pt in image_paths] class_names = [str(x) for x in np.unique(class_names)] # initialize the image preprocessors aspect_aware_preprocessor = AspectAwarePreprocessor(64, 64) image_to_array_preprocessor = ImageToArrayPreprocessor() # load the dataset from disk then scale the raw pixel intensities to the range [0, 1] simple_dataset_loader = SimpleDatasetLoader( preprocessors=[aspect_aware_preprocessor, image_to_array_preprocessor]) (data, labels) = simple_dataset_loader.load(image_paths, verbose=500) data = data.astype("float") / 255.0 # partition the data into training and testing splits using 75% of # the data for training and the remaining 25% for testing (train_x, test_x, train_y, test_y) = train_test_split(data, labels, test_size=0.25, random_state=42) # convert the labels from integers to vectors train_y = LabelBinarizer().fit_transform(train_y) test_y = LabelBinarizer().fit_transform(test_y) # initialize the optimizer and model print("[INFO] compiling model...") opt = SGD(lr=0.05) model = MiniVGGNet.build(width=64, height=64, depth=3, classes=len(class_names)) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the network print("[INFO] training network...") model_fit = model.fit(train_x, train_y, validation_data=(test_x, test_y), batch_size=32, epochs=100, verbose=1) # evaluate the network print("[INFO] evaluating network...") predictions = model.predict(test_x, batch_size=32) print( classification_report(test_y.argmax(axis=1), predictions.argmax(axis=1), target_names=class_names)) # plot the training loss and accuracy plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, 100), model_fit.history["loss"], label="train_loss") plt.plot(np.arange(0, 100), model_fit.history["val_loss"], label="val_loss") plt.plot(np.arange(0, 100), model_fit.history["acc"], label="train_acc") plt.plot(np.arange(0, 100), model_fit.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy") plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy") plt.legend() plt.show()
(data, label) = sdl.load(imagePaths, verbose=500) data = data.astype('float') / 255.0 (trainX, testX, trainY, testY) = train_test_split(data, label, test_size=0.25, random_state=42) trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) print('[INFO] initializing model....') opt = SGD(lr=0.05) model = MiniVGGNet.build(64, 64, 3, classes=len(classNames)) model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['acc']) # construct image generator for augmentations aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest') print('[INFO] training model...') H = model.fit_generator(aug.flow(trainX, trainY, batch_size=32), validation_data=(testX, testY), steps_per_epoch=len(trainX) // 32,
def main(): """Train and evaluate MiniVGGNet using TrainigMonitor callback. """ # construct the argument parse and parse the arguments args = argparse.ArgumentParser() args.add_argument("-o", "--output", required=True, help="path to the output directory") args = vars(args.parse_args()) # show information on the process ID print("[INFO process ID: {}".format(os.getpid())) # load the training and testing data, then scale it into the # range [0, 1] print("[INFO] loading CIFAR-10 data...") ((train_x, train_y), (test_x, test_y)) = cifar10.load_data() train_x = train_x.astype("float") / 255.0 test_x = test_x.astype("float") / 255.0 # convert the labels from integers to vectors label_binarizer = LabelBinarizer() train_y = label_binarizer.fit_transform(train_y) test_y = label_binarizer.transform(test_y) # initialize the label names for the CIFAR-10 dataset label_names = [ "airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck" ] # initialize the SGD optimizer, but without any learning rate decay print("[INFO] compiling model...") opt = SGD(lr=0.01, momentum=0.9, nesterov=True) model = MiniVGGNet.build(width=32, height=32, depth=3, classes=10) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # construct the set of callbacks fig_path = os.path.sep.join([args["output"], "{}.png".format(os.getpid())]) json_path = os.path.sep.join( [args["output"], "{}.json".format(os.getpid())]) callbacks = [TrainingMonitor(fig_path, json_path=json_path)] # train the network print("[INFO] training network...") model.fit(train_x, train_y, validation_data=(test_x, test_y), batch_size=64, epochs=100, callbacks=callbacks, verbose=1) # evaluate the network print("[INFO] evaluating network...") predictions = model.predict(test_x, batch_size=64) print( classification_report(test_y.argmax(axis=1), predictions.argmax(axis=1), target_names=label_names))
def main(): """Train ensemble model. """ # construct the argument parse and parse the arguments args = argparse.ArgumentParser() args.add_argument("-o", "--output", required=True, help="path to output directory") args.add_argument("-m", "--models", required=True, help="path to output models directory") args.add_argument("-n", "--num-models", type=int, default=5, help="# of models to train") args = vars(args.parse_args()) # load the training and testing data, then scale it into the range [0, 1] ((train_x, train_y), (test_x, test_y)) = cifar10.load_data() train_x = train_x.astype("float") / 255.0 test_x = test_x.astype("float") / 255.0 # convert the labels from integers to vectors label_binarizer = LabelBinarizer() train_y = label_binarizer.fit_transform(train_y) test_y = label_binarizer.transform(test_y) # initialize the label names for the CIFAR-10 dataset label_names = [ "airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck" ] # construct the image generator for data augmentation augmentation = ImageDataGenerator(rotation_range=10, width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True, fill_mode="nearest") # loop over the number of models to train for i in np.arange(0, args["num_models"]): # initialize the optimizer and model print("[INFO] training model {}/{}".format(i + 1, args["num_models"])) opt = SGD(lr=0.01, decay=0.01 / 40, momentum=0.9, nesterov=True) model = MiniVGGNet.build(width=32, height=32, depth=3, classes=10) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the network model_fit = model.fit_generator( augmentation.flow(train_x, train_y, batch_size=64), validation_data=(test_x, test_y), epochs=40, steps_per_epoch=len(train_x) // 64, verbose=1, ) # save the model to disk path = [args["models"], "model_{}.model".format(i)] model.save(os.path.sep.join(path)) # evaluate the network predictions = model.predict(test_x, batch_size=64) report = classification_report(test_y.argmax(axis=1), predictions.argmax(axis=1), target_names=label_names) # save the classification report to file path = [args["output"], "model_{}.txt".format(i)] f = open(os.path.sep.join(path), "w") f.write(report) f.close() # plot the training loss and accuracy path = [args["output"], "model_{}.png".format(i)] plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, 40), model_fit.history["loss"], label="train_loss") plt.plot(np.arange(0, 40), model_fit.history["val_loss"], label="val_loss") plt.plot(np.arange(0, 40), model_fit.history["acc"], label="train_acc") plt.plot(np.arange(0, 40), model_fit.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy for model {}".format(i)) plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy") plt.legend() plt.savefig(os.path.sep.join(path)) plt.close()
'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck' ] # Construct image generator for data augmentation aug = ImageDataGenerator(rotation_range=10, width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True) # loop over number of models to train for i in range(0, args['num_models']): # initialize optimizer and compile model print(f"[INFO] training model {i+1}/{args['num_models']}...") opt = SGD(lr=0.01, decay=0.01 / 40, momentum=0.9, nesterov=True) model = MiniVGGNet.build(32, 32, 3, len(label_names)) model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) # train network H = model.fit_generator(aug.flow(trainX, trainY, batch_size=64), validation_data=(testX, testY), steps_per_epoch=len(trainX) // 64, epochs=40, verbose=2) # save model to disk model.save(os.path.sep.join([args['models'], f'model_{i}.model'])) # evaluate model