<jupyter_start><jupyter_text>Data LoadingRead STM Au(111) data into memory via ImageDataGenerator.<jupyter_code>root_dir ='H:\\Research\\AI_STM\\sxm-storage\\'
labels = ['artifacts',
          'atomic-lattice-clear',
          'herringbone-clear',
          'steps-clear']
img_folder1 = 'All Au(111)'
img_folder2 = 'Au-111_Images_jpeg_gray'
img_dir =  os.path.join(root_dir, img_folder2)
sources = [os.path.join(img_dir, label) for label in labels]
sources
num_data = np.array([len(os.listdir(source)) for source in sources])
print(num_data)
neg=num_data[0]
pos=num_data[1]
total_data = np.sum(num_data)
precentage = 100.0*num_data/total_data
plt.bar(range(len(num_data)), num_data)
plt.show()
print(precentage)
num_train = np.array([math.floor(count * 0.7) for count in num_data])
num_test =  np.array([math.floor(count * 0.3) for count in num_data])
total_train = sum(num_train)
total_val = sum(num_test)
# print("Train:", total_train, num_train)
# print("Val:", total_val, num_val)

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, num_train, width, label='Train')
rects2 = ax.bar(x + width/2, num_test, width, label='Test')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Count')
ax.set_title('Dataset Distribution')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

fig.tight_layout()
plt.xticks(rotation=60)
plt.show()
out = '-'.join(labels)
out_dir = os.path.join(root_dir, 'datasets', out)
train_dir = os.path.join(out_dir, 'train')
validation_dir = os.path.join(out_dir, 'validation')
training_label_dirs = list(map(lambda label: os.path.join(train_dir, label), labels))
validation_label_dirs = list(map(lambda label: os.path.join(validation_dir, label), labels))
validation_label_dirs
rng = np.random.default_rng()
train_samples = list(range(len(labels)))
test_samples = list(range(len(labels)))
# Re-order train and vlidation images
for i, label in enumerate(labels):
    src = sources[i]
    train = training_label_dirs[i]
    n_train = num_train[i]
    test = validation_label_dirs[i]
    n_test = num_test[i]
    images = os.listdir(src)
    # Obtain a random list of images of size k without replacement to use as the valiation set
    test_sample = random.sample(images, k=n_test)
    test_samples[i] = len(test_sample)
    # Remove sample from origional list 
    train_sample = list(set(images)-set(test_sample))
    # Oversample unbalanced classes
    train_sample = rng.choice(train_sample, size=num_train.max())
    train_samples[i] = len(train_sample)
    # Copy validation images to new folder
    copy_all(test_sample, src, test, label)
    # Copy remaining training images to new folder
    copy_all(train_sample, src, train, label)

print(train_samples)
print(test_samples)
x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/3, train_samples, width, label='Train')
rects2 = ax.bar(x + width/3, test_samples, width, label='Test')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Count')
ax.set_title('Oversampled Dataset Distribution')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

fig.tight_layout()
plt.xticks(rotation=60)
plt.show()<jupyter_output>Complete.
Complete.
Complete.
Complete.
Complete.
Complete.
Complete.
Complete.
[110, 110, 110, 110]
[30, 35, 47, 21]<jupyter_text>Import Data<jupyter_code># This function will plot images in the form of a grid with 1 row and 5 columns where images are placed in each column.
def plotImages(images_arr):
    fig, axes = plt.subplots(1, 5, figsize=(20,20))
    axes = axes.flatten()
    for img, ax in zip(images_arr, axes):
        ax.imshow(img)
    plt.tight_layout()
    plt.show()
# Number of training examples to process before updating our models variables
BATCH_SIZE = 64
# Our training data consists of images with width of 150 pixels and height of 150 pixels
IMG_SHAPE  = 256
image_generator = ImageDataGenerator(
    rescale=1./255,
    rotation_range=2,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.4,
    horizontal_flip=True,
    fill_mode='wrap',
    validation_split=0.2)

