<jupyter_start><jupyter_text>Visualize convolution kernels<jupyter_code>net_weights  = NNtest.net.get_weights()
conv_filters = net_weights[0].T.squeeze()

N_filter = len(conv_filters)

fig, axs = plt.subplots(1, N_filter, figsize = (18,3))
fig.suptitle('CNN kernels')

for idx, ax in enumerate(axs.flatten()):
    ax.imshow(conv_filters[idx], interpolation='nearest', cmap='binary')
    ax.axis('off')<jupyter_output><empty_output><jupyter_text>Encoding MNIST through the magnificent bottleneck<jupyter_code>def create_mnist_squares(train_X, Nsamples = 100000, upsample=2):
    """ Merge MNSIT images to square of NxN images.    
    """
 
    Nimage, dim_x, dim_y = train_X.shape
    
    randint4 = np.random.randint(0, Nimage, upsample*upsample*Nsamples).reshape(-1, upsample*upsample)
    
    a      = train_X[randint4]
    aa     = a.reshape(-1, upsample, upsample*dim_x, dim_x)
    aamove = np.moveaxis(aa,1,2)
    aaa    = np.squeeze(aamove.reshape(-1,upsample*dim_x,1,upsample*dim_x))
    
    return aaa


class NN_MNIST_encoder:
    "Aboutaleb class for loading and training a neural net with synthetic generated data sets."

    def __init__(self, trainX, testX, layers=[], activationFunc='relu', lossFunc = 'mean_squared_error', optimizer = 'Adam'):

        self.trainX_size, self.image_dim_x, self.image_dim_y  = trainX.shape
        
        # Set images properties.
        self.trainX = np.expand_dims(trainX, 3)
        self.testX  = np.expand_dims(testX, 3)
                
        # Set network properties.
        self.filter_size    = 5
        self.Nconv_filer    = 20
        self.layers         = layers
        self.activationFunc = activationFunc
        self.lossFunc       = lossFunc
        self.optimizer      = optimizer
        
        # Build and train network.
        self.__build_network()
        self.__compile_network()
                        
    def __build_network(self):

        image_shape = (self.image_dim_x, self.image_dim_y, 1)
        kernel_size = (self.filter_size, self.filter_size)
        
        AveragePooling2D = kr.layers.AveragePooling2D
        UpSampling2D = kr.layers.UpSampling2D
        Conv2D = kr.layers.Conv2D
                
        autoenc = kr.models.Sequential()        
        
        autoenc.add(Conv2D(input_shape=image_shape, filters=self.Nconv_filer, kernel_size=self.filter_size,\
                           activation=self.activationFunc, padding='same'))
        autoenc.add(AveragePooling2D(pool_size=4))
        
        autoenc.add(Conv2D(filters=self.Nconv_filer, kernel_size=kernel_size, activation='relu', padding='same'))
        autoenc.add(AveragePooling2D(pool_size=4))
        
        autoenc.add(Conv2D(filters=self.Nconv_filer, kernel_size=[1,1], activation='relu', padding='same'))
        autoenc.add(UpSampling2D(size=4))
        
        autoenc.add(Conv2D(filters=self.Nconv_filer, kernel_size=kernel_size, activation='relu', padding='same'))
        autoenc.add(UpSampling2D(size=4))
        
        autoenc.add(Conv2D(filters=1, kernel_size=self.filter_size, activation='linear', padding='same'))

        self.net = autoenc
            
    def __compile_network(self):
        self.net.compile(loss      = self.lossFunc,
                         optimizer = self.optimizer,
                         metrics   = ['accuracy'])
            
    def train_network(self, batch_size=100, epochs=30):
        self.history = self.net.fit(self.trainX, self.trainX, batch_size=batch_size, epochs=epochs)
        
    def test_network(self):
        testX_probs    = self.net.predict_on_batch(self.testX)
        testX_max_prob = np.argmax(testX_probs, axis=1)
        accuracy       = sum((np.argmax(self.testY, axis=1) - testX_max_prob) == 0)/len(self.testY) 
        
        return accuracy
    
    def test_example(self):
        
        rand_idx     = np.random.randint(len(self.testX))
        image_sample = self.testX[rand_idx]
        shape_idx_to_name = self.testX[rand_idx]
        predict_prop = self.net.predict_on_batch(np.array([image_sample]))[0]
        predict_label = np.argmax(predict_prop)
        
        print('Label:', self.shape_idx_to_name[np.argmax(self.testY[rand_idx])], ', Prediction:', self.shape_idx_to_name[predict_label],\
              ', (prob=', predict_prop[predict_label] , ')')

        display_image(image_sample.squeeze())
train_X_sq = np.pad(train_X, [(0,0), (2,2),(2,2)])
test_X_sq = np.pad(test_X, [(0,0), (2,2),(2,2)])

#train_X_sq = create_mnist_squares(train_X, 100000, 2)
#test_X_sq  = create_mnist_squares(test_X,   20000, 2)
encode = NN_MNIST_encoder(train_X_sq, test_X_sq)
encode.net.summary()

for weights in encode.net.weights:
    print(weights.shape)
encode.train_network()
rnd_idx = np.random.randint(len(test_X_sq))

test_img = test_X_sq[rnd_idx]
test_img_ext = np.expand_dims([test_img], 3)
display_image(test_img)
a = encode.net.predict_on_batch(test_img_ext)
display_image(a.squeeze())
extractor = kr.Model(inputs=encode.net.inputs,
                     outputs=[layer.output for layer in encode.net.layers])
features  = extractor(test_X_sq)
encode.net.inputs
def display_filters(which_layer):
    global BigImage
    
    the_layers=net.layers
    filters=the_layers[which_layer].get_weights()[0]
    s=shape(filters)
    MX=s[0]
    MY=s[1]
    num_in=s[2] # number of input channels
    num_out=s[3] # number of output channels
    BigImage=zeros([num_in*(MX+1),num_out*(MY+1)])
    BigImage[:,:]=1.0
    for j_in in range(num_in):
        for j_out in range(num_out):
            BigImage[j_in*(MX+1):(j_in+1)*(MX+1)-1,j_out*(MY+1):(j_out+1)*(MY+1)-1]=rescale_image(filters[:,:,j_in,j_out])
    plt.imshow(BigImage,origin='lower',interpolation='nearest',cmap='binary')
    plt.axis('off')
    plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1264645072363887, 'fraction_numerical': 0.017401791867677464, 'mean_word_length': 3.0995762711864407, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '632519', 'license': 'MIT', 'n_tokens_mistral': 2108, 'n_tokens_neox': 2038, 'n_words': 298, 'path': 'LectureV/Joris_LectureV_convolutionNN.ipynb', 'repo_name': 'Paijmans/ML_for_physicists', 'size': '5804'}