<jupyter_start><jupyter_text>Compressed Sensing MR - CNN TrainingIn this notebook we are going to train a CNN to perform compressed sensing. In this illustration we extract patches using the [Calgary-Campinas-347](http://miclab.fee.unicamp.br/tools) public dataset. The goal of this tutorial is: - Illustrate how to to train a CNN for compressed sensing.<jupyter_code>%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import nibabel as nib
import os
import glob
import sys
import random
import time

from keras.callbacks import ModelCheckpoint, EarlyStopping

MY_UTILS_PATH = "../Modules/"
if not MY_UTILS_PATH in sys.path:
    sys.path.append(MY_UTILS_PATH)
import cnn_utils
patches_path = "/media/roberto/DATA/CS-CC347"
max_value = 1000.0
#Loading data
data = np.load(os.path.join(patches_path,"cs_cc347_orig.npy")).astype(float)/max_value
data_under = np.load(os.path.join(patches_path,"cs_cc347_under.npy")).astype(float)/max_value

# Splitting into development and test set
n = int(data.shape[0]*0.3)
data_train = data[:-n]
data_test = data[-n:]
data_train_under = data_under[:-n]
data_test_under = data_under[-n:]

#Shuffling samples
indexes = np.arange(0,data_train.shape[0],dtype = int)
np.random.shuffle(indexes)
data_train = data_train[indexes]
data_train_under = data_train_under[indexes]
print data_train.shape
print data_test.shape
# Early stopping callback to shut down training after 10 epochs with no improvement
earlyStopping = EarlyStopping(monitor='mean_absolute_error',
                                        patience=5, 
                                        verbose=1, mode='min')
# Checkpoint callback to save model after each improvement along the epochs
checkpoint = ModelCheckpoint('cnn_cs_2x_best.hdf5', mode = 'min', monitor='mean_absolute_error'
                            ,verbose=0, save_best_only=True, save_weights_only = True)

model = cnn_utils.get_unet_mod_reg((256,256),nchannels = 1,\
                                   learning_rate = 1e-3,\
                                   learning_decay =1e-6)   
model.load_weights('cnn_cs_2x_best.hdf5')
print model.summary()
hist = model.fit(data_train_under[:,:,:,np.newaxis],data_train[:,:,:,np.newaxis],epochs=80,
                 batch_size=16,
                 verbose=1,
                 validation_split=0.3,callbacks=[checkpoint,earlyStopping])       
np.save(os.path.join(patches_path,"cs_cc347_train.npy"),(data_train*1000).astype(np.uint16))
np.save(os.path.join(patches_path,"cs_cc347_train_under.npy"),(data_train_under*1000).astype(np.uint16))
np.save(os.path.join(patches_path,"cs_cc347_test.npy"),(data_test*1000).astype(np.uint16))
np.save(os.path.join(patches_path,"cs_cc347_test.npy"),(data_test_under*1000).astype(np.uint16))
model = cnn_utils.get_unet_mod_reg((256,256),nchannels = 1,\
                                   learning_rate = 1e-3,\
                                   learning_decay =5e-6)   
model.load_weights('cnn_cs_2x_best.hdf5')
predict = model.predict(data_test_under[:,:,:,np.newaxis])[:,:,:,0]
error = np.abs(predict-data_test).mean()
print error
print data_test.mean()
plt.figure()
plt.scatter(np.arange(data_test.shape[0]),np.abs(predict-data_test).mean(axis = (1,2))/data_test.mean())
plt.grid()
plt.show()
nb = 400
print nb
plt.figure(dpi = 300)
plt.subplot(131)
plt.imshow(data_test[nb],cmap = 'gray')
plt.axis("off")
plt.subplot(132)
plt.imshow(data_test_under[nb],cmap = 'gray')
plt.axis("off")
plt.subplot(133)
plt.imshow(predict[nb],cmap = 'gray')
plt.axis("off")
plt.show()<jupyter_output>400
{'fraction_non_alphanumeric': 0.13028764805414553, 'fraction_numerical': 0.0338409475465313, 'mean_word_length': 4.0383522727272725, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '552761', 'license': 'MIT', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1240, 'n_words': 220, 'path': 'Drafts/compressed_sensing_mr_cnn_training.ipynb', 'repo_name': 'rmsouza01/ML101', 'size': '3546'}