def main(): # load & preprocess data (X_train, y_train), (X_test, y_test) = get_data(balance=True, sampling_strategy=0.90, debug=True) print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) if DO_TRAINING: # build model model = Net(X_train.shape[1]) criterion = nn.BCELoss() #optimizer = optim.Adam(model.parameters(), lr=LR) optimizer = optim.SGD(model.parameters(), lr=LR) model.compile(loss=criterion, optimizer=optimizer, metrics=['accuracy']) print(model) scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=200, gamma=0.2) hist = model.fit(X_train, y_train, validation_split=0.2, epochs=NUM_EPOCHS, batch_size=2048, lr_scheduler=scheduler, report_interval=50, verbose=1) pytk.show_plots(hist, metric='accuracy') model.save(MODEL_SAVE_PATH) del model if not os.path.exists(MODEL_SAVE_PATH): raise ValueError( f"Could not find saved model at {MODEL_SAVE_PATH}. Did you train model?" ) model = pytk.load_model(MODEL_SAVE_PATH) print(model) if DO_EVALUATION: # evaluate performance print('Evaluating performance...') loss, acc = model.evaluate(X_train, y_train, batch_size=2048) print(f' - Train dataset -> loss: {loss:.3f} acc: {acc:.3f}') loss, acc = model.evaluate(X_test, y_test) print(f' - Test dataset -> loss: {loss:.3f} acc: {acc:.3f}') if DO_PREDICTION: # run predictions y_pred = (model.predict(X_test) >= 0.5).astype('int32').ravel() y_test = y_test.astype('int32') print(classification_report(y_test, y_pred)) pytk.plot_confusion_matrix(confusion_matrix(y_test, y_pred), ["No Rain", "Rain"], title="Rain Prediction for Tomorrow")
def main(): (X_train, y_train), (X_val, y_val), (X_test, y_test) = load_data(upsample=True) train_dataset = PimaDataset(X_train, y_train) val_dataset = PimaDataset(X_val, y_val) test_dataset = PimaDataset(X_test, y_test) if DO_TRAINING: print('Building model...') model = PimaModel() # define the loss function & optimizer that model should loss_fn = nn.BCELoss() # nn.CrossEntropyLoss() # optimizer = torch.optim.SGD( # model.parameters(), lr=LEARNING_RATE, weight_decay=DECAY) optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE, weight_decay=DECAY) model.compile(loss=loss_fn, optimizer=optimizer, metrics=['acc']) print(model) # train model print('Training model...') # split training data into train/cross-val datasets in 80:20 ratio hist = model.fit_dataset(train_dataset, validation_dataset=val_dataset, epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, report_interval=100) pytk.show_plots(hist, metric='acc', plot_title="Performance Metrics") # evaluate model performance on train/eval & test datasets print('\nEvaluating model performance...') loss, acc = model.evaluate_dataset(train_dataset) print(f" Training dataset -> loss: {loss:.4f} - acc: {acc:.4f}") loss, acc = model.evaluate_dataset(val_dataset) print(f" Cross-val dataset -> loss: {loss:.4f} - acc: {acc:.4f}") loss, acc = model.evaluate_dataset(test_dataset) print(f" Testing dataset -> loss: {loss:.4f} - acc: {acc:.4f}") # save model state model.save(MODEL_SAVE_PATH) del model if DO_PREDICTION: print('\nRunning predictions...') model = pytk.load_model(MODEL_SAVE_PATH) _, y_pred = model.predict_dataset(X_test) # display output print('Sample labels: ', y_test.flatten()) print('Sample predictions: ', y_pred) print('We got %d/%d correct!' % ((y_test.flatten() == y_pred).sum(), len(y_test.flatten())))
def main(): (X_train, y_train), (X_test, y_test) = load_data() print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) y_train, y_test = y_train.astype(np.float), y_test.astype(np.float) if DO_TRAINING: print('Building model...') model = WBCNet(NUM_FEATURES, 30, 30, NUM_CLASSES) # define the loss function & optimizer that model should loss_fn = nn.BCELoss() #nn.CrossEntropyLoss() optimizer = torch.optim.SGD(model.parameters(), lr=LEARNING_RATE, nesterov=True, weight_decay=0.005, momentum=0.9, dampening=0) model.compile(loss=loss_fn, optimizer=optimizer, metrics=['acc', 'f1']) print(model) # train model print('Training model...') # split training data into train/cross-val datasets in 80:20 ratio hist = model.fit(X_train, y_train, validation_split=0.20, epochs=NUM_EPOCHS, batch_size=BATCH_SIZE) pytk.show_plots(hist, metric='f1', plot_title="Performance Metrics") # evaluate model performance on train/eval & test datasets print('\nEvaluating model performance...') loss, acc, f1 = model.evaluate(X_train, y_train) print(' Training dataset -> loss: %.4f - acc: %.4f - f1: %.4f' % (loss, acc, f1)) loss, acc, f1 = model.evaluate(X_test, y_test) print(' Test dataset -> loss: %.4f - acc: %.4f - f1: %.4f' % (loss, acc, f1)) # save model state model.save(MODEL_SAVE_NAME) del model if DO_PREDICTION: print('\nRunning predictions...') model = pytk.load_model(MODEL_SAVE_NAME) y_pred = np.round(model.predict(X_test)).reshape(-1) # display output print('Sample labels: ', y_test) print('Sample predictions: ', y_pred) print('We got %d/%d correct!' % ((y_test == y_pred).sum(), len(y_test)))
def main(): # generate data with noise M, C = 1.8, 32.0 X, y = get_synthesized_data(M, C, numelems=500, std=25) print('Displaying data sample...') print(f"X.shape = {X.shape}, y.shape = {y.shape}") df = pd.DataFrame(X, columns=['Calsius']) df['Farenheit'] = y print(df.head(10)) # display plot of generated data plt.figure(figsize=(8, 6)) plt.scatter(X, y, s=40, c='steelblue') plt.title(f'Original Data -> $y = {M:.3f} * X + {C:.3f}$') plt.show() # build our network net = Net() print('Before training: ') print(' Weight: %.3f bias: %.3f' % (net.fc1.weight, net.fc1.bias)) criterion = nn.MSELoss() # optimizer = optim.SGD(net.parameters(), lr=0.001) optimizer = optim.Adam(net.parameters(), lr=0.001) net.compile(loss=criterion, optimizer=optimizer, metrics=['mse', 'rmse', 'mae', 'r2_score']) print(net) # train on the data hist = net.fit(X, y, epochs=5000, batch_size=32, report_interval=100) pytk.show_plots(hist, metric='r2_score', plot_title="Performance Metrics (R2 Score)") # print the results print('After training: ') W, b = net.fc1.weight.item(), net.fc1.bias.item() print(f' Weight: {W:.3f} bias: {b:.3f}') # get predictions (need to pass Tensors!) y_pred = net.predict(X) # what is my r2_score? print('R2 score (sklearn): %.3f' % r2_score(y, y_pred)) print('R2 score (pytk): %.3f' % pytk.r2_score(torch.Tensor(y_pred), torch.Tensor(y))) # display plot plt.figure(figsize=(8, 6)) plt.scatter(X, y, s=40, c='steelblue') plt.plot(X, y_pred, lw=2, color='firebrick') plt.title('Predicted Line -> $y = %.3f * X + %.3f$' % (W, b)) plt.show()
def main(): (X_train, y_train), (X_val, y_val), (X_test, y_test) = load_data() print(X_train.shape, y_train.shape, X_val.shape, y_val.shape, X_test.shape, y_test.shape) if DO_TRAINING: print('Building model...') model = build_model() print(model) # train model print('Training model...') hist = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=NUM_EPOCHS, batch_size=BATCH_SIZE) pytk.show_plots(hist, metric='acc', plot_title='Training metrics') # evaluate model performance on train/eval & test datasets print('\nEvaluating model performance...') loss, acc = model.evaluate(X_train, y_train) print(' Training dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) loss, acc = model.evaluate(X_val, y_val) print(' Cross-val dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) loss, acc = model.evaluate(X_test, y_test) print(' Test dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) # save model state model.save(MODEL_SAVE_PATH) del model if DO_PREDICTION: print('\nRunning predictions...') # load model state from .pt file model = build_model() model.load(MODEL_SAVE_PATH) print(model) print('\nEvaluating model performance...') loss, acc = model.evaluate(X_train, y_train) print(' Training dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) loss, acc = model.evaluate(X_val, y_val) print(' Cross-val dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) loss, acc = model.evaluate(X_test, y_test) print(' Test dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) y_preds = np.argmax(model.predict(X_test), axis=1) # display all predictions print(f'Sample labels: {y_test}') print(f'Sample predictions: {y_preds}') print(f'We got {(y_preds == y_test).sum()}/{len(y_test)} correct!!')
def main(): (X_train, y_train), (X_val, y_val), (X_test, y_test) = load_data() print("X_train.shape = {}, y_train.shape = {}, X_val.shape = {}, y_val.shape = {}, X_test.shape = {}, y_test.shape = {}".format( X_train.shape, y_train.shape, X_val.shape, y_val.shape, X_test.shape, y_test.shape)) if DO_TRAINING: print('Building model...') model = IrisNet(4, 16, 16, 4) # define the loss function & optimizer that model should loss_fn = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=0.001, weight_decay=0.005) model.compile(loss=loss_fn, optimizer=optimizer, metrics=['acc']) print(model) # train model - here is the magic, notice the Keras-like fit(...) call print('Training model...') hist = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=250, batch_size=BATCH_SIZE, verbose=0) # display plots of loss & accuracy against epochs pytk.show_plots(hist, metric='acc', plot_title='Training metrics') # evaluate model performance on train/eval & test datasets # Again, notice the Keras-like API to evaluate model performance print('\nEvaluating model performance...') loss, acc = model.evaluate(X_train, y_train) print(f' Training dataset -> loss: {loss:.4f} - acc: {acc:.4f}') loss, acc = model.evaluate(X_val, y_val) print(f' Cross-val dataset -> loss: {loss:.4f} - acc: {acc:.4f}') loss, acc = model.evaluate(X_test, y_test) print(f' Test dataset -> loss: {loss:.4f} - acc: {acc:.4f}') # save model state model.save(MODEL_SAVE_NAME) del model if DO_PREDICTION: print('\nRunning predictions...') # load model state from .pt file #model = pytk.load_model(MODEL_SAVE_NAME) model = IrisNet(4, 16, 16, 4) model.load(MODEL_SAVE_NAME) y_pred = np.argmax(model.predict(X_test), axis=1) # we have just 5 elements in dataset, showing ALL print(f'Sample labels: {y_test}') print(f'Sample predictions: {y_pred}') print(f'We got {(y_test == y_pred).sum()}/{len(y_test)} correct!!')
def main(): # generate data with noise X, y = get_data() print(f"X.shape: {X.shape} - y.shape: {y.shape}") # build our network net = Net() print('Before training: ') print(' Weight: %.3f bias: %.3f' % (net.fc1.weight.item(), net.fc1.bias.item())) criterion = nn.MSELoss() optimizer = optim.SGD(net.parameters(), lr=0.001) net.compile(loss=criterion, optimizer=optimizer, metrics=['mse', 'rmse', 'mae', 'r2_score']) print(net) # train on the data hist = net.fit(X, y, epochs=5000, report_interval=100) pytk.show_plots(hist, metric='r2_score', plot_title="Performance Metrics") # print the results print('After training: ') W, b = net.fc1.weight.item(), net.fc1.bias.item() print(f"After training -> Weight: {W:.3f} - bias: {b:.3f}") # get predictions (need to pass Tensors!) y_pred = net.predict(X) # what is my r2_score? print('R2 score (sklearn): %.3f' % r2_score(y, y_pred)) print('R2 score (pytk): %.3f' % pytk.r2_score(torch.Tensor(y_pred), torch.Tensor(y))) # display plot plt.figure(figsize=(8, 6)) plt.scatter(X, y, s=40, c='steelblue') plt.plot(X, y_pred, lw=2, color='firebrick') plt.title('Predicted Line -> $y = %.3f * X + %.3f$' % (W, b)) plt.show()
def main(): # read Salary data csv & return X & y # NOTE: we have just 1 variable (YearsOfExperience) # X, y = get_data() # print(X.shape, y.shape) # X2, y2 = X.reshape(-1, 1), y.reshape(-1,1) (X_train, y_train), (X_test, y_test) = get_data() print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) # display plot plt.figure(figsize=(8, 6)) plt.scatter(X_train, y_train, s=40, c='steelblue') plt.title('Original Data') plt.show() net = Net(1, 1) criterion = nn.MSELoss() optimizer = optim.SGD(net.parameters(), lr=LR, weight_decay=0.10) #scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=NUM_EPOCHS//5, gamma=0.1) net.compile(loss=criterion, optimizer=optimizer) print(net) hist = net.fit(X_train, y_train, epochs=NUM_EPOCHS, batch_size=BATCH_SIZE) #, lr_scheduler=scheduler) pytk.show_plots(hist) # run predictions y_pred = net.predict(X_test) # what is my r2_score? r2 = r2_score(y_test, y_pred) print('R2 score: %.3f' % r2) # got 0.974 # display plot plt.figure(figsize=(8, 6)) X = np.vstack([X_train, X_test]) y = np.vstack([y_train, y_test]) plt.scatter(X, y, s=40, c='steelblue') plt.plot(X, net.predict(X), lw=2, color='firebrick') title = 'Regression Plot: R2 Score = %.3f' % r2 plt.title(title) plt.show() if RUN_SKLEARN: # what does scikit-learn give me from sklearn.linear_model import LinearRegression lr = LinearRegression() lr.fit(X_train, y_train) y_pred_skl = lr.predict(X_test) print('sklearn Logistic Regression: r2_score = %.3f' % r2_score(y_test, y_pred_skl)) print('Pytorch Model: r2_score = %.3f' % r2_score(y_test, y_pred)) if RUN_KERAS: # what does an equivalent Keras model give me? import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Input from tensorflow.keras.optimizers import SGD from tensorflow.keras.regularizers import l2 reg = l2(0.01) kr_model = Sequential([ Input(shape=(1, )), Dense(1, activation='linear', kernel_regularizer=reg) ]) opt = SGD(learning_rate=LR) kr_model.compile(loss='mse', optimizer=opt) hist = kr_model.fit(X_train, y_train, epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, verbose=0) y_pred_k = kr_model.predict(X_test) print('Keras model: r2_score = %.3f' % r2_score(y_test, y_pred_k))
def main(): # read Salary data csv & return X & y # NOTE: we have just 1 variable (YearsOfExperience) # X, y = get_data() # print(X.shape, y.shape) # X2, y2 = X.reshape(-1, 1), y.reshape(-1,1) (X_train, y_train), (X_test, y_test) = get_data() print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) if RUN_TORCH: net = Net(X_train.shape[1], 1) criterion = nn.MSELoss() optimizer = torch.optim.SGD(net.parameters(), lr=LR) # , weight_decay=0.10) scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=NUM_EPOCHS // 10, gamma=0.1) net.compile(loss=criterion, optimizer=optimizer, metrics=['mae']) print(net) print('Training model with Pytorch...please wait') hist = net.fit(X_train, y_train, validation_split=0.05, epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, lr_scheduler=scheduler, verbose=2) pytk.show_plots(hist, metric='mae', plot_title='Pytorch Model performance') # run predictions y_pred = net.predict(X_test) # what is my r2_score? r2 = r2_score(y_test, y_pred) print('Pytorch R2 score: %.3f' % r2) # got 0.974 # display plot # plt.figure(figsize=(8, 6)) # X = np.vstack([X_train, X_test]) # y = np.vstack([y_train, y_test]) # plt.scatter(X, y, s=40, c='steelblue') # plt.plot(X, net.predict(X), lw=2, color='firebrick') # title = 'Regression Plot: R2 Score = %.3f' % r2 # plt.title(title) # plt.show() if RUN_SKLEARN: # what does scikit-learn give me from sklearn.linear_model import LinearRegression lr = LinearRegression() lr.fit(X_train, y_train) y_pred_skl = lr.predict(X_test) print( f'sklearn Logistic Regression: r2_score = {r2_score(y_test, y_pred_skl)}' ) if RUN_KERAS: # what does an equivalent Keras model give me? import tensorflow as tf from tensorflow import keras print( f"Using Tensorflow {tf.__version__} and Keras {keras.__version__}") from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Input from tensorflow.keras.optimizers import SGD from tensorflow.keras.regularizers import l2 reg = l2(0.01) kr_model = Sequential([ Input(shape=(X_train.shape[1], )), Dense(1, activation='linear', kernel_regularizer=reg) ]) opt = SGD(learning_rate=LR) kr_model.compile(loss='mse', optimizer=opt, metrics=['mse']) print('Training model with Keras....please wait') hist = kr_model.fit(X_train, y_train, epochs=NUM_EPOCHS, validation_split=0.05, batch_size=BATCH_SIZE, verbose=0) pytk.show_plots(hist.history, metric='mae', plot_title='Keras Model performance') y_pred_k = kr_model.predict(X_test) print('Keras model: r2_score = %.3f' % r2_score(y_test, y_pred_k))
def main(): (X_train_image_paths, y_train), (X_val_image_paths, y_val), \ (X_test_image_paths, y_test) = get_data() # define our datasets train_dataset = HistoDataset(X_train_image_paths, y_train, xforms['train']) eval_dataset = HistoDataset(X_val_image_paths, y_val, xforms['eval']) test_dataset = HistoDataset(X_test_image_paths, y_test, xforms['test']) print(len(train_dataset), len(X_train_image_paths), len(eval_dataset), len(X_val_image_paths), \ len(test_dataset), len(X_test_image_paths)) if SHOW_SAMPLE: # display a sample of 64 images/labels from the test dataset loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=True) data_iter = iter(loader) sample_images, sample_labels = data_iter.next( ) # fetch first batch of 64 images & labels print( f'Dataset: image.shape = {sample_images.shape}, labels.shape = {sample_labels.shape}' ) display_sample(sample_images.cpu().numpy(), sample_labels.cpu().numpy(), plot_title="Sample Test Images") # define our model # model = pytk.PytkModuleWrapper(cnn_model) # criterion = nn.CrossEntropyLoss() # optimizer = optim.Adam(params=model.parameters(), lr=LR_RATE, weight_decay=L2_REG) # model.compile(loss=criterion, optimizer=optimizer, metrics=['acc']) model = build_model() print(model.summary((NUM_CHANNELS, IMAGE_HEIGHT, IMAGE_WIDTH))) # train the model from torch.optim.lr_scheduler import ReduceLROnPlateau, StepLR #lr_scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=10, verbose=1) lr_scheduler = StepLR(optimizer, step_size=20, gamma=0.5, verbose=1) hist = model.fit_dataset(train_dataset, validation_dataset=eval_dataset, epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, num_workers=3, verbose=1) pytk.show_plots(hist, metric='acc') # evaluate performance print('Evaluating model performance after training...') loss, acc = model.evaluate_dataset(train_dataset) print(' Training data -> loss: %.3f, acc: %.3f' % (loss, acc)) loss, acc = model.evaluate_dataset(eval_dataset) print(' Cross-val data -> loss: %.3f, acc: %.3f' % (loss, acc)) loss, acc = model.evaluate_dataset(test_dataset) print(' Testing data -> loss: %.3f, acc: %.3f' % (loss, acc)) model.save(MODEL_SAVE_PATH) del model # load model from saved state # model = pytk.PytkModuleWrapper(pytk.load_model(MODEL_SAVE_PATH)) # criterion = nn.CrossEntropyLoss() # optimizer = optim.Adam(params=model.parameters(), lr=LR_RATE, weight_decay=L2_REG) # model.compile(loss=criterion, optimizer=optimizer, metrics=['acc']) model = build_model() model.load(MODEL_SAVE_PATH) print(model.summary((NUM_CHANNELS, IMAGE_HEIGHT, IMAGE_WIDTH))) # run predictions # display sample from test dataset print('Running predictions....') test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=True) actuals, predictions = [], [] for batch_no, (images, labels) in enumerate(test_loader): # images, labels = data_iter.next() # fetch first batch of 64 images & labels preds = model.predict(images) actuals.extend(labels.cpu().numpy().ravel()) predictions.extend(np.argmax(preds, axis=1).ravel()) actuals = np.array(actuals) predictions = np.array(predictions) print('Sample actual values & predictions...') print(' - Acutal values: ', actuals[:25]) print(' - Predictions : ', predictions[:25]) correct_preds = (actuals == predictions).sum() acc = correct_preds / len(actuals) print(' We got %d of %d correct (%.3f accuracy)' % (correct_preds, len(actuals), acc)) from sklearn.metrics import confusion_matrix, classification_report print(classification_report(actuals, predictions)) # display predictions test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=True) data_iter = iter(test_loader) images, labels = data_iter.next() preds = model.predict(images) preds = np.argmax(preds, axis=1) print(images.shape, labels.shape, preds.shape) display_sample(images.cpu().numpy(), labels.cpu().numpy(), sample_predictions=preds, grid_shape=(8, 8), fig_size=(16, 20), plot_title='Sample Predictions') del model
def main(): print('Loading datasets...') train_dataset, val_dataset, test_dataset = load_data() if SHOW_SAMPLE: # display sample from test dataset print('Displaying sample from train dataset...') trainloader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=True) data_iter = iter(trainloader) images, labels = data_iter.next( ) # fetch first batch of 64 images & labels display_sample(images, labels, grid_shape=(8, 8), plot_title='Sample Images') if DO_TRAINING: print(f'Using {"CNN" if USE_CNN else "ANN"} model...') model = MNISTConvNet() if USE_CNN else MNISTNet() # define the loss function & optimizer that model should loss_fn = nn.CrossEntropyLoss() optimizer = optim.Adam(params=model.parameters(), lr=LEARNING_RATE, weight_decay=L2_REG) scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.2) model.compile(loss=loss_fn, optimizer=optimizer, metrics=['acc']) # display Keras like summary model.summary((NUM_CHANNELS, IMAGE_HEIGHT, IMAGE_WIDTH)) # train model print(f'Training {"CNN" if USE_CNN else "ANN"} model') hist = model.fit_dataset(train_dataset, validation_dataset=val_dataset, lr_scheduler=scheduler, epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, verbose=1) pytk.show_plots(hist, metric='acc', plot_title='Training metrics') # evaluate model performance on train/eval & test datasets print('Evaluating model performance...') loss, acc = model.evaluate_dataset(train_dataset) print(' Training dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) loss, acc = model.evaluate_dataset(val_dataset) print(' Cross-val dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) loss, acc = model.evaluate_dataset(test_dataset) print(' Test dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) # save model state model.save(MODEL_SAVE_PATH) del model if DO_PREDICTION: print('Running predictions...') # load model state from .pt file model = pytk.load_model(MODEL_SAVE_PATH) y_pred, y_true = model.predict_dataset(test_dataset) y_pred = np.argmax(y_pred, axis=1) print('Sample labels (50): ', y_true[:50]) print('Sample predictions: ', y_true[:50]) print('We got %d/%d incorrect!' % ((y_pred != y_true).sum(), len(y_true))) # display sample from test dataset print('Displaying sample predictions...') trainloader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=True) data_iter = iter(trainloader) images, labels = data_iter.next() # fetch a batch of 64 random images preds = np.argmax(model.predict(images), axis=1) display_sample(images, labels, sample_predictions=preds, grid_shape=(8, 8), plot_title='Sample Predictions')
def main(): print('Loading datasets...') train_dataset, val_dataset, test_dataset = load_data() if SHOW_SAMPLE: # display sample from test dataset print('Displaying sample from train dataset...') testloader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=True) data_iter = iter(testloader) images, labels = data_iter.next( ) # fetch first batch of 64 images & labels display_sample(images.cpu().numpy(), labels.cpu().numpy(), grid_shape=(8, 8), plot_title='Sample Images') if DO_TRAINING: model = build_model() # display Keras like summary model.summary((NUM_CHANNELS, IMAGE_HEIGHT, IMAGE_WIDTH)) # train model hist = model.fit_dataset(train_dataset, validation_dataset=val_dataset, epochs=NUM_EPOCHS, batch_size=BATCH_SIZE) pytk.show_plots(hist, metric='acc', plot_title='Training metrics') # evaluate model performance on train/eval & test datasets print('Evaluating model performance...') loss, acc = model.evaluate_dataset(train_dataset) print(' Training dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) loss, acc = model.evaluate_dataset(val_dataset) print(' Cross-val dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) loss, acc = model.evaluate_dataset(test_dataset) print(' Test dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) # save model state model.save(MODEL_SAVE_PATH) del model if DO_PREDICTION: # load model state from .pt file model = build_model() model.load(MODEL_SAVE_PATH) # model = pytk.load_model(MODEL_SAVE_PATH) model.summary((NUM_CHANNELS, IMAGE_HEIGHT, IMAGE_WIDTH)) # evaluate model performance on train/eval & test datasets print('Evaluating model performance...') loss, acc = model.evaluate_dataset(train_dataset) print(' Training dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) loss, acc = model.evaluate_dataset(val_dataset) print(' Cross-val dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) loss, acc = model.evaluate_dataset(test_dataset) print(' Test dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) y_pred, y_true = model.predict_dataset(test_dataset) y_pred = np.argmax(y_pred, axis=1) print('Sample labels (50): ', y_true[:50]) print('Sample predictions: ', y_true[:50]) print('We got %d/%d incorrect!' % ((y_pred != y_true).sum(), len(y_true))) # display sample from test dataset print('Displaying sample predictions...') test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=True) data_iter = iter(test_loader) images, labels = data_iter.next() # fetch a batch of 64 random images preds = np.argmax(model.predict(images), axis=1) display_sample(images.cpu().numpy(), labels.cpu().numpy(), sample_predictions=preds, grid_shape=(8, 8), plot_title='Sample Predictions')
def main(): (X_train, y_train), (X_test, y_test) = load_data() # NOTE: BCELoss() functions expects labels to be floats (why can't it handle integers??) y_train, y_test = y_train.astype(np.float), y_test.astype(np.float) print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) # sys.exit(-1) if DO_TRAINING: print('Building model...') model = build_model() print(model) # train model print('Training model...') # split training data into train/cross-val datasets in 80:20 ratio hist = model.fit(X_train, y_train, validation_split=0.20, epochs=NUM_EPOCHS, batch_size=BATCH_SIZE) pytk.show_plots(hist, metric='f1', plot_title="Performance Metrics (f1-score)") # save model state model.save(MODEL_SAVE_PATH) del model if DO_TESTING: # evaluate model performance on train/eval & test datasets print('\nEvaluating model performance...') model = build_model() model.load(MODEL_SAVE_PATH) print(model) loss, acc, f1, prec, rec = model.evaluate(X_train, y_train, metrics=METRICS_LIST) print(f' Training dataset -> loss: {loss:.4f} - acc: {acc:.4f} ' + f'- f1: {f1:.4f} - prec: {prec:.4f} - rec: {rec:.4f}') loss, acc, f1, prec, rec = model.evaluate(X_test, y_test, metrics=METRICS_LIST) print(f' Test dataset -> loss: {loss:.4f} - acc: {acc:.4f} ' + f'- f1: {f1:.4f} - prec: {prec:.4f} - rec: {rec:.4f}') del model if DO_PREDICTION: print('\nRunning predictions...') model = build_model() model.load(MODEL_SAVE_PATH) print(model) y_pred = np.round(model.predict(X_test)).reshape(-1) # display output print('Sample labels: ', y_test.flatten()) print('Sample predictions: ', y_pred) print('We got %d/%d correct!' % ((y_test.flatten() == y_pred).sum(), len(y_test.flatten()))) del model
def main(): (X_train, y_train), (X_val, y_val), (X_test, y_test) = load_data() print(X_train.shape, y_train.shape, X_val.shape, y_val.shape, X_test.shape, y_test.shape) if DO_TRAINING: print('Building model...') model = WineNet(13, 20, 3) # define the loss function & optimizer that model should criterion = nn.CrossEntropyLoss() optimizer = torch.optim.SGD(model.parameters(), lr=LEARNING_RATE, nesterov=True, momentum=0.9, dampening=0, weight_decay=L2_REG) model.compile(loss=criterion, optimizer=optimizer, metrics=['acc']) print(model) # train model print('Training model...') hist = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=NUM_EPOCHS, batch_size=BATCH_SIZE) pytk.show_plots(hist, metric='acc', plot_title='Training metrics') # evaluate model performance on train/eval & test datasets print('\nEvaluating model performance...') loss, acc = model.evaluate(X_train, y_train) print(' Training dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) loss, acc = model.evaluate(X_val, y_val) print(' Cross-val dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) oss, acc = model.evaluate(X_test, y_test) print(' Test dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) # save model state model.save(MODEL_SAVE_NAME) del model if DO_PREDICTION: print('\nRunning predictions...') # load model state from .pt file model = pytk.load_model(MODEL_SAVE_NAME) print(f'Loaded an instance of {type(model)}') print('\nEvaluating model performance...') loss, acc = model.evaluate(X_train, y_train) print(' Training dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) loss, acc = model.evaluate(X_val, y_val) print(' Cross-val dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) oss, acc = model.evaluate(X_test, y_test) print(' Test dataset -> loss: %.4f - acc: %.4f' % (loss, acc)) y_preds = np.argmax(model.predict(X_test), axis=1) # display all predictions print(f'Sample labels: {y_test}') print(f'Sample predictions: {y_preds}') print(f'We got {(y_preds == y_test).sum()}/{len(y_test)} correct!!')