Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AmeyaWagh committed Jun 3, 2018
1 parent c53a472 commit c85bb6d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 150 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Examples of augmentation are given in [https://github.com/matterport/Mask_RCNN](

#### Training Loss

The model was trained using pretrained CoCo weights.
Instead of a single training loop, it was trained multiple times in smaller epochs to observe change in the loss with changes in parameters and to avoid overfitting. As the data was less, the network used to saturate quickly and required more augmentations to proceed. Also i did not wanted to go overboard on the augmentation so was observing which one works best. Below are the logs of final training setting with the above given augmentation.

| heads Epoch | all Epoch | loss | val_loss |
Expand Down
40 changes: 4 additions & 36 deletions inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
import matplotlib.pyplot as plt
import skimage.io
import time
# from imgaug import augmenters as iaa

# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

ROOT_DIR = os.path.abspath("./")
MODEL_DIR = os.path.join('./', "logs")
Expand All @@ -31,7 +30,7 @@



class ShapesConfig(Config):
class LyftChallengeConfig(Config):
"""Configuration for training on the toy shapes dataset.
Derives from the base Config class and overrides values specific
to the toy shapes dataset.
Expand Down Expand Up @@ -68,10 +67,10 @@ class ShapesConfig(Config):



config = ShapesConfig()
config = LyftChallengeConfig()
config.display()

class InferenceConfig(ShapesConfig):
class InferenceConfig(LyftChallengeConfig):
GPU_COUNT = 1
IMAGES_PER_GPU = 1

Expand Down Expand Up @@ -99,9 +98,6 @@ class InferenceConfig(ShapesConfig):

def segment_images(original_image):
results = model.detect([original_image], verbose=0)
# print ("-"*80)
# print(len(results))
# print ("-"*80)
r = results[0]
f_mask = r['masks']
f_class = r["class_ids"]
Expand All @@ -123,34 +119,6 @@ def segment_images(original_image):
final_img = cv2.addWeighted(final_img, 1, mask1.astype(np.uint8), 1, 0)
return final_img

def segment_images_batch(original_images):
results = model.detect(original_images, verbose=0)
print ("-"*80)
print(len(results))
print ("-"*80)
images = []
for idx,res in enumerate(results):
r = res
f_mask = r['masks']
f_class = r["class_ids"]


no_ch = f_mask.shape[2]
final_img = np.copy(original_images[idx,:,:,:])
for ch in range(no_ch):

_id = f_class[ch]
if _id==1:
color_id=0
else:
color_id=1
mask_1 = f_mask[:,:,ch]
mask1 = np.dstack([mask_1*colors[color_id][0],
mask_1*colors[color_id][1],
mask_1*colors[color_id][2]])
final_img = cv2.addWeighted(final_img, 1, mask1.astype(np.uint8), 1, 0)
images.append(final_img)
return np.dstack(images)

import sys, skvideo.io, json, base64

Expand Down
28 changes: 8 additions & 20 deletions test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

ROOT_DIR = os.path.abspath("./Lyft_challenge")
MODEL_DIR = os.path.join('./Lyft_challenge', "logs")
ROOT_DIR = os.path.abspath("./")
MODEL_DIR = os.path.join('./', "logs")

sys.path.append(ROOT_DIR) # To find local version of the library
sys.path.append(os.path.join(os.getcwd(),"./Lyft_challenge/Mask_RCNN/"))
sys.path.append(os.path.join(os.getcwd(),"./Mask_RCNN/"))

from mrcnn.config import Config
from mrcnn import utils
Expand All @@ -21,13 +21,13 @@
from mrcnn.model import log


class ShapesConfig(Config):
class LyftChallengeConfig(Config):
"""Configuration for training on the toy shapes dataset.
Derives from the base Config class and overrides values specific
to the toy shapes dataset.
"""
# Give the configuration a recognizable name
NAME = "shapes"
NAME = "lyft_perception_challenge"

# Train on 1 GPU and 8 images per GPU. We can put multiple images on each
# GPU because the images are small. Batch size is 8 (GPUs * images/GPU).
Expand Down Expand Up @@ -57,11 +57,11 @@ class ShapesConfig(Config):
VALIDATION_STEPS = 5


config = ShapesConfig()
config = LyftChallengeConfig()
# config.display()


class InferenceConfig(ShapesConfig):
class InferenceConfig(LyftChallengeConfig):
GPU_COUNT = 1
IMAGES_PER_GPU = 1

Expand All @@ -77,7 +77,7 @@ class InferenceConfig(ShapesConfig):
config=inference_config,
model_dir=MODEL_DIR)

model_path = os.path.join('./Lyft_challenge', "mask_rcnn_lyft.h5")
model_path = os.path.join('./', "mask_rcnn_lyft.h5")
assert model_path != "", "Provide path to trained weights"
# print("Loading weights from ", model_path)
model.load_weights(model_path, by_name=True)
Expand All @@ -101,12 +101,6 @@ def segment_image(image_frame):

return car_mask,road_mask

# Define encoder function
# def encode(array):
# pil_img = Image.fromarray(array)
# buff = BytesIO()
# pil_img.save(buff, format="PNG")
# return base64.b64encode(buff.getvalue()).decode("utf-8")

def encode(array):
retval, buffer = cv2.imencode('.png', array)
Expand All @@ -121,13 +115,7 @@ def encode(array):

for rgb_frame in video:

# Grab red channel
# red = rgb_frame[:,:,0]
# Look for red cars :)
#

# Look for road :)
#
car_mask,road_mask = segment_image(rgb_frame)
binary_car_result = car_mask*1
binary_road_result = road_mask*1
Expand Down
Loading

0 comments on commit c85bb6d

Please sign in to comment.