def run(self):

        batch_size = 32
        split_name = 'sample_test'
        #         split_name = 'train'
        #         split_name = 'eval'
        generator, dataset_size = self.input_batch_generator(
            split_name,
            is_training=True,
            batch_size=batch_size,
            get_filenames=True,
            get_sparselabel=True)
        num_batches_per_epoch = int(math.ceil(dataset_size /
                                              float(batch_size)))
        for _ in range(num_batches_per_epoch):
            batch_inputs, batch_labels_sparse, batch_labels = next(generator)
            batch_inputs = np.array(batch_inputs)
            print(batch_labels)
            print("batch_size={}".format(len(batch_labels)))
            vis = True
            if vis:
                grid = vis_utils.visualize_grid(batch_inputs[:4])
                grid = np.squeeze(grid)
                plt.imshow(grid, cmap='gray')
                plt.show()
                break

        return
def show_net_weights(net):
    W1 = net.params['W1']
    W1 = W1.reshape(32, 32, 3, -1).transpose(3, 0, 1, 2)
    plt.imshow(visualize_grid(W1, padding=3).astype('uint8'))
    plt.gca().axis('off')
    plt.savefig("TwoLayerNet_CIFAR10_weight.jpg")
    plt.show()
Example #3
0
def show_net_weights(net):
    W1 = net.params['W1']
    W1 = W1.reshape(IMAGE_SIZE[0], IMAGE_SIZE[1], IMAGE_SIZE[2],
                    -1).transpose(3, 0, 1, 2)
    plt.imshow(visualize_grid(W1, padding=3).astype('uint8'))
    plt.gca().axis('off')
    plt.show()
Example #4
0
def vis_activations_from_model(train_data, model, sess, save_dir, seed=10):

    np.random.seed(seed)
    idx = np.random.choice(train_data.shape[0], size=1, replace=False)
    img = train_data[idx[0], :, :, :]
    ## visualize layer-1 kernel weights in grid
    img = np.expand_dims(img, 0)
    h_conv1_1 = sess.run(model['h_conv1_1'], feed_dict={model['x_image']: img})
    h_conv1_1 = h_conv1_1.transpose(3, 1, 2, 0)  # reshape to: (N, H, W, 1)
    vis_grid = visualize_grid(h_conv1_1, grey=True)
    plot_weights_in_grid(vis_grid, os.path.join(save_dir,
                                                'vis_activations.png'))
Example #5
0
def training():
    """
    """
    X_train, Y_train, X_val, Y_val, X_dev, Y_dev, X_test, Y_test = get_CIFAR10_data(
    )
    input_size = 32 * 32 * 3
    hidden_size = 280
    num_classes = 10
    Net = TwoLayerNet(input_size, hidden_size, num_classes)

    result = Net.train(X_train,
                       Y_train,
                       X_val,
                       Y_val,
                       lr_rate=1.6e-3,
                       lr_rate_decay=0.9,
                       reg_strength=0.5,
                       num_iters=3000,
                       batch_size=400,
                       verbose=True)

    y_val_predict = Net.predict(X_val)
    val_acc = np.mean(y_val_predict == Y_val)
    print('Validation accuracy: %f' % val_acc)
    y_test_predict = Net.predict(X_test)
    test_acc = np.mean(y_test_predict == Y_test)
    print('Test accuracy : %f' % test_acc)

    # 绘制损失值
    plt.subplot(211)
    plt.plot(result['loss history'])
    plt.xlabel('Iteration'), plt.ylabel('Loss value')
    plt.title('Loss history')

    plt.subplot(212)
    plt.plot(result['train acc history'], label='train')
    plt.plot(result['val acc history'], label='val')
    plt.xlabel('Epoch'), plt.ylabel('Accuracy value')
    plt.title('Accuracy history')
    plt.show()

    # 将第一层中w1可视化,有32 * 32 * 3个隐藏层神经元,应有32 * 32 * 3张图片
    w1 = Net.params['w1']
    w1 = w1.reshape((32, 32, 3, -1)).transpose(3, 0, 1, 2)
    plt.imshow(visualize_grid(w1, padding=3).astype('uint8'))
    plt.gca().axis('off')
    plt.show()
Example #6
0
                num_epochs=5, batch_size=100,
                update_rule='adam',
                optim_config={
                  'learning_rate': 5e-5,
                },
                verbose=True, print_every=50)
  asolver.train()

