def evaluate(split): print('Loading {} set...'.format(split)) x, y_true = load_data(split) x = preprocess_input(x) y_pred = model.predict(x) y_pred = y_pred.argmax(axis=1) print("{} set statistics:".format(split)) print("Top-1-accuracy: {:.4f}".format(np.mean(y_true == y_pred))) print(metrics.classification_report(y_true, y_pred))
def grad_cam(x, class_ix, layer): x = np.expand_dims(x, axis=0) x = preprocess_input(x) last_conv_features = model.get_layer(layer).output class_prob = model.get_layer("predictions").output grads = K.gradients(class_prob[:, class_ix], last_conv_features)[0] weights = GlobalAveragePooling2D()(Activation("relu")(grads)) grad_cam_fn = K.function([model.input, K.learning_phase()], [last_conv_features, weights]) learning_phase = 0 out = grad_cam_fn([x, learning_phase]) heatmap = out[0] * out[1].reshape(1, 1, 1, out[1].shape[-1]) heatmap = np.sum(heatmap, axis=3) return heatmap
# increase lr lr *= self.lr_mult K.set_value(self.model.optimizer.lr, lr) def plot(self): plt.plot(self.lrs, self.losses) plt.xscale("log") plt.xlabel("learning rate (log scale)") plt.ylabel("loss") if __name__ == "__main__": # set up data x_train, y_train = load_data("train") num_classes = 12 y_train = keras.utils.to_categorical(y_train, num_classes) x_train = preprocess_input(x_train) # set up model model = DeepYeast() model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.SGD()) # search for lr lr_finder = LearningRateFinder(model) lr_finder.find() lr_finder.plot()
import keras from deepyeast.dataset import load_data from deepyeast.utils import preprocess_input from deepyeast.models import DeepYeast # set up data x_val, y_val = load_data("val") x_train, y_train = load_data("train") num_classes = 12 y_val = keras.utils.to_categorical(y_val, num_classes) y_train = keras.utils.to_categorical(y_train, num_classes) x_train = preprocess_input(x_train) x_val = preprocess_input(x_val) # set up model model = DeepYeast() model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(), metrics=['accuracy']) filepath = "../weights-{epoch:02d}-{val_acc:.3f}.hdf5" checkpoint = keras.callbacks.ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max') reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_acc',