<jupyter_start><jupyter_text>reg_lambda=.001model = Sequential()model.add(Conv2D(input_shape=(27,27,3),filters=64,kernel_size=(3,3),kernel_regularizer=tf.keras.regularizers.l2(reg_lambda),padding="same", activation="relu"))model.add(Conv2D(filters=64,kernel_size=(3,3),kernel_regularizer=tf.keras.regularizers.l2(reg_lambda),padding="same", activation="relu"))    model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))    model.add(Conv2D(filters=128, kernel_size=(3,3),kernel_regularizer=tf.keras.regularizers.l2(reg_lambda), padding="same", activation="relu"))model.add(Conv2D(filters=128, kernel_size=(3,3),kernel_regularizer=tf.keras.regularizers.l2(reg_lambda), padding="same", activation="relu"))    model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))    model.add(Conv2D(filters=256, kernel_size=(3,3),kernel_regularizer=tf.keras.regularizers.l2(reg_lambda), padding="same", activation="relu"))model.add(Conv2D(filters=256, kernel_size=(3,3),kernel_regularizer=tf.keras.regularizers.l2(reg_lambda), padding="same", activation="relu"))model.add(Conv2D(filters=256, kernel_size=(3,3),kernel_regularizer=tf.keras.regularizers.l2(reg_lambda), padding="same", activation="relu"))    model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))    model.add(Conv2D(filters=512, kernel_size=(3,3),kernel_regularizer=tf.keras.regularizers.l2(reg_lambda), padding="same", activation="relu"))model.add(Conv2D(filters=512, kernel_size=(3,3),kernel_regularizer=tf.keras.regularizers.l2(reg_lambda), padding="same", activation="relu"))model.add(Conv2D(filters=512, kernel_size=(3,3),kernel_regularizer=tf.keras.regularizers.l2(reg_lambda), padding="same", activation="relu"))    model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))    model.add(Flatten())model.add(Dropout(0.2))model.add(Dense(1024,activation="relu"))model.add(Dropout(0.2))model.add(Dense(512,activation="relu"))model.add(Dropout(0.2))model.add(Dense(256, activation="relu"))model.add(Dropout(0.2))model.add(Dense(4, activation='softmax'))<jupyter_code>tf.keras.utils.plot_model(model, show_shapes=True)
optimizer = Adam(lr=1e-5, beta_1=0.9, beta_2=0.999, decay=1.0e-6)
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['categorical_accuracy'])

# define callbacks
weight_path="{}_model.best.hdf5".format('CellTypeName')
checkpoint = ModelCheckpoint(weight_path, monitor = 'val_loss', mode = 'min', verbose = 1, save_best_only = True, save_weights_only = True)
early = EarlyStopping(monitor = 'val_loss', mode = 'min', patience=10)
callbacks_list = [checkpoint, early]
history = model.fit(
        train_generator,
        validation_data= validation_generator,
        epochs=100,
        verbose=0,
        callbacks=callbacks_list)
def plot_history(history):
    
    f = plt.figure()
    f.set_figwidth(10)
    f.add_subplot(1, 2, 1)
    plt.plot(history.history['val_loss'], label='val loss')
    plt.plot(history.history['loss'], label='train loss')
    plt.legend()

    f.add_subplot(1, 2, 2)
    plt.plot(history.history['val_categorical_accuracy'], label='val accuracy')
    plt.plot(history.history['categorical_accuracy'], label='train accuracy')
    plt.legend()

    plt.show()
plot_history(history)
#Data Augmentation
batch_size = 32
train_img_aug = ImageDataGenerator(rescale=1.0/255.0,
                            horizontal_flip=True,
                            vertical_flip=True,
                            shear_range=0.2,
                            zoom_range=0.2,
                            data_format='channels_last',
                            rotation_range=15)
val_img_aug = ImageDataGenerator(rescale=1.0/255.0)


train_generator = train_img_aug.flow_from_dataframe(
            dataframe=train_data,
            directory='./',
            x_col="imagePath",
            y_col="cellTypeName",
            target_size=(27,27),
            class_mode='categorical')

