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))
break # 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()
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',
### plot image def stretch_contrast(img): p2, p98 = np.percentile(img, (2, 98)) img_rescale = exposure.rescale_intensity(img, in_range=(p2, p98)) return img_rescale def add_third_dimension(x): b_channel = np.zeros((64, 64, 1), dtype=np.uint8) x = np.append(x, b_channel, axis=-1) return x x_val, y_val = load_data("val") i = 0 x = x_val[i] x = add_third_dimension(x) x = stretch_contrast(x) plt.imshow(x) ### plot activation map 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