This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from sklearn.datasets import load_iris | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.metrics import classification_report | |
| from sklearn.neighbors import KNeighborsClassifier | |
| #load the iris dataset |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def PCA(x, N): | |
| ''' | |
| Arguments: | |
| x: input features | |
| N: number fo principal components | |
| ''' | |
| #mean | |
| x_mean = np.mean(x) | |
| #standard deviation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def markov_chain(transition_matrix): | |
| # Create a directed graph. | |
| G = nx.DiGraph() | |
| # Nodes represent pages. Assume node labels are 0, 1, 2, ... for simplicity. | |
| num_nodes = transition_matrix.shape[0] | |
| G.add_nodes_from(range(num_nodes)) | |
| # Iterate through the transition matrix to create edges. | |
| for i in range(num_nodes): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def PageRank(transition_matrix, d, max_iterations, conv_thres): | |
| ''' | |
| Arguments: | |
| transition_matrix: a matrix or numpy array representing the probabilities of going from one page to another | |
| d: damping factor | |
| max_iterations: number of iterations | |
| conv_thres: convergence threshold | |
| Return: ranks of each webpage, as columns of the transition matrix | |
| ''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Input image of size = 448*448 | |
| from keras.preprocessing.image import load_img, img_to_array | |
| img = load_img("test2.png") | |
| #Converting image to array | |
| img = img_to_array(img) | |
| img = np.expand_dims(img, axis=0) | |
| #Predicting using the truncated model | |
| feature_output = model_short.predict(img) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Analyzing the filter outputs | |
| #For that, we will define a truncated architecture to study the filter outputs | |
| conv_layer_index=[0, 2, 4, 5, 6, 7, 9, 10, 11, 12] #We can choose any number of convolution layers; simply find the indices of them from the layer array | |
| outputs = [model.layers[i].output for i in conv_layer_index] | |
| model_short = Model(inputs=model.inputs, outputs=outputs) | |
| #The following command will plot the complete analysis of our architecture | |
| print(model_short.summary()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Studying and analyzing the layers of the YOLO architecture | |
| layer = model.layers | |
| filters, biases = model.layers[0].get_weights() #the indices of layers can be chosen once you see the summary of the model | |
| print(layer[0].name, filters.shape) | |
| #Plotting the filters | |
| fig1 = plt.figure(figsize=(8,8)) | |
| columns=8 | |
| rows=8 | |
| n_filters=columns*rows |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Rectified Layer Unit activation function and coefficient of the Leaky ReLU equation is 0.1 | |
| lrelu = keras.layers.LeakyReLU(alpha=0.1) | |
| #Building the YOLO architecture | |
| model = Sequential() | |
| model.add(Conv2D(filters=64, kernel_size= (7, 7), strides=(1, 1), input_shape =(448, 448, 3), padding = 'same', activation=lrelu, kernel_regularizer=l2(5e-4))) | |
| model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding = 'same')) | |
| model.add(Conv2D(filters=192, kernel_size= (3, 3), padding = 'same', activation=lrelu, kernel_regularizer=l2(5e-4))) | |
| model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding = 'same')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| from tensorflow import keras | |
| from keras.models import Model | |
| from keras.models import Sequential | |
| from keras.layers import Dense, Dropout, Flatten | |
| from keras.layers import Conv2D, MaxPooling2D | |
| from keras.regularizers import l2 |