Skip to content

Commit

Permalink
trained 4th iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
AmeyaWagh committed May 31, 2018
1 parent 0372021 commit efdf29b
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Train/
*.h5
logs/
*.zip
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Validation loss
|:-----------:|:---------:|:----:|:--------:|
| 10 | 40 | ![loss](./assets/loss_40.png) | ![val_loss](./assets/val_loss_40.png) |
| 40 | 100 | ![loss](./assets/loss2.png) | ![val_loss](./assets/val_loss2.png) |

| 10 | 20 | ![loss](./assets/loss3.png) | ![val_loss](./assets/val_loss3.png) |
| 20 | 60 | ![loss](./assets/loss4.png) | ![val_loss](./assets/val_loss4.png) |


## Reference
Expand Down
Binary file added assets/loss3.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 assets/loss4.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 assets/val_loss3.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 assets/val_loss4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 53 additions & 11 deletions inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
import matplotlib
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 @@ -40,7 +41,7 @@ class ShapesConfig(Config):
# 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).
GPU_COUNT = 1
IMAGES_PER_GPU = 4
IMAGES_PER_GPU = 1

# Number of classes (including background)
NUM_CLASSES = 1 + 2 # background + 3 shapes
Expand Down Expand Up @@ -97,6 +98,9 @@ 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 @@ -118,20 +122,41 @@ def segment_images(original_image):
final_img = cv2.addWeighted(final_img, 1, mask1.astype(np.uint8), 1, 0)
return final_img

# for image_id in range(900,1000):
# try:
# original_image = cv2.imread('./Train/CameraRGB/{}.png'.format(image_id))[:,:,::-1]

# final_img = segment_images(original_image)
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)

# cv2.imshow('output', final_img[:,:,::-1])
# cv2.waitKey(5)
# except KeyboardInterrupt as e:
# break
import sys, skvideo.io, json, base64


video = skvideo.io.vread('./test_video.mp4')
print(video.shape)

for rgb_frame in video:
try:
final_img = segment_images(rgb_frame)
Expand All @@ -140,4 +165,21 @@ def segment_images(original_image):
except KeyboardInterrupt as e:
break

# video_len = video.shape[0]
# offset=1
# a = time.time()
# for idx in range(0,video_len,4):
# # if video_len-idx >4:
# # offset = 4
# # else:
# # offset = video_len-idx
# # model.config.IMAGES_PER_GPU = offset
# rgb_frames = video[idx:idx+offset,:,:,:]
# print(rgb_frames.shape)
# final_img = segment_images_batch(rgb_frames)
# b=time.time()
# _secs = (b-a)%60

# print("FPS:",video_len/_secs)

exit()
27 changes: 14 additions & 13 deletions train_mrcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ShapesConfig(Config):
# 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).
GPU_COUNT = 1
IMAGES_PER_GPU = 2
IMAGES_PER_GPU = 1

# Number of classes (including background)
NUM_CLASSES = 1 + 2 # background + 3 shapes
Expand All @@ -65,7 +65,7 @@ class ShapesConfig(Config):
TRAIN_ROIS_PER_IMAGE = 32

# Use a small epoch since the data is simple
STEPS_PER_EPOCH = 500
STEPS_PER_EPOCH = 200

# use small validation steps since the epoch is small
VALIDATION_STEPS = 5
Expand All @@ -87,11 +87,12 @@ def load_images(self,dataset_dir,dataset_type='train'):
self.add_class("shapes", 2, "car")

if dataset_type=='train':
images = images[:800]
images = images[:900]
elif dataset_type=='val':
images = images[800:900]
else:
images = images[900:]
else:
# images = images[900:]
raise ValueError("param should be train or val")

for _image in images:
# image = skimage.io.imread(os.path.join(image_paths,_image))
Expand Down Expand Up @@ -179,7 +180,7 @@ def image_reference(self, image_id):
# dataset_test = lyftDataset()
# dataset_test.load_images(RGB_PATH,dataset_type='test')
# dataset_test.prepare()
augmentation = iaa.SomeOf((0, 2), [
augmentation = iaa.SomeOf((0, None), [
iaa.Fliplr(0.5),
iaa.Flipud(0.5),
iaa.OneOf([iaa.Affine(rotate=90),
Expand Down Expand Up @@ -214,13 +215,13 @@ def image_reference(self, image_id):

model.train(dataset_train, dataset_val,
learning_rate=config.LEARNING_RATE,
epochs=10,
epochs=20,
augmentation=augmentation,
layers='heads')

model.train(dataset_train, dataset_val,
learning_rate=config.LEARNING_RATE / 10,
epochs=20,
epochs=60,
augmentation=augmentation,
layers="all")

Expand Down Expand Up @@ -279,13 +280,13 @@ def segment_images(original_image):
final_img = cv2.addWeighted(final_img, 1, mask1.astype(np.uint8), 1, 0)
return final_img

for image_id in range(900,1000):
# for image_id in range(900,1000):

original_image = cv2.imread('./Train/CameraRGB/{}.png'.format(image_id))[:,:,::-1]
# original_image = cv2.imread('./Train/CameraRGB/{}.png'.format(image_id))[:,:,::-1]

final_img = segment_images(original_image)
# final_img = segment_images(original_image)

cv2.imshow('output', final_img[:,:,::-1])
cv2.waitKey(1)
# cv2.imshow('output', final_img[:,:,::-1])
# cv2.waitKey(1)

exit()

0 comments on commit efdf29b

Please sign in to comment.