def evaluate_model(self, X_test, y_test, batch_size): # Standardize the model input if self.standardization_mode == 'per_slice' or \ self.standardization_mode == 'per_sample' or \ self.standardization_mode == 'per_batch': print('Standardizing test data with mode: ', self.standardization_mode) X_test = impro.standardize_data(data=X_test, mode=self.standardization_mode) else: print('Test data is not standardized.') # Expand the dimensions if len(X_test.shape) == 3: # Expand the batch (dim0) and channels (dim4) X_test = np.expand_dims(np.expand_dims(X_test, axis=0), axis=4) if len(X_test.shape) == 4: # Expand the channels (dim4) X_test = np.expand_dims(X_test, axis=4) if len(y_test.shape) == 3: # Expand the batch (dim0) and channels (dim4) y_test = np.expand_dims(np.expand_dims(y_test, axis=0), axis=4) if len(y_test.shape) == 4: # Expand the channels (dim4) y_test = np.expand_dims(y_test, axis=4) test_loss = self.model.evaluate(x=X_test, y=y_test, batch_size=batch_size, verbose=1, sample_weight=None, steps=None) return test_loss
def predict_sample(self, X_pred): # Standardize the model input X_pred, mean, sigma = impro.standardize_data(X_pred) # Expand the dims for batches and channels X_pred = np.expand_dims(np.expand_dims(X_pred, axis=3), axis=0) # Predict y y_pred = self.model.predict(X_pred) y_pred = y_pred[0, :, :, :, 0] / self.linear_output_scaling_factor return y_pred
def predict_batch(self, X_pred_batch): # Standardize each model input in the batch for i in range(np.size(X_pred_batch[0])): X_pred = X_pred_batch[i, ] X_pred, mean, sigma = impro.standardize_data(X_pred) X_pred_batch[i, ] = X_pred # Expand the dims for channels X_pred_batch = np.expand_dims(X_pred_batch, axis=3) # Predict y y_pred_batch = self.model.predict(X_pred_batch) return y_pred_batch
def predict_batch(self, X_pred_batch): # Standardize the model input if self.standardization_mode == 'per_slice' or \ self.standardization_mode == 'per_sample' or \ self.standardization_mode == 'per_batch': #print('Standardizing input data with mode: ', self.standardization_mode) X_pred_batch = impro.standardize_data( data=X_pred_batch, mode=self.standardization_mode) # Expand the dims for channels X_pred_batch = np.expand_dims(X_pred_batch, axis=4) # Predict y y_pred_batch = self.model.predict(X_pred_batch) # Undo the linear scaling y_pred_batch = y_pred_batch[0, :, :, :, 0] / self.linear_output_scaling_factor return y_pred_batch
def load_data(path_to_dataset, data_list, input_shape, standardization_mode=None, border=None): # Make the data matrix X_data = np.zeros(shape=(np.size(data_list), input_shape[0], input_shape[1], input_shape[2]), dtype='float32') if border == None: y_data = np.zeros(shape=(np.size(data_list), input_shape[0], input_shape[1], input_shape[2]), dtype='float32') else: y_data = np.zeros(shape=(np.size(data_list), input_shape[0] - 2 * border[0], input_shape[1] - 2 * border[1], input_shape[2] - 2 * border[2]), dtype='float32') # Load the data into the data matrix for i in range(np.size(data_list)): filepath = os.path.join(path_to_dataset, data_list[i]) data, header = nrrd.read(filepath) X_data[i, ] = data[0, ] if border == None: y_data[i, ] = data[1, ] else: y_data[i, ] = impro.get_inner_slice(data[1, ], border) # Standardize the input-data if standardization_mode == 'per_slice' or \ standardization_mode == 'per_sample' or \ standardization_mode == 'per_batch': X_data = impro.standardize_data(data=X_data, mode=standardization_mode) return X_data, y_data
def predict_sample(self, X_pred): # Standardize the model input if self.standardization_mode == 'per_slice' or \ self.standardization_mode == 'per_sample' or \ self.standardization_mode == 'per_batch': #print('Standardizing input data with mode: ', self.standardization_mode) #print('Standardization function: impro.standardize_data') X_pred = impro.standardize_data(data=X_pred, mode=self.standardization_mode) #print('Standardization function: impro.standardize_3d_image') #X_pred = impro.standardize_3d_image(X_pred) # Expand the dims for batches and channels X_pred = np.expand_dims(np.expand_dims(X_pred, axis=3), axis=0) # Predict y y_pred = self.model.predict(X_pred) # Undo the linear scaling y_pred = y_pred[0, :, :, :, 0] / self.linear_output_scaling_factor return y_pred