def enhance_roi(data_id): data_class = 'allClass' data_type = 'cropped_roi' # file paths data_path = f'networks/data/sgp/{data_id}/{data_type}/*' model_path = 'networks/model' img_save_path = 'networks/reconstructed_roi/conv1d' # mkdir if not exists Path(f'{img_save_path}').mkdir(parents=True, exist_ok=True) channel_len = 23 # load test data print('Load test data..') test_dataset, sample_img = load_images_data(data_path, rescale_ratio=0.25) img_height, img_width = sample_img.shape # load model print('Load model..') model = models.conv1d_net(channel_len, 3) model.load_state_dict( torch.load(f'{model_path}/conv1d_on_{data_class}.pth', map_location='cpu')) model.eval() print('Model predict..') predictions = models.predict_class(test_dataset, model) print('Reconstruct..') sample_img = reconstruct_image(sample_img, predictions, enhance_intensity=20) imsave(f'{img_save_path}/{data_id}_conv1d.png', sample_img)
def enhance_roi(data_id): data_class = 'allClass' data_type = 'cropped_roi' # file paths data_path = f'networks/data/sgp/{data_id}/cropped_roi/*' model_path = 'networks/model' img_save_path = 'networks/reconstructed_roi/conv_resid' # mkdir if not exists Path(f'{img_save_path}').mkdir(parents=True, exist_ok=True) # load test data print('Load test data..') test_imgs = load_raw_images_data(data_path, rescale_ratio=0.25, preserve_range_after_rescale=True) sample_img = test_imgs[0] test_dataset, channel_len = load_patch_dataset_from_imgs(test_imgs, patch_size=3) # load model print('Load model..') model = models.conv_resid(channel_len, 3) model.load_state_dict( torch.load(f'{model_path}/conv_resid_on_{data_class}.pth', map_location='cpu')) model.eval() print('Model predict..') predictions = models.predict_class(test_dataset, model) predictions = pad_prediction(predictions, sample_img, test_dataset.patch_size) print('Reconstruct..') sample_img = reconstruct_image(sample_img, predictions, enhance_intensity=20, count_note=True) imsave(f'{img_save_path}/{data_id}_conv_resid.png', sample_img)
elif net_style == 1: model_name = f'fconv{conv_nd}d_on_{data_class}_{data_id}.pth' elif net_style == 0: model_name = f'conv{conv_nd}d_on_{data_class}_{data_id}.pth' torch.save(conv_model.state_dict(), f'{model_path}/{model_name}') # save log df log_df.to_pickle(f'{log_path}/{model_name}_loss_acc_log.pkl') print('Reconstruct..') with torch.no_grad(): output = conv_model(torch.FloatTensor(imgs_norm)) _, conv_pred = torch.max(output.data, 1) print('-max in conv_pred: ', torch.max(conv_pred.data).item()) print('-min in conv_pred: ', torch.min(conv_pred.data).item()) imsave(f'{img_save_path}/{data_id}_orig.png', sample_img) sample_img_conv = reconstruct_image(sample_img, conv_pred, count_note=True) if net_style == 2: img_name = f'{img_save_path}/conv{conv_nd}d_hyb/{data_id}_conv{conv_nd}d_hyb.png' elif net_style == 1: img_name = f'{img_save_path}/fconv{conv_nd}d/{data_id}_fconv{conv_nd}d.png' elif net_style == 0: img_name = f'{img_save_path}/conv{conv_nd}d/{data_id}_conv{conv_nd}d.png' imsave(img_name, sample_img_conv) # plot learning curve plot_loss_acc_one_model(log_df)
channel_train, y_true, channel_len = load_raw_labeled_data() # fit lda print('Model training..') classifier = lda() classifier.fit(channel_train, y_true) precision_clf = classifier.score(channel_train, y_true) prediction = classifier.predict(channel_train) balanced_acc = balanced_accuracy_score(y_true, prediction) kappa = cohen_kappa_score(y_true, prediction) # plot learning curve plot_learning_curve(classifier, 'learning curve of LDA', channel_train, y_true) print('train-accuracy: ', precision_clf) print('balanced-accuracy: ', balanced_acc) print('kappa: ', kappa) # prepare test data print('Prepare test data..') imgs = load_raw_images_data(test_data_path, rescale_ratio=0.25, preserve_range_after_rescale=True) channel_test, _ = flatten_images(imgs) print('Model predict..') predictions = classifier.predict(channel_test) print('Reconstruct..') sample_img = imgs[0] sample_img = reconstruct_image(sample_img, predictions) imsave(f'{img_save_path}/{data_id}_lda.png', sample_img)
sample_img = test_imgs[0] test_dataset, channel_len = load_patch_dataset_from_imgs(test_imgs, patch_size=3) # load model print('Load model..') model = models.conv_hybrid(channel_len, 3) if is_consider_residual: model.load_state_dict( torch.load(f'{model_path}/conv_hybrid_on_{data_class}.pth', map_location='cpu')) else: model.load_state_dict( torch.load(f'{model_path}/conv_hybrid_no_res_on_{data_class}.pth', map_location='cpu')) model.eval() print('Model predict..') predictions = models.predict_class(test_dataset, model) predictions = pad_prediction(predictions, sample_img, test_dataset.patch_size) print('Reconstruct..') sample_img = reconstruct_image(sample_img, predictions, enhance_intensity=20, count_note=True) if is_consider_residual: imsave(f'{img_save_path}/{data_id}_conv_hybrid.png', sample_img) else: imsave(f'{img_save_path}/{data_id}_conv_hybrid_no_res.png', sample_img)
def main(): print('* Start! ') print('* Loading config.json') with open('config.json') as json_data: config = json.load(json_data) depth = int(config["layers"]) width = int(config["neurons_by_layer"]) drop_in = float(config["dropout_input"]) drop_hid = float(config["dropout_hidden"]) num_epochs = int(config["num_epochs"]) winSide = int(config["window_side"]) ImageShape = config["image_shape"] ImageShape = (int(ImageShape[0]),int(ImageShape[1]),int(ImageShape[2])) ValidationSet = utils.getValues(config["validation_set"]) alpha = float(config["alpha"]) # Other global variables PatternShape = (ImageShape[0],ImageShape[1]) winSize = (winSide,winSide) n_features = ImageShape[2]*(winSide**2) print("* Building model and compiling functions...") input_var = T.matrix('inputs') target_var = T.ivector('targets') network = utils.build_custom_mlp(n_features, input_var, depth, width, drop_in, drop_hid) prediction = lasagne.layers.get_output(network) t2 = theano.tensor.extra_ops.to_one_hot(target_var, 2, dtype='int32') error = lasagne.objectives.categorical_crossentropy(prediction, t2) params = lasagne.layers.get_all_params(network, trainable=True) output_model = theano.function([input_var], prediction) # compilation comp_params_updater = [] for w in params: w_in = T.matrix() if(w_in.type != w.type): w_in = T.vector() w_update = theano.function([w_in], updates=[(w, w_in)]) comp_params_updater = comp_params_updater + [w_update] ''' Method that receives the new set of weights and inserts them in the net. ''' def params_updater(all_w): idx_init = 0 params_idx = 0 for w_updater in comp_params_updater: w = params[params_idx] params_idx += 1 w_value_pre = w.get_value() w_act = all_w[idx_init:idx_init+w_value_pre.size] w_value = w_act.reshape(w_value_pre.shape) idx_init += w_value_pre.size w_updater(w_value) return w_t = numpy.load('../data/w_t.npy') params_updater(w_t) print('* Show test images... ') test_n = ValidationSet test_idx = numpy.arange(test_n.size) accuracy = numpy.zeros(test_n.size,) for idx in test_idx: print('* Test image: {}'.format(idx)) x_image, t_image, mask_image = utils.get_images(ImageShape, PatternShape, winSize, test_n[idx]) print('* get_mean. ') x_mean = utils.get_mean(x_image, winSize, ImageShape[2], ImageShape) print('* get_std. ') x_std = utils.get_std(x_image, winSize, ImageShape[2], ImageShape, x_mean) print('* get_predictions. ') y_preds = utils.get_predictions(x_image, ImageShape, PatternShape, winSize, output_model, x_mean, x_std) output_image = utils.reconstruct_image(y_preds,w=winSize, PatternShape=PatternShape, alpha=alpha) t_image = t_image.astype(numpy.float_)/255 mask_image = mask_image.astype(numpy.float_)/255 error_image, accuracy[idx] = utils.get_error_image(output_image, t_image, mask_image) print('Accuracy[{}]: {}'.format(test_n[idx], accuracy[idx])) error_image = numpy.floor(error_image*255) cv2.imwrite('debug/error_image-'+str(test_n[idx])+'.png',error_image) # Output of model output_image = numpy.floor(output_image*255) cv2.imwrite('debug/y_preds-'+str(test_n[idx])+'.png',output_image)
def optimizer(func, x0, fprime, training_data, callback): n1 = ImageShape[0] n2 = ImageShape[1] diff = (winSize[0] - 1) // 2 valid_windows = int(n1 * n2 - diff * 2 * (n1 + n2) + 4 * diff * diff) print('* Optimizer method. ') training_set_idx = 0 n_samples = 1000 w_t = x0 m_t = 0 x_image, t_image, mask_image = utils.get_images( ImageShape, PatternShape, winSize, TrainingSet[training_set_idx]) x_mean = utils.get_mean(x_image, winSize, ImageShape[2], ImageShape) x_std = utils.get_std(x_image, winSize, ImageShape[2], ImageShape, x_mean) train_data = sampleData(valid_windows, n_samples, x_image, t_image, winSize, ImageShape, x_mean, x_std) e_t = func(w_t.astype('float32'), *train_data) e_it = numpy.zeros(num_epochs) de_it = numpy.zeros(num_epochs) auc_it = numpy.zeros(num_epochs) auc_x = numpy.zeros(num_epochs) m_r = 0.99 it2 = 0 for i in numpy.arange(num_epochs): dedw = fprime(w_t.astype('float32'), *train_data) g_t = -dedw l_r = learning_rate m_t = m_r * m_t + g_t * l_r dw_t = m_r * m_t + g_t * l_r w_t = w_t + dw_t e_t = func(w_t.astype('float32'), *train_data) e_it[i] = e_t if (i % 50 == 0): train_data = sampleData(valid_windows, n_samples, x_image, t_image, winSize, ImageShape, x_mean, x_std) print("i: {}, e_t: {}, time: {}".format(i, e_t, time.ctime())) de_it[i] = numpy.abs(dw_t).mean() if ((i > 10) and (i % 400 == 0)): training_set_idx = (training_set_idx + 1) % TrainingSet.size x_image, t_image, mask_image = utils.get_images( ImageShape, PatternShape, winSize, TrainingSet[training_set_idx]) x_mean = utils.get_mean(x_image, winSize, ImageShape[2], ImageShape) x_std = utils.get_std(x_image, winSize, ImageShape[2], ImageShape, x_mean) if ((i > 10) and (i % 800 == 0)): numpy.save('../data/w_t.npy', w_t) sio.savemat( '../data/BVS_data.mat', { 'depth': depth, 'width': width, 'drop_in': drop_in, 'drop_hid': drop_hid, 'w_t': w_t }) y_preds = utils.get_predictions(x_image, ImageShape, PatternShape, winSize, output_model, x_mean, x_std) t_data = utils.sliding_window(t_image, winSize, dim=1, output=1) auc_it[it2] = getAUC(w_t.astype('float32'), y_preds, t_data) print('AUC: {}'.format(auc_it[it2])) auc_x[it2] = i it2 += 1 # debug images fig, ax = plt.subplots(nrows=1, ncols=1) ax.plot(numpy.arange(i), e_it[0:i], 'r-') fig.savefig('debug/error.png') plt.close(fig) fig, ax = plt.subplots(nrows=1, ncols=1) ax.plot(numpy.arange(i), de_it[0:i], 'g-') fig.savefig('debug/dw_t.png') plt.close(fig) fig, ax = plt.subplots(nrows=1, ncols=1) ax.plot(auc_x[0:it2], auc_it[0:it2], 'b-') fig.savefig('debug/auc.png') plt.close(fig) print('Show test imge... ') output_image = utils.reconstruct_image( y_preds, w=winSize, PatternShape=PatternShape, alpha=alpha) img = numpy.floor(output_image * 255) cv2.imwrite( 'debug/image-last-{}.png'.format( TrainingSet[training_set_idx]), img)
def enhanced_roi(data_id): data_class = 'allClass' folio_ids = ['024r_029v', '102v_107r', '214v_221r'] model_data_id = folio_ids[0] data_type = 'cropped_roi' conv_nd = 2 # net_style {'normal': 0, 'fconv': 1, 'hybrid': 2} net_style = 2 # file paths data_path = f'networks/data/sgp/{data_id}/cropped_roi/*' model_path = 'networks/model' img_save_path = 'networks/reconstructed_roi' # mkdir if not exists Path(f'{img_save_path}').mkdir(parents=True, exist_ok=True) # load images print('-load images..') imgs_norm = load_raw_images_data(data_path, rescale_ratio=0.25) sample_img = get_sample_image(data_path, rescale_ratio=0.25) img_height, img_width = sample_img.shape channel_len = 23 # conv model if net_style == 2: if conv_nd == 2: conv_model = models.conv2d_hyb_net(channel_len, img_width, img_height, 3) elif conv_nd == 3: conv_model = models.conv3d_hyb_net(channel_len, img_width, img_height, 3) model_name = f'{model_path}/conv{conv_nd}d_hyb_on_{data_class}_{model_data_id}.pth' elif net_style == 1: if conv_nd == 2: conv_model = models.fconv2d_net(channel_len, img_width, img_height, 3) elif conv_nd == 3: conv_model = models.fconv3d_net(channel_len, img_width, img_height, 3) model_name = f'{model_path}/fconv{conv_nd}d_on_{data_class}_{model_data_id}.pth' elif net_style == 0: if conv_nd == 2: conv_model = models.conv2d_net(channel_len, img_width, img_height, 3) elif conv_nd == 3: conv_model = models.conv3d_net(channel_len, img_width, img_height, 3) model_name = f'{model_path}/conv{conv_nd}d_on_{data_class}_{model_data_id}.pth' conv_model.load_state_dict(torch.load(model_name, map_location='cpu')) conv_model.eval() print('Reconstruct..') with torch.no_grad(): output = conv_model(torch.FloatTensor(imgs_norm)) _, conv_pred = torch.max(output.data, 1) print('-max in conv_pred: ', torch.max(conv_pred.data).item()) print('-min in conv_pred: ', torch.min(conv_pred.data).item()) imsave(f'{img_save_path}/{data_id}_orig_eval.png', sample_img) sample_img_conv = reconstruct_image(sample_img, conv_pred, count_note=True) if net_style == 2: img_name = f'{img_save_path}/conv{conv_nd}d_hyb/{data_id}_conv{conv_nd}d_hyb_eval_model_{model_data_id}.png' elif net_style == 1: img_name = f'{img_save_path}/fconv{conv_nd}d/{data_id}_fconv{conv_nd}d_eval_model_{model_data_id}.png' elif net_style == 0: img_name = f'{img_save_path}/conv{conv_nd}d/{data_id}_conv{conv_nd}d_eval_model_{model_data_id}.png' imsave(img_name, sample_img_conv)
def test_image_reconstruct(self, cb, image, image_blocks): mat = np.reshape(cb, (cb.shape[0], cb.shape[1] * cb.shape[2] * cb.shape[3])).T closest_blocks = [cb[utils.closest_codeblock_index(mat, block)] for block in image_blocks] constructed_im = utils.reconstruct_image(np.array(closest_blocks), image.shape) return constructed_im