コード例 #1
0
def displayGridRandom(path_dir: Path,
                      total_img: int,
                      grid_row: int = 3,
                      grid_col: int = 3,
                      figsize: tuple = (3, 3)) -> None:
    """
        Display random selected images w/Colab widget Grid (to use only in Colab).
        Args
            path_dir    : path to image directory
            total_img   : total number of images to be displayed
            row         : select number of grid row
            col         : select number of grid column
            figsize     : H,W of figure size to be displayed
        Return
            None
    """
    if total_img > grid_row * grid_col:
        print(
            f'To diplay {total_img} images, please increase grid row or col number.'
        )
    else:
        pass

    grid = widgets.Grid(grid_row, grid_col)  # row x col
    n = len(itemize(path_dir))
    choice = np.random.choice(n, 6, replace=False).tolist()
    index_list = [itemize(path_dir)[i] for i in choice]

    for id, (row, col) in enumerate(grid):
        try:
            print(f'Fname: {Path(index_list[id][1].name)}')
            displayImage(Path(index_list[id][1]), figsize=figsize)
            print(f'Image Size(HxW):{imageSize(Path(index_list[id][1]))}')
        except:
            pass
コード例 #2
0
def main_loop(args, device, train_loader, test_loader, train_size):
    model = Net().to(device)
    optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum)
    
    #epoch = load_checkpoint(optimizer, model, 'checkpoints/mnist-010.pkl')
    #print('Resuming training from epoch', epoch)
    grid = widgets.Grid(2,1)
    
    train_losses = []
    test_losses = []
    
    for epoch in range(1, args.epochs + 1):
        train_loss = train(args, model, device, train_loader, optimizer, epoch, grid, train_size)
        train_losses.append(train_loss)
        
        test_loss = test(args, model, device, test_loader, grid)
        test_losses.append(test_loss)

        #checkpoint_filename = 'checkpoints/mnist-{:03d}.pkl'.format(epoch)
        #save_checkpoint(optimizer, model, epoch, checkpoint_filename)
        # Plot loss
        with grid.output_to(1, 0):
            grid.clear_cell()
    
    if args.save_model:
        torch.save(model.state_dict(),"mnist_cnn.pt")
    
    draw_losses(train_losses, test_losses)
