<jupyter_start><jupyter_text>CNN based on ResNet50<jupyter_code>import sys, json, os
import pandas as pd
import numpy as np
import skimage.io
#from tensorflow.python.keras.applications import ResNet50
import tensorflow
from tensorflow import keras
tensorflow.__version__
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing.image import ImageDataGenerator

from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

from tensorflow.keras.models import model_from_json, save_model, load_model
#from tensorflow.python.keras.applications import EfficientNetB3
import tensorflow_hub as hub
newmodel = hub.Module('b4')
embed = hub.KerasLayer('b4')
model = tensorflow.keras.Sequential()
model.add(embed)
model.summary()
from panda_bvv_config import *

model_name = os.path.join(note_path, 'model_panda.h5')
checkpoint_name = os.path.join(note_path, 'model_panda_check')
bsize = 32
num_epochs = 20
temp_arr = []
for path, subdirs, files in os.walk(train_cnn):
    for name in files:
        temp_arr.append(name)
num_train_files = len(temp_arr)
temp_arr = []
for path, subdirs, files in os.walk(valid_cnn):
    for name in files:
        temp_arr.append(name)
num_valid_files = len(temp_arr)
image_sizey, image_sizex, _ = skimage.io.imread(os.path.join(train_cnn,'isup0',\
                                                os.listdir(train_cnn+'/isup0')[0])).shape


s_per_epoch= num_train_files//bsize
val_steps = num_valid_files//bsize
data_generator = ImageDataGenerator(preprocessing_function=preprocess_input)
train_generator = data_generator.flow_from_directory(
        train_cnn,
        target_size=(image_sizey, image_sizex),
        batch_size= bsize,
        class_mode='categorical')


validation_generator = data_generator.flow_from_directory(
        valid_cnn,
        target_size=(image_sizey, image_sizex),
        batch_size = bsize,
        class_mode='categorical')

callbacks_list = [
        keras.callbacks.EarlyStopping(
        # Stop training when `val_loss` is no longer improving
        monitor='val_loss',
        mode = 'min',
        # "no longer improving" being defined as "no better than 1e-2 less"
        min_delta=1e-2,
        # "no longer improving" being further defined as "for at least 2 epochs"
        patience=5,
        verbose=1,
        restore_best_weights = True
        ),
        
        keras.callbacks.ModelCheckpoint(
        filepath= checkpoint_name +".{epoch:02d}.h5",
        monitor='val_loss',
        mode = 'auto',
        save_weights_only = False,
        save_freq = 'epoch',
        save_best_only=False
        ),
    
        #keras.callbacks.ReduceLROnPlateau(
        #monitor='val_loss',
        #factor=0.1,
        #patience=6,
        #)
]
resnet = ResNet50(include_top=False, pooling='avg', weights='imagenet')
new_model = Sequential()
new_model.add(resnet)
new_model.add(Flatten())
new_model.add(Dense(512, activation='relu'))
new_model.add(Dense(num_classes, activation='softmax'))
#new_model.layers[0].summary()
#new_model.summary()
# Say not to train first layer (ResNet) model. It is already trained
new_model.layers[0].trainable = False
new_model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
new_model.fit_generator(
        train_generator,
        steps_per_epoch=s_per_epoch,
        epochs = num_epochs,
        callbacks=callbacks_list,
        validation_data=validation_generator,
        validation_steps=val_steps)
new_model.save(os.path.join(base_path, model_name))
import os
module_name = 'respca_simple_fit'

os.system('jupyter nbconvert --to python ' + module_name + '.ipynb')
with open(module_name + '.py', 'r') as f:
    lines = f.readlines()
with open(module_name + '.py', 'w') as f:
    for line in lines:
        if 'nbconvert --to python' in line:
            break
        else:
            f.write(line)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10110736639383726, 'fraction_numerical': 0.009629272989889264, 'mean_word_length': 3.7813578826237055, 'pattern_counts': {'":': 0, '<': 5, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '144138', 'license': 'Apache-2.0', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1355, 'n_words': 283, 'path': 'tf_hub_ex.ipynb', 'repo_name': 'Belobragin/PCA-Kaggle', 'size': '4154'}