def test_model(): dataloader = load_dataset(train=False) model = cnn.Net() model.load_state_dict(torch.load("./modelo/mnist.pt")) model.to(cnn.get_device()) model.eval() dictionary = cnn.test_model(model=model, dataloader=dataloader) classes = list(dataloader.dataset.class_to_idx.keys()) dictionary['classes'] = classes dictionary = {**load_stats(), **dictionary} save_stats(dictionary) show_stats(dictionary)
model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy', metrics=["accuracy"]) # callbacks for Save weights, Tensorboard # creating a new directory for each run using timestamp # folder = os.path.join(os.getcwd(), datetime.now().strftime("%d-%m-%Y_%H-%M-%S")) # history_file = folder + "/inception" + ".h5" # save_callback = ModelCheckpoint(filepath=history_file, verbose=1) # tb_callback = TensorBoard(log_dir=folder) # train model with data generator train_accuracy, train_loss, valid_accuracy, valid_loss = cnn.train_model_datagen( model, datagen, val_datagen, x_train, y_train, BATCH_SIZE, NUM_EPOCHS, x_valid, y_valid) # test model test_accuracy, test_loss, predictions = cnn.test_model(model, x_test, y_test) # Visualization and Output num_epochs_plot = range(1, len(train_accuracy) + 1) # x axis range # Loss curves plt.figure() plt.plot(num_epochs_plot, train_loss, "b", label="Training Loss") plt.plot(num_epochs_plot, valid_loss, "r", label="Validation Loss") plt.title("Loss Curves") plt.xlabel("Number of Epochs") plt.ylabel("Loss") plt.legend() plt.savefig('Figures/loss_inception.png') plt.close() #plt.show()
num_classes = 10 ################################################################################ # callbacks for Save weights, Tensorboard # creating a new directory for each run using timestamp folder = os.path.join(os.getcwd(), datetime.now().strftime("%d-%m-%Y_%H-%M-%S"), str(ACTIV_FN)) history_file = folder + "\cnn_" + str(ACTIV_FN) + ".h5" save_callback = ModelCheckpoint(filepath=history_file, verbose=1) tb_callback = TensorBoard(log_dir=folder) # Build, train, and test model model = cnn.build_model(input_shape, activation_fn, LEARNING_RATE, DROP_PROB, NUM_NEURONS_IN_DENSE_1, num_classes) train_accuracy, train_loss, valid_accuracy, valid_loss = cnn.train_model(model, train_images, train_labels, BATCH_SIZE, NUM_EPOCHS, valid_images, valid_labels, save_callback, tb_callback) test_accuracy, test_loss, predictions = cnn.test_model(model, test_images, test_labels) # save test set results to csv predictions = np.round(predictions) predictions = predictions.astype(int) df = pd.DataFrame(predictions) df.to_csv("mnist.csv", header=None, index=None) ################################################################################ # Visualization and Output num_epochs_plot = range(1, len(train_accuracy) + 1) # Loss curves plt.figure(1) plt.plot(num_epochs_plot, train_loss, "b", label="Training Loss") plt.plot(num_epochs_plot, valid_loss, "r", label="Validation Loss")