def predict(self, record): with self.graph.as_default(): with self.session.as_default(): indices = np.r_[0:6] X = np.transpose( np.transpose(self.test_dict[record]["x"])[indices][:][:]) # X = self.test_dict[record]["x"] X = np.expand_dims(X, 0) X = np.expand_dims(X, 0) X = rescale_array(X) # print(X.shape) Y_pred = self.model.predict(X) all_rows = [] # print(Y_pred.shape) for i in range(len(Y_pred[0][0])): row = {} row["sleepstage"] = mapping[i] row["value"] = Y_pred[0][0][i] all_rows.append(row) with open('../frontend/data/SleepProb.json', 'w') as f: f.write( str(all_rows).replace('\'', '"').replace('None', 'null')) grads = visualization.visualize_saliency(self.model, -2, filter_indices=None, seed_input=X) # print("Predicted values", Y_pred) Y_pred = Y_pred.argmax(axis=-1) return grads, Y_pred[0]
def update_image(self, fft_array, factor=None): scaled_values, sf = rescale_array(np.real(np.log2(fft_array)), self.scale_factor) self.scale_factor = sf self.image = array_to_image(scaled_values) self.setMinimumSize(self.image.size()) self.overlay.resize(self.image.size()) self.update() self.updateGeometry() # I have to deliver, and it's broken on windows. self.setMinimumSize(self.image.size()) self.overlay.resize(self.image.size()) self.update() self.updateGeometry()
def pred_alt(X): preds = [] gt = [] for record in test_dict: all_rows = test_dict[record]['x'] record_y_gt = [] record_y_pred = [] for batch_hyp in chunker(range(all_rows.shape[0])): X = all_rows[min(batch_hyp):max(batch_hyp)+1, ...] Y = test_dict[record]['y'][min(batch_hyp):max(batch_hyp)+1] X = np.expand_dims(X, 0) X = rescale_array(X) Y_pred = model.predict(X) Y_pred = Y_pred.argmax(axis=-1).ravel().tolist() gt += Y.ravel().tolist() preds += Y_pred record_y_gt += Y.ravel().tolist() record_y_pred += Y_pred # fig_1 = plt.figure(figsize=(12, 6)) # plt.plot(record_y_gt) # plt.title("Sleep Stages") # plt.ylabel("Classes") # plt.xlabel("Time") # plt.show() # # fig_2 = plt.figure(figsize=(12, 6)) # plt.plot(record_y_pred) # plt.title("Predicted Sleep Stages") # plt.ylabel("Classes") # plt.xlabel("Time") # plt.show() return preds
validation_steps=300, callbacks=callbacks_list) model.load_weights(file_path) for record in tqdm(test_dict): all_rows = test_dict[record]['x'] record_y_gt = [] record_y_pred = [] for batch_hyp in chunker(range(all_rows.shape[0])): X = all_rows[min(batch_hyp):max(batch_hyp) + 1, ...] Y = test_dict[record]['y'][min(batch_hyp):max(batch_hyp) + 1] X = np.expand_dims(X, 0) X = rescale_array(X) Y_pred = model.predict(X) Y_pred = Y_pred.argmax(axis=-1).ravel().tolist() gt += Y.ravel().tolist() preds += Y_pred record_y_gt += Y.ravel().tolist() record_y_pred += Y_pred f1 = f1_score(gt, preds, average="macro") acc = accuracy_score(gt, preds) print("acc %s, f1 %s" % (acc, f1))