Пример #1
0
    def fit_model_on_fold(self, compiled_model: Model, curr_fold_indices,
                          train_sequences, test_sequences):
        """
        trains compiled (but previously unfitted) model against given indices
        :param compiled_model:
        :param curr_fold_indices:
        :param train_sequences:
        :param test_sequences:
        :return:
        """
        train_indices, val_indices = curr_fold_indices
        x_train = train_sequences[train_indices]
        y_train = self.raw_train_df[
            self.target_cols].iloc[train_indices].values
        train_generator = ELMoTFHubGenerator(
            x_train,
            y_train,
            batch_size=self.batch_size,
            shuffle=True,
            num_processes=self.num_processes).batch_generator()

        x_val = train_sequences[val_indices]
        y_val = self.raw_train_df[self.target_cols].iloc[val_indices].values
        val_generator = ELMoTFHubGenerator(
            x_val,
            y_val,
            batch_size=self.batch_size,
            shuffle=False,
            num_processes=self.num_processes).batch_generator()

        test_generator = ELMoTFHubGenerator(
            test_sequences,
            None,
            batch_size=self.batch_size,
            shuffle=False,
            num_processes=self.num_processes).batch_generator()

        compiled_model.fit(
            train_generator,
            steps_per_epoch=len(train_indices) // self.batch_size,
            epochs=self.epochs,
            validation_data=val_generator,
            validation_steps=len(val_indices) // self.batch_size)

        val_pred = compiled_model.predict_generator(val_generator,
                                                    steps=len(val_indices) //
                                                    self.batch_size,
                                                    verbose=0)
        val_roc_auc_score = roc_auc_score(y_val, val_pred)
        print('ROC-AUC val score: {0:.4f}'.format(val_roc_auc_score))

        test_pred = compiled_model.predict_generator(
            test_generator,
            steps=len(test_sequences) // self.batch_size,
            verbose=0)

        return val_roc_auc_score, test_pred
Пример #2
0
def default_categorical():
    img_in = Input(
        shape=(120, 160, 3), name='img_in'
    )  # First layer, input layer, Shape comes from camera.py resolution, RGB
    x = img_in
    x = Convolution2D(24, (5, 5), strides=(2, 2), activation='relu')(
        x
    )  # 24 features, 5 pixel x 5 pixel kernel (convolution, feauture) window, 2wx2h stride, relu activation
    x = Convolution2D(32, (5, 5), strides=(2, 2), activation='relu')(
        x)  # 32 features, 5px5p kernel window, 2wx2h stride, relu activatiion
    x = Convolution2D(64, (5, 5), strides=(2, 2), activation='relu')(
        x)  # 64 features, 5px5p kernal window, 2wx2h stride, relu
    x = Convolution2D(64, (3, 3), strides=(2, 2), activation='relu')(
        x)  # 64 features, 3px3p kernal window, 2wx2h stride, relu
    x = Convolution2D(64, (3, 3), strides=(1, 1), activation='relu')(
        x)  # 64 features, 3px3p kernal window, 1wx1h stride, relu

    # Possibly add MaxPooling (will make it less sensitive to position in image).  Camera angle fixed, so may not to be needed

    x = Flatten(name='flattened')(x)  # Flatten to 1D (Fully connected)
    x = Dense(100, activation='relu')(
        x)  # Classify the data into 100 features, make all negatives 0
    x = Dropout(.1)(
        x
    )  # Randomly drop out (turn off) 10% of the neurons (Prevent overfitting)
    x = Dense(50, activation='relu')(
        x)  # Classify the data into 50 features, make all negatives 0
    x = Dropout(.1)(
        x)  # Randomly drop out 10% of the neurons (Prevent overfitting)
    # categorical output of the angle
    angle_out = Dense(15, activation='softmax', name='angle_out')(
        x
    )  # Connect every input with every output and output 15 hidden units. Use Softmax to give percentage. 15 categories and find best one based off percentage 0.0-1.0

    # continous output of throttle
    throttle_out = Dense(1, activation='relu', name='throttle_out')(
        x)  # Reduce to 1 number, Positive number only

    model = Model(inputs=[img_in], outputs=[angle_out, throttle_out])
    model.compile(optimizer='adam',
                  loss={
                      'angle_out': 'categorical_crossentropy',
                      'throttle_out': 'mean_absolute_error'
                  },
                  loss_weights={
                      'angle_out': 0.9,
                      'throttle_out': .01
                  })
    model.predict_generator()
    return model
Пример #3
0
class ConvMnist:
    def __init__(self, filename=None):
        '''
		学習済みモデルファイルをロードする (optional)
		'''
        self.model = None
        if filename is not None:
            print('load model: ', filename)
            self.model = load_model(filename)
            self.model.summary()

    def preprocess_input(x, **kwargs):
        '''
		画像前処理(ここでは何もしない)
		'''
        #		x = 255 - x
        return x.astype(np.float32)

    def train(self):
        '''
		学習する
		'''
        # Convolutionモデルの作成
        input = Input(shape=(MODEL_WIDTH, MODEL_HEIGHT, 1))
        conv1 = Conv2D(filters=8,
                       kernel_size=(3, 3),
                       strides=(1, 1),
                       padding='same',
                       activation='relu')(input)
        pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
        conv2 = Conv2D(filters=4,
                       kernel_size=(3, 3),
                       strides=(1, 1),
                       padding='same',
                       activation='relu')(pool1)
        dropout1 = Dropout(0.2)(conv2)
        flatten1 = Flatten()(dropout1)
        output = Dense(units=10, activation='softmax')(flatten1)
        self.model = Model(inputs=[input], outputs=[output])

        self.model.summary()

        self.model.compile(optimizer='adam',
                           loss='categorical_crossentropy',
                           metrics=['accuracy'])

        ## fit_generatorを使用する場合。windowsだと遅い(画像読み込みをうまく並列化できない)
        # データセットをディレクトリ内画像から読み込む用意
        idg_train = ImageDataGenerator(
            validation_split=0.2,
            rescale=1 / 255.,
            #		preprocessing_function=preprocess_input
        )

        # training用データのgenerator(全学習画像の内80%)
        img_itr_train = idg_train.flow_from_directory(
            'mnist_images/train',
            subset="training",
            color_mode="grayscale",
            target_size=(MODEL_WIDTH, MODEL_HEIGHT),
            batch_size=BATCH_SIZE,
            class_mode='categorical',
        )

        # validation用データのgenerator(全学習画像の内20%)
        img_itr_validation = idg_train.flow_from_directory(
            'mnist_images/train',
            subset="validation",
            color_mode="grayscale",
            target_size=(MODEL_WIDTH, MODEL_HEIGHT),
            batch_size=BATCH_SIZE,
            class_mode='categorical',
        )

        # Convolutionモデルの学習
        self.model.fit_generator(
            img_itr_train,
            steps_per_epoch=math.ceil(img_itr_train.samples / BATCH_SIZE),
            epochs=EPOCH_NUM,
            validation_data=img_itr_validation,
            validation_steps=math.ceil(img_itr_validation.samples /
                                       BATCH_SIZE),
        )

        # テスト用データで評価する
        idg_test = ImageDataGenerator(
            rescale=1 / 255.,
            #		preprocessing_function=preprocess_input
        )

        img_itr_test = idg_test.flow_from_directory('mnist_images/test',
                                                    color_mode="grayscale",
                                                    target_size=(MODEL_WIDTH,
                                                                 MODEL_HEIGHT),
                                                    batch_size=BATCH_SIZE,
                                                    class_mode=None,
                                                    shuffle=False)

        # 識別処理実施
        probs = self.model.predict_generator(
            img_itr_test, steps=math.ceil(img_itr_test.samples / BATCH_SIZE))

        # 識別精度を計算
        predictions = np.argmax(probs, axis=1)
        print("score: " +
              str(1.0 * np.sum(predictions == img_itr_test.classes) /
                  img_itr_test.n))

    def save_trained_model(self, filename):
        '''
		学習済みモデルをファイル(h5)に保存する
		'''
        self.model.save(filename)

    def predict(self, input_image):
        '''
		1つのグレースケール入力画像(28x28のndarray)に対して、数字(0~9)を判定する
		ret: result, score
		'''
        if input_image.shape != (MODEL_WIDTH, MODEL_HEIGHT):
            return -1, -1
        input_image = input_image.reshape(1, input_image.shape[0],
                                          input_image.shape[1], 1)
        input_image = input_image / 255.

        probs = self.model.predict(input_image)
        result = np.argmax(probs[0])
        return result, probs[0][result]
Пример #4
0
# ---------------------------------------------
# TRAIN TOP LAYERS ON NEW DATA FOR A FEW EPOCHS|
# ---------------------------------------------
post_training_model = model.fit_generator(training_generator,
                                          steps_per_epoch=((hprm['TRAIN_SIZE'] // hprm['BATCH_SIZE'])+1),
                                          epochs=10,  # number of epochs, training cycles
                                          validation_data=validation_generator,  # performance eval on test set
                                          validation_steps=((hprm['TEST_SIZE'] // hprm['BATCH_SIZE'])+1),
                                          verbose=1,
                                          callbacks=[history,
                                                     npt_monitor])
                                                     # validation_output_callback])

y_pred = model.predict_generator(validation_generator,
                                 steps=(hprm['TEST_SIZE'] // hprm['BATCH_SIZE'])+1,
                                 callbacks=[],
                                 verbose=1)
# ---------------------------------------------------------------------------------------------------------------------

# --------------------------------------
# EXPORT MODEL ARCHITECTURE AND WEIGHTS |
# --------------------------------------
# export model structure to json file:
model_struct_json = model.to_json()
filename = filepattern('model_allfreeze_', '.json')
with open(filename, 'w') as f:
    f.write(model_struct_json)

# export weights to an hdf5 file:
w_filename = filepattern('weights_allfreeze_', '.h5')
model.save_weights(w_filename)
Пример #5
0
        opt = optimizers.SGD(lr=1e-3, momentum=0.9)
        model.compile(loss="categorical_crossentropy",
                      optimizer=opt,
                      metrics=["accuracy"])
        for layer in basemodel.layers:
            layer.trainable = False
        train_generator = DataGenerator(indexes=train, **generator_parameters)
        val_generator = DataGenerator(indexes=test, **generator_parameters)
        model.fit_generator(generator=train_generator,
                            validation_data=val_generator,
                            epochs=8)
        model.save("%s/VGG16_%s.h5" % (dump_folder, fold_id))

        generator_parameters["shuffle"] = False
        val_generator = DataGenerator(indexes=test, **generator_parameters)
        y_pred_raw = model.predict_generator(val_generator)
        y_pred = np.argmax(y_pred_raw, axis=-1)
        y_true = np.argmax(y[test], axis=-1)
        model_predictions = ({
            "path":
            np.array(metadata_df.path.values.tolist())[test],
            "y_true":
            y_true,
            "y_pred":
            y_pred,
            "y_pred_raw":
            y_pred_raw.tolist()
        })
        # export new data
        model_predictions_df = pd.DataFrame(model_predictions)
        model_predictions_df.to_csv(
Пример #6
0
              metrics=['accuracy'])
"""
8. 重新训练组合的新模型,仍然保持 VGG16 的 15 个最低层处于冻结状态。在这个特定的例子中,也使用 Image Augumentator 来增强训练集:
"""
train_datagen = ImageDataGenerator(rescale=1. / 255, horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(train_data_dir,
                                                    target_size=(img_height,
                                                                 img_width),
                                                    batch_size=batch_size,
                                                    class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_height, img_width),
    batch_size=batch_size,
    class_mode='binary',
    shuffle=False)
model.fit_generator(train_generator,
                    steps_per_epoch=nb_train_samples // batch_size,
                    epochs=epochs,
                    validation_data=nb_validation_samples // batch_size,
                    verbose=2,
                    workers=12)
"""
9. 在组合网络上评估结果
"""
score = model.evaluate_generator(validation_generator,
                                 nb_validation_samples / batch_size)
scores = model.predict_generator(validation_generator,
                                 nb_validation_samples / batch_size)
# print(score, scores)
Пример #7
0
    target_size = (image_size, image_size),
    batch_size = BATCH_SIZE_TESTING,
    class_mode = None,
    shuffle = False,
    seed = 123
)

#Need to compile layer[0] for extracting the 256- dim features.
#model.layers[1].compile(optimizer = sgd, loss = OBJECTIVE_FUNCTION, metrics = LOSS_METRICS)

test_generator.reset()
#pred = model.layers[1].predict_generator(test_generator, steps = len(test_generator), verbose = 1) 
f =  Model(inputs=model.input,outputs=model.get_layer('dense').output)
f.compile(optimizer = sgd, loss = OBJECTIVE_FUNCTION, metrics = LOSS_METRICS)
for idx,layer in enumerate(f.layers):
        if(layer.name == 'dense'):
                print(idx,layer.name)
                #print(layer.get_weights())
                print("________________")

pred = f.predict_generator(test_generator, steps = len(test_generator), verbose = 1)
#Predicted labels
#pred2 = model.predict_generator(test_generator, steps = len(test_generator), verbose = 1)
#predicted_class_indices = np.argmax(pred2, axis = 1)

fname = test_generator.filenames
sio.savemat('sketchmatnew.mat',mdict={'feature':pred,'label':fname})

print(pred.shape)

Пример #8
0
# buraya batch halinde alınan resimleri veriyoruz
# steps per epoch her epochta kaç adımda verilerin tümünü alıcak toplam veri sayısı bölü batch boyutu
model.fit_generator(train_batches,
                    steps_per_epoch=4,
                    validation_data=valid_batches,
                    validation_steps=4,
                    epochs=10)

test_images, test_labels = next(test_batches)
print(test_batches.class_indices)  # cat 0. indis dog 1. indis

# test_labels = test_labels[:, 0]
# print(test_labels)
Categories = ['Cat', 'Dog']

predictions = model.predict_generator(test_batches, steps=1)

for i in predictions:
    print(i)

#saving model
model.save('dog_or_cat_mobilnet.h5')


def prepare_image(file):
    img = image.load_img(file, target_size=(224, 224))
    img_array = image.img_to_array(img)
    img_array_expanded_dims = np.expand_dims(img_array, axis=0)
    return mobilenet.preprocess_input(
        img_array_expanded_dims)  #imagei 0 ile 1 arasında normalize eder
Пример #9
0
model.load_weights('./weights/starter.hdf5')

test_paths = glob(os.path.join(data_dir, 'test/audio/*wav'))

def test_generator(test_batch_size):
    while True:
        for start in range(0, len(test_paths), test_batch_size):
            x_batch = []
            end = min(start + test_batch_size, len(test_paths))
            this_paths = test_paths[start:end]
            for x in this_paths:
                x_batch.append(process_wav_file(x))
            x_batch = np.array(x_batch)
            yield x_batch

predictions = model.predict_generator(test_generator(64), int(np.ceil(len(test_paths)*1.0/64)))

classes = np.argmax(predictions, axis=1)

# last batch will contain padding, so remove duplicates
submission = dict()
for i in range(len(test_paths)):
    fname, label = os.path.basename(test_paths[i]), id2name[classes[i]]
    submission[fname] = label

with open('starter_submission.csv', 'w') as fout:
    fout.write('fname,label\n')
    for fname, label in submission.items():
        fout.write('{},{}\n'.format(fname, label))
from fashion.data.data_generatorAGR import DataGeneratorAgro
from fashion.settings import settings

# set gpu
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
os.environ["KERAS_BACKEND"] = "tensorflow"

# gpu growth
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
K.set_session(sess)

model = ResNet50(weights='imagenet')
# input = (224,224,3)

LAYER = "avg_pool"
model = Model(inputs=model.input, outputs=model.get_layer(LAYER).output)

pic_paths = glob.glob(settings["agro_path"] + "**/*", recursive=True)
# WARNING WE SLICE SOME VALUES
pic_df = pd.DataFrame({"path":
                       pic_paths})  #[:len(pic_paths) - (len(pic_paths) % 16)]

data_generatorAgro = DataGeneratorAgro(pic_df, batch_size=128)
predict = model.predict_generator(data_generatorAgro, verbose=1)

np.save("AGRO.npy", predict)
Пример #11
0
def predict_densenet121():

    # DesneNet generators

    test_batches = ImageDataGenerator(
        preprocessing_function= \
            applications.densenet.preprocess_input).flow_from_directory(
        test2_path,
        target_size=(image_size, image_size),
        batch_size=test_batch_size,
        shuffle=False)

    input_tensor = Input(shape=(224, 224, 3))

    #Loading the model
    model = DenseNet121(input_tensor=input_tensor,
                        weights='imagenet',
                        include_top=False)

    # add a global spatial average pooling layer
    x = model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(7, activation='softmax')(x)

    # this is the model we will train
    model = Model(inputs=model.input, outputs=predictions)

    model.load_weights('modelDenseNet121.h5')

    test_batches.reset()

    test_labels = test_batches.classes

    # Make predictions
    predictions = model.predict_generator(test_batches,
                                          steps=test_steps,
                                          verbose=1)

    # Declare a function for plotting the confusion matrix
    def plot_confusion_matrix(cm,
                              classes,
                              normalize=False,
                              title='Confusion matrix',
                              cmap=plt.cm.Blues):
        """
        This function prints and plots the confusion matrix.
        Normalization can be applied by setting `normalize=True`.
        """
        if normalize:
            cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
            print("Normalized confusion matrix")
        else:
            print('Confusion matrix, without normalization')

        print(cm)

        plt.imshow(cm, interpolation='nearest', cmap=cmap)
        plt.title(title)
        plt.colorbar()
        tick_marks = np.arange(len(classes))
        plt.xticks(tick_marks, classes, rotation=45)
        plt.yticks(tick_marks, classes)

        fmt = '.2f' if normalize else 'd'
        thresh = cm.max() / 2.
        for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
            plt.text(j,
                     i,
                     format(cm[i, j], fmt),
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")

        plt.ylabel('True label')
        plt.xlabel('Predicted label')
        plt.tight_layout()
        plt.savefig('confusion_matrix_DenseNet121.png')

    cm = confusion_matrix(test_labels, predictions.argmax(axis=1))

    cm_plot_labels = ['akiec', 'bcc', 'bkl', 'df', 'mel', 'nv', 'vasc']

    plot_confusion_matrix(cm, cm_plot_labels)

    recall = np.diag(cm) / np.sum(cm, axis=1)
    precision = np.diag(cm) / np.sum(cm, axis=0)

    print("Mean recall " + str(np.mean(recall).item()))
    print("Mean precision " + str(np.mean(precision).item()))

    print("Mean recall " + str(
        recall_score(
            test_labels, predictions.argmax(axis=1), average='weighted')))
    print("Balanced Accuracy " +
          str(balanced_accuracy_score(test_labels, predictions.argmax(
              axis=1))))
    print("Mean Precision " + str(
        precision_score(
            test_labels, predictions.argmax(axis=1), average='weighted')))
    print("Mean f1 score " + str(
        f1_score(test_labels, predictions.argmax(axis=1), average='weighted')))

    file = open("DenseNet121_results_last", "w+")
    file.write("Mean recall " + str(np.mean(recall).item()) + "\n")
    file.write("Mean precision " + str(np.mean(precision).item()) + "\n")
    file.write("Mean recall " + str(
        recall_score(
            test_labels, predictions.argmax(axis=1), average='weighted')) +
               "\n")
    file.write(
        "Balanced Accuracy " +
        str(balanced_accuracy_score(test_labels, predictions.argmax(axis=1))) +
        "\n")
    file.write("Mean Precision " + str(
        precision_score(
            test_labels, predictions.argmax(axis=1), average='weighted')) +
               "\n")
    file.write("Mean f1 score " + str(
        f1_score(test_labels, predictions.argmax(
            axis=1), average='weighted')) + "\n")

    file.close()

    predicted_class_indices = np.argmax(predictions, axis=1)

    labels = train_batches.class_indices
    labels = dict((v, k) for k, v in labels.items())
    predictions = [labels[k] for k in predicted_class_indices]

    filenames = test_batches.filenames
    results = pd.DataFrame({
        "Filename": filenames,
        "DenseNet121_Predictions": predictions
    })
    results.to_csv("test_prediction_densenet121.csv", index=False)
Пример #12
0
test_paths = glob(os.path.join('./test/', 'test/*wav'))


def test_generator(test_batch_size):
    while True:
        for start in range(0, len(test_paths), test_batch_size):
            x_batch = []
            end = min(start + test_batch_size, len(test_paths))
            this_paths = test_paths[start:end]
            for x in this_paths:
                x_batch.append(process_wav_file(x))
            x_batch = np.array(x_batch)
            yield x_batch


predictions = model.predict_generator(test_generator(50),
                                      int(np.ceil(len(test_paths) / 50)))

classes = np.argmax(predictions, axis=1)

# last batch will contain padding, so remove duplicates
submission = dict()
for i in range(0, 10500):
    fname, label = os.path.basename(test_paths[i]), id2name[classes[i]]
    submission[fname] = label

with open('H5.csv', 'w') as fout:
    fout.write('fname,label\n')
    for fname, label in submission.items():
        fout.write('{},{}\n'.format(fname, label))
Пример #13
0
def test_generator(test_batch_size):
    while True:
        for start in range(0, len(test_paths), test_batch_size):
            x_batch = []
            x_batch_1d = []
            end = min(start + test_batch_size, len(test_paths))
            this_paths = test_paths[start:end]
            for x in this_paths:
                x_2d, x_1d = process_wav_file(x, phase='TEST')
                x_batch.append(x_2d)
                x_batch_1d.append(x_1d)
            x_batch = np.array(x_batch)
            x_batch_1d = np.array(x_batch_1d)
            yield [x_batch, x_batch_1d]


predictions = model.predict_generator(
    test_generator(batch_size), int(np.ceil(len(test_paths) / batch_size)))
classes = np.argmax(predictions, axis=1)

# last batch will contain padding, so remove duplicates
submission = dict()
for i in range(len(test_paths)):
    fname, label = os.path.basename(test_paths[i]), id2name[classes[i]]
    submission[fname] = label

with open(root_dir + weight_name + '.csv', 'w') as fout:
    fout.write('fname,label\n')
    for fname, label in submission.items():
        fout.write('{},{}\n'.format(fname, label))
Пример #14
0
def get_apoz(model, layer, x_val, node_indexes=None):
    """Identify neurons with high Average Percentage of Zeros (APoZ).

    The APoZ a.k.a. (A)verage (P)ercentage (o)f activations equal to (Z)ero,
    is a metric for the usefulness of a channel defined in this paper:
    "Network Trimming: A Data-Driven Neuron Pruning Approach towards Efficient
    Deep Architectures" - [Hu et al. (2016)][]
    `high_apoz()` enables the pruning methodology described in this paper to be
    replicated.

    If node_indexes are not specified and the layer is shared within the model
    the APoZ will be calculated over all instances of the shared layer.

    Args:
        model: A Keras model.
        layer: The layer whose channels will be evaluated for pruning.
        x_val: The input of the validation set. This will be used to calculate
            the activations of the layer of interest.
        node_indexes(list[int]): (optional) A list of node indices.

    Returns:
        List of the APoZ values for each channel in the layer.
    """

    if isinstance(layer, str):
        layer = model.get_layer(name=layer)

    # Check that layer is in the model
    if layer not in model.layers:
        raise ValueError('layer is not a valid Layer in model.')

    layer_node_indices = utils.find_nodes_in_model(model, layer)
    # If no nodes are specified, all of the layer's inbound nodes which are
    # in model are selected.
    if not node_indexes:
        node_indexes = layer_node_indices
    # Check for duplicate node indices
    elif len(node_indexes) != len(set(node_indexes)):
        raise ValueError('`node_indexes` contains duplicate values.')
    # Check that all of the selected nodes are in the layer
    elif not set(node_indexes).issubset(layer_node_indices):
        raise ValueError('One or more nodes specified by `layer` and '
                         '`node_indexes` are not in `model`.')

    data_format = getattr(layer, 'data_format', 'channels_last')
    # Perform the forward pass and get the activations of the layer.
    mean_calculator = utils.MeanCalculator(sum_axis=0)
    for node_index in node_indexes:
        act_layer, act_index = utils.find_activation_layer(layer, node_index)
        # Get activations
        if hasattr(x_val, "__iter__") and not isinstance(x_val, np.ndarray):
            temp_model = Model(model.inputs,
                               act_layer.get_output_at(act_index))
            a = temp_model.predict_generator(
                x_val, x_val.n // x_val.batch_size)
        else:
            get_activations = k.function(model.input, act_layer.output)

            a = get_activations([x_val, 0])
            # Ensure that the channels axis is last
        if data_format == 'channels_first':
            a = np.swapaxes(a, 1, -1)
        # Flatten all except channels axis
        activations = np.reshape(a, [-1, a.shape[-1]])
        zeros = (activations == 0).astype(int)
        mean_calculator.add(zeros)

    return mean_calculator.calculate()