# Notebook from Oftatkofta/BactUnet
Path: bactnet_V4_ES_pilot.ipynb

import tensorflow as tf
from tensorflow import keras

if tf.test.gpu_device_name()=='':
    print('You do not have GPU access.')
    !nvidia-smi

else:
  print('You have GPU access')
  !nvidia-smi

# from tensorflow.python.client import device_lib
# device_lib.list_local_devices()

# print the tensorflow version
print('TensorFlow {}; Keras {}'.format(tf.__version__, keras.__version__))
tf.test.gpu_device_name()You have GPU access
Thu Apr  7 09:11:25 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 471.41       Driver Version: 471.41       CUDA Version: 11.4     |
|-------------------------------+----------------------+----------------------+
| GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  Quadro P5000       WDDM  | 00000000:91:00.0  On |                  Off |
| 26%   28C    P8     8W / 180W |  15972MiB / 16384MiB |      9%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      1584    C+G   Insufficient Permissions        N/A      |
|    0   N/A  N/A      2032    C+G   ...nnector\DesktopClient.exe    N/A      |
|    0   N/A  N/A      5224    C+G   ...kyb3d8bbwe\Calculator.exe    N/A      |
|    0   N/A  N/A      6832    C+G   ...2txyewy\TextInputHost.exe    N/A      |
|    0   N/A  N/A      7016    C+G   ...cw5n1h2txyewy\LockApp.exe    N/A      |
|    0   N/A  N/A      7572    C+G   C:\Windows\explorer.exe         N/A      |
|    0   N/A  N/A      9052    C+G   ...5n1h2txyewy\SearchApp.exe    N/A      |
|    0   N/A  N/A     10744    C+G   ...y\ShellExperienceHost.exe    N/A      |
|    0   N/A  N/A     15240    C+G   ...wekyb3d8bbwe\Video.UI.exe    N/A      |
|    0   N/A  N/A     84676      C   ...conda3\envs\tf\python.exe    N/A      |
|    0   N/A  N/A     91320    C+G   ...bbwe\Microsoft.Photos.exe    N/A      |
|    0   N/A  N/A     97020    C+G   ...zilla Firefox\firefox.exe    N/A      |
|    0   N/A  N/A    102916    C+G   ...zilla Firefox\firefox.exe    N/A      |
|    0   N/A  N/A    140856    C+G   ...ekyb3d8bbwe\YourPhone.exe    N/A      |
+-----------------------------------------------------------------------------+
TensorFlow 2.6.0; Keras 2.6.0

This network is trained as v1, but the input data is not scrutinized for empty patches. Maybe this will suppress some signal from debris._____no_output_____

from tensorflow.keras import layers
import os
import tifffile as tiff
import numpy as np
from matplotlib import pyplot as plt
from patchify import patchify
from tensorflow import keras

def patch_image(img, SIZE=288):
    #breaks up image to SIZExSIZE non-overlapping patches, returns reshaped array
    # img.shape = (2304, 2304)
    patches = patchify(img, patch_size=(SIZE, SIZE), step=(SIZE, SIZE))
    #patches.shape = (8, 8, 288, 288)

    #return.shape = (64, 288 ,288)
    return patches.reshape(patches.shape[0]*patches.shape[1], SIZE, SIZE, 1)

def patch_stack(img, SIZE=288, DEPTH=3, STRIDE=1):
    # breaks up image to SIZExSIZE non-overlapping patches, returns reshaped array
    # img.shape = (12, 2304, 2304)
    patches = patchify(img, patch_size=(DEPTH, SIZE, SIZE), step=(STRIDE, SIZE, SIZE))
    # patches.shape = (10, 8, 8, 3, 288, 288)

    # return.shape = (64, 3, 288 ,288)
    return patches.reshape(patches.shape[0] * patches.shape[1] * patches.shape[2], -1,  SIZE, SIZE)

# Normalization functions from Martin Weigert
def normalizePercentile(x, pmin=1, pmax=99.8, axis=None, clip=False, eps=1e-20, dtype=np.float32):
    """This function is adapted from Martin Weigert"""
    """Percentile-based image normalization."""

    mi = np.percentile(x, pmin, axis=axis, keepdims=True)
    ma = np.percentile(x, pmax, axis=axis, keepdims=True)
    return normalize_mi_ma(x, mi, ma, clip=clip, eps=eps, dtype=dtype)

def normalize_mi_ma(x, mi, ma, clip=False, eps=1e-20, dtype=np.float32):  # dtype=np.float32
    """This function is adapted from Martin Weigert"""
    if dtype is not None:
        x = x.astype(dtype, copy=False)
        mi = dtype(mi) if np.isscalar(mi) else mi.astype(dtype, copy=False)
        ma = dtype(ma) if np.isscalar(ma) else ma.astype(dtype, copy=False)
        eps = dtype(eps)

    try:
        import numexpr
        x = numexpr.evaluate("(x - mi) / ( ma - mi + eps )")
    except ImportError:
        x = (x - mi) / (ma - mi + eps)

    if clip:
        x = np.clip(x, 0, 1)

    return x

# Simple normalization to min/max fir the Mask
def normalizeMinMax(x, dtype=np.float32):
    x = x.astype(dtype, copy=False)
    x = (x - np.amin(x)) / (np.amax(x) - np.amin(x))
    return x
_____no_output_____def get_model_memory_usage(batch_size, model):
    import numpy as np
    try:
        from keras import backend as K
    except:
        from tensorflow.keras import backend as K

    shapes_mem_count = 0
    internal_model_mem_count = 0
    for l in model.layers:
        layer_type = l.__class__.__name__
        if layer_type == 'Model':
            internal_model_mem_count += get_model_memory_usage(batch_size, l)
        single_layer_mem = 1
        out_shape = l.output_shape
        if type(out_shape) is list:
            out_shape = out_shape[0]
        for s in out_shape:
            if s is None:
                continue
            single_layer_mem *= s
        shapes_mem_count += single_layer_mem

    trainable_count = np.sum([K.count_params(p) for p in model.trainable_weights])
    non_trainable_count = np.sum([K.count_params(p) for p in model.non_trainable_weights])

    number_size = 4.0
    if K.floatx() == 'float16':
        number_size = 2.0
    if K.floatx() == 'float64':
        number_size = 8.0

    total_memory = number_size * (batch_size * shapes_mem_count + trainable_count + non_trainable_count)
    gbytes = np.round(total_memory / (1024.0 ** 3), 3) + internal_model_mem_count
    return gbytes_____no_output_____train_path = r"Bactnet/BT0398_OGM"

batch_size = 16
SIZE = 288 #patch size
image_dataset = None
mask_dataset = None

def prepare_data(train_path, PATCH_SIZE):
    stacks = os.listdir(train_path)
    image_dataset = None
    mask_dataset = None
    for stack in stacks:
        if (stack.split(".")[-1]=="tif"):
            img = tiff.imread(os.path.join(train_path, stack))
            mask = tiff.imread(os.path.join(r"Bactnet/Training data/ES/training_target", stack))

            img = patch_stack(img, PATCH_SIZE)
            mask =patch_stack(mask, PATCH_SIZE)
            mask = mask[:, 1, :, :].reshape((mask.shape[0], 1, mask.shape[2], -1))

            #print(stack, img.shape, mask.shape)
            mask = normalizeMinMax(mask)
            img = normalizePercentile(img, 0.1, 99.9, clip=True)

            #print(stack, img.shape, mask.shape)

            if image_dataset is not None:
                image_dataset = np.concatenate((image_dataset, img))

            if mask_dataset is not None:
                mask_dataset = np.concatenate((mask_dataset, mask))

            if image_dataset is None:
                image_dataset = img

            if mask_dataset is None:
                mask_dataset = mask

            #print(image_dataset.shape, mask_dataset.shape)

    return image_dataset, mask_dataset

_____no_output_____image_dataset, mask_dataset = prepare_data(train_path, SIZE)
print(image_dataset.shape, mask_dataset.shape)

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(image_dataset, mask_dataset, test_size = 0.15, random_state = 8)

print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)(15232, 3, 288, 288) (15232, 1, 288, 288)
(12947, 3, 288, 288) (2285, 3, 288, 288) (12947, 1, 288, 288) (2285, 1, 288, 288)
#Sanity check, view few mages
import random

image_number = random.randint(0, X_test.shape[0])
plt.figure(figsize=(12, 6))
plt.subplot(231)
plt.imshow(X_test[image_number, 0, :, :])
plt.subplot(232)
plt.imshow(X_test[image_number, 1, :, :])
plt.subplot(233)
plt.imshow(X_test[image_number, 2, :, :])
plt.subplot(235)
plt.imshow(y_test[image_number, 0, :, :])
plt.show()

###############################################################_____no_output_____from keras.models import Model
from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate, Conv2DTranspose, BatchNormalization, Dropout, Lambda
from keras.layers import Activation, MaxPool2D, Concatenate
from keras.regularizers import l2

