Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save adnan-bashir-fuaad/926f0a4bda30c74a0c4a011975df3daf to your computer and use it in GitHub Desktop.

Select an option

Save adnan-bashir-fuaad/926f0a4bda30c74a0c4a011975df3daf to your computer and use it in GitHub Desktop.
Extracting the activations from layers with Keras and pickling them for future use with Scikit-Learn
model = load_model('/home/adnan/Datasets/Network-weights/FCN-Classifier-Light-CIFAR-10/Classifier_CIFAR10_FCN_light_trained_models-97-0.7636.h5')
layer_outputs = [layer.output for layer in model.layers]
activation_model = Model(inputs=model.input, outputs=layer_outputs)
def get_activations(img):
layer_activations = list()
activations = activation_model.predict(img)
layer_count = 0
for activation in activations:
lst = list()
if len(activation.shape) < 4:
break
filter_count = activation.shape[3]
layer_count += 1
if layer_count not in important_layers:
continue
for count in range(filter_count):
x = activation[0, :, :, count].reshape(1, -1)
lst.append(x)
lst = np.array(lst)
lst = lst.reshape(-1, 1)
layer_activations.append(lst)
return np.array(layer_activations)
temp = list()
bar = list()
for img_id in range(10000):
print(img_id)
temp.append(get_activations(X_train_edited[img_id]))
bar.append(np.count_nonzero((x_train_edited[img_id] - x_train[img_id]).flatten()))
pickle.dump({'X_train': np.array(temp).squeeze(axis=(1, 3,)), 'y_train': np.array(bar).reshape(-1, 1)}, open('/home/adnan/Desktop/Project-Edit-Pipeline/Edited-Activations-1.pkl', 'wb'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment