Skip to content

Instantly share code, notes, and snippets.

# Reference - https://github.com/jainammm/TableNet/blob/master/TableNet.ipynb
class TableNet:
@staticmethod
def build_table_decoder(inputs, pool3, pool4):
x = Conv2D(512, (1, 1), activation = 'relu', name='conv7_table')(inputs)
x = UpSampling2D(size=(2, 2))(x)
concatenated = Concatenate()([x, pool4])
# concatenated = concatenate([x, pool4])
tf_lite_converter = tf.lite.TFLiteConverter.from_keras_model(model)
tf_lite_converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
tflite_model = tf_lite_converter.convert()
def get_file_size(file_path):
size = os.path.getsize(file_path)
return size
def convert_bytes(size, unit=None):
if unit == "KB":
return print('File size: ' + str(round(size / 1024, 3)) + ' Kilobytes')
elif unit == "MB":
return print('File size: ' + str(round(size / (1024 * 1024), 3)) + ' Megabytes')
else:
def create_mask(pred_mask1, pred_mask2):
"""Reference - https://github.com/jainammm/TableNet/blob/master/TableNet.ipynb
"""
pred_mask1 = tf.argmax(pred_mask1, axis=-1)
pred_mask1 = pred_mask1[..., tf.newaxis]
pred_mask2 = tf.argmax(pred_mask2, axis=-1)
pred_mask2 = pred_mask2[..., tf.newaxis]
return pred_mask1[0], pred_mask2[0]
resnet50 = tf.keras.applications.resnet50
conv_model = resnet50.ResNet50(weights='imagenet', include_top=False, input_shape=(228,228,3))
for layer in conv_model.layers:
layer.trainable = False
x = Conv2D(128, (1, 1), activation = 'relu', name='block6_conv1_table')(conv_model.output)
x = Dropout(0.8, name='block6_dropout_1')(x)
x = Conv2D(128, (1, 1), activation = 'relu', name='block6_conv2_table')(x)
x = Dropout(0.8, name='block6_dropout_2')(x)
def fun_column(x,output):
x = Conv2D(128, (1, 1), activation = 'relu', name='conv7_column')(x)
x = Dropout(0.8, name='block7_dropout_1')(x)
concatenated = Concatenate()([x,output])
concatenated = Concatenate()([concatenated,output])
x = UpSampling2D(size=(2, 2))(concatenated)
x = UpSampling2D(size=(2, 2))(x)
x = UpSampling2D(size=(2, 2))(x)
x = UpSampling2D(size=(2, 2))(x)
def fun_table(x,output):
x = Conv2D(128, (1, 1), activation = 'relu', name='conv7_table')(x)
concatenated = Concatenate()([x, output])
concatenated = Concatenate()([concatenated, output])
x = UpSampling2D(size=(2, 2))(concatenated)
x = UpSampling2D(size=(2, 2))(x)
x = UpSampling2D(size=(2, 2))(x)
x = UpSampling2D(size=(2, 2))(x)
last = tf.keras.layers.Conv2DTranspose(3, 3, strides=2,padding='same', name='table_output')
Model: "tablenet"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_7 (InputLayer) [(None, 228, 228, 3) 0
__________________________________________________________________________________________________
conv1_pad (ZeroPadding2D) (None, 234, 234, 3) 0 input_7[0][0]
__________________________________________________________________________________________________
conv1_conv (Conv2D) (None, 114, 114, 64) 9472 conv1_pad[0][0]
__________________________________________________________________________________________________
def normalize_image(input_image):
input_image = tf.cast(input_image, tf.float32) / 255.0
return input_image
def decode_image(img):
img = tf.image.decode_jpeg(img)
return tf.image.resize(img, [img_height, img_width])
def decode_mask_image(img):
img = tf.image.decode_jpeg(img, channels=1)
destination_t = '\content\drive\MyDrive\cs2_table'
destination_c = '\content\drive\MyDrive\cs2_col'
for i in df_org['filename'].unique():
# for each unique file, we take the height,width,depth from dataframe
file_width = int(df_org[df_org['filename']==i]['width'].unique())
file_height = int(df_org[df_org['filename']==i]['height'].unique())
# Creating an image array of dtype int32