def conv_block(input, num_filters):
    t = Conv2D(num_filters, 3, padding="same",  data_format="channels_first", activation='relu', kernel_regularizer=l2())(input)
    t = BatchNormalization()(t)
    #t = Dropout(0.1)(t)
    t = Conv2D(num_filters, 3, padding="same",  data_format="channels_first", activation='relu',  kernel_regularizer=l2())(t)
    t = BatchNormalization()(t)
    #t = Dropout(0.1)(t)
    return t

#Encoder block: Conv block followed by maxpooling

def encoder_block(input, num_filters):
    x = conv_block(input, num_filters)
    p = MaxPool2D((2, 2), data_format="channels_first")(x)
    return x, p

def decoder_block(input, skip_features, num_filters):
    x = Conv2DTranspose(num_filters, (2, 2), strides=2, padding="same", data_format="channels_first")(input)
    x = Concatenate(axis=1)([x, skip_features])
    x = conv_block(x, num_filters)
    return x

#Build Unet using the blocks
def build_unet(input_shape):
    inputs = Input(input_shape)

    s1, p1 = encoder_block(inputs, 64)
    s2, p2 = encoder_block(p1, 128)
    s3, p3 = encoder_block(p2, 256)
    s4, p4 = encoder_block(p3, 512)

    b1 = conv_block(p4, 1024) #Bridge

    d1 = decoder_block(b1, s4, 512)
    d2 = decoder_block(d1, s3, 256)
    d3 = decoder_block(d2, s2, 128)
    d4 = decoder_block(d3, s1, 64)

    outputs = Conv2D(1, 1, padding="same", activation="sigmoid", data_format="channels_first")(d4)  #Binary (can be multiclass)

    model = Model(inputs, outputs, name="ES-Net_V1")
    return model_____no_output_____keras.backend.clear_session() # Free up RAM in case the model definition cells were run multiple times

#from keras_unet_collection import models
from keras_unet_collection import losses

# Build model
def hybrid_loss(y_true, y_pred):

    loss_focal = losses.focal_tversky(y_true, y_pred, alpha=0.5, gamma=4/3)
    loss_iou = losses.iou_seg(y_true, y_pred)

    # (x)
    #loss_ssim = losses.ms_ssim(y_true, y_pred, max_val=1.0, filter_size=4)

    return loss_focal+loss_iou #+loss_ssim

def dice_loss(y_true, y_pred):
    y_true = tf.cast(y_true, tf.float32)
    y_pred = tf.math.sigmoid(y_pred)
    numerator = 2 * tf.reduce_sum(y_true * y_pred)
    denominator = tf.reduce_sum(y_true + y_pred)

    return 1 - numerator / denominator

SIZE=288
input_shape = (3, SIZE, SIZE)
batch_size = 8

model = build_unet(input_shape)
model.compile(loss=dice_loss,
                  optimizer=keras.optimizers.Adam(learning_rate=1e-4), metrics=[losses.dice_coef, losses.iou_seg])