###################################################################################
# Visualize Filters                                                               #
#                                                                                 #
# You can visualize the first-layer convolutional filters from the trained        #
# network by running the following:                                               #
###################################################################################

from vis_utils import visualize_grid

if model.params != {}:
  grid = visualize_grid(model.params['theta1'].transpose(0, 2, 3, 1))
  plt.imshow(grid.astype('uint8'))
  plt.axis('off')
  plt.gcf().set_size_inches(5, 5)
  plt.show()

# Problem 3.2.6
###################################################################################
# Decrease the size of the filter and increase number of filters                  #
# The aim is to achieve > 50% validation accuracy                                 #
###################################################################################
# TODO: build your model, set up a solver, train the model, visualize the weights
Example #7
0
# gradient checks and works on toy data, it's time to load up our favorite
# CIFAR-10 data so we can use it to train a classifier on a real dataset.
# Invoke the get_CIFAR10_data function to get our data.

X_train, y_train, X_val, y_val, X_test, y_test = get_CIFAR10_data()
print('Train data shape: ', X_train.shape)
print('Train labels shape: ', y_train.shape)
print('Validation data shape: ', X_val.shape)
print('Validation labels shape: ', y_val.shape)
print('Test data shape: ', X_test.shape)
print('Test labels shape: ', y_test.shape)

# Visualize some images to get a feel for the data
plt.figure(2)
plt.imshow(
    visualize_grid(X_train[:100, :].reshape(100, 32, 32, 3),
                   padding=3).astype('uint8'))
plt.gca().axis('off')
plt.show()

# Train a network
# To train our network we will use SGD. In addition, we will
# adjust the learning rate with an exponential learning rate schedule as
# optimization proceeds; after each epoch, we will reduce the learning rate by
# multiplying it by a decay rate.

input_size = 32 * 32 * 3
hidden_size = 50
num_classes = 10
net = TwoLayerNet(input_size, hidden_size, num_classes)
# Train the network
stats = net.train(X_train,
                update_rule='adam',
                optim_config={
                    'learning_rate': 1e-3,
                },
                verbose=True,
                print_every=20)
# solver.train()

# ## Visualize Filters
# You can visualize the first-layer convolutional filters from the trained network by running the following:

# In[ ]:

from vis_utils import visualize_grid

grid = visualize_grid(model.params['W1'].transpose(0, 2, 3, 1))
plt.imshow(grid.astype('uint8'))
plt.axis('off')
plt.gcf().set_size_inches(5, 5)
plt.show()

# # Spatial Batch Normalization
# We already saw that batch normalization is a very useful technique for training deep fully-connected networks. Batch normalization can also be used for convolutional networks, but we need to tweak it a bit; the modification will be called "spatial batch normalization."
#
# Normally batch-normalization accepts inputs of shape `(N, D)` and produces outputs of shape `(N, D)`, where we normalize across the minibatch dimension `N`. For data coming from convolutional layers, batch normalization needs to accept inputs of shape `(N, C, H, W)` and produce outputs of shape `(N, C, H, W)` where the `N` dimension gives the minibatch size and the `(H, W)` dimensions give the spatial size of the feature map.
#
# If the feature map was produced using convolutions, then we expect the statistics of each feature channel to be relatively consistent both between different imagesand different locations within the same image. Therefore spatial batch normalization computes a mean and variance for each of the `C` feature channels by computing statistics over both the minibatch dimension `N` and the spatial dimensions `H` and `W`.

# ## Spatial batch normalization: forward
#
# In the file `cs231n/layers.py`, implement the forward pass for spatial batch normalization in the function `spatial_batchnorm_forward`. Check your implementation by running the following:
Example #9
0
def show_two_layer_net_weight(net):
    W1 = net.params['W1']
    W1 = W1.reshape(32, 32, 3, -1).transpose(3, 0, 1, 2)
    plt.imshow(visualize_grid(W1, padding=3).astype('uint8'))
    plt.gca().axis('off')
    plt.show()
Example #10
0
def show_net_weights(net):
    W1 = net.params['W1']
    W1 = W1.reshape(3, 32, 32, -1).transpose(3, 1, 2, 0)
    plt.imshow(visualize_grid(W1, padding=3).astype('uint8'))
    plt.gca().axis('off')