def test_plot_occlusion(self, net_fitted, X_train, y_train): from nolearn.lasagne.visualize import plot_occlusion plot_occlusion(net_fitted, X_train[3:4], [0]) plot_occlusion(net_fitted, X_train[2:5], [1, 2, 3], square_length=3, figsize=(5, 5)) plt.clf() plt.cla()
def test_plot_occlusion_last_layer_has_no_num_units( self, net_with_nonlinearity_layer, X_train, y_train): from nolearn.lasagne.visualize import plot_occlusion net = net_with_nonlinearity_layer net.initialize() plot_occlusion(net, X_train[3:4], [0]) plot_occlusion(net, X_train[2:5], [1, 2, 3], square_length=3, figsize=(5, 5)) plt.clf() plt.cla()
def test_plot_occlusion_colored_non_square( self, net_color_non_square, X_train, y_train): import numpy as np from nolearn.lasagne.visualize import plot_occlusion X = np.hstack( (X_train[:, :20 * 28], X_train[:, :20 * 28], X_train[:, :20 * 28])) X = X.reshape(-1, 3, 20, 28) net_color_non_square.fit(X[:100], y_train[:100]) plot_occlusion(net_color_non_square, X[:1], [0]) plot_occlusion(net_color_non_square, X[:3], [3, 2, 1]) plt.clf() plt.cla()
def plot_occ(path, real): global net img = load_image(path) cats = load_classes() X = np.asarray([img], dtype="float32") plt = plot_occlusion(net, X, [real]) return plt
name = exp_name + '_' + str(date.today()) with open('models/conv_net_'+name+'.pkl', 'wb') as f: cPickle.dump(conv_net, f, -1) conv_net.save_params_to('models/params_'+name) # ----- Train set ---- train_predictions = conv_net.predict_proba(X) make_submission_file(train_predictions[:sample_size], images_id[:sample_size], output_filepath='models/training_'+name+'.csv') # ----- Test set ---- X_test, _, images_id_test = load_numpy_arrays(args['test_file']) print "Test:" print "X_test.shape:", X_test.shape predictions = conv_net.predict_proba(X_test) make_submission_file(predictions, images_id_test, output_filepath='submissions/submission_'+name+'.csv') # ----- Make plots ---- plot_loss(conv_net, "models/loss_"+name+".png", show=False) plot_conv_weights(conv_net.layers_[1], figsize=(4, 4)) plt.savefig('models/weights_'+name+'.png') plot_conv_activity(conv_net.layers_[1], X[0:1]) plt.savefig('models/activity_'+name+'.png') plot_occlusion(conv_net, X[:5], y[:5]) plt.savefig('models/occlusion_'+name+'.png')
def plot_occlusion(self, net, X, y, **kwargs): from nolearn.lasagne.visualize import plot_occlusion plot_occlusion(net, X, y, **kwargs) plt.clf() plt.cla()
def example_occ(n,X,target,sqrL=7,figsize=(9,None)): x1=X[n,:,:,:];x1=x1.reshape(1,1,96,96)*-1 t1=target[n,:] v.plot_occlusion(net,x1,t1,sqrL,figsize) pyplot.show()
draw_to_notebook(net0) plot_loss(net0) #plot helps determine if we are overfitting: #If the train loss is much lower than the validation loss, #we should probably do something to regularize the net. # visualize layer weights plot_conv_weights(net0.layers_[1], figsize = (4,4)) #If the weights just look like noise, we might have to do something #(e.g. use more filters so that each can specialize better). # visualize layers' activities x = X_train[0:1] # an image in the bc01 format (so use X[0:1] instead of just X[0]). plot_conv_activity(net0.layers_[1], x) plot_occlusion(net0, X_train[:5], y_train[:5]) plot_saliency(net0, X_train[:5]) from nolearn.lasagne import PrintLayerInfo layers1 = [ (InputLayer, {'shape': (None, 1, 28, 28)}), (Conv2DLayer, {'num_filters': 32, 'filter_size': (3, 3)}), (MaxPool2DLayer, {'pool_size': (2, 2)}), (Conv2DLayer, {'num_filters': 64, 'filter_size': (3, 3)}), (Conv2DLayer, {'num_filters': 64, 'filter_size': (3, 3)}), (MaxPool2DLayer, {'pool_size': (2, 2)}),
def test_visualize_functions_with_cnn(mnist): # this test simply tests that no exception is raised when using # the plotting functions from nolearn.lasagne import NeuralNet from nolearn.lasagne.visualize import plot_conv_activity from nolearn.lasagne.visualize import plot_conv_weights from nolearn.lasagne.visualize import plot_loss from nolearn.lasagne.visualize import plot_occlusion X, y = mnist X_train, y_train = X[:100].reshape(-1, 1, 28, 28), y[:100] X_train = X_train.reshape(-1, 1, 28, 28) num_epochs = 3 nn = NeuralNet( layers=[ ('input', InputLayer), ('conv1', Conv2DLayer), ('conv2', Conv2DLayer), ('pool2', MaxPool2DLayer), ('conv3', Conv2DLayer), ('conv4', Conv2DLayer), ('pool4', MaxPool2DLayer), ('hidden1', DenseLayer), ('output', DenseLayer), ], input_shape=(None, 1, 28, 28), output_num_units=10, output_nonlinearity=softmax, more_params=dict( conv1_filter_size=(5, 5), conv1_num_filters=16, conv2_filter_size=(3, 3), conv2_num_filters=16, pool2_ds=(3, 3), conv3_filter_size=(3, 3), conv3_num_filters=16, conv4_filter_size=(3, 3), conv4_num_filters=16, pool4_ds=(2, 2), hidden1_num_units=16, ), update=nesterov_momentum, update_learning_rate=0.01, update_momentum=0.9, max_epochs=num_epochs, ) nn.fit(X_train, y_train) plot_loss(nn) plot_conv_weights(nn.layers_['conv1']) plot_conv_weights(nn.layers_['conv2'], figsize=(1, 2)) plot_conv_activity(nn.layers_['conv3'], X_train[:1]) plot_conv_activity(nn.layers_['conv4'], X_train[10:11], figsize=(3, 4)) plot_occlusion(nn, X_train[:1], y_train[:1]) plot_occlusion(nn, X_train[2:4], y_train[2:4], square_length=3, figsize=(5, 5)) # clear figures from memory plt.clf() plt.cla()