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

Fix TF 2.x compatibility and Add code to get object labels #94

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions annotations/annotations_test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test1.jpg,0,0,10,10,object,3000,2000
test2.jpg,0,0,10,10,object,427,367
test3.jpg,0,0,10,10,object,363,481
Binary file added images/test1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/test2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/test3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def resize_images(images, size, method='bilinear', align_corners=False):
'bicubic' : tensorflow.image.ResizeMethod.BICUBIC,
'area' : tensorflow.image.ResizeMethod.AREA,
}
return tensorflow.image.resize_images(images, size, methods[method], align_corners)
return tensorflow.image.resize(images, size, methods[method], align_corners)


def non_max_suppression(*args, **kwargs):
Expand Down
Empty file.
8 changes: 4 additions & 4 deletions object_detector_retinanet/keras_retinanet/bin/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import os
import sys

import keras
import tensorflow.keras as keras
import tensorflow as tf


Expand All @@ -34,9 +34,9 @@
def get_session():
""" Construct a modified tf session.
"""
config = tf.ConfigProto()
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
return tf.Session(config=config)
return tf.compat.v1.Session(config=config)


def create_generator(args):
Expand Down Expand Up @@ -127,7 +127,7 @@ def main(args=None):
os.environ["CUDA_VISIBLE_DEVICES"] = str(666)
else:
os.environ["CUDA_VISIBLE_DEVICES"] = gpu_num
keras.backend.tensorflow_backend.set_session(get_session())
tf.compat.v1.keras.backend.set_session(get_session())

# make save path if it doesn't exist
if args.save_path is not None and not os.path.exists(args.save_path):
Expand Down
2 changes: 1 addition & 1 deletion object_detector_retinanet/keras_retinanet/initializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ def get_config(self):

def __call__(self, shape, dtype=None):
# set bias to -log((1 - p)/p) for foreground
result = np.ones(shape, dtype=dtype) * -math.log((1 - self.probability) / self.probability)
result = np.ones(shape, dtype=np.float32) * -math.log((1 - self.probability) / self.probability)

return result
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
limitations under the License.
"""

import keras
import tensorflow as tf
import tensorflow.keras as keras
import numpy

from .. import backend


def filter_detections(boxes, classification, other=[], nms=True, score_threshold=0.05, max_detections=300, nms_threshold=0.5):
""" Filter detections using the boxes and classification values.

