Skip to content

Instantly share code, notes, and snippets.

View SE Test.py
from keras.models import load_model
#load the model
test_model = load_model('weights-improvement-108-0.86.hdf5')
#calculate the performance metrics
perf = test_model.evaluate(x_test,y_test)
print('Validation loss - ',perf[0])
View SE Model.py
from keras.models import Sequential
from keras.layers import Input,Conv2D,BatchNormalization,MaxPooling2D,Dropout,Activation,Flatten
from keras import regularizers
from keras import models
from keras.callbacks import ModelCheckpoint
num_classes = 10
weight_decay = 1e-4
img_input = Input(shape=(32,32,3))
View test_CNN.py
from keras.models import load_model
#load the model
test_model = load_model('weights-improvement-125-0.86.hdf5')
perf = test_model.evaluate(x_test,y_test)
print('Validation Loss - ',perf[0])
View Creating Model.py
from keras.models import Sequential
from keras.layers import Input,Conv2D,BatchNormalization,MaxPooling2D,Dropout,Activation,Flatten,Dense
from keras import regularizers
from keras import models
from keras.callbacks import ModelCheckpoint
#we have 10 classes in the dataset
num_classes = 10
#define the input
img_input = Input(shape=(32,32,3))
View Data Import.py
#importing the required libraries
from keras.datasets import cifar10
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
#instantiating the OneHotEncoder
enc = OneHotEncoder()
#loading the CIFAR 10 dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
enc.fit(y_train)
View SQ Layer.py
def squeeze_excite_block(input, ratio=16):
init = input
channel_axis = 1 if K.image_data_format() == "channels_first" else -1
filters = init._keras_shape[channel_axis]
se_shape = (1, 1, filters)
se = GlobalAveragePooling2D()(init)
se = Reshape(se_shape)(se)
se = Dense(filters // ratio, activation='relu', kernel_initializer='he_normal', use_bias=False)(se)
se = Dense(filters, activation='sigmoid', kernel_initializer='he_normal', use_bias=False)(se)
View layerwise_output.py
#importing required libraries and functions
from keras.models import Model
#defining names of layers from which we will take the output
layer_names = ['block1_conv1','block2_conv1','block3_conv1','block4_conv2']
outputs = []
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
#extracting the output and appending to outputs
for layer_name in layer_names:
intermediate_layer_model = Model(inputs=model.input,outputs=model.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(image)
View Grad_CAM_1.py
from vis.visualization import visualize_cam
from vis.utils import utils
from keras import activations
# Utility to search for layer index by name.
# Alternatively we can specify this as -1 since it corresponds to the last layer.
layer_idx = utils.find_layer_idx(model, 'predictions')
#read the image and plot it
image = io.imread('car.jpeg')
io.imshow(image)
View Saliency_1.py
from vis.visualization import visualize_saliency
from vis.utils import utils
from keras import activations
#read the image
image = io.imread('car.jpeg')
#plot the image
io.imshow(image)
View occlusion_1.py
import numpy as np
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Activation, Conv2D, MaxPooling2D
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.preprocessing.image import ImageDataGenerator
from keras.activations import relu
You can’t perform that action at this time.