def labels_binarizer(Y_train, Y_valid, Y_test): # update each image label to its binary reporesentation Y_train = LabelBinarizer().fit_transform(Y_train) Y_train = Y_train.argmax(axis=-1) # update each image label to its binary reporesentation Y_valid = LabelBinarizer().fit_transform(Y_valid) Y_valid = Y_valid.argmax(axis=-1) # update each image label to its binary reporesentation Y_test = LabelBinarizer().fit_transform(Y_test) Y_test = Y_test.argmax(axis=-1) return Y_train, Y_valid, Y_test
def main(): #loading in the 8x8 version of the dataset, as the whole dataset took too much time to run digits = datasets.load_digits() #Converting to floats data = digits.data.astype("float") #MinMax regularization data = (data - data.min()) / (data.max() - data.min()) #splitting data X_train, X_test, y_train, y_test = train_test_split(data, digits.target, test_size=0.2) #converting labels from integers to vectors y_train = LabelBinarizer().fit_transform(y_train) y_test = LabelBinarizer().fit_transform(y_test) #training network print("[INFO] training network...") nn = NeuralNetwork([X_train.shape[1], 32, 16, 10]) #here instead of putting in hte number of input, we just put the data in print("[INFO] {}".format(nn)) nn.fit(X_train, y_train, epochs=1000) #evaluating network print(["[INFO] evaluating network..."]) predictions = nn.predict(X_test) predictions = predictions.argmax(axis=1) print(classification_report(y_test.argmax(axis=1), predictions))
def run(): # load the MNIST dataset and apply min / max scaling to scale the # pixel intensity values to the range [0, 1] (each image is # represented by an 8 x 8 = 64-dim feature vector) print("[INFO] loading MNIST (sample dataset)") digits = datasets.load_digits() data = digits.data.astype("float") data = (data - data.min()) / (data.max() - data.min()) print(f"[INFO] samples: {data.shape[0]}, dim: {data.shape[1]}") # construct the training and testing splits (trainX, testX, trainY, testY) = train_test_split(data, digits.target, test_size=0.25) # convert the labels from integers to vectors trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) # train the network print("[INFO] training the network") nn = NeuralNetwork([trainX.shape[1], 32, 16, 10]) print(f"[INFO] {nn}") nn.fit(trainX, trainY, epochs=1000) # evaluate the network print("[INFO] evaluating network...") predictions = nn.predict(testX) predictions = predictions.argmax(axis=1) print(classification_report(testY.argmax(axis=1), predictions))
def main(): """Train neural network implementation on the MNIST dataset. """ # load the MNIST dataset and apply min/max scaling to scale the pixel intensity values # to the range [0, 1] (each image is represented by an 8 x 8 = 64-dim feature vector) print("[INFO] loading MNIST (sample) dataset...") digits = datasets.load_digits() data = digits.data.astype("float") # pylint: disable=no-member data = (data - data.min()) / (data.max() - data.min()) print("[INFO] samples: {}, dim: {}".format(data.shape[0], data.shape[1])) # construct the training and testing splits (train_x, test_x, train_y, test_y) = \ train_test_split(data, digits.target, test_size=0.25) # pylint: disable=no-member # convert the labels from integers to vectors using one-hot-encoding train_y = LabelBinarizer().fit_transform(train_y) test_y = LabelBinarizer().fit_transform(test_y) # train the network print("[INFO] training network...") network = NeuralNetwork([train_x.shape[1], 32, 16, 10]) print("[INFO] {}".format(network)) network.fit(train_x, train_y, epochs=1000) # evaluate the network print("[INFO] evaluating network...") predictions = network.predict(test_x) predictions = predictions.argmax(axis=1) print(classification_report(test_y.argmax(axis=1), predictions))
def main(): """Train ShallowNet on animals dataset. """ # construct the argument parser 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 print("[INFO] loading images...") image_paths = list(paths.list_images(args["dataset"])) # initialize the image preprocessors simple_preprocessor = SimplePreprocessor(32, 32) image_to_array_preprocessor = ImageToArrayPreprocessor() # load the dataset from disk then scale the raw pixel intensities to the range [0, 1] dataset_loader = SimpleDatasetLoader(preprocessors=[simple_preprocessor, image_to_array_preprocessor]) (data, labels) = 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.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...") 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=["cat", "dog", "panda"]) ) # 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()
def main(): #fetching data, where X is the dataset and y is labels of the data X, y = fetch_openml('mnist_784', version=1, return_X_y=True) #converting data into numpy arrays and data type float, bc otherwise the model won't converge later X = np.array(X, dtype=np.float128) y = np.array(y, dtype=np.float128) #predefining classes classes = sorted(set(y)) nclasses = len(classes) # MinMax regularization X = (X - X.min())/(X.max() - X.min()) #splitting data into training and test dataset X_train, X_test, y_train, y_test = train_test_split(X, #our data y, #labels random_state=9, #makes it reproducible train_size=0.8, #splitting by 80%-20% test_size=0.2) #converting labels from integers to vectors y_train = LabelBinarizer().fit_transform(y_train) y_test = LabelBinarizer().fit_transform(y_test) #training network (from 784 nodes to 10) print("[INFO] training network...") nn = NeuralNetwork([X_train.shape[1], 400, 120, 10]) print("[INFO] {}".format(nn)) nn.fit(X_train, y_train, epochs=500) #evaluating network print(["[INFO] evaluating network..."]) predictions = nn.predict(X_test) predictions = predictions.argmax(axis=1) print(classification_report(y_test.argmax(axis=1), predictions))
def train(dataset_shape, dataste_path, test_size, img_width, img_height, img_depth, classes, batch_size, epochs, target_names): print("[INFO] accessing images ....") image_paths = list(paths.list_images(dataset_path)) dataset_loader = image_processing.DatasetLoader(image_paths, dataset_shape, verbose=1) (images, labels) = dataset_loader.load() images = images.astype("float") / 255.0 print("[INFO] spliting images for train set and test set ....") (trainX, testX, trainY, testY) = train_test_split(images, labels, test_size=test_size, random_state=42) print("[INFO] converting labels to vectors ....") trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) print("[INFO] loading model ....") optimizer = SGD(learning_rate=0.005) model = KNeuralNet.build(width=img_width, height=img_height, depth=img_depth, classes=classes) model.compile(optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"]) print("[INFO] training network ....") train = model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=batch_size, epochs=epochs, verbose=1) print("[INFO] saving model ....") model.save("model/k_neural_net.hdf5") print("[INFO] evaluating network ....") predictions = model.predict(testX, batch_size=batch_size) print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=target_names)) plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, epochs), train.history["loss"], label="train_loss") plt.plot(np.arange(0, epochs), train.history["val_loss"], label="val_loss") plt.plot(np.arange(0, epochs), train.history["accuracy"], label="train_acc") plt.plot(np.arange(0, epochs), train.history["val_accuracy"], label="val_acc") plt.title("Training loss and accuracy") plt.xlabel("Epoch ") plt.ylabel("Loss/Accuracy") plt.legend() plt.show()
def main(data_path, epochs): # Load data as np arrays img, label = load_mnist(data_path) # We are assuming the min and max values for pixel intensities # are between 0 and 255. The minmax normalization from session 7 # might give values between say 10 and 230, which might not work # well when given a new image that has pixel values above or below those img = img / 255.0 # normalize pixel vals to between 0 and 1 as float classes = sorted(set(label)) num_classes = len(classes) # Split our data 80/20 - train/test img_train, img_test, label_train, label_test = train_test_split( img, label, random_state=1337, test_size=0.2) # Convert labels to binary representation (e.g. 2 becomes [0,0,1,0,0,0,0,0,0,0]) label_train = LabelBinarizer().fit_transform(label_train) label_test = LabelBinarizer().fit_transform(label_test) # Specify the neural network structure neural_network = NeuralNetwork([ img_train.shape[1], 32, 16, num_classes ]) # 1 input node for every pixel in images, 1 output node for every class # Train the model neural_network.fit(img_train, label_train, epochs=epochs) # Make predictions on all test images label_pred = neural_network.predict(img_test) label_pred = label_pred.argmax( axis=1) # Give us the highest probability label # Generate comparative metrics with test data classifier_metrics = metrics.classification_report( label_test.argmax(axis=1), label_pred) print(classifier_metrics)
def main(): ''' --------------Defining command line arguments--------------- ''' ap = argparse.ArgumentParser( description="[INFO] creating benchmark classifier") #1. argument: Number of hidden layers in first pile of hidden layers: ap.add_argument( "-hl1", #flag "--hidden_layer_1", required=False, # You do not need to give any arguments, but you can. default=32, # If you don't the default is 32 hiddenlayers. type=int, # The input has has to be a integer. help="The the number of hidden layers.") #2. argument: Number of hidden layers in second pile of hidden layers: ap.add_argument( "-hl2", #flag "--hidden_layer_2", required=False, # You do not need to give any arguments, but you can. default=0, type=int, # The input has has to be a integer. help="The the number of hidden layers.") #3. argument: Number of hidden layers in third pile of hidden layers: ap.add_argument( "-hl3", #flag "--hidden_layer_3", required=False, # You do not need to give any arguments, but you can. default=0, type=int, # The input has has to be a integer. help="The the number of hidden layers.") #4. argument: number of epochs the data should train on: ap.add_argument("-epochs", "--number_of_epochs", required=False, default=100, type=int, help="The number of times the data is run through") args = vars(ap.parse_args()) # Putting the arguments into variables: hidden_layer_1 = args["hidden_layer_1"] hidden_layer_2 = args["hidden_layer_2"] hidden_layer_3 = args["hidden_layer_3"] number_of_epochs = args["number_of_epochs"] ''' -----------------------Downloading and cleaning data--------------------------- ''' # Fetching/downloading data set X, y = fetch_openml('mnist_784', version=1, return_X_y=True) #X is the the images, y is the the category. X = np.array(X) y = np.array(y) X = (X - X.min()) / (X.max() - X.min()) #Creating training and test data. X_train, X_test, y_train, y_test = train_test_split( X, y, #random_state=9, train_size=7500, test_size=2500) # convert labels from integers to vectors y_train = LabelBinarizer().fit_transform(y_train) y_test = LabelBinarizer().fit_transform(y_test) ''' --------------------Creating different options for piles of hidden layers-------------------- ''' #train network: # If only 1 argument of hidden layers are givin: if hidden_layer_1 > 0 and hidden_layer_2 == 0 and hidden_layer_3 == 0: print("[INFO] training network...") nn = NeuralNetwork([X_train.shape[1], hidden_layer_1, 10]) #CLI-argument print("[INFO] {}".format(nn)) nn.fit(X_train, y_train, epochs=number_of_epochs) #CLI-argument print("___________ 2 args _____________" ) #For my self so i can see that the code took 2 arguments # evaluate network print(["[INFO] evaluating network..."]) predictions = nn.predict(X_test) predictions = predictions.argmax(axis=1) print(classification_report(y_test.argmax(axis=1), predictions)) # If 2 argument of hidden layers are givin: elif hidden_layer_1 > 0 and hidden_layer_2 > 0 and hidden_layer_3 == 0: print("[INFO] training network...") nn = NeuralNetwork( [X_train.shape[1], hidden_layer_1, hidden_layer_2, 10]) #CLI-argument print("[INFO] {}".format(nn)) nn.fit(X_train, y_train, epochs=number_of_epochs) #CLI-argument print("___________ 3 args _____________" ) #For my self so i can see that the code took 3 arguments # evaluate network print(["[INFO] evaluating network..."]) predictions = nn.predict(X_test) predictions = predictions.argmax(axis=1) print(classification_report(y_test.argmax(axis=1), predictions)) # If 3 argument of hidden layers are givin: elif hidden_layer_1 > 0 and hidden_layer_2 > 0 and hidden_layer_3 > 0: print("[INFO] training network...") nn = NeuralNetwork([ X_train.shape[1], hidden_layer_1, hidden_layer_2, hidden_layer_3, 10 ]) #CLI-argument print("[INFO] {}".format(nn)) nn.fit(X_train, y_train, epochs=number_of_epochs) #CLI-argument print("___________ 4 args _____________" ) #For my self so i can see that the code took 4 arguments # evaluate network print(["[INFO] evaluating network..."]) predictions = nn.predict(X_test) predictions = predictions.argmax(axis=1) print(classification_report(y_test.argmax(axis=1), predictions))
train_y = LabelBinarizer().fit_transform(train_y) test_y = LabelBinarizer().fit_transform(test_y) # Initialize the optimizer and model print('[INFO]: Compiling model....') optimizer = SGD(lr=0.005) model = ShallowNet.build(width=32, height=32, depth=3, classes=3) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) # Train the network print('[INFO]: Training the network....') H = model.fit(train_x, train_y, validation_data=(test_x, test_y), batch_size=32, epochs=100, verbose=1) # Test the network print('[INFO]: Evaluating the network....') predictions = model.predict(test_x, batch_size=32) print(classification_report(test_y.argmax(axis=1), predictions.argmax(axis=1), target_names=['cat', 'dog', 'panda'])) # Plot the training loss and accuracy plt.style.use('ggplot') plt.figure() plt.plot(np.arange(0, 100), H.history['loss'], label='train_loss') plt.plot(np.arange(0, 100), H.history['val_loss'], label='val_loss') plt.plot(np.arange(0, 100), H.history['acc'], label='train_acc') plt.plot(np.arange(0, 100), H.history['val_acc'], label='val_acc') plt.title('Training Loss and Accuracy') plt.xlabel('Epoch #') plt.ylabel('Loss/Accuracy') plt.legend() plt.savefig('shallownet_animals.png')
def main(): """Use transfer learning and fine-tuning to train a network on a new dataset""" a = argparse.ArgumentParser() a.add_argument("-d", "--dataset", required=True, help="path to input dataset") a.add_argument("-m", "--model", required=True, help="output model file") a.add_argument("--plot", action="store_true") args = a.parse_args() if (not os.path.exists(args.dataset)): print("directories do not exist") sys.exit(1) # construct the image generator for data augmentation 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") # grab the list of images that we'll be describing, then extract # the class label names from the image paths print("[INFO] loading images...") imagePaths = list(paths.list_images(args.dataset)) classNames = [pt.split(os.path.sep)[-2] for pt in imagePaths] classNames = [str(x) for x in np.unique(classNames)] # initialize the image preprocessors aap = AspectAwarePreprocessor(IM_WIDTH, IM_HEIGHT) iap = ImageToArrayPreprocessor() # load the dataset from disk then scale the raw pixel intensities to # the range [0, 1] sdl = SimpleDatasetLoader(preprocessors=[aap, iap]) (data, labels) = sdl.load(imagePaths, 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 (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) # setup model base_model = InceptionV3( weights='imagenet', include_top=False) #include_top=False excludes final FC layer model = add_new_fc_layer(base_model, len(classNames)) # transfer learning by turning off all conv layers setup_to_transfer_learn(model, base_model) # train the head of the network for a few epochs (all other # layers are frozen) -- this will allow the new FC layers to # start to become initialized with actual "learned" values # versus pure random print("[INFO] training head...") history_tl = model.fit_generator(aug.flow(trainX, trainY, batch_size=16), validation_data=(testX, testY), epochs=TL_EPOCHS, steps_per_epoch=len(trainX) // 32, verbose=1) # evaluate the network after initialization print("[INFO] evaluating after initialization...") predictions = model.predict(testX, batch_size=16) print( classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=classNames)) # plot(history_tl, TL_EPOCHS, "inc_tl_plot.png") # fine-tuning setup_to_finetune(model) # for the changes to the model to take affect we need to recompile # the model, this time using SGD with a *very* small learning rate print("[INFO] re-compiling model...") opt = SGD(lr=0.001) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the model again, this time fine-tuning *both* the final set # of CONV layers along with our set of FC layers print("[INFO] fine-tuning model...") history_ft = model.fit_generator(aug.flow(trainX, trainY, batch_size=16), validation_data=(testX, testY), epochs=FT_EPOCHS, steps_per_epoch=len(trainX) // 16, verbose=1) # evaluate the network on the fine-tuned model print("[INFO] evaluating after fine-tuning...") predictions = model.predict(testX, batch_size=16) print( classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=classNames)) plot(history_ft, FT_EPOCHS, "inc_ft_plot.png") # save the model to disk print("[INFO] serializing model...") model.save(args.model)
validation=0.2, included_folders=classNames, only_val=True) testX, testY = c.load_and_split() # convert the labels from integers to vectors testY = LabelBinarizer().fit_transform(testY) lb = LabelBinarizer() testY = lb.fit_transform(testY) # evaluate the network on the fine-tuned model print("[INFO] evaluating after fine-tuning...") predictions = model.predict(testX, batch_size=batch_size) print( classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=classNames)) c_dict = classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=classNames, output_dict=True) avg_score = c_dict['macro avg']['recall'] ax = skplt.metrics.plot_confusion_matrix(testY.argmax(axis=1), predictions.argmax(axis=1), normalize=True, cmap='Blues', figsize=(12, 8)) ax.xaxis.set_ticklabels(classNames) ax.yaxis.set_ticklabels(classNames)
from sklearn import datasets from sklearn.preprocessing import LabelBinarizer from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report from pyimagesearch.nn.neuralnetwork import NeuralNetwork print('[INFO] loading MNIST (sample) dataset...') digits = datasets.load_digits() data = digits.data.astype('float') data = (data - data.min()) / (data.max() - data.min()) print('[INFO] samples: {}, dim: {}'.format(data.shape[0], data.shape[1])) (trainX, testX, trainY, testY) = train_test_split(data, digits.target, test_size=0.25) trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) print('[INFO] training network...') nn = NeuralNetwork([trainX.shape[1], 32, 16, 10]) print('[INFO] {}'.format(nn)) nn.fit(trainX, trainY, epochs=1000) print('[INFO] evaluating network...') predictions = nn.predict(testX) predictions = predictions.argmax(axis=1) print(classification_report(testY.argmax(axis=1), predictions))
print('[INFO] Training network...') H = model.fit(X_train, y_train, validation_data=(X_test, y_test), batch_size=64, epochs=100, verbose=1) print('[INFO] Serializing network...') model.save(arguments['model']) print('[INFO] Evaluating network...') predictions = model.predict(X_test, batch_size=64) print( classification_report(y_test.argmax(axis=1), predictions.argmax(axis=1), target_names=['cat', 'dog', 'panda'])) plt.style.use('ggplot') plt.figure() plt.plot(np.arange(0, 100), H.history['loss'], label='train_loss') plt.plot(np.arange(0, 100), H.history['val_loss'], label='val_loss') plt.plot(np.arange(0, 100), H.history['acc'], label='train_acc') plt.plot(np.arange(0, 100), H.history['val_acc'], label='val_acc') plt.title('Training loss and accuracy') plt.xlabel('Epoch #') plt.ylabel('Loss/Accuracy') plt.legend() plt.show()
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...") model.save(args["model"]) # evaluate the network print("[INFO] evaluating network...") predictions = model.predict(testX, batch_size=32) print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=["cat","dog","panda"])) # plot the training loss and accuracy plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0,100), H.history["loss"],label="train_loss") plt.plot(np.arange(0,100), H.history["val_loss"],label="val_loss") #print("history keys: %s" % list(H.history.keys())) plt.plot(np.arange(0,100), H.history["accuracy"],label="train_acc") plt.plot(np.arange(0,100), H.history["val_accuracy"],label="val_acc") plt.xlabel("Training Loss and Accuracy") plt.ylabel("Loss/Accuracy") plt.legend() plt.show()
# get the test images and labels classNames = list(os.listdir(DATASET_PATH)) c = CustomDatasetLoaderAndSplitter(DATASET_PATH, validation=0.2, test=0.1) testX, testY = c.load_and_split(only_test=True) testY = LabelBinarizer().fit_transform(testY) # evaluate the network on the fine-tuned model i = 1 preds = [] for m in models: print("[INFO] evaluating model {}...".format(i)) preds.append(m.predict(testX, batch_size=batch_size)) i += 1 print(preds[0].shape) predictions = np.copy(preds[0]) for i in range(1, len(preds)): predictions = predictions + preds[i] print(i) print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=classNames)) c_dict = classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=classNames, output_dict=True) avg_score = c_dict['macro avg']['recall'] ax = skplt.metrics.plot_confusion_matrix(testY.argmax(axis=1), predictions.argmax(axis=1), normalize=True, cmap='Blues', figsize=(12,8)) ax.xaxis.set_ticklabels(classNames); ax.yaxis.set_ticklabels(classNames) plt.setp(ax.get_xticklabels(), rotation=60, horizontalalignment='right') plt.tight_layout() ax.set_xlabel('Predicted label\nAverage score: {:.03f}'.format(avg_score)) plt.show()
# Train full model print("Training full model...") epoch_full=120 H=model.fit(aug.flow(trainX, trainY, batch_size=32), validation_data=(testX, testY), epochs=epoch_full, steps_per_epoch=len(trainX) // 32, callbacks=callbacks, verbose=1) #Load best model for evaluation purposes model_best = load_model(full_model_path) #Compute precision/recall, confusion matrix, accuracy/loss graphs; save them in models directory # evaluate train data print("[INFO] evaluating train data ...") predictions = model_best.predict(trainX, batch_size=16) class_report=classification_report(trainY.argmax(axis=1), predictions.argmax(axis=1), target_names=classNames) class_file=open(full_model_path + "_train_class_report.txt","w") class_file.write(class_report) class_file.close() print(class_report) #print(classification_report(np.argmax(testY,axis=1), predictions.argmax(axis=1),target_names=classNames)) print("Confusion matrix") print(classNames) con_mat=confusion_matrix(np.argmax(trainY,axis=1), predictions.argmax(axis=1)) #print(confusion_matrix(np.argmax(testY,axis=1), predictions.argmax(axis=1))) print(con_mat) #confusion_matrix(np.argmax(testY,axis=1), predictions.argmax(axis=1)) np.savetxt(full_model_path + "_train_confusion_matrix.csv", con_mat, delimiter=",") # evaluate test data
Y_test= LabelBinarizer().transform(Y_test) # Initialise the NN and optimiser opt = SGD(lr=LEARNING_RATE) nnarch = ShallowNetNN(num_classes=NUM_CLASSES) nnarch.build_model() nnarch.model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) MODEL_NAME = DATASET_NAME + "_" + nnarch.title # Train the network hist = nnarch.model.fit(X_train, Y_train, batch_size=BATCH_SIZE, epochs=NUM_EPOCH, validation_data=(X_test, Y_test), verbose=1) # Make predictions on the test set and print the results to the console preds = nnarch.model.predict(X_test, batch_size=BATCH_SIZE) print(classification_report(Y_test.argmax(axis=-1), preds.argmax(axis=1), target_names=ANIMALS_CLASS_NAMES)) # Plot the training results plot_training_history(hist, NUM_EPOCH, show=False, save_path=OUTPUT_PATH + MODEL_NAME, time_stamp=True) # Visualise a few random images (could be training and/or test images) imagePaths = np.array(list(paths.list_images(args["dataset"]))) idxs = np.random.randint(0, len(imagePaths), size=(10,)) # Load and preprocess as before (X, Y) = dl.load(imagePaths=imagePaths[idxs]) # Make predictions preds = nnarch.model.predict(X) # Display results
# # Loading training history file print("[INFO] Loading Predictions from file...") import pickle p = [ args["output"], "{}_predictions_{}_lr:{}_epochs:{}_batch:{}_{}.pickle".format( args["network"], args["optimizer"], args["lr"], args["epochs"], args["batch"], os.getpid()) ] f = open(os.path.sep.join(p), 'rb') predictions = pickle.load(f) f.close() report = classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=classNames) # display Classification Report print("Learning Ratio = {}\n".format(args["lr"])) print(report) print("") # 3. Confusion Matrix confmatrix = confusion_matrix(testY.argmax(axis=1), predictions.argmax(axis=1)) # display Confusion Matrix print(confmatrix) # 4. Saving the classification report and confussion matrix to file p = [ args["output"],
def main(): args = option() # construct the image generator for data augmentation 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") # grab the list of images print("[INFO] loading images...") imagePaths = list(paths.list_images(args['dataset'])) classNames = [pt.split(os.path.sep)[-2] for pt in imagePaths] classNames = [str(x) for x in np.unique(classNames)] # initialize the image preprocessors sp = SimplePreprocessor(224, 224) iap = ImageToArrayPreprocessor() sdl = SimpleDatasetLoader(preprocessors=[sp, iap]) (data, labels) = sdl.load(imagePaths, verbose=500) data = data.astype("float") / 255.0 # partition the data into training:75% and testing:25% (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) # load the VGG16 network, ensuring the head FC layer # sets are left off baseModel = VGG16(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3))) # initialize the new head of the network, a set of FC layers # followed by a softmax classifier headModel = FCHeadNet.build(baseModel, len(classNames), 256) # place the head FC model on top of the base model, # become the actual model model = Model(inputs=baseModel.input, outputs=headModel) # loop over all layers in the base model and freeze them so they # will not be updated during the training process for layer in baseModel.layers: layer.trainable = False # compile our model (this needs to be done after our setting our # layers to being non-trainable) print("[INFO] compiling model...") opt = RMSprop(lr=0.001) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the head of the network for a few epochs (all other # layers are frozen) print("[INFO] training head...") #model.fit_generator(aug.flow(trainX, trainY, batch_size=32), # validation_data=(testX, testY), epochs=20, # steps_per_epoch=len(trainX) // 32, verbose=1) H = model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=32, epochs=20, verbose=1) # evaluate the network after initialization print("[INFO] evaluating after initialization...") predictions = model.predict(testX, batch_size=32) print( classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=classNames)) # unfreeze the final set of CONV layers and make them trainable for layer in baseModel.layers[15:]: layer.trainable = True #recompile the model print("[INFO] re-compiling model...") opt = SGD(lr=0.001) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the model again print("[INFO] fine-tuning model...") #model.fit_generator(aug.flow(trainX, trainY, batch_size=32), # validation_data=(testX, testY), epochs=30, # steps_per_epoch=len(trainX) // 32, verbose=1) H = model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=32, epochs=32, verbose=1) # save the network to disk print("[INFO] serializing network ...") model.save(args["model"]) # evaluate the network print("[INFO] evaluating after fine-tuning...") predictions = model.predict(testX, batch_size=32) print( classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=classNames))
save_best_only=True, mode='max') callbacks_list = [checkpoint] # train the model again, this time fine-tuning *both* the final set # of CONV layers along with our set of FC layers print("[INFO] fine-tuning model...") history = model.fit(trainX, trainY, validation_data=(valX, valY), epochs=150, batch_size=batch_size, verbose=1, callbacks=callbacks_list) model.save(args["model"]) with open('history_training.txt', 'w') as outfile: json.dump(history.history, outfile) outfile.close() # evaluate the network on the fine-tuned model print("[INFO] evaluating after fine-tuning...") predictions = model.predict(valX, batch_size=batch_size) print( classification_report(valY.argmax(axis=1), predictions.argmax(axis=1), target_names=classNames)) # save the model to disk print("[INFO] serializing model...")
def main(): """Fine tune VGG16 """ # construct the argument parse and parse the arguments args = argparse.ArgumentParser() args.add_argument("-d", "--dataset", required=True, help="path to input dataset") args.add_argument("-m", "--model", required=True, help="path to output model") args = vars(args.parse_args()) # construct the image generator for data augmentation augmentation = 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", ) # 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(224, 224) 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().transform(test_y) # load the VGG16 network, ensuring the head FC layer sets are left off base_model = VGG16(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3))) # initialize the new head of the network, a set of FC layers followed by a softmax classifier head_model = FCHeadNet.build(base_model, len(class_names), 256) # place the head FC model on top of the base model -- this will # become the actual model we will train model = Model(inputs=base_model.input, outputs=head_model) # loop over all layers in the base model and freeze them so they # will *not* be updated during the training process for layer in base_model.layers: layer.trainable = False # compile our model (this needs to be done after our setting our layers to being non-trainable print("[INFO] compiling model...") opt = RMSprop(lr=0.001) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the head of the network for a few epochs (all other layers are frozen) -- this will # allow the new FC layers to start to become initialized with actual "learned" values # versus pure random print("[INFO] training head...") model.fit_generator( augmentation.flow(train_x, train_y, batch_size=32), validation_data=(test_x, test_y), epochs=25, steps_per_epoch=len(train_x) // 32, verbose=1, ) # evaluate the network after initialization print("[INFO] evaluating after initialization...") predictions = model.predict(test_x, batch_size=32) print( classification_report(test_y.argmax(axis=1), predictions.argmax(axis=1), target_names=class_names)) # now that the head FC layers have been trained/initialized, lets # unfreeze the final set of CONV layers and make them trainable for layer in base_model.layers[15:]: layer.trainable = True # for the changes to the model to take affect we need to recompile # the model, this time using SGD with a *very* small learning rate print("[INFO] re-compiling model...") opt = SGD(lr=0.001) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the model again, this time fine-tuning *both* the final set # of CONV layers along with our set of FC layers print("[INFO] fine-tuning model...") model.fit_generator( augmentation.flow(train_x, train_y, batch_size=32), validation_data=(test_x, test_y), epochs=100, steps_per_epoch=len(train_x) // 32, verbose=1, ) # evaluate the network on the fine-tuned model print("[INFO] evaluating after fine-tuning...") predictions = model.predict(test_x, batch_size=32) print( classification_report(test_y.argmax(axis=1), predictions.argmax(axis=1), target_names=class_names)) # save the model to disk print("[INFO] serializing model...") model.save(args["model"])
optimizer=opt, metrics=["accuracy"]) print("[INFO] training network...") H = model.fit(train_x, train_y, validation_data=(test_x, test_y), batch_size=32, epochs=100, verbose=1) 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=["diamonds", "hearts", "spades", "three_sisters"])) plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, 100), H.history["loss"], label="train_loss") plt.plot(np.arange(0, 100), H.history["val_loss"], label="val_loss") plt.plot(np.arange(0, 100), H.history["acc"], label="train_acc") plt.plot(np.arange(0, 100), H.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy") plt.xlabel("Epock #") plt.ylabel("Loss/Accuracy") plt.legend() print("[INFO] saving network....")
train_y = LabelBinarizer().fit_transform(train_y) test_y = LabelBinarizer().fit_transform(test_y) print ("[INFO] compiling model...") opt = SGD(lr=0.005) model = LeNet.build(width=32, height=32, depth=3, classes=4) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) print ("[INFO] training network...") H = model.fit(train_x, train_y, validation_data=(test_x, test_y), batch_size=32, epochs=100, verbose=1) 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=["diamonds", "hearts", "spades", "three_sisters"])) plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, 100), H.history["loss"], label="train_loss") plt.plot(np.arange(0, 100), H.history["val_loss"], label="val_loss") plt.plot(np.arange(0, 100), H.history["acc"], label="train_acc") plt.plot(np.arange(0, 100), H.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy") plt.xlabel("Epock #") plt.ylabel("Loss/Accuracy") plt.legend() print ("[INFO] saving network....")
"q", "r", "s", "t", "u", "v", "w", "x", "y" ] labels = test['label'].values test.drop('label', axis=1, inplace=True) images = test.values labels = LabelBinarizer().fit_transform(labels) images = images.astype("float") / 255.0 images = images.reshape(images.shape[0], 28, 28, 1) model = load_model("./VGG_weights.hdf5") preds = model.predict(images, batch_size=32).argmax(axis=1) report = classification_report(labels.argmax(axis=1), preds, target_names=target_names) print(report) for i, image in enumerate(images): if i is 40: break prediction = preds[i] label = labels[i] #print(label.argmax(axis = 0)) src = image.reshape(28, 28) img = cv2.resize(src, (400, 400), interpolation=cv2.INTER_CUBIC) cv2.putText( img, "{}/{}".format(target_names[prediction], target_names[label.argmax()]), (10, 30),
print("[INFO] training network...") H = model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=size, epochs=ep, verbose=1) print("Saving network") model.save(f'./SavedModel/{args["model"]}') print("Network have been saved") print("[INFO] evaluating network...") predictions = model.predict(testX, batch_size=size) print( classification_report( testY.argmax(axis=1), predictions.argmax(axis=1), target_names=["EOSINOPHIL", "LYMPHOCYTE", "MONOCYTE", "NEUTROPHIL"])) plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, ep), H.history["loss"], label="train_loss") plt.plot(np.arange(0, ep), H.history["val_loss"], label="val_loss") plt.plot(np.arange(0, ep), H.history["accuracy"], label="accuracy") plt.plot(np.arange(0, ep), H.history["val_accuracy"], label="val_acc") plt.title("AMINJAMAL") plt.xlabel("Epoch #") plt.ylabel("Loss/ACC") plt.legend() plt.show()
def shallownet_animals(dataset): # grab the list of images that we'll describing print("[INFO] loading images...") imagePaths = list(paths.list_images(dataset)) # initialize the image preprocessor sp = SimplePreprocessor(32, 32) iap = ImageToArrayPreprocessor() # load the dataset from disk then scale the raw pixel intensities sdl = SimpleDatasetLoader(preprocessors=[sp, iap]) (data, labels) = sdl.load(imagePaths, verbose=500) #labels = labels.reshape(1, -1) data = data.astype("float") / 255.0 # pritition the into training and testing splits using 75% of # the data for training and the remaining 25% fir testing ''' cat和dog的图片总数=512是, labels.shape=(512,) data.shape=(512, 32, 32, 3) trainX.shape=<class 'tuple'>: (384, 32, 32, 3) trainY.shape=<class 'tuple'>: (384, 1) testY.shape=<class 'tuple'>: (128, 1) ''' (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 = ShallowNet.build2(width=32, height=32, depth=3, classes=2) #model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) model.compile(loss="binary_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) # evaluate the network print('[INFO]: Evaluating the network....') predictions = model.predict(testX, batch_size=32) #print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=['cat', 'dog', 'panda'])) print( classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=['cat', 'dog'])) # Plot the training loss and accuracy plt.style.use('ggplot') plt.figure() plt.plot(np.arange(0, 100), H.history['loss'], label='train_loss') plt.plot(np.arange(0, 100), H.history['val_loss'], label='val_loss') plt.plot(np.arange(0, 100), H.history['acc'], label='train_acc') plt.plot(np.arange(0, 100), H.history['val_acc'], label='val_acc') plt.title('Training Loss and Accuracy') plt.xlabel('Epoch #') plt.ylabel('Loss/Accuracy') plt.legend() plt.show()
(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.005) 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(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=classNames)) # Commented out IPython magic to ensure Python compatibility. plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, 100), H.history["loss"], label="train_loss") plt.plot(np.arange(0, 100), H.history["val_loss"], label="val_loss") plt.plot(np.arange(0, 100), H.history["acc"], label="train_acc") plt.plot(np.arange(0, 100), H.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy") plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy") plt.legend() # %cd /content/drive/My\ Drive/Colab_Work plt.savefig('minivggnet_flowers17_baseline_train_test_plot.png')
'incoming_weight_list': [], 'bias': None, 'loss': 'cross_entropy', 'act_func_name': 'softmax', 'value': None, 'layer_type': 'output', 'back_error': 0, 'link2input': None, 'link2target': y_train } } network = NeuralNetwork(n_layers=2, layer_dict = Networklayer_dict) network.fit(batch_size = 1000, learning_rate = step_iterator(0.1,0.01,-0.02), weight_decay = step_iterator(0,0,0), momentum = step_iterator(0.1,0.9,0.1), n_iter = 100, switch_point = 10) y_pred = network.transform(rbm2.transform(rbm1.transform(rbm0.transform(X_test))))[0] correct = np.sum(y_pred.argmax(axis=1) == y_test.argmax(axis=1)) print('correct = %d in %d'%(correct,X_test.shape[0])) network.transform(rbm2.transform(rbm1.transform(rbm0.transform(X_train_copy))))[0] error = network.empirical_error(target = y_train) print('initial error: %f'%error) with open(r"C:\Users\daredavil\Documents\Python Scripts\RBMver2\rbms.pkl",'wb') as file_: pickle.dump((rbm0.hidden_layer.dimension, rbm0.weight_list[0], rbm0.hidden_layer.bias, rbm1.hidden_layer.dimension, rbm1.weight_list[0], rbm1.hidden_layer.bias, rbm2.hidden_layer.dimension, rbm2.weight_list[0], rbm2.hidden_layer.bias, network.output_layer_list[0].incoming_weight_list[0], network.output_layer_list[0].bias), file_) Networklayer_dict = { 0: { 'n_neuron': X.shape[1], 'incoming_layer_list': [],
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) # evaluate the network print("[INFO] evaluating network...") predictions = model.predict(testX, batch_size=32) print( classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=["cat", "dog", "panda"])) # plot the training loss and accuracy plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, 100), H.history["loss"], label="train_loss") plt.plot(np.arange(0, 100), H.history["val_loss"], label="val_loss") plt.plot(np.arange(0, 100), H.history["acc"], label="train_acc") plt.plot(np.arange(0, 100), H.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy") plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy") plt.legend() plt.show()
class nn_mnist: #Create init function loading data and creating self.args with arguments from argparse def __init__(self, args): self.args = args self.data = pd.read_csv(self.args["mnist"]) #Get data from dataframe format into np.array and perform min/max normalization def data_wrangle(self): self.y = np.array(self.data.y) self.data = self.data.drop("y", axis=1) self.X = np.array(self.data) self.X = (self.X - self.X.min()) / (self.X.max() - self.X.min()) #Make train and test split with optional test_split argument from self.args #Perform the labelBinarizer def split(self): self.X_train, self.X_test, self.y_train, self.y_test = train_test_split( self.X, self.y, random_state=9, test_size=self.args["test_split"]) self.y_train = LabelBinarizer().fit_transform(self.y_train) self.y_test = LabelBinarizer().fit_transform(self.y_test) #Define the neural network with specified layers from self.args #Train the model with specified amount of epochs #Save the model if save_model_path is provided def nn_run(self): if sum(self.args["layers"]) < int(self.X_train.shape[1]) + 10: layers = [self.X_train.shape[1]] + self.args["layers"] + [10] else: print( f"Number of hidden layers should be below {self.X_train.shape[1]+10}. Using default hidden layers: [32,16]" ) layers = [self.X_train.shape[1], 32, 16, 10] print("[INFO] training network...") self.nn = NeuralNetwork([self.X_train.shape[1], 32, 16, 10]) print("[INFO] {}".format(self.nn)) self.nn.fit(self.X_train, self.y_train, epochs=self.args["epochs"]) if self.args["save_model_path"] != "": out_path = os.path.join(self.args["save_model_path"], "nn_model.pkl") joblib.dump(self.nn, out_path) #Print results to terminal #Save results to .csv file at desire output def results(self): predictions = self.nn.predict(self.X_test) predictions = predictions.argmax(axis=1) print(classification_report(self.y_test.argmax(axis=1), predictions)) results_df = pd.DataFrame( classification_report(self.y_test.argmax(axis=1), predictions, output_dict=True)).transpose() output_path = os.path.join(self.args["output"], "results_df_nn.csv") results_df.to_csv(output_path) #Load test_image if provided #Wrangle the data into the right format #Predict value using the neural network and print result def pred_new_number(self): test_image = cv2.imread(self.args["test_image"]) gray = cv2.bitwise_not(cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY)) compressed = cv2.resize(gray, (28, 28), interpolation=cv2.INTER_AREA) flatten = [item for sublist in compressed for item in sublist] flatten_scaled = (np.array(flatten) - np.array(flatten).min()) / ( np.array(flatten).max() - np.array(flatten).min()) flatten_reshaped = flatten_scaled.reshape(1, -1) prediction = self.nn.predict(flatten_reshaped).argmax(axis=1) print(f"The test image is predicted to show a {str(prediction)}") #Run all the functions def run(self): self.data_wrangle() self.split() self.nn_run() self.results() if self.args["test_image"] != "": self.pred_new_number()