Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template of Training Pipeline #1

Merged
merged 2 commits into from
Apr 24, 2021
Merged

Template of Training Pipeline #1

merged 2 commits into from
Apr 24, 2021

Conversation

dupeljan
Copy link
Collaborator

No description provided.



def get_gt_boxes(self):
self.gt_boxes = [
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential place to put annotations for our custom dataset

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

train_od_ex.py Outdated
matplotlib.use('module://backend_interagg')


class RubberDucksTraining:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need DuckTraining at all? How this related to our project?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, change names and structure more related to our project w/o some rubber ducks

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rubber ducks are quite good as simplest validation dataset

@kprokofi
Copy link
Owner

@dupeljan Please, consider aligning training with my proposal data loader from kp/data branch. It's more appropriate to build a dataset separately. The proposal function will return ready-to-go for loop TensorFlow datasets

@dupeljan
Copy link
Collaborator Author

dupeljan commented Apr 11, 2021

@kprokofi it's the simplest use case of standard tensorwlow od model training. Now it's your turn to allingn chosen dataset to this pipeline (it's not a big deal)

self.finetune()
self.test()

def load_dataset(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change behavior of this function to load tf dataset. Will I? Anyway, I have a question. For what reason, we need to load images in numpy arrays?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the only reason for that is pyplot visualization I suggest removing it and move visualization to another script if you need this because I created a function to load data exactly in tf representation

Copy link
Collaborator Author

@dupeljan dupeljan Apr 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it only an example. I'll remove it and when you will add appropriate tf loader instead. I can't test pipeline without this method

zero_indexed_groundtruth_classes, self.num_classes))
print('Done prepping data.')

def visualize_np(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, do we have two visualization functions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's exist to visualize images in numpy ^)

configs = config_util.get_configs_from_pipeline_file(pipeline_config)
model_config = configs['model']
model_config.ssd.num_classes = num_classes
model_config.ssd.freeze_batchnorm = True
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we freezing batch normalization for fine-tuning?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion of official tf tutorial. We can ignore it and tune whatever we want

# restore the box regression head but initialize the classification head
# from scratch (we show the omission below by commenting out the line that
# we would add if we wanted to restore both heads)
fake_box_predictor = tf.compat.v2.train.Checkpoint(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why do we need some fake heads and stuff below? Actually, all we need to do is to load weights for the backbone and initialize a new head for classification. Don't we? Or these weird and non-intuitive steps are required by TensorFlow?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's weird way to load model from checkpoint without classification head. TF checkpoints is really difficult to load partially thus this hack exist

train_od_ex.py Outdated
# These parameters can be tuned; since our training set has 5 images
# it doesn't make sense to have a much larger batch size, though we could
# fit more examples in memory if we wanted to.
batch_size = 4
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hardcoded numbers. Please, do it in a more explicit way. As a last resort, make global variables.

Copy link
Collaborator Author

@dupeljan dupeljan Apr 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll make an attributes

prefixes_to_train = [
'WeightSharedConvolutionalBoxPredictor/WeightSharedConvolutionalBoxHead',
'WeightSharedConvolutionalBoxPredictor/WeightSharedConvolutionalClassHead']
for var in trainable_variables:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it mean that we fine-tune only heads?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, we have to inspect graph. But I think it tunes only head, yeah. We can choose layers whatever we want

ckpt.restore(checkpoint_path).expect_partial()

# Run model through a dummy image so that variables are created
image, shapes = self.detection_model.preprocess(tf.zeros([1, 640, 640, 3]))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the preprocess function? It's important for me to know

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import tensorflow as tf
from object_detection.utils import visualization_utils as viz_utils

def load_image_into_numpy_array(path):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another function to load data to NumPy?

Copy link
Collaborator Author

@dupeljan dupeljan Apr 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It loads single image

return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)

def plot_detections(image_np,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The third function for visualization? :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function quite useful because it visualize bounding boxes on images

@dupeljan dupeljan changed the title Add rubber duck training example Template of Training Pipeline Apr 18, 2021
@kprokofi kprokofi merged commit 1c82476 into main Apr 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants