Untitled

 avatar
unknown
plain_text
2 years ago
5.2 kB
3
Indexable
for training-

from tensorflow.keras.applications.inception_v3 import InceptionV3, preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D



import ssl

try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    pass
else:
    ssl._create_default_https_context = _create_unverified_https_context

# Load the pre-trained InceptionV3 model without the top (fully connected) layers
base_model = InceptionV3(weights='imagenet', include_top=False)

# Add custom top layers for fine-tuning
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(1, activation='sigmoid')(x)  # Adjust the number of units based on binary or multi-class classification

# Combine the base model and custom top layers
model = Model(inputs=base_model.input, outputs=predictions)


for layer in base_model.layers:
    layer.trainable = False


model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(
    rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True
)

test_datagen = ImageDataGenerator(rescale=1./255)

batch_size = 32

# Define paths to your train and validation datasets
train_dataset_path = '/Analytics/venv/Jup/CAPE_Case_Management_PDF_Invoicing/Data/images/Train_dataset_images/'
validation_dataset_path = '/Analytics/venv/Jup/CAPE_Case_Management_PDF_Invoicing/Data/images/Validation_dataset_images/'

import pandas as pd
import os

# Create DataFrames with file paths and labels
train_invoice_df = pd.DataFrame({'filepath': [os.path.join(train_dataset_path, 'invoice', f) for f in os.listdir(os.path.join(train_dataset_path, 'invoice'))],
                                  'label': 'invoice'})

train_non_invoice_df = pd.DataFrame({'filepath': [os.path.join(train_dataset_path, 'non_invoice', f) for f in os.listdir(os.path.join(train_dataset_path, 'non_invoice'))],
                                      'label': 'non_invoice'})

validation_invoice_df = pd.DataFrame({'filepath': [os.path.join(validation_dataset_path, 'invoice', f) for f in os.listdir(os.path.join(validation_dataset_path, 'invoice'))],
                                       'label': 'invoice'})

validation_non_invoice_df = pd.DataFrame({'filepath': [os.path.join(validation_dataset_path, 'non_invoice', f) for f in os.listdir(os.path.join(validation_dataset_path, 'non_invoice'))],
                                           'label': 'non_invoice'})

# Concatenate the dataframes
train_df = pd.concat([train_invoice_df, train_non_invoice_df], ignore_index=True)
validation_df = pd.concat([validation_invoice_df, validation_non_invoice_df], ignore_index=True)

# Shuffle the dataframes
train_df = train_df.sample(frac=1).reset_index(drop=True)
validation_df = validation_df.sample(frac=1).reset_index(drop=True)

# Create data generators
train_generator = train_datagen.flow_from_dataframe(
    train_df,
    x_col='filepath',
    y_col='label',
    target_size=(299, 299),
    batch_size=batch_size,
    class_mode='binary'
)

validation_generator = test_datagen.flow_from_dataframe(
    validation_df,
    x_col='filepath',
    y_col='label',
    target_size=(299, 299),
    batch_size=batch_size,
    class_mode='binary'
)

epochs =10

print("Number of training samples:", len(train_generator))
print("Number of validation samples:", len(validation_generator))

print("Class indices for training:", train_generator.class_indices)
print("Class indices for validation:", validation_generator.class_indices)



model.fit(
    train_generator,
    steps_per_epoch=train_generator.samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=validation_generator.samples // batch_size
)

model.save('fine_tuned_invoice_model.h5')


There is one sample code-

model = Sequential()
model.add(Conv2D(input_shape = (32,32,1), filters = 8, kernel_size = (5,5),activation = "relu", padding = "same" ))
model.add(MaxPooling2D(pool_size = (2,2)))

model.add(Conv2D(filters = 8, kernel_size = (3,3),activation = "relu", padding = "same" ))
model.add(MaxPooling2D(pool_size = (2,2)))

model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(units = 256, activation = "relu"))
model.add(Dropout(0.2))
model.add(Dense(units = noOfClasses, activation = "softmax"))

model.compile(loss = "categorical_crossentropy", optimizer=("Adam"), metrics = ["accuracy"])

batch_size = 250

hist = model.fit_generator(dataGen.flow(x_train, y_train, batch_size = batch_size),
                                      validation_data = (x_validation, y_validation),
                                      epochs = 15, steps_per_epoch = x_train.shape[0]//batch_size, shuffle = 1)

Can we modify our code to create these layers and also implement how it is shown here in sample code for training.
Editor is loading...
Leave a Comment