Skip to content

Instantly share code, notes, and snippets.

View tkshnkmr's full-sized avatar

tkshnkmr

  • London, UK
View GitHub Profile
import numpy as np
import matplotlib.pyplot as plt
# image modules
from PIL import Image
import matplotlib.image as mpimg
import cv2
# PyTorch
import torch
from torch.utils import data
from torchvision import transforms
import numpy as np
import matplotlib.pyplot as plt
# Input x, output y
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)
# Creates figure first
my_dpi = 200
fig = plt.figure(figsize=(4, 2), dpi=my_dpi)
import numpy as np
import matplotlib.pyplot as plt
# Input x, output y
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)
# Creates figure first
my_dpi = 40
fig = plt.figure(figsize=(20, 10), dpi=my_dpi)
import numpy as np
import matplotlib.pyplot as plt
# Input x, output y
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)
# make multiple subplots.
# you can define the size of figure and dpi (dot per inch, defalt dpi=72)
my_dpi = 50
import numpy as np
import matplotlib.pyplot as plt
# Input x, output y
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)
# Creates fig and ax from subplots().
# But only create a single plot
fig, ax = plt.subplots()
import numpy as np
import matplotlib.pyplot as plt
# Input x, output y
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)
# Plot
plt.plot(x, y)
# Change the fontsize of ticks
plt.xticks(fontsize=8)
# data loader
batch_size = 1
num_workers = 4
data_loader = torch.utils.data.DataLoader(my_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers
)
# data directory
root = "my_data"
# assume we have 3 jpg images
filenames = ['img1.jpg', 'img2.jpg', 'img3.jpg']
# the class of image might be ['black cat', 'tabby cat', 'tabby cat']
labels = [0, 1, 1]
# create own Dataset
import os
import torch
from torch.utils import data
from PIL import Image
from torchvision import transforms
class simpleDataset(data.Dataset):
# initialise function of class
def __init__(self, root, filenames, labels):
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
def get_model_instance_segmentation(num_classes):
# load an instance segmentation model pre-trained pre-trained on COCO
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=False)
# get number of input features for the classifier
in_features = model.roi_heads.box_predictor.cls_score.in_features
# replace the pre-trained head with a new one
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)