コード例 #3
0
    def __init__(self,
                 update_interval: int,
                 interactive: bool = False,
                 print_results: bool = True,
                 google_colab: bool = False,
                 nr_genes: int = None,
                 static_families: bool = True,
                 brains: List[BasicBrain] = None):
        self.nr_genes = nr_genes

        # Tracks results each step
        self.track_results = {
            "Avg Population Size": {gene: [] for gene in range(nr_genes)},
            "Avg Population Age": {gene: [] for gene in range(nr_genes)},
            "Avg Population Fitness": {gene: [] for gene in range(nr_genes)},
            "Best Population Age": {gene: [] for gene in range(nr_genes)},
            "Avg Number of Attacks": {gene: [] for gene in range(nr_genes)},
            "Avg Number of Kills": {gene: [] for gene in range(nr_genes)},
            "Avg Number of Intra Kills": {gene: [] for gene in range(nr_genes)},
            "Avg Number of Populations": []
        }

        # Averages results from the tracker above each update_interval
        self.results = {
            "Avg Population Size": {gene: [] for gene in range(nr_genes)},
            "Avg Population Age": {gene: [] for gene in range(nr_genes)},
            "Avg Population Fitness": {gene: [] for gene in range(nr_genes)},
            "Best Population Age": {gene: [] for gene in range(nr_genes)},
            "Avg Number of Attacks": {gene: [] for gene in range(nr_genes)},
            "Avg Number of Kills": {gene: [] for gene in range(nr_genes)},
            "Avg Number of Intra Kills": {gene: [] for gene in range(nr_genes)},
            "Avg Number of Populations": []
        }

        self.variables = list(self.results.keys())
        self.update_interval = update_interval
        self.interactive = interactive
        self.google_colab = google_colab
        self.families = static_families
        self.colors = ["#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "#000000"]
        self.first_run = True
        self.print_results = print_results

        if not self.families:
            self.nr_genes = 1

        if interactive:
            columns = 3
            self.variables_to_plot = [self.get_val(self.variables, i, columns) for i in range(0, columns ** 2, columns)]

            # Prepare plot for google colab
            if self.google_colab:
                from google.colab import widgets
                self.grid = widgets.Grid(columns, columns)

            # Prepare plot for matplotlib
            else:
                self._init_matplotlib(columns, brains)
        else:
            self.fig = None
コード例 #4
0
def run_style_transfer(content_path,
                       style_path,
                       num_iterations=1000,
                       content_weight=1e3,
                       style_weight=1e-2):
    # We don't need to (or want to) train any layers of our model, so we set their
    # trainable to false.
    model = get_model()
    for layer in model.layers:
        layer.trainable = False

    # Get the style and content feature representations (from our specified intermediate layers)
    style_features, content_features = get_feature_representations(
        model, content_path, style_path)
    gram_style_features = [
        gram_matrix(style_feature) for style_feature in style_features
    ]

    # Set initial image
    init_image = load_and_process_img(content_path)
    init_image = tf.Variable(init_image, dtype=tf.float32)
    # Create our optimizer
    opt = tf.keras.optimizers.Adam(learning_rate=5, beta_1=0.99, epsilon=1e-1)

    # For displaying intermediate images
    iter_count = 1

    # Store our best result
    best_loss, best_img = float('inf'), None

    # Create a nice config
    loss_weights = (style_weight, content_weight)
    cfg = {
        'model': model,
        'loss_weights': loss_weights,
        'init_image': init_image,
        'gram_style_features': gram_style_features,
        'content_features': content_features
    }

    # For displaying
    num_rows = 2
    num_cols = 5
    display_interval = num_iterations / (num_rows * num_cols)
    start_time = time.time()
    global_start = time.time()

    norm_means = np.array([103.939, 116.779, 123.68])
    min_vals = -norm_means
    max_vals = 255 - norm_means
    content_img = load_img(content_path).astype('uint8')
    style_img = load_img(style_path).astype('uint8')

    grid = widgets.Grid(1, 3, header_row=True, header_column=True)

    with grid.output_to(0, 0):
        imshow(content_img, 'Content Image')
    with grid.output_to(0, 1):
        imshow(style_img, 'Style Image')

    plt.subplot(1, 3, 3)
    plt.show()
    imgs = []
    for i in range(num_iterations):
        grads, all_loss = compute_grads(cfg)
        loss, style_score, content_score = all_loss
        opt.apply_gradients([(grads, init_image)])
        clipped = tf.clip_by_value(init_image, min_vals, max_vals)
        init_image.assign(clipped)
        end_time = time.time()

        if loss < best_loss:
            # Update best loss and best image from total loss.
            best_loss = loss
            best_img = deprocess_img(init_image.numpy())

        if i % 20 == 0:
            start_time = time.time()

            # Use the .numpy() method to get the concrete numpy array
            plot_img = init_image.numpy()
            plot_img = deprocess_img(plot_img)
            imgs.append(plot_img)

            with grid.output_to(0, 2):
                grid.clear_cell()
                print(
                    'Transformed Image \n Transfer is {}% done, please be patient'
                    .format(i / num_iterations * 100))
                IPython.display.display_png(Image.fromarray(best_img))

    return best_img, best_loss
コード例 #5
0
ファイル: code_for_hw10.py プロジェクト: wesenu/6.036-6
class No_Exit(MDP):
    # Like breakout or pong, but one player, no walls to break out, no
    # way to win You can move paddle vertically up or down or stay
    actions = (+1, 0, -1)
    
    def __init__(self, field_size, ball_speed = 1, random_start = True):
        # image space is n by n
        self.q = None
        self.n = field_size
        h = self.n * ball_speed 
        self.discount_factor = (h - 1.0) / h
        self.ball_speed = ball_speed
        # state space is: ball position and velocity, paddle position
        # and velocity
        # - ball position is n by n
        # - ball velocity is one of (-1, -1), (-1, 1), (0, -1), (0, 1),
        #                          (1, -1), (1, 1)
        # - paddle position is n; this is location of bottom of paddle,
        #    can stick "up" out of the screen
        # - paddle velocity is one of 1, 0, -1
        self.states = [((br, bc), (brv, bcv), pp, pv) for \
                         br in range(self.n) for 
                         bc in range(self.n) for
                         brv in (-1, 0, 1) for
                         bcv in (-1, 1) for 
                         pp in range(self.n) for 
                         pv in (-1, 0, 1)]
        self.states.append('over')
        self.start = dist.uniform_dist([((br, 0), (0, 1), 0, 0) \
                                        for br in range(self.n)]) \
                if random_start else  \
                dist.delta_dist(((int(self.n/2), 0), (0, 1), 0, 0))

    ax = None
    ims = None
    # Updating values in google colab
    try:
        from google.colab import widgets
        IS_COLAB = True
        grid = widgets.Grid(1, 10, header_row=False, header_column=False)
        parity = 0
    except:
        IS_COLAB = False
        grid = None

    def draw_state(self, state = None, pause = False):
        def _update(self, state, pause):
            if self.ax is None or self.IS_COLAB:
                plt.ion()
                plt.figure(facecolor="white")
                self.ax = plt.subplot()

            if state is None: state = self.state
            ((br, bc), (brv, bcv), pp, pv) = state
            im = np.zeros((self.n, self.n+1))
            im[br, bc] = -1
            im[pp, self.n] = 1
            self.ax.cla()
            self.ims = self.ax.imshow(im, interpolation = 'none',
                                    cmap = 'viridis', 
                                    extent = [-0.5, self.n+0.5,
                                                -0.5, self.n-0.5],
                                    animated = True)
            self.ims.set_clim(-1, 1)
            plt.pause(0.0001) 
            if pause: input('go?')
            else: plt.pause(0.1 if self.IS_COLAB else 0.01) 

        if self.IS_COLAB:
            with self.grid.output_to(0, (self.parity % 10)):
                # _update(self, state, pause)
                self.grid.clear_cell(0, (self.parity + 1) % 10)
                self.parity =  (self.parity + 9) % 10
        else:
            _update(self, state, pause)

    def state2vec(self, s):
        if s == 'over':
            return np.array([[0, 0, 0, 0, 0, 0, 1]])
        ((br, bc), (brv, bcv), pp, pv) = s
        return np.array([[br, bc, brv, bcv, pp, pv, 0]])

    def terminal(self, state):
        return state == 'over'

    def reward_fn(self, s, a):
        return 0 if s == 'over' else 1
        
    def transition_model(self, s, a, p = 0.4):
        # Only randomness is in brv and brc after a bounce
        # 1- prob of negating nominal velocity
        if s == 'over':
            return dist.delta_dist('over')
        # Current state
        ((br, bc), (brv, bcv), pp, pv) = s
        # Nominal next ball state
        new_br = br + self.ball_speed*brv; new_brv = brv
        new_bc = bc + self.ball_speed*bcv; new_bcv = bcv
        # nominal paddle state, a is action (-1, 0, 1)
        new_pp = max(0, min(self.n-1, pp + a))
        new_pv = a
        new_s = None
        hit_r = hit_c = False
        # bottom, top contacts
        if new_br < 0:
            new_br = 0; new_brv = 1; hit_r = True
        elif new_br >= self.n:
            new_br = self.n - 1; new_brv = -1; hit_r = True
        # back, front contacts
        if new_bc < 0:                  # back bounce
            new_bc = 0; new_bcv = 1; hit_c = True
        elif new_bc >= self.n:
            if self.paddle_hit(pp, new_pp, br, bc, new_br, new_bc):
                new_bc = self.n-1; new_bcv = -1; hit_c = True
            else:
                return dist.delta_dist('over')

        new_s = ((new_br, new_bc), (new_brv, new_bcv), new_pp, new_pv)
        if ((not hit_c) and (not hit_r)):
            return dist.delta_dist(new_s)
        elif hit_c:                     # also hit_c and hit_r
            if abs(new_brv) > 0:
                return dist.DDist({new_s: p,
                                   ((new_br, new_bc), (-new_brv, new_bcv), new_pp, new_pv) : 1-p})
            else:
                return dist.DDist({new_s: p,
                                   ((new_br, new_bc), (-1, new_bcv), new_pp, new_pv) : 0.5*(1-p),
                                   ((new_br, new_bc), (1, new_bcv), new_pp, new_pv) : 0.5*(1-p)})
        elif hit_r:
            return dist.DDist({new_s: p,
                               ((new_br, new_bc), (new_brv, -new_bcv), new_pp, new_pv) : 1-p})


    def paddle_hit(self, pp, new_pp, br, bc, new_br, new_bc):
        # Being generous to paddle, any overlap in row
        prset = set(range(pp, pp+2)).union(set(range(new_pp, new_pp+2)))
        brset = set([br, br+1, new_br, new_br+1])
        return len(prset.intersection(brset)) >= 2
コード例 #6
0
               ['income_bracket'].apply(lambda x: '>50K' in x).astype(int))
classes = ['Over $50K', 'Less than $50K']

# To stay consistent, we have to flip the confusion
# matrix around on both axes because sklearn's confusion matrix module by
# default is rotated.
rotated_confusion_matrix = np.fliplr(confusion_matrix(actuals, predictions))
rotated_confusion_matrix = np.flipud(rotated_confusion_matrix)

tb = widgets.TabBar(['Confusion Matrix', 'Evaluation Metrics'], location='top')

with tb.output_to('Confusion Matrix'):
    plot_confusion_matrix(rotated_confusion_matrix, classes)

with tb.output_to('Evaluation Metrics'):
    grid = widgets.Grid(2, 3)

    p, r, fpr = compute_eval_metrics(actuals, predictions)

    with grid.output_to(0, 0):
        print('Precision ')
    with grid.output_to(1, 0):
        print(' %.4f ' % p)

    with grid.output_to(0, 1):
        print(' Recall ')
    with grid.output_to(1, 1):
        print(' %.4f ' % r)

    with grid.output_to(0, 2):
        print(' False Positive Rate ')