bethgelab/siamese-mask-rcnn

Loading weights

Opened this issue · 0 comments

Hello!
Very nice paper :)
I am trying to use the model for my own images so I loaded the pretrained weights but ran into an issue: here is the code adapted from evaluate.ipynb

`
%load_ext autoreload
%autoreload 2
%matplotlib inline
#%load_ext line_profiler

import tensorflow.compat.v1 as tf
tf.logging.set_verbosity(tf.logging.INFO)
sess_config = tf.ConfigProto()

import sys
import os

COCO_DATA = 'data/coco/'
MASK_RCNN_MODEL_PATH = 'lib/Mask_RCNN/'

if MASK_RCNN_MODEL_PATH not in sys.path:
sys.path.append(MASK_RCNN_MODEL_PATH)

from samples.coco import coco
from mrcnn import utils
from mrcnn import model as modellib
from mrcnn import visualize

from lib import utils as siamese_utils
from lib import model as siamese_model
from lib import config as siamese_config

import time
import datetime
import random
import numpy as np
import skimage.io
import imgaug
import pickle
import matplotlib.pyplot as plt
from collections import OrderedDict

Root directory of the project

ROOT_DIR = os.getcwd()

Directory to save logs and trained model

MODEL_DIR = os.path.join(ROOT_DIR, "logs")

Select checkpoint

checkpoint = 'checkpoints/small_siamese_mrcnn_0160.h5'

class SmallEvalConfig(siamese_config.Config):
# Set batch size to 1 since we'll be running inference on
# one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
GPU_COUNT = 1
IMAGES_PER_GPU = 1
NUM_CLASSES = 1 + 1
NAME = 'coco'
EXPERIMENT = 'evaluation'
CHECKPOINT_DIR = 'checkpoints/'
NUM_TARGETS = 1

config = SmallEvalConfig()

model = siamese_model.SiameseMaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
model.load_checkpoint(checkpoint, training_schedule=train_schedule)

`

When i load the checkpoint I get the following error:


ValueError Traceback (most recent call last)
Cell In[50], line 2
1 model = siamese_model.SiameseMaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
----> 2 model.load_checkpoint(checkpoint, training_schedule=train_schedule)
3 model

File ~/siamese-mask-rcnn/lib/model.py:847, in SiameseMaskRCNN.load_checkpoint(self, weights_path, training_schedule, verbose)
845 self.set_trainable(".*")
846 # load weights
--> 847 self.load_weights(weights_path, by_name=True)
848 self.epoch = epoch_index

File ~/siamese-mask-rcnn/lib/Mask_RCNN/mrcnn/model.py:2115, in MaskRCNN.load_weights(self, filepath, by_name, exclude)
2112 layers = filter(lambda l: l.name not in exclude, layers)
2114 if by_name:
-> 2115 hdf5_format.load_weights_from_hdf5_group(f, layers)
2116 else:
2117 hdf5_format.load_weights_from_hdf5_group(f, layers)

File ~/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/hdf5_format.py:705, in load_weights_from_hdf5_group(f, layers)
702 weight_values = preprocess_weights_for_loading(
703 layer, weight_values, original_keras_version, original_backend)
704 if len(weight_values) != len(symbolic_weights):
--> 705 raise ValueError('Layer #' + str(k) + ' (named "' + layer.name +
706 '" in the current model) was found to '
707 'correspond to layer ' + name + ' in the save file. '
708 'However the new layer ' + layer.name + ' expects ' +
709 str(len(symbolic_weights)) +
710 ' weights, but the saved weights have ' +
711 str(len(weight_values)) + ' elements.')
712 weight_value_tuples += zip(symbolic_weights, weight_values)
713 backend.batch_set_value(weight_value_tuples)

ValueError: Layer #13 (named "mrcnn_class_logits" in the current model) was found to correspond to layer mrcnn_class_bn1 in the save file. However the new layer mrcnn_class_logits expects 2 weights, but the saved weights have 4 elements