|
|
from os import listdir |
|
|
from xml.etree import ElementTree |
|
|
import json |
|
|
from numpy import zeros |
|
|
from numpy import asarray |
|
|
from bboxcnn.utils import Dataset |
|
|
from bboxcnn.config import Config |
|
|
from bboxcnn.model import BBoxCNN |
|
|
|
|
|
class TABLE_Dataset(Dataset): |
|
|
|
|
|
def load_dataset(self, dataset_dir, is_train=True): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.add_class("dataset", 1, "inductor") |
|
|
self.add_class("dataset", 2, "capacitor") |
|
|
self.add_class("dataset", 3, "resistor") |
|
|
self.add_class("dataset", 4, "Active_IC") |
|
|
self.add_class("dataset", 5, "fuse") |
|
|
self.add_class("dataset", 6, "pnp") |
|
|
self.add_class("dataset", 7, "npn") |
|
|
self.add_class("dataset", 8, "crystal") |
|
|
self.add_class("dataset", 9, "pwr_connector") |
|
|
self.add_class("dataset", 10, "diode") |
|
|
self.add_class("dataset", 11, "connectors") |
|
|
self.add_class("dataset", 12, "switch") |
|
|
self.add_class("dataset", 13, "headers") |
|
|
self.add_class("dataset", 14, "pmos") |
|
|
self.add_class("dataset", 15, "nmos") |
|
|
self.add_class("dataset", 16, "led") |
|
|
self.add_class("dataset", 17, "pwr") |
|
|
self.add_class("dataset", 18, "gnd") |
|
|
|
|
|
images_dir = dataset_dir + '/images/' |
|
|
annotations_dir = dataset_dir + '/annots/' |
|
|
print(f"image directory: {images_dir}") |
|
|
|
|
|
for filename in listdir(images_dir): |
|
|
|
|
|
image_id = filename[:-4] |
|
|
print(filename) |
|
|
|
|
|
if image_id in ['017999']: |
|
|
continue |
|
|
|
|
|
if is_train and int(image_id) >= 130: |
|
|
continue |
|
|
|
|
|
if not is_train and int(image_id) < 130: |
|
|
continue |
|
|
img_path = images_dir + filename |
|
|
ann_path = annotations_dir + image_id + '.json' |
|
|
|
|
|
self.add_image('dataset', image_id=image_id, path=img_path, annotation=ann_path) |
|
|
|
|
|
|
|
|
def extract_boxes(self, filename): |
|
|
|
|
|
with open(filename, 'r') as f: |
|
|
data = json.load(f) |
|
|
boxes = list() |
|
|
bndboxes = [i['bndbox'] for i in data['object']] |
|
|
class_names = [i['name'] for i in data['object']] |
|
|
for box in bndboxes: |
|
|
xmin = int(box['xmin']) |
|
|
ymin = int(box['ymin']) |
|
|
xmax = int(box['xmax']) |
|
|
ymax = int(box['ymax']) |
|
|
coors = [xmin, ymin, xmax, ymax] |
|
|
boxes.append(coors) |
|
|
|
|
|
width = int(data['size']['width']) |
|
|
height = int(data['size']['height']) |
|
|
return boxes, class_names, width, height |
|
|
|
|
|
|
|
|
def load_mask(self, image_id): |
|
|
|
|
|
info = self.image_info[image_id] |
|
|
|
|
|
path = info['annotation'] |
|
|
|
|
|
boxes, class_names, w, h = self.extract_boxes(path) |
|
|
|
|
|
masks = zeros([h, w, len(boxes)], dtype='uint8') |
|
|
|
|
|
class_ids = list() |
|
|
for i, entity in enumerate(zip(boxes, class_names)): |
|
|
box, class_name = entity |
|
|
row_s, row_e = box[1], box[3] |
|
|
col_s, col_e = box[0], box[2] |
|
|
masks[row_s:row_e, col_s:col_e, i] = i+1 |
|
|
class_ids.append(self.class_names.index(class_name)) |
|
|
return masks, asarray(class_ids, dtype='int32') |
|
|
|
|
|
|
|
|
def image_reference(self, image_id): |
|
|
info = self.image_info[image_id] |
|
|
return info['path'] |
|
|
|
|
|
|
|
|
class TABLE_Config(Config): |
|
|
|
|
|
NAME = "EC_cfg" |
|
|
|
|
|
NUM_CLASSES = 1 + 18 |
|
|
|
|
|
STEPS_PER_EPOCH = 50 |
|
|
|
|
|
|
|
|
|
|
|
train_set = TABLE_Dataset() |
|
|
train_set.load_dataset('/visual_nlp/data/final/train', is_train=True) |
|
|
train_set.prepare() |
|
|
print('Train: %d' % len(train_set.image_ids)) |
|
|
|
|
|
test_set = TABLE_Dataset() |
|
|
test_set.load_dataset('/visual_nlp/data/final/test', is_train=False) |
|
|
test_set.prepare() |
|
|
print('Test: %d' % len(test_set.image_ids)) |
|
|
|
|
|
config = TABLE_Config() |
|
|
config.display() |
|
|
|
|
|
model = BBoxCNN(mode='training', model_dir='./', config=config) |
|
|
|
|
|
|
|
|
model.load_weights('bboxcnn_base.h5', by_name=True, exclude=["bboxcnn_class_logits", "bboxcnn_bbox_fc", "bboxcnn_bbox", "bboxcnn_mask"]) |
|
|
|
|
|
model.train(train_set, test_set, learning_rate=config.LEARNING_RATE, epochs=150, layers='heads') |