-
Notifications
You must be signed in to change notification settings - Fork 7.2k
MS Build casting error #951
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import numpy as np | ||
import torchvision | ||
import skimage.io | ||
import colorsys | ||
import matplotlib | ||
import random | ||
import matplotlib.patches as patches | ||
from matplotlib import pyplot as plt | ||
from skimage.measure import find_contours | ||
from PIL import Image | ||
from matplotlib.patches import Polygon | ||
|
||
# COCO Class names | ||
# Index of the class in the list is its ID. For example, to get ID of | ||
# the teddy bear class, use: class_names.index('teddy bear') | ||
class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', | ||
'bus', 'train', 'truck', 'boat', 'traffic light', | ||
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', | ||
'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', | ||
'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', | ||
'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', | ||
'kite', 'baseball bat', 'baseball glove', 'skateboard', | ||
'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', | ||
'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', | ||
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', | ||
'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', | ||
'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', | ||
'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', | ||
'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', | ||
'teddy bear', 'hair drier', 'toothbrush'] | ||
|
||
def apply_mask(image, mask, color, alpha=0.5): | ||
"""Apply the given mask to the image. | ||
""" | ||
for c in range(3): | ||
image[:, :, c] = np.where(mask >= 0.5, | ||
image[:, :, c] * | ||
(1 - alpha) + alpha * color[c] * 255, | ||
image[:, :, c]) | ||
return image | ||
|
||
def random_colors(N, bright=True): | ||
""" | ||
Generate random colors. | ||
To get visually distinct colors, generate them in HSV space then | ||
convert to RGB. | ||
""" | ||
brightness = 1.0 if bright else 0.7 | ||
hsv = [(i / N, 1, brightness) for i in range(N)] | ||
colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv)) | ||
random.shuffle(colors) | ||
return colors | ||
|
||
def display_instances(image, boxes, masks, class_ids, class_names, | ||
scores=None, title="", | ||
figsize=(16, 16), ax=None): | ||
""" | ||
boxes: [num_instance, (y1, x1, y2, x2, class_id)] in image coordinates. | ||
masks: [height, width, num_instances] | ||
class_ids: [num_instances] | ||
class_names: list of class names of the dataset | ||
scores: (optional) confidence scores for each box | ||
figsize: (optional) the size of the image. | ||
""" | ||
# Number of instances | ||
N = boxes.shape[0] | ||
if not N: | ||
print("\n*** No instances to display *** \n") | ||
|
||
if not ax: | ||
_, ax = plt.subplots(1, figsize=figsize) | ||
|
||
# Generate random colors | ||
colors = random_colors(N) | ||
|
||
# Show area outside image boundaries. | ||
height, width = image.shape[:2] | ||
ax.set_ylim(height + 10, -10) | ||
ax.set_xlim(-10, width + 10) | ||
ax.axis('off') | ||
ax.set_title(title) | ||
|
||
masked_image = image.astype(np.uint32).copy() | ||
for i in range(N): | ||
color = colors[i] | ||
|
||
score = scores[i] if scores is not None else None | ||
if score < 0.4: | ||
continue | ||
|
||
x1, y1, x2, y2, = boxes[i] | ||
p = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2, | ||
alpha=0.7, linestyle="dashed", | ||
edgecolor=color, facecolor='none') | ||
ax.add_patch(p) | ||
|
||
# Label | ||
class_id = class_ids[i] | ||
label = class_names[class_id] | ||
caption = "{} {:.3f}".format(label, score) if score else label | ||
ax.text(x1, y1 + 8, caption, | ||
color='w', size=11, backgroundcolor="none") | ||
|
||
# Mask | ||
mask = masks[i, :, :].detach().squeeze(-1) | ||
masked_image = apply_mask(masked_image, mask, color) | ||
|
||
# Mask Polygon | ||
# Pad to ensure proper polygons for masks that touch image edges. | ||
padded_mask = np.zeros( | ||
(mask.shape[1] + 2, mask.shape[2] + 2), dtype=np.uint8) | ||
padded_mask[1:-1, 1:-1] = mask | ||
contours = find_contours(padded_mask, 0.5) | ||
for verts in contours: | ||
# Subtract the padding and flip (y, x) to (x, y) | ||
verts = np.fliplr(verts) - 1 | ||
p = Polygon(verts, facecolor="none", edgecolor=color) | ||
ax.add_patch(p) | ||
ax.imshow(masked_image.astype(np.uint8)) | ||
plt.show() | ||
|
||
model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True) | ||
# set it to evaluation mode, as the model behaves differently | ||
# during training and during evaluation | ||
model.eval() | ||
|
||
image = Image.open('test.jpg') | ||
image_tensor = torchvision.transforms.functional.to_tensor(image) | ||
|
||
output = model([image_tensor]) | ||
|
||
img = skimage.io.imread('test.jpg') | ||
|
||
# Visualize results | ||
r = output[0] | ||
display_instances(img, r['boxes'], r['masks'], r['labels'], | ||
class_names, r['scores']) | ||
plt.show() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this file is completely unrelated to the windows build, right? I think it's a good idea to have it in the repo, but as a separate PR so that we can focus on how to better illustrate this.