model.summary()
print(get_model_memory_usage(batch_size, model))
Model: "ES-Net_V1"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input_1 (InputLayer)            [(None, 3, 288, 288) 0
__________________________________________________________________________________________________
conv2d (Conv2D)                 (None, 64, 288, 288) 1792        input_1[0][0]
__________________________________________________________________________________________________
batch_normalization (BatchNorma (None, 64, 288, 288) 1152        conv2d[0][0]
__________________________________________________________________________________________________
conv2d_1 (Conv2D)               (None, 64, 288, 288) 36928       batch_normalization[0][0]
__________________________________________________________________________________________________
batch_normalization_1 (BatchNor (None, 64, 288, 288) 1152        conv2d_1[0][0]
__________________________________________________________________________________________________
max_pooling2d (MaxPooling2D)    (None, 64, 144, 144) 0           batch_normalization_1[0][0]
__________________________________________________________________________________________________
conv2d_2 (Conv2D)               (None, 128, 144, 144 73856       max_pooling2d[0][0]
__________________________________________________________________________________________________
batch_normalization_2 (BatchNor (None, 128, 144, 144 576         conv2d_2[0][0]
__________________________________________________________________________________________________
conv2d_3 (Conv2D)               (None, 128, 144, 144 147584      batch_normalization_2[0][0]
__________________________________________________________________________________________________
batch_normalization_3 (BatchNor (None, 128, 144, 144 576         conv2d_3[0][0]
__________________________________________________________________________________________________
max_pooling2d_1 (MaxPooling2D)  (None, 128, 72, 72)  0           batch_normalization_3[0][0]
__________________________________________________________________________________________________
conv2d_4 (Conv2D)               (None, 256, 72, 72)  295168      max_pooling2d_1[0][0]
__________________________________________________________________________________________________
batch_normalization_4 (BatchNor (None, 256, 72, 72)  288         conv2d_4[0][0]
__________________________________________________________________________________________________
conv2d_5 (Conv2D)               (None, 256, 72, 72)  590080      batch_normalization_4[0][0]
__________________________________________________________________________________________________
batch_normalization_5 (BatchNor (None, 256, 72, 72)  288         conv2d_5[0][0]
__________________________________________________________________________________________________
max_pooling2d_2 (MaxPooling2D)  (None, 256, 36, 36)  0           batch_normalization_5[0][0]
__________________________________________________________________________________________________
conv2d_6 (Conv2D)               (None, 512, 36, 36)  1180160     max_pooling2d_2[0][0]
__________________________________________________________________________________________________
batch_normalization_6 (BatchNor (None, 512, 36, 36)  144         conv2d_6[0][0]
__________________________________________________________________________________________________
conv2d_7 (Conv2D)               (None, 512, 36, 36)  2359808     batch_normalization_6[0][0]
__________________________________________________________________________________________________
batch_normalization_7 (BatchNor (None, 512, 36, 36)  144         conv2d_7[0][0]
__________________________________________________________________________________________________
max_pooling2d_3 (MaxPooling2D)  (None, 512, 18, 18)  0           batch_normalization_7[0][0]
__________________________________________________________________________________________________
conv2d_8 (Conv2D)               (None, 1024, 18, 18) 4719616     max_pooling2d_3[0][0]
__________________________________________________________________________________________________
batch_normalization_8 (BatchNor (None, 1024, 18, 18) 72          conv2d_8[0][0]
__________________________________________________________________________________________________
conv2d_9 (Conv2D)               (None, 1024, 18, 18) 9438208     batch_normalization_8[0][0]
__________________________________________________________________________________________________
batch_normalization_9 (BatchNor (None, 1024, 18, 18) 72          conv2d_9[0][0]
__________________________________________________________________________________________________
conv2d_transpose (Conv2DTranspo (None, 512, 36, 36)  2097664     batch_normalization_9[0][0]
__________________________________________________________________________________________________
concatenate (Concatenate)       (None, 1024, 36, 36) 0           conv2d_transpose[0][0]
                                                                 batch_normalization_7[0][0]
__________________________________________________________________________________________________
conv2d_10 (Conv2D)              (None, 512, 36, 36)  4719104     concatenate[0][0]
__________________________________________________________________________________________________
batch_normalization_10 (BatchNo (None, 512, 36, 36)  144         conv2d_10[0][0]
__________________________________________________________________________________________________
conv2d_11 (Conv2D)              (None, 512, 36, 36)  2359808     batch_normalization_10[0][0]
__________________________________________________________________________________________________
batch_normalization_11 (BatchNo (None, 512, 36, 36)  144         conv2d_11[0][0]
__________________________________________________________________________________________________
conv2d_transpose_1 (Conv2DTrans (None, 256, 72, 72)  524544      batch_normalization_11[0][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 512, 72, 72)  0           conv2d_transpose_1[0][0]
                                                                 batch_normalization_5[0][0]
__________________________________________________________________________________________________
conv2d_12 (Conv2D)              (None, 256, 72, 72)  1179904     concatenate_1[0][0]
__________________________________________________________________________________________________
batch_normalization_12 (BatchNo (None, 256, 72, 72)  288         conv2d_12[0][0]
__________________________________________________________________________________________________
conv2d_13 (Conv2D)              (None, 256, 72, 72)  590080      batch_normalization_12[0][0]
__________________________________________________________________________________________________
batch_normalization_13 (BatchNo (None, 256, 72, 72)  288         conv2d_13[0][0]
__________________________________________________________________________________________________
conv2d_transpose_2 (Conv2DTrans (None, 128, 144, 144 131200      batch_normalization_13[0][0]
__________________________________________________________________________________________________
concatenate_2 (Concatenate)     (None, 256, 144, 144 0           conv2d_transpose_2[0][0]
                                                                 batch_normalization_3[0][0]
__________________________________________________________________________________________________
conv2d_14 (Conv2D)              (None, 128, 144, 144 295040      concatenate_2[0][0]
__________________________________________________________________________________________________
batch_normalization_14 (BatchNo (None, 128, 144, 144 576         conv2d_14[0][0]
__________________________________________________________________________________________________
conv2d_15 (Conv2D)              (None, 128, 144, 144 147584      batch_normalization_14[0][0]
__________________________________________________________________________________________________
batch_normalization_15 (BatchNo (None, 128, 144, 144 576         conv2d_15[0][0]
__________________________________________________________________________________________________
conv2d_transpose_3 (Conv2DTrans (None, 64, 288, 288) 32832       batch_normalization_15[0][0]
__________________________________________________________________________________________________
concatenate_3 (Concatenate)     (None, 128, 288, 288 0           conv2d_transpose_3[0][0]
                                                                 batch_normalization_1[0][0]
__________________________________________________________________________________________________
conv2d_16 (Conv2D)              (None, 64, 288, 288) 73792       concatenate_3[0][0]
__________________________________________________________________________________________________
batch_normalization_16 (BatchNo (None, 64, 288, 288) 1152        conv2d_16[0][0]
__________________________________________________________________________________________________
conv2d_17 (Conv2D)              (None, 64, 288, 288) 36928       batch_normalization_16[0][0]
__________________________________________________________________________________________________
batch_normalization_17 (BatchNo (None, 64, 288, 288) 1152        conv2d_17[0][0]
__________________________________________________________________________________________________
conv2d_18 (Conv2D)              (None, 1, 288, 288)  65          batch_normalization_17[0][0]
==================================================================================================
Total params: 31,040,529
Trainable params: 31,036,137
Non-trainable params: 4,392
__________________________________________________________________________________________________
3.502
#New generator with rotation and shear where interpolation that comes with rotation and shear are thresholded in masks.
#This gives a binary mask rather than a mask with interpolated values.
seed=1337
from keras.preprocessing.image import ImageDataGenerator

img_data_gen_args = dict(rotation_range=180,
                     width_shift_range=0.3,
                     height_shift_range=0.3,
                     shear_range=0.5,
                     zoom_range=0.3,
                     horizontal_flip=True,
                     vertical_flip=True,
                     fill_mode='reflect',
                     data_format="channels_first")

mask_data_gen_args = dict(rotation_range=180,
                     width_shift_range=0.3,
                     height_shift_range=0.3,
                     shear_range=0.5,
                     zoom_range=0.3,
                     horizontal_flip=True,
                     vertical_flip=True,
                     fill_mode='reflect',
                     data_format="channels_first",
                     preprocessing_function = lambda x: np.where(x>0, 1, 0).astype(x.dtype)) #Binarize the output again.

image_data_generator = ImageDataGenerator(**img_data_gen_args)
#image_data_generator.fit(X_train, augment=True, seed=seed)

image_generator = image_data_generator.flow(X_train, seed=seed, batch_size=batch_size)
valid_img_generator = image_data_generator.flow(X_test, seed=seed, batch_size=batch_size) #Default batch size 32, if not specified here

mask_data_generator = ImageDataGenerator(**mask_data_gen_args)
#mask_data_generator.fit(y_train, augment=True, seed=seed)
mask_generator = mask_data_generator.flow(y_train, seed=seed, batch_size=batch_size)
valid_mask_generator = mask_data_generator.flow(y_test, seed=seed, batch_size=batch_size)  #Default batch size 32, if not specified here

def my_image_mask_generator(image_generator, mask_generator):
    train_generator = zip(image_generator, mask_generator)
    for (img, mask) in train_generator:
        yield (img, mask)

my_generator = my_image_mask_generator(image_generator, mask_generator)

validation_datagen = my_image_mask_generator(valid_img_generator, valid_mask_generator)

x = valid_img_generator.next()
y = valid_mask_generator.next()
print(x.shape, y.shape)
for i in range(len(x)):
    image = x[i]
    fig1 = plt.subplot(4, 4, i+1)
    fig1.set_xticks([])  #Turn off axis
    fig1.set_yticks([])
    plt.imshow(image[1, :,:], cmap='gray')
    #plt.subplot(4,4,2)
    #plt.imshow(mask[0,:,:])
plt.show()

for i in range(len(x)):
    image = y[i]
    fig1 = plt.subplot(4, 4, i+1)
    fig1.set_xticks([])  #Turn off axis
    fig1.set_yticks([])
    plt.imshow(image[0, :,:], cmap='gray')
    #plt.subplot(4,4,2)
    #plt.imshow(mask[0,:,:])
plt.show()

steps_per_epoch = 3*(len(X_train))//batch_size(8, 3, 288, 288) (8, 1, 288, 288)
from keras.callbacks import ModelCheckpoint, EarlyStopping, CSVLogger

epochs = 200
model.load_weights(r"models/bactunet_3frame_dice_noEmpty_alldata.hdf5")

#ModelCheckpoint callback saves a model at some interval.

filepath=r"models/ESnet_dice_noEmpty_v4.hdf5"
#Use Mode = max for accuracy and min for loss.
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_freq='epoch',period=10, mode='min')

#https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/EarlyStopping
early_stop = EarlyStopping(monitor='val_loss', patience=10, verbose=1)
#This callback will stop the training when there is no improvement in
# the validation loss for 10 consecutive epochs.

#CSVLogger logs epoch, acc, loss, val_acc, val_loss
log_csv = CSVLogger(r'models/ESnet_v4_logs.csv', separator=',', append=False)

callbacks_list = [checkpoint, early_stop, log_csv]

#We can now use these generators to train our model.
#Give this a name so we can call it later for plotting loss, accuracy etc. as a function of epochs.

history = model.fit(
        my_generator,
        epochs=epochs,
        validation_data=validation_datagen,
        validation_steps=steps_per_epoch,
        steps_per_epoch=steps_per_epoch,
        callbacks=callbacks_list)

model.save(r"models/ESnet_noEmpty_final_dice.hdf5") WARNING:tensorflow:`period` argument is deprecated. Please use `save_freq` to specify the frequency in number of batches seen.
Epoch 1/200
4855/4855 [==============================] - 3380s 696ms/step - loss: 77.7832 - dice_coef: 0.0296 - iou_seg: 0.9839 - val_loss: 1.0001 - val_dice_coef: 0.0093 - val_iou_seg: 0.9953
Epoch 2/200
4855/4855 [==============================] - 3381s 696ms/step - loss: 0.9916 - dice_coef: 0.0092 - iou_seg: 0.9953 - val_loss: 0.9908 - val_dice_coef: 0.0093 - val_iou_seg: 0.9953
Epoch 3/200
4855/4855 [==============================] - 3362s 692ms/step - loss: 0.9907 - dice_coef: 0.0094 - iou_seg: 0.9953 - val_loss: 0.9907 - val_dice_coef: 0.0094 - val_iou_seg: 0.9953
Epoch 4/200
4855/4855 [==============================] - 3371s 694ms/step - loss: 0.9906 - dice_coef: 0.0095 - iou_seg: 0.9952 - val_loss: 0.9905 - val_dice_coef: 0.0095 - val_iou_seg: 0.9952
Epoch 5/200
4855/4855 [==============================] - 3379s 696ms/step - loss: 0.9905 - dice_coef: 0.0096 - iou_seg: 0.9952 - val_loss: 0.9907 - val_dice_coef: 0.0093 - val_iou_seg: 0.9953
Epoch 6/200
4855/4855 [==============================] - 3386s 698ms/step - loss: 0.9905 - dice_coef: 0.0096 - iou_seg: 0.9951 - val_loss: 0.9909 - val_dice_coef: 0.0092 - val_iou_seg: 0.9954
Epoch 7/200
4855/4855 [==============================] - 3354s 691ms/step - loss: 0.9905 - dice_coef: 0.0095 - iou_seg: 0.9952 - val_loss: 0.9908 - val_dice_coef: 0.0092 - val_iou_seg: 0.9953
Epoch 8/200
4855/4855 [==============================] - 3368s 694ms/step - loss: 0.9908 - dice_coef: 0.0093 - iou_seg: 0.9953 - val_loss: 0.9907 - val_dice_coef: 0.0093 - val_iou_seg: 0.9953
Epoch 9/200
4855/4855 [==============================] - 3357s 691ms/step - loss: 0.9905 - dice_coef: 0.0095 - iou_seg: 0.9952 - val_loss: 0.9907 - val_dice_coef: 0.0093 - val_iou_seg: 0.9953
Epoch 10/200
4855/4855 [==============================] - 3362s 693ms/step - loss: 0.9905 - dice_coef: 0.0095 - iou_seg: 0.9952 - val_loss: 0.9907 - val_dice_coef: 0.0093 - val_iou_seg: 0.9953

Epoch 00010: saving model to models\ESnet_dice_noEmpty_v4.hdf5
Epoch 11/200
4855/4855 [==============================] - 3379s 696ms/step - loss: 0.9905 - dice_coef: 0.0095 - iou_seg: 0.9952 - val_loss: 0.9907 - val_dice_coef: 0.0093 - val_iou_seg: 0.9953
Epoch 12/200
4855/4855 [==============================] - 3368s 694ms/step - loss: 0.9906 - dice_coef: 0.0094 - iou_seg: 0.9952 - val_loss: 0.9907 - val_dice_coef: 0.0094 - val_iou_seg: 0.9953
Epoch 13/200
4855/4855 [==============================] - 3366s 693ms/step - loss: 0.9905 - dice_coef: 0.0095 - iou_seg: 0.9952 - val_loss: 0.9907 - val_dice_coef: 0.0093 - val_iou_seg: 0.9953
Epoch 14/200
4855/4855 [==============================] - 3385s 697ms/step - loss: 0.9905 - dice_coef: 0.0096 - iou_seg: 0.9952 - val_loss: 0.9905 - val_dice_coef: 0.0096 - val_iou_seg: 0.9952
Epoch 15/200
4855/4855 [==============================] - 3364s 693ms/step - loss: 0.9904 - dice_coef: 0.0096 - iou_seg: 0.9951 - val_loss: 0.9908 - val_dice_coef: 0.0093 - val_iou_seg: 0.9953
Epoch 16/200
4855/4855 [==============================] - 3378s 696ms/step - loss: 0.9906 - dice_coef: 0.0095 - iou_seg: 0.9952 - val_loss: 0.9906 - val_dice_coef: 0.0094 - val_iou_seg: 0.9952
Epoch 17/200
4855/4855 [==============================] - 3398s 700ms/step - loss: 0.9906 - dice_coef: 0.0094 - iou_seg: 0.9952 - val_loss: 0.9907 - val_dice_coef: 0.0094 - val_iou_seg: 0.9953
Epoch 18/200
4855/4855 [==============================] - 3377s 696ms/step - loss: 0.9907 - dice_coef: 0.0093 - iou_seg: 0.9953 - val_loss: 0.9906 - val_dice_coef: 0.0094 - val_iou_seg: 0.9952
Epoch 19/200
4855/4855 [==============================] - 3355s 691ms/step - loss: 0.9905 - dice_coef: 0.0096 - iou_seg: 0.9952 - val_loss: 0.9907 - val_dice_coef: 0.0094 - val_iou_seg: 0.9953
Epoch 20/200
4855/4855 [==============================] - 3410s 702ms/step - loss: 0.9906 - dice_coef: 0.0095 - iou_seg: 0.9952 - val_loss: 0.9907 - val_dice_coef: 0.0093 - val_iou_seg: 0.9953

Epoch 00020: saving model to models\ESnet_dice_noEmpty_v4.hdf5
Epoch 21/200
4855/4855 [==============================] - 3400s 700ms/step - loss: 0.9907 - dice_coef: 0.0094 - iou_seg: 0.9952 - val_loss: 0.9907 - val_dice_coef: 0.0094 - val_iou_seg: 0.9953
Epoch 22/200
4855/4855 [==============================] - 3382s 697ms/step - loss: 0.9905 - dice_coef: 0.0095 - iou_seg: 0.9952 - val_loss: 0.9907 - val_dice_coef: 0.0094 - val_iou_seg: 0.9953
Epoch 23/200
4855/4855 [==============================] - 3376s 695ms/step - loss: 0.9906 - dice_coef: 0.0095 - iou_seg: 0.9952 - val_loss: 0.9907 - val_dice_coef: 0.0093 - val_iou_seg: 0.9953
Epoch 24/200
4855/4855 [==============================] - 3390s 698ms/step - loss: 0.9907 - dice_coef: 0.0094 - iou_seg: 0.9953 - val_loss: 0.9907 - val_dice_coef: 0.0093 - val_iou_seg: 0.9953
Epoch 00024: early stopping
model.save(r"models/ESnet_noEmpty_final_dice.hdf5")
#model.load_weights(r"models/bactunet_3frame_local_dice.hdf5")_____no_output_____#Continue training

filepath=r"models/ESnet_dice_noEmpty_v2.hdf5"
#Use Mode = max for accuracy and min for loss.
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')

#https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/EarlyStopping
early_stop = EarlyStopping(monitor='val_loss', patience=10, verbose=1)
#This callback will stop the training when there is no improvement in
# the validation loss for 10 consecutive epochs.

#CSVLogger logs epoch, acc, loss, val_acc, val_loss
log_csv = CSVLogger(r'models/ESnet_v2_logs.csv', separator=',', append=False)

callbacks_list = [checkpoint, early_stop, log_csv]

#We can now use these generators to train our model.
#Give this a name so we can call it later for plotting loss, accuracy etc. as a function of epochs.

history = model.fit(
        my_generator,
        steps_per_epoch=steps_per_epoch,
        epochs=1000,
        validation_data=validation_datagen,
        validation_steps=steps_per_epoch,
        callbacks=callbacks_list)

model.save(r"models/ESnet_noEmpty_final_alldata.hdf5") Epoch 1/1000
4855/4855 [==============================] - 3719s 766ms/step - loss: 0.4376 - dice_coef: 939511.1250 - iou_seg: 1.0000 - val_loss: 0.4327 - val_dice_coef: 177518896.0000 - val_iou_seg: 1.0000

Epoch 00001: val_loss improved from inf to 0.43269, saving model to models\ESnet_dice_noEmpty_v1.hdf5
Epoch 2/1000
4855/4855 [==============================] - 3679s 758ms/step - loss: nan - dice_coef: nan - iou_seg: nan - val_loss: nan - val_dice_coef: nan - val_iou_seg: nan

Epoch 00002: val_loss did not improve from 0.43269
Epoch 3/1000
4855/4855 [==============================] - 3680s 758ms/step - loss: nan - dice_coef: nan - iou_seg: nan - val_loss: nan - val_dice_coef: nan - val_iou_seg: nan

Epoch 00003: val_loss did not improve from 0.43269
Epoch 4/1000
4855/4855 [==============================] - 3678s 758ms/step - loss: nan - dice_coef: nan - iou_seg: nan - val_loss: nan - val_dice_coef: nan - val_iou_seg: nan

Epoch 00004: val_loss did not improve from 0.43269
Epoch 5/1000
4855/4855 [==============================] - 3676s 757ms/step - loss: nan - dice_coef: nan - iou_seg: nan - val_loss: nan - val_dice_coef: nan - val_iou_seg: nan

Epoch 00005: val_loss did not improve from 0.43269
Epoch 6/1000
4855/4855 [==============================] - 3676s 757ms/step - loss: nan - dice_coef: nan - iou_seg: nan - val_loss: nan - val_dice_coef: nan - val_iou_seg: nan

Epoch 00006: val_loss did not improve from 0.43269
Epoch 7/1000
4855/4855 [==============================] - 3677s 757ms/step - loss: nan - dice_coef: nan - iou_seg: nan - val_loss: nan - val_dice_coef: nan - val_iou_seg: nan

Epoch 00007: val_loss did not improve from 0.43269
Epoch 8/1000
4855/4855 [==============================] - 3677s 757ms/step - loss: nan - dice_coef: nan - iou_seg: nan - val_loss: nan - val_dice_coef: nan - val_iou_seg: nan

Epoch 00008: val_loss did not improve from 0.43269
Epoch 9/1000
4855/4855 [==============================] - 3676s 757ms/step - loss: nan - dice_coef: nan - iou_seg: nan - val_loss: nan - val_dice_coef: nan - val_iou_seg: nan

Epoch 00009: val_loss did not improve from 0.43269
Epoch 10/1000
4855/4855 [==============================] - 3676s 757ms/step - loss: nan - dice_coef: nan - iou_seg: nan - val_loss: nan - val_dice_coef: nan - val_iou_seg: nan

Epoch 00010: val_loss did not improve from 0.43269
Epoch 11/1000
4855/4855 [==============================] - 3675s 757ms/step - loss: nan - dice_coef: nan - iou_seg: nan - val_loss: nan - val_dice_coef: nan - val_iou_seg: nan

Epoch 00011: val_loss did not improve from 0.43269
Epoch 00011: early stopping
# ##################################
# #IOU
y_pred = model.predict(X_test)
IOUs = []
dices = []
thresh = []

for threshold in range(0, 11):
  threshold = threshold/10
  y_pred_thresholded = y_pred > threshold
  intersection = np.logical_and(y_test, y_pred_thresholded)
  union = np.logical_or(y_test, y_pred_thresholded)
  iou_score = np.sum(intersection) / np.sum(union)
  dice_c = 0#losses.dice_coef(y_test, y_pred)
  print("IoU socre is: ", round(iou_score, 4), "Dice coeff is: ", round(dice_c, 4),"at threshold: ", threshold)
  IOUs.append(iou_score)
  thresh.append(threshold)
  dices.append(dice_c)

#plot IOUs vs threshold
plt.plot(thresh, IOUs, 'r', label='IOU')
plt.plot(thresh, dices, 'b', label='Dice coeff')
plt.title('IOU & dice vs threshold')
plt.xlabel('Threshold')
plt.ylabel('IOU')
plt.show()IoU socre is:  0.0 Dice coeff is:  0 at threshold:  0.0
IoU socre is:  0.0 Dice coeff is:  0 at threshold:  0.1
IoU socre is:  0.0 Dice coeff is:  0 at threshold:  0.2
IoU socre is:  0.0 Dice coeff is:  0 at threshold:  0.3
IoU socre is:  0.0 Dice coeff is:  0 at threshold:  0.4
IoU socre is:  0.0 Dice coeff is:  0 at threshold:  0.5
IoU socre is:  0.0 Dice coeff is:  0 at threshold:  0.6
IoU socre is:  0.0 Dice coeff is:  0 at threshold:  0.7
IoU socre is:  0.0 Dice coeff is:  0 at threshold:  0.8
IoU socre is:  0.0 Dice coeff is:  0 at threshold:  0.9
IoU socre is:  0.0 Dice coeff is:  0 at threshold:  1.0
#######################################################################
#Predict on a few images
#model = get_model()
#model.load_weights('mitochondria_50_plus_100_epochs.hdf5') #Trained for 50 epochs and then additional 100
#model.load_weights('mitochondria_gpu_tf1.4.hdf5')  #Trained for 50 epochs¨
#import pickle
#with open("/content/gdrive/MyDrive/Colab Notebooks/BT0403/model_folder/bactunet_3frame_25ep_history") as fh:
#    history = pickle.load(fh)

idx=random.randint(0, (len(X_test)))

plt.figure(figsize=(16, 8))
plt.subplot(131)
plt.title('Testing Image')
plt.imshow(X_test[idx, 1, :, : ])
plt.subplot(132)
plt.title('Testing Label')
plt.imshow(y_test[idx, 0, : , :])
plt.subplot(133)
plt.title('Prediction on test image')
plt.imshow(y_pred[idx, 0, :, :])

plt.show()_____no_output_____loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(loss) + 1)
plt.plot(epochs, loss, 'y', label='Training loss')
plt.plot(epochs, val_loss, 'r', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()

acc = history.history['dice_coef']
#acc = history.history['accuracy']
val_acc = history.history['val_dice_coef']
#val_acc = history.history['val_accuracy']

plt.plot(epochs, acc, 'y', label='Training acc')
plt.plot(epochs, val_acc, 'r', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()_____no_output_____import math
from patchify import unpatchify

#######
#THERE IS A BUG HERE!!! FIX PREDICTION AND UNPATCHING

def _createOutArr(shape, nrows, ncols, nchannels):
    out_height = int(nrows * shape[-2])
    out_width = int(ncols * shape[-1])
    out_frames = int(shape[0] / (nrows * ncols))
    outshape = (out_frames, nchannels, out_height, out_width)
    out_arr = np.empty(outshape, dtype=np.float32)

    return out_arr

def unpatcher(arr, nrows, ncols, nchannels=1):
    out_arr = _createOutArr(arr.shape, nrows, ncols, nchannels)
    patch_h = arr.shape[-2]
    patch_w = arr.shape[-1]
    n = 0
    for frame in range(out_arr.shape[0]):
        for i in range(nrows):
            for j in range(ncols):
                y = patch_h * i
                x = patch_w * j
                out_arr[frame, :, y:y+patch_h, x:x+patch_w] = arr[n]
                n += 1

    return out_arr

#load unseen data

validation_image_directory = r"C:\Users\analyst\Documents\Python Scripts\BactUnet\Bactnet\Training data\stacks\predict"
result_folder = r"C:\Users\analyst\Documents\Python Scripts\BactUnet\_results"

val_image_dataset = []
val_mask_dataset = []
pred_mask_dataset = []

images = os.listdir(validation_image_directory)

for i, image_name in enumerate(images):    #Remember enumerate method adds a counter and returns the enumerate object
    if (image_name.split('.')[1] == 'tif'):

        image = tiff.imread(os.path.join(validation_image_directory, image_name))
        original_shape = image.shape
        patch = patch_stack(image, SIZE)

        patch = normalizePercentile(patch, 0.1, 99.9, clip=True)
        pred_mask_patch = model.predict(patch)
        print(image_name, original_shape, patch.shape, pred_mask_patch.shape)
        #pred_mask_patch = pred_mask_patch[:, 0, :,:]
        image = np.expand_dims(patch[:, 1, :,:], axis=1)
        patch = np.concatenate((image, pred_mask_patch), axis=1)
        unpatched = unpatcher(patch, 8, 8, 2)
        print(patch.shape)
        tiff.imwrite(os.path.join(result_folder, image_name), unpatched, imagej=True, resolution=(1./2.6755, 1./2.6755),
                      metadata={'unit': 'um', 'finterval': 15,
                                'axes': 'TCYX'})

        #pred_mask = unpatch_stack(pred_mask_patch, original_shape)
        #tiff.imsave(os.path.join(result_folder, image_name), pred_mask_patch)
        #val_image_dataset.append(image)
        #pred_mask_dataset.append(pred_mask)BT0398_210.tif (11, 2304, 2304) (576, 3, 288, 288) (576, 1, 288, 288)
(576, 2, 288, 288)
BT403_013.tif (12, 2304, 2304) (640, 3, 288, 288) (640, 1, 288, 288)
(640, 2, 288, 288)
BT403_216.tif (12, 2304, 2304) (640, 3, 288, 288) (640, 1, 288, 288)
(640, 2, 288, 288)
BT404_001.tif (12, 2304, 2304) (640, 3, 288, 288) (640, 1, 288, 288)
(640, 2, 288, 288)
#Let's try the full movies

image_directory = r"C:\Users\analyst\Documents\Python Scripts\BactUnet\Bactnet"
result_folder = r"C:\Users\analyst\Documents\Python Scripts\BactUnet\_results"
filelist = []

for dir in os.listdir(image_directory):
    for file in os.listdir(os.path.join(image_directory, dir)):
        if ".tif" in file:
            loadme = os.path.join(image_directory, dir, file)
            image = tiff.imread(loadme)
            original_shape = image.shape
            patch = patch_stack(image, SIZE)
            patchlist = np.split(patch, 238)

            for i, p in enumerate(patchlist):
                p = normalizePercentile(p, 0.1, 99.9, clip=True)
                pred_mask_patch = model.predict(p)
                unpatched = unpatcher(pred_mask_patch, 8, 8, 1)
                #unpatched = unpatched.astype('uint8')
                savename = file.split(".")[0]+ "_V3_" + str(i+1) + ".tif"
                print(savename, unpatched.shape)
                tiff.imwrite(os.path.join(result_folder, savename), unpatched, imagej=True, resolution=(1./2.6755, 1./2.6755),
                             metadata={'unit': 'um', 'finterval': 15, 'axes': 'TCYX'})
BT0398_OGM_V3_1.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_2.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_3.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_4.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_5.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_6.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_7.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_8.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_9.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_10.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_11.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_12.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_13.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_14.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_15.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_16.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_17.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_18.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_19.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_20.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_21.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_22.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_23.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_24.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_25.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_26.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_27.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_28.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_29.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_30.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_31.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_32.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_33.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_34.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_35.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_36.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_37.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_38.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_39.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_40.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_41.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_42.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_43.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_44.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_45.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_46.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_47.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_48.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_49.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_50.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_51.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_52.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_53.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_54.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_55.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_56.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_57.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_58.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_59.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_60.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_61.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_62.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_63.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_64.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_65.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_66.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_67.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_68.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_69.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_70.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_71.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_72.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_73.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_74.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_75.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_76.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_77.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_78.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_79.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_80.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_81.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_82.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_83.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_84.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_85.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_86.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_87.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_88.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_89.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_90.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_91.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_92.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_93.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_94.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_95.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_96.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_97.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_98.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_99.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_100.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_101.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_102.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_103.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_104.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_105.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_106.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_107.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_108.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_109.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_110.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_111.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_112.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_113.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_114.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_115.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_116.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_117.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_118.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_119.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_120.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_121.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_122.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_123.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_124.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_125.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_126.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_127.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_128.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_129.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_130.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_131.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_132.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_133.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_134.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_135.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_136.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_137.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_138.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_139.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_140.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_141.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_142.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_143.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_144.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_145.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_146.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_147.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_148.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_149.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_150.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_151.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_152.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_153.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_154.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_155.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_156.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_157.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_158.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_159.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_160.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_161.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_162.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_163.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_164.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_165.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_166.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_167.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_168.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_169.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_170.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_171.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_172.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_173.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_174.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_175.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_176.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_177.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_178.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_179.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_180.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_181.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_182.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_183.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_184.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_185.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_186.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_187.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_188.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_189.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_190.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_191.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_192.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_193.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_194.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_195.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_196.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_197.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_198.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_199.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_200.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_201.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_202.tif (1, 1, 2304, 2304)
BT0398_OGM_V3_203.tif (1, 1, 2304, 2304)

loadme = os.path.join(image_directory, fname)
image = tiff.imread(loadme)
original_shape = image.shape
patch = patch_stack(image, SIZE)
print(original_shape, patch.shape)

#split into smaller predictions
patchlist = np.split(patch, 238)
print(patchlist[0].shape)
for i, p in enumerate(patchlist):
    p = normalizePercentile(p, 0.1, 99.9, clip=True)
    pred_mask_patch = model.predict(p)
    unpatched = unpatcher(pred_mask_patch, 8, 8, 1)
    #unpatched = unpatched.astype('uint8')
    savename = "BT404_" + str(i+1) + ".tif"
    print(savename, unpatched.shape)
    tiff.imwrite(os.path.join(result_folder, savename), unpatched, imagej=True, resolution=(1./2.6755, 1./2.6755),
                 metadata={'unit': 'um', 'finterval': 15, 'axes': 'TCYX'})(240, 2304, 2304) (15232, 3, 288, 288)
(64, 3, 288, 288)
BT404_1.tif (1, 1, 2304, 2304)
BT404_2.tif (1, 1, 2304, 2304)
BT404_3.tif (1, 1, 2304, 2304)
BT404_4.tif (1, 1, 2304, 2304)
BT404_5.tif (1, 1, 2304, 2304)
BT404_6.tif (1, 1, 2304, 2304)
BT404_7.tif (1, 1, 2304, 2304)
BT404_8.tif (1, 1, 2304, 2304)
BT404_9.tif (1, 1, 2304, 2304)
BT404_10.tif (1, 1, 2304, 2304)
BT404_11.tif (1, 1, 2304, 2304)
BT404_12.tif (1, 1, 2304, 2304)
BT404_13.tif (1, 1, 2304, 2304)
BT404_14.tif (1, 1, 2304, 2304)
BT404_15.tif (1, 1, 2304, 2304)
BT404_16.tif (1, 1, 2304, 2304)
BT404_17.tif (1, 1, 2304, 2304)
BT404_18.tif (1, 1, 2304, 2304)
BT404_19.tif (1, 1, 2304, 2304)
BT404_20.tif (1, 1, 2304, 2304)
BT404_21.tif (1, 1, 2304, 2304)
BT404_22.tif (1, 1, 2304, 2304)
BT404_23.tif (1, 1, 2304, 2304)
BT404_24.tif (1, 1, 2304, 2304)
BT404_25.tif (1, 1, 2304, 2304)
BT404_26.tif (1, 1, 2304, 2304)
BT404_27.tif (1, 1, 2304, 2304)
BT404_28.tif (1, 1, 2304, 2304)
BT404_29.tif (1, 1, 2304, 2304)
BT404_30.tif (1, 1, 2304, 2304)
BT404_31.tif (1, 1, 2304, 2304)
BT404_32.tif (1, 1, 2304, 2304)
BT404_33.tif (1, 1, 2304, 2304)
BT404_34.tif (1, 1, 2304, 2304)
BT404_35.tif (1, 1, 2304, 2304)
BT404_36.tif (1, 1, 2304, 2304)
BT404_37.tif (1, 1, 2304, 2304)
BT404_38.tif (1, 1, 2304, 2304)
BT404_39.tif (1, 1, 2304, 2304)
BT404_40.tif (1, 1, 2304, 2304)
BT404_41.tif (1, 1, 2304, 2304)
BT404_42.tif (1, 1, 2304, 2304)
BT404_43.tif (1, 1, 2304, 2304)
BT404_44.tif (1, 1, 2304, 2304)
BT404_45.tif (1, 1, 2304, 2304)
BT404_46.tif (1, 1, 2304, 2304)
BT404_47.tif (1, 1, 2304, 2304)
BT404_48.tif (1, 1, 2304, 2304)
BT404_49.tif (1, 1, 2304, 2304)
BT404_50.tif (1, 1, 2304, 2304)
BT404_51.tif (1, 1, 2304, 2304)
BT404_52.tif (1, 1, 2304, 2304)
BT404_53.tif (1, 1, 2304, 2304)
BT404_54.tif (1, 1, 2304, 2304)
BT404_55.tif (1, 1, 2304, 2304)
BT404_56.tif (1, 1, 2304, 2304)
BT404_57.tif (1, 1, 2304, 2304)
BT404_58.tif (1, 1, 2304, 2304)
BT404_59.tif (1, 1, 2304, 2304)
BT404_60.tif (1, 1, 2304, 2304)
BT404_61.tif (1, 1, 2304, 2304)
BT404_62.tif (1, 1, 2304, 2304)
BT404_63.tif (1, 1, 2304, 2304)
BT404_64.tif (1, 1, 2304, 2304)
BT404_65.tif (1, 1, 2304, 2304)
BT404_66.tif (1, 1, 2304, 2304)
BT404_67.tif (1, 1, 2304, 2304)
BT404_68.tif (1, 1, 2304, 2304)
BT404_69.tif (1, 1, 2304, 2304)
BT404_70.tif (1, 1, 2304, 2304)
BT404_71.tif (1, 1, 2304, 2304)
BT404_72.tif (1, 1, 2304, 2304)
BT404_73.tif (1, 1, 2304, 2304)
BT404_74.tif (1, 1, 2304, 2304)
BT404_75.tif (1, 1, 2304, 2304)
BT404_76.tif (1, 1, 2304, 2304)
BT404_77.tif (1, 1, 2304, 2304)
BT404_78.tif (1, 1, 2304, 2304)
BT404_79.tif (1, 1, 2304, 2304)
BT404_80.tif (1, 1, 2304, 2304)
BT404_81.tif (1, 1, 2304, 2304)
BT404_82.tif (1, 1, 2304, 2304)
BT404_83.tif (1, 1, 2304, 2304)
BT404_84.tif (1, 1, 2304, 2304)
BT404_85.tif (1, 1, 2304, 2304)
BT404_86.tif (1, 1, 2304, 2304)
BT404_87.tif (1, 1, 2304, 2304)
BT404_88.tif (1, 1, 2304, 2304)
BT404_89.tif (1, 1, 2304, 2304)
BT404_90.tif (1, 1, 2304, 2304)
BT404_91.tif (1, 1, 2304, 2304)
BT404_92.tif (1, 1, 2304, 2304)
BT404_93.tif (1, 1, 2304, 2304)
BT404_94.tif (1, 1, 2304, 2304)
BT404_95.tif (1, 1, 2304, 2304)
BT404_96.tif (1, 1, 2304, 2304)
BT404_97.tif (1, 1, 2304, 2304)
BT404_98.tif (1, 1, 2304, 2304)
BT404_99.tif (1, 1, 2304, 2304)
BT404_100.tif (1, 1, 2304, 2304)
BT404_101.tif (1, 1, 2304, 2304)
BT404_102.tif (1, 1, 2304, 2304)
BT404_103.tif (1, 1, 2304, 2304)
BT404_104.tif (1, 1, 2304, 2304)
BT404_105.tif (1, 1, 2304, 2304)
BT404_106.tif (1, 1, 2304, 2304)
BT404_107.tif (1, 1, 2304, 2304)
BT404_108.tif (1, 1, 2304, 2304)
BT404_109.tif (1, 1, 2304, 2304)
BT404_110.tif (1, 1, 2304, 2304)
BT404_111.tif (1, 1, 2304, 2304)
BT404_112.tif (1, 1, 2304, 2304)
BT404_113.tif (1, 1, 2304, 2304)
BT404_114.tif (1, 1, 2304, 2304)
BT404_115.tif (1, 1, 2304, 2304)
BT404_116.tif (1, 1, 2304, 2304)
BT404_117.tif (1, 1, 2304, 2304)
BT404_118.tif (1, 1, 2304, 2304)
BT404_119.tif (1, 1, 2304, 2304)
BT404_120.tif (1, 1, 2304, 2304)
BT404_121.tif (1, 1, 2304, 2304)
BT404_122.tif (1, 1, 2304, 2304)
BT404_123.tif (1, 1, 2304, 2304)
BT404_124.tif (1, 1, 2304, 2304)
BT404_125.tif (1, 1, 2304, 2304)
BT404_126.tif (1, 1, 2304, 2304)
BT404_127.tif (1, 1, 2304, 2304)
BT404_128.tif (1, 1, 2304, 2304)
BT404_129.tif (1, 1, 2304, 2304)
BT404_130.tif (1, 1, 2304, 2304)
BT404_131.tif (1, 1, 2304, 2304)
BT404_132.tif (1, 1, 2304, 2304)
BT404_133.tif (1, 1, 2304, 2304)
BT404_134.tif (1, 1, 2304, 2304)
BT404_135.tif (1, 1, 2304, 2304)
BT404_136.tif (1, 1, 2304, 2304)
BT404_137.tif (1, 1, 2304, 2304)
BT404_138.tif (1, 1, 2304, 2304)
BT404_139.tif (1, 1, 2304, 2304)
BT404_140.tif (1, 1, 2304, 2304)
BT404_141.tif (1, 1, 2304, 2304)
BT404_142.tif (1, 1, 2304, 2304)
BT404_143.tif (1, 1, 2304, 2304)
BT404_144.tif (1, 1, 2304, 2304)
BT404_145.tif (1, 1, 2304, 2304)
BT404_146.tif (1, 1, 2304, 2304)
BT404_147.tif (1, 1, 2304, 2304)
BT404_148.tif (1, 1, 2304, 2304)
BT404_149.tif (1, 1, 2304, 2304)
BT404_150.tif (1, 1, 2304, 2304)
BT404_151.tif (1, 1, 2304, 2304)
BT404_152.tif (1, 1, 2304, 2304)
BT404_153.tif (1, 1, 2304, 2304)
BT404_154.tif (1, 1, 2304, 2304)
BT404_155.tif (1, 1, 2304, 2304)
BT404_156.tif (1, 1, 2304, 2304)
BT404_157.tif (1, 1, 2304, 2304)
BT404_158.tif (1, 1, 2304, 2304)
BT404_159.tif (1, 1, 2304, 2304)
BT404_160.tif (1, 1, 2304, 2304)
BT404_161.tif (1, 1, 2304, 2304)
BT404_162.tif (1, 1, 2304, 2304)
BT404_163.tif (1, 1, 2304, 2304)
BT404_164.tif (1, 1, 2304, 2304)
BT404_165.tif (1, 1, 2304, 2304)
BT404_166.tif (1, 1, 2304, 2304)
BT404_167.tif (1, 1, 2304, 2304)
BT404_168.tif (1, 1, 2304, 2304)
BT404_169.tif (1, 1, 2304, 2304)
BT404_170.tif (1, 1, 2304, 2304)
BT404_171.tif (1, 1, 2304, 2304)
BT404_172.tif (1, 1, 2304, 2304)
BT404_173.tif (1, 1, 2304, 2304)
BT404_174.tif (1, 1, 2304, 2304)
BT404_175.tif (1, 1, 2304, 2304)
BT404_176.tif (1, 1, 2304, 2304)
BT404_177.tif (1, 1, 2304, 2304)
BT404_178.tif (1, 1, 2304, 2304)
BT404_179.tif (1, 1, 2304, 2304)
BT404_180.tif (1, 1, 2304, 2304)
BT404_181.tif (1, 1, 2304, 2304)
BT404_182.tif (1, 1, 2304, 2304)
BT404_183.tif (1, 1, 2304, 2304)
BT404_184.tif (1, 1, 2304, 2304)
BT404_185.tif (1, 1, 2304, 2304)
BT404_186.tif (1, 1, 2304, 2304)
BT404_187.tif (1, 1, 2304, 2304)
BT404_188.tif (1, 1, 2304, 2304)
BT404_189.tif (1, 1, 2304, 2304)
BT404_190.tif (1, 1, 2304, 2304)
BT404_191.tif (1, 1, 2304, 2304)
BT404_192.tif (1, 1, 2304, 2304)
BT404_193.tif (1, 1, 2304, 2304)
BT404_194.tif (1, 1, 2304, 2304)
BT404_195.tif (1, 1, 2304, 2304)
BT404_196.tif (1, 1, 2304, 2304)
BT404_197.tif (1, 1, 2304, 2304)
BT404_198.tif (1, 1, 2304, 2304)
BT404_199.tif (1, 1, 2304, 2304)
BT404_200.tif (1, 1, 2304, 2304)
BT404_201.tif (1, 1, 2304, 2304)
BT404_202.tif (1, 1, 2304, 2304)
BT404_203.tif (1, 1, 2304, 2304)
BT404_204.tif (1, 1, 2304, 2304)
BT404_205.tif (1, 1, 2304, 2304)
BT404_206.tif (1, 1, 2304, 2304)
BT404_207.tif (1, 1, 2304, 2304)
BT404_208.tif (1, 1, 2304, 2304)
BT404_209.tif (1, 1, 2304, 2304)
BT404_210.tif (1, 1, 2304, 2304)
BT404_211.tif (1, 1, 2304, 2304)
BT404_212.tif (1, 1, 2304, 2304)
BT404_213.tif (1, 1, 2304, 2304)
BT404_214.tif (1, 1, 2304, 2304)
BT404_215.tif (1, 1, 2304, 2304)
BT404_216.tif (1, 1, 2304, 2304)
BT404_217.tif (1, 1, 2304, 2304)
BT404_218.tif (1, 1, 2304, 2304)
BT404_219.tif (1, 1, 2304, 2304)
BT404_220.tif (1, 1, 2304, 2304)
BT404_221.tif (1, 1, 2304, 2304)
BT404_222.tif (1, 1, 2304, 2304)
BT404_223.tif (1, 1, 2304, 2304)
BT404_224.tif (1, 1, 2304, 2304)
BT404_225.tif (1, 1, 2304, 2304)
BT404_226.tif (1, 1, 2304, 2304)
BT404_227.tif (1, 1, 2304, 2304)
BT404_228.tif (1, 1, 2304, 2304)
BT404_229.tif (1, 1, 2304, 2304)
BT404_230.tif (1, 1, 2304, 2304)
BT404_231.tif (1, 1, 2304, 2304)
BT404_232.tif (1, 1, 2304, 2304)
BT404_233.tif (1, 1, 2304, 2304)
BT404_234.tif (1, 1, 2304, 2304)
BT404_235.tif (1, 1, 2304, 2304)
BT404_236.tif (1, 1, 2304, 2304)
BT404_237.tif (1, 1, 2304, 2304)
BT404_238.tif (1, 1, 2304, 2304)
for i, layer in enumerate(model.layers):
  if "onv" in layer.name:
    print(i, layer.name, model.layers[i].get_weights()[0].shape)

weights, biases =  model.layers[4].get_weights()
fig1=plt.figure(figsize=(12, 12))

columns = 8
rows = 8
n_filters = columns*rows
for i in range(1, n_filters +1):
    f = weights[:, :, (i-1), 0]
    fig1 =plt.subplot(rows, columns, i)
    fig1.set_xticks([])  #Turn off axis
    fig1.set_yticks([])

    #plt.imshow(f[i%3, :, :], cmap='gray')
    plt.imshow(f[:, :], cmap='gray')
    #plt.imshow(f[2, :, :], cmap='gray') #Show only the filters from 0th channel (R)
    #ix += 1
plt.show() 1 conv2d (3, 3, 3, 64)
4 conv2d_1 (3, 3, 64, 64)
8 conv2d_2 (3, 3, 64, 128)
11 conv2d_3 (3, 3, 128, 128)
15 conv2d_4 (3, 3, 128, 256)
18 conv2d_5 (3, 3, 256, 256)
22 conv2d_6 (3, 3, 256, 512)
25 conv2d_7 (3, 3, 512, 512)
29 conv2d_8 (3, 3, 512, 1024)
32 conv2d_9 (3, 3, 1024, 1024)
35 conv2d_transpose (2, 2, 512, 1024)
37 conv2d_10 (3, 3, 1024, 512)
40 conv2d_11 (3, 3, 512, 512)
43 conv2d_transpose_1 (2, 2, 256, 512)
45 conv2d_12 (3, 3, 512, 256)
48 conv2d_13 (3, 3, 256, 256)
51 conv2d_transpose_2 (2, 2, 128, 256)
53 conv2d_14 (3, 3, 256, 128)
56 conv2d_15 (3, 3, 128, 128)
59 conv2d_transpose_3 (2, 2, 64, 128)
61 conv2d_16 (3, 3, 128, 64)
64 conv2d_17 (3, 3, 64, 64)
67 conv2d_18 (1, 1, 64, 1)


# Notebook from robraddi/tu_chem
Path: CHEM3405_Physical_Chemistry_Bio/Labs/Chemical_Kinetics_II.ipynb

#### 14.

Computer Lab – Chemical Kinetics 2

Goal: The purpose of this lab is to compare two- and three-state models of protein folding kinetics using the steady-state approximation

Introduction

This exercise is based on the following paper:

Teilum, K., Maki, K., Kragelund, B. B., Poulsen, F. M., & Roder, H. (2002). Early kinetic intermediate in the folding of acyl-CoA binding protein detected by fluorescence labeling and ultrarapid mixing. Proceedings of the National Academy of Sciences of the United States of America, 99(15), 9807–9812. doi:10.1073/pnas.152321499

In this work, the folding kinetics of an 86-residue four helix-bundle protein called ACBP (acyl-CoA binding protein) was measured using time-resolved fluorescence spectroscopy (monitoring UV tryptophan fluorescence at 280 nm), after fast dilution from chemical denaturant (guanidinium hydrochloride, GuHCl) in a capillary mixer. The authors find that their data can be fit to the following kinetic model,

$$ U \underset{k_{IU}}{\stackrel{k_{UI}}{\rightleftharpoons}} I \underset{k_{NI}}{\stackrel{k_{IN}}{\rightleftharpoons}} N \tag{1}$$

where U is a highly fluorescent unfolded state, I is a partially folded intermediate, and N is the folded state.

Procedure

Modeling the three-state mechanism

Use SimBiology to construct a model of the three-state folding kinetics, using the following kinetic rate parameters determined by the authors at 299 K:

$$ U \underset{k_{IU}= 7000 s^{-1}}{\stackrel{k_{UI} =  11000 s^{-1}}{\rightleftharpoons}} I \underset{k_{NI} =  0.07 s^{-1}}{\stackrel{k_{IN} =  650 s^{-1}}{\rightleftharpoons}} N $$

Here is some experimental data that might be helpful in validating your model

Time (ms)

Fluorescence intensity for the U state

High concentration = more intensity

0.2
1.1

0.4
1.0

0.6
0.98

0.8
0.96

1
0.91

2
0.6

20
0.33

40
0.32

60
0.31

80
0.29

_____no_output_____
Using initial concentrations of [U] = 10.0, [I] = 0.0, and [N] = 10.0, visualize the concentrations of each species over time. Draw or print/plot the results below.

Using the results of your SimBiology model, estimate the populations of U and N at equilibrium.

Estimate from this the equilibrium constant K = [Nequilibrium]/[Uequilibrium] and the free energy of folding.

Modeling the two-state mechanism

Let’s now apply the steady-state approximation to this three-state model. This approximation is a simplification saying that the concentration of the intermediate [I] is constant throughout the reaction. Since the formation of the intermediate is fast compared to the slower, rate-limiting step of folding to the native state N, the steady-state approximation is still fairly accurate.

To find the steady-state approximation, we go through the following steps:

1. Write down an expression for d[I]/dt :

$$\frac{d[I]}{dt} = -k_{IU}[I] - k_{IN}[I] + k_{UI}[U] + k_{NI}[I]\tag{1.1}$$

2. Solve $\frac{d[I]}{dt} = 0$ (i.e. steady-state conditions) to get $[I]$ as a function of $[U]$ and $[N]$ :

\begin{align}
0 &= -k_{IU}[I] - k_{IN}[I] + k_{UI}[U] + k_{NI}[N]\\
[I](k_{IU} + k_{IN}) &= k_{UI}[U] + k_{NI}[N]\\
[I] &= (k_{UI}[U] + k_{NI}[N])/(k_{IU} + k_{IN})
\end{align}

3. Next, we plug this expression for $[I]$ into the rate laws for $[U]$ and $[N]$:

\begin{align}
\frac{d[U]}{dt} &= -k_{UI}[U] + k_{IU}[I]\\
&= -k_{UI}[U] + k_{IU} (k_{UI}[U] + k_{NI}[N])/(k_{IU} + k_{IN}) \tag{1.2}\\
\frac{d[N]}{dt} &= -k_{NI}[N] + k_{IN}[I]\\
&= -k_{NI}[N] + k_{IN} (k_{UI}[U] + k_{NI}[N])/(k_{IU} + k_{IN}) \tag{1.3}
\end{align}

4. Finally, to get rates for a two-state $U\rightleftharpoons N$ model of folding, consider that at equilibrium, $\frac{d[U]}{dt} = \frac{d[N]}{dt} = 0$. Set the two expressions for $\frac{d[U]}{dt}$ and $\frac{d[N]}{dt}$ above to be equal to each other, and rearrange the equation to get it in the following form:

$$k_{forward} [U] = k_{backward}[N]$$

What are your computed values of for $k_{forward}$ and $k_{backward}$? (Hint: They should be close to the values of $k_{IN}$ and $k_{NI}$, since $I \to N$ is the rate-limiting step.)

Estimate from $k_{forward}$ and $k_{backward}$ the equilibrium constant ($K = k_{f}/k_{b}$) and free energy of folding.

_____no_output_____

import numpy as np
import pandas as pd_____no_output_____def ODE_eqs(C, t, k):
    r"""See eqs 1.1, 1.2 and 1.3
    :math:`U \underset{k_{IU}}{\stackrel{k_{UI}}{\rightleftharpoons}} \
    I \underset{k_{NI}}{\stackrel{k_{IN}}{\rightleftharpoons}} N`

    Args:
        C(list): collection of concentrations for each species [L, R, LR, ...]
        t(float): iterate over some time-course
        k(list): collection of rate constants [kf1, kb1, kf2, ...]

        k𝑈𝐼=11000𝑠−1, k𝐼𝑁=650𝑠−1
        k𝐼𝑈=7000𝑠−1, k𝑁𝐼=0.07𝑠−1
    """

    U, I, N = C[0], C[1], C[2]
    kUI, kIU, kIN, kNI = k[0], k[1], k[2], k[3]
    dIdt = - kIU*I - kIN*I + kUI*U + kNI*I
    dUdt = - kUI*U + kIU*I
    dNdt = - kNI*N + kIN*I
    return [dUdt, dIdt, dNdt]

def ODE_eqs_SS(C, t, k):
    r"""See eqs 1.1, 1.2 and 1.3
    :math:`U \underset{k_{IU}}{\stackrel{k_{UI}}{\rightleftharpoons}} \
    I \underset{k_{NI}}{\stackrel{k_{IN}}{\rightleftharpoons}} N`

    Args:
        C(list): collection of concentrations for each species [L, R, LR, ...]
        t(float): iterate over some time-course
        k(list): collection of rate constants [kf1, kb1, kf2, ...]

        k𝑈𝐼=11000𝑠−1, k𝐼𝑁=650𝑠−1
        k𝐼𝑈=7000𝑠−1, k𝑁𝐼=0.07𝑠−1
    """

    U, I, N = C[0], C[1], C[2]
    kUI, kIU, kIN, kNI = k[0], k[1], k[2], k[3]
    dIdt = - kIU*I - kIN*I + kUI*U + kNI*I
    dUdt = - kUI*U + kIU*(kUI*U+kNI*N)/(kIU+kIN)
    dNdt = - kNI*N + kIN*(kUI*U+kNI*N)/kIU+kIN
    return [dUdt, dIdt, dNdt]

def simulate(steps, simulation_time, parameters, rate_eqs):
    """Method for integrating the ODEs with SciPy odeint
    https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.odeint.html

    Args:
        steps(int): number of iterations
        simulation_time(int): duration of simulation (units: s)
        parameters(dict): {"species": ["L", "R", "LR"],\
                          "k": [kf1, kb1, kf2, kb2, ...],\  # rate constants
                          "InitConc": [L, R, P, ...]}       # initial concentrations (units: M)
        rate_eqs(function): method containing the ordinary differential equations
    Returns:
        Pandas DataFrame, plot
    """

    from scipy.integrate import odeint
    print(f"Number of species: {len(parameters['InitConc'])}")
    time = np.linspace(0, simulation_time, steps)
    C = odeint(rate_eqs, parameters["InitConc"], time, (parameters["k"],)).transpose()
    data = {"time": time, **{"%s"%s: C[i] for i,s in enumerate(parameters["species"])}}
    df = pd.DataFrame(data)
    plot = df.plot(x="time", y=parameters["species"], figsize=(10, 4),
                   xlim=(data["time"][0], data["time"][-1]))
    return df,plot

_____no_output_____# U = I = N
#[U] = 10.0, [I] = 0.0, and [N] = 10.0,
"""
k𝑈𝐼=11000𝑠−1, k𝐼𝑁=650𝑠−1
k𝐼𝑈=7000𝑠−1, k𝑁𝐼=0.07𝑠−1
"""

parameters = {"species": ["U", "I", "N", ],
             "InitConc": [10.0, 0.0, 10.0, ],
             #            [kf1, kb1, kf2, ...]
             "k":        [11000.0, 7000.0, 650.0, 0.07]}
df,plot = simulate(steps=10000, simulation_time=0.005,
                   parameters=parameters, rate_eqs=ODE_eqs)
plot = plot.set_title(r"$L \rightleftharpoons I \rightleftharpoons N$")
fig = plot.get_figure()
#fig.savefig("figure.png")
dfNumber of species: 3
# Stead State Approximation
parameters = {"species": ["U", "I", "N", ],
             "InitConc": [10.0, 0.0, 10.0, ],
             #            [kf1, kb1, kf2, ...]
             "k":        [11000.0, 7000.0, 650.0, 0.07]}
df,plot = simulate(steps=10000, simulation_time=0.005,
                   parameters=parameters, rate_eqs=ODE_eqs_SS)
plot = plot.set_title(r"$L \rightleftharpoons I \rightleftharpoons N$")
fig = plot.get_figure()
#fig.savefig("figure.png")
dfNumber of species: 3

Answer key:

$$k_{forward} = k_{IN} \left(\frac{k_{UI}}{k_{IU} + k_{IN}} \right)$$
$$k_{backward} = k_{NI} \left(\frac{k_{IU}}{k_{IU} + k_{IN}}\right)$$

NOTE: it takes some simplification to get this

Set $d[I]/dt = 0$. Occurs at time $t =0.0002 s$

$[U]$ is about $4 M$ and $[N]$ is about $10.5 M$

$$K = \frac{k_{f}}{k_{b}} = \frac{10.5}{4} = 2.63$$

$$\Delta G = -RT ln(K) = -8.314 J.mol^{-1}.K^{-1} (299 K)(ln(2.63))\frac{1kJ}{1000 J} = -24 kJ.mol^{-1} $$

2-state mechanism

$$k_{forward} = k_{IN} \left(\frac{k_{UI}}{k_{IU} + k_{IN}} \right) = 650(11000/(7000+650)) = 935 s^{-1}$$

$$k_{backward} = k_{NI} \left(\frac{k_{IU}}{k_{IU} + k_{IN}}\right) =  0.07(7000/(7000+650)) = 0.064 s^{-1}$$

Estimate: $K = 635/0.064 = 14609$

$$\Delta G = -RT ln(K) = -8.314 J.mol^{-1}.K^{-1} (299 K)(ln(2.63))\frac{1kJ}{1000 J} = -24 kJ.mol^{-1} $$
_____no_output_____