<jupyter_start><jupyter_text>Show predicted segmentations<jupyter_code>spl = int(np.round(imgs.shape[0]*0.7))
tr_imgs = imgs[:5, np.newaxis, :, :]

luna = './data/luna2016/'
unet = get_unet()
#model_checkpoint = ModelCheckpoint(luna + 'unet.hdf5', monitor='loss', save_best_only=True)
unet.load_weights(luna + 'unet19.h5')

predicted = unet.predict(tr_imgs, batch_size=4)
fig,ax = plt.subplots(5,3,figsize=(16, 16))

for i in range(5):
    ax[i][0].imshow(tr_imgs[i][0],cmap='gray')
    ax[i][1].imshow(predicted[i][0],cmap='gray')
    ax[i][2].imshow(tr_imgs[i][0]*predicted[i][0],cmap='gray')
plt.show()<jupyter_output><empty_output><jupyter_text>Working with Kaggle's dataset<jupyter_code>import dicom
from preprocessing import *
%load_ext autoreload
%autoreload 2

sample_folder = './data/kaggle/sample_images/'
patients = os.listdir(sample_folder)
patients.sort()

first_patient = load_scan(sample_folder + patients[0])
first_patient_pixels = get_pixels_hu(first_patient)
pix_resampled, spacing = resample(first_patient_pixels, first_patient, [1,1,1])
fig,ax = plt.subplots(1, 2,figsize=(12, 12))

ax[0].imshow(first_patient_pixels[80], cmap='gray')
ax[1].imshow(pix_resampled[200], cmap='gray')
plot_3d(pix_resampled, 400)<jupyter_output><class 'numpy.ndarray'> <class 'int'><jupyter_text>Testing predictions on the first patient<jupyter_code>first_patient_masks = []

first_patient_pixels = first_patient_pixels[:, np.newaxis, :, :]

luna = './data/luna2016/'
unet = get_unet()
#model_checkpoint = ModelCheckpoint(luna + 'unet.hdf5', monitor='loss', save_best_only=True)
unet.load_weights(luna + 'unet19.h5')

predicted = unet.predict(first_patient_pixels, batch_size=4)
np.save(luna + 'first-patient-mask-predictions.npy', predicted)<jupyter_output><empty_output><jupyter_text>Showing a heatmap of predictions<jupyter_code>all_predictions = np.zeros_like(first_patient_pixels[80][0], dtype=np.float32)

for prediction in predicted:
    all_predictions += prediction[0]
img = first_patient_pixels[105][0]

fig,ax = plt.subplots(1,3,figsize=(12, 12))
ax[0].imshow(img,cmap='gray')
ax[1].imshow(all_predictions)
ax[2].imshow(img*all_predictions,cmap='gray')
plt.show()
import matplotlib.image as mpimg

for i, img in enumerate(cancer_patient_pixels):
    mpimg.imsave('./data/sample/cancer-patient/%d' % i, img[0], cmap='gray')<jupyter_output><empty_output><jupyter_text>Trying with a patient with cancer now...<jupyter_code>cancer_patient = load_scan(sample_folder + '0acbebb8d463b4b9ca88cf38431aac69')
cancer_patient_pixels = get_pixels_hu(cancer_patient)

cancer_patient_pixels = cancer_patient_pixels[:, np.newaxis, :, :]

predicted = unet.predict(cancer_patient_pixels, batch_size=4)
np.save(luna + 'cancer-patient-mask-predictions.npy', predicted)
heatmap = np.zeros_like(first_patient_pixels[80][0], dtype=np.float32)
threshold = 8.0

for prediction in predicted:
    heatmap += prediction[0]

thresh_heatmap = np.copy(heatmap)
thresh_heatmap[thresh_heatmap <= threshold] = 0
img = first_patient_pixels[80][0]

fig,ax = plt.subplots(1,4,figsize=(12, 12))
ax[0].imshow(img,cmap='gray')
ax[1].imshow(heatmap, cmap='hot')
ax[2].imshow(thresh_heatmap, cmap='hot')
ax[3].imshow(img*thresh_heatmap,cmap='gray')
plt.show()
label(thresh_heatmap)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15767002134797195, 'fraction_numerical': 0.035681610247026534, 'mean_word_length': 8.425287356321839, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '219469', 'license': 'MIT', 'n_tokens_mistral': 1386, 'n_tokens_neox': 1313, 'n_words': 178, 'path': 'Visualization.ipynb', 'repo_name': 'olinguyen/kaggle-lung-cancer-detection', 'size': '3279'}