validation_generator = val_img_aug.flow_from_dataframe(
            dataframe=val_data,
            directory='./',
            x_col="imagePath",
            y_col="cellTypeName",
            target_size=(27,27),
            class_mode='categorical')
reg_lambda = 0.001
#RCCNet Model
model = Sequential()

model.add(Conv2D(filters=32,kernel_size=(3,3), activation='relu', padding='same', input_shape=(27,27,3),kernel_regularizer=tf.keras.regularizers.l2(reg_lambda) ))
model.add(BatchNormalization())
model.add(Conv2D(32,(3,3),activation='relu',kernel_regularizer=tf.keras.regularizers.l2(reg_lambda)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(64,(3,3), activation='relu', padding='same',kernel_regularizer=tf.keras.regularizers.l2(reg_lambda)))
model.add(BatchNormalization())
model.add(Conv2D(64,(3,3), activation='relu',kernel_regularizer=tf.keras.regularizers.l2(reg_lambda)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())
model.add(Dense(512,activation='relu',kernel_regularizer=tf.keras.regularizers.l2(reg_lambda)))
model.add(BatchNormalization())
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu',kernel_regularizer=tf.keras.regularizers.l2(reg_lambda)))
model.add(BatchNormalization())
model.add(Dropout(0.2))
model.add(Dense(4,activation='softmax'))
optimizer = Adam(lr=1e-5)
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['categorical_accuracy'])

# define callbacks
weight_path="{}_model.best.hdf5".format('CellTypeName')
checkpoint = ModelCheckpoint(weight_path, monitor = 'val_loss', mode = 'min', verbose = 1, save_best_only = True, save_weights_only = True)
early = EarlyStopping(monitor = 'val_loss', mode = 'min', patience=10)
callbacks_list = [checkpoint, early]
history = model.fit_generator(
        train_generator,
        validation_data= validation_generator,
        epochs=500,
        verbose=0,
        callbacks=callbacks_list)
plot_history(history)
test_datagen = ImageDataGenerator(rescale=1./255, data_format='channels_last')

batch_size_t = 1

# Here the validation is used for testing and this should be changes to a separate test set.
test_generator = train_img_aug.flow_from_dataframe(
        dataframe=test_data,
        directory='./',
        x_col="imagePath",
        y_col="cellTypeName",
        target_size=(27, 27),
        batch_size=batch_size_t,
        class_mode='categorical')
d = {'fibroblast':0, 'inflammatory':1, 'epithelial':2, 'others':3}
d_inv = {v: k for k, v in d.items()}
plt.figure(figsize=(16,4))
batches = 0 
for x,y in test_generator:
    batches = batches + 1
    y_hat = model.predict(x, verbose = 0)
    x = np.squeeze(x)
    if batches < 5:
        plt.subplot(1,5,batches)
        plt.imshow(x)
        plt.title(f"GT-{d_inv[np.argmax(y[0])]}, Pred-{d_inv[np.argmax(y_hat[0])]}")
    else:
        break
        
    plt.axis('off')
extra_img = pd.read_csv("data_labels_extraData.csv")
extra_img.head()
extra_img_path = {os.path.basename(x) : x for x in
               glob(os.path.join("./patch_images","*.png"))}
print("Total Images found in image directory:", len(extra_img_path), ", Total images in main CSV file" , extra_img.shape[0])
extra_img["imagePath"] = extra_img["ImageName"].map(extra_img_path.get)

extra_img = extra_img["imagePath"]
extra_img.head()
model.load_weights(weight_path)
labled_data = model.predict(extra_img)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1574589385095697, 'fraction_numerical': 0.03515678023618841, 'mean_word_length': 5.357204486626402, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '88373', 'license': 'Apache-2.0', 'n_tokens_mistral': 2677, 'n_tokens_neox': 2562, 'n_words': 308, 'path': 'TASK2.ipynb', 'repo_name': 'kumarv987/ML_2021_Assignment2', 'size': '7367'}