train_data_gen = image_generator.flow_from_directory(subset='training',
                                                    directory=train_dir,
                                                    shuffle=True,
                                                    batch_size=BATCH_SIZE,
                                                    target_size=(IMG_SHAPE,IMG_SHAPE),
                                                    class_mode='categorical')

val_data_gen = image_generator.flow_from_directory(subset='validation',
                                                 directory=train_dir,
                                                 batch_size=BATCH_SIZE,
                                                 target_size=(IMG_SHAPE, IMG_SHAPE),
                                                 class_mode='categorical')

image_gen_test = ImageDataGenerator(rescale=1./255)
test_data_gen = image_gen_test.flow_from_directory(batch_size=BATCH_SIZE,
                                                 directory=validation_dir,
                                                 target_size=(IMG_SHAPE, IMG_SHAPE),
                                                 class_mode='categorical')

augmented_images = [train_data_gen[0][0][0] for i in range(5)]
plotImages(augmented_images)<jupyter_output>Found 380 images belonging to 4 classes.
Found 92 images belonging to 4 classes.
Found 133 images belonging to 4 classes.<jupyter_text>Define the model<jupyter_code>METRICS = [
      tf.keras.metrics.TruePositives(name='tp'),
      tf.keras.metrics.FalsePositives(name='fp'),
      tf.keras.metrics.TrueNegatives(name='tn'),
      tf.keras.metrics.FalseNegatives(name='fn'), 
      tf.keras.metrics.CategoricalAccuracy(name='accuracy'),
      tf.keras.metrics.Precision(name='precision'),
      tf.keras.metrics.Recall(name='recall'),
      tf.keras.metrics.AUC(name='auc'),
]
num_classes = len(labels)
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(IMG_SHAPE, IMG_SHAPE, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),

    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dense(num_classes, activation='softmax')
])
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(8, (7,7), activation='relu', input_shape=(256, 256, 3)),
    tf.keras.layers.Conv2D(8, (3,3), activation='relu'),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2, 2),

    tf.keras.layers.Conv2D(16, (3,3), activation='relu'),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(num_classes, activation='softmax')
])
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(8, (7,7), activation='relu', input_shape=(256, 256, 3),kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Conv2D(8, (3,3), activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2, 2),

    tf.keras.layers.Conv2D(16, (3,3), activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Conv2D(32, (3,3), activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Conv2D(32, (3,3), activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(num_classes, activation='softmax')
])

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(8, 7,  padding='same', activation='relu', input_shape=(256, 256, 3),  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Conv2D(8, 3, padding='same', activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Conv2D(8, 3, padding='same', activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2, 2),

    tf.keras.layers.Conv2D(16, 3, padding='same', activation='relu',  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Conv2D(1, 1, padding='same', activation='relu',  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Conv2D(16, 3, padding='same', activation='relu',  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2,2),
        
    tf.keras.layers.Conv2D(1, 1, activation='relu',  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    
    tf.keras.layers.Conv2D(32, (3,3), padding='same', activation='relu',  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Conv2D(32, (3,3), padding='same', activation='relu',  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(num_classes, activation='softmax')
])
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(8, 7,  padding='same', activation='relu', input_shape=(256, 256, 3),  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Conv2D(8, 3,  padding='same', activation='relu', input_shape=(256, 256, 3),  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Conv2D(8, 3,  padding='same', activation='relu', input_shape=(256, 256, 3),  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2, 2),
    
    tf.keras.layers.Conv2D(16, 3, padding='same', activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Conv2D(16, 3, padding='same', activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2, 2),

    tf.keras.layers.Conv2D(32, 3, padding='same', activation='relu',  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Conv2D(32, 3, padding='same', activation='relu',  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2,2),
        
    tf.keras.layers.Conv2D(64, 3, padding='same', activation='relu',  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Conv2D(64, 3, padding='same', activation='relu',  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2,2),
    
    tf.keras.layers.Conv2D(128, 3, padding='same', activation='relu',  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.Conv2D(128, 3, padding='same', activation='relu',  kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    tf.keras.layers.BatchNormalization(momentum=0.0),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(num_classes, activation='softmax')
])
model.compile(optimizer='adam',
              loss=tf.keras.losses.CategoricalCrossentropy(),
              metrics=METRICS)

# opt = tf.keras.optimizers.SGD(
#     learning_rate=0.01, momentum=0.001, name='SGD'
# )
# model.compile(optimizer=opt,
#               loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
#               metrics=['accuracy'])
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_10 (Conv2D)           (None, 250, 250, 8)       1184      
_________________________________________________________________
conv2d_11 (Conv2D)           (None, 248, 248, 8)       584       
_________________________________________________________________
batch_normalization_8 (Batch (None, 248, 248, 8)       32        
_________________________________________________________________
max_pooling2d_8 (MaxPooling2 (None, 124, 124, 8)       0         
_________________________________________________________________
conv2d_12 (Conv2D)           (None, 122, 122, 16)      1168      
_________________________________________________________________
batch_normalization_9 (Batch (None, 122, 122, 16)      64        
________________________________________________________[...]<jupyter_text>Train updated model<jupyter_code>class_weights = class_weight.compute_class_weight('balanced',
                                                  classes=np.unique(test_data_gen.classes), 
                                                  y=test_data_gen.classes)
class_weights = dict(zip(range(len(class_weights)), class_weights))
class_weights
early_stopping = tf.keras.callbacks.EarlyStopping(
    monitor='val_auc', 
    verbose=1,
    patience=10,
    mode='max',
    restore_best_weights=True)
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
# Test fit
epochs=100
history = model.fit_generator(
    train_data_gen,
    steps_per_epoch = train_data_gen.samples // BATCH_SIZE,
    epochs=epochs,
    validation_data =val_data_gen,
    validation_steps = val_data_gen.samples // BATCH_SIZE,
    class_weight=class_weights,
    callbacks=[tensorboard_callback]
)
def plot_metrics(history):
    metrics = ['loss', 'accuracy', 'precision', 'recall', 'auc']
    plt.figure(figsize=(16, 12))
    for n, metric in enumerate(metrics):
        name = metric.replace("_"," ").capitalize()
        plt.subplot(2,3,n+1)
        plt.plot(history.epoch, history.history[metric], label='Train')
        plt.plot(history.epoch, history.history['val_'+metric], linestyle="--", label='Val')
        plt.xlabel('Epoch')
        plt.ylabel(name)
        plt.autoscale()
        plt.legend()
    
#     name = metric.replace("_"," ").capitalize()
#     plt.subplot(2,3,6)
#     plt.plot(history.history['recall'], history.history['precision'], label='Train')
#     plt.plot(history.history['val_recall'], history.history['val_precision'], label='Val')
#     plt.xlabel('Recall')
#     plt.ylabel('Precision')
    plt.autoscale()
    plt.legend()
plot_metrics(history)
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(epochs)

plt.figure(figsize=(12, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
# %tensorboard --logdir logs/fit<jupyter_output><empty_output><jupyter_text>Make Predictions<jupyter_code>from sklearn.metrics import confusion_matrix
import seaborn as sns

def plot_cm(labels, predictions, p=0.5):
    cm = confusion_matrix(labels, predictions, labels=range(4))
    plt.figure(figsize=(5,5))
    sns.heatmap(cm, annot=True, fmt="d", cmap=plt.cm.Blues)
    plt.title('Confusion matrix @{:.2f}'.format(p))
    plt.ylabel('Actual label')
    plt.xlabel('Predicted label')

    print('True Negatives: ', cm[0][0])
    print('False Positives: ', cm[0][1])
    print('False Negatives: ', cm[1][0])
    print('True Positives: ', cm[1][1])
    print('Total Fraudulent: ', np.sum(cm[1]))
test_data_gen.classes
train_predictions_baseline = model.predict_generator(train_data_gen, steps=BATCH_SIZE)
test_predictions_baseline = model.predict_generator(test_data_gen, steps=BATCH_SIZE)
y_true = test_data_gen.classes
y_pred = np.argmax(test_predictions_baseline, axis=1)
plot_cm(y_true, y_pred)<jupyter_output>True Negatives:  6
False Positives:  6
False Negatives:  9
True Positives:  9
Total Fraudulent:  35<jupyter_text>Plot the ROCNow plot the ROC. This plot is useful because it shows, at a glance, the range of performance the model can reach just by tuning the output threshold.<jupyter_code>def plot_roc(name, labels, predictions, **kwargs):
    fp, tp, _ = sklearn.metrics.roc_curve(labels, predictions)
    plt.plot(100*fp, 100*tp, label=name, linewidth=2, **kwargs)
    plt.xlabel('False positives [%]')
    plt.ylabel('True positives [%]')
    plt.grid(True)
    ax = plt.gca()
    ax.set_aspect('equal')
from itertools import cycle
def plot_roc_multiclass(name, labels, predictions, **kwargs):
    fpr = dict()
    tpr = dict()
    roc_auc = dict()
    n_classes = 4
    plt.figure(figsize=(8, 6))
    for i in range(n_classes):
        y_test = (labels == i).astype(int)
        y_pred = (predictions == i).astype(int)
        fpr[i], tpr[i], _ = sklearn.metrics.roc_curve(y_test, y_pred)
        roc_auc[i] = sklearn.metrics.auc(fpr[i], tpr[i])
#         roc_display = sklearn.metrics.RocCurveDisplay(fpr=fpr[i], tpr=tpr[i]).plot()
    
    lw = 2
    colors = cycle(['blue', 'red', 'green', 'purple'])
    for i, color in zip(range(n_classes), colors):
        plt.plot(fpr[i], tpr[i], color=color, lw=lw,
                 label='ROC curve class {0} (area = {1:0.2f})'.format(i, roc_auc[i]))
    
    plt.plot([0, 1], [0, 1], 'k--', lw=lw, label='No Skill')
    plt.xlim([-0.05, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate (%)')
    plt.ylabel('True Positive Rate (%)')
    plt.title('Receiver operating characteristic for {} data'.format(name))
    plt.legend(loc="lower right")
    plt.gca().set_aspect('equal')
    plt.show()

plot_roc_multiclass("Train", train_data_gen.classes, np.argmax(train_predictions_baseline, axis=1))
plot_roc_multiclass("Test", test_data_gen.classes, np.argmax(test_predictions_baseline, axis=1), linestyle='--')
from sklearn.metrics import precision_recall_curve

def plot_pr_multiclass(name, labels, predictions, **kwargs):
    precision = dict()
    recall = dict()
    thresholds = dict()
    mAP = dict()
    n_classes = 4
    plt.figure(figsize=(8, 6))
    for i in range(n_classes):
        y_test = (labels == i).astype(int)
        y_pred = (predictions == i).astype(int)
        precision[i], recall[i], thresholds[i] = sklearn.metrics.precision_recall_curve(y_test, y_pred)
        mAP[i] = sklearn.metrics.average_precision_score(y_test, y_pred)
#         pr_display = sklearn.metrics.PrecisionRecallDisplay(precision=precision[i], recall=recall[i]).plot()
        
    lw = 2
    colors = cycle(['blue', 'red', 'green', 'purple'])
    for i, color in zip(range(n_classes), colors):
        plt.plot(recall[i], precision[i], color=color, lw=lw,
                 label='Percision-Recall curve class {0} (mAP = {1:0.2f})'.format(i, mAP[i]))

    no_skill = len(labels[labels==1]) / len(labels)
    plt.plot([0, 1], [no_skill, no_skill], 'k--', linestyle='--', label='No Skill')
    plt.xlim([-0.05, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('Recall')
    plt.ylabel('Percision')
    plt.title('Percision-Recall Curve for {} data'.format(name))
    plt.legend(loc="upper right")
    plt.gca().set_aspect('equal')
    plt.show()
plot_pr_multiclass("Train", train_data_gen.classes, np.argmax(train_predictions_baseline, axis=1))
plot_pr_multiclass("Test", test_data_gen.classes, np.argmax(test_predictions_baseline, axis=1), linestyle='--')
# # residual network
# input = Input(shape=(input_shape[0], input_shape[1], input_shape[2]))

# '''block_1'''
# b1_cnv2d_1 = Conv2D(filters=16, kernel_size=(3, 3), strides=(2, 2), padding='same',
#                  use_bias=False, name='b1_cnv2d_1', kernel_initializer='normal')(input)
# b1_relu_1 = ReLU(name='b1_relu_1')(b1_cnv2d_1)
# b1_bn_1 = BatchNormalization(epsilon=1e-3, momentum=0.999, name='b1_bn_1')(b1_relu_1)  # size: 14*14

# b1_cnv2d_2 = Conv2D(filters=32, kernel_size=(1, 1), strides=(2, 2), padding='same',
#                     use_bias=False, name='b1_cnv2d_2', kernel_initializer='normal')(b1_bn_1)
# b1_relu_2 = ReLU(name='b1_relu_2')(b1_cnv2d_2)
# b1_out = BatchNormalization(epsilon=1e-3, momentum=0.999, name='b1_out')(b1_relu_2)  # size: 14*14

# '''block 2'''
# b2_cnv2d_1 = Conv2D(filters=32, kernel_size=(1, 1), strides=(1, 1), padding='same',
#                     use_bias=False, name='b2_cnv2d_1', kernel_initializer='normal')(b1_out)
# b2_relu_1 = ReLU(name='b2_relu_1')(b2_cnv2d_1)
# b2_bn_1 = BatchNormalization(epsilon=1e-3, momentum=0.999, name='b2_bn_1')(b2_relu_1)  # size: 14*14

# b2_add = add([b1_out, b2_bn_1])  #

# b2_cnv2d_2 = Conv2D(filters=64, kernel_size=(3, 3), strides=(2, 2), padding='same',
#                     use_bias=False, name='b2_cnv2d_2', kernel_initializer='normal')(b2_add)
# b2_relu_2 = ReLU(name='b2_relu_2')(b2_cnv2d_2)
# b2_out = BatchNormalization(epsilon=1e-3, momentum=0.999, name='b2_bn_2')(b2_relu_2)  # size: 7*7

# '''block 3'''
# b3_cnv2d_1 = Conv2D(filters=64, kernel_size=(1, 1), strides=(1, 1), padding='same',
#                     use_bias=False, name='b3_cnv2d_1', kernel_initializer='normal')(b2_out)
# b3_relu_1 = ReLU(name='b3_relu_1')(b3_cnv2d_1)
# b3_bn_1 = BatchNormalization(epsilon=1e-3, momentum=0.999, name='b3_bn_1')(b3_relu_1)  # size: 7*7

# b3_add = add([b2_out, b3_bn_1])  #

# b3_cnv2d_2 = Conv2D(filters=128, kernel_size=(3, 3), strides=(2, 2), padding='same',
#                     use_bias=False, name='b3_cnv2d_2', kernel_initializer='normal')(b3_add)
# b3_relu_2 = ReLU(name='b3_relu_2')(b3_cnv2d_2)
# b3_out = BatchNormalization(epsilon=1e-3, momentum=0.999, name='b3_out')(b3_relu_2)  # size: 3*3

# '''block 4'''
# b4_avg_p = GlobalAveragePooling2D()(b3_out)
# output = Dense(output_shape, name='model_output', activation='softmax',
#                kernel_initializer='he_uniform')(b4_avg_p)

# model = Model(input, output)

# model_json = model.to_json()

# with open("sample_res_net_v0.json", "w") as json_file:
#     json_file.write(model_json)
# model.summary()
# return model<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1543027955540586, 'fraction_numerical': 0.040417649040080834, 'mean_word_length': 4.111469765440069, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '313299', 'license': 'MIT', 'n_tokens_mistral': 9071, 'n_tokens_neox': 8542, 'n_words': 1354, 'path': 'Examples/.ipynb_checkpoints/Herringbone-Lattice_CNN_Greyscale-checkpoint.ipynb', 'repo_name': 'darianSmalley/NanoscoPy', 'size': '23752'}