<jupyter_start><jupyter_text>Single Block ResNet Loads ResNet model from keras library Turns 50-layer ResNet to 1-layer Trains the model Prints confusion matrix Saves the trained model as *.h5* and *.tflite* files Software License Agreement (MIT License) Copyright (c) 2020, <NAME>.<jupyter_code>import tensorflow as tf
import keras
import keras.layers as kl
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import numpy as np
import itertools
res50 = keras.applications.resnet50.ResNet50(include_top=True, weights=None, input_tensor=None, 
                                   input_shape=(128,128,3), pooling=None, classes=1)
res50.summary()
# create single block resnet
x = res50.layers[-160].output
pr = keras.layers.Activation('relu')(x)
pr = keras.layers.GlobalAveragePooling2D(data_format='channels_last')(pr)
pr = keras.layers.core.Dense(2, activation='softmax')(pr)
res1 = keras.models.Model(inputs=res50.input, outputs=pr)
res1.summary()
batchSize = 10
# datasets
dataGen = ImageDataGenerator(validation_split=0.1, 
                             preprocessing_function=keras.applications.mobilenet.preprocess_input)

trainBatch = dataGen.flow_from_directory(
    '/home/amirhossein/Codes/Project/Dataset/Dataset_678/dataset_openclose_678',
    target_size=(128,128), batch_size=batchSize, subset='training')

validateBatch = dataGen.flow_from_directory(
    '/home/amirhossein/Codes/Project/Dataset/Dataset_678/dataset_openclose_678',
    target_size=(128,128), batch_size=batchSize, subset='validation', shuffle=False)
res1.compile(keras.optimizers.Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])
res1.fit_generator(trainBatch, steps_per_epoch=1000, validation_data=validateBatch, validation_steps=4, 
                    epochs=6)
# to load existing model
res1 = keras.models.load_model('resnet.h5')
# plot confusion matrix
def plot_confusion_matrix(cm, classes, normalize=False, title='confusion_matrix', cmap=plt.cm.Blues):
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)
    
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print('Normalized confusion matrix')
    else:
        print('Confusion matrix, without normalization')
    print(cm)
    
    thresh = cm.max()/2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i,j], 
                horizontalalignment='center', 
                color='white' if cm[i,j] > thresh else 'black')
    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
plt.rcParams.update({'font.size': 12, 'mathtext.fontset':'stix', 'font.family':'STIXGeneral'})
# plt.rcParams.update(plt.rcParamsDefault)
valLabels = validateBatch.classes
predictions = res1.predict_generator(validateBatch, steps= len(valLabels)//batchSize+1)
cm = confusion_matrix(valLabels, predictions.argmax(axis=1))
cmPlotLabels = ['Grasped', 'Not Grasped']
plot_confusion_matrix(cm, cmPlotLabels, title='Confusion Matrix')
res1.save('resnet.h5')
converter = tf.lite.TFLiteConverter.from_keras_model_file('resnet.h5')
tflite_model = converter.convert()
open("resnet.tflite", "wb").write(tflite_model)<jupyter_output>W1117 12:29:24.995331 139739011262208 deprecation.py:573] From /usr/local/lib/python3.5/dist-packages/tensorflow/python/util/deprecation.py:507: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with distribution=normal is deprecated and will be removed in a future version.
Instructions for updating:
`normal` is a deprecated alias for `truncated_normal`
W1117 12:29:28.313690 139739011262208 deprecation.py:323] From /usr/local/lib/python3.5/dist-packages/tensorflow/lite/python/util.py:238: convert_variables_to_constants (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.compat.v1.graph_util.convert_variables_to_constants`
W1117 12:29:28.315587 139739011262208 deprecation.py:323] From /usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/graph_util_impl.py:270: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in [...]
{'fraction_non_alphanumeric': 0.11897251013970257, 'fraction_numerical': 0.04709328526363227, 'mean_word_length': 5.269774011299435, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '439862', 'license': 'MIT', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1447, 'n_words': 282, 'path': 'Single_Block_ResNet/resnet.ipynb', 'repo_name': 'amirhpd/grasp_verification', 'size': '4452'}