Expand Down Expand Up @@ -60,9 +60,11 @@ def filter_detections(boxes, classification, other=[], nms=True, score_threshold
indices = keras.backend.gather(indices, nms_indices)

# add indices to list of all indices
labels = c * keras.backend.ones((keras.backend.shape(indices)[0],), dtype='int64')
init_shape = (keras.backend.shape(indices)[0],)
labels = c * tf.ones(init_shape, dtype='int64')
indices = keras.backend.stack([indices[:, 0], labels], axis=1)
all_indices.append(indices)


# concatenate indices to single tensor
indices = keras.backend.concatenate(all_indices, axis=0)
Expand Down
4 changes: 2 additions & 2 deletions object_detector_retinanet/keras_retinanet/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def load_model(filepath, backbone_name='resnet50', convert=False, nms=False):
ImportError: if h5py is not available.
ValueError: In case of an invalid savefile.
"""
import keras.models
import tensorflow.keras.models

model = keras.models.load_model(filepath, custom_objects=backbone(backbone_name).custom_objects)
model = tensorflow.keras.models.load_model(filepath, custom_objects=backbone(backbone_name).custom_objects)
if convert:
from object_detector_retinanet.keras_retinanet.models.retinanet import retinanet_bbox
model = retinanet_bbox(model=model, nms=nms)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
"""

import keras
import tensorflow.keras as keras
from keras.applications.densenet import DenseNet, get_file

from . import retinanet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
"""

import keras
import tensorflow.keras as keras
from keras.applications.mobilenet import MobileNet, BASE_WEIGHT_PATH, get_file, relu6, DepthwiseConv2D

from . import retinanet
Expand Down
2 changes: 1 addition & 1 deletion object_detector_retinanet/keras_retinanet/models/resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
"""

import keras
import tensorflow.keras as keras
import keras_resnet
import keras_resnet.models
from keras.utils import get_file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
"""

import keras
import tensorflow.keras as keras
from .. import initializers
from .. import layers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import threading
import warnings

import keras
import tensorflow.keras as keras

from ..utils.anchors import anchor_targets_bbox, bbox_transform
from ..utils.image import (
Expand Down
2 changes: 1 addition & 1 deletion object_detector_retinanet/keras_retinanet/utils/Boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def perform_nms_on_image_dataframe(image_data, overlap_threshold=0.5):
number_of_images = len(image_data['image_name'].unique())
if number_of_images > 1:
print('nms received data including more than 1 image - cannot perform nms!')
image_boxes = image_data.as_matrix(BOX_CONSTANTS)
image_boxes = image_data.to_numpy()
image_scores = numpy.array(image_data['confidence'])

nms_boxes, nms_scores, deleted_indices = non_maximal_suppression(image_boxes, image_scores,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ def __init__(self, sec):
self.sec = sec

def __enter__(self):
signal.signal(signal.SIGALRM, self.raise_timeout)
signal.setitimer(signal.ITIMER_REAL, self.sec)
# signal.signal(signal.SIGALRM, self.raise_timeout)
# signal.setitimer(signal.ITIMER_REAL, self.sec)
pass

def __exit__(self, *args):
signal.alarm(0) # disable alarm
# signal.alarm(0) # disable alarm
pass

def raise_timeout(self, *args):
raise Timeout.Timeout()
Expand Down
15 changes: 9 additions & 6 deletions object_detector_retinanet/keras_retinanet/utils/EmMerger.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def filter_duplicate_candidates(self, data, image):
cv2.normalize(heat_map, heat_map, 0, 255, cv2.NORM_MINMAX)
heat_map = cv2.convertScaleAbs(heat_map)
h2, heat_map = cv2.threshold(heat_map, 4, 255, cv2.THRESH_TOZERO)
# cv2.imshow("Heat map", heat_map)
# cv2.waitKey()
contours = cv2.findContours(numpy.ndarray.copy(heat_map), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

candidates = self.find_new_candidates(contours, heat_map, data, original_detection_centers, image)
Expand All @@ -75,13 +77,12 @@ def filter_duplicate_candidates(self, data, image):
filtered_data = pandas.DataFrame(columns=data.columns)
for i, candidate in candidates.items():
label = candidate['original_detection_ids']
original_detections = data.ix[label]
original_detections[
'avg_score'] = 0.5 * original_detections.confidence + 0.5 * original_detections.hard_score
original_detections = data.iloc[label]
original_detections.insert(0, 'avg_score', 0.5 * original_detections.confidence + 0.5 * original_detections.hard_score, True)
best_detection_id = original_detections.avg_score.argmax()
# best_detection_id = original_detections.confidence.argmax()
# best_detection_id = original_detections.hard_score.argmax()
best_detection = original_detections.ix[best_detection_id].copy()
best_detection = original_detections.iloc[best_detection_id].copy()

# The following code creates the median bboxes
# original_detections = original_detections[original_detections.confidence > 0.5]
Expand Down Expand Up @@ -110,7 +111,7 @@ def filter_duplicate_candidates(self, data, image):

def find_new_candidates(self, contours, heat_map, data, original_detection_centers, image):
candidates = []
for contour_i, contour in enumerate(contours[1]):
for contour_i, contour in enumerate(contours[0]):
contour_bounding_rect = cv2.boundingRect(contour)

contour_bbox = extract_boxes_from_edge_boxes(numpy.array(contour_bounding_rect))[0]
Expand Down Expand Up @@ -200,8 +201,9 @@ def remove_redundant(self, contour_bbox, cov, k, mu, image, sub_heat_map):
ellipse_mask = cv2.fillPoly(local_m, [poly], (1, 1, 1))
contours = cv2.findContours(ellipse_mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts.append(contours[1][0])
cnts.append(contours[0][0])
center_points = mu.copy()

distances = scipy.spatial.distance.cdist(center_points, center_points)
scaled_distances = numpy.ndarray(shape=[k, k], dtype=numpy.float64)
for i in range(0, k):
Expand Down Expand Up @@ -368,6 +370,7 @@ def merge_detections(image_name, results):
result_df['y2'] = results[:, 3].astype(int)
result_df['confidence'] = results[:, 4]
result_df['hard_score'] = results[:, 5]
result_df['classification'] = results[:, 6]
result_df['uuid'] = 'object_label'
result_df['label_type'] = 'object_label'
# result_df['project'] = project
Expand Down
2 changes: 1 addition & 1 deletion object_detector_retinanet/keras_retinanet/utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""

from __future__ import division
import keras
import tensorflow.keras as keras
import numpy as np
import cv2
from PIL import Image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from __future__ import print_function

import keras
import tensorflow.keras as keras
import sys

minimum_keras_version = 2, 1, 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def predict(
hard_score_rate=1.):
all_detections = [[None for i in range(generator.num_classes())] for j in range(generator.size())]
csv_data_lst = []
csv_data_lst.append(['image_id', 'x1', 'y1', 'x2', 'y2', 'confidence', 'hard_score'])
csv_data_lst.append(['image_id', 'x1', 'y1', 'x2', 'y2', 'confidence', 'hard_score', 'classification'])
result_dir = os.path.join(root_dir(), 'results')
create_folder(result_dir)
timestamp = datetime.datetime.utcnow()
res_file = result_dir + '/detections_output_iou_{}_{}.csv'.format(hard_score_rate, timestamp)
res_file = result_dir + '/detections_output_iou_{}.csv'.format(hard_score_rate)
for i in range(generator.size()):
image_name = os.path.join(generator.image_path(i).split(os.path.sep)[-2],
generator.image_path(i).split(os.path.sep)[-1])
Expand Down Expand Up @@ -89,7 +89,7 @@ def predict(
filtered_scores.append(detection['confidence'])
filtered_labels.append('{0:.2f}'.format(detection['hard_score']))
row = [image_name, detection['x1'], detection['y1'], detection['x2'], detection['y2'],
detection['confidence'], detection['hard_score']]
detection['confidence'], detection['hard_score'], generator.labels[int(detection['classification'])]]
csv_data_lst.append(row)

if save_path is not None:
Expand Down
2 changes: 1 addition & 1 deletion object_detector_retinanet/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def root_dir():
if platform.system() == 'Linux':
return os.path.join(os.getenv('HOME'), 'Documents', 'SKU110K')
elif platform.system() == 'Windows':
return os.path.abspath('C:/Users/{}/Documents/SKU110K/'.format(os.getenv('username')))
return os.path.abspath('C:/Users/{}/Desktop/SKU110K_CVPR19/'.format(os.getenv('username')))


def image_path():
Expand Down
15 changes: 8 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Keras==2.2.5
keras-retinanet==0.5.1
tensorflow-gpu==1.15.4
numpy==1.19.2
opencv-python==3.1.0.5
tqdm==4.50.2
pandas==0.23.4
keras==2.4.3
keras_resnet
tensorflow==2.4.0
numpy==1.19.5
opencv-python==4.5.1
opencv-contrib-python==4.5.1
tqdm
pandas
Binary file added res_images_iou/0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res_images_iou/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res_images_iou/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading