def eval(): device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") np.random.seed(seed=46) model = Net() path = 'model.pth.tar' checkpoint = torch.load(path, map_location=torch.device('cpu')) model.load_state_dict(checkpoint) model = model.to(device) model.eval() ious = [] for _ in tqdm(range(1000)): img, label = make_data() img = torch.from_numpy(np.asarray(img, dtype=np.float32)) img = torch.unsqueeze(img, 0) img = torch.unsqueeze(img, 0) img = img.to(device) pred = model.predict(img) ious.append(score_iou(label, pred)) ious = np.asarray(ious, dtype="float") ious = ious[~np.isnan(ious)] # remove true negatives print((ious > 0.7).mean())
def eval(): model = keras.models.load_model("model.hdf5") ious = [] for _ in tqdm(range(1000)): img, label = make_data() pred = model.predict(img[None]) pred = np.squeeze(pred) ious.append(score_iou(label, pred)) ious = np.asarray(ious, dtype="float") ious = ious[~np.isnan(ious)] # remove true negatives print((ious > 0.7).mean())
def evaluate(model, batch_size, num_samples): images, labels = zip( *[make_data() for _ in tqdm(range(num_samples), desc="creating data")]) images = [ preprocess_image(img) for img in tqdm(images, desc="preprocessing") ] images = np.stack(images) predictions, _, _ = model.predict_batch(images, batch_size) ious = [score_iou(label, pred) for pred, label in zip(predictions, labels)] ious = np.asarray(ious, dtype="float") ious = ious[~np.isnan(ious)] # remove true negatives score = (ious > 0.7).mean() print() print(f"score: {score:.5f}")
def make_batch(batch_size, task): # uses 50/50 split (i%2==0) if in classification mode, otherwise we'll use # always create a spaceship for the regression task imgs, labels = zip( *[ make_data(has_spaceship=i % 2 == 0 if task == "classification" else True) for i in range(batch_size) ] ) imgs = [preprocess_image(img) for img in imgs] if task == "classification": labels = [0 if np.any(np.isnan(label)) else 1 for label in labels] elif task == "regression": labels = [label / SCALE_VECTOR for label in labels] # normalize labels else: raise ValueError("task must be one of classification or regression") imgs = np.stack(imgs) labels = np.stack(labels) return imgs, labels
def test_make_data(): # your implementation of slam should work with the following inputs # feel free to change these input values and see how it responds! # world parameters num_landmarks = 5 # number of landmarks N = 20 # time steps world_size = 100.0 # size of world (square) # robot parameters measurement_range = 50.0 # range at which we can sense landmarks motion_noise = 2.0 # noise in robot motion measurement_noise = 2.0 # noise in the measurements distance = 20.0 # distance by which robot (intends to) move each iteratation # make_data instantiates a robot, AND generates random landmarks for a given world size and number of landmarks data = make_data(N, num_landmarks, world_size, measurement_range, motion_noise, measurement_noise, distance) time_step = 0 print('Example measurements: \n', data[time_step][0]) print('\n') print('Example motion: \n', data[time_step][1])
# your implementation of slam should work with the following inputs # feel free to change these input values and see how it responds! # world parameters num_landmarks = 5 # number of landmarks N = 20 # time steps world_size = 100.0 # size of world (square) # robot parameters measurement_range = 50.0 # range at which we can sense landmarks motion_noise = 2.0 # noise in robot motion measurement_noise = 2.0 # noise in the measurements distance = 20.0 # distance by which robot (intends to) move each iteratation # make_data instantiates a robot, AND generates random landmarks for a given world size and number of landmarks data = make_data(N, num_landmarks, world_size, measurement_range, motion_noise, measurement_noise, distance) # ### A note on `make_data` # # The function above, `make_data`, takes in so many world and robot motion/sensor parameters because it is responsible for: # 1. Instantiating a robot (using the robot class) # 2. Creating a grid world with landmarks in it # # **This function also prints out the true location of landmarks and the *final* robot location, which you should refer back to when you test your implementation of SLAM.** # # The `data` this returns is an array that holds information about **robot sensor measurements** and **robot motion** `(dx, dy)` that is collected over a number of time steps, `N`. You will have to use *only* these readings about motion and measurements to track a robot over time and find the determine the location of the landmarks using SLAM. We only print out the true landmark locations for comparison, later. # # # In `data` the measurement and motion data can be accessed from the first and second index in the columns of the data array. See the following code for an example, where `i` is the time step: # ``` # measurement = data[i][0]
def make_batch(batch_size): # this model can only train on data where a spaceship is guaranteed, this is not true when testing imgs, labels = zip(*[make_data(has_spaceship=True) for _ in range(batch_size)]) imgs = np.stack(imgs) labels = np.stack(labels) return imgs, labels
def main(): # world parameters num_landmarks = 5 # number of landmarks N = 20 # time steps world_size = 100.0 # size of world (square) # robot parameters measurement_range = 50.0 # range at which we can sense landmarks motion_noise = 2.0 # noise in robot motion measurement_noise = 2.0 # noise in the measurements distance = 20.0 # distance by which robot (intends to) move each iteratation # make_data instantiates a robot, AND generates random landmarks for a given world size and number of landmarks data = make_data(N, num_landmarks, world_size, measurement_range, motion_noise, measurement_noise, distance) # print out some stats about the data time_step = 0 print('Example measurements: \n', data[time_step][0]) print('\n') print('Example motion: \n', data[time_step][1]) # define a small N and world_size (small for ease of visualization) N_test = 5 num_landmarks_test = 2 small_world = 10 # initialize the constraints initial_omega, initial_xi = initialize_constraints(N_test, num_landmarks_test, small_world) plt.rcParams["figure.figsize"] = (10, 7) # display omega (need to convert omega to a 2x2 matrix for the heatmap to show) display_omega = reformat_omega(initial_omega) sns.heatmap(display_omega, cmap='Blues', annot=True, linewidths=.5) #plt.show() # define figure size plt.rcParams["figure.figsize"] = (1, 7) # display xi sns.heatmap(DataFrame(initial_xi), cmap='Oranges', annot=True, linewidths=.5) #plt.show() ## TODO: Complete the code to implement SLAM # call your implementation of slam, passing in the necessary parameters mu = slam(data, N, num_landmarks, world_size, motion_noise, measurement_noise) # print out the resulting landmarks and poses if (mu is not None): # get the lists of poses and landmarks # and print them out poses, landmarks = get_poses_landmarks(mu, N, num_landmarks) print_all(poses, landmarks) # Display the final world! # define figure size plt.rcParams["figure.figsize"] = (20, 20) # check if poses has been created if 'poses' in locals(): # print out the last pose print('Last pose: ', poses[-1]) # display the last position of the robot *and* the landmark positions display_world(int(world_size), poses[-1], landmarks) print("*** THE END ***")
from skimage.draw import line fig, ax = plt.subplots(1, 3, figsize=(12, 4)) def plot(ax, img, label, title): ax.imshow(img, cmap="gray") ax.set_title(title) if label.size > 0: x, y, _, _, _ = label ax.scatter(x, y, c="r") xy = _make_box_pts(*label) ax.plot(xy[:, 0], xy[:, 1], c="r") img, label = make_data(has_spaceship=True) plot(ax[0], img, label, "example (with spaceship)") img, label = make_data(has_spaceship=False) plot(ax[1], img, label, "example (without spaceship)") img, label = make_data(has_spaceship=True, no_lines=0, noise_level=0) plot(ax[2], img, label, "example (without noise)") fig.savefig("example.png")
from datetime import datetime import numpy as np from Kaggle.CV_Utilities import loadObject from Kaggle.utilities import DatasetPair from helpers import quick_score, make_data bestPipe = loadObject('/home/jj/code/Kaggle/Loan Default Prediction/output/gridSearchOutput/GBC_25fts_simple.pk')['best_estimator'] # ------ small x, y, _, enc = make_data("/home/jj/code/Kaggle/Loan Default Prediction/Data/modSmallTrain.csv", enc=None) # ------ full # x, y, _, enc = make_data("/home/jj/code/Kaggle/Loan Default Prediction/Data/modTrain.csv", enc=None) data = DatasetPair(np.array(x), np.array(y)) # ---------- double-check cv score and classification roc auc dt = datetime.now() print 'jj score:', quick_score(bestPipe, data.X, data.Y) print 'quick jj score took', datetime.now() - dt dt = datetime.now() bestPipe.classification_metrics(data.X, data.Y, n_iter=10) print 'Classifier metrics took', datetime